NEW shipping set draft and update line

This commit is contained in:
atm-quentin 2018-10-02 15:49:31 +02:00
parent 985a268477
commit baed678b6e
4 changed files with 1153 additions and 152 deletions

File diff suppressed because it is too large Load Diff

View File

@ -642,9 +642,8 @@ class Expedition extends CommonObject
{
$numref = $this->getNextNumRef($soc);
}
else
{
$numref = "EXP".$this->id;
else {
$numref = $this->ref;
}
$this->newref = $numref;
@ -2170,6 +2169,138 @@ class Expedition extends CommonObject
return -1;
}
}
/**
* Set draft status
*
* @param User $user Object user that modify
* @return int <0 if KO, >0 if OK
*/
function set_draft($user)
{
global $conf,$langs;
$error=0;
// Protection
if ($this->statut <= self::STATUS_DRAFT)
{
return 0;
}
if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->expedition->creer))
|| (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->expedition->shipping_advance->validate))))
{
$this->error='Permission denied';
return -1;
}
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."expedition";
$sql.= " SET fk_statut = ".self::STATUS_DRAFT;
$sql.= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::set_draft", LOG_DEBUG);
if ($this->db->query($sql))
{
// If stock increment is done on closing
if (! $error && ! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT))
{
require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
$langs->load("agenda");
// Loop on each product line to add a stock movement
// TODO possibilite d'expedier a partir d'une propale ou autre origine
$sql = "SELECT cd.fk_product, cd.subprice,";
$sql.= " ed.rowid, ed.qty, ed.fk_entrepot,";
$sql.= " edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock";
$sql.= " FROM ".MAIN_DB_PREFIX."commandedet as cd,";
$sql.= " ".MAIN_DB_PREFIX."expeditiondet as ed";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid";
$sql.= " WHERE ed.fk_expedition = ".$this->id;
$sql.= " AND cd.rowid = ed.fk_origin_line";
dol_syslog(get_class($this)."::valid select details", LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
$cpt = $this->db->num_rows($resql);
for ($i = 0; $i < $cpt; $i++)
{
$obj = $this->db->fetch_object($resql);
if (empty($obj->edbrowid))
{
$qty = $obj->qty;
}
else
{
$qty = $obj->edbqty;
}
if ($qty <= 0) continue;
dol_syslog(get_class($this)."::reopen expedition movement index ".$i." ed.rowid=".$obj->rowid." edb.rowid=".$obj->edbrowid);
//var_dump($this->lines[$i]);
$mouvS = new MouvementStock($this->db);
$mouvS->origin = &$this;
if (empty($obj->edbrowid))
{
// line without batch detail
// We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
$result=$mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, -$qty, $obj->subprice, $langs->trans("ShipmentBackToDraftInDolibarr",$this->ref));
if ($result < 0) {
$this->error = $mouvS->error;
$this->errors = $mouvS->errors;
$error++; break;
}
}
else
{
// line with batch detail
// We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
$result=$mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, -$qty, $obj->subprice, $langs->trans("ShipmentBackToDraftInDolibarr",$this->ref), '', $this->db->jdate($obj->eatby), $this->db->jdate($obj->sellby), $obj->batch, $obj->fk_origin_stock);
if ($result < 0) {
$this->error = $mouvS->error;
$this->errors = $mouvS->errors;
$error++; break;
}
}
}
}
else
{
$this->error=$this->db->lasterror();
$error++;
}
}
if (!$error) {
// Call trigger
$result=$this->call_trigger('SHIPPING_UNVALIDATE',$user);
if ($result < 0) $error++;
}
if (!$error) {
$this->statut=self::STATUS_DRAFT;
$this->db->commit();
return 1;
}else {
$this->db->rollback();
return -1;
}
}
else
{
$this->error=$this->db->error();
$this->db->rollback();
return -1;
}
}
/**
* Create a document onto disk according to template module.
@ -2229,6 +2360,17 @@ class Expedition extends CommonObject
*/
class ExpeditionLigne extends CommonObjectLine
{
public $element='expeditiondet';
public $table_element='expeditiondet';
public $fk_origin_line;
/**
* Id of shipment
* @var int
*/
public $fk_expedition;
var $db;
// From llx_expeditiondet
@ -2236,6 +2378,12 @@ class ExpeditionLigne extends CommonObjectLine
var $qty_shipped;
var $fk_product;
var $detail_batch;
/**
* Id of warehouse
* @var int
*/
public $entrepot_id;
// From llx_commandedet or llx_propaldet
var $qty_asked;
@ -2252,10 +2400,7 @@ class ExpeditionLigne extends CommonObjectLine
var $total_localtax1; // Total Local tax 1
var $total_localtax2; // Total Local tax 2
public $element='expeditiondet';
public $table_element='expeditiondet';
public $fk_origin_line;
// Deprecated
/**
@ -2284,5 +2429,393 @@ class ExpeditionLigne extends CommonObjectLine
$this->db=$db;
}
/**
* Load line expedition
*
* @param int $rowid Id line order
* @return int <0 if KO, >0 if OK
*/
function fetch($rowid)
{
$sql = 'SELECT ed.rowid, ed.fk_expedition, ed.fk_entrepot, ed.fk_origin_line, ed.qty, ed.rang';
$sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as ed';
$sql.= ' WHERE ed.rowid = '.$rowid;
$result = $this->db->query($sql);
if ($result)
{
$objp = $this->db->fetch_object($result);
$this->id = $objp->rowid;
$this->fk_expedition = $objp->fk_expedition;
$this->entrepot_id = $objp->fk_entrepot;
$this->fk_origin_line = $objp->fk_origin_line;
$this->qty = $objp->qty;
$this->rang = $objp->rang;
$this->db->free($result);
return 1;
}
else
{
$this->errors[] = $this->db->lasterror();
$this->error = $this->db->lasterror();
return -1;
}
}
/**
* Insert line into database
*
* @param User $user User that modify
* @param int $notrigger 1 = disable triggers
* @return int <0 if KO, line id >0 if OK
*/
function insert($user=null, $notrigger=0)
{
global $langs, $conf;
$error=0;
// Check parameters
if (empty($this->fk_expedition) || empty($this->fk_origin_line) || ! is_numeric($this->qty))
{
$this->error = 'ErrorMandatoryParametersNotProvided';
return -1;
}
// Clean parameters
if (empty($this->entrepot_id)) $this->entrepot_id='null';
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."expeditiondet (";
$sql.= "fk_expedition";
$sql.= ", fk_entrepot";
$sql.= ", fk_origin_line";
$sql.= ", qty";
$sql.= ") VALUES (";
$sql.= $this->fk_expedition;
$sql.= ", ".$this->entrepot_id;
$sql.= ", ".$this->fk_origin_line;
$sql.= ", ".$this->qty;
$sql.= ")";
dol_syslog(get_class($this)."::insert", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."expeditiondet");
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
if (! $error && ! $notrigger)
{
// Call trigger
$result=$this->call_trigger('LINESHIPPING_INSERT',$user);
if ($result < 0)
{
$error++;
}
// End call triggers
}
if (! $error) {
$this->db->commit();
return $this->id;
}
foreach($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
else
{
$error++;
}
}
/**
* Delete shipment line.
*
* @param User $user User that modify
* @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int >0 if OK, <0 if KO
*/
function delete($user = null, $notrigger = 0)
{
global $conf;
$error=0;
$this->db->begin();
// delete batch expedition line
if ($conf->productbatch->enabled)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet_batch";
$sql.= " WHERE fk_expeditiondet = ".$this->id;
if (!$this->db->query($sql))
{
$this->errors[]=$this->db->lasterror()." - sql=$sql";
$error++;
}
}
$sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet";
$sql.= " WHERE rowid = ".$this->id;
if (! $error && $this->db->query($sql))
{
// Remove extrafields
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->deleteExtraFields();
if ($result < 0)
{
$this->errors[]=$this->error;
$error++;
}
}
if (! $error && ! $notrigger)
{
// Call trigger
$result=$this->call_trigger('LINESHIPPING_DELETE',$user);
if ($result < 0)
{
$this->errors[]=$this->error;
$error++;
}
// End call triggers
}
}
else
{
$this->errors[]=$this->db->lasterror()." - sql=$sql";
$error++;
}
if (! $error) {
$this->db->commit();
return 1;
}
else
{
foreach($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
}
/**
* Update a line in database
*
* @param User $user User that modify
* @param int $notrigger 1 = disable triggers
* @return int < 0 if KO, > 0 if OK
*/
function update($user = null, $notrigger = 0)
{
global $conf;
$error=0;
dol_syslog(get_class($this)."::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty");
$this->db->begin();
// Clean parameters
if (empty($this->qty)) $this->qty=0;
$qty=price2num($this->qty);
$remainingQty = 0;
$batch = null;
$batch_id = null;
$expedition_batch_id = null;
if (is_array($this->detail_batch)) // array of ExpeditionLineBatch
{
if (count($this->detail_batch) > 1)
{
dol_syslog(get_class($this).'::update only possible for one batch', LOG_ERR);
$this->errors[]='ErrorBadParameters';
$error++;
}
else
{
$batch = $this->detail_batch[0]->batch;
$batch_id = $this->detail_batch[0]->fk_origin_stock;
$expedition_batch_id = $this->detail_batch[0]->id;
if ($this->entrepot_id != $this->detail_batch[0]->entrepot_id)
{
dol_syslog(get_class($this).'::update only possible for batch of same warehouse', LOG_ERR);
$this->errors[]='ErrorBadParameters';
$error++;
}
$qty = price2num($this->detail_batch[0]->dluo_qty);
}
}
else if (! empty($this->detail_batch))
{
$batch = $this->detail_batch->batch;
$batch_id = $this->detail_batch->fk_origin_stock;
$expedition_batch_id = $this->detail_batch->id;
if ($this->entrepot_id != $this->detail_batch->entrepot_id)
{
dol_syslog(get_class($this).'::update only possible for batch of same warehouse', LOG_ERR);
$this->errors[]='ErrorBadParameters';
$error++;
}
$qty = price2num($this->detail_batch->dluo_qty);
}
// check parameters
if (! isset($this->id) || ! isset($this->entrepot_id))
{
dol_syslog(get_class($this).'::update missing line id and/or warehouse id', LOG_ERR);
$this->errors[]='ErrorMandatoryParametersNotProvided';
$error++;
return -1;
}
// update lot
if (! empty($batch) && $conf->productbatch->enabled)
{
dol_syslog(get_class($this)."::update expedition batch id=$expedition_batch_id, batch_id=$batch_id, batch=$batch");
if (empty($batch_id) || empty($this->fk_product)) {
dol_syslog(get_class($this).'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR);
$this->errors[]='ErrorMandatoryParametersNotProvided';
$error++;
}
// fetch remaining lot qty
require_once DOL_DOCUMENT_ROOT.'/expedition/class/expeditionbatch.class.php';
if (! $error && ($lotArray = ExpeditionLineBatch::fetchAll($this->db, $this->id)) < 0)
{
$this->errors[]=$this->db->lasterror()." - ExpeditionLineBatch::fetchAll";
$error++;
}
else
{
// caculate new total line qty
foreach ($lotArray as $lot)
{
if ($expedition_batch_id != $lot->id)
{
$remainingQty += $lot->dluo_qty;
}
}
$qty += $remainingQty;
//fetch lot details
// fetch from product_lot
require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
$lot = new Productlot($this->db);
if ($lot->fetch(0,$this->fk_product,$batch) < 0)
{
$this->errors[] = $lot->errors;
$error++;
}
if (! $error && ! empty($expedition_batch_id))
{
// delete lot expedition line
$sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet_batch";
$sql.= " WHERE fk_expeditiondet = ".$this->id;
$sql.= " AND rowid = ".$expedition_batch_id;
if (!$this->db->query($sql))
{
$this->errors[]=$this->db->lasterror()." - sql=$sql";
$error++;
}
}
if (! $error && $this->detail_batch->dluo_qty > 0)
{
// create lot expedition line
if (isset($lot->id))
{
$shipmentLot = new ExpeditionLineBatch($this->db);
$shipmentLot->batch = $lot->batch;
$shipmentLot->eatby = $lot->eatby;
$shipmentLot->sellby = $lot->sellby;
$shipmentLot->entrepot_id = $this->detail_batch->entrepot_id;
$shipmentLot->dluo_qty = $this->detail_batch->dluo_qty;
$shipmentLot->fk_origin_stock = $batch_id;
if ($shipmentLot->create($this->id) < 0)
{
$this->errors[]=$shipmentLot->errors;
$error++;
}
}
}
}
}
if (! $error)
{
// update line
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
$sql.= " fk_entrepot = ".($this->entrepot_id > 0 ? $this->entrepot_id : 'null');
$sql.= " , qty = ".$qty;
$sql.= " WHERE rowid = ".$this->id;
if (!$this->db->query($sql))
{
$this->errors[]=$this->db->lasterror()." - sql=$sql";
$error++;
}
else
{
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$this->errors[]=$this->error;
$error++;
}
}
}
}
if (! $error && ! $notrigger)
{
// Call trigger
$result=$this->call_trigger('LINESHIPPING_UPDATE',$user);
if ($result < 0)
{
$this->errors[]=$this->error;
$error++;
}
// End call triggers
}
if (!$error) {
$this->db->commit();
return 1;
}
else
{
foreach($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
}
}

View File

@ -55,6 +55,7 @@ MemberSubscriptionAddedInDolibarr=Subscription for member %s added
ShipmentValidatedInDolibarr=Shipment %s validated
ShipmentClassifyClosedInDolibarr=Shipment %s classified billed
ShipmentUnClassifyCloseddInDolibarr=Shipment %s classified reopened
ShipmentBackToDraftInDolibarr=Shipment %s go back to draft status
ShipmentDeletedInDolibarr=Shipment %s deleted
OrderCreatedInDolibarr=Order %s created
OrderValidatedInDolibarr=Order %s validated

View File

@ -55,6 +55,7 @@ MemberSubscriptionAddedInDolibarr=Souscription adhérent %s
ShipmentValidatedInDolibarr=Expédition %s validée
ShipmentClassifyClosedInDolibarr=Expédition %s classée payée
ShipmentUnClassifyCloseddInDolibarr=Expédition %s réouverte
ShipmentBackToDraftInDolibarr=Expédition %s repassée en brouillon
ShipmentDeletedInDolibarr=Expédition %s supprimée
OrderCreatedInDolibarr=Commande %s créée
OrderValidatedInDolibarr=Commande %s validée