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