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

View File

@ -57,97 +57,99 @@ class Commande extends CommonOrder
* Client ID
* @var int
*/
var $socid;
public $socid;
var $ref_client;
var $ref_int;
var $contactid;
public $ref_client;
public $ref_int;
public $contactid;
/**
* Status of the order. Check the following constants:
* @var int
* @see Commande::STATUS_CANCELED, Commande::STATUS_DRAFT, Commande::STATUS_ACCEPTED, Commande::STATUS_CLOSED
*/
var $statut;
public $statut;
/**
* @deprecated
* @see billed
*/
var $facturee;
var $billed; // billed or not
public $facturee;
public $billed; // billed or not
var $brouillon;
var $cond_reglement_code;
public $brouillon;
public $cond_reglement_code;
var $fk_account;
public $fk_account;
/**
* It holds the label of the payment mode. Use it in case translation cannot be found.
* @var string
*/
var $mode_reglement;
public $mode_reglement;
/**
* Payment mode id
* @var int
*/
var $mode_reglement_id;
public $mode_reglement_id;
/**
* Payment mode code
* @var string
*/
var $mode_reglement_code;
public $mode_reglement_code;
/**
* Availability delivery time id
* @var int
*/
var $availability_id;
public $availability_id;
/**
* Availability delivery time code
* @var string
*/
var $availability_code;
public $availability_code;
/**
* Label of availability delivery time. Use it in case translation cannot be found.
* @var string
*/
var $availability;
public $availability;
var $demand_reason_id;
var $demand_reason_code;
var $address;
var $date; // Date commande
public $demand_reason_id;
public $demand_reason_code;
public $address;
public $date; // Date commande
/**
* @deprecated
* @see date
*/
var $date_commande;
var $date_livraison; // Date livraison souhaitee
var $fk_remise_except;
var $remise_percent;
var $remise_absolue;
var $info_bits;
var $rang;
var $special_code;
var $source; // Origin of order
var $extraparams=array();
public $date_commande;
public $date_livraison; // Date livraison souhaitee
public $fk_remise_except;
public $remise_percent;
public $remise_absolue;
public $info_bits;
public $rang;
public $special_code;
public $source; // Origin of order
public $extraparams=array();
var $linked_objects=array();
public $linked_objects=array();
var $user_author_id;
public $user_author_id;
/**
* @var OrderLine[]
*/
var $lines = array();
public $lines = array();
// Multicurrency
var $fk_multicurrency;
var $multicurrency_code;
var $multicurrency_tx;
var $multicurrency_total_ht;
var $multicurrency_total_tva;
var $multicurrency_total_ttc;
public $fk_multicurrency;
public $multicurrency_code;
public $multicurrency_tx;
public $multicurrency_total_ht;
public $multicurrency_total_tva;
public $multicurrency_total_ttc;
public $oldcopy;
/**
* ERR Not enough stock
@ -469,7 +471,7 @@ class Commande extends CommonOrder
if (!$error) {
// Call trigger
$result=$this->call_trigger('ORDER_SETDRAFT',$user);
$result=$this->call_trigger('ORDER_UNVALIDATE',$user);
if ($result < 0) $error++;
}
@ -2044,30 +2046,62 @@ class Commande extends CommonOrder
*
* @param User $user User qui positionne la remise
* @param float $remise Discount (percent)
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 if KO, >0 if OK
*/
function set_remise($user, $remise)
function set_remise($user, $remise, $notrigger=0)
{
$remise=trim($remise)?trim($remise):0;
if ($user->rights->commande->creer)
{
$error=0;
$this->db->begin();
$remise=price2num($remise);
$sql = 'UPDATE '.MAIN_DB_PREFIX.'commande';
$sql.= ' SET remise_percent = '.$remise;
$sql.= ' WHERE rowid = '.$this->id.' AND fk_statut = '.self::STATUS_DRAFT.' ;';
if ($this->db->query($sql))
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if (!$resql)
{
$this->remise_percent = $remise;
$this->update_price(1);
return 1;
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->remise_percent = $remise;
$this->update_price(1);
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('ORDER_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error();
return -1;
foreach($this->errors as $errmsg)
{
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
}
}
@ -2078,32 +2112,62 @@ class Commande extends CommonOrder
*
* @param User $user User qui positionne la remise
* @param float $remise Discount
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 if KO, >0 if OK
*/
function set_remise_absolue($user, $remise)
function set_remise_absolue($user, $remise, $notrigger=0)
{
$remise=trim($remise)?trim($remise):0;
if ($user->rights->commande->creer)
{
$error=0;
$this->db->begin();
$remise=price2num($remise);
$sql = 'UPDATE '.MAIN_DB_PREFIX.'commande';
$sql.= ' SET remise_absolue = '.$remise;
$sql.= ' WHERE rowid = '.$this->id.' AND fk_statut = '.self::STATUS_DRAFT.' ;';
dol_syslog(get_class($this)."::set_remise_absolue", LOG_DEBUG);
if ($this->db->query($sql))
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if (!$resql)
{
$this->remise_absolue = $remise;
$this->update_price(1);
return 1;
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->remise_absolue = $remise;
$this->update_price(1);
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('ORDER_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error();
return -1;
foreach($this->errors as $errmsg)
{
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
}
}
@ -2112,29 +2176,59 @@ class Commande extends CommonOrder
/**
* Set the order date
*
* @param User $user Object user making change
* @param int $date Date
* @return int <0 if KO, >0 if OK
* @param User $user Object user making change
* @param int $date Date
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 if KO, >0 if OK
*/
function set_date($user, $date)
function set_date($user, $date, $notrigger=0)
{
if ($user->rights->commande->creer)
{
$error=0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."commande";
$sql.= " SET date_commande = ".($date ? $this->db->idate($date) : 'null');
$sql.= " WHERE rowid = ".$this->id." AND fk_statut = ".self::STATUS_DRAFT;
dol_syslog(get_class($this)."::set_date",LOG_DEBUG);
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
if (!$resql)
{
$this->date = $date;
return 1;
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->date = $date;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('ORDER_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error();
return -1;
foreach($this->errors as $errmsg)
{
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
}
else
@ -2146,29 +2240,59 @@ class Commande extends CommonOrder
/**
* Set the planned delivery date
*
* @param User $user Objet utilisateur qui modifie
* @param User $user Objet utilisateur qui modifie
* @param int $date_livraison Date de livraison
* @return int <0 si ko, >0 si ok
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 si ko, >0 si ok
*/
function set_date_livraison($user, $date_livraison)
function set_date_livraison($user, $date_livraison, $notrigger=0)
{
if ($user->rights->commande->creer)
{
$error=0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."commande";
$sql.= " SET date_livraison = ".($date_livraison ? "'".$this->db->idate($date_livraison)."'" : 'null');
$sql.= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::set_date_livraison", LOG_DEBUG);
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
if (!$resql)
{
$this->date_livraison = $date_livraison;
return 1;
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->date_livraison = $date_livraison;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('ORDER_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error();
return -1;
foreach($this->errors as $errmsg)
{
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
}
else
@ -2182,26 +2306,57 @@ class Commande extends CommonOrder
*
* @param User $user Object user making change
* @param int $id If of availability delay
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 if KO, >0 if OK
*/
function set_availability($user, $id)
function set_availability($user, $id, $notrigger=0)
{
if ($user->rights->commande->creer)
{
$error=0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."commande ";
$sql.= " SET fk_availability = '".$id."'";
$sql.= " WHERE rowid = ".$this->id;
if ($this->db->query($sql))
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if (!$resql)
{
$this->fk_availability = $id;
return 1;
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->fk_availability = $id;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('ORDER_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error();
dol_syslog(get_class($this)."::set_availability Erreur SQL");
return -1;
foreach($this->errors as $errmsg)
{
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
}
}
@ -2209,28 +2364,60 @@ class Commande extends CommonOrder
/**
* Set source of demand
*
* @param User $user Object user making change
* @param int $id Id of source
* @return int <0 if KO, >0 if OK
* @param User $user Object user making change
* @param int $id Id of source
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 if KO, >0 if OK
*/
function set_demand_reason($user, $id)
function set_demand_reason($user, $id, $notrigger=0)
{
if ($user->rights->commande->creer)
{
$error=0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."commande ";
$sql.= " SET fk_input_reason = '".$id."'";
$sql.= " WHERE rowid = ".$this->id;
if ($this->db->query($sql))
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if (!$resql)
{
$this->fk_input_reason = $id;
return 1;
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->fk_input_reason = $id;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('ORDER_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error();
dol_syslog(get_class($this)."::set_demand_reason Erreur SQL");
return -1;
foreach($this->errors as $errmsg)
{
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
}
}
@ -2309,69 +2496,137 @@ class Commande extends CommonOrder
}
/**
* Change le delai de livraison
* Update delivery delay
*
* @param int $availability_id Id du nouveau mode
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int >0 if OK, <0 if KO
*/
function availability($availability_id)
function availability($availability_id, $notrigger=0)
{
dol_syslog('Commande::availability('.$availability_id.')');
if ($this->statut >= self::STATUS_DRAFT)
{
$error=0;
$this->db->begin();
$sql = 'UPDATE '.MAIN_DB_PREFIX.'commande';
$sql .= ' SET fk_availability = '.$availability_id;
$sql .= ' WHERE rowid='.$this->id;
if ( $this->db->query($sql) )
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if (!$resql)
{
$this->availability_id = $availability_id;
return 1;
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->availability_id = $availability_id;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('ORDER_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
dol_syslog('Commande::availability Erreur '.$sql.' - '.$this->db->error(), LOG_ERR);
$this->error=$this->db->lasterror();
return -1;
foreach($this->errors as $errmsg)
{
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
}
else
{
dol_syslog('Commande::availability, etat facture incompatible', LOG_ERR);
$this->error='Etat commande incompatible '.$this->statut;
$error_str='Command status do not meet requirement '.$this->statut;
dol_syslog(__METHOD__.$error_str, LOG_ERR);
$this->error=$error_str;
$this->errors[]= $this->error;
return -2;
}
}
/**
* Change la source de la demande
* Update order demand_reason
*
* @param int $demand_reason_id Id of new demand
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int >0 if ok, <0 if ko
*/
function demand_reason($demand_reason_id)
function demand_reason($demand_reason_id, $notrigger=0)
{
dol_syslog('Commande::demand_reason('.$demand_reason_id.')');
if ($this->statut >= self::STATUS_DRAFT)
{
$error=0;
$this->db->begin();
$sql = 'UPDATE '.MAIN_DB_PREFIX.'commande';
$sql .= ' SET fk_input_reason = '.$demand_reason_id;
$sql .= ' WHERE rowid='.$this->id;
if ( $this->db->query($sql) )
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if (!$resql)
{
$this->demand_reason_id = $demand_reason_id;
return 1;
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->demand_reason_id = $demand_reason_id;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('ORDER_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
dol_syslog('Commande::demand_reason Erreur '.$sql.' - '.$this->db->error(), LOG_ERR);
$this->error=$this->db->lasterror();
return -1;
foreach($this->errors as $errmsg)
{
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
}
else
{
dol_syslog('Commande::demand_reason, etat facture incompatible', LOG_ERR);
$this->error='Etat commande incompatible '.$this->statut;
$error_str='order status do not meet requirement '.$this->statut;
dol_syslog(__METHOD__.$error_str, LOG_ERR);
$this->error=$error_str;
$this->errors[]= $this->error;
return -2;
}
}
@ -2381,27 +2636,57 @@ class Commande extends CommonOrder
*
* @param User $user User that make change
* @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
*/
function set_ref_client($user, $ref_client)
function set_ref_client($user, $ref_client, $notrigger=0)
{
if ($user->rights->commande->creer)
{
dol_syslog(get_class($this).'::set_ref_client this->id='.$this->id.', ref_client='.$ref_client);
$error=0;
$this->db->begin();
$sql = 'UPDATE '.MAIN_DB_PREFIX.'commande SET';
$sql.= ' ref_client = '.(empty($ref_client) ? 'NULL' : '\''.$this->db->escape($ref_client).'\'');
$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->ref_client = $ref_client;
return 1;
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->ref_client = $ref_client;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('ORDER_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->lasterror();
return -2;
foreach($this->errors as $errmsg)
{
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
}
else
@ -2413,10 +2698,11 @@ class Commande extends CommonOrder
/**
* Classify the order as invoiced
*
* @param User $user Object user making the change
* @return int <0 if KO, >0 if OK
* @param User $user Object user making the change
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 if KO, >0 if OK
*/
function classifyBilled(User $user)
function classifyBilled(User $user, $notrigger=0)
{
global $user;
$error = 0;
@ -2429,16 +2715,24 @@ class Commande extends CommonOrder
dol_syslog(get_class($this)."::classifyBilled", LOG_DEBUG);
if ($this->db->query($sql))
{
// Call trigger
$result=$this->call_trigger('ORDER_CLASSIFY_BILLED',$user);
if ($result < 0) $error++;
// End call triggers
if (! $error)
{
$this->oldcopy= clone $this;
$this->facturee=1; // deprecated
$this->billed=1;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('ORDER_CLASSIFY_BILLED',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
@ -2494,6 +2788,13 @@ class Commande extends CommonOrder
dol_syslog(get_class($this)."::classifyUnBilled", LOG_DEBUG);
if ($this->db->query($sql))
{
if (! $error)
{
$this->oldcopy= clone $this;
$this->facturee=1; // deprecated
$this->billed=1;
}
// Call trigger
$result=$this->call_trigger('ORDER_CLASSIFY_UNBILLED',$user);
if ($result < 0) $error++;

View File

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