From 68aac4476c7c2f5266a483dae30f48088614b3e0 Mon Sep 17 00:00:00 2001 From: fappels Date: Wed, 14 Oct 2015 12:16:54 +0200 Subject: [PATCH] FIX: #3605 deleting a shipping Get lots to move back to stock. Move lots back to stock. Do not log object because it will be deleted Improve error reporting --- htdocs/expedition/class/expedition.class.php | 97 ++++++++++++------- .../class/expeditionbatch.class.php | 9 +- 2 files changed, 69 insertions(+), 37 deletions(-) diff --git a/htdocs/expedition/class/expedition.class.php b/htdocs/expedition/class/expedition.class.php index b43afdfc206..6c2214c7e82 100644 --- a/htdocs/expedition/class/expedition.class.php +++ b/htdocs/expedition/class/expedition.class.php @@ -7,7 +7,7 @@ * Copyright (C) 2013 Florian Henry * Copyright (C) 2014 Cedric GROSS * Copyright (C) 2014-2015 Marcos GarcĂ­a - * Copyright (C) 2014 Francis Appels + * Copyright (C) 2014-2015 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 @@ -1002,7 +1002,7 @@ class Expedition extends CommonObject } } - /** + /** * Delete shipment. * Warning, do not delete a shipment if a delivery is linked to (with table llx_element_element) * @@ -1011,9 +1011,13 @@ class Expedition extends CommonObject function delete() { global $conf, $langs, $user; - require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; - + require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; + if ($conf->productbatch->enabled) + { + require_once DOL_DOCUMENT_ROOT.'/expedition/class/expeditionbatch.class.php'; + } $error=0; + $this->error=''; // Add a protection to refuse deleting if shipment has at least one delivery $this->fetchObjectLinked($this->id, 'shipping', 0, 'delivery'); // Get deliveries linked to this shipment @@ -1024,15 +1028,6 @@ class Expedition extends CommonObject } $this->db->begin(); - - if ($conf->productbatch->enabled) - { - require_once DOL_DOCUMENT_ROOT.'/expedition/class/expeditionbatch.class.php'; - if (ExpeditionLineBatch::deletefromexp($this->db,$this->id) < 0) - { - $error++;$this->errors[]="Error ".$this->db->lasterror(); - } - } // Stock control if ($conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_SHIPMENT && $this->statut > 0) { @@ -1041,7 +1036,7 @@ class Expedition extends CommonObject $langs->load("agenda"); // Loop on each product line to add a stock movement - $sql = "SELECT cd.fk_product, cd.subprice, ed.qty, ed.fk_entrepot"; + $sql = "SELECT cd.fk_product, cd.subprice, ed.qty, ed.fk_entrepot, ed.rowid as expeditiondet_id"; $sql.= " FROM ".MAIN_DB_PREFIX."commandedet as cd,"; $sql.= " ".MAIN_DB_PREFIX."expeditiondet as ed"; $sql.= " WHERE ed.fk_expedition = ".$this->id; @@ -1056,26 +1051,62 @@ class Expedition extends CommonObject { dol_syslog(get_class($this)."::delete movement index ".$i); $obj = $this->db->fetch_object($resql); - - //var_dump($this->lines[$i]); + $mouvS = new MouvementStock($this->db); - $mouvS->origin = &$this; - // We decrement stock of product (and sub-products) - // We use warehouse selected for each line - $result=$mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, $obj->subprice, $langs->trans("ShipmentDeletedInDolibarr",$this->ref)); - if ($result < 0) + // we do not log origin because it will be deleted + $mouvS->origin = null; + // get lot/serial + $lotArray = null; + if ($conf->productbatch->enabled) { - $error++; - break; + $lotArray = ExpeditionLineBatch::fetchAll($this->db,$obj->expeditiondet_id); + if (! is_array($lotArray)) + { + $error++;$this->errors[]="Error ".$this->db->lasterror(); + } } + if (empty($lotArray)) { + // no lot/serial + // We increment stock of product (and sub-products) + // We use warehouse selected for each line + $result=$mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, $obj->subprice, $langs->trans("ShipmentDeletedInDolibarr", $this->ref)); + if ($result < 0) + { + $error++;$this->errors=$this->errors + $mouvS->errors; + break; + } + } + else + { + // We increment stock of batches + // We use warehouse selected for each line + foreach($lotArray as $lot) + { + $result=$mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $lot->dluo_qty, $obj->subprice, $langs->trans("ShipmentDeletedInDolibarr", $this->ref), $lot->eatby, $lot->sellby, $lot->batch); + if ($result < 0) + { + $error++;$this->errors=$this->errors + $mouvS->errors; + break; + } + } + } } } else { - $error++; + $error++;$this->errors[]="Error ".$this->db->lasterror(); } } - + + // delete batch expedition line + if (! $error && $conf->productbatch->enabled) + { + if (ExpeditionLineBatch::deletefromexp($this->db,$this->id) < 0) + { + $error++;$this->errors[]="Error ".$this->db->lasterror(); + } + } + if (! $error) { $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet"; @@ -1094,13 +1125,13 @@ class Expedition extends CommonObject if ($this->db->query($sql)) { - // Call trigger - $result=$this->call_trigger('SHIPPING_DELETE',$user); - if ($result < 0) { $error++; } - // End call triggers + // Call trigger + $result=$this->call_trigger('SHIPPING_DELETE',$user); + if ($result < 0) { $error++; } + // End call triggers - if (! $error) - { + if (! $error) + { $this->db->commit(); // We delete PDFs @@ -1127,8 +1158,8 @@ class Expedition extends CommonObject } return 1; - } - else + } + else { $this->db->rollback(); return -1; diff --git a/htdocs/expedition/class/expeditionbatch.class.php b/htdocs/expedition/class/expeditionbatch.class.php index fd424fc4990..49a59b726e1 100644 --- a/htdocs/expedition/class/expeditionbatch.class.php +++ b/htdocs/expedition/class/expeditionbatch.class.php @@ -189,11 +189,12 @@ class ExpeditionLineBatch extends CommonObject $sql.= " WHERE fk_expeditiondet=".(int) $id_line_expdet; dol_syslog(__METHOD__ ."", LOG_DEBUG); - $resql=$db->query($sql); - if ($resql) - { + $resql=$db->query($sql); + if ($resql) + { $num=$db->num_rows($resql); - $i=0; + $i=0; + $ret = array(); while ($i<$num) { $tmp=new self($db);