Fix trigger to insert lines where called when inserting line but also

when cloning parent object, making no way to know when to use them or
not. I introduced a context to allow code to know that and do not
triggers twice.
This commit is contained in:
Laurent Destailleur 2015-02-26 13:03:17 +01:00
parent 386eaf0911
commit 41a74c62e2
15 changed files with 196 additions and 149 deletions

View File

@ -57,7 +57,7 @@ class Mailing extends CommonObject
var $date_creat; var $date_creat;
var $date_valid; var $date_valid;
var $extraparams=array(); var $extraparams=array();
public $statut_dest=array(); public $statut_dest=array();
@ -78,12 +78,12 @@ class Mailing extends CommonObject
$this->statuts[1] = 'MailingStatusValidated'; $this->statuts[1] = 'MailingStatusValidated';
$this->statuts[2] = 'MailingStatusSentPartialy'; $this->statuts[2] = 'MailingStatusSentPartialy';
$this->statuts[3] = 'MailingStatusSentCompletely'; $this->statuts[3] = 'MailingStatusSentCompletely';
$this->statut_dest[-1] = 'MailingStatusError'; $this->statut_dest[-1] = 'MailingStatusError';
$this->statut_dest[1] = 'MailingStatusSent'; $this->statut_dest[1] = 'MailingStatusSent';
$this->statut_dest[2] = 'MailingStatusRead'; $this->statut_dest[2] = 'MailingStatusRead';
$this->statut_dest[3] = 'MailingStatusNotContact'; $this->statut_dest[3] = 'MailingStatusNotContact';
} }
/** /**
@ -186,7 +186,7 @@ class Mailing extends CommonObject
function fetch($rowid) function fetch($rowid)
{ {
global $conf; global $conf;
$sql = "SELECT m.rowid, m.titre, m.sujet, m.body, m.bgcolor, m.bgimage"; $sql = "SELECT m.rowid, m.titre, m.sujet, m.body, m.bgcolor, m.bgimage";
$sql.= ", m.email_from, m.email_replyto, m.email_errorsto"; $sql.= ", m.email_from, m.email_replyto, m.email_errorsto";
$sql.= ", m.statut, m.nbemail"; $sql.= ", m.statut, m.nbemail";
@ -211,14 +211,14 @@ class Mailing extends CommonObject
$this->statut = $obj->statut; $this->statut = $obj->statut;
$this->nbemail = $obj->nbemail; $this->nbemail = $obj->nbemail;
$this->titre = $obj->titre; $this->titre = $obj->titre;
$this->sujet = $obj->sujet; $this->sujet = $obj->sujet;
if (!empty($conf->global->FCKEDITOR_ENABLE_MAILING) && dol_textishtml(dol_html_entity_decode($obj->body, ENT_COMPAT | ENT_HTML401))) { if (!empty($conf->global->FCKEDITOR_ENABLE_MAILING) && dol_textishtml(dol_html_entity_decode($obj->body, ENT_COMPAT | ENT_HTML401))) {
$this->body = dol_html_entity_decode($obj->body, ENT_COMPAT | ENT_HTML401); $this->body = dol_html_entity_decode($obj->body, ENT_COMPAT | ENT_HTML401);
}else { }else {
$this->body = $obj->body; $this->body = $obj->body;
} }
$this->bgcolor = $obj->bgcolor; $this->bgcolor = $obj->bgcolor;
$this->bgimage = $obj->bgimage; $this->bgimage = $obj->bgimage;
@ -232,7 +232,7 @@ class Mailing extends CommonObject
$this->date_creat = $this->db->jdate($obj->date_creat); $this->date_creat = $this->db->jdate($obj->date_creat);
$this->date_valid = $this->db->jdate($obj->date_valid); $this->date_valid = $this->db->jdate($obj->date_valid);
$this->date_envoi = $this->db->jdate($obj->date_envoi); $this->date_envoi = $this->db->jdate($obj->date_envoi);
$this->extraparams = (array) json_decode($obj->extraparams, true); $this->extraparams = (array) json_decode($obj->extraparams, true);
return 1; return 1;
@ -267,6 +267,8 @@ class Mailing extends CommonObject
$object=new Mailing($this->db); $object=new Mailing($this->db);
$object->context['createfromclone']='createfromclone';
$this->db->begin(); $this->db->begin();
// Load source object // Load source object
@ -313,13 +315,13 @@ class Mailing extends CommonObject
{ {
//Clone target //Clone target
if (!empty($option2)) { if (!empty($option2)) {
require_once DOL_DOCUMENT_ROOT .'/core/modules/mailings/modules_mailings.php'; require_once DOL_DOCUMENT_ROOT .'/core/modules/mailings/modules_mailings.php';
$mailing_target = new MailingTargets($this->db); $mailing_target = new MailingTargets($this->db);
$target_array=array(); $target_array=array();
$sql = "SELECT fk_contact, "; $sql = "SELECT fk_contact, ";
$sql.=" lastname, "; $sql.=" lastname, ";
$sql.=" firstname,"; $sql.=" firstname,";
@ -330,7 +332,7 @@ class Mailing extends CommonObject
$sql.=" source_type "; $sql.=" source_type ";
$sql.= " FROM ".MAIN_DB_PREFIX."mailing_cibles "; $sql.= " FROM ".MAIN_DB_PREFIX."mailing_cibles ";
$sql.= " WHERE fk_mailing = ".$fromid; $sql.= " WHERE fk_mailing = ".$fromid;
dol_syslog(get_class($this)."::createFromClone", LOG_DEBUG); dol_syslog(get_class($this)."::createFromClone", LOG_DEBUG);
$result=$this->db->query($sql); $result=$this->db->query($sql);
if ($result) if ($result)
@ -338,17 +340,17 @@ class Mailing extends CommonObject
if ($this->db->num_rows($result)) if ($this->db->num_rows($result))
{ {
while ($obj = $this->db->fetch_object($result)) { while ($obj = $this->db->fetch_object($result)) {
$target_array[]=array('fk_contact'=>$obj->fk_contact, $target_array[]=array('fk_contact'=>$obj->fk_contact,
'lastname'=>$obj->lastname, 'lastname'=>$obj->lastname,
'firstname'=>$obj->firstname, 'firstname'=>$obj->firstname,
'email'=>$obj->email, 'email'=>$obj->email,
'other'=>$obj->other, 'other'=>$obj->other,
'source_url'=>$obj->source_url, 'source_url'=>$obj->source_url,
'source_id'=>$obj->source_id, 'source_id'=>$obj->source_id,
'source_type'=>$obj->source_type); 'source_type'=>$obj->source_type);
} }
} }
} }
else else
@ -356,12 +358,14 @@ class Mailing extends CommonObject
$this->error=$this->db->lasterror(); $this->error=$this->db->lasterror();
return -1; return -1;
} }
$mailing_target->add_to_target($object->id, $target_array); $mailing_target->add_to_target($object->id, $target_array);
} }
} }
unset($object->context['createfromclone']);
// End // End
if (! $error) if (! $error)
{ {
@ -514,7 +518,7 @@ class Mailing extends CommonObject
} }
} }
/** /**
* Renvoi le libelle d'un statut donne * Renvoi le libelle d'un statut donne
* *
@ -526,7 +530,7 @@ class Mailing extends CommonObject
{ {
global $langs; global $langs;
$langs->load('mails'); $langs->load('mails');
if ($mode == 0) if ($mode == 0)
{ {
return $langs->trans($this->statut_dest[$statut]); return $langs->trans($this->statut_dest[$statut]);
@ -563,10 +567,10 @@ class Mailing extends CommonObject
if ($statut==2) return $langs->trans("MailingStatusRead").' '.img_picto($langs->trans("MailingStatusRead"),'statut6'); if ($statut==2) return $langs->trans("MailingStatusRead").' '.img_picto($langs->trans("MailingStatusRead"),'statut6');
if ($statut==3) return $langs->trans("MailingStatusNotContact").' '.img_picto($langs->trans("MailingStatusNotContact"),'statut8'); if ($statut==3) return $langs->trans("MailingStatusNotContact").' '.img_picto($langs->trans("MailingStatusNotContact"),'statut8');
} }
} }
} }

View File

@ -393,6 +393,8 @@ class Propal extends CommonObject
// Insert line // Insert line
$this->line=new PropaleLigne($this->db); $this->line=new PropaleLigne($this->db);
$this->line->context = $this->context;
$this->line->fk_propal=$this->id; $this->line->fk_propal=$this->id;
$this->line->label=$label; $this->line->label=$label;
$this->line->desc=$desc; $this->line->desc=$desc;
@ -953,6 +955,8 @@ class Propal extends CommonObject
{ {
global $user,$langs,$conf,$hookmanager; global $user,$langs,$conf,$hookmanager;
$this->context['createfromclone']='createfromclone';
$error=0; $error=0;
$now=dol_now(); $now=dol_now();
@ -1046,6 +1050,8 @@ class Propal extends CommonObject
// End call triggers // End call triggers
} }
unset($this->context['createfromclone']);
// End // End
if (! $error) if (! $error)
{ {

View File

@ -878,6 +878,8 @@ class Commande extends CommonOrder
$error=0; $error=0;
$this->context['createfromclone'] = 'createfromclone';
$this->db->begin(); $this->db->begin();
// get extrafields so they will be clone // get extrafields so they will be clone
@ -942,6 +944,8 @@ class Commande extends CommonOrder
// End call triggers // End call triggers
} }
unset($this->context['createfromclone']);
// End // End
if (! $error) if (! $error)
{ {

View File

@ -300,7 +300,9 @@ class BankCateg // extends CommonObject
$object=new BankCateg($this->db); $object=new BankCateg($this->db);
$this->db->begin(); $object->context['createfromclone'] = 'createfromclone';
$this->db->begin();
// Load source object // Load source object
$object->fetch($fromid); $object->fetch($fromid);
@ -327,6 +329,8 @@ class BankCateg // extends CommonObject
} }
unset($object->context['createfromclone']);
// End // End
if (! $error) if (! $error)
{ {

View File

@ -607,6 +607,8 @@ class Facture extends CommonInvoice
$error=0; $error=0;
$this->context['createfromclone'] = 'createfromclone';
$this->db->begin(); $this->db->begin();
// get extrafields so they will be clone // get extrafields so they will be clone
@ -693,6 +695,8 @@ class Facture extends CommonInvoice
// End call triggers // End call triggers
} }
unset($this->context['createfromclone']);
// End // End
if (! $error) if (! $error)
{ {

View File

@ -33,6 +33,7 @@ class PaymentTerm // extends CommonObject
var $errors=array(); //!< To return several error codes (or messages) var $errors=array(); //!< To return several error codes (or messages)
//public $element='c_payment_term'; //!< Id that identify managed objects //public $element='c_payment_term'; //!< Id that identify managed objects
//public $table_element='c_payment_term'; //!< Name of table without prefix where object is stored //public $table_element='c_payment_term'; //!< Name of table without prefix where object is stored
var $context =array();
var $id; var $id;
@ -409,6 +410,8 @@ class PaymentTerm // extends CommonObject
$object=new PaymentTerm($this->db); $object=new PaymentTerm($this->db);
$object->context['createfromclone'] = 'createfromclone';
$this->db->begin(); $this->db->begin();
// Load source object // Load source object
@ -436,6 +439,8 @@ class PaymentTerm // extends CommonObject
} }
unset($this->context['createfromclone']);
// End // End
if (! $error) if (! $error)
{ {

View File

@ -384,6 +384,8 @@ class PaymentSocialContribution extends CommonObject
$object=new PaymentSocialContribution($this->db); $object=new PaymentSocialContribution($this->db);
$object->context['createfromclone'] = 'createfromclone';
$this->db->begin(); $this->db->begin();
// Load source object // Load source object
@ -411,6 +413,8 @@ class PaymentSocialContribution extends CommonObject
} }
unset($this->context['createfromclone']);
// End // End
if (! $error) if (! $error)
{ {

View File

@ -38,6 +38,7 @@ abstract class CommonObject
public $error; public $error;
public $errors; public $errors;
public $canvas; // Contains canvas name if it is public $canvas; // Contains canvas name if it is
public $context=array(); // Use to pass context information
public $name; public $name;
public $lastname; public $lastname;

View File

@ -718,6 +718,8 @@ class Cronjob extends CommonObject
$object=new Cronjob($this->db); $object=new Cronjob($this->db);
$object->context['createfromclone'] = 'createfromclone';
$this->db->begin(); $this->db->begin();
// Load source object // Load source object
@ -744,6 +746,8 @@ class Cronjob extends CommonObject
} }
unset($this->context['createfromclone']);
// End // End
if (! $error) if (! $error)
{ {

View File

@ -1024,7 +1024,9 @@ class CommandeFournisseur extends CommonOrder
$error=0; $error=0;
$this->db->begin(); $this->context['createfromclone'] = 'createfromclone';
$this->db->begin();
// Load source object // Load source object
$objFrom = dol_clone($this); $objFrom = dol_clone($this);
@ -1060,7 +1062,9 @@ class CommandeFournisseur extends CommonOrder
// End call triggers // End call triggers
} }
// End unset($this->context['createfromclone']);
// End
if (! $error) if (! $error)
{ {
$this->db->commit(); $this->db->commit();

View File

@ -1699,7 +1699,9 @@ class FactureFournisseur extends CommonInvoice
$object=new FactureFournisseur($this->db); $object=new FactureFournisseur($this->db);
$this->db->begin(); $object->context['createfromclone'] = 'createfromclone';
$this->db->begin();
// Load source object // Load source object
$object->fetch($fromid); $object->fetch($fromid);
@ -1745,6 +1747,8 @@ class FactureFournisseur extends CommonInvoice
} }
unset($object->context['createfromclone']);
// End // End
if (! $error) if (! $error)
{ {

View File

@ -327,7 +327,9 @@ class Productbatch extends CommonObject
$object=new Productbatch($this->db); $object=new Productbatch($this->db);
$this->db->begin(); $object->context['createfromclone']='createfromclone';
$this->db->begin();
// Load source object // Load source object
$object->fetch($fromid); $object->fetch($fromid);
@ -353,6 +355,8 @@ class Productbatch extends CommonObject
} }
unset($object->context['createfromclone']);
// End // End
if (! $error) if (! $error)
{ {

View File

@ -75,9 +75,9 @@ class Productcustomerprice extends CommonObject
global $conf, $langs; global $conf, $langs;
$error = 0; $error = 0;
// Clean parameters // Clean parameters
if (isset($this->entity)) if (isset($this->entity))
$this->entity = trim($this->entity); $this->entity = trim($this->entity);
if (isset($this->fk_product)) if (isset($this->fk_product))
@ -106,16 +106,16 @@ class Productcustomerprice extends CommonObject
$this->fk_user = trim($this->fk_user); $this->fk_user = trim($this->fk_user);
if (isset($this->import_key)) if (isset($this->import_key))
$this->import_key = trim($this->import_key); $this->import_key = trim($this->import_key);
// Check parameters // Check parameters
// Put here code to add control on parameters values // Put here code to add control on parameters values
if ($this->price != '' || $this->price == 0) { if ($this->price != '' || $this->price == 0) {
if ($this->price_base_type == 'TTC') { if ($this->price_base_type == 'TTC') {
$this->price_ttc = price2num($this->price, 'MU'); $this->price_ttc = price2num($this->price, 'MU');
$this->price = price2num($this->price) / (1 + ($this->tva_tx / 100)); $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
$this->price = price2num($this->price, 'MU'); $this->price = price2num($this->price, 'MU');
if ($this->price_min != '' || $this->price_min == 0) { if ($this->price_min != '' || $this->price_min == 0) {
$this->price_min_ttc = price2num($this->price_min, 'MU'); $this->price_min_ttc = price2num($this->price_min, 'MU');
$this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100)); $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
@ -128,7 +128,7 @@ class Productcustomerprice extends CommonObject
$this->price = price2num($this->price, 'MU'); $this->price = price2num($this->price, 'MU');
$this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price; $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
$this->price_ttc = price2num($this->price_ttc, 'MU'); $this->price_ttc = price2num($this->price_ttc, 'MU');
if ($this->price_min != '' || $this->price_min == 0) { if ($this->price_min != '' || $this->price_min == 0) {
$this->price_min = price2num($this->price_min, 'MU'); $this->price_min = price2num($this->price_min, 'MU');
$this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100)); $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
@ -140,10 +140,10 @@ class Productcustomerprice extends CommonObject
} }
} }
} }
// Insert request // Insert request
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_customer_price("; $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_customer_price(";
$sql .= "entity,"; $sql .= "entity,";
$sql .= "datec,"; $sql .= "datec,";
$sql .= "fk_product,"; $sql .= "fk_product,";
@ -159,9 +159,9 @@ class Productcustomerprice extends CommonObject
$sql .= "localtax2_tx,"; $sql .= "localtax2_tx,";
$sql .= "fk_user,"; $sql .= "fk_user,";
$sql .= "import_key"; $sql .= "import_key";
$sql .= ") VALUES ("; $sql .= ") VALUES (";
$sql .= " " . $conf->entity . ","; $sql .= " " . $conf->entity . ",";
$sql .= " '" . $this->db->idate(dol_now()) . "',"; $sql .= " '" . $this->db->idate(dol_now()) . "',";
$sql .= " " . (! isset($this->fk_product) ? 'NULL' : "'" . $this->fk_product . "'") . ","; $sql .= " " . (! isset($this->fk_product) ? 'NULL' : "'" . $this->fk_product . "'") . ",";
@ -177,25 +177,25 @@ class Productcustomerprice extends CommonObject
$sql .= " " . (! isset($this->localtax2_tx) ? 'NULL' : "'" . $this->localtax2_tx . "'") . ","; $sql .= " " . (! isset($this->localtax2_tx) ? 'NULL' : "'" . $this->localtax2_tx . "'") . ",";
$sql .= " " . $user->id . ","; $sql .= " " . $user->id . ",";
$sql .= " " . (! isset($this->import_key) ? 'NULL' : "'" . $this->db->escape($this->import_key) . "'") . ""; $sql .= " " . (! isset($this->import_key) ? 'NULL' : "'" . $this->db->escape($this->import_key) . "'") . "";
$sql .= ")"; $sql .= ")";
$this->db->begin(); $this->db->begin();
dol_syslog(get_class($this) . "::create", LOG_DEBUG); dol_syslog(get_class($this) . "::create", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if (! $resql) { if (! $resql) {
$error ++; $error ++;
$this->errors [] = "Error " . $this->db->lasterror(); $this->errors [] = "Error " . $this->db->lasterror();
} }
if (! $error) { if (! $error) {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "product_customer_price"); $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "product_customer_price");
if (! $notrigger) { if (! $notrigger) {
// Uncomment this and change MYOBJECT to your own tag if you // Uncomment this and change MYOBJECT to your own tag if you
// want this action calls a trigger. // want this action calls a trigger.
// // Call triggers // // Call triggers
// include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
// $interface=new Interfaces($this->db); // $interface=new Interfaces($this->db);
@ -204,14 +204,14 @@ class Productcustomerprice extends CommonObject
// // End call triggers // // End call triggers
} }
} }
if (! $error) { if (! $error) {
$result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate); $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
if ($result < 0) { if ($result < 0) {
$error ++; $error ++;
} }
} }
// Commit or rollback // Commit or rollback
if ($error) { if ($error) {
foreach ( $this->errors as $errmsg ) { foreach ( $this->errors as $errmsg ) {
@ -237,7 +237,7 @@ class Productcustomerprice extends CommonObject
global $langs; global $langs;
$sql = "SELECT"; $sql = "SELECT";
$sql .= " t.rowid,"; $sql .= " t.rowid,";
$sql .= " t.entity,"; $sql .= " t.entity,";
$sql .= " t.datec,"; $sql .= " t.datec,";
$sql .= " t.tms,"; $sql .= " t.tms,";
@ -254,18 +254,18 @@ class Productcustomerprice extends CommonObject
$sql .= " t.localtax2_tx,"; $sql .= " t.localtax2_tx,";
$sql .= " t.fk_user,"; $sql .= " t.fk_user,";
$sql .= " t.import_key"; $sql .= " t.import_key";
$sql .= " FROM " . MAIN_DB_PREFIX . "product_customer_price as t"; $sql .= " FROM " . MAIN_DB_PREFIX . "product_customer_price as t";
$sql .= " WHERE t.rowid = " . $id; $sql .= " WHERE t.rowid = " . $id;
dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
if ($this->db->num_rows($resql)) { if ($this->db->num_rows($resql)) {
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
$this->id = $obj->rowid; $this->id = $obj->rowid;
$this->entity = $obj->entity; $this->entity = $obj->entity;
$this->datec = $this->db->jdate($obj->datec); $this->datec = $this->db->jdate($obj->datec);
$this->tms = $this->db->jdate($obj->tms); $this->tms = $this->db->jdate($obj->tms);
@ -284,7 +284,7 @@ class Productcustomerprice extends CommonObject
$this->import_key = $obj->import_key; $this->import_key = $obj->import_key;
} }
$this->db->free($resql); $this->db->free($resql);
return 1; return 1;
} else { } else {
$this->error = "Error " . $this->db->lasterror(); $this->error = "Error " . $this->db->lasterror();
@ -307,7 +307,7 @@ class Productcustomerprice extends CommonObject
global $langs; global $langs;
$sql = "SELECT"; $sql = "SELECT";
$sql .= " t.rowid,"; $sql .= " t.rowid,";
$sql .= " t.entity,"; $sql .= " t.entity,";
$sql .= " t.datec,"; $sql .= " t.datec,";
$sql .= " t.tms,"; $sql .= " t.tms,";
@ -332,7 +332,7 @@ class Productcustomerprice extends CommonObject
$sql .= " WHERE soc.rowid=t.fk_soc "; $sql .= " WHERE soc.rowid=t.fk_soc ";
$sql .= " AND prod.rowid=t.fk_product "; $sql .= " AND prod.rowid=t.fk_product ";
$sql .= " AND prod.entity IN (" . getEntity('product', 1) . ")"; $sql .= " AND prod.entity IN (" . getEntity('product', 1) . ")";
// Manage filter // Manage filter
if (count($filter) > 0) { if (count($filter) > 0) {
foreach ( $filter as $key => $value ) { foreach ( $filter as $key => $value ) {
@ -346,27 +346,27 @@ class Productcustomerprice extends CommonObject
} }
} }
} }
if (! empty($sortfield)) { if (! empty($sortfield)) {
$sql .= " ORDER BY " . $sortfield . ' ' . $sortorder; $sql .= " ORDER BY " . $sortfield . ' ' . $sortorder;
} }
if (! empty($limit)) { if (! empty($limit)) {
$sql .= ' ' . $this->db->plimit($limit + 1, $offset); $sql .= ' ' . $this->db->plimit($limit + 1, $offset);
} }
dol_syslog(get_class($this) . "::fetch_all", LOG_DEBUG); dol_syslog(get_class($this) . "::fetch_all", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
$this->lines = array (); $this->lines = array ();
$num = $this->db->num_rows($resql); $num = $this->db->num_rows($resql);
while ( $obj = $this->db->fetch_object($resql) ) { while ( $obj = $this->db->fetch_object($resql) ) {
$line = new PriceByCustomerLine(); $line = new PriceByCustomerLine();
$line->id = $obj->rowid; $line->id = $obj->rowid;
$line->entity = $obj->entity; $line->entity = $obj->entity;
$line->datec = $this->db->jdate($obj->datec); $line->datec = $this->db->jdate($obj->datec);
$line->tms = $this->db->jdate($obj->tms); $line->tms = $this->db->jdate($obj->tms);
@ -385,11 +385,11 @@ class Productcustomerprice extends CommonObject
$line->import_key = $obj->import_key; $line->import_key = $obj->import_key;
$line->socname = $obj->socname; $line->socname = $obj->socname;
$line->prodref = $obj->prodref; $line->prodref = $obj->prodref;
$this->lines [] = $line; $this->lines [] = $line;
} }
$this->db->free($resql); $this->db->free($resql);
return $num; return $num;
} else { } else {
$this->error = "Error " . $this->db->lasterror(); $this->error = "Error " . $this->db->lasterror();
@ -412,7 +412,7 @@ class Productcustomerprice extends CommonObject
global $langs; global $langs;
$sql = "SELECT"; $sql = "SELECT";
$sql .= " t.rowid,"; $sql .= " t.rowid,";
$sql .= " t.entity,"; $sql .= " t.entity,";
$sql .= " t.datec,"; $sql .= " t.datec,";
$sql .= " t.fk_product,"; $sql .= " t.fk_product,";
@ -436,7 +436,7 @@ class Productcustomerprice extends CommonObject
$sql .= " WHERE soc.rowid=t.fk_soc "; $sql .= " WHERE soc.rowid=t.fk_soc ";
$sql .= " AND prod.rowid=t.fk_product "; $sql .= " AND prod.rowid=t.fk_product ";
$sql .= " AND prod.entity IN (" . getEntity('product', 1) . ")"; $sql .= " AND prod.entity IN (" . getEntity('product', 1) . ")";
// Manage filter // Manage filter
if (count($filter) > 0) { if (count($filter) > 0) {
foreach ( $filter as $key => $value ) { foreach ( $filter as $key => $value ) {
@ -450,27 +450,27 @@ class Productcustomerprice extends CommonObject
} }
} }
} }
if (! empty($sortfield)) { if (! empty($sortfield)) {
$sql .= " ORDER BY " . $sortfield . ' ' . $sortorder; $sql .= " ORDER BY " . $sortfield . ' ' . $sortorder;
} }
if (! empty($limit)) { if (! empty($limit)) {
$sql .= ' ' . $this->db->plimit($limit + 1, $offset); $sql .= ' ' . $this->db->plimit($limit + 1, $offset);
} }
dol_syslog(get_class($this) . "::fetch_all_log", LOG_DEBUG); dol_syslog(get_class($this) . "::fetch_all_log", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
$this->lines = array (); $this->lines = array ();
$num = $this->db->num_rows($resql); $num = $this->db->num_rows($resql);
while ( $obj = $this->db->fetch_object($resql) ) { while ( $obj = $this->db->fetch_object($resql) ) {
$line = new PriceByCustomerLine(); $line = new PriceByCustomerLine();
$line->id = $obj->rowid; $line->id = $obj->rowid;
$line->entity = $obj->entity; $line->entity = $obj->entity;
$line->datec = $this->db->jdate($obj->datec); $line->datec = $this->db->jdate($obj->datec);
$line->tms = $this->db->jdate($obj->tms); $line->tms = $this->db->jdate($obj->tms);
@ -489,11 +489,11 @@ class Productcustomerprice extends CommonObject
$line->import_key = $obj->import_key; $line->import_key = $obj->import_key;
$line->socname = $obj->socname; $line->socname = $obj->socname;
$line->prodref = $obj->prodref; $line->prodref = $obj->prodref;
$this->lines [] = $line; $this->lines [] = $line;
} }
$this->db->free($resql); $this->db->free($resql);
return $num; return $num;
} else { } else {
$this->error = "Error " . $this->db->lasterror(); $this->error = "Error " . $this->db->lasterror();
@ -513,9 +513,9 @@ class Productcustomerprice extends CommonObject
global $conf, $langs; global $conf, $langs;
$error = 0; $error = 0;
// Clean parameters // Clean parameters
if (isset($this->entity)) if (isset($this->entity))
$this->entity = trim($this->entity); $this->entity = trim($this->entity);
if (isset($this->fk_product)) if (isset($this->fk_product))
@ -544,16 +544,16 @@ class Productcustomerprice extends CommonObject
$this->fk_user = trim($this->fk_user); $this->fk_user = trim($this->fk_user);
if (isset($this->import_key)) if (isset($this->import_key))
$this->import_key = trim($this->import_key); $this->import_key = trim($this->import_key);
// Check parameters // Check parameters
// Put here code to add a control on parameters values // Put here code to add a control on parameters values
if ($this->price != '' || $this->price == 0) { if ($this->price != '' || $this->price == 0) {
if ($this->price_base_type == 'TTC') { if ($this->price_base_type == 'TTC') {
$this->price_ttc = price2num($this->price, 'MU'); $this->price_ttc = price2num($this->price, 'MU');
$this->price = price2num($this->price) / (1 + ($this->tva_tx / 100)); $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
$this->price = price2num($this->price, 'MU'); $this->price = price2num($this->price, 'MU');
if ($this->price_min != '' || $this->price_min == 0) { if ($this->price_min != '' || $this->price_min == 0) {
$this->price_min_ttc = price2num($this->price_min, 'MU'); $this->price_min_ttc = price2num($this->price_min, 'MU');
$this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100)); $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
@ -566,7 +566,7 @@ class Productcustomerprice extends CommonObject
$this->price = price2num($this->price, 'MU'); $this->price = price2num($this->price, 'MU');
$this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price; $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
$this->price_ttc = price2num($this->price_ttc, 'MU'); $this->price_ttc = price2num($this->price_ttc, 'MU');
if ($this->price_min != '' || $this->price_min == 0) { if ($this->price_min != '' || $this->price_min == 0) {
$this->price_min = price2num($this->price_min, 'MU'); $this->price_min = price2num($this->price_min, 'MU');
$this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100)); $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
@ -578,11 +578,11 @@ class Productcustomerprice extends CommonObject
} }
} }
} }
// Do a copy of current record into log table // Do a copy of current record into log table
// Insert request // Insert request
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_customer_price_log("; $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_customer_price_log(";
$sql .= "entity,"; $sql .= "entity,";
$sql .= "datec,"; $sql .= "datec,";
$sql .= "fk_product,"; $sql .= "fk_product,";
@ -598,10 +598,10 @@ class Productcustomerprice extends CommonObject
$sql .= "localtax2_tx,"; $sql .= "localtax2_tx,";
$sql .= "fk_user,"; $sql .= "fk_user,";
$sql .= "import_key"; $sql .= "import_key";
$sql .= ") "; $sql .= ") ";
$sql .= "SELECT"; $sql .= "SELECT";
$sql .= " t.entity,"; $sql .= " t.entity,";
$sql .= " t.datec,"; $sql .= " t.datec,";
$sql .= " t.fk_product,"; $sql .= " t.fk_product,";
@ -617,10 +617,10 @@ class Productcustomerprice extends CommonObject
$sql .= " t.localtax2_tx,"; $sql .= " t.localtax2_tx,";
$sql .= " t.fk_user,"; $sql .= " t.fk_user,";
$sql .= " t.import_key"; $sql .= " t.import_key";
$sql .= " FROM " . MAIN_DB_PREFIX . "product_customer_price as t"; $sql .= " FROM " . MAIN_DB_PREFIX . "product_customer_price as t";
$sql .= " WHERE t.rowid = " . $this->id; $sql .= " WHERE t.rowid = " . $this->id;
$this->db->begin(); $this->db->begin();
dol_syslog(get_class($this) . "::update", LOG_DEBUG); dol_syslog(get_class($this) . "::update", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
@ -628,10 +628,10 @@ class Productcustomerprice extends CommonObject
$error ++; $error ++;
$this->errors [] = "Error " . $this->db->lasterror(); $this->errors [] = "Error " . $this->db->lasterror();
} }
// Update request // Update request
$sql = "UPDATE " . MAIN_DB_PREFIX . "product_customer_price SET"; $sql = "UPDATE " . MAIN_DB_PREFIX . "product_customer_price SET";
$sql .= " entity=" . $conf->entity . ","; $sql .= " entity=" . $conf->entity . ",";
$sql .= " datec='" . $this->db->idate(dol_now()) . "',"; $sql .= " datec='" . $this->db->idate(dol_now()) . "',";
$sql .= " tms=" . (dol_strlen($this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : 'null') . ","; $sql .= " tms=" . (dol_strlen($this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : 'null') . ",";
@ -648,21 +648,21 @@ class Productcustomerprice extends CommonObject
$sql .= " localtax2_tx=" . (isset($this->localtax2_tx) ? $this->localtax2_tx : "null") . ","; $sql .= " localtax2_tx=" . (isset($this->localtax2_tx) ? $this->localtax2_tx : "null") . ",";
$sql .= " fk_user=" . $user->id . ","; $sql .= " fk_user=" . $user->id . ",";
$sql .= " import_key=" . (isset($this->import_key) ? "'" . $this->db->escape($this->import_key) . "'" : "null") . ""; $sql .= " import_key=" . (isset($this->import_key) ? "'" . $this->db->escape($this->import_key) . "'" : "null") . "";
$sql .= " WHERE rowid=" . $this->id; $sql .= " WHERE rowid=" . $this->id;
dol_syslog(get_class($this) . "::update", LOG_DEBUG); dol_syslog(get_class($this) . "::update", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if (! $resql) { if (! $resql) {
$error ++; $error ++;
$this->errors [] = "Error " . $this->db->lasterror(); $this->errors [] = "Error " . $this->db->lasterror();
} }
if (! $error) { if (! $error) {
if (! $notrigger) { if (! $notrigger) {
// Uncomment this and change MYOBJECT to your own tag if you // Uncomment this and change MYOBJECT to your own tag if you
// want this action calls a trigger. // want this action calls a trigger.
// // Call triggers // // Call triggers
// include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
// $interface=new Interfaces($this->db); // $interface=new Interfaces($this->db);
@ -671,14 +671,14 @@ class Productcustomerprice extends CommonObject
// // End call triggers // // End call triggers
} }
} }
if (! $error) { if (! $error) {
$result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate); $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
if ($result < 0) { if ($result < 0) {
$error ++; $error ++;
} }
} }
// Commit or rollback // Commit or rollback
if ($error) { if ($error) {
foreach ( $this->errors as $errmsg ) { foreach ( $this->errors as $errmsg ) {
@ -703,50 +703,50 @@ class Productcustomerprice extends CommonObject
function setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate) { function setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate) {
$error = 0; $error = 0;
// Find all susidiaries // Find all susidiaries
$sql = "SELECT s.rowid"; $sql = "SELECT s.rowid";
$sql .= " FROM " . MAIN_DB_PREFIX . "societe as s"; $sql .= " FROM " . MAIN_DB_PREFIX . "societe as s";
$sql .= " WHERE s.parent = " . $this->fk_soc; $sql .= " WHERE s.parent = " . $this->fk_soc;
$sql .= " AND s.entity IN (" . getEntity('societe', 1) . ")"; $sql .= " AND s.entity IN (" . getEntity('societe', 1) . ")";
dol_syslog(get_class($this) . "::setPriceOnAffiliateThirdparty", LOG_DEBUG); dol_syslog(get_class($this) . "::setPriceOnAffiliateThirdparty", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
$this->lines = array (); $this->lines = array ();
$num = $this->db->num_rows($resql); $num = $this->db->num_rows($resql);
while ( ($obj = $this->db->fetch_object($resql)) && (empty($error)) ) { while ( ($obj = $this->db->fetch_object($resql)) && (empty($error)) ) {
// find if there is an existing line for the product and the subsidiaries // find if there is an existing line for the product and the subsidiaries
$prodsocprice = new Productcustomerprice($this->db); $prodsocprice = new Productcustomerprice($this->db);
$filter = array ( $filter = array (
't.fk_product' => $this->fk_product,'t.fk_soc' => $obj->rowid 't.fk_product' => $this->fk_product,'t.fk_soc' => $obj->rowid
); );
$result = $prodsocprice->fetch_all('', '', 0, 0, $filter); $result = $prodsocprice->fetch_all('', '', 0, 0, $filter);
if ($result < 0) { if ($result < 0) {
$error ++; $error ++;
$this->error = $prodsocprice->error; $this->error = $prodsocprice->error;
} else { } else {
// There is one line // There is one line
if (count($prodsocprice->lines) > 0) { if (count($prodsocprice->lines) > 0) {
// If force update => Update // If force update => Update
if (! empty($forceupdateaffiliate)) { if (! empty($forceupdateaffiliate)) {
$prodsocpriceupd = new Productcustomerprice($this->db); $prodsocpriceupd = new Productcustomerprice($this->db);
$prodsocpriceupd->fetch($prodsocprice->lines [0]->id); $prodsocpriceupd->fetch($prodsocprice->lines [0]->id);
$prodsocpriceupd->price = $this->price; $prodsocpriceupd->price = $this->price;
$prodsocpriceupd->price_min = $this->price_min; $prodsocpriceupd->price_min = $this->price_min;
$prodsocpriceupd->price_base_type = $this->price_base_type; $prodsocpriceupd->price_base_type = $this->price_base_type;
$prodsocpriceupd->tva_tx = $this->tva_tx; $prodsocpriceupd->tva_tx = $this->tva_tx;
$prodsocpriceupd->recuperableonly = $this->recuperableonly; $prodsocpriceupd->recuperableonly = $this->recuperableonly;
$resultupd = $prodsocpriceupd->update($user, 0, $forceupdateaffiliate); $resultupd = $prodsocpriceupd->update($user, 0, $forceupdateaffiliate);
if ($result < 0) { if ($result < 0) {
$error ++; $error ++;
@ -763,7 +763,7 @@ class Productcustomerprice extends CommonObject
$prodsocpricenew->price_base_type = $this->price_base_type; $prodsocpricenew->price_base_type = $this->price_base_type;
$prodsocpricenew->tva_tx = $this->tva_tx; $prodsocpricenew->tva_tx = $this->tva_tx;
$prodsocpricenew->recuperableonly = $this->recuperableonly; $prodsocpricenew->recuperableonly = $this->recuperableonly;
$resultupd = $prodsocpricenew->create($user, 0, $forceupdateaffiliate); $resultupd = $prodsocpricenew->create($user, 0, $forceupdateaffiliate);
if ($result < 0) { if ($result < 0) {
$error ++; $error ++;
@ -773,7 +773,7 @@ class Productcustomerprice extends CommonObject
} }
} }
$this->db->free($resql); $this->db->free($resql);
if (empty($error)) { if (empty($error)) {
return 1; return 1;
} else { } else {
@ -796,14 +796,14 @@ class Productcustomerprice extends CommonObject
global $conf, $langs; global $conf, $langs;
$error = 0; $error = 0;
$this->db->begin(); $this->db->begin();
if (! $error) { if (! $error) {
if (! $notrigger) { if (! $notrigger) {
// Uncomment this and change MYOBJECT to your own tag if you // Uncomment this and change MYOBJECT to your own tag if you
// want this action calls a trigger. // want this action calls a trigger.
// // Call triggers // // Call triggers
// include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
// $interface=new Interfaces($this->db); // $interface=new Interfaces($this->db);
@ -812,11 +812,11 @@ class Productcustomerprice extends CommonObject
// // End call triggers // // End call triggers
} }
} }
if (! $error) { if (! $error) {
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_customer_price"; $sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_customer_price";
$sql .= " WHERE rowid=" . $this->id; $sql .= " WHERE rowid=" . $this->id;
dol_syslog(get_class($this) . "::delete", LOG_DEBUG); dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if (! $resql) { if (! $resql) {
@ -824,7 +824,7 @@ class Productcustomerprice extends CommonObject
$this->errors [] = "Error " . $this->db->lasterror(); $this->errors [] = "Error " . $this->db->lasterror();
} }
} }
// Commit or rollback // Commit or rollback
if ($error) { if ($error) {
foreach ( $this->errors as $errmsg ) { foreach ( $this->errors as $errmsg ) {
@ -848,33 +848,37 @@ class Productcustomerprice extends CommonObject
function createFromClone($fromid) { function createFromClone($fromid) {
global $user, $langs; global $user, $langs;
$error = 0; $error = 0;
$object = new Productcustomerprice($this->db); $object = new Productcustomerprice($this->db);
$object->context['createfromclone']='createfromclone';
$this->db->begin(); $this->db->begin();
// Load source object // Load source object
$object->fetch($fromid); $object->fetch($fromid);
$object->id = 0; $object->id = 0;
$object->statut = 0; $object->statut = 0;
// Clear fields // Clear fields
// ... // ...
// Create clone // Create clone
$result = $object->create($user); $result = $object->create($user);
// Other options // Other options
if ($result < 0) { if ($result < 0) {
$this->error = $object->error; $this->error = $object->error;
$error ++; $error ++;
} }
if (! $error) { if (! $error) {
} }
unset($object->context['createfromclone']);
// End // End
if (! $error) { if (! $error) {
$this->db->commit(); $this->db->commit();
@ -894,7 +898,7 @@ class Productcustomerprice extends CommonObject
function initAsSpecimen() { function initAsSpecimen() {
$this->id = 0; $this->id = 0;
$this->entity = ''; $this->entity = '';
$this->datec = ''; $this->datec = '';
$this->tms = ''; $this->tms = '';

View File

@ -1017,6 +1017,8 @@ class Project extends CommonObject
$clone_project=new Project($this->db); $clone_project=new Project($this->db);
$clone_project->context['createfromclone']='createfromclone';
$this->db->begin(); $this->db->begin();
// Load source object // Load source object
@ -1068,8 +1070,6 @@ class Project extends CommonObject
if (! $error) if (! $error)
{ {
$this->db->commit();
//Get the new project id //Get the new project id
$clone_project_id=$clone_project->id; $clone_project_id=$clone_project->id;
@ -1223,23 +1223,19 @@ class Project extends CommonObject
} }
} }
} }
}
unset($clone_project->context['createfromclone']);
if (! $error)
if (! $error) {
{ $this->db->commit();
return $clone_project_id; return $clone_project_id;
}
else
{
dol_syslog(get_class($this)."::createFromClone nbError: ".$error." error : " . $this->error, LOG_ERR);
return -1;
}
} }
else else
{ {
$this->db->rollback(); $this->db->rollback();
dol_syslog(get_class($this)."::createFromClone nbError: ".$error." error : " . $this->error, LOG_ERR);
return -1; return -1;
} }
} }

View File

@ -1092,6 +1092,8 @@ class Task extends CommonObject
$clone_task=new Task($this->db); $clone_task=new Task($this->db);
$origin_task=new Task($this->db); $origin_task=new Task($this->db);
$clone_task->context['createfromclone']='createfromclone';
$this->db->begin(); $this->db->begin();
// Load source object // Load source object
@ -1158,8 +1160,6 @@ class Task extends CommonObject
// End // End
if (! $error) if (! $error)
{ {
$this->db->commit();
$clone_task_id=$clone_task->id; $clone_task_id=$clone_task->id;
$clone_task_ref = $clone_task->ref; $clone_task_ref = $clone_task->ref;
@ -1279,20 +1279,19 @@ class Task extends CommonObject
{ {
//TODO clone time of affectation //TODO clone time of affectation
} }
}
if (! $error) unset($clone_task->context['createfromclone']);
{
return $clone_task_id; if (! $error)
} {
else $this->db->commit();
{ return $clone_task_id;
dol_syslog(get_class($this)."::createFromClone nbError: ".$error." error : " . $this->error, LOG_ERR);
return -1;
}
} }
else else
{ {
$this->db->rollback(); $this->db->rollback();
dol_syslog(get_class($this)."::createFromClone nbError: ".$error." error : " . $this->error, LOG_ERR);
return -1; return -1;
} }
} }