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