Add Line triggers, Improve line error handling, ...

Add triggers,
Improve line error handling,
use line->insert in create_line
This commit is contained in:
fappels 2017-10-18 17:01:17 +02:00
parent 907ae36cdf
commit 2ea687a60b

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-2015 Francis Appels <francis.appels@yahoo.com> * Copyright (C) 2014-2017 Francis Appels <francis.appels@yahoo.com>
* Copyright (C) 2015 Claudio Aschieri <c.aschieri@19.coop> * Copyright (C) 2015 Claudio Aschieri <c.aschieri@19.coop>
* Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es> * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
* *
@ -372,49 +372,19 @@ class Expedition extends CommonObject
* @return int <0 if KO, line_id if OK * @return int <0 if KO, line_id if OK
*/ */
function create_line($entrepot_id, $origin_line_id, $qty,$array_options=0) function create_line($entrepot_id, $origin_line_id, $qty,$array_options=0)
{
global $conf;
$error = 0;
$line_id = 0;
$sql = "INSERT INTO ".MAIN_DB_PREFIX."expeditiondet (";
$sql.= "fk_expedition";
$sql.= ", fk_entrepot";
$sql.= ", fk_origin_line";
$sql.= ", qty";
$sql.= ") VALUES (";
$sql.= $this->id;
$sql.= ", ".($entrepot_id?$entrepot_id:'null');
$sql.= ", ".$origin_line_id;
$sql.= ", ".$qty;
$sql.= ")";
dol_syslog(get_class($this)."::create_line", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$line_id = $this->db->last_insert_id(MAIN_DB_PREFIX."expeditiondet");
}
else
{
$error++;
}
if (! $error && empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options)>0) // For avoid conflicts if trigger used
{ {
$expeditionline = new ExpeditionLigne($this->db); $expeditionline = new ExpeditionLigne($this->db);
$expeditionline->array_options=$array_options; $expeditionline->fk_expedition = $this->id;
$expeditionline->id= $this->db->last_insert_id(MAIN_DB_PREFIX.$expeditionline->table_element); $expeditionline->entrepot_id = $entrepot_id;
$result=$expeditionline->insertExtraFields(); $expeditionline->fk_origin_line = $origin_line_id;
if ($result < 0) $expeditionline->qty = $qty;
$expeditionline->$array_options = $array_options;
if (($lineId = $expeditionline->insert()) < 0)
{ {
$this->error[]=$expeditionline->error; $this->error[]=$expeditionline->error;
$error++;
} }
} return $lineId;
if (! $error) return $line_id;
else return -1;
} }
@ -2334,9 +2304,9 @@ class ExpeditionLigne extends CommonObjectLine
$sql.= ", fk_origin_line"; $sql.= ", fk_origin_line";
$sql.= ", qty"; $sql.= ", qty";
$sql.= ") VALUES ("; $sql.= ") VALUES (";
$sql.= $this->id; $sql.= $this->fk_expedition;
$sql.= ", ".$this->entrepot_id; $sql.= ", ".$this->entrepot_id;
$sql.= ", ".$this->origin_line_id; $sql.= ", ".$this->fk_origin_line;
$sql.= ", ".$this->qty; $sql.= ", ".$this->qty;
$sql.= ")"; $sql.= ")";
@ -2347,7 +2317,6 @@ class ExpeditionLigne extends CommonObjectLine
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."expeditiondet"); $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."expeditiondet");
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{ {
$this->id=$this->rowid;
$result=$this->insertExtraFields(); $result=$this->insertExtraFields();
if ($result < 0) if ($result < 0)
{ {
@ -2358,12 +2327,16 @@ class ExpeditionLigne extends CommonObjectLine
if (! $error && ! $notrigger) if (! $error && ! $notrigger)
{ {
// Call trigger // Call trigger
$result=$this->call_trigger('LINESHIPMENT_INSERT',$user); $result=$this->call_trigger('LINESHIPPING_INSERT',$user);
if ($result < 0) $error++; if ($result < 0)
{
$this->errors[]=$this->error;
$error++;
}
// End call triggers // End call triggers
} }
if (!$error) { if (! $error) {
$this->db->commit(); $this->db->commit();
return $this->id; return $this->id;
} }
@ -2380,50 +2353,21 @@ class ExpeditionLigne extends CommonObjectLine
{ {
$error++; $error++;
} }
if (! $error && empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options)>0) // For avoid conflicts if trigger used
{
$expeditionline = new ExpeditionLigne($this->db);
$expeditionline->array_options=$array_options;
$expeditionline->id= $this->db->last_insert_id(MAIN_DB_PREFIX.$expeditionline->table_element);
$result=$expeditionline->insertExtraFields();
if ($result < 0)
{
$this->error[]=$expeditionline->error;
$error++;
}
}
if (! $error) return $line_id;
else return -1;
// ////////////////////
dol_syslog(get_class($this)."::insert", LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
$this->rowid=$this->db->last_insert_id(MAIN_DB_PREFIX.'commandedet');
}
else
{
$this->error=$this->db->error();
$this->db->rollback();
return -2;
}
} }
/** /**
* Delete shipment line. * 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 * @return int >0 if OK, <0 if KO
*/ */
function delete() function delete($user = null, $notrigger = 0)
{ {
global $conf; global $conf;
$error=0;
$this->db->begin(); $this->db->begin();
// delete batch expedition line // delete batch expedition line
@ -2435,15 +2379,14 @@ class ExpeditionLigne extends CommonObjectLine
if (!$this->db->query($sql)) if (!$this->db->query($sql))
{ {
$this->errors[]=$this->db->lasterror()." - sql=$sql"; $this->errors[]=$this->db->lasterror()." - sql=$sql";
$this->db->rollback(); $error++;
return -2;
} }
} }
$sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet"; $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet";
$sql.= " WHERE rowid = ".$this->id; $sql.= " WHERE rowid = ".$this->id;
if ( $this->db->query($sql)) if (! $error && $this->db->query($sql))
{ {
// Remove extrafields // Remove extrafields
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
@ -2452,26 +2395,40 @@ class ExpeditionLigne extends CommonObjectLine
if ($result < 0) if ($result < 0)
{ {
$this->errors[]=$this->error; $this->errors[]=$this->error;
$this->db->rollback(); $error++;
return -4;
} }
else }
if (! $error && ! $notrigger)
{ {
$this->db->commit(); // Call trigger
return 1; $result=$this->call_trigger('LINESHIPPING_DELETE',$user);
} if ($result < 0)
}
else
{ {
$this->db->commit(); $this->errors[]=$this->error;
return 1; $error++;
}
// End call triggers
} }
} }
else else
{ {
$this->errors[]=$this->db->lasterror()." - sql=$sql"; $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(); $this->db->rollback();
return -3; return -1*$error;
} }
} }
@ -2480,17 +2437,20 @@ class ExpeditionLigne extends CommonObjectLine
* *
* @return int < 0 if KO, > 0 if OK * @return int < 0 if KO, > 0 if OK
*/ */
function update() function update($user = null, $notrigger = 0)
{ {
global $conf; 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"); dol_syslog(get_class($this)."::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty");
// Add a protection to refuse deleting if shipment is not in draft status // check parameters
if (! isset($this->id) || ! isset($this->entrepot_id)) if (! isset($this->id) || ! isset($this->entrepot_id))
{ {
dol_syslog(get_class($this).'::update missing line id and/or warehouse id', LOG_ERR); dol_syslog(get_class($this).'::update missing line id and/or warehouse id', LOG_ERR);
$this->errors[]='ErrorBadParameters'; $this->errors[]='ErrorMandatoryParametersNotProvided';
$error++;
return -1; return -1;
} }
@ -2508,7 +2468,7 @@ class ExpeditionLigne extends CommonObjectLine
{ {
dol_syslog(get_class($this).'::update only possible for one batch', LOG_ERR); dol_syslog(get_class($this).'::update only possible for one batch', LOG_ERR);
$this->errors[]='ErrorBadParameters'; $this->errors[]='ErrorBadParameters';
return -7; $error++;
} }
else else
{ {
@ -2524,12 +2484,12 @@ class ExpeditionLigne extends CommonObjectLine
// update lot // update lot
if (!empty($batch) && $conf->productbatch->enabled) if (! empty($batch) && $conf->productbatch->enabled)
{ {
if (empty($batch_id) || empty($this->fk_product)) { 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); dol_syslog(get_class($this).'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR);
$this->errors[]='ErrorBadParameters'; $this->errors[]='ErrorMandatoryParametersNotProvided';
return -8; $error++;
} }
// fetch remaining lot qty // fetch remaining lot qty
@ -2537,9 +2497,10 @@ class ExpeditionLigne extends CommonObjectLine
if (($lotArray = ExpeditionLineBatch::fetchAll($this->db, $this->id)) < 0) if (($lotArray = ExpeditionLineBatch::fetchAll($this->db, $this->id)) < 0)
{ {
$this->errors[]=$this->db->lasterror()." - ExpeditionLineBatch::fetchAll"; $this->errors[]=$this->db->lasterror()." - ExpeditionLineBatch::fetchAll";
$this->db->rollback(); $error++;
return -4;
} }
else
{
foreach ($lotArray as $lot) foreach ($lotArray as $lot)
{ {
if ($batch != $lot->batch) if ($batch != $lot->batch)
@ -2556,9 +2517,10 @@ class ExpeditionLigne extends CommonObjectLine
if ($lot->fetch(0,$this->fk_product,$batch) < 0) if ($lot->fetch(0,$this->fk_product,$batch) < 0)
{ {
$this->errors[] = $lot->errors; $this->errors[] = $lot->errors;
return -3; $error++;
} }
else
{
// delete lot expedition line // delete lot expedition line
$sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet_batch"; $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet_batch";
$sql.= " WHERE fk_expeditiondet = ".$this->id; $sql.= " WHERE fk_expeditiondet = ".$this->id;
@ -2567,11 +2529,10 @@ class ExpeditionLigne extends CommonObjectLine
if (!$this->db->query($sql)) if (!$this->db->query($sql))
{ {
$this->errors[]=$this->db->lasterror()." - sql=$sql"; $this->errors[]=$this->db->lasterror()." - sql=$sql";
$this->db->rollback(); $error++;
return -2;
} }
else if ($qty > 0)
if ($qty > 0) { {
if (isset($lot->id)) if (isset($lot->id))
{ {
$shipmentLot = new ExpeditionLineBatch($this->db); $shipmentLot = new ExpeditionLineBatch($this->db);
@ -2584,13 +2545,15 @@ class ExpeditionLigne extends CommonObjectLine
if ($shipmentLot->create($this->id) < 0) if ($shipmentLot->create($this->id) < 0)
{ {
$this->errors[]=$shipmentLot->errors; $this->errors[]=$shipmentLot->errors;
$this->db->rollback(); $error++;
return -6;
} }
} }
} }
} }
}
}
if (! $error)
{
// update line // update line
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
$sql.= " fk_entrepot = ".$this->entrepot_id; $sql.= " fk_entrepot = ".$this->entrepot_id;
@ -2600,30 +2563,45 @@ class ExpeditionLigne extends CommonObjectLine
if (!$this->db->query($sql)) if (!$this->db->query($sql))
{ {
$this->errors[]=$this->db->lasterror()." - sql=$sql"; $this->errors[]=$this->db->lasterror()." - sql=$sql";
$this->db->rollback(); $error++;
return -5;
} }
else
{
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{ {
$this->id=$this->rowid;
$result=$this->insertExtraFields(); $result=$this->insertExtraFields();
if ($result < 0) if ($result < 0)
{ {
$this->errors[]=$this->error; $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(); $this->db->rollback();
return -4; return -1*$error;
}
else
{
$this->db->commit();
return 1;
}
}
else
{
$this->db->commit();
return 1;
} }
} }
} }