Merge pull request #5467 from atm-florian/add_update_trigger

NEW : review Propal/Order/Invoice class to be uniformize on triggers calls
This commit is contained in:
Laurent Destailleur 2016-07-14 23:00:11 +02:00 committed by GitHub
commit dd7c7b9d4b
3 changed files with 1223 additions and 403 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -59,63 +59,63 @@ class Facture extends CommonInvoice
*/ */
protected $table_ref_field = 'facnumber'; protected $table_ref_field = 'facnumber';
var $socid; public $socid;
var $author; public $author;
var $fk_user_author; public $fk_user_author;
var $fk_user_valid; public $fk_user_valid;
var $date; // Date invoice public $date; // Date invoice
var $date_creation; // Creation date public $date_creation; // Creation date
var $date_validation; // Validation date public $date_validation; // Validation date
var $datem; public $datem;
var $ref_client; public $ref_client;
var $ref_int; public $ref_int;
//Check constants for types //Check constants for types
var $type = self::TYPE_STANDARD; public $type = self::TYPE_STANDARD;
//var $amount; //var $amount;
var $remise_absolue; public $remise_absolue;
var $remise_percent; public $remise_percent;
var $total_ht=0; public $total_ht=0;
var $total_tva=0; public $total_tva=0;
var $total_ttc=0; public $total_ttc=0;
var $revenuestamp; public $revenuestamp;
//! Fermeture apres paiement partiel: discount_vat, badcustomer, abandon //! Fermeture apres paiement partiel: discount_vat, badcustomer, abandon
//! Fermeture alors que aucun paiement: replaced (si remplace), abandon //! Fermeture alors que aucun paiement: replaced (si remplace), abandon
var $close_code; public $close_code;
//! Commentaire si mis a paye sans paiement complet //! Commentaire si mis a paye sans paiement complet
var $close_note; public $close_note;
//! 1 if invoice paid COMPLETELY, 0 otherwise (do not use it anymore, use statut and close_code) //! 1 if invoice paid COMPLETELY, 0 otherwise (do not use it anymore, use statut and close_code)
var $paye; public $paye;
//! id of source invoice if replacement invoice or credit note //! id of source invoice if replacement invoice or credit note
var $fk_facture_source; public $fk_facture_source;
var $linked_objects=array(); public $linked_objects=array();
var $date_lim_reglement; public $date_lim_reglement;
var $cond_reglement_code; // Code in llx_c_paiement public $cond_reglement_code; // Code in llx_c_paiement
var $mode_reglement_code; // Code in llx_c_paiement public $mode_reglement_code; // Code in llx_c_paiement
var $fk_bank; // Field to store bank id to use when payment mode is withdraw public $fk_bank; // Field to store bank id to use when payment mode is withdraw
/** /**
* @deprecated * @deprecated
*/ */
var $products=array(); public $products=array();
/** /**
* @var FactureLigne[] * @var FactureLigne[]
*/ */
var $lines=array(); public $lines=array();
var $line; public $line;
var $extraparams=array(); public $extraparams=array();
var $specimen; public $specimen;
var $fac_rec; public $fac_rec;
// Multicurrency // Multicurrency
var $fk_multicurrency; public $fk_multicurrency;
var $multicurrency_code; public $multicurrency_code;
var $multicurrency_tx; public $multicurrency_tx;
var $multicurrency_total_ht; public $multicurrency_total_ht;
var $multicurrency_total_tva; public $multicurrency_total_tva;
var $multicurrency_total_ttc; public $multicurrency_total_ttc;
/** /**
* @var int Situation cycle reference number * @var int Situation cycle reference number
@ -142,6 +142,8 @@ class Facture extends CommonInvoice
*/ */
public $tab_next_situation_invoice=array(); public $tab_next_situation_invoice=array();
public $oldcopy;
/** /**
* Standard invoice * Standard invoice
*/ */
@ -1486,25 +1488,60 @@ class Facture extends CommonInvoice
* Set customer ref * Set customer ref
* *
* @param string $ref_client Customer ref * @param string $ref_client Customer ref
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function set_ref_client($ref_client) function set_ref_client($ref_client, $notrigger=0)
{ {
$error=0;
$this->db->begin();
$sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture';
if (empty($ref_client)) if (empty($ref_client))
$sql .= ' SET ref_client = NULL'; $sql .= ' SET ref_client = NULL';
else else
$sql .= ' SET ref_client = \''.$this->db->escape($ref_client).'\''; $sql .= ' SET ref_client = \''.$this->db->escape($ref_client).'\'';
$sql .= ' WHERE rowid = '.$this->id; $sql .= ' WHERE rowid = '.$this->id;
if ($this->db->query($sql))
dol_syslog(__METHOD__.' this->id='.$this->id.', ref_client='.$ref_client, LOG_DEBUG);
$resql=$this->db->query($sql);
if (!$resql)
{
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{ {
$this->ref_client = $ref_client; $this->ref_client = $ref_client;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('BILL_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->ref_client = $ref_client;
$this->db->commit();
return 1; return 1;
} }
else else
{ {
dol_print_error($this->db); foreach($this->errors as $errmsg)
return -1; {
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
} }
} }
@ -2070,7 +2107,7 @@ class Facture extends CommonInvoice
$i++; $i++;
} }
if ($final) { if ($final) {
$this->setFinal(); $this->setFinal($user);
} }
} }
} }
@ -2760,9 +2797,10 @@ class Facture extends CommonInvoice
* *
* @param User $user User that set discount * @param User $user User that set discount
* @param double $remise Discount * @param double $remise Discount
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 if ko, >0 if ok * @return int <0 if ko, >0 if ok
*/ */
function set_remise($user, $remise) function set_remise($user, $remise, $notrigger=0)
{ {
// Clean parameters // Clean parameters
if (empty($remise)) $remise=0; if (empty($remise)) $remise=0;
@ -2771,21 +2809,48 @@ class Facture extends CommonInvoice
{ {
$remise=price2num($remise); $remise=price2num($remise);
$error=0;
$this->db->begin();
$sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture';
$sql.= ' SET remise_percent = '.$remise; $sql.= ' SET remise_percent = '.$remise;
$sql.= ' WHERE rowid = '.$this->id; $sql.= ' WHERE rowid = '.$this->id;
$sql.= ' AND fk_statut = '.self::STATUS_DRAFT; $sql.= ' AND fk_statut = '.self::STATUS_DRAFT;
if ($this->db->query($sql)) dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if (!$resql)
{
$this->errors[]=$this->db->error();
$error++;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('BILL_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{ {
$this->remise_percent = $remise; $this->remise_percent = $remise;
$this->update_price(1); $this->update_price(1);
$this->db->commit();
return 1; return 1;
} }
else else
{ {
$this->error=$this->db->error(); foreach($this->errors as $errmsg)
return -1; {
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
} }
} }
} }
@ -2796,14 +2861,19 @@ class Facture extends CommonInvoice
* *
* @param User $user User that set discount * @param User $user User that set discount
* @param double $remise Discount * @param double $remise Discount
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function set_remise_absolue($user, $remise) function set_remise_absolue($user, $remise, $notrigger=0)
{ {
if (empty($remise)) $remise=0; if (empty($remise)) $remise=0;
if ($user->rights->facture->creer) if ($user->rights->facture->creer)
{ {
$error=0;
$this->db->begin();
$remise=price2num($remise); $remise=price2num($remise);
$sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture';
@ -2811,18 +2881,43 @@ class Facture extends CommonInvoice
$sql.= ' WHERE rowid = '.$this->id; $sql.= ' WHERE rowid = '.$this->id;
$sql.= ' AND fk_statut = '.self::STATUS_DRAFT; $sql.= ' AND fk_statut = '.self::STATUS_DRAFT;
dol_syslog(get_class($this)."::set_remise_absolue", LOG_DEBUG); dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if ($this->db->query($sql)) if (!$resql)
{ {
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->remise_absolue = $remise; $this->remise_absolue = $remise;
$this->update_price(1); $this->update_price(1);
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('BILL_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1; return 1;
} }
else else
{ {
$this->error=$this->db->error(); foreach($this->errors as $errmsg)
return -1; {
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
} }
} }
} }
@ -3819,25 +3914,49 @@ class Facture extends CommonInvoice
/** /**
* Sets the invoice as a final situation * Sets the invoice as a final situation
* *
* @return int 1 if ok, -1 if error * @param User $user Object user
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 if KO, >0 if OK
*/ */
function setFinal() function setFinal(User $user, $notrigger=0)
{ {
$error=0;
$this->db->begin(); $this->db->begin();
$this->situation_final = 1; $this->situation_final = 1;
$sql = 'update ' . MAIN_DB_PREFIX . 'facture set situation_final = ' . $this->situation_final . ' where rowid = ' . $this->id; $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture SET situation_final = ' . $this->situation_final . ' where rowid = ' . $this->id;
$resql = $this->db->query($sql);
if ($resql) { dol_syslog(__METHOD__, LOG_DEBUG);
// FIXME: call triggers MODIFY because we modify invoice $resql=$this->db->query($sql);
if (!$resql)
{
$this->errors[]=$this->db->error();
$error++;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('BILL_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit(); $this->db->commit();
return 1; return 1;
} else { }
$this->error = $this->db->lasterror(); else
dol_syslog(get_class($this) . "::update Error setFinal " . $sql, LOG_ERR); {
foreach($this->errors as $errmsg)
{
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback(); $this->db->rollback();
return -1; return -1*$error;
} }
} }