From a4d0eed50316b968ef2314aacc608bccd2a2ceb5 Mon Sep 17 00:00:00 2001 From: gauthier Date: Mon, 19 Sep 2016 17:33:29 +0200 Subject: [PATCH] NEW : Save product stock warehouse in database and display list --- htdocs/langs/en_US/stocks.lang | 3 + .../class/productstockentrepot.class.php | 103 +++++++----------- htdocs/product/stock/product.php | 70 +++++++++++- 3 files changed, 107 insertions(+), 69 deletions(-) diff --git a/htdocs/langs/en_US/stocks.lang b/htdocs/langs/en_US/stocks.lang index 53dc0c087e5..793c2889c7c 100644 --- a/htdocs/langs/en_US/stocks.lang +++ b/htdocs/langs/en_US/stocks.lang @@ -136,3 +136,6 @@ OpenInternal=Open for internal actions OpenShipping=Open for shippings OpenDispatch=Open for dispatch UseDispatchStatus=Use dispatch status (aprouve/refuse) +ProductStockWarehouseCreated=Stock limit for alert and desired optimal stock correctly created +ProductStockWarehouseUpdated=Stock limit for alert and desired optimal stock correctly updated +AddNewProductStockWarehouse=Set new limit for alert and desired optimal stock \ No newline at end of file diff --git a/htdocs/product/stock/class/productstockentrepot.class.php b/htdocs/product/stock/class/productstockentrepot.class.php index 764baee1dd3..afa72d799f7 100644 --- a/htdocs/product/stock/class/productstockentrepot.class.php +++ b/htdocs/product/stock/class/productstockentrepot.class.php @@ -89,23 +89,11 @@ class ProductStockEntrepot extends CommonObject // Clean parameters - if (isset($this->fk_product)) { - $this->fk_product = trim($this->fk_product); - } - if (isset($this->fk_entrepot)) { - $this->fk_entrepot = trim($this->fk_entrepot); - } - if (isset($this->seuil_stock_alerte)) { - $this->seuil_stock_alerte = trim($this->seuil_stock_alerte); - } - if (isset($this->desiredstock)) { - $this->desiredstock = trim($this->desiredstock); - } - if (isset($this->import_key)) { - $this->import_key = trim($this->import_key); - } - - + if (isset($this->fk_product)) $this->fk_product = trim($this->fk_product); + if (isset($this->fk_entrepot)) $this->fk_entrepot = trim($this->fk_entrepot); + if (isset($this->seuil_stock_alerte)) $this->seuil_stock_alerte = trim($this->seuil_stock_alerte); + if (isset($this->desiredstock)) $this->desiredstock = trim($this->desiredstock); + if (isset($this->import_key)) $this->import_key = trim($this->import_key); // Check parameters // Put here code to add control on parameters values @@ -115,8 +103,8 @@ class ProductStockEntrepot extends CommonObject $sql.= 'fk_product,'; $sql.= 'fk_entrepot,'; - $sql.= 'seuil_stock_alerte'; - $sql.= 'desiredstock'; + $sql.= 'seuil_stock_alerte,'; + $sql.= 'desiredstock,'; $sql.= 'import_key'; @@ -170,12 +158,15 @@ class ProductStockEntrepot extends CommonObject * Load object in memory from the database * * @param int $id Id object - * @param string $ref Ref + * @param int $fk_product Id product + * @param int $fk_entrepot Id warehouse * * @return int <0 if KO, 0 if not found, >0 if OK */ - public function fetch($id, $ref = null) + public function fetch($id, $fk_product, $fk_entrepot) { + if(empty($id) && (empty($fk_product) || empty($fk_entrepot))) return -1; + dol_syslog(__METHOD__, LOG_DEBUG); $sql = 'SELECT'; @@ -190,12 +181,9 @@ class ProductStockEntrepot extends CommonObject $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t'; - if (null !== $ref) { - $sql .= ' WHERE t.ref = ' . '\'' . $ref . '\''; - } else { - $sql .= ' WHERE t.rowid = ' . $id; - } - + if(!empty($id)) $sql .= ' WHERE t.rowid = ' . $id; + else $sql.= ' WHERE t.fk_product = '.$fk_product.' AND t.fk_entrepot = '.$fk_entrepot; + $resql = $this->db->query($sql); if ($resql) { $numrows = $this->db->num_rows($resql); @@ -250,7 +238,7 @@ class ProductStockEntrepot extends CommonObject * * @return int <0 if KO, >0 if OK */ - public function fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND') + public function fetchAll($fk_product='', $sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND') { dol_syslog(__METHOD__, LOG_DEBUG); @@ -266,7 +254,9 @@ class ProductStockEntrepot extends CommonObject $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t'; - + + $sql .= ' WHERE 1=1'; + // Manage filter $sqlwhere = array(); if (count($filter) > 0) { @@ -274,16 +264,13 @@ class ProductStockEntrepot extends CommonObject $sqlwhere [] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\''; } } - if (count($sqlwhere) > 0) { - $sql .= ' WHERE ' . implode(' '.$filtermode.' ', $sqlwhere); - } - - if (!empty($sortfield)) { - $sql .= $this->db->order($sortfield,$sortorder); - } - if (!empty($limit)) { - $sql .= ' ' . $this->db->plimit($limit + 1, $offset); - } + if (count($sqlwhere) > 0) $sql .= ' AND ' . implode(' '.$filtermode.' ', $sqlwhere); + + if(!empty($fk_product)) $sql .= ' AND fk_product = '.$fk_product; + + if (!empty($sortfield)) $sql .= $this->db->order($sortfield,$sortorder); + if (!empty($limit)) $sql .= ' ' . $this->db->plimit($limit + 1, $offset); + $this->lines = array(); $resql = $this->db->query($sql); @@ -291,20 +278,21 @@ class ProductStockEntrepot extends CommonObject $num = $this->db->num_rows($resql); while ($obj = $this->db->fetch_object($resql)) { - $line = new ProductStockEntrepotLine(); - - $line->id = $obj->rowid; - $line->tms = $this->db->jdate($obj->tms); - $line->fk_product = $obj->fk_product; - $line->fk_entrepot = $obj->fk_entrepot; + //$line->tms = $this->db->jdate($obj->tms); $line->seuil_stock_alerte = $obj->seuil_stock_alerte; $line->desiredstock = $obj->desiredstock; - $line->import_key = $obj->import_key; + //$line->import_key = $obj->import_key; - $this->lines[$line->id] = $line; + $this->lines[$obj->rowid] = array( + 'id'=>$obj->rowid + ,'fk_product'=>$obj->fk_product + ,'fk_entrepot'=>$obj->fk_entrepot + ,'seuil_stock_alerte'=>$obj->seuil_stock_alerte + ,'desiredstock'=>$obj->desiredstock + ); } $this->db->free($resql); @@ -333,22 +321,11 @@ class ProductStockEntrepot extends CommonObject // Clean parameters - if (isset($this->fk_product)) { - $this->fk_product = trim($this->fk_product); - } - if (isset($this->fk_entrepot)) { - $this->fk_entrepot = trim($this->fk_entrepot); - } - if (isset($this->seuil_stock_alerte)) { - $this->seuil_stock_alerte = trim($this->seuil_stock_alerte); - } - if (isset($this->desiredstock)) { - $this->desiredstock = trim($this->desiredstock); - } - if (isset($this->import_key)) { - $this->import_key = trim($this->import_key); - } - + if (isset($this->fk_product)) $this->fk_product = trim($this->fk_product); + if (isset($this->fk_entrepot)) $this->fk_entrepot = trim($this->fk_entrepot); + if (isset($this->seuil_stock_alerte)) $this->seuil_stock_alerte = trim($this->seuil_stock_alerte); + if (isset($this->desiredstock)) $this->desiredstock = trim($this->desiredstock); + if (isset($this->import_key)) $this->import_key = trim($this->import_key); // Check parameters diff --git a/htdocs/product/stock/product.php b/htdocs/product/stock/product.php index 1f90c473248..a611a7b663a 100644 --- a/htdocs/product/stock/product.php +++ b/htdocs/product/stock/product.php @@ -35,6 +35,7 @@ require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php'; +require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productstockentrepot.class.php'; if (! empty($conf->productbatch->enabled)) require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php'; $langs->load("products"); @@ -71,8 +72,44 @@ if ($cancel) $action=''; if($action == 'addlimitstockwarehouse') { - //var_dump($_REQUEST);exit; - $action=''; + $seuil_stock_alerte = GETPOST('seuil_stock_alerte'); + $desiredstock = GETPOST('desiredstock'); + + $maj_ok = true; + if($seuil_stock_alerte == '') { + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("StockLimit")), null, 'errors'); + $maj_ok = false; + } + if($desiredstock == '') { + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("DesiredStock")), null, 'errors'); + $maj_ok = false; + } + + if($maj_ok) { + + $pse = new ProductStockEntrepot($db); + if($pse->fetch('', GETPOST('id'), GETPOST('fk_entrepot')) > 0) { + + // Update + $pse->seuil_stock_alerte = $seuil_stock_alerte; + $pse->desiredstock = $desiredstock; + if($pse->update($user) > 0) setEventMessage($langs->trans('ProductStockWarehouseUpdated')); + + } else { + + // Create + $pse->fk_entrepot = GETPOST('fk_entrepot'); + $pse->fk_product = GETPOST('id'); + $pse->seuil_stock_alerte = GETPOST('seuil_stock_alerte'); + $pse->desiredstock = GETPOST('desiredstock'); + if($pse->create($user) > 0) setEventMessage($langs->trans('ProductStockWarehouseCreated')); + + } + + } + + header("Location: ".$_SERVER["PHP_SELF"]."?id=".GETPOST('id')); + exit; } @@ -834,7 +871,7 @@ print ""; if(!empty($conf->global->STOCK_ALLOW_ADD_LIMIT_STOCK_BY_WAREHOUSE)) { print '

'; - print_titre('Indiquer une limite pour alerte et un stock optimal'); + print_titre($langs->trans('AddNewProductStockWarehouse')); //print '
'; print '
'; @@ -842,12 +879,33 @@ if(!empty($conf->global->STOCK_ALLOW_ADD_LIMIT_STOCK_BY_WAREHOUSE)) { print ''; print ''; - print ''; - print ''; + print ''; + print ''; print ''; - print ''; + print ''; print ''; + $pse = new ProductStockEntrepot($db); + $pse->fetchAll(GETPOST('id')); + + if(!empty($pse->lines)) { + + $var=false; + foreach($pse->lines as $line) { + + $ent = new Entrepot($db); + $ent->fetch($line['fk_entrepot']); + print ''; + print ''; + print ''; + print ''; + print ''; + $var=!$var; + + } + + } + print "
'.$formproduct->selectWarehouses().'
'.$formproduct->selectWarehouses('', 'fk_entrepot').'
'.$ent->getNomUrl(3).''.$line['seuil_stock_alerte'].''.$line['desiredstock'].' 
"; print '
';