dolibarr  13.0.2
bom.php
1 <?php
2 /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2020 Floiran Henry <florian.henry@scopen.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 require '../../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
31 
32 // Load translation files required by the page
33 $langs->loadLangs(array('mrp', 'products', 'companies'));
34 
35 $id = GETPOST('id', 'int');
36 $ref = GETPOST('ref', 'alpha');
37 
38 // Security check
39 $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
40 $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
41 if ($user->socid) $socid = $user->socid;
42 $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
43 
44 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
45 $hookmanager->initHooks(array('productstatscontract'));
46 
47 $mesg = '';
48 $option = '';
49 
50 // Load variable for pagination
51 $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
52 $sortfield = GETPOST("sortfield", 'alpha');
53 $sortorder = GETPOST("sortorder", 'alpha');
54 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
55 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
56 $offset = $limit * $page;
57 $pageprev = $page - 1;
58 $pagenext = $page + 1;
59 if (!$sortorder) $sortorder = "DESC";
60 if (!$sortfield) $sortfield = "b.date_valid";
61 
62 
63 /*
64  * View
65  */
66 
67 $form = new Form($db);
68 
69 if ($id > 0 || !empty($ref))
70 {
71  $product = new Product($db);
72  $result = $product->fetch($id, $ref);
73 
74  $object = $product;
75 
76  $parameters = array('id'=>$id);
77  $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
78  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
79 
80  llxHeader("", "", $langs->trans("CardProduct".$product->type));
81 
82  if ($result > 0)
83  {
84  $head = product_prepare_head($product);
85  $titre = $langs->trans("CardProduct".$product->type);
86  $picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
87  print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
88 
89  $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
90  print $hookmanager->resPrint;
91  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
92 
93  $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
94 
95  $shownav = 1;
96  if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) $shownav = 0;
97 
98  dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
99 
100  print '<div class="fichecenter">';
101 
102  print '<div class="underbanner clearboth"></div>';
103  print '<table class="border tableforfield" width="100%">';
104 
105  $nboflines = show_stats_for_company($product, $socid);
106 
107  print "</table>";
108 
109  print '</div>';
110  print '<div style="clear:both"></div>';
111 
113 
114  $now = dol_now();
115 
116  //Calcul total qty and amount for global if full scan list
117  $total_qty_toconsume = 0;
118  $total_qty_toproduce = 0;
119  $bom_data_result = array();
120 
121 
122  //Qauntity to produce
123  $sql = "SELECT b.rowid as rowid, b.ref, b.status, b.date_valid,";
124  $sql .= " b.qty as qty_toproduce";
125  $sql .= " FROM ".MAIN_DB_PREFIX."bom_bom as b";
126  $sql .= " WHERE ";
127  $sql .= " b.entity IN (".getEntity('bom').")";
128  $sql .= " AND b.fk_product =".$product->id;
129  $sql .= $db->order($sortfield, $sortorder);
130 
131  // Count total nb of records
132  $totalofrecords = '';
133  if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
134  {
135  $result = $db->query($sql);
136  if ($result) {
137  $totalofrecords = $db->num_rows($result);
138  while ($objp = $db->fetch_object($result)) {
139  $total_qty_toproduce += $objp->qty_toproduce;
140  }
141  } else {
142  dol_print_error($db);
143  }
144  }
145  $sql .= $db->plimit($limit + 1, $offset);
146 
147  $result = $db->query($sql);
148  if ($result) {
149  $bomtmp = new BOM($db);
150  $num = $db->num_rows($result);
151  $i = 0;
152  if ($num > 0) {
153  while ($i < min($num, $limit)) {
154  $objp = $db->fetch_object($result);
155  $bomtmp->id = $objp->rowid;
156  $bomtmp->ref = $objp->ref;
157  $bom_data_result[$objp->rowid]['link'] = $bomtmp->getNomUrl(1, 'production');
158  $bom_data_result[$objp->rowid]['qty_toproduce'] += ($objp->qty_toproduce > 0 ? $objp->qty_toproduce : 0);
159  $bom_data_result[$objp->rowid]['qty_toconsume'] = 0;
160  $bom_data_result[$objp->rowid]['date_valid'] = dol_print_date($db->jdate($objp->date_valid), 'dayhour');
161  $bom_data_result[$objp->rowid]['status'] = $bomtmp->LibStatut($objp->status, 5);
162  $i++;
163  }
164  }
165  } else {
166  dol_print_error($db);
167  }
168  $db->free($result);
169 
170  //Qauntity to consume
171  $sql = "SELECT b.rowid as rowid, b.ref, b.status, b.date_valid,";
172  $sql .= " SUM(bl.qty) as qty_toconsume";
173  $sql .= " FROM ".MAIN_DB_PREFIX."bom_bom as b";
174  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."bom_bomline as bl ON bl.fk_bom=b.rowid";
175  $sql .= " WHERE ";
176  $sql .= " b.entity IN (".getEntity('bom').")";
177  $sql .= " AND bl.fk_product=".$product->id;
178  $sql .= " GROUP BY b.rowid, b.ref, b.date_valid, b.status";
179  $sql .= $db->order($sortfield, $sortorder);
180 
181  // Count total nb of records
182  $totalofrecords = '';
183  if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
184  {
185  $result = $db->query($sql);
186  if ($result) {
187  $totalofrecords = $db->num_rows($result);
188  while ($objp = $db->fetch_object($result)) {
189  $total_qty_toconsume += $objp->qty_toconsume;
190  }
191  } else {
192  dol_print_error($db);
193  }
194  }
195  $sql .= $db->plimit($limit + 1, $offset);
196 
197  $result = $db->query($sql);
198  if ($result) {
199  $bomtmp = new BOM($db);
200  $num = $db->num_rows($result);
201  $i = 0;
202  if ($num > 0) {
203  while ($i < min($num, $limit)) {
204  $objp = $db->fetch_object($result);
205  $bomtmp->id = $objp->rowid;
206  $bomtmp->ref = $objp->ref;
207 
208  if (!array_key_exists($objp->rowid, $bom_data_result)) {
209  $bom_data_result[$objp->rowid]['link'] = $bomtmp->getNomUrl(1, 'production');
210  $bom_data_result[$objp->rowid]['qty_toproduce'] = 0;
211  $bom_data_result[$objp->rowid]['qty_toconsume'] += ($objp->qty_toconsume > 0 ? $objp->qty_toconsume : 0);
212  $bom_data_result[$objp->rowid]['date_valid'] = dol_print_date($db->jdate($objp->date_valid), 'dayhour');
213  $bom_data_result[$objp->rowid]['status'] = $bomtmp->LibStatut($objp->status, 5);
214  } else {
215  $bom_data_result[$objp->rowid]['qty_toconsume'] += ($objp->qty_toconsume > 0 ? $objp->qty_toconsume : 0);
216  }
217  $i++;
218  }
219  }
220  } else {
221  dol_print_error($db);
222  }
223  $db->free($result);
224 
225 
226  if ($limit > 0 && $limit != $conf->liste_limit) $option .= '&limit='.urlencode($limit);
227  if (!empty($id)) $option .= '&id='.$product->id;
228  if (!empty($search_month)) $option .= '&search_month='.urlencode($search_month);
229  if (!empty($search_year)) $option .= '&search_year='.urlencode($search_year);
230 
231  print '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
232  if (!empty($sortfield))
233  print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
234  if (!empty($sortorder))
235  print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
236 
237  print_barre_liste($langs->trans("BOMs"), $page, $_SERVER["PHP_SELF"], $option, $sortfield, $sortorder, '', count($bom_data_result), count($bom_data_result), '', 0, '', '', $limit, 0, 0, 1);
238 
239  if (!empty($page)) $option .= '&page='.urlencode($page);
240 
241  print '<div class="div-table-responsive">';
242  print '<table class="tagtable liste listwithfilterbefore" width="100%">';
243 
244  print '<tr class="liste_titre">';
245  print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "b.rowid", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
246  print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "b.date_valid", "", "&amp;id=".$product->id, 'align="center"', $sortfield, $sortorder);
247  print_liste_field_titre("RowMaterial", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
248  print_liste_field_titre("Finished", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
249  print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "b.status", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
250  print "</tr>\n";
251 
252  if (!empty($bom_data_result)) {
253  foreach ($bom_data_result as $data)
254  {
255  print '<tr class="oddeven">';
256  print '<td>';
257  print $data['link'];
258  print "</td>\n";
259  print "<td align=\"center\">";
260  print $data['date_valid']."</td>";
261  print '<td class="center">'.$data['qty_toconsume'].'</td>';
262  print '<td class="center">'.$data['qty_toproduce'].'</td>';
263  print '<td class="center">'.$data['status'].'</td>';
264  print "</tr>\n";
265  }
266  print '</table>';
267  print '</div>';
268  print '</form>';
269  }
270  }
271 } else {
272  dol_print_error();
273 }
274 
275 // End of page
276 llxFooter();
277 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class for BOM.
Definition: bom.class.php:32
Class to manage products or services.
dol_now($mode= 'auto')
Return date for now.
const TYPE_SERVICE
Service.
show_stats_for_company($product, $socid)
Show stats for company.
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
print_barre_liste($titre, $page, $file, $options= '', $sortfield= '', $sortorder= '', $morehtmlcenter= '', $num=-1, $totalnboflines= '', $picto= 'generic', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limit=-1, $hideselectlimit=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow= '')
Print a title with navigation controls for pagination.
Class to manage generation of HTML components Only common components must be here.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_banner_tab($object, $paramid, $morehtml= '', $shownav=1, $fieldid= 'rowid', $fieldref= 'ref', $morehtmlref= '', $moreparam= '', $nodbprefix=0, $morehtmlleft= '', $morehtmlstatus= '', $onlybanner=0, $morehtmlright= '')
Show tab footer of a card.
llxFooter()
Empty footer.
Definition: wrapper.php:59
product_prepare_head($object)
Prepare array with list of tabs.
Definition: product.lib.php:35