dolibarr 19.0.3
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// Load Dolibarr environment
28require '../../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
31require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array('mrp', 'products', 'companies'));
35
36$id = GETPOST('id', 'int');
37$ref = GETPOST('ref', 'alpha');
38
39// Security check
40$fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
41$fieldtype = (!empty($ref) ? 'ref' : 'rowid');
42if ($user->socid) {
43 $socid = $user->socid;
44}
45
46// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
47$hookmanager->initHooks(array('productstatsbom'));
48
49$mesg = '';
50$option = '';
51
52// Load variable for pagination
53$limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
54$sortfield = GETPOST('sortfield', 'aZ09comma');
55$sortorder = GETPOST('sortorder', 'aZ09comma');
56$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
57if (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;
63if (!$sortorder) {
64 $sortorder = "DESC";
65}
66if (!$sortfield) {
67 $sortfield = "b.date_valid";
68}
69
70$result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
71
72
73/*
74 * View
75 */
76
77$form = new Form($db);
78
79if ($id > 0 || !empty($ref)) {
80 $product = new Product($db);
81 $result = $product->fetch($id, $ref);
82
83 $object = $product;
84
85 $parameters = array('id'=>$id);
86 $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
87 if ($reshook < 0) {
88 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
89 }
90
91 llxHeader("", "", $langs->trans("CardProduct".$product->type));
92
93 if ($result > 0) {
94 $head = product_prepare_head($product);
95 $titre = $langs->trans("CardProduct".$product->type);
96 $picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
97 print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
98
99 $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
100 print $hookmanager->resPrint;
101 if ($reshook < 0) {
102 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
103 }
104
105 $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
106
107 $shownav = 1;
108 if ($user->socid && !in_array('product', explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL')))) {
109 $shownav = 0;
110 }
111
112 dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
113
114 print '<div class="fichecenter">';
115
116 print '<div class="underbanner clearboth"></div>';
117 print '<table class="border tableforfield" width="100%">';
118
119 $nboflines = show_stats_for_company($product, $socid);
120
121 print "</table>";
122
123 print '</div>';
124 print '<div class="clearboth"></div>';
125
126 print dol_get_fiche_end();
127
128 $now = dol_now();
129
130 //Calcul total qty and amount for global if full scan list
131 $total_qty_toconsume = 0;
132 $total_qty_toproduce = 0;
133 $product_cache=array();
134 $bom_data_result = array();
135
136 //Qauntity to produce
137 $sql = "SELECT b.rowid as rowid, b.ref, b.status, b.date_valid, b.fk_product,";
138 $sql .= " b.qty as qty_toproduce";
139 $sql .= " FROM ".MAIN_DB_PREFIX."bom_bom as b";
140 $sql .= " WHERE ";
141 $sql .= " b.entity IN (".getEntity('bom').")";
142 $sql .= " AND b.fk_product = ".((int) $product->id);
143 $sql .= $db->order($sortfield, $sortorder);
144
145 // Count total nb of records
146 $totalofrecords = '';
147 if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
148 $result = $db->query($sql);
149 if ($result) {
150 $totalofrecords = $db->num_rows($result);
151 while ($objp = $db->fetch_object($result)) {
152 $total_qty_toproduce += $objp->qty_toproduce;
153 }
154 } else {
155 dol_print_error($db);
156 }
157 }
158 $sql .= $db->plimit($limit + 1, $offset);
159
160 $result = $db->query($sql);
161 if ($result) {
162 $bomtmp = new BOM($db);
163 $num = $db->num_rows($result);
164 $i = 0;
165 if ($num > 0) {
166 while ($i < min($num, $limit)) {
167 $objp = $db->fetch_object($result);
168 $bomtmp->id = $objp->rowid;
169 $bomtmp->ref = $objp->ref;
170 $product = new Product($db);
171 if (!empty($objp->fk_product)) {
172 if (!array_key_exists($product->id, $product_cache)) {
173 $resultFetch = $product->fetch($objp->fk_product);
174 if ($resultFetch < 0) {
175 setEventMessages($product->error, $product->errors, 'errors');
176 } else {
177 $product_cache[$product->id] = $product;
178 }
179 }
180 }
181 $bomtmp->fk_product = $objp->fk_product;
182 $bom_data_result[$objp->rowid]['link'] = $bomtmp->getNomUrl(1, 'production');
183 $bom_data_result[$objp->rowid]['product'] = (array_key_exists($objp->fk_product, $product_cache) ? $product_cache[$objp->fk_product]->getNomUrl(1) : '');
184 $bom_data_result[$objp->rowid]['qty_toproduce'] += ($objp->qty_toproduce > 0 ? $objp->qty_toproduce : 0);
185 $bom_data_result[$objp->rowid]['qty_toconsume'] = 0;
186 $bom_data_result[$objp->rowid]['date_valid'] = dol_print_date($db->jdate($objp->date_valid), 'dayhour');
187 $bom_data_result[$objp->rowid]['status'] = $bomtmp->LibStatut($objp->status, 5);
188 $i++;
189 }
190 }
191 } else {
192 dol_print_error($db);
193 }
194 $db->free($result);
195
196 //Qauntity to consume
197 $sql = "SELECT b.rowid as rowid, b.ref, b.status, b.date_valid, b.fk_product,";
198 $sql .= " SUM(bl.qty) as qty_toconsume";
199 $sql .= " FROM ".MAIN_DB_PREFIX."bom_bom as b";
200 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."bom_bomline as bl ON bl.fk_bom=b.rowid";
201 $sql .= " WHERE b.entity IN (".getEntity('bom').")";
202 $sql .= " AND bl.fk_product = ".((int) $product->id);
203 $sql .= " GROUP BY b.rowid, b.ref, b.status, b.date_valid, b.fk_product";
204 $sql .= $db->order($sortfield, $sortorder);
205
206 // Count total nb of records
207 $totalofrecords = '';
208 if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
209 $result = $db->query($sql);
210 if ($result) {
211 $totalofrecords = $db->num_rows($result);
212 while ($objp = $db->fetch_object($result)) {
213 $total_qty_toconsume += $objp->qty_toconsume;
214 }
215 } else {
216 dol_print_error($db);
217 }
218 }
219 $sql .= $db->plimit($limit + 1, $offset);
220
221 $result = $db->query($sql);
222 if ($result) {
223 $bomtmp = new BOM($db);
224 $num = $db->num_rows($result);
225 $i = 0;
226 if ($num > 0) {
227 while ($i < min($num, $limit)) {
228 $objp = $db->fetch_object($result);
229 $bomtmp->id = $objp->rowid;
230 $bomtmp->ref = $objp->ref;
231 $product = new Product($db);
232 if (!empty($objp->fk_product)) {
233 if (!array_key_exists($product->id, $product_cache)) {
234 $resultFetch = $product->fetch($objp->fk_product);
235 if ($resultFetch < 0) {
236 setEventMessages($product->error, $product->errors, 'errors');
237 } else {
238 $product_cache[$product->id] = $product;
239 }
240 }
241 }
242 $bomtmp->fk_product = $objp->fk_product;
243
244 if (!array_key_exists($objp->rowid, $bom_data_result)) {
245 $bom_data_result[$objp->rowid]['link'] = $bomtmp->getNomUrl(1, 'production');
246 $bom_data_result[$objp->rowid]['product'] = (array_key_exists($objp->fk_product, $product_cache) ? $product_cache[$objp->fk_product]->getNomUrl(1) : '');
247 $bom_data_result[$objp->rowid]['qty_toproduce'] = 0;
248 $bom_data_result[$objp->rowid]['qty_toconsume'] += ($objp->qty_toconsume > 0 ? $objp->qty_toconsume : 0);
249 $bom_data_result[$objp->rowid]['date_valid'] = dol_print_date($db->jdate($objp->date_valid), 'dayhour');
250 $bom_data_result[$objp->rowid]['status'] = $bomtmp->LibStatut($objp->status, 5);
251 } else {
252 $bom_data_result[$objp->rowid]['qty_toconsume'] += ($objp->qty_toconsume > 0 ? $objp->qty_toconsume : 0);
253 }
254 $i++;
255 }
256 }
257 } else {
258 dol_print_error($db);
259 }
260 $db->free($result);
261
262 $option .= '&id='.$product->id;
263
264 if ($limit > 0 && $limit != $conf->liste_limit) {
265 $option .= '&limit='.((int) $limit);
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 {
324}
325
326// End of page
327llxFooter();
328$db->close();
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:43
Class to manage generation of HTML components Only common components must be here.
Class to manage products or services.
const TYPE_SERVICE
Service.
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.
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_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_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
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.
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.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
product_prepare_head($object)
Prepare array with list of tabs.
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.