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