dolibarr  16.0.5
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  $option .= '&id='.$product->id;
262 
263  if ($limit > 0 && $limit != $conf->liste_limit) {
264  $option .= '&limit='.urlencode($limit);
265  }
266  if (!empty($search_month)) {
267  $option .= '&search_month='.urlencode($search_month);
268  }
269  if (!empty($search_year)) {
270  $option .= '&search_year='.urlencode($search_year);
271  }
272 
273  print '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
274  print '<input type="hidden" name="token" value="'.newToken().'">';
275  if (!empty($sortfield)) {
276  print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
277  }
278  if (!empty($sortorder)) {
279  print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
280  }
281 
282  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);
283 
284  if (!empty($page)) {
285  $option .= '&page='.urlencode($page);
286  }
287 
288  print '<div class="div-table-responsive">';
289  print '<table class="tagtable liste listwithfilterbefore centpercent">';
290 
291  print '<tr class="liste_titre">';
292  print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "b.rowid", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
293  print_liste_field_titre("Product", $_SERVER["PHP_SELF"], "b.fk_product", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
294  print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "b.date_valid", "", "&amp;id=".$product->id, 'align="center"', $sortfield, $sortorder);
295  print_liste_field_titre("RowMaterial", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
296  print_liste_field_titre("Finished", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
297  print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "b.status", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'right ');
298  print "</tr>\n";
299 
300  if (!empty($bom_data_result)) {
301  foreach ($bom_data_result as $data) {
302  print '<tr class="oddeven">';
303  print '<td>';
304  print $data['link'];
305  print "</td>\n";
306  print '<td>';
307  print $data['product'];
308  print "</td>\n";
309  print "<td align=\"center\">";
310  print $data['date_valid']."</td>";
311  print '<td class="center">'.$data['qty_toconsume'].'</td>';
312  print '<td class="center">'.$data['qty_toproduce'].'</td>';
313  print '<td class="right">'.$data['status'].'</td>';
314  print "</tr>\n";
315  }
316  print '</table>';
317  print '</div>';
318  print '</form>';
319  }
320  }
321 } else {
322  dol_print_error();
323 }
324 
325 // End of page
326 llxFooter();
327 $db->close();
restrictedArea
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.
Definition: security.lib.php:234
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
product_prepare_head
product_prepare_head($object)
Prepare array with list of tabs.
Definition: product.lib.php:35
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
BOM
Class for BOM.
Definition: bom.class.php:34
dol_banner_tab
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.
Definition: functions.lib.php:2046
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
print_barre_liste
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.
Definition: functions.lib.php:5257
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
print_liste_field_titre
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
Definition: functions.lib.php:5026
Product
Class to manage products or services.
Definition: product.class.php:46
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
show_stats_for_company
show_stats_for_company($product, $socid)
Show stats for company.
Definition: product.lib.php:361
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
Product\TYPE_SERVICE
const TYPE_SERVICE
Service.
Definition: product.class.php:504
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
llxHeader
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