dolibarr  17.0.3
html.formproduct.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2008-2009 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2015-2017 Francis Appels <francis.appels@yahoo.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
24 require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
25 
31 {
35  public $db;
36 
40  public $error = '';
41 
42  // Cache arrays
43  public $cache_warehouses = array();
44  public $cache_lot = array();
45  public $cache_workstations = array();
46 
47 
53  public function __construct($db)
54  {
55  $this->db = $db;
56  }
57 
58 
76  public function loadWarehouses($fk_product = 0, $batch = '', $status = '', $sumStock = true, $exclude = array(), $stockMin = false, $orderBy = 'e.ref')
77  {
78  global $conf, $langs;
79 
80  if (empty($fk_product) && count($this->cache_warehouses)) {
81  return 0; // Cache already loaded and we do not want a list with information specific to a product
82  }
83 
84  $warehouseStatus = array();
85 
86  if (preg_match('/warehouseclosed/', $status)) {
87  $warehouseStatus[] = Entrepot::STATUS_CLOSED;
88  }
89  if (preg_match('/warehouseopen/', $status)) {
90  $warehouseStatus[] = Entrepot::STATUS_OPEN_ALL;
91  }
92  if (preg_match('/warehouseinternal/', $status)) {
93  $warehouseStatus[] = Entrepot::STATUS_OPEN_INTERNAL;
94  }
95 
96  $sql = "SELECT e.rowid, e.ref as label, e.description, e.fk_parent";
97  if (!empty($fk_product) && $fk_product > 0) {
98  if (!empty($batch)) {
99  $sql .= ", pb.qty as stock";
100  } else {
101  $sql .= ", ps.reel as stock";
102  }
103  } elseif ($sumStock) {
104  $sql .= ", sum(ps.reel) as stock";
105  }
106  $sql .= " FROM ".$this->db->prefix()."entrepot as e";
107  $sql .= " LEFT JOIN ".$this->db->prefix()."product_stock as ps on ps.fk_entrepot = e.rowid";
108  if (!empty($fk_product) && $fk_product > 0) {
109  $sql .= " AND ps.fk_product = ".((int) $fk_product);
110  if (!empty($batch)) {
111  $sql .= " LEFT JOIN ".$this->db->prefix()."product_batch as pb on pb.fk_product_stock = ps.rowid AND pb.batch = '".$this->db->escape($batch)."'";
112  }
113  }
114  $sql .= " WHERE e.entity IN (".getEntity('stock').")";
115  if (count($warehouseStatus)) {
116  $sql .= " AND e.statut IN (".$this->db->sanitize(implode(',', $warehouseStatus)).")";
117  } else {
118  $sql .= " AND e.statut = 1";
119  }
120 
121  if (is_array($exclude) && !empty($exclude)) {
122  $sql .= ' AND e.rowid NOT IN('.$this->db->sanitize(implode(',', $exclude)).')';
123  }
124 
125  // minimum stock
126  if ($stockMin !== false) {
127  if (!empty($fk_product) && $fk_product > 0) {
128  if (!empty($batch)) {
129  $sql .= " AND pb.qty > ".((float) $stockMin);
130  } else {
131  $sql .= " AND ps.reel > ".((float) $stockMin);
132  }
133  }
134  }
135 
136  if ($sumStock && empty($fk_product)) {
137  $sql .= " GROUP BY e.rowid, e.ref, e.description, e.fk_parent";
138 
139  // minimum stock
140  if ($stockMin !== false) {
141  $sql .= " HAVING sum(ps.reel) > ".((float) $stockMin);
142  }
143  }
144  $sql .= " ORDER BY ".$orderBy;
145 
146  dol_syslog(get_class($this).'::loadWarehouses', LOG_DEBUG);
147  $resql = $this->db->query($sql);
148  if ($resql) {
149  $num = $this->db->num_rows($resql);
150  $i = 0;
151  while ($i < $num) {
152  $obj = $this->db->fetch_object($resql);
153  if ($sumStock) {
154  $obj->stock = price2num($obj->stock, 5);
155  }
156  $this->cache_warehouses[$obj->rowid]['id'] = $obj->rowid;
157  $this->cache_warehouses[$obj->rowid]['label'] = $obj->label;
158  $this->cache_warehouses[$obj->rowid]['parent_id'] = $obj->fk_parent;
159  $this->cache_warehouses[$obj->rowid]['description'] = $obj->description;
160  $this->cache_warehouses[$obj->rowid]['stock'] = $obj->stock;
161  $i++;
162  }
163 
164  // Full label init
165  foreach ($this->cache_warehouses as $obj_rowid => $tab) {
166  $this->cache_warehouses[$obj_rowid]['full_label'] = $this->get_parent_path($tab);
167  }
168 
169  return $num;
170  } else {
171  dol_print_error($this->db);
172  return -1;
173  }
174  }
175 
186  public function loadWorkstations($fk_product = 0, $exclude = array(), $orderBy = 'w.ref')
187  {
188  global $conf, $langs;
189 
190  if (empty($fk_product) && count($this->cache_workstations)) {
191  return 0; // Cache already loaded and we do not want a list with information specific to a product
192  }
193 
194  $sql = "SELECT w.rowid, w.ref as ref, w.label as label, w.type, w.nb_operators_required,w.thm_operator_estimated,w.thm_machine_estimated";
195  $sql .= " FROM ".$this->db->prefix()."workstation_workstation as w";
196  $sql .= " WHERE 1 = 1";
197  if (!empty($fk_product) && $fk_product > 0) {
198  $sql .= " AND w.fk_product = ".((int) $fk_product);
199  }
200  $sql .= " AND w.entity IN (".getEntity('workstation').")";
201 
202  if (is_array($exclude) && !empty($exclude)) {
203  $sql .= ' AND w.rowid NOT IN('.$this->db->sanitize(implode(',', $exclude)).')';
204  }
205 
206  $sql .= " ORDER BY ".$orderBy;
207 
208  dol_syslog(get_class($this).'::loadWorkstations', LOG_DEBUG);
209  $resql = $this->db->query($sql);
210  if ($resql) {
211  $num = $this->db->num_rows($resql);
212  $i = 0;
213  while ($i < $num) {
214  $obj = $this->db->fetch_object($resql);
215 
216  $this->cache_workstations[$obj->rowid]['id'] = $obj->rowid;
217  $this->cache_workstations[$obj->rowid]['ref'] = $obj->ref;
218  $this->cache_workstations[$obj->rowid]['label'] = $obj->label;
219  $this->cache_workstations[$obj->rowid]['type'] = $obj->type;
220  $this->cache_workstations[$obj->rowid]['nb_operators_required'] = $obj->nb_operators_required;
221  $this->cache_workstations[$obj->rowid]['thm_operator_estimated'] = $obj->thm_operator_estimated;
222  $this->cache_workstations[$obj->rowid]['thm_machine_estimated'] = $obj->thm_machine_estimated;
223  $i++;
224  }
225 
226  return $num;
227  } else {
228  dol_print_error($this->db);
229  return -1;
230  }
231  }
232 
233  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
241  private function get_parent_path($tab, $final_label = '')
242  {
243  //phpcs:enable
244  if (empty($final_label)) {
245  $final_label = $tab['label'];
246  }
247 
248  if (empty($tab['parent_id'])) {
249  return $final_label;
250  } else {
251  if (!empty($this->cache_warehouses[$tab['parent_id']])) {
252  $final_label = $this->cache_warehouses[$tab['parent_id']]['label'].' >> '.$final_label;
253  return $this->get_parent_path($this->cache_warehouses[$tab['parent_id']], $final_label);
254  }
255  }
256 
257  return $final_label;
258  }
259 
285  public function selectWarehouses($selected = '', $htmlname = 'idwarehouse', $filterstatus = '', $empty = 0, $disabled = 0, $fk_product = 0, $empty_label = '', $showstock = 0, $forcecombo = 0, $events = array(), $morecss = 'minwidth200', $exclude = array(), $showfullpath = 1, $stockMin = false, $orderBy = 'e.ref')
286  {
287  global $conf, $langs, $user, $hookmanager;
288 
289  dol_syslog(get_class($this)."::selectWarehouses $selected, $htmlname, $filterstatus, $empty, $disabled, $fk_product, $empty_label, $showstock, $forcecombo, $morecss", LOG_DEBUG);
290 
291  $out = '';
292  if (empty($conf->global->ENTREPOT_EXTRA_STATUS)) {
293  $filterstatus = '';
294  }
295  if (!empty($fk_product) && $fk_product > 0) {
296  $this->cache_warehouses = array();
297  }
298 
299  $this->loadWarehouses($fk_product, '', $filterstatus, true, $exclude, $stockMin, $orderBy);
300  $nbofwarehouses = count($this->cache_warehouses);
301 
302  if ($conf->use_javascript_ajax && !$forcecombo) {
303  include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
304  $comboenhancement = ajax_combobox($htmlname, $events);
305  $out .= $comboenhancement;
306  }
307 
308  if (strpos($htmlname, 'search_') !== 0) {
309  if (empty($user->fk_warehouse) || $user->fk_warehouse == -1) {
310  if (($selected == '-2' || $selected == 'ifone') && !empty($conf->global->MAIN_DEFAULT_WAREHOUSE)) {
311  $selected = $conf->global->MAIN_DEFAULT_WAREHOUSE;
312  }
313  } else {
314  if (($selected == '-2' || $selected == 'ifone') && !empty($conf->global->MAIN_DEFAULT_WAREHOUSE_USER)) {
315  $selected = $user->fk_warehouse;
316  }
317  }
318  }
319 
320  $out .= '<select class="flat'.($morecss ? ' '.$morecss : '').'"'.($disabled ? ' disabled' : '').' id="'.$htmlname.'" name="'.($htmlname.($disabled ? '_disabled' : '')).'">';
321  if ($empty) {
322  $out .= '<option value="-1">'.($empty_label ? $empty_label : '&nbsp;').'</option>';
323  }
324  foreach ($this->cache_warehouses as $id => $arraytypes) {
325  $label = '';
326  if ($showfullpath) {
327  $label .= $arraytypes['full_label'];
328  } else {
329  $label .= $arraytypes['label'];
330  }
331  if (($fk_product || ($showstock > 0)) && ($arraytypes['stock'] != 0 || ($showstock > 0))) {
332  if ($arraytypes['stock'] <= 0) {
333  $label .= ' <span class= \'text-warning\'>('.$langs->trans("Stock").':'.$arraytypes['stock'].')</span>';
334  } else {
335  $label .= ' <span class=\'opacitymedium\'>('.$langs->trans("Stock").':'.$arraytypes['stock'].')</span>';
336  }
337  }
338 
339  $out .= '<option value="'.$id.'"';
340  if ($selected == $id || (preg_match('/^ifone/', $selected) && $nbofwarehouses == 1)) {
341  $out .= ' selected';
342  }
343  $out .= ' data-html="'.dol_escape_htmltag($label).'"';
344  $out .= '>';
345  $out .= $label;
346  $out .= '</option>';
347  }
348  $out .= '</select>';
349  if ($disabled) {
350  $out .= '<input type="hidden" name="'.$htmlname.'" value="'.(($selected > 0) ? $selected : '').'">';
351  }
352 
353  $parameters = array(
354  'selected' => $selected,
355  'htmlname' => $htmlname,
356  'filterstatus' => $filterstatus,
357  'empty' => $empty,
358  'disabled ' => $disabled,
359  'fk_product' => $fk_product,
360  'empty_label' => $empty_label,
361  'showstock' => $showstock,
362  'forcecombo' => $forcecombo,
363  'events' => $events,
364  'morecss' => $morecss,
365  'exclude' => $exclude,
366  'showfullpath' => $showfullpath,
367  'stockMin' => $stockMin,
368  'orderBy' => $orderBy
369  );
370 
371  $reshook = $hookmanager->executeHooks('selectWarehouses', $parameters, $this);
372  if ($reshook > 0) {
373  $out = $hookmanager->resPrint;
374  } elseif ($reshook == 0) {
375  $out .= $hookmanager->resPrint;
376  }
377 
378  return $out;
379  }
380 
400  public function selectWorkstations($selected = '', $htmlname = 'idworkstations', $empty = 0, $disabled = 0, $fk_product = 0, $empty_label = '', $forcecombo = 0, $events = array(), $morecss = 'minwidth200', $exclude = array(), $showfullpath = 1, $orderBy = 'e.ref')
401  {
402  global $conf, $langs, $user, $hookmanager;
403 
404  dol_syslog(get_class($this)."::selectWorkstations $selected, $htmlname, $empty, $disabled, $fk_product, $empty_label, $forcecombo, $morecss", LOG_DEBUG);
405 
406  $filterstatus='';
407  $out = '';
408  if (!empty($fk_product) && $fk_product > 0) {
409  $this->cache_workstations = array();
410  }
411 
412  $this->loadWorkstations($fk_product);
413  $nbofworkstations = count($this->cache_workstations);
414 
415  if ($conf->use_javascript_ajax && !$forcecombo) {
416  include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
417  $comboenhancement = ajax_combobox($htmlname, $events);
418  $out .= $comboenhancement;
419  }
420 
421  if (strpos($htmlname, 'search_') !== 0) {
422  if (empty($user->fk_workstation) || $user->fk_workstation == -1) {
423  if (($selected == '-2' || $selected == 'ifone') && !empty($conf->global->MAIN_DEFAULT_WORKSTATION)) {
424  $selected = $conf->global->MAIN_DEFAULT_WORKSTATION;
425  }
426  } else {
427  if (($selected == '-2' || $selected == 'ifone') && !empty($conf->global->MAIN_DEFAULT_WORKSTATION)) {
428  $selected = $user->fk_workstation;
429  }
430  }
431  }
432 
433  $out .= '<select class="flat'.($morecss ? ' '.$morecss : '').'"'.($disabled ? ' disabled' : '').' id="'.$htmlname.'" name="'.($htmlname.($disabled ? '_disabled' : '')).'">';
434  if ($empty) {
435  $out .= '<option value="-1">'.($empty_label ? $empty_label : '&nbsp;').'</option>';
436  }
437  foreach ($this->cache_workstations as $id => $arraytypes) {
438  $label = $arraytypes['label'];
439 
440  $out .= '<option value="'.$id.'"';
441  if ($selected == $id || (preg_match('/^ifone/', $selected) && $nbofworkstations == 1)) {
442  $out .= ' selected';
443  }
444  $out .= ' data-html="'.dol_escape_htmltag($label).'"';
445  $out .= '>';
446  $out .= $label;
447  $out .= '</option>';
448  }
449  $out .= '</select>';
450  if ($disabled) {
451  $out .= '<input type="hidden" name="'.$htmlname.'" value="'.(($selected > 0) ? $selected : '').'">';
452  }
453 
454  $parameters = array(
455  'selected' => $selected,
456  'htmlname' => $htmlname,
457  'filterstatus' => $filterstatus,
458  'empty' => $empty,
459  'disabled ' => $disabled,
460  'fk_product' => $fk_product,
461  'empty_label' => $empty_label,
462  'forcecombo' => $forcecombo,
463  'events' => $events,
464  'morecss' => $morecss,
465  'exclude' => $exclude,
466  'showfullpath' => $showfullpath,
467  'orderBy' => $orderBy
468  );
469 
470  $reshook = $hookmanager->executeHooks('selectWorkstations', $parameters, $this);
471  if ($reshook > 0) {
472  $out = $hookmanager->resPrint;
473  } elseif ($reshook == 0) {
474  $out .= $hookmanager->resPrint;
475  }
476 
477  return $out;
478  }
479 
489  public function formSelectWarehouses($page, $selected = '', $htmlname = 'warehouse_id', $addempty = 0)
490  {
491  global $langs;
492  if ($htmlname != "none") {
493  print '<form method="POST" action="'.$page.'">';
494  print '<input type="hidden" name="action" value="setwarehouse">';
495  print '<input type="hidden" name="token" value="'.newToken().'">';
496  print '<table class="nobordernopadding">';
497  print '<tr><td>';
498  print $this->selectWarehouses($selected, $htmlname, '', $addempty);
499  print '</td>';
500  print '<td class="left"><input type="submit" class="button smallpaddingimp" value="'.$langs->trans("Modify").'"></td>';
501  print '</tr></table></form>';
502  } else {
503  if ($selected) {
504  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
505  $warehousestatic = new Entrepot($this->db);
506  $warehousestatic->fetch($selected);
507  print $warehousestatic->getNomUrl();
508  } else {
509  print "&nbsp;";
510  }
511  }
512  }
513 
514  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
527  public function select_measuring_units($name = 'measuring_units', $measuring_style = '', $default = '0', $adddefault = 0, $mode = 0)
528  {
529  //phpcs:enable
530  print $this->selectMeasuringUnits($name, $measuring_style, $default, $adddefault, $mode);
531  }
532 
545  public function selectMeasuringUnits($name = 'measuring_units', $measuring_style = '', $default = '0', $adddefault = 0, $mode = 0, $morecss = 'maxwidth125')
546  {
547  global $langs, $conf, $mysoc, $db;
548 
549  $langs->load("other");
550 
551  $return = '';
552 
553  // TODO Use a cache
554  require_once DOL_DOCUMENT_ROOT.'/core/class/cunits.class.php';
555  $measuringUnits = new CUnits($db);
556 
557  $filter = array();
558  $filter['t.active'] = 1;
559  if ($measuring_style) {
560  $filter['t.unit_type'] = $measuring_style;
561  }
562 
563  $result = $measuringUnits->fetchAll(
564  '',
565  '',
566  0,
567  0,
568  $filter
569  );
570  if ($result < 0) {
571  dol_print_error($db);
572  return -1;
573  } else {
574  $return .= '<select class="flat'.($morecss ? ' '.$morecss : '').'" name="'.$name.'" id="'.$name.'">';
575  if ($adddefault || $adddefault === '') {
576  $return .= '<option value="0">'.($adddefault ? $langs->trans("Default") : '').'</option>';
577  }
578 
579  foreach ($measuringUnits->records as $lines) {
580  $return .= '<option value="';
581  if ($mode == 1) {
582  $return .= $lines->short_label;
583  } elseif ($mode == 2) {
584  $return .= $lines->scale;
585  } else {
586  $return .= $lines->id;
587  }
588  $return .= '"';
589  if ($mode == 1 && $lines->short_label == $default) {
590  $return .= ' selected';
591  } elseif ($mode == 2 && $lines->scale == $default) {
592  $return .= ' selected';
593  } elseif ($mode == 0 && $lines->id == $default) {
594  $return .= ' selected';
595  }
596  $return .= '>';
597  if ($measuring_style == 'time') {
598  $return .= $langs->trans(ucfirst($lines->label));
599  } else {
600  $return .= $langs->trans($lines->label);
601  }
602  $return .= '</option>';
603  }
604  $return .= '</select>';
605  }
606 
607  $return .= ajax_combobox($name);
608 
609  return $return;
610  }
611 
622  public function selectProductNature($name = 'finished', $selected = '', $mode = 0, $showempty = 1)
623  {
624  global $langs, $db;
625 
626  $langs->load('products');
627 
628  $return = '';
629 
630  // TODO Use a cache
631  require_once DOL_DOCUMENT_ROOT.'/core/class/cproductnature.class.php';
632  $productNature = new CProductNature($db);
633 
634  $filter = array();
635  $filter['t.active'] = 1;
636 
637  $result = $productNature->fetchAll('', '', 0, 0, $filter);
638 
639  if ($result < 0) {
640  dol_print_error($db);
641  return -1;
642  } else {
643  $return .= '<select class="flat" name="'.$name.'" id="'.$name.'">';
644  if ($showempty || ($selected == '' || $selected == '-1')) {
645  $return .= '<option value="-1"';
646  if ($selected == '' || $selected == '-1') {
647  $return .= ' selected';
648  }
649  $return .= '></option>';
650  }
651  if (!empty($productNature->records) && is_array($productNature->records)) {
652  foreach ($productNature->records as $lines) {
653  $return .= '<option value="';
654  if ($mode == 1) {
655  $return .= $lines->label;
656  } else {
657  $return .= $lines->code;
658  }
659 
660  $return .= '"';
661 
662  if ($mode == 1 && $lines->label == $selected) {
663  $return .= ' selected';
664  } elseif ($lines->code == $selected) {
665  $return .= ' selected';
666  }
667 
668  $return .= '>';
669  $return .= $langs->trans($lines->label);
670  $return .= '</option>';
671  }
672  }
673  $return .= '</select>';
674  }
675 
676  $return .= ajax_combobox($name);
677 
678  return $return;
679  }
680 
699  public function selectLotStock($selected = '', $htmlname = 'batch_id', $filterstatus = '', $empty = 0, $disabled = 0, $fk_product = 0, $fk_entrepot = 0, $objectLines = array(), $empty_label = '', $forcecombo = 0, $events = array(), $morecss = 'minwidth200')
700  {
701  global $conf, $langs;
702 
703  dol_syslog(get_class($this)."::selectLotStock $selected, $htmlname, $filterstatus, $empty, $disabled, $fk_product, $fk_entrepot, $empty_label, $forcecombo, $morecss", LOG_DEBUG);
704 
705  $out = '';
706  $productIdArray = array();
707  if (!is_array($objectLines) || !count($objectLines)) {
708  if (!empty($fk_product) && $fk_product > 0) {
709  $productIdArray[] = (int) $fk_product;
710  }
711  } else {
712  foreach ($objectLines as $line) {
713  if ($line->fk_product) {
714  $productIdArray[] = $line->fk_product;
715  }
716  }
717  }
718 
719  $nboflot = $this->loadLotStock($productIdArray);
720 
721  if ($conf->use_javascript_ajax && !$forcecombo) {
722  include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
723  $comboenhancement = ajax_combobox($htmlname, $events);
724  $out .= $comboenhancement;
725  }
726 
727  $out .= '<select class="flat'.($morecss ? ' '.$morecss : '').'"'.($disabled ? ' disabled' : '').' id="'.$htmlname.'" name="'.($htmlname.($disabled ? '_disabled' : '')).'">';
728  if ($empty) {
729  $out .= '<option value="-1">'.($empty_label ? $empty_label : '&nbsp;').'</option>';
730  }
731  if (!empty($fk_product) && $fk_product > 0) {
732  $productIdArray = array((int) $fk_product); // only show lot stock for product
733  } else {
734  foreach ($this->cache_lot as $key => $value) {
735  $productIdArray[] = $key;
736  }
737  }
738 
739  foreach ($productIdArray as $productId) {
740  foreach ($this->cache_lot[$productId] as $id => $arraytypes) {
741  if (empty($fk_entrepot) || $fk_entrepot == $arraytypes['entrepot_id']) {
742  $label = $arraytypes['entrepot_label'].' - ';
743  $label .= $arraytypes['batch'];
744  if ($arraytypes['qty'] <= 0) {
745  $label .= ' <span class=\'text-warning\'>('.$langs->trans("Stock").' '.$arraytypes['qty'].')</span>';
746  } else {
747  $label .= ' <span class=\'opacitymedium\'>('.$langs->trans("Stock").' '.$arraytypes['qty'].')</span>';
748  }
749 
750  $out .= '<option value="'.$id.'"';
751 
752  if ($selected == $id || ($selected == 'ifone' && $nboflot == 1)) {
753  $out .= ' selected';
754  }
755  $out .= ' data-html="'.dol_escape_htmltag($label).'"';
756  $out .= '>';
757  $out .= $label;
758  $out .= '</option>';
759  }
760  }
761  }
762  $out .= '</select>';
763  if ($disabled) {
764  $out .= '<input type="hidden" name="'.$htmlname.'" value="'.(($selected > 0) ? $selected : '').'">';
765  }
766 
767  return $out;
768  }
769 
770 
771 
782  public function selectLotDataList($htmlname = 'batch_id', $empty = 0, $fk_product = 0, $fk_entrepot = 0, $objectLines = array())
783  {
784  dol_syslog(get_class($this)."::selectLotDataList $htmlname, $empty, $fk_product, $fk_entrepot", LOG_DEBUG);
785 
786  $out = '';
787  $productIdArray = array();
788  if (!is_array($objectLines) || !count($objectLines)) {
789  if (!empty($fk_product) && $fk_product > 0) {
790  $productIdArray[] = (int) $fk_product;
791  }
792  } else {
793  foreach ($objectLines as $line) {
794  if ($line->fk_product) {
795  $productIdArray[] = $line->fk_product;
796  }
797  }
798  }
799 
800  $nboflot = $this->loadLotStock($productIdArray);
801 
802  $out .= '<datalist id="'.$htmlname.'" >';
803 
804  if (!empty($fk_product) && $fk_product > 0) {
805  $productIdArray = array((int) $fk_product); // only show lot stock for product
806  } else {
807  foreach ($this->cache_lot as $key => $value) {
808  $productIdArray[] = $key;
809  }
810  }
811 
812  foreach ($productIdArray as $productId) {
813  if (array_key_exists($productId, $this->cache_lot)) {
814  foreach ($this->cache_lot[$productId] as $id => $arraytypes) {
815  if (empty($fk_entrepot) || $fk_entrepot == $arraytypes['entrepot_id']) {
816  $label = $arraytypes['entrepot_label'] . ' - ';
817  $label .= $arraytypes['batch'];
818  $out .= '<option data-warehouse="'.dol_escape_htmltag($label).'">' . $arraytypes['batch'] . '</option>';
819  }
820  }
821  }
822  }
823  $out .= '</datalist>';
824 
825  return $out;
826  }
827 
828 
836  private function loadLotStock($productIdArray = array())
837  {
838  global $conf, $langs;
839 
840  $cacheLoaded = false;
841  if (empty($productIdArray)) {
842  // only Load lot stock for given products
843  $this->cache_lot = array();
844  return 0;
845  }
846  if (count($productIdArray) && count($this->cache_lot)) {
847  // check cache already loaded for product id's
848  foreach ($productIdArray as $productId) {
849  $cacheLoaded = !empty($this->cache_lot[$productId]) ? true : false;
850  }
851  }
852  if ($cacheLoaded) {
853  return count($this->cache_lot);
854  } else {
855  // clear cache
856  $this->cache_lot = array();
857  $productIdList = implode(',', $productIdArray);
858  $sql = "SELECT pb.batch, pb.rowid, ps.fk_entrepot, pb.qty, e.ref as label, ps.fk_product";
859  $sql .= " FROM ".$this->db->prefix()."product_batch as pb";
860  $sql .= " LEFT JOIN ".$this->db->prefix()."product_stock as ps on ps.rowid = pb.fk_product_stock";
861  $sql .= " LEFT JOIN ".$this->db->prefix()."entrepot as e on e.rowid = ps.fk_entrepot AND e.entity IN (".getEntity('stock').")";
862  if (!empty($productIdList)) {
863  $sql .= " WHERE ps.fk_product IN (".$this->db->sanitize($productIdList).")";
864  }
865  $sql .= " ORDER BY e.ref, pb.batch";
866 
867  dol_syslog(get_class($this).'::loadLotStock', LOG_DEBUG);
868  $resql = $this->db->query($sql);
869  if ($resql) {
870  $num = $this->db->num_rows($resql);
871  $i = 0;
872  while ($i < $num) {
873  $obj = $this->db->fetch_object($resql);
874  $this->cache_lot[$obj->fk_product][$obj->rowid]['id'] = $obj->rowid;
875  $this->cache_lot[$obj->fk_product][$obj->rowid]['batch'] = $obj->batch;
876  $this->cache_lot[$obj->fk_product][$obj->rowid]['entrepot_id'] = $obj->fk_entrepot;
877  $this->cache_lot[$obj->fk_product][$obj->rowid]['entrepot_label'] = $obj->label;
878  $this->cache_lot[$obj->fk_product][$obj->rowid]['qty'] = $obj->qty;
879  $i++;
880  }
881 
882  return $num;
883  } else {
884  dol_print_error($this->db);
885  return -1;
886  }
887  }
888  }
889 }
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4993
Entrepot\STATUS_OPEN_INTERNAL
const STATUS_OPEN_INTERNAL
Warehouse open and only operations for stock transfers/corrections allowed (not for customer shipping...
Definition: entrepot.class.php:160
CProductNature
Class of dictionary of nature of product (used by imports)
Definition: cproductnature.class.php:29
FormProduct\selectLotStock
selectLotStock($selected='', $htmlname='batch_id', $filterstatus='', $empty=0, $disabled=0, $fk_product=0, $fk_entrepot=0, $objectLines=array(), $empty_label='', $forcecombo=0, $events=array(), $morecss='minwidth200')
Return list of lot numbers (stock from product_batch) with stock location and stock qty.
Definition: html.formproduct.class.php:699
FormProduct\formSelectWarehouses
formSelectWarehouses($page, $selected='', $htmlname='warehouse_id', $addempty=0)
Display form to select warehouse.
Definition: html.formproduct.class.php:489
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5823
Entrepot\STATUS_OPEN_ALL
const STATUS_OPEN_ALL
Warehouse open and any operations are allowed (customer shipping, supplier dispatch,...
Definition: entrepot.class.php:155
FormProduct\selectMeasuringUnits
selectMeasuringUnits($name='measuring_units', $measuring_style='', $default='0', $adddefault=0, $mode=0, $morecss='maxwidth125')
Return a combo box with list of units Units labels are defined in llx_c_units.
Definition: html.formproduct.class.php:545
FormProduct\selectLotDataList
selectLotDataList($htmlname='batch_id', $empty=0, $fk_product=0, $fk_entrepot=0, $objectLines=array())
Return list of lot numbers (stock from product_batch) for product and warehouse.
Definition: html.formproduct.class.php:782
FormProduct\loadWorkstations
loadWorkstations($fk_product=0, $exclude=array(), $orderBy='w.ref')
Load in cache array list of workstations If fk_product is not 0, we do not use cache.
Definition: html.formproduct.class.php:186
getEntity
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Definition: functions.lib.php:180
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
CUnits
Class of dictionary type of thirdparty (used by imports)
Definition: cunits.class.php:28
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
FormProduct
Class with static methods for building HTML components related to products Only components common to ...
Definition: html.formproduct.class.php:30
FormProduct\loadLotStock
loadLotStock($productIdArray=array())
Load in cache array list of lot available in stock from a given list of products.
Definition: html.formproduct.class.php:836
FormProduct\loadWarehouses
loadWarehouses($fk_product=0, $batch='', $status='', $sumStock=true, $exclude=array(), $stockMin=false, $orderBy='e.ref')
Load in cache array list of warehouses If fk_product is not 0, we do not use cache.
Definition: html.formproduct.class.php:76
FormProduct\get_parent_path
get_parent_path($tab, $final_label='')
Return full path to current warehouse in $tab (recursive function)
Definition: html.formproduct.class.php:241
FormProduct\selectProductNature
selectProductNature($name='finished', $selected='', $mode=0, $showempty=1)
Return a combo box with list of units NAture of product labels are defined in llx_c_product_nature.
Definition: html.formproduct.class.php:622
Entrepot\STATUS_CLOSED
const STATUS_CLOSED
Warehouse closed, inactive.
Definition: entrepot.class.php:150
Entrepot
Class to manage warehouses.
Definition: entrepot.class.php:35
FormProduct\select_measuring_units
select_measuring_units($name='measuring_units', $measuring_style='', $default='0', $adddefault=0, $mode=0)
Output a combo box with list of units pour l'instant on ne definit pas les unites dans la base.
Definition: html.formproduct.class.php:527
FormProduct\selectWorkstations
selectWorkstations($selected='', $htmlname='idworkstations', $empty=0, $disabled=0, $fk_product=0, $empty_label='', $forcecombo=0, $events=array(), $morecss='minwidth200', $exclude=array(), $showfullpath=1, $orderBy='e.ref')
Return list of workstations.
Definition: html.formproduct.class.php:400
ajax_combobox
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:449
FormProduct\selectWarehouses
selectWarehouses($selected='', $htmlname='idwarehouse', $filterstatus='', $empty=0, $disabled=0, $fk_product=0, $empty_label='', $showstock=0, $forcecombo=0, $events=array(), $morecss='minwidth200', $exclude=array(), $showfullpath=1, $stockMin=false, $orderBy='e.ref')
Return list of warehouses.
Definition: html.formproduct.class.php:285
FormProduct\__construct
__construct($db)
Constructor.
Definition: html.formproduct.class.php:53
if
if(!defined( 'CSRFCHECK_WITH_TOKEN'))
Definition: journals_list.php:25