* Copyright (C) 2015-2017 Francis Appels * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/product/class/html.formproduct.class.php * \brief Fichier de la classe des fonctions predefinie de composants html */ require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php'; /** * Class with static methods for building HTML components related to products * Only components common to products and services must be here. */ class FormProduct { /** * @var DoliDB Database handler. */ public $db; /** * @var string Error code (or message) */ public $error = ''; // Cache arrays public $cache_warehouses = array(); public $cache_lot = array(); public $cache_workstations = array(); /** * Constructor * * @param DoliDB $db Database handler */ public function __construct($db) { $this->db = $db; } /** * Load in cache array list of warehouses * If fk_product is not 0, we do not use cache * * @param int $fk_product Add quantity of stock in label for product with id fk_product. Nothing if 0. * @param string $batch Add quantity of batch stock in label for product with batch name batch, batch name precedes batch_id. Nothing if ''. * @param string $status warehouse status filter, following comma separated filter options can be used * 'warehouseopen' = select products from open warehouses, * 'warehouseclosed' = select products from closed warehouses, * 'warehouseinternal' = select products from warehouses for internal correct/transfer only * @param boolean $sumStock sum total stock of a warehouse, default true * @param array $exclude warehouses ids to exclude * @param bool|int $stockMin [=false] Value of minimum stock to filter or false not not filter by minimum stock * @param string $orderBy [='e.ref'] Order by * @return int Nb of loaded lines, 0 if already loaded, <0 if KO * @throws Exception */ public function loadWarehouses($fk_product = 0, $batch = '', $status = '', $sumStock = true, $exclude = array(), $stockMin = false, $orderBy = 'e.ref') { global $conf, $langs; if (empty($fk_product) && count($this->cache_warehouses)) { return 0; // Cache already loaded and we do not want a list with information specific to a product } $warehouseStatus = array(); if (preg_match('/warehouseclosed/', $status)) { $warehouseStatus[] = Entrepot::STATUS_CLOSED; } if (preg_match('/warehouseopen/', $status)) { $warehouseStatus[] = Entrepot::STATUS_OPEN_ALL; } if (preg_match('/warehouseinternal/', $status)) { $warehouseStatus[] = Entrepot::STATUS_OPEN_INTERNAL; } $sql = "SELECT e.rowid, e.ref as label, e.description, e.fk_parent"; if (!empty($fk_product) && $fk_product > 0) { if (!empty($batch)) { $sql .= ", pb.qty as stock"; } else { $sql .= ", ps.reel as stock"; } } elseif ($sumStock) { $sql .= ", sum(ps.reel) as stock"; } $sql .= " FROM ".$this->db->prefix()."entrepot as e"; $sql .= " LEFT JOIN ".$this->db->prefix()."product_stock as ps on ps.fk_entrepot = e.rowid"; if (!empty($fk_product) && $fk_product > 0) { $sql .= " AND ps.fk_product = ".((int) $fk_product); if (!empty($batch)) { $sql .= " LEFT JOIN ".$this->db->prefix()."product_batch as pb on pb.fk_product_stock = ps.rowid AND pb.batch = '".$this->db->escape($batch)."'"; } } $sql .= " WHERE e.entity IN (".getEntity('stock').")"; if (count($warehouseStatus)) { $sql .= " AND e.statut IN (".$this->db->sanitize(implode(',', $warehouseStatus)).")"; } else { $sql .= " AND e.statut = 1"; } if (is_array($exclude) && !empty($exclude)) { $sql .= ' AND e.rowid NOT IN('.$this->db->sanitize(implode(',', $exclude)).')'; } // minimum stock if ($stockMin !== false) { if (!empty($fk_product) && $fk_product > 0) { if (!empty($batch)) { $sql .= " AND pb.qty > ".((float) $stockMin); } else { $sql .= " AND ps.reel > ".((float) $stockMin); } } } if ($sumStock && empty($fk_product)) { $sql .= " GROUP BY e.rowid, e.ref, e.description, e.fk_parent"; // minimum stock if ($stockMin !== false) { $sql .= " HAVING sum(ps.reel) > ".((float) $stockMin); } } $sql .= " ORDER BY ".$orderBy; dol_syslog(get_class($this).'::loadWarehouses', LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { $num = $this->db->num_rows($resql); $i = 0; while ($i < $num) { $obj = $this->db->fetch_object($resql); if ($sumStock) { $obj->stock = price2num($obj->stock, 5); } $this->cache_warehouses[$obj->rowid]['id'] = $obj->rowid; $this->cache_warehouses[$obj->rowid]['label'] = $obj->label; $this->cache_warehouses[$obj->rowid]['parent_id'] = $obj->fk_parent; $this->cache_warehouses[$obj->rowid]['description'] = $obj->description; $this->cache_warehouses[$obj->rowid]['stock'] = $obj->stock; $i++; } // Full label init foreach ($this->cache_warehouses as $obj_rowid => $tab) { $this->cache_warehouses[$obj_rowid]['full_label'] = $this->get_parent_path($tab); } return $num; } else { dol_print_error($this->db); return -1; } } /** * Load in cache array list of workstations * If fk_product is not 0, we do not use cache * * @param int $fk_product Add quantity of stock in label for product with id fk_product. Nothing if 0. * @param array $exclude warehouses ids to exclude * @param string $orderBy [='e.ref'] Order by * @return int Nb of loaded lines, 0 if already loaded, <0 if KO * @throws Exception */ public function loadWorkstations($fk_product = 0, $exclude = array(), $orderBy = 'w.ref') { global $conf, $langs; if (empty($fk_product) && count($this->cache_workstations)) { return 0; // Cache already loaded and we do not want a list with information specific to a product } $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"; $sql .= " FROM ".$this->db->prefix()."workstation_workstation as w"; $sql .= " WHERE 1 = 1"; if (!empty($fk_product) && $fk_product > 0) { $sql .= " AND w.fk_product = ".((int) $fk_product); } $sql .= " AND w.entity IN (".getEntity('workstation').")"; if (is_array($exclude) && !empty($exclude)) { $sql .= ' AND w.rowid NOT IN('.$this->db->sanitize(implode(',', $exclude)).')'; } $sql .= " ORDER BY ".$orderBy; dol_syslog(get_class($this).'::loadWorkstations', LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { $num = $this->db->num_rows($resql); $i = 0; while ($i < $num) { $obj = $this->db->fetch_object($resql); $this->cache_workstations[$obj->rowid]['id'] = $obj->rowid; $this->cache_workstations[$obj->rowid]['ref'] = $obj->ref; $this->cache_workstations[$obj->rowid]['label'] = $obj->label; $this->cache_workstations[$obj->rowid]['type'] = $obj->type; $this->cache_workstations[$obj->rowid]['nb_operators_required'] = $obj->nb_operators_required; $this->cache_workstations[$obj->rowid]['thm_operator_estimated'] = $obj->thm_operator_estimated; $this->cache_workstations[$obj->rowid]['thm_machine_estimated'] = $obj->thm_machine_estimated; $i++; } return $num; } else { dol_print_error($this->db); return -1; } } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Return full path to current warehouse in $tab (recursive function) * * @param array $tab warehouse data in $this->cache_warehouses line * @param String $final_label full label with all parents, separated by ' >> ' (completed on each call) * @return String full label with all parents, separated by ' >> ' */ private function get_parent_path($tab, $final_label = '') { //phpcs:enable if (empty($final_label)) { $final_label = $tab['label']; } if (empty($tab['parent_id'])) { return $final_label; } else { if (!empty($this->cache_warehouses[$tab['parent_id']])) { $final_label = $this->cache_warehouses[$tab['parent_id']]['label'].' >> '.$final_label; return $this->get_parent_path($this->cache_warehouses[$tab['parent_id']], $final_label); } } return $final_label; } /** * Return list of warehouses * * @param string|int $selected Id of preselected warehouse ('' or '-1' for no value, 'ifone' and 'ifonenodefault' = select value if one value otherwise no value, '-2' to use the default value from setup) * @param string $htmlname Name of html select html * @param string $filterstatus warehouse status filter, following comma separated filter options can be used * 'warehouseopen' = select products from open warehouses, * 'warehouseclosed' = select products from closed warehouses, * 'warehouseinternal' = select products from warehouses for internal correct/transfer only * @param int $empty 1=Can be empty, 0 if not * @param int $disabled 1=Select is disabled * @param int $fk_product Add quantity of stock in label for product with id fk_product. Nothing if 0. * @param string $empty_label Empty label if needed (only if $empty=1) * @param int $showstock 1=Show stock count * @param int $forcecombo 1=Force combo iso ajax select2 * @param array $events Events to add to select2 * @param string $morecss Add more css classes to HTML select * @param array $exclude Warehouses ids to exclude * @param int $showfullpath 1=Show full path of name (parent ref into label), 0=Show only ref of current warehouse * @param bool|int $stockMin [=false] Value of minimum stock to filter or false not not filter by minimum stock * @param string $orderBy [='e.ref'] Order by * @return string HTML select * * @throws Exception */ 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') { global $conf, $langs, $user, $hookmanager; dol_syslog(get_class($this)."::selectWarehouses $selected, $htmlname, $filterstatus, $empty, $disabled, $fk_product, $empty_label, $showstock, $forcecombo, $morecss", LOG_DEBUG); $out = ''; if (empty($conf->global->ENTREPOT_EXTRA_STATUS)) { $filterstatus = ''; } if (!empty($fk_product) && $fk_product > 0) { $this->cache_warehouses = array(); } $this->loadWarehouses($fk_product, '', $filterstatus, true, $exclude, $stockMin, $orderBy); $nbofwarehouses = count($this->cache_warehouses); if ($conf->use_javascript_ajax && !$forcecombo) { include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php'; $comboenhancement = ajax_combobox($htmlname, $events); $out .= $comboenhancement; } if (strpos($htmlname, 'search_') !== 0) { if (empty($user->fk_warehouse) || $user->fk_warehouse == -1) { if (($selected == '-2' || $selected == 'ifone') && !empty($conf->global->MAIN_DEFAULT_WAREHOUSE)) { $selected = $conf->global->MAIN_DEFAULT_WAREHOUSE; } } else { if (($selected == '-2' || $selected == 'ifone') && !empty($conf->global->MAIN_DEFAULT_WAREHOUSE_USER)) { $selected = $user->fk_warehouse; } } } $out .= ''; if ($empty) { $out .= ''; } foreach ($this->cache_workstations as $id => $arraytypes) { $label = $arraytypes['label']; $out .= '