dolibarr  16.0.1
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) {
42  $socid = $user->socid;
43 }
44 
45 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
46 $hookmanager->initHooks(array('productstatsbom'));
47 
48 $mesg = '';
49 $option = '';
50 
51 // Load variable for pagination
52 $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
53 $sortfield = GETPOST('sortfield', 'aZ09comma');
54 $sortorder = GETPOST('sortorder', 'aZ09comma');
55 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
56 if (empty($page) || $page == -1) {
57  $page = 0;
58 } // If $page is not defined, or '' or -1
59 $offset = $limit * $page;
60 $pageprev = $page - 1;
61 $pagenext = $page + 1;
62 if (!$sortorder) {
63  $sortorder = "DESC";
64 }
65 if (!$sortfield) {
66  $sortfield = "b.date_valid";
67 }
68 
69 $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
70 
71 
72 /*
73  * View
74  */
75 
76 $form = new Form($db);
77 
78 if ($id > 0 || !empty($ref)) {
79  $product = new Product($db);
80  $result = $product->fetch($id, $ref);
81 
82  $object = $product;
83 
84  $parameters = array('id'=>$id);
85  $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
86  if ($reshook < 0) {
87  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
88  }
89 
90  llxHeader("", "", $langs->trans("CardProduct".$product->type));
91 
92  if ($result > 0) {
93  $head = product_prepare_head($product);
94  $titre = $langs->trans("CardProduct".$product->type);
95  $picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
96  print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
97 
98  $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
99  print $hookmanager->resPrint;
100  if ($reshook < 0) {
101  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
102  }
103 
104  $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
105 
106  $shownav = 1;
107  if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) {
108  $shownav = 0;
109  }
110 
111  dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
112 
113  print '<div class="fichecenter">';
114 
115  print '<div class="underbanner clearboth"></div>';
116  print '<table class="border tableforfield" width="100%">';
117 
118  $nboflines = show_stats_for_company($product, $socid);
119 
120  print "</table>";
121 
122  print '</div>';
123  print '<div style="clear:both"></div>';
124 
125  print dol_get_fiche_end();
126 
127  $now = dol_now();
128 
129  //Calcul total qty and amount for global if full scan list
130  $total_qty_toconsume = 0;
131  $total_qty_toproduce = 0;
132  $product_cache=array();
133  $bom_data_result = array();
134 
135  //Qauntity to produce
136  $sql = "SELECT b.rowid as rowid, b.ref, b.status, b.date_valid, b.fk_product,";
137  $sql .= " b.qty as qty_toproduce";
138  $sql .= " FROM ".MAIN_DB_PREFIX."bom_bom as b";
139  $sql .= " WHERE ";
140  $sql .= " b.entity IN (".getEntity('bom').")";
141  $sql .= " AND b.fk_product = ".((int) $product->id);
142  $sql .= $db->order($sortfield, $sortorder);
143 
144  // Count total nb of records
145  $totalofrecords = '';
146  if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
147  $result = $db->query($sql);
148  if ($result) {
149  $totalofrecords = $db->num_rows($result);
150  while ($objp = $db->fetch_object($result)) {
151  $total_qty_toproduce += $objp->qty_toproduce;
152  }
153  } else {
154  dol_print_error($db);
155  }
156  }
157  $sql .= $db->plimit($limit + 1, $offset);
158 
159  $result = $db->query($sql);
160  if ($result) {
161  $bomtmp = new BOM($db);
162  $num = $db->num_rows($result);
163  $i = 0;
164  if ($num > 0) {
165  while ($i < min($num, $limit)) {
166  $objp = $db->fetch_object($result);
167  $bomtmp->id = $objp->rowid;
168  $bomtmp->ref = $objp->ref;
169  $product = new Product($db);
170  if (!empty($objp->fk_product)) {
171  if (!array_key_exists($product->id, $product_cache)) {
172  $resultFetch = $product->fetch($objp->fk_product);
173  if ($resultFetch < 0) {
174  setEventMessages($product->error, $product->errors, 'errors');
175  } else {
176  $product_cache[$product->id] = $product;
177  }
178  }
179  }
180  $bomtmp->fk_product = $objp->fk_product;
181  $bom_data_result[$objp->rowid]['link'] = $bomtmp->getNomUrl(1, 'production');
182  $bom_data_result[$objp->rowid]['product'] = (array_key_exists($objp->fk_product, $product_cache)? $product_cache[$objp->fk_product]->getNomUrl(1): '');
183  $bom_data_result[$objp->rowid]['qty_toproduce'] += ($objp->qty_toproduce > 0 ? $objp->qty_toproduce : 0);
184  $bom_data_result[$objp->rowid]['qty_toconsume'] = 0;
185  $bom_data_result[$objp->rowid]['date_valid'] = dol_print_date($db->jdate($objp->date_valid), 'dayhour');
186  $bom_data_result[$objp->rowid]['status'] = $bomtmp->LibStatut($objp->status, 5);
187  $i++;
188  }
189  }
190  } else {
191  dol_print_error($db);
192  }
193  $db->free($result);
194 
195  //Qauntity to consume
196  $sql = "SELECT b.rowid as rowid, b.ref, b.status, b.date_valid, b.fk_product,";
197  $sql .= " SUM(bl.qty) as qty_toconsume";
198  $sql .= " FROM ".MAIN_DB_PREFIX."bom_bom as b";
199  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."bom_bomline as bl ON bl.fk_bom=b.rowid";
200  $sql .= " WHERE b.entity IN (".getEntity('bom').")";
201  $sql .= " AND bl.fk_product = ".((int) $product->id);
202  $sql .= " GROUP BY b.rowid, b.ref, b.status, b.date_valid, b.fk_product";
203  $sql .= $db->order($sortfield, $sortorder);
204 
205  // Count total nb of records
206  $totalofrecords = '';
207  if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
208  $result = $db->query($sql);
209  if ($result) {
210  $totalofrecords = $db->num_rows($result);
211  while ($objp = $db->fetch_object($result)) {
212  $total_qty_toconsume += $objp->qty_toconsume;
213  }
214  } else {
215  dol_print_error($db);
216  }
217  }
218  $sql .= $db->plimit($limit + 1, $offset);
219 
220  $result = $db->query($sql);
221  if ($result) {
222  $bomtmp = new BOM($db);
223  $num = $db->num_rows($result);
224  $i = 0;
225  if ($num > 0) {
226  while ($i < min($num, $limit)) {
227  $objp = $db->fetch_object($result);
228  $bomtmp->id = $objp->rowid;
229  $bomtmp->ref = $objp->ref;
230  $product = new Product($db);
231  if (!empty($objp->fk_product)) {
232  if (!array_key_exists($product->id, $product_cache)) {
233  $resultFetch = $product->fetch($objp->fk_product);
234  if ($resultFetch < 0) {
235  setEventMessages($product->error, $product->errors, 'errors');
236  } else {
237  $product_cache[$product->id] = $product;
238  }
239  }
240  }
241  $bomtmp->fk_product = $objp->fk_product;
242 
243  if (!array_key_exists($objp->rowid, $bom_data_result)) {
244  $bom_data_result[$objp->rowid]['link'] = $bomtmp->getNomUrl(1, 'production');
245  $bom_data_result[$objp->rowid]['product'] = (array_key_exists($objp->fk_product, $product_cache)? $product_cache[$objp->fk_product]->getNomUrl(1): '');
246  $bom_data_result[$objp->rowid]['qty_toproduce'] = 0;
247  $bom_data_result[$objp->rowid]['qty_toconsume'] += ($objp->qty_toconsume > 0 ? $objp->qty_toconsume : 0);
248  $bom_data_result[$objp->rowid]['date_valid'] = dol_print_date($db->jdate($objp->date_valid), 'dayhour');
249  $bom_data_result[$objp->rowid]['status'] = $bomtmp->LibStatut($objp->status, 5);
250  } else {
251  $bom_data_result[$objp->rowid]['qty_toconsume'] += ($objp->qty_toconsume > 0 ? $objp->qty_toconsume : 0);
252  }
253  $i++;
254  }
255  }
256  } else {
257  dol_print_error($db);
258  }
259  $db->free($result);
260 
261  if ($limit > 0 && $limit != $conf->liste_limit) {
262  $option .= '&limit='.urlencode($limit);
263  }
264  if (!empty($id)) {
265  $option .= '&id='.$product->id;
266  }
267  if (!empty($search_month)) {
268  $option .= '&search_month='.urlencode($search_month);
269  }
270  if (!empty($search_year)) {
271  $option .= '&search_year='.urlencode($search_year);
272  }
273 
274  print '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
275  print '<input type="hidden" name="token" value="'.newToken().'">';
276  if (!empty($sortfield)) {
277  print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
278  }
279  if (!empty($sortorder)) {
280  print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
281  }
282 
283  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);
284 
285  if (!empty($page)) {
286  $option .= '&page='.urlencode($page);
287  }
288 
289  print '<div class="div-table-responsive">';
290  print '<table class="tagtable liste listwithfilterbefore centpercent">';
291 
292  print '<tr class="liste_titre">';
293  print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "b.rowid", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
294  print_liste_field_titre("Product", $_SERVER["PHP_SELF"], "b.fk_product", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
295  print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "b.date_valid", "", "&amp;id=".$product->id, 'align="center"', $sortfield, $sortorder);
296  print_liste_field_titre("RowMaterial", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
297  print_liste_field_titre("Finished", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
298  print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "b.status", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'right ');
299  print "</tr>\n";
300 
301  if (!empty($bom_data_result)) {
302  foreach ($bom_data_result as $data) {
303  print '<tr class="oddeven">';
304  print '<td>';
305  print $data['link'];
306  print "</td>\n";
307  print '<td>';
308  print $data['product'];
309  print "</td>\n";
310  print "<td align=\"center\">";
311  print $data['date_valid']."</td>";
312  print '<td class="center">'.$data['qty_toconsume'].'</td>';
313  print '<td class="center">'.$data['qty_toproduce'].'</td>';
314  print '<td class="right">'.$data['status'].'</td>';
315  print "</tr>\n";
316  }
317  print '</table>';
318  print '</div>';
319  print '</form>';
320  }
321  }
322 } else {
323  dol_print_error();
324 }
325 
326 // End of page
327 llxFooter();
328 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
if($cancel &&!$id) if($action== 'add'&&!$cancel) if($action== 'delete') if($id) $form
Actions.
Definition: card.php:142
Class for BOM.
Definition: bom.class.php:33
Class to manage products or services.
dol_now($mode= 'auto')
Return date for now.
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59
const TYPE_SERVICE
Service.
show_stats_for_company($product, $socid)
Show stats for company.
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 from a POST of a form...
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, $mode=0)
Check permissions of a user to show a page and an object.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
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:73
product_prepare_head($object)
Prepare array with list of tabs.
Definition: product.lib.php:35