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