Merge pull request #3733 from fappels/3.8-shipment-lot-delete

FIX: #3605 deleting a shipping
This commit is contained in:
Laurent Destailleur 2015-10-14 21:41:20 +02:00
commit 05686b532c
2 changed files with 69 additions and 37 deletions

View File

@ -7,7 +7,7 @@
* Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro> * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2014 Cedric GROSS <c.gross@kreiz-it.fr> * Copyright (C) 2014 Cedric GROSS <c.gross@kreiz-it.fr>
* Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com> * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
* Copyright (C) 2014 Francis Appels <francis.appels@yahoo.com> * Copyright (C) 2014-2015 Francis Appels <francis.appels@yahoo.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -1002,7 +1002,7 @@ class Expedition extends CommonObject
} }
} }
/** /**
* Delete shipment. * Delete shipment.
* Warning, do not delete a shipment if a delivery is linked to (with table llx_element_element) * 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() function delete()
{ {
global $conf, $langs, $user; 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; $error=0;
$this->error='';
// Add a protection to refuse deleting if shipment has at least one delivery // 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 $this->fetchObjectLinked($this->id, 'shipping', 0, 'delivery'); // Get deliveries linked to this shipment
@ -1024,15 +1028,6 @@ class Expedition extends CommonObject
} }
$this->db->begin(); $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 // Stock control
if ($conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_SHIPMENT && $this->statut > 0) if ($conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_SHIPMENT && $this->statut > 0)
{ {
@ -1041,7 +1036,7 @@ class Expedition extends CommonObject
$langs->load("agenda"); $langs->load("agenda");
// Loop on each product line to add a stock movement // 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.= " FROM ".MAIN_DB_PREFIX."commandedet as cd,";
$sql.= " ".MAIN_DB_PREFIX."expeditiondet as ed"; $sql.= " ".MAIN_DB_PREFIX."expeditiondet as ed";
$sql.= " WHERE ed.fk_expedition = ".$this->id; $sql.= " WHERE ed.fk_expedition = ".$this->id;
@ -1057,22 +1052,58 @@ class Expedition extends CommonObject
dol_syslog(get_class($this)."::delete movement index ".$i); dol_syslog(get_class($this)."::delete movement index ".$i);
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
//var_dump($this->lines[$i]);
$mouvS = new MouvementStock($this->db); $mouvS = new MouvementStock($this->db);
$mouvS->origin = &$this; // we do not log origin because it will be deleted
// We decrement stock of product (and sub-products) $mouvS->origin = null;
// We use warehouse selected for each line // get lot/serial
$result=$mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, $obj->subprice, $langs->trans("ShipmentDeletedInDolibarr",$this->ref)); $lotArray = null;
if ($result < 0) if ($conf->productbatch->enabled)
{ {
$error++; $lotArray = ExpeditionLineBatch::fetchAll($this->db,$obj->expeditiondet_id);
break; 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 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();
} }
} }
@ -1094,13 +1125,13 @@ class Expedition extends CommonObject
if ($this->db->query($sql)) if ($this->db->query($sql))
{ {
// Call trigger // Call trigger
$result=$this->call_trigger('SHIPPING_DELETE',$user); $result=$this->call_trigger('SHIPPING_DELETE',$user);
if ($result < 0) { $error++; } if ($result < 0) { $error++; }
// End call triggers // End call triggers
if (! $error) if (! $error)
{ {
$this->db->commit(); $this->db->commit();
// We delete PDFs // We delete PDFs
@ -1127,8 +1158,8 @@ class Expedition extends CommonObject
} }
return 1; return 1;
} }
else else
{ {
$this->db->rollback(); $this->db->rollback();
return -1; return -1;

View File

@ -189,11 +189,12 @@ class ExpeditionLineBatch extends CommonObject
$sql.= " WHERE fk_expeditiondet=".(int) $id_line_expdet; $sql.= " WHERE fk_expeditiondet=".(int) $id_line_expdet;
dol_syslog(__METHOD__ ."", LOG_DEBUG); dol_syslog(__METHOD__ ."", LOG_DEBUG);
$resql=$db->query($sql); $resql=$db->query($sql);
if ($resql) if ($resql)
{ {
$num=$db->num_rows($resql); $num=$db->num_rows($resql);
$i=0; $i=0;
$ret = array();
while ($i<$num) while ($i<$num)
{ {
$tmp=new self($db); $tmp=new self($db);