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

View File

@ -59,24 +59,24 @@ class Propal extends CommonObject
* ID of the client * ID of the client
* @var int * @var int
*/ */
var $socid; public $socid;
var $contactid; public $contactid;
var $author; public $author;
var $ref_client; public $ref_client;
/** /**
* Status of the quote * Status of the quote
* @var int * @var int
* @see Propal::STATUS_DRAFT, Propal::STATUS_VALIDATED, Propal::STATUS_SIGNED, Propal::STATUS_NOTSIGNED, Propal::STATUS_BILLED * @see Propal::STATUS_DRAFT, Propal::STATUS_VALIDATED, Propal::STATUS_SIGNED, Propal::STATUS_NOTSIGNED, Propal::STATUS_BILLED
*/ */
var $statut; public $statut;
/** /**
* @deprecated * @deprecated
* @see date_creation * @see date_creation
*/ */
var $datec; public $datec;
/** /**
* Creation date * Creation date
@ -88,7 +88,7 @@ class Propal extends CommonObject
* @deprecated * @deprecated
* @see date_validation * @see date_validation
*/ */
var $datev; public $datev;
/** /**
* Validation date * Validation date
@ -100,70 +100,72 @@ class Propal extends CommonObject
* Date of the quote * Date of the quote
* @var * @var
*/ */
var $date; public $date;
/** /**
* @deprecated * @deprecated
* @see date * @see date
*/ */
var $datep; public $datep;
var $date_livraison; public $date_livraison;
var $fin_validite; public $fin_validite;
var $user_author_id; public $user_author_id;
var $user_valid_id; public $user_valid_id;
var $user_close_id; public $user_close_id;
/** /**
* @deprecated * @deprecated
* @see total_ht * @see total_ht
*/ */
var $price; public $price;
/** /**
* @deprecated * @deprecated
* @see total_tva * @see total_tva
*/ */
var $tva; public $tva;
/** /**
* @deprecated * @deprecated
* @see total_ttc * @see total_ttc
*/ */
var $total; public $total;
var $cond_reglement_code; public $cond_reglement_code;
var $mode_reglement_code; public $mode_reglement_code;
var $remise; public $remise;
var $remise_percent; public $remise_percent;
var $remise_absolue; public $remise_absolue;
var $fk_address; public $fk_address;
var $address_type; public $address_type;
var $address; public $address;
var $availability_id; public $availability_id;
var $availability_code; public $availability_code;
var $demand_reason_id; public $demand_reason_id;
var $demand_reason_code; public $demand_reason_code;
var $products=array(); public $products=array();
var $extraparams=array(); public $extraparams=array();
/** /**
* @var PropaleLigne[] * @var PropaleLigne[]
*/ */
var $lines = array(); public $lines = array();
var $line; public $line;
var $labelstatut=array(); public $labelstatut=array();
var $labelstatut_short=array(); public $labelstatut_short=array();
var $specimen; public $specimen;
// 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;
public $oldcopy;
/** /**
* Draft status * Draft status
@ -1612,9 +1614,10 @@ class Propal extends CommonObject
* *
* @param User $user Object user that modify * @param User $user Object user that modify
* @param int $date Date * @param int $date Date
* @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_date($user, $date) function set_date($user, $date, $notrigger=0)
{ {
if (empty($date)) if (empty($date))
{ {
@ -1625,20 +1628,50 @@ class Propal extends CommonObject
if (! empty($user->rights->propal->creer)) if (! empty($user->rights->propal->creer))
{ {
$error=0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET datep = '".$this->db->idate($date)."'"; $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET datep = '".$this->db->idate($date)."'";
$sql.= " WHERE rowid = ".$this->id." AND fk_statut = ".self::STATUS_DRAFT; $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);
if ($this->db->query($sql) ) $resql=$this->db->query($sql);
if (!$resql)
{ {
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->date = $date; $this->date = $date;
$this->datep = $date; // deprecated $this->datep = $date; // deprecated
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('PROPAL_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1; return 1;
} }
else else
{ {
$this->error=$this->db->lasterror(); 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;
} }
} }
} }
@ -1648,23 +1681,57 @@ class Propal extends CommonObject
* *
* @param User $user Object user that modify * @param User $user Object user that modify
* @param int $date_fin_validite End of validity date * @param int $date_fin_validite End of validity date
* @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_echeance($user, $date_fin_validite) function set_echeance($user, $date_fin_validite, $notrigger=0)
{ {
if (! empty($user->rights->propal->creer)) if (! empty($user->rights->propal->creer))
{ {
$error=0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fin_validite = ".($date_fin_validite!=''?"'".$this->db->idate($date_fin_validite)."'":'null'); $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fin_validite = ".($date_fin_validite!=''?"'".$this->db->idate($date_fin_validite)."'":'null');
$sql.= " WHERE rowid = ".$this->id." AND fk_statut = ".self::STATUS_DRAFT; $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->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->fin_validite = $date_fin_validite; $this->fin_validite = $date_fin_validite;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('PROPAL_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;
} }
} }
} }
@ -1674,26 +1741,57 @@ class Propal extends CommonObject
* *
* @param User $user Object user that modify * @param User $user Object user that modify
* @param int $date_livraison Delivery date * @param int $date_livraison Delivery date
* @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_date_livraison($user, $date_livraison) function set_date_livraison($user, $date_livraison, $notrigger=0)
{ {
if (! empty($user->rights->propal->creer)) if (! empty($user->rights->propal->creer))
{ {
$error=0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."propal "; $sql = "UPDATE ".MAIN_DB_PREFIX."propal ";
$sql.= " SET date_livraison = ".($date_livraison!=''?"'".$this->db->idate($date_livraison)."'":'null'); $sql.= " SET date_livraison = ".($date_livraison!=''?"'".$this->db->idate($date_livraison)."'":'null');
$sql.= " WHERE rowid = ".$this->id; $sql.= " WHERE rowid = ".$this->id;
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->date_livraison = $date_livraison; $this->date_livraison = $date_livraison;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('PROPAL_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)
dol_syslog(get_class($this)."::set_date_livraison Erreur SQL"); {
return -1; dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
} }
} }
} }
@ -1703,26 +1801,57 @@ class Propal extends CommonObject
* *
* @param User $user Object user that modify * @param User $user Object user that modify
* @param int $id Availability id * @param int $id Availability id
* @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_availability($user, $id) function set_availability($user, $id, $notrigger=0)
{ {
if (! empty($user->rights->propal->creer)) if (! empty($user->rights->propal->creer))
{ {
$error=0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."propal "; $sql = "UPDATE ".MAIN_DB_PREFIX."propal ";
$sql.= " SET fk_availability = '".$id."'"; $sql.= " SET fk_availability = '".$id."'";
$sql.= " WHERE rowid = ".$this->id; $sql.= " WHERE rowid = ".$this->id;
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->fk_availability = $id; $this->fk_availability = $id;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('PROPAL_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)
dol_syslog(get_class($this)."::set_availability Erreur SQL"); {
return -1; dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
} }
} }
} }
@ -1732,26 +1861,59 @@ class Propal extends CommonObject
* *
* @param User $user Object user that modify * @param User $user Object user that modify
* @param int $id Input reason id * @param int $id Input reason id
* @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_demand_reason($user, $id) function set_demand_reason($user, $id, $notrigger=0)
{ {
if (! empty($user->rights->propal->creer)) if (! empty($user->rights->propal->creer))
{ {
$error=0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."propal "; $sql = "UPDATE ".MAIN_DB_PREFIX."propal ";
$sql.= " SET fk_input_reason = '".$id."'"; $sql.= " SET fk_input_reason = '".$id."'";
$sql.= " WHERE rowid = ".$this->id; $sql.= " WHERE rowid = ".$this->id;
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->fk_input_reason = $id; $this->fk_input_reason = $id;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('PROPAL_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)
dol_syslog(get_class($this)."::set_demand_reason Erreur SQL"); {
return -1; dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
} }
} }
} }
@ -1761,26 +1923,56 @@ class Propal extends CommonObject
* *
* @param User $user Object user that modify * @param User $user Object user that modify
* @param string $ref_client Customer reference * @param string $ref_client Customer reference
* @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($user, $ref_client) function set_ref_client($user, $ref_client, $notrigger=0)
{ {
if (! empty($user->rights->propal->creer)) if (! empty($user->rights->propal->creer))
{ {
dol_syslog('Propale::set_ref_client this->id='.$this->id.', ref_client='.$ref_client); $error=0;
$this->db->begin();
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propal SET ref_client = '.(empty($ref_client) ? 'NULL' : '\''.$this->db->escape($ref_client).'\''); $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal SET ref_client = '.(empty($ref_client) ? 'NULL' : '\''.$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->oldcopy= clone $this;
$this->ref_client = $ref_client; $this->ref_client = $ref_client;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('PROPAL_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)
dol_syslog('Propale::set_ref_client Erreur '.$this->error.' - '.$sql); {
return -2; dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
} }
} }
else else
@ -1794,9 +1986,10 @@ class Propal extends CommonObject
* *
* @param User $user Object user that modify * @param User $user Object user that modify
* @param double $remise Amount discount * @param double $remise Amount 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_percent($user, $remise) function set_remise_percent($user, $remise, $notrigger=0)
{ {
$remise=trim($remise)?trim($remise):0; $remise=trim($remise)?trim($remise):0;
@ -1804,19 +1997,50 @@ class Propal extends CommonObject
{ {
$remise = price2num($remise); $remise = price2num($remise);
$error=0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET remise_percent = ".$remise; $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET remise_percent = ".$remise;
$sql.= " WHERE rowid = ".$this->id." AND fk_statut = ".self::STATUS_DRAFT; $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->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->remise_percent = $remise; $this->remise_percent = $remise;
$this->update_price(1); $this->update_price(1);
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('PROPAL_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;
} }
} }
} }
@ -1827,9 +2051,10 @@ class Propal extends CommonObject
* *
* @param User $user Object user that modify * @param User $user Object user that modify
* @param double $remise Amount discount * @param double $remise Amount 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)
{ {
$remise=trim($remise)?trim($remise):0; $remise=trim($remise)?trim($remise):0;
@ -1837,20 +2062,51 @@ class Propal extends CommonObject
{ {
$remise = price2num($remise); $remise = price2num($remise);
$error=0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."propal "; $sql = "UPDATE ".MAIN_DB_PREFIX."propal ";
$sql.= " SET remise_absolue = ".$remise; $sql.= " SET remise_absolue = ".$remise;
$sql.= " WHERE rowid = ".$this->id." AND fk_statut = ".self::STATUS_DRAFT; $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->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('PROPAL_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;
} }
} }
} }
@ -1924,9 +2180,10 @@ class Propal extends CommonObject
* @param User $user Object user that close * @param User $user Object user that close
* @param int $statut Statut * @param int $statut Statut
* @param string $note Comment * @param string $note Comment
* @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 cloture($user, $statut, $note) function cloture($user, $statut, $note, $notrigger=0)
{ {
global $langs,$conf; global $langs,$conf;
@ -1981,15 +2238,24 @@ class Propal extends CommonObject
$this->generateDocument($modelpdf, $outputlangs); $this->generateDocument($modelpdf, $outputlangs);
} }
if (! $error)
{
$this->oldcopy= clone $this;
$this->statut = $statut;
$this->date_cloture = $now;
$this->note_private = $note;
}
if (! $notrigger && empty($error))
{
// Call trigger // Call trigger
$result=$this->call_trigger($trigger_name,$user); $result=$this->call_trigger($trigger_name,$user);
if ($result < 0) { $error++; } if ($result < 0) { $error++; }
// End call triggers // End call triggers
}
if ( ! $error ) if ( ! $error )
{ {
$this->statut = $statut;
$this->db->commit(); $this->db->commit();
return 1; return 1;
} }
@ -2010,20 +2276,55 @@ class Propal extends CommonObject
/** /**
* Class invoiced the Propal * Class invoiced the Propal
* *
* @param User $user Object user
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 si ko, >0 si ok * @return int <0 si ko, >0 si ok
*/ */
function classifyBilled() function classifyBilled(User $user, $notrigger=0)
{ {
$error=0;
$this->db->begin();
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propal SET fk_statut = '.self::STATUS_BILLED; $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal SET fk_statut = '.self::STATUS_BILLED;
$sql .= ' WHERE rowid = '.$this->id.' AND fk_statut > '.self::STATUS_DRAFT.' ;'; $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->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->statut=self::STATUS_BILLED; $this->statut=self::STATUS_BILLED;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('PROPAL_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1; return 1;
} }
else else
{ {
dol_print_error($this->db); foreach($this->errors as $errmsg)
{
dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
} }
} }
@ -2046,22 +2347,55 @@ class Propal extends CommonObject
* Set draft status * Set draft status
* *
* @param User $user Object user that modify * @param User $user Object user that modify
* @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_draft($user) function set_draft($user, $notrigger=0)
{ {
$error=0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_statut = ".self::STATUS_DRAFT; $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_statut = ".self::STATUS_DRAFT;
$sql.= " WHERE rowid = ".$this->id; $sql.= " WHERE rowid = ".$this->id;
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->statut = self::STATUS_DRAFT; $this->statut = self::STATUS_DRAFT;
$this->brouillon = 1; $this->brouillon = 1;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('PROPAL_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1; return 1;
} }
else else
{ {
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;
} }
} }
@ -2364,32 +2698,65 @@ class Propal extends CommonObject
* Change the delivery time * Change the delivery time
* *
* @param int $availability_id Id of new delivery time * @param int $availability_id Id of new delivery time
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int >0 if OK, <0 if KO * @return int >0 if OK, <0 if KO
*/ */
function availability($availability_id) function availability($availability_id, $notrigger=0)
{ {
dol_syslog('Propale::availability('.$availability_id.')');
if ($this->statut >= self::STATUS_DRAFT) if ($this->statut >= self::STATUS_DRAFT)
{ {
$error=0;
$this->db->begin();
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propal'; $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal';
$sql .= ' SET fk_availability = '.$availability_id; $sql .= ' SET fk_availability = '.$availability_id;
$sql .= ' WHERE rowid='.$this->id; $sql .= ' WHERE rowid='.$this->id;
if ( $this->db->query($sql) )
dol_syslog(__METHOD__.' availability('.$availability_id.')', LOG_DEBUG);
$resql=$this->db->query($sql);
if (!$resql)
{ {
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->availability_id = $availability_id; $this->availability_id = $availability_id;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('PROPAL_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1; return 1;
} }
else else
{ {
dol_syslog('Propale::availability Erreur '.$sql.' - '.$this->db->error()); foreach($this->errors as $errmsg)
$this->error=$this->db->error(); {
return -1; dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
} }
} }
else else
{ {
dol_syslog('Propale::availability, etat propale incompatible'); $error_str='Propal status do not meet requirement '.$this->statut;
$this->error='Etat propale incompatible '.$this->statut; dol_syslog(__METHOD__.$error_str, LOG_ERR);
$this->error=$error_str;
$this->errors[]= $this->error;
return -2; return -2;
} }
} }
@ -2398,32 +2765,65 @@ class Propal extends CommonObject
* Change source demand * Change source demand
* *
* @param int $demand_reason_id Id of new source demand * @param int $demand_reason_id Id of new source demand
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int >0 si ok, <0 si ko * @return int >0 si ok, <0 si ko
*/ */
function demand_reason($demand_reason_id) function demand_reason($demand_reason_id, $notrigger=0)
{ {
dol_syslog('Propale::demand_reason('.$demand_reason_id.')');
if ($this->statut >= self::STATUS_DRAFT) if ($this->statut >= self::STATUS_DRAFT)
{ {
$error=0;
$this->db->begin();
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propal'; $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal';
$sql .= ' SET fk_input_reason = '.$demand_reason_id; $sql .= ' SET fk_input_reason = '.$demand_reason_id;
$sql .= ' WHERE rowid='.$this->id; $sql .= ' WHERE rowid='.$this->id;
if ( $this->db->query($sql) )
dol_syslog(__METHOD__.' demand_reason('.$demand_reason_id.')', LOG_DEBUG);
$resql=$this->db->query($sql);
if (!$resql)
{ {
$this->errors[]=$this->db->error();
$error++;
}
if (! $error)
{
$this->oldcopy= clone $this;
$this->demand_reason_id = $demand_reason_id; $this->demand_reason_id = $demand_reason_id;
}
if (! $notrigger && empty($error))
{
// Call trigger
$result=$this->call_trigger('PROPAL_MODIFY',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1; return 1;
} }
else else
{ {
dol_syslog('Propale::demand_reason Erreur '.$sql.' - '.$this->db->error()); foreach($this->errors as $errmsg)
$this->error=$this->db->error(); {
return -1; dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
} }
} }
else else
{ {
dol_syslog('Propale::demand_reason, etat propale incompatible'); $error_str='Propal status do not meet requirement '.$this->statut;
$this->error='Etat propale incompatible '.$this->statut; dol_syslog(__METHOD__.$error_str, LOG_ERR);
$this->error=$error_str;
$this->errors[]= $this->error;
return -2; return -2;
} }
} }

View File

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

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;
} }
} }