dolibarr  20.0.0-beta
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  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
28 // Load Dolibarr environment
29 require '../../main.inc.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
33 
34 // Load translation files required by the page
35 $langs->loadLangs(array('mrp', 'products', 'companies'));
36 
37 $id = GETPOSTINT('id');
38 $ref = GETPOST('ref', 'alpha');
39 
40 // Security check
41 $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
42 $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
43 if ($user->socid) {
44  $socid = $user->socid;
45 }
46 
47 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
48 $hookmanager->initHooks(array('productstatsbom'));
49 
50 $option = '';
51 
52 // Load variable for pagination
53 $limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
54 $sortfield = GETPOST('sortfield', 'aZ09comma');
55 $sortorder = GETPOST('sortorder', 'aZ09comma');
56 $page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
57 if (empty($page) || $page == -1) {
58  $page = 0;
59 } // If $page is not defined, or '' or -1
60 $offset = $limit * $page;
61 $pageprev = $page - 1;
62 $pagenext = $page + 1;
63 if (!$sortorder) {
64  $sortorder = "DESC";
65 }
66 if (!$sortfield) {
67  $sortfield = "b.date_valid";
68 }
69 
70 $socid = 0;
71 
72 $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
73 
74 
75 /*
76  * View
77  */
78 
79 $form = new Form($db);
80 
81 if ($id > 0 || !empty($ref)) {
82  $product = new Product($db);
83  $result = $product->fetch($id, $ref);
84 
85  $object = $product;
86 
87  $parameters = array('id' => $id);
88  $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
89  if ($reshook < 0) {
90  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
91  }
92 
93  llxHeader("", "", $langs->trans("CardProduct".$product->type));
94 
95  if ($result > 0) {
96  $head = product_prepare_head($product);
97  $titre = $langs->trans("CardProduct".$product->type);
98  $picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
99  print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
100 
101  $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
102  print $hookmanager->resPrint;
103  if ($reshook < 0) {
104  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
105  }
106 
107  $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
108 
109  $shownav = 1;
110  if ($user->socid && !in_array('product', explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL')))) {
111  $shownav = 0;
112  }
113 
114  dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
115 
116  print '<div class="fichecenter">';
117 
118  print '<div class="underbanner clearboth"></div>';
119  print '<table class="border tableforfield centpercent">';
120 
121  $nboflines = show_stats_for_company($product, $socid);
122 
123  print "</table>";
124 
125  print '</div>';
126  print '<div class="clearboth"></div>';
127 
128  print dol_get_fiche_end();
129 
130  $now = dol_now();
131 
132  //Calcul total qty and amount for global if full scan list
133  $total_qty_toconsume = 0;
134  $total_qty_toproduce = 0;
135  $product_cache = array();
136  $bom_data_result = array();
137 
138  //Qauntity to produce
139  $sql = "SELECT b.rowid as rowid, b.ref, b.status, b.date_valid, b.fk_product,";
140  $sql .= " b.qty as qty_toproduce";
141  $sql .= " FROM ".MAIN_DB_PREFIX."bom_bom as b";
142  $sql .= " WHERE ";
143  $sql .= " b.entity IN (".getEntity('bom').")";
144  $sql .= " AND b.fk_product = ".((int) $product->id);
145  $sql .= $db->order($sortfield, $sortorder);
146 
147  // Count total nb of records
148  $totalofrecords = '';
149  if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
150  $result = $db->query($sql);
151  if ($result) {
152  $totalofrecords = $db->num_rows($result);
153  while ($objp = $db->fetch_object($result)) {
154  $total_qty_toproduce += $objp->qty_toproduce;
155  }
156  } else {
157  dol_print_error($db);
158  }
159  }
160  $sql .= $db->plimit($limit + 1, $offset);
161 
162  $result = $db->query($sql);
163  if ($result) {
164  $bomtmp = new BOM($db);
165  $num = $db->num_rows($result);
166  $i = 0;
167  if ($num > 0) {
168  while ($i < min($num, $limit)) {
169  $objp = $db->fetch_object($result);
170  $bomtmp->id = $objp->rowid;
171  $bomtmp->ref = $objp->ref;
172  $product = new Product($db);
173  if (!empty($objp->fk_product)) {
174  if (!array_key_exists($product->id, $product_cache)) {
175  $resultFetch = $product->fetch($objp->fk_product);
176  if ($resultFetch < 0) {
177  setEventMessages($product->error, $product->errors, 'errors');
178  } else {
179  $product_cache[$product->id] = $product;
180  }
181  }
182  }
183  $bomtmp->fk_product = $objp->fk_product;
184  $bom_data_result[$objp->rowid]['link'] = $bomtmp->getNomUrl(1, 'production');
185  $bom_data_result[$objp->rowid]['product'] = (array_key_exists($objp->fk_product, $product_cache) ? $product_cache[$objp->fk_product]->getNomUrl(1) : '');
186  if (empty($bom_data_result[$objp->rowid]['qty_toproduce'])) {
187  $bom_data_result[$objp->rowid]['qty_toproduce'] = 0;
188  }
189  $bom_data_result[$objp->rowid]['qty_toproduce'] += ($objp->qty_toproduce > 0 ? $objp->qty_toproduce : 0);
190  $bom_data_result[$objp->rowid]['qty_toconsume'] = 0;
191  $bom_data_result[$objp->rowid]['date_valid'] = dol_print_date($db->jdate($objp->date_valid), 'dayhour');
192  $bom_data_result[$objp->rowid]['status'] = $bomtmp->LibStatut($objp->status, 5);
193  $i++;
194  }
195  }
196  } else {
197  dol_print_error($db);
198  }
199  $db->free($result);
200 
201  //Qauntity to consume
202  $sql = "SELECT b.rowid as rowid, b.ref, b.status, b.date_valid, b.fk_product,";
203  $sql .= " SUM(bl.qty) as qty_toconsume";
204  $sql .= " FROM ".MAIN_DB_PREFIX."bom_bom as b";
205  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."bom_bomline as bl ON bl.fk_bom=b.rowid";
206  $sql .= " WHERE b.entity IN (".getEntity('bom').")";
207  $sql .= " AND bl.fk_product = ".((int) $product->id);
208  $sql .= " GROUP BY b.rowid, b.ref, b.status, b.date_valid, b.fk_product";
209  $sql .= $db->order($sortfield, $sortorder);
210 
211  // Count total nb of records
212  $totalofrecords = '';
213  if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
214  $result = $db->query($sql);
215  if ($result) {
216  $totalofrecords = $db->num_rows($result);
217  while ($objp = $db->fetch_object($result)) {
218  $total_qty_toconsume += $objp->qty_toconsume;
219  }
220  } else {
221  dol_print_error($db);
222  }
223  }
224  $sql .= $db->plimit($limit + 1, $offset);
225 
226  $result = $db->query($sql);
227  if ($result) {
228  $bomtmp = new BOM($db);
229  $num = $db->num_rows($result);
230  $i = 0;
231  if ($num > 0) {
232  while ($i < min($num, $limit)) {
233  $objp = $db->fetch_object($result);
234  $bomtmp->id = $objp->rowid;
235  $bomtmp->ref = $objp->ref;
236  $product = new Product($db);
237  if (!empty($objp->fk_product)) {
238  if (!array_key_exists($product->id, $product_cache)) {
239  $resultFetch = $product->fetch($objp->fk_product);
240  if ($resultFetch < 0) {
241  setEventMessages($product->error, $product->errors, 'errors');
242  } else {
243  $product_cache[$product->id] = $product;
244  }
245  }
246  }
247  $bomtmp->fk_product = $objp->fk_product;
248 
249  if (!array_key_exists($objp->rowid, $bom_data_result)) {
250  $bom_data_result[$objp->rowid]['link'] = $bomtmp->getNomUrl(1, 'production');
251  $bom_data_result[$objp->rowid]['product'] = (array_key_exists($objp->fk_product, $product_cache) ? $product_cache[$objp->fk_product]->getNomUrl(1) : '');
252  $bom_data_result[$objp->rowid]['qty_toproduce'] = 0;
253  if (empty($bom_data_result[$objp->rowid]['qty_toconsume'])) {
254  $bom_data_result[$objp->rowid]['qty_toconsume'] = 0;
255  }
256  $bom_data_result[$objp->rowid]['qty_toconsume'] += ($objp->qty_toconsume > 0 ? $objp->qty_toconsume : 0);
257  $bom_data_result[$objp->rowid]['date_valid'] = dol_print_date($db->jdate($objp->date_valid), 'dayhour');
258  $bom_data_result[$objp->rowid]['status'] = $bomtmp->LibStatut($objp->status, 5);
259  } else {
260  $bom_data_result[$objp->rowid]['qty_toconsume'] += ($objp->qty_toconsume > 0 ? $objp->qty_toconsume : 0);
261  }
262  $i++;
263  }
264  }
265  } else {
266  dol_print_error($db);
267  }
268  $db->free($result);
269 
270  $option .= '&id='.$product->id;
271 
272  if ($limit > 0 && $limit != $conf->liste_limit) {
273  $option .= '&limit='.((int) $limit);
274  }
275  if (!empty($search_month)) {
276  $option .= '&search_month='.urlencode((string) $search_month);
277  }
278  if (!empty($search_year)) {
279  $option .= '&search_year='.urlencode((string) $search_year);
280  }
281 
282  print '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
283  print '<input type="hidden" name="token" value="'.newToken().'">';
284  if (!empty($sortfield)) {
285  print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
286  }
287  if (!empty($sortorder)) {
288  print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
289  }
290 
291  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);
292 
293  if (!empty($page)) {
294  $option .= '&page='.urlencode((string) ($page));
295  }
296 
297  print '<div class="div-table-responsive">';
298  print '<table class="tagtable liste listwithfilterbefore centpercent">';
299 
300  print '<tr class="liste_titre">';
301  print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "b.rowid", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
302  print_liste_field_titre("Product", $_SERVER["PHP_SELF"], "b.fk_product", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
303  print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "b.date_valid", "", "&amp;id=".$product->id, 'align="center"', $sortfield, $sortorder);
304  print_liste_field_titre("RowMaterial", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
305  print_liste_field_titre("Finished", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
306  print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "b.status", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'right ');
307  print "</tr>\n";
308 
309  if (!empty($bom_data_result)) {
310  foreach ($bom_data_result as $data) {
311  print '<tr class="oddeven">';
312  print '<td>';
313  print $data['link'];
314  print "</td>\n";
315  print '<td>';
316  print $data['product'];
317  print "</td>\n";
318  print "<td align=\"center\">";
319  print $data['date_valid']."</td>";
320  print '<td class="center">'.$data['qty_toconsume'].'</td>';
321  print '<td class="center">'.$data['qty_toproduce'].'</td>';
322  print '<td class="right">'.$data['status'].'</td>';
323  print "</tr>\n";
324  }
325  print '</table>';
326  print '</div>';
327  print '</form>';
328  }
329  }
330 } else {
331  dol_print_error();
332 }
333 
334 // End of page
335 llxFooter();
336 $db->close();
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:55
llxFooter()
Empty footer.
Definition: wrapper.php:69
Class for BOM.
Definition: bom.class.php:45
Class to manage generation of HTML components Only common components must be here.
Class to manage products or services.
const TYPE_SERVICE
Service.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
print_barre_liste($title, $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.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
product_prepare_head($object)
Prepare array with list of tabs.
Definition: product.lib.php:37
show_stats_for_company($product, $socid)
Show stats for company.
restrictedArea(User $user, $features, $object=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.