24 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
43 public $cache_warehouses = array();
44 public $cache_lot = array();
75 public function loadWarehouses($fk_product = 0, $batch =
'', $status =
'', $sumStock =
true, $exclude = array(), $stockMin =
false, $orderBy =
'e.ref')
79 if (empty($fk_product) && count($this->cache_warehouses)) {
83 $warehouseStatus = array();
85 if (preg_match(
'/warehouseclosed/', $status)) {
88 if (preg_match(
'/warehouseopen/', $status)) {
91 if (preg_match(
'/warehouseinternal/', $status)) {
95 $sql =
"SELECT e.rowid, e.ref as label, e.description, e.fk_parent";
96 if (!empty($fk_product) && $fk_product > 0) {
98 $sql .=
", pb.qty as stock";
100 $sql .=
", ps.reel as stock";
102 } elseif ($sumStock) {
103 $sql .=
", sum(ps.reel) as stock";
105 $sql .=
" FROM ".$this->db->prefix().
"entrepot as e";
106 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_stock as ps on ps.fk_entrepot = e.rowid";
107 if (!empty($fk_product) && $fk_product > 0) {
108 $sql .=
" AND ps.fk_product = ".((int) $fk_product);
109 if (!empty($batch)) {
110 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_batch as pb on pb.fk_product_stock = ps.rowid AND pb.batch = '".$this->
db->escape($batch).
"'";
113 $sql .=
" WHERE e.entity IN (".getEntity(
'stock').
")";
114 if (count($warehouseStatus)) {
115 $sql .=
" AND e.statut IN (".$this->db->sanitize(implode(
',', $warehouseStatus)).
")";
117 $sql .=
" AND e.statut = 1";
120 if (is_array($exclude) && !empty($exclude)) {
121 $sql .=
' AND e.rowid NOT IN('.$this->db->sanitize(implode(
',', $exclude)).
')';
125 if ($stockMin !==
false) {
126 if (!empty($fk_product) && $fk_product > 0) {
127 if (!empty($batch)) {
128 $sql .=
" AND pb.qty > ".((float) $stockMin);
130 $sql .=
" AND ps.reel > ".((float) $stockMin);
135 if ($sumStock && empty($fk_product)) {
136 $sql .=
" GROUP BY e.rowid, e.ref, e.description, e.fk_parent";
139 if ($stockMin !==
false) {
140 $sql .=
" HAVING sum(ps.reel) > ".((float) $stockMin);
143 $sql .=
" ORDER BY ".$orderBy;
145 dol_syslog(get_class($this).
'::loadWarehouses', LOG_DEBUG);
151 $obj = $this->
db->fetch_object(
$resql);
155 $this->cache_warehouses[$obj->rowid][
'id'] = $obj->rowid;
156 $this->cache_warehouses[$obj->rowid][
'label'] = $obj->label;
157 $this->cache_warehouses[$obj->rowid][
'parent_id'] = $obj->fk_parent;
158 $this->cache_warehouses[$obj->rowid][
'description'] = $obj->description;
159 $this->cache_warehouses[$obj->rowid][
'stock'] = $obj->stock;
164 foreach ($this->cache_warehouses as $obj_rowid => $tab) {
165 $this->cache_warehouses[$obj_rowid][
'full_label'] = $this->
get_parent_path($tab);
186 if (empty($final_label)) {
187 $final_label = $tab[
'label'];
190 if (empty($tab[
'parent_id'])) {
193 if (!empty($this->cache_warehouses[$tab[
'parent_id']])) {
194 $final_label = $this->cache_warehouses[$tab[
'parent_id']][
'label'].
' >> '.$final_label;
195 return $this->
get_parent_path($this->cache_warehouses[$tab[
'parent_id']], $final_label);
227 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')
229 global $conf, $langs, $user, $hookmanager;
231 dol_syslog(get_class($this).
"::selectWarehouses $selected, $htmlname, $filterstatus, $empty, $disabled, $fk_product, $empty_label, $showstock, $forcecombo, $morecss", LOG_DEBUG);
234 if (empty($conf->global->ENTREPOT_EXTRA_STATUS)) {
237 if (!empty($fk_product) && $fk_product > 0) {
238 $this->cache_warehouses = array();
241 $this->
loadWarehouses($fk_product,
'', $filterstatus,
true, $exclude, $stockMin, $orderBy);
242 $nbofwarehouses = count($this->cache_warehouses);
244 if ($conf->use_javascript_ajax && !$forcecombo) {
245 include_once DOL_DOCUMENT_ROOT.
'/core/lib/ajax.lib.php';
247 $out .= $comboenhancement;
250 if (strpos($htmlname,
'search_') !== 0) {
251 if (empty($user->fk_warehouse) || $user->fk_warehouse == -1) {
252 if (($selected ==
'-2' || $selected ==
'ifone') && !empty($conf->global->MAIN_DEFAULT_WAREHOUSE)) {
253 $selected = $conf->global->MAIN_DEFAULT_WAREHOUSE;
256 if (($selected ==
'-2' || $selected ==
'ifone') && !empty($conf->global->MAIN_DEFAULT_WAREHOUSE_USER)) {
257 $selected = $user->fk_warehouse;
262 $out .=
'<select class="flat'.($morecss ?
' '.$morecss :
'').
'"'.($disabled ?
' disabled' :
'').
' id="'.$htmlname.
'" name="'.($htmlname.($disabled ?
'_disabled' :
'')).
'">';
264 $out .=
'<option value="-1">'.($empty_label ? $empty_label :
' ').
'</option>';
266 foreach ($this->cache_warehouses as $id => $arraytypes) {
269 $label .= $arraytypes[
'full_label'];
271 $label .= $arraytypes[
'label'];
273 if (($fk_product || ($showstock > 0)) && ($arraytypes[
'stock'] != 0 || ($showstock > 0))) {
274 if ($arraytypes[
'stock'] <= 0) {
275 $label .=
' <span class= \'text-warning\'>('.$langs->trans(
"Stock").
':'.$arraytypes[
'stock'].
')</span>';
277 $label .=
' <span class=\'opacitymedium\'>('.$langs->trans(
"Stock").
':'.$arraytypes[
'stock'].
')</span>';
281 $out .=
'<option value="'.$id.
'"';
282 if ($selected == $id || (preg_match(
'/^ifone/', $selected) && $nbofwarehouses == 1)) {
285 $out .=
' data-html="'.dol_escape_htmltag($label).
'"';
292 $out .=
'<input type="hidden" name="'.$htmlname.
'" value="'.(($selected > 0) ? $selected :
'').
'">';
296 'selected' => $selected,
297 'htmlname' => $htmlname,
298 'filterstatus' => $filterstatus,
300 'disabled ' => $disabled,
301 'fk_product' => $fk_product,
302 'empty_label' => $empty_label,
303 'showstock' => $showstock,
304 'forcecombo' => $forcecombo,
306 'morecss' => $morecss,
307 'exclude' => $exclude,
308 'showfullpath' => $showfullpath,
309 'stockMin' => $stockMin,
310 'orderBy' => $orderBy
313 $reshook = $hookmanager->executeHooks(
'selectWarehouses', $parameters, $this);
315 $out = $hookmanager->resPrint;
316 } elseif ($reshook == 0) {
317 $out .= $hookmanager->resPrint;
335 if ($htmlname !=
"none") {
336 print
'<form method="POST" action="'.$page.
'">';
337 print
'<input type="hidden" name="action" value="setwarehouse">';
338 print
'<input type="hidden" name="token" value="'.newToken().
'">';
339 print
'<table class="nobordernopadding">';
343 print
'<td class="left"><input type="submit" class="button smallpaddingimp" value="'.$langs->trans(
"Modify").
'"></td>';
344 print
'</tr></table></form>';
347 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
349 $warehousestatic->fetch($selected);
350 print $warehousestatic->getNomUrl();
370 public function select_measuring_units($name =
'measuring_units', $measuring_style =
'', $default =
'0', $adddefault = 0, $mode = 0)
388 public function selectMeasuringUnits($name =
'measuring_units', $measuring_style =
'', $default =
'0', $adddefault = 0, $mode = 0, $morecss =
'maxwidth125')
390 global $langs, $conf, $mysoc, $db;
392 $langs->load(
"other");
397 require_once DOL_DOCUMENT_ROOT.
'/core/class/cunits.class.php';
398 $measuringUnits =
new CUnits($db);
401 $filter[
't.active'] = 1;
402 if ($measuring_style) {
403 $filter[
't.unit_type'] = $measuring_style;
406 $result = $measuringUnits->fetchAll(
417 $return .=
'<select class="flat'.($morecss ?
' '.$morecss :
'').
'" name="'.$name.
'" id="'.$name.
'">';
418 if ($adddefault || $adddefault ===
'') {
419 $return .=
'<option value="0">'.($adddefault ? $langs->trans(
"Default") :
'').
'</option>';
422 foreach ($measuringUnits->records as $lines) {
423 $return .=
'<option value="';
425 $return .= $lines->short_label;
426 } elseif ($mode == 2) {
427 $return .= $lines->scale;
429 $return .= $lines->id;
432 if ($mode == 1 && $lines->short_label == $default) {
433 $return .=
' selected';
434 } elseif ($mode == 2 && $lines->scale == $default) {
435 $return .=
' selected';
436 } elseif ($mode == 0 && $lines->id == $default) {
437 $return .=
' selected';
440 if ($measuring_style ==
'time') {
441 $return .= $langs->trans(ucfirst($lines->label));
443 $return .= $langs->trans($lines->label);
445 $return .=
'</option>';
447 $return .=
'</select>';
469 $langs->load(
'products');
474 require_once DOL_DOCUMENT_ROOT.
'/core/class/cproductnature.class.php';
478 $filter[
't.active'] = 1;
480 $result = $productNature->fetchAll(
'',
'', 0, 0, $filter);
486 $return .=
'<select class="flat" name="'.$name.
'" id="'.$name.
'">';
487 if ($showempty || ($selected ==
'' || $selected ==
'-1')) {
488 $return .=
'<option value="-1"';
489 if ($selected ==
'' || $selected ==
'-1') {
490 $return .=
' selected';
492 $return .=
'></option>';
494 if (!empty($productNature->records) && is_array($productNature->records)) {
495 foreach ($productNature->records as $lines) {
496 $return .=
'<option value="';
498 $return .= $lines->label;
500 $return .= $lines->code;
505 if ($mode == 1 && $lines->label == $selected) {
506 $return .=
' selected';
507 } elseif ($lines->code == $selected) {
508 $return .=
' selected';
512 $return .= $langs->trans($lines->label);
513 $return .=
'</option>';
516 $return .=
'</select>';
542 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')
544 global $conf, $langs;
546 dol_syslog(get_class($this).
"::selectLotStock $selected, $htmlname, $filterstatus, $empty, $disabled, $fk_product, $fk_entrepot, $empty_label, $forcecombo, $morecss", LOG_DEBUG);
549 $productIdArray = array();
550 if (!is_array($objectLines) || !count($objectLines)) {
551 if (!empty($fk_product) && $fk_product > 0) {
552 $productIdArray[] = (int) $fk_product;
555 foreach ($objectLines as $line) {
556 if ($line->fk_product) {
557 $productIdArray[] = $line->fk_product;
564 if ($conf->use_javascript_ajax && !$forcecombo) {
565 include_once DOL_DOCUMENT_ROOT.
'/core/lib/ajax.lib.php';
567 $out .= $comboenhancement;
570 $out .=
'<select class="flat'.($morecss ?
' '.$morecss :
'').
'"'.($disabled ?
' disabled' :
'').
' id="'.$htmlname.
'" name="'.($htmlname.($disabled ?
'_disabled' :
'')).
'">';
572 $out .=
'<option value="-1">'.($empty_label ? $empty_label :
' ').
'</option>';
574 if (!empty($fk_product) && $fk_product > 0) {
575 $productIdArray = array((
int) $fk_product);
577 foreach ($this->cache_lot as $key => $value) {
578 $productIdArray[] = $key;
582 foreach ($productIdArray as $productId) {
583 foreach ($this->cache_lot[$productId] as $id => $arraytypes) {
584 if (empty($fk_entrepot) || $fk_entrepot == $arraytypes[
'entrepot_id']) {
585 $label = $arraytypes[
'entrepot_label'].
' - ';
586 $label .= $arraytypes[
'batch'];
587 if ($arraytypes[
'qty'] <= 0) {
588 $label .=
' <span class=\'text-warning\'>('.$langs->trans(
"Stock").
' '.$arraytypes[
'qty'].
')</span>';
590 $label .=
' <span class=\'opacitymedium\'>('.$langs->trans(
"Stock").
' '.$arraytypes[
'qty'].
')</span>';
593 $out .=
'<option value="'.$id.
'"';
595 if ($selected == $id || ($selected ==
'ifone' && $nboflot == 1)) {
598 $out .=
' data-html="'.dol_escape_htmltag($label).
'"';
607 $out .=
'<input type="hidden" name="'.$htmlname.
'" value="'.(($selected > 0) ? $selected :
'').
'">';
625 public function selectLotDataList($htmlname =
'batch_id', $empty = 0, $fk_product = 0, $fk_entrepot = 0, $objectLines = array())
627 global $conf, $langs;
629 dol_syslog(get_class($this).
"::selectLotDataList $htmlname, $empty, $fk_product, $fk_entrepot,$objectLines", LOG_DEBUG);
632 $productIdArray = array();
633 if (!is_array($objectLines) || !count($objectLines)) {
634 if (!empty($fk_product) && $fk_product > 0) {
635 $productIdArray[] = (int) $fk_product;
638 foreach ($objectLines as $line) {
639 if ($line->fk_product) {
640 $productIdArray[] = $line->fk_product;
647 $out .=
'<datalist id="'.$htmlname.
'" >';
649 if (!empty($fk_product) && $fk_product > 0) {
650 $productIdArray = array((
int) $fk_product);
652 foreach ($this->cache_lot as $key => $value) {
653 $productIdArray[] = $key;
657 foreach ($productIdArray as $productId) {
658 if (array_key_exists($productId, $this->cache_lot)) {
659 foreach ($this->cache_lot[$productId] as $id => $arraytypes) {
660 if (empty($fk_entrepot) || $fk_entrepot == $arraytypes[
'entrepot_id']) {
661 $label = $arraytypes[
'entrepot_label'] .
' - ';
662 $label .= $arraytypes[
'batch'];
663 $out .=
'<option>' . $arraytypes[
'batch'] .
'</option>';
668 $out .=
'</datalist>';
683 global $conf, $langs;
685 $cacheLoaded =
false;
686 if (empty($productIdArray)) {
688 $this->cache_lot = array();
691 if (count($productIdArray) && count($this->cache_lot)) {
693 foreach ($productIdArray as $productId) {
694 $cacheLoaded = !empty($this->cache_lot[$productId]) ? true :
false;
698 return count($this->cache_lot);
701 $this->cache_lot = array();
702 $productIdList = implode(
',', $productIdArray);
703 $sql =
"SELECT pb.batch, pb.rowid, ps.fk_entrepot, pb.qty, e.ref as label, ps.fk_product";
704 $sql .=
" FROM ".$this->db->prefix().
"product_batch as pb";
705 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_stock as ps on ps.rowid = pb.fk_product_stock";
706 $sql .=
" LEFT JOIN ".$this->db->prefix().
"entrepot as e on e.rowid = ps.fk_entrepot AND e.entity IN (".
getEntity(
'stock').
")";
707 if (!empty($productIdList)) {
708 $sql .=
" WHERE ps.fk_product IN (".$this->db->sanitize($productIdList).
")";
710 $sql .=
" ORDER BY e.ref, pb.batch";
712 dol_syslog(get_class($this).
'::loadLotStock', LOG_DEBUG);
718 $obj = $this->
db->fetch_object(
$resql);
719 $this->cache_lot[$obj->fk_product][$obj->rowid][
'id'] = $obj->rowid;
720 $this->cache_lot[$obj->fk_product][$obj->rowid][
'batch'] = $obj->batch;
721 $this->cache_lot[$obj->fk_product][$obj->rowid][
'entrepot_id'] = $obj->fk_entrepot;
722 $this->cache_lot[$obj->fk_product][$obj->rowid][
'entrepot_label'] = $obj->label;
723 $this->cache_lot[$obj->fk_product][$obj->rowid][
'qty'] = $obj->qty;