NEW : auto delete product_stock_entrepot elements when we drop a product or a warehouse

This commit is contained in:
gauthier 2016-09-21 16:21:23 +02:00
parent 6034b21746
commit 2556e59ccf
4 changed files with 36 additions and 35 deletions

View File

@ -926,7 +926,7 @@ class Product extends CommonObject
// Delete all child tables // Delete all child tables
if (! $error) if (! $error)
{ {
$elements = array('product_fournisseur_price','product_price','product_lang','categorie_product','product_stock','product_customer_price','product_lot'); $elements = array('product_fournisseur_price','product_price','product_lang','categorie_product','product_stock','product_customer_price','product_lot','product_stock_entrepot');
foreach($elements as $table) foreach($elements as $table)
{ {
if (! $error) if (! $error)

View File

@ -221,17 +221,24 @@ class Entrepot extends CommonObject
{ {
$this->db->begin(); $this->db->begin();
$sql = "DELETE FROM ".MAIN_DB_PREFIX."stock_mouvement"; $elements = array('stock_mouvement','product_stock','product_stock_entrepot');
foreach($elements as $table)
{
if (! $error)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX.$table;
$sql.= " WHERE fk_entrepot = " . $this->id; $sql.= " WHERE fk_entrepot = " . $this->id;
dol_syslog(get_class($this)."::delete", LOG_DEBUG); dol_syslog(get_class($this)."::delete", LOG_DEBUG);
$resql1=$this->db->query($sql); $result=$this->db->query($sql);
if (! $result)
{
$error++;
$this->errors[] = $this->db->lasterror();
}
}
}
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_stock"; if (! $error)
$sql.= " WHERE fk_entrepot = " . $this->id;
dol_syslog(get_class($this)."::delete", LOG_DEBUG);
$resql2=$this->db->query($sql);
if ($resql1 && $resql2)
{ {
$sql = "DELETE FROM ".MAIN_DB_PREFIX."entrepot"; $sql = "DELETE FROM ".MAIN_DB_PREFIX."entrepot";
$sql.= " WHERE rowid = " . $this->id; $sql.= " WHERE rowid = " . $this->id;

View File

@ -230,6 +230,7 @@ class ProductStockEntrepot extends CommonObject
* Load object in memory from the database * Load object in memory from the database
* *
* @param int $fk_product Product from which we want to get limit and desired stock by warehouse * @param int $fk_product Product from which we want to get limit and desired stock by warehouse
* @param int $fk_entrepot Warehouse in which we want to get products limit and desired stock
* @param string $sortorder Sort Order * @param string $sortorder Sort Order
* @param string $sortfield Sort field * @param string $sortfield Sort field
* @param int $limit offset limit * @param int $limit offset limit
@ -239,7 +240,7 @@ class ProductStockEntrepot extends CommonObject
* *
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
public function fetchAll($fk_product='', $sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND') public function fetchAll($fk_product='', $fk_entrepot='', $sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND')
{ {
dol_syslog(__METHOD__, LOG_DEBUG); dol_syslog(__METHOD__, LOG_DEBUG);
@ -268,26 +269,19 @@ class ProductStockEntrepot extends CommonObject
if (count($sqlwhere) > 0) $sql .= ' AND ' . implode(' '.$filtermode.' ', $sqlwhere); if (count($sqlwhere) > 0) $sql .= ' AND ' . implode(' '.$filtermode.' ', $sqlwhere);
if(!empty($fk_product)) $sql .= ' AND fk_product = '.$fk_product; if(!empty($fk_product)) $sql .= ' AND fk_product = '.$fk_product;
elseif(!empty($fk_entrepot)) $sql .= ' AND fk_entrepot = '.$fk_entrepot;
// "elseif" used instead of "if" because getting list with specified fk_product and specified fk_entrepot would be the same as doing a fetch
if (!empty($sortfield)) $sql .= $this->db->order($sortfield,$sortorder); if (!empty($sortfield)) $sql .= $this->db->order($sortfield,$sortorder);
if (!empty($limit)) $sql .= ' ' . $this->db->plimit($limit + 1, $offset); if (!empty($limit)) $sql .= ' ' . $this->db->plimit($limit + 1, $offset);
$this->lines = array(); $lines = array();
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
$num = $this->db->num_rows($resql);
while ($obj = $this->db->fetch_object($resql)) { while ($obj = $this->db->fetch_object($resql)) {
$lines[$obj->rowid] = array(
//$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;
$this->lines[$obj->rowid] = array(
'id'=>$obj->rowid 'id'=>$obj->rowid
,'fk_product'=>$obj->fk_product ,'fk_product'=>$obj->fk_product
,'fk_entrepot'=>$obj->fk_entrepot ,'fk_entrepot'=>$obj->fk_entrepot
@ -297,7 +291,7 @@ class ProductStockEntrepot extends CommonObject
} }
$this->db->free($resql); $this->db->free($resql);
return $num; return $lines;
} else { } else {
$this->errors[] = 'Error ' . $this->db->lasterror(); $this->errors[] = 'Error ' . $this->db->lasterror();
dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);

View File

@ -937,12 +937,12 @@ if(!empty($conf->global->STOCK_ALLOW_ADD_LIMIT_STOCK_BY_WAREHOUSE)) {
print '</tr>'; print '</tr>';
$pse = new ProductStockEntrepot($db); $pse = new ProductStockEntrepot($db);
$pse->fetchAll(GETPOST('id')); $lines = $pse->fetchAll(GETPOST('id'));
if(!empty($pse->lines)) { if(!empty($lines)) {
$var=false; $var=false;
foreach($pse->lines as $line) { foreach($lines as $line) {
$ent = new Entrepot($db); $ent = new Entrepot($db);
$ent->fetch($line['fk_entrepot']); $ent->fetch($line['fk_entrepot']);