From 80d3c68c073cf5d0b306b5e50c455a6775fb0037 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Fri, 8 Jul 2016 18:30:50 +0200 Subject: [PATCH 1/4] NEW : Add ORDER_MODIFY trigger on each order modification --- htdocs/commande/class/commande.class.php | 440 +++++++++++++++++------ 1 file changed, 331 insertions(+), 109 deletions(-) diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index 6f14d407a5d..08fe1f19861 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -148,7 +148,7 @@ class Commande extends CommonOrder var $multicurrency_total_ht; var $multicurrency_total_tva; var $multicurrency_total_ttc; - + /** * ERR Not enough stock */ @@ -450,7 +450,7 @@ class Commande extends CommonOrder if (! empty($conf->stock->enabled) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) { $result = 0; - + require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php'; $langs->load("agenda"); @@ -472,7 +472,7 @@ class Commande extends CommonOrder $result=$this->call_trigger('ORDER_SETDRAFT',$user); if ($result < 0) $error++; } - + if (!$error) { $this->statut=self::STATUS_DRAFT; $this->db->commit(); @@ -701,7 +701,7 @@ class Commande extends CommonOrder // Clean parameters $this->brouillon = 1; // set command as draft - + // Multicurrency (test on $this->multicurrency_tx because we sould take the default rate only if not using origin rate) if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) list($this->fk_multicurrency,$this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code); else $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code); @@ -711,7 +711,7 @@ class Commande extends CommonOrder $this->fk_multicurrency = 0; $this->multicurrency_tx = 1; } - + dol_syslog(get_class($this)."::create user=".$user->id); // Check parameters @@ -1231,7 +1231,7 @@ class Commande extends CommonOrder if (empty($txlocaltax2)) $txlocaltax2=0; if (empty($fk_parent_line) || $fk_parent_line < 0) $fk_parent_line=0; if (empty($this->fk_multicurrency)) $this->fk_multicurrency=0; - + $remise_percent=price2num($remise_percent); $qty=price2num($qty); $pu_ht=price2num($pu_ht); @@ -1281,7 +1281,7 @@ class Commande extends CommonOrder $localtaxes_type=getLocalTaxesFromRate($txtva,0,$this->thirdparty,$mysoc); $txtva = preg_replace('/\s*\(.*\)/','',$txtva); // Remove code into vatrate. - + $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx); $total_ht = $tabprice[0]; @@ -1294,7 +1294,7 @@ class Commande extends CommonOrder $multicurrency_total_ht = $tabprice[16]; $multicurrency_total_tva = $tabprice[17]; $multicurrency_total_ttc = $tabprice[18]; - + // Rang to use $rangtouse = $rang; if ($rangtouse == -1) @@ -1596,7 +1596,7 @@ class Commande extends CommonOrder $this->multicurrency_total_ht = $obj->multicurrency_total_ht; $this->multicurrency_total_tva = $obj->multicurrency_total_tva; $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc; - + $this->extraparams = (array) json_decode($obj->extraparams, true); $this->lines = array(); @@ -1787,7 +1787,7 @@ class Commande extends CommonOrder $line->product_desc = $objp->product_desc; $line->fk_product_type = $objp->fk_product_type; // Produit ou service $line->fk_unit = $objp->fk_unit; - + $line->weight = $objp->weight; $line->weight_units = $objp->weight_units; $line->volume = $objp->volume; @@ -1803,7 +1803,7 @@ class Commande extends CommonOrder $line->multicurrency_total_ht = $objp->multicurrency_total_ht; $line->multicurrency_total_tva = $objp->multicurrency_total_tva; $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc; - + $this->lines[$i] = $line; $i++; @@ -1854,7 +1854,7 @@ class Commande extends CommonOrder /** * Load array this->expeditions of lines of shipments with nb of products sent for each order line * Note: For a dedicated shipment, the fetch_lines can be used to load the qty_asked and qty_shipped. This function is use to return qty_shipped cumulated for the order - * + * * @param int $filtre_statut Filter on shipment status * @return int <0 if KO, Nb of lines found if OK */ @@ -2044,30 +2044,57 @@ class Commande extends CommonOrder * * @param User $user User qui positionne la remise * @param float $remise Discount (percent) + * @param int $notrigger Not Trigger * @return int <0 if KO, >0 if OK */ - function set_remise($user, $remise) + function set_remise($user, $remise, $notrigger=0) { $remise=trim($remise)?trim($remise):0; if ($user->rights->commande->creer) { + $error=0; + + $this->db->begin(); + $remise=price2num($remise); $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande'; $sql.= ' SET remise_percent = '.$remise; $sql.= ' WHERE rowid = '.$this->id.' AND fk_statut = '.self::STATUS_DRAFT.' ;'; - if ($this->db->query($sql)) + $resql=$this->db->query($sql); + if (!$resql) { - $this->remise_percent = $remise; - $this->update_price(1); - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('ORDER_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->remise_percent = $remise; + $this->update_price(1); + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->error(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } } @@ -2078,32 +2105,59 @@ class Commande extends CommonOrder * * @param User $user User qui positionne la remise * @param float $remise Discount + * @param int $notrigger Not Trigger * @return int <0 if KO, >0 if OK */ - function set_remise_absolue($user, $remise) + function set_remise_absolue($user, $remise, $notrigger=0) { $remise=trim($remise)?trim($remise):0; if ($user->rights->commande->creer) { + $error=0; + + $this->db->begin(); + $remise=price2num($remise); $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande'; $sql.= ' SET remise_absolue = '.$remise; $sql.= ' WHERE rowid = '.$this->id.' AND fk_statut = '.self::STATUS_DRAFT.' ;'; - dol_syslog(get_class($this)."::set_remise_absolue", LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); - if ($this->db->query($sql)) + $resql=$this->db->query($sql); + if (!$resql) { - $this->remise_absolue = $remise; - $this->update_price(1); - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('ORDER_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->remise_absolue = $remise; + $this->update_price(1); + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->error(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } } @@ -2112,29 +2166,56 @@ class Commande extends CommonOrder /** * Set the order date * - * @param User $user Object user making change - * @param int $date Date - * @return int <0 if KO, >0 if OK + * @param User $user Object user making change + * @param int $date Date + * @param int $notrigger Not Trigger + * @return int <0 if KO, >0 if OK */ - function set_date($user, $date) + function set_date($user, $date, $notrigger=0) { if ($user->rights->commande->creer) { + + $error=0; + + $this->db->begin(); + $sql = "UPDATE ".MAIN_DB_PREFIX."commande"; $sql.= " SET date_commande = ".($date ? $this->db->idate($date) : 'null'); $sql.= " WHERE rowid = ".$this->id." AND fk_statut = ".self::STATUS_DRAFT; - dol_syslog(get_class($this)."::set_date",LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); $resql=$this->db->query($sql); - if ($resql) + if (!$resql) { - $this->date = $date; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('ORDER_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->date = $date; + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->error(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } else @@ -2146,11 +2227,12 @@ class Commande extends CommonOrder /** * Set the planned delivery date * - * @param User $user Objet utilisateur qui modifie + * @param User $user Objet utilisateur qui modifie * @param int $date_livraison Date de livraison - * @return int <0 si ko, >0 si ok + * @param int $notrigger Not Trigger + * @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) { @@ -2158,17 +2240,38 @@ class Commande extends CommonOrder $sql.= " SET date_livraison = ".($date_livraison ? "'".$this->db->idate($date_livraison)."'" : 'null'); $sql.= " WHERE rowid = ".$this->id; - dol_syslog(get_class($this)."::set_date_livraison", LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); $resql=$this->db->query($sql); - if ($resql) + if (!$resql) { - $this->date_livraison = $date_livraison; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('ORDER_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->date_livraison = $date_livraison; + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->error(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } else @@ -2182,9 +2285,10 @@ class Commande extends CommonOrder * * @param User $user Object user making change * @param int $id If of availability delay + * @param int $notrigger Not Trigger * @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) { @@ -2192,16 +2296,38 @@ class Commande extends CommonOrder $sql.= " SET fk_availability = '".$id."'"; $sql.= " WHERE rowid = ".$this->id; - if ($this->db->query($sql)) + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) { - $this->fk_availability = $id; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('ORDER_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->fk_availability = $id; + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->error(); - dol_syslog(get_class($this)."::set_availability Erreur SQL"); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } } @@ -2209,11 +2335,12 @@ class Commande extends CommonOrder /** * Set source of demand * - * @param User $user Object user making change - * @param int $id Id of source - * @return int <0 if KO, >0 if OK + * @param User $user Object user making change + * @param int $id Id of source + * @param int $notrigger Not Trigger + * @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) { @@ -2221,16 +2348,38 @@ class Commande extends CommonOrder $sql.= " SET fk_input_reason = '".$id."'"; $sql.= " WHERE rowid = ".$this->id; - if ($this->db->query($sql)) + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) { - $this->fk_input_reason = $id; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('ORDER_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->fk_input_reason = $id; + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->error(); - dol_syslog(get_class($this)."::set_demand_reason Erreur SQL"); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } } @@ -2309,12 +2458,13 @@ class Commande extends CommonOrder } /** - * Change le delai de livraison + * Update delivery delay * * @param int $availability_id Id du nouveau mode + * @param int $notrigger Not Trigger * @return int >0 if OK, <0 if KO */ - function availability($availability_id) + function availability($availability_id, $notrigger=0) { dol_syslog('Commande::availability('.$availability_id.')'); if ($this->statut >= self::STATUS_DRAFT) @@ -2322,33 +2472,59 @@ class Commande extends CommonOrder $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande'; $sql .= ' SET fk_availability = '.$availability_id; $sql .= ' WHERE rowid='.$this->id; - if ( $this->db->query($sql) ) + + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) { - $this->availability_id = $availability_id; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('ORDER_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->availability_id = $availability_id; + + $this->db->commit(); + return 1; } else { - dol_syslog('Commande::availability Erreur '.$sql.' - '.$this->db->error(), LOG_ERR); - $this->error=$this->db->lasterror(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } else { - dol_syslog('Commande::availability, etat facture incompatible', LOG_ERR); - $this->error='Etat commande incompatible '.$this->statut; + $error_str='Command status do not meet requirement '.$this->statut; + dol_syslog(__METHOD__.$error_str, LOG_ERR); + $this->error=$error_str; + $this->errors[]= $this->error; return -2; } } /** - * Change la source de la demande + * Update order demand_reason * * @param int $demand_reason_id Id of new demand + * @param int $notrigger Not Trigger * @return int >0 if ok, <0 if ko */ - function demand_reason($demand_reason_id) + function demand_reason($demand_reason_id, $notrigger=0) { dol_syslog('Commande::demand_reason('.$demand_reason_id.')'); if ($this->statut >= self::STATUS_DRAFT) @@ -2356,22 +2532,47 @@ class Commande extends CommonOrder $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande'; $sql .= ' SET fk_input_reason = '.$demand_reason_id; $sql .= ' WHERE rowid='.$this->id; - if ( $this->db->query($sql) ) + + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) { - $this->demand_reason_id = $demand_reason_id; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('ORDER_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->demand_reason_id = $demand_reason_id; + + $this->db->commit(); + return 1; } else { - dol_syslog('Commande::demand_reason Erreur '.$sql.' - '.$this->db->error(), LOG_ERR); - $this->error=$this->db->lasterror(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } else { - dol_syslog('Commande::demand_reason, etat facture incompatible', LOG_ERR); - $this->error='Etat commande incompatible '.$this->statut; + $error_str='order status do not meet requirement '.$this->statut; + dol_syslog(__METHOD__.$error_str, LOG_ERR); + $this->error=$error_str; + $this->errors[]= $this->error; return -2; } } @@ -2387,21 +2588,42 @@ class Commande extends CommonOrder { if ($user->rights->commande->creer) { - dol_syslog(get_class($this).'::set_ref_client this->id='.$this->id.', ref_client='.$ref_client); - $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande SET'; $sql.= ' ref_client = '.(empty($ref_client) ? 'NULL' : '\''.$this->db->escape($ref_client).'\''); $sql.= ' WHERE rowid = '.$this->id; - if ($this->db->query($sql) ) + dol_syslog(__METHOD__.' this->id='.$this->id.', ref_client='.$ref_client, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) { - $this->ref_client = $ref_client; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('ORDER_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->ref_client = $ref_client; + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->lasterror(); - return -2; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } else @@ -2485,12 +2707,12 @@ class Commande extends CommonOrder { global $conf, $user, $langs; $error = 0; - + $this->db->begin(); - + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande SET facture = 0'; $sql.= ' WHERE rowid = '.$this->id.' AND fk_statut > '.self::STATUS_DRAFT; - + dol_syslog(get_class($this)."::classifyUnBilled", LOG_DEBUG); if ($this->db->query($sql)) { @@ -2498,12 +2720,12 @@ class Commande extends CommonOrder $result=$this->call_trigger('ORDER_CLASSIFY_UNBILLED',$user); if ($result < 0) $error++; // End call triggers - + if (! $error) { $this->facturee=0; // deprecated $this->billed=0; - + $this->db->commit(); return 1; } @@ -2525,8 +2747,8 @@ class Commande extends CommonOrder return -1; } } - - + + /** * Update a line in database * @@ -2589,7 +2811,7 @@ class Commande extends CommonOrder $localtaxes_type=getLocalTaxesFromRate($txtva,0,$this->thirdparty, $mysoc); $txtva = preg_replace('/\s*\(.*\)/','',$txtva); // Remove code into vatrate. - + $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx); $total_ht = $tabprice[0]; @@ -2602,14 +2824,14 @@ class Commande extends CommonOrder $multicurrency_total_ht = $tabprice[16]; $multicurrency_total_tva = $tabprice[17]; $multicurrency_total_ttc = $tabprice[18]; - + // Anciens indicateurs: $price, $subprice, $remise (a ne plus utiliser) $price = $pu; - if ($price_base_type == 'TTC') + if ($price_base_type == 'TTC') { $subprice = $tabprice[5]; - } - else + } + else { $subprice = $pu; } @@ -2691,7 +2913,7 @@ class Commande extends CommonOrder $this->line->multicurrency_total_ht = $multicurrency_total_ht; $this->line->multicurrency_total_tva = $multicurrency_total_tva; $this->line->multicurrency_total_ttc = $multicurrency_total_ttc; - + // TODO deprecated $this->line->price=$price; $this->line->remise=$remise; @@ -3310,7 +3532,7 @@ class Commande extends CommonOrder $prodid = mt_rand(1, $num_prods); $line->fk_product=$prodids[$prodid]; } - + $this->lines[$xnbp]=$line; $this->total_ht += $line->total_ht; @@ -3442,7 +3664,7 @@ class Commande extends CommonOrder return max($this->date_commande, $this->date_livraison) < ($now - $conf->commande->client->warning_delay); } - + /** * Show the customer delayed info * @@ -3451,11 +3673,11 @@ class Commande extends CommonOrder public function showDelay() { global $conf, $langs; - + if (empty($this->date_livraison)) $text=$langs->trans("OrderDate").' '.dol_print_date($this->date_commande, 'day'); else $text=$text=$langs->trans("DeliveryDate").' '.dol_print_date($this->date_livraison, 'day'); $text.=' '.($conf->commande->client->warning_delay>0?'+':'-').' '.round(abs($conf->commande->client->warning_delay)/3600/24,1).' '.$langs->trans("days").' < '.$langs->trans("Today"); - + return $text; } } @@ -3884,13 +4106,13 @@ class OrderLine extends CommonOrderLine $sql.= " , fk_parent_line=".(! empty($this->fk_parent_line)?$this->fk_parent_line:"null"); if (! empty($this->rang)) $sql.= ", rang=".$this->rang; $sql.= " , fk_unit=".(!$this->fk_unit ? 'NULL' : $this->fk_unit); - + // Multicurrency $sql.= " , multicurrency_subprice=".price2num($this->multicurrency_subprice).""; $sql.= " , multicurrency_total_ht=".price2num($this->multicurrency_total_ht).""; $sql.= " , multicurrency_total_tva=".price2num($this->multicurrency_total_tva).""; $sql.= " , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc).""; - + $sql.= " WHERE rowid = ".$this->rowid; dol_syslog(get_class($this)."::update", LOG_DEBUG); From 195d6187a3fc8da99cf4fa1313879b3816e8d645 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Fri, 8 Jul 2016 20:40:25 +0200 Subject: [PATCH 2/4] NEW : Uniformize code and correct deal with triggers --- htdocs/comm/propal/class/propal.class.php | 611 ++++++++++++++---- htdocs/commande/class/commande.class.php | 49 +- htdocs/compta/facture/class/facture.class.php | 157 ++++- 3 files changed, 645 insertions(+), 172 deletions(-) diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index ffc412fba70..c6e554957b1 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -164,7 +164,7 @@ class Propal extends CommonObject var $multicurrency_total_ht; var $multicurrency_total_tva; var $multicurrency_total_ttc; - + /** * Draft status */ @@ -273,7 +273,7 @@ class Propal extends CommonObject $line->tva_tx=$tva_tx; $line->fk_unit=$prod->fk_unit; if ($tva_npr) $line->info_bits = 1; - + $this->lines[]=$line; } } @@ -444,7 +444,7 @@ class Propal extends CommonObject return -3; } } - + // Calcul du total TTC et de la TVA pour la ligne a partir de // qty, pu, remise_percent et txtva // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker @@ -452,7 +452,7 @@ class Propal extends CommonObject $localtaxes_type=getLocalTaxesFromRate($txtva,0,$this->thirdparty,$mysoc); $txtva = preg_replace('/\s*\(.*\)/','',$txtva); // Remove code into vatrate. - + $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx); $total_ht = $tabprice[0]; @@ -529,7 +529,7 @@ class Propal extends CommonObject $this->line->multicurrency_total_ht = $multicurrency_total_ht; $this->line->multicurrency_total_tva = $multicurrency_total_tva; $this->line->multicurrency_total_ttc = $multicurrency_total_ttc; - + // Mise en option de la ligne if (empty($qty) && empty($special_code)) $this->line->special_code=3; @@ -627,7 +627,7 @@ class Propal extends CommonObject $localtaxes_type=getLocalTaxesFromRate($txtva,0,$this->thirdparty,$mysoc); $txtva = preg_replace('/\s*\(.*\)/','',$txtva); // Remove code into vatrate. - + $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx); $total_ht = $tabprice[0]; $total_tva = $tabprice[1]; @@ -639,7 +639,7 @@ class Propal extends CommonObject $multicurrency_total_ht = $tabprice[16]; $multicurrency_total_tva = $tabprice[17]; $multicurrency_total_ttc = $tabprice[18]; - + // Anciens indicateurs: $price, $remise (a ne plus utiliser) $price = $pu; if ($remise_percent > 0) @@ -800,7 +800,7 @@ class Propal extends CommonObject $this->fk_multicurrency = 0; $this->multicurrency_tx = 1; } - + dol_syslog(get_class($this)."::create"); // Check parameters @@ -1110,7 +1110,7 @@ class Propal extends CommonObject $clonedObj->cond_reglement_id = (! empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0); $clonedObj->mode_reglement_id = (! empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0); $clonedObj->fk_delivery_address = ''; - + /*if (!empty($conf->projet->enabled)) { $project = new Project($db); @@ -1610,11 +1610,12 @@ class Propal extends CommonObject /** * Define proposal date * - * @param User $user Object user that modify - * @param int $date Date - * @return int <0 if KO, >0 if OK + * @param User $user Object user that modify + * @param int $date Date + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers + * @return int <0 if KO, >0 if OK */ - function set_date($user, $date) + function set_date($user, $date, $notrigger=0) { if (empty($date)) { @@ -1625,20 +1626,46 @@ class Propal extends CommonObject if (! empty($user->rights->propal->creer)) { + $error=0; + + $this->db->begin(); + $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET datep = '".$this->db->idate($date)."'"; $sql.= " WHERE rowid = ".$this->id." AND fk_statut = ".self::STATUS_DRAFT; - dol_syslog(get_class($this)."::set_date", LOG_DEBUG); - if ($this->db->query($sql) ) + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) { - $this->date = $date; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('PROPAL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->date = $date; $this->datep = $date; // deprecated - return 1; + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->lasterror(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } } @@ -1646,25 +1673,54 @@ class Propal extends CommonObject /** * Define end validity date * - * @param User $user Object user that modify - * @param int $date_fin_validite End of validity date - * @return int <0 if KO, >0 if OK + * @param User $user Object user that modify + * @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 */ - function set_echeance($user, $date_fin_validite) + function set_echeance($user, $date_fin_validite, $notrigger=0) { 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.= " 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->fin_validite = $date_fin_validite; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('PROPAL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->fin_validite = $date_fin_validite; + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->error(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } } @@ -1672,28 +1728,55 @@ class Propal extends CommonObject /** * Set delivery date * - * @param User $user Object user that modify - * @param int $date_livraison Delivery date - * @return int <0 if ko, >0 if ok + * @param User $user Object user that modify + * @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 */ - function set_date_livraison($user, $date_livraison) + function set_date_livraison($user, $date_livraison, $notrigger=0) { if (! empty($user->rights->propal->creer)) { + $error=0; + + $this->db->begin(); + $sql = "UPDATE ".MAIN_DB_PREFIX."propal "; $sql.= " SET date_livraison = ".($date_livraison!=''?"'".$this->db->idate($date_livraison)."'":'null'); $sql.= " WHERE rowid = ".$this->id; - if ($this->db->query($sql)) + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) { - $this->date_livraison = $date_livraison; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('PROPAL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->date_livraison = $date_livraison; + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->error(); - dol_syslog(get_class($this)."::set_date_livraison Erreur SQL"); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } } @@ -1703,26 +1786,53 @@ class Propal extends CommonObject * * @param User $user Object user that modify * @param int $id Availability id + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int <0 if KO, >0 if OK */ - function set_availability($user, $id) + function set_availability($user, $id, $notrigger=0) { if (! empty($user->rights->propal->creer)) { + $error=0; + + $this->db->begin(); + $sql = "UPDATE ".MAIN_DB_PREFIX."propal "; $sql.= " SET fk_availability = '".$id."'"; $sql.= " WHERE rowid = ".$this->id; - if ($this->db->query($sql)) + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) { - $this->fk_availability = $id; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('PROPAL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->fk_availability = $id; + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->error(); - dol_syslog(get_class($this)."::set_availability Erreur SQL"); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } } @@ -1732,26 +1842,53 @@ class Propal extends CommonObject * * @param User $user Object user that modify * @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 */ - function set_demand_reason($user, $id) + function set_demand_reason($user, $id, $notrigger=0) { if (! empty($user->rights->propal->creer)) { + $error=0; + + $this->db->begin(); + $sql = "UPDATE ".MAIN_DB_PREFIX."propal "; $sql.= " SET fk_input_reason = '".$id."'"; $sql.= " WHERE rowid = ".$this->id; - if ($this->db->query($sql)) + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) { - $this->fk_input_reason = $id; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('PROPAL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->fk_input_reason = $id; + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->error(); - dol_syslog(get_class($this)."::set_demand_reason Erreur SQL"); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } } @@ -1761,26 +1898,52 @@ class Propal extends CommonObject * * @param User $user Object user that modify * @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 */ - function set_ref_client($user, $ref_client) + function set_ref_client($user, $ref_client, $notrigger=0) { 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.= ' WHERE rowid = '.$this->id; - if ($this->db->query($sql) ) + + dol_syslog(__METHOD__.' $this->id='.$this->id.', ref_client='.$ref_client, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) { - $this->ref_client = $ref_client; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('PROPAL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->ref_client = $ref_client; + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->error(); - dol_syslog('Propale::set_ref_client Erreur '.$this->error.' - '.$sql); - return -2; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } else @@ -1793,10 +1956,11 @@ class Propal extends CommonObject * Set an overall discount on the proposal * * @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 */ - function set_remise_percent($user, $remise) + function set_remise_percent($user, $remise, $notrigger=0) { $remise=trim($remise)?trim($remise):0; @@ -1804,19 +1968,46 @@ class Propal extends CommonObject { $remise = price2num($remise); + $error=0; + + $this->db->begin(); + $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET remise_percent = ".$remise; $sql.= " WHERE rowid = ".$this->id." AND fk_statut = ".self::STATUS_DRAFT; - if ($this->db->query($sql) ) + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) { - $this->remise_percent = $remise; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('PROPAL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->remise_percent = $remise; $this->update_price(1); - return 1; + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->error(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } } @@ -1825,11 +2016,12 @@ class Propal extends CommonObject /** * Set an absolute overall discount on the proposal * - * @param User $user Object user that modify - * @param double $remise Amount discount + * @param User $user Object user that modify + * @param double $remise Amount discount + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int <0 if ko, >0 if ok */ - function set_remise_absolue($user, $remise) + function set_remise_absolue($user, $remise, $notrigger=0) { $remise=trim($remise)?trim($remise):0; @@ -1837,20 +2029,47 @@ class Propal extends CommonObject { $remise = price2num($remise); + $error=0; + + $this->db->begin(); + $sql = "UPDATE ".MAIN_DB_PREFIX."propal "; $sql.= " SET remise_absolue = ".$remise; $sql.= " WHERE rowid = ".$this->id." AND fk_statut = ".self::STATUS_DRAFT; - if ($this->db->query($sql) ) + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) { - $this->remise_absolue = $remise; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('PROPAL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->remise_absolue = $remise; $this->update_price(1); - return 1; + + $this->db->commit(); + return 1; } else { - $this->error=$this->db->error(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } } @@ -1924,9 +2143,10 @@ class Propal extends CommonObject * @param User $user Object user that close * @param int $statut Statut * @param string $note Comment + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int <0 if KO, >0 if OK */ - function cloture($user, $statut, $note) + function cloture($user, $statut, $note, $notrigger=0) { global $langs,$conf; @@ -1981,10 +2201,13 @@ class Propal extends CommonObject $this->generateDocument($modelpdf, $outputlangs); } - // Call trigger - $result=$this->call_trigger($trigger_name,$user); - if ($result < 0) { $error++; } - // End call triggers + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger($trigger_name,$user); + if ($result < 0) { $error++; } + // End call triggers + } if ( ! $error ) { @@ -2010,20 +2233,51 @@ class Propal extends CommonObject /** * Class invoiced the Propal * - * @return int <0 si ko, >0 si ok + * @param User $user Object user + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers + * @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 .= ' 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 (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('PROPAL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) { $this->statut=self::STATUS_BILLED; - return 1; + + $this->db->commit(); + return 1; } 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; } } @@ -2032,7 +2286,7 @@ class Propal extends CommonObject * * @return int <0 si ko, >0 si ok * @deprecated - * @see classifyBilled() + * @see classifyBilled() */ function classer_facturee() { @@ -2046,22 +2300,51 @@ class Propal extends CommonObject * Set draft status * * @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 */ - 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.= " WHERE rowid = ".$this->id; - if ($this->db->query($sql)) + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) { - $this->statut = self::STATUS_DRAFT; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('PROPAL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->statut = self::STATUS_DRAFT; $this->brouillon = 1; - return 1; + + $this->db->commit(); + return 1; } 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 +2647,61 @@ class Propal extends CommonObject * Change the 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 */ - function availability($availability_id) + function availability($availability_id, $notrigger=0) { - dol_syslog('Propale::availability('.$availability_id.')'); if ($this->statut >= self::STATUS_DRAFT) { + $error=0; + + $this->db->begin(); + $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal'; $sql .= ' SET fk_availability = '.$availability_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->availability_id = $availability_id; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('PROPAL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->availability_id = $availability_id; + + $this->db->commit(); + return 1; } else { - dol_syslog('Propale::availability Erreur '.$sql.' - '.$this->db->error()); - $this->error=$this->db->error(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } else { - dol_syslog('Propale::availability, etat propale incompatible'); - $this->error='Etat propale incompatible '.$this->statut; + $error_str='Propal status do not meet requirement '.$this->statut; + dol_syslog(__METHOD__.$error_str, LOG_ERR); + $this->error=$error_str; + $this->errors[]= $this->error; return -2; } } @@ -2398,32 +2710,61 @@ class Propal extends CommonObject * Change 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 */ - 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) { + $error=0; + + $this->db->begin(); + $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal'; $sql .= ' SET fk_input_reason = '.$demand_reason_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->demand_reason_id = $demand_reason_id; - return 1; + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('PROPAL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { + $this->demand_reason_id = $demand_reason_id; + + $this->db->commit(); + return 1; } else { - dol_syslog('Propale::demand_reason Erreur '.$sql.' - '.$this->db->error()); - $this->error=$this->db->error(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } else { - dol_syslog('Propale::demand_reason, etat propale incompatible'); - $this->error='Etat propale incompatible '.$this->statut; + $error_str='Propal status do not meet requirement '.$this->statut; + dol_syslog(__METHOD__.$error_str, LOG_ERR); + $this->error=$error_str; + $this->errors[]= $this->error; return -2; } } @@ -2507,21 +2848,21 @@ class Propal extends CommonObject */ function LibStatut($statut,$mode=1) { - global $langs; - $langs->load("propal"); + global $langs; + $langs->load("propal"); - if ($statut==self::STATUS_DRAFT) $statuttrans='statut0'; - if ($statut==self::STATUS_VALIDATED) $statuttrans='statut1'; - if ($statut==self::STATUS_SIGNED) $statuttrans='statut3'; - if ($statut==self::STATUS_NOTSIGNED) $statuttrans='statut5'; - if ($statut==self::STATUS_BILLED) $statuttrans='statut6'; + if ($statut==self::STATUS_DRAFT) $statuttrans='statut0'; + if ($statut==self::STATUS_VALIDATED) $statuttrans='statut1'; + if ($statut==self::STATUS_SIGNED) $statuttrans='statut3'; + if ($statut==self::STATUS_NOTSIGNED) $statuttrans='statut5'; + if ($statut==self::STATUS_BILLED) $statuttrans='statut6'; - if ($mode == 0) return $this->labelstatut[$statut]; - if ($mode == 1) return $this->labelstatut_short[$statut]; - if ($mode == 2) return img_picto($this->labelstatut_short[$statut], $statuttrans).' '.$this->labelstatut_short[$statut]; - if ($mode == 3) return img_picto($this->labelstatut[$statut], $statuttrans); - if ($mode == 4) return img_picto($this->labelstatut[$statut],$statuttrans).' '.$this->labelstatut[$statut]; - if ($mode == 5) return ''.$this->labelstatut_short[$statut].' '.img_picto($this->labelstatut_short[$statut],$statuttrans); + if ($mode == 0) return $this->labelstatut[$statut]; + if ($mode == 1) return $this->labelstatut_short[$statut]; + if ($mode == 2) return img_picto($this->labelstatut_short[$statut], $statuttrans).' '.$this->labelstatut_short[$statut]; + if ($mode == 3) return img_picto($this->labelstatut[$statut], $statuttrans); + if ($mode == 4) return img_picto($this->labelstatut[$statut],$statuttrans).' '.$this->labelstatut[$statut]; + if ($mode == 5) return ''.$this->labelstatut_short[$statut].' '.img_picto($this->labelstatut_short[$statut],$statuttrans); } @@ -2681,7 +3022,7 @@ class Propal extends CommonObject $prodid = mt_rand(1, $num_prods); $line->fk_product=$prodids[$prodid]; } - + $this->lines[$xnbp]=$line; $this->total_ht += $line->total_ht; @@ -2851,7 +3192,7 @@ class Propal extends CommonObject function getLinesArray() { // For other object, here we call fetch_lines. But fetch_lines does not exists on proposal - + $sql = 'SELECT pt.rowid, pt.label as custom_label, pt.description, pt.fk_product, pt.fk_remise_except,'; $sql.= ' pt.qty, pt.tva_tx, pt.remise_percent, pt.subprice, pt.info_bits,'; $sql.= ' pt.total_ht, pt.total_tva, pt.total_ttc, pt.fk_product_fournisseur_price as fk_fournprice, pt.buy_price_ht as pa_ht, pt.special_code, pt.localtax1_tx, pt.localtax2_tx,'; @@ -2911,7 +3252,7 @@ class Propal extends CommonObject $this->lines[$i]->date_start = $this->db->jdate($obj->date_start); $this->lines[$i]->date_end = $this->db->jdate($obj->date_end); $this->lines[$i]->fk_unit = $obj->fk_unit; - + // Multicurrency $this->lines[$i]->fk_multicurrency = $obj->fk_multicurrency; $this->lines[$i]->multicurrency_code = $obj->multicurrency_code; @@ -3096,7 +3437,7 @@ class PropaleLigne extends CommonObjectLine var $multicurrency_total_ht; var $multicurrency_total_tva; var $multicurrency_total_ttc; - + /** * Class line Contructor * @@ -3180,7 +3521,7 @@ class PropaleLigne extends CommonObjectLine $this->multicurrency_total_ht = $objp->multicurrency_total_ht; $this->multicurrency_total_tva = $objp->multicurrency_total_tva; $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc; - + $this->db->free($result); return 1; @@ -3206,7 +3547,7 @@ class PropaleLigne extends CommonObjectLine dol_syslog(get_class($this)."::insert rang=".$this->rang); $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht == ''); // If true, we can use a default value. If this->pa_ht = '0', we must use '0'. - + // Clean parameters if (empty($this->tva_tx)) $this->tva_tx=0; if (empty($this->localtax1_tx)) $this->localtax1_tx=0; @@ -3226,7 +3567,7 @@ class PropaleLigne extends CommonObjectLine if (empty($this->pa_ht)) $this->pa_ht=0; // if buy price not defined, define buyprice as configured in margin admin - if ($this->pa_ht == 0 && $pa_ht_isemptystring) + if ($this->pa_ht == 0 && $pa_ht_isemptystring) { if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) { @@ -3391,7 +3732,7 @@ class PropaleLigne extends CommonObjectLine $error=0; $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht == ''); // If true, we can use a default value. If this->pa_ht = '0', we must use '0'. - + // Clean parameters if (empty($this->tva_tx)) $this->tva_tx=0; if (empty($this->localtax1_tx)) $this->localtax1_tx=0; @@ -3413,7 +3754,7 @@ class PropaleLigne extends CommonObjectLine if (empty($this->pa_ht)) $this->pa_ht=0; // if buy price not defined, define buyprice as configured in margin admin - if ($this->pa_ht == 0 && $pa_ht_isemptystring) + if ($this->pa_ht == 0 && $pa_ht_isemptystring) { if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) { @@ -3459,13 +3800,13 @@ class PropaleLigne extends CommonObjectLine $sql.= " , date_start=".(! empty($this->date_start)?"'".$this->db->idate($this->date_start)."'":"null"); $sql.= " , date_end=".(! empty($this->date_end)?"'".$this->db->idate($this->date_end)."'":"null"); $sql.= " , fk_unit=".(!$this->fk_unit ? 'NULL' : $this->fk_unit); - + // Multicurrency $sql.= " , multicurrency_subprice=".price2num($this->multicurrency_subprice).""; $sql.= " , multicurrency_total_ht=".price2num($this->multicurrency_total_ht).""; $sql.= " , multicurrency_total_tva=".price2num($this->multicurrency_total_tva).""; $sql.= " , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc).""; - + $sql.= " WHERE rowid = ".$this->rowid; dol_syslog(get_class($this)."::update", LOG_DEBUG); diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index 08fe1f19861..b762ed4de24 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -469,7 +469,7 @@ class Commande extends CommonOrder if (!$error) { // Call trigger - $result=$this->call_trigger('ORDER_SETDRAFT',$user); + $result=$this->call_trigger('ORDER_UNVALIDATE',$user); if ($result < 0) $error++; } @@ -2044,7 +2044,7 @@ class Commande extends CommonOrder * * @param User $user User qui positionne la remise * @param float $remise Discount (percent) - * @param int $notrigger Not Trigger + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int <0 if KO, >0 if OK */ function set_remise($user, $remise, $notrigger=0) @@ -2063,6 +2063,7 @@ class Commande extends CommonOrder $sql.= ' SET remise_percent = '.$remise; $sql.= ' WHERE rowid = '.$this->id.' AND fk_statut = '.self::STATUS_DRAFT.' ;'; + dol_syslog(__METHOD__, LOG_DEBUG); $resql=$this->db->query($sql); if (!$resql) { @@ -2105,7 +2106,7 @@ class Commande extends CommonOrder * * @param User $user User qui positionne la remise * @param float $remise Discount - * @param int $notrigger Not Trigger + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int <0 if KO, >0 if OK */ function set_remise_absolue($user, $remise, $notrigger=0) @@ -2125,7 +2126,6 @@ class Commande extends CommonOrder $sql.= ' WHERE rowid = '.$this->id.' AND fk_statut = '.self::STATUS_DRAFT.' ;'; dol_syslog(__METHOD__, LOG_DEBUG); - $resql=$this->db->query($sql); if (!$resql) { @@ -2168,14 +2168,13 @@ class Commande extends CommonOrder * * @param User $user Object user making change * @param int $date Date - * @param int $notrigger Not Trigger + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int <0 if KO, >0 if OK */ function set_date($user, $date, $notrigger=0) { if ($user->rights->commande->creer) { - $error=0; $this->db->begin(); @@ -2229,13 +2228,17 @@ class Commande extends CommonOrder * * @param User $user Objet utilisateur qui modifie * @param int $date_livraison Date de livraison - * @param int $notrigger Not Trigger + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int <0 si ko, >0 si ok */ function set_date_livraison($user, $date_livraison, $notrigger=0) { if ($user->rights->commande->creer) { + $error=0; + + $this->db->begin(); + $sql = "UPDATE ".MAIN_DB_PREFIX."commande"; $sql.= " SET date_livraison = ".($date_livraison ? "'".$this->db->idate($date_livraison)."'" : 'null'); $sql.= " WHERE rowid = ".$this->id; @@ -2285,13 +2288,17 @@ class Commande extends CommonOrder * * @param User $user Object user making change * @param int $id If of availability delay - * @param int $notrigger Not Trigger + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int <0 if KO, >0 if OK */ function set_availability($user, $id, $notrigger=0) { if ($user->rights->commande->creer) { + $error=0; + + $this->db->begin(); + $sql = "UPDATE ".MAIN_DB_PREFIX."commande "; $sql.= " SET fk_availability = '".$id."'"; $sql.= " WHERE rowid = ".$this->id; @@ -2337,13 +2344,18 @@ class Commande extends CommonOrder * * @param User $user Object user making change * @param int $id Id of source - * @param int $notrigger Not Trigger + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int <0 if KO, >0 if OK */ function set_demand_reason($user, $id, $notrigger=0) { if ($user->rights->commande->creer) { + + $error=0; + + $this->db->begin(); + $sql = "UPDATE ".MAIN_DB_PREFIX."commande "; $sql.= " SET fk_input_reason = '".$id."'"; $sql.= " WHERE rowid = ".$this->id; @@ -2461,7 +2473,7 @@ class Commande extends CommonOrder * Update delivery delay * * @param int $availability_id Id du nouveau mode - * @param int $notrigger Not Trigger + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int >0 if OK, <0 if KO */ function availability($availability_id, $notrigger=0) @@ -2469,6 +2481,10 @@ class Commande extends CommonOrder dol_syslog('Commande::availability('.$availability_id.')'); if ($this->statut >= self::STATUS_DRAFT) { + $error=0; + + $this->db->begin(); + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande'; $sql .= ' SET fk_availability = '.$availability_id; $sql .= ' WHERE rowid='.$this->id; @@ -2521,7 +2537,7 @@ class Commande extends CommonOrder * Update order demand_reason * * @param int $demand_reason_id Id of new demand - * @param int $notrigger Not Trigger + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int >0 if ok, <0 if ko */ function demand_reason($demand_reason_id, $notrigger=0) @@ -2529,6 +2545,10 @@ class Commande extends CommonOrder dol_syslog('Commande::demand_reason('.$demand_reason_id.')'); if ($this->statut >= self::STATUS_DRAFT) { + $error=0; + + $this->db->begin(); + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande'; $sql .= ' SET fk_input_reason = '.$demand_reason_id; $sql .= ' WHERE rowid='.$this->id; @@ -2582,12 +2602,17 @@ class Commande extends CommonOrder * * @param User $user User that make change * @param string $ref_client Customer ref + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int <0 if KO, >0 if OK */ - function set_ref_client($user, $ref_client) + function set_ref_client($user, $ref_client, $notrigger=0) { if ($user->rights->commande->creer) { + $error=0; + + $this->db->begin(); + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande SET'; $sql.= ' ref_client = '.(empty($ref_client) ? 'NULL' : '\''.$this->db->escape($ref_client).'\''); $sql.= ' WHERE rowid = '.$this->id; diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 73127f9a1f2..089729b4440 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -1486,25 +1486,54 @@ class Facture extends CommonInvoice * Set customer ref * * @param string $ref_client Customer ref + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int <0 if KO, >0 if OK */ - function set_ref_client($ref_client) + function set_ref_client($ref_client, $notrigger=0) { + $error=0; + + $this->db->begin(); + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; if (empty($ref_client)) $sql .= ' SET ref_client = NULL'; else $sql .= ' SET ref_client = \''.$this->db->escape($ref_client).'\''; $sql .= ' WHERE rowid = '.$this->id; - if ($this->db->query($sql)) + + dol_syslog(__METHOD__.' this->id='.$this->id.', ref_client='.$ref_client, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) + { + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('BILL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) { $this->ref_client = $ref_client; + + $this->db->commit(); return 1; } else { - dol_print_error($this->db); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } @@ -2070,7 +2099,7 @@ class Facture extends CommonInvoice $i++; } if ($final) { - $this->setFinal(); + $this->setFinal($user); } } } @@ -2760,9 +2789,10 @@ class Facture extends CommonInvoice * * @param User $user User that set discount * @param double $remise Discount + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int <0 if ko, >0 if ok */ - function set_remise($user, $remise) + function set_remise($user, $remise, $notrigger=0) { // Clean parameters if (empty($remise)) $remise=0; @@ -2771,21 +2801,48 @@ class Facture extends CommonInvoice { $remise=price2num($remise); + $error=0; + + $this->db->begin(); + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; $sql.= ' SET remise_percent = '.$remise; $sql.= ' WHERE rowid = '.$this->id; $sql.= ' AND fk_statut = '.self::STATUS_DRAFT; - if ($this->db->query($sql)) + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) + { + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('BILL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) { $this->remise_percent = $remise; $this->update_price(1); + + $this->db->commit(); return 1; } else { - $this->error=$this->db->error(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } } @@ -2796,14 +2853,19 @@ class Facture extends CommonInvoice * * @param User $user User that set discount * @param double $remise Discount + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @return int <0 if KO, >0 if OK */ - function set_remise_absolue($user, $remise) + function set_remise_absolue($user, $remise, $notrigger=0) { if (empty($remise)) $remise=0; if ($user->rights->facture->creer) { + $error=0; + + $this->db->begin(); + $remise=price2num($remise); $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; @@ -2811,18 +2873,39 @@ class Facture extends CommonInvoice $sql.= ' WHERE rowid = '.$this->id; $sql.= ' AND fk_statut = '.self::STATUS_DRAFT; - dol_syslog(get_class($this)."::set_remise_absolue", LOG_DEBUG); + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) + { + $this->errors[]=$this->db->error(); + $error++; + } - if ($this->db->query($sql)) + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('BILL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) { $this->remise_absolue = $remise; $this->update_price(1); + + $this->db->commit(); return 1; } else { - $this->error=$this->db->error(); - return -1; + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } } } @@ -3818,25 +3901,49 @@ class Facture extends CommonInvoice /** * Sets the invoice as a final situation * - * @return int 1 if ok, -1 if error + * @param User $user Object user + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers + * @return int <0 if KO, >0 if OK */ - function setFinal() + function setFinal(User $user, $notrigger=0) { + $error=0; - $this->db->begin(); + $this->db->begin(); $this->situation_final = 1; - $sql = 'update ' . MAIN_DB_PREFIX . 'facture set situation_final = ' . $this->situation_final . ' where rowid = ' . $this->id; - $resql = $this->db->query($sql); - if ($resql) { - // FIXME: call triggers MODIFY because we modify invoice + $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture SET situation_final = ' . $this->situation_final . ' where rowid = ' . $this->id; + + dol_syslog(__METHOD__, LOG_DEBUG); + $resql=$this->db->query($sql); + if (!$resql) + { + $this->errors[]=$this->db->error(); + $error++; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('BILL_MODIFY',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (! $error) + { $this->db->commit(); return 1; - } else { - $this->error = $this->db->lasterror(); - dol_syslog(get_class($this) . "::update Error setFinal " . $sql, LOG_ERR); + } + else + { + foreach($this->errors as $errmsg) + { + dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } $this->db->rollback(); - return -1; + return -1*$error; } } From 2d9876d1277ff68988262137f77cc3df2d8040fa Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Fri, 8 Jul 2016 21:02:27 +0200 Subject: [PATCH 3/4] clean command class --- htdocs/commande/class/commande.class.php | 114 +++++++++++++----- htdocs/compta/facture/class/facture.class.php | 6 + 2 files changed, 90 insertions(+), 30 deletions(-) diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index b762ed4de24..19778f39547 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -149,6 +149,8 @@ class Commande extends CommonOrder var $multicurrency_total_tva; var $multicurrency_total_ttc; + var $oldcopy; + /** * ERR Not enough stock */ @@ -2071,6 +2073,13 @@ class Commande extends CommonOrder $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->remise_percent = $remise; + $this->update_price(1); + } + if (! $notrigger && empty($error)) { // Call trigger @@ -2081,9 +2090,6 @@ class Commande extends CommonOrder if (! $error) { - $this->remise_percent = $remise; - $this->update_price(1); - $this->db->commit(); return 1; } @@ -2133,6 +2139,13 @@ class Commande extends CommonOrder $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->remise_absolue = $remise; + $this->update_price(1); + } + if (! $notrigger && empty($error)) { // Call trigger @@ -2143,9 +2156,6 @@ class Commande extends CommonOrder if (! $error) { - $this->remise_absolue = $remise; - $this->update_price(1); - $this->db->commit(); return 1; } @@ -2191,6 +2201,12 @@ class Commande extends CommonOrder $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->date = $date; + } + if (! $notrigger && empty($error)) { // Call trigger @@ -2201,8 +2217,6 @@ class Commande extends CommonOrder if (! $error) { - $this->date = $date; - $this->db->commit(); return 1; } @@ -2251,6 +2265,12 @@ class Commande extends CommonOrder $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->date_livraison = $date_livraison; + } + if (! $notrigger && empty($error)) { // Call trigger @@ -2261,8 +2281,6 @@ class Commande extends CommonOrder if (! $error) { - $this->date_livraison = $date_livraison; - $this->db->commit(); return 1; } @@ -2311,6 +2329,12 @@ class Commande extends CommonOrder $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->fk_availability = $id; + } + if (! $notrigger && empty($error)) { // Call trigger @@ -2321,8 +2345,6 @@ class Commande extends CommonOrder if (! $error) { - $this->fk_availability = $id; - $this->db->commit(); return 1; } @@ -2368,6 +2390,12 @@ class Commande extends CommonOrder $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->fk_input_reason = $id; + } + if (! $notrigger && empty($error)) { // Call trigger @@ -2378,8 +2406,6 @@ class Commande extends CommonOrder if (! $error) { - $this->fk_input_reason = $id; - $this->db->commit(); return 1; } @@ -2497,6 +2523,12 @@ class Commande extends CommonOrder $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->availability_id = $availability_id; + } + if (! $notrigger && empty($error)) { // Call trigger @@ -2507,8 +2539,6 @@ class Commande extends CommonOrder if (! $error) { - $this->availability_id = $availability_id; - $this->db->commit(); return 1; } @@ -2561,6 +2591,12 @@ class Commande extends CommonOrder $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->demand_reason_id = $demand_reason_id; + } + if (! $notrigger && empty($error)) { // Call trigger @@ -2571,8 +2607,6 @@ class Commande extends CommonOrder if (! $error) { - $this->demand_reason_id = $demand_reason_id; - $this->db->commit(); return 1; } @@ -2625,6 +2659,12 @@ class Commande extends CommonOrder $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->ref_client = $ref_client; + } + if (! $notrigger && empty($error)) { // Call trigger @@ -2635,8 +2675,6 @@ class Commande extends CommonOrder if (! $error) { - $this->ref_client = $ref_client; - $this->db->commit(); return 1; } @@ -2660,10 +2698,11 @@ class Commande extends CommonOrder /** * Classify the order as invoiced * - * @param User $user Object user making the change - * @return int <0 if KO, >0 if OK + * @param User $user Object user making the change + * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers + * @return int <0 if KO, >0 if OK */ - function classifyBilled(User $user) + function classifyBilled(User $user, $notrigger=0) { global $user; $error = 0; @@ -2676,16 +2715,24 @@ class Commande extends CommonOrder dol_syslog(get_class($this)."::classifyBilled", LOG_DEBUG); if ($this->db->query($sql)) { - // Call trigger - $result=$this->call_trigger('ORDER_CLASSIFY_BILLED',$user); - if ($result < 0) $error++; - // End call triggers + + if (! $error) + { + $this->oldcopy= clone $this; + $this->facturee=1; // deprecated + $this->billed=1; + } + + if (! $notrigger && empty($error)) + { + // Call trigger + $result=$this->call_trigger('ORDER_CLASSIFY_BILLED',$user); + if ($result < 0) $error++; + // End call triggers + } if (! $error) { - $this->facturee=1; // deprecated - $this->billed=1; - $this->db->commit(); return 1; } @@ -2741,6 +2788,13 @@ class Commande extends CommonOrder dol_syslog(get_class($this)."::classifyUnBilled", LOG_DEBUG); if ($this->db->query($sql)) { + if (! $error) + { + $this->oldcopy= clone $this; + $this->facturee=1; // deprecated + $this->billed=1; + } + // Call trigger $result=$this->call_trigger('ORDER_CLASSIFY_UNBILLED',$user); if ($result < 0) $error++; diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 089729b4440..4acea81db36 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -1510,6 +1510,11 @@ class Facture extends CommonInvoice $error++; } + if (! $error) + { + + $this->ref_client = $ref_client; + if (! $notrigger && empty($error)) { // Call trigger @@ -1520,6 +1525,7 @@ class Facture extends CommonInvoice if (! $error) { + $this->ref_client = $ref_client; $this->db->commit(); From 10b0e5dd577a46931afaf0e425e10e2e4e6d47b7 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Fri, 8 Jul 2016 21:28:21 +0200 Subject: [PATCH 4/4] clean trigger and normalize code --- htdocs/comm/propal/class/propal.class.php | 203 +++++++++++------- htdocs/commande/class/commande.class.php | 86 ++++---- htdocs/compta/facture/class/facture.class.php | 90 ++++---- 3 files changed, 222 insertions(+), 157 deletions(-) diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index c6e554957b1..88cfdf907b4 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -59,24 +59,24 @@ class Propal extends CommonObject * ID of the client * @var int */ - var $socid; + public $socid; - var $contactid; - var $author; - var $ref_client; + public $contactid; + public $author; + public $ref_client; /** * Status of the quote * @var int * @see Propal::STATUS_DRAFT, Propal::STATUS_VALIDATED, Propal::STATUS_SIGNED, Propal::STATUS_NOTSIGNED, Propal::STATUS_BILLED */ - var $statut; + public $statut; /** * @deprecated * @see date_creation */ - var $datec; + public $datec; /** * Creation date @@ -88,7 +88,7 @@ class Propal extends CommonObject * @deprecated * @see date_validation */ - var $datev; + public $datev; /** * Validation date @@ -100,70 +100,72 @@ class Propal extends CommonObject * Date of the quote * @var */ - var $date; + public $date; /** * @deprecated * @see date */ - var $datep; - var $date_livraison; - var $fin_validite; + public $datep; + public $date_livraison; + public $fin_validite; - var $user_author_id; - var $user_valid_id; - var $user_close_id; + public $user_author_id; + public $user_valid_id; + public $user_close_id; /** * @deprecated * @see total_ht */ - var $price; + public $price; /** * @deprecated * @see total_tva */ - var $tva; + public $tva; /** * @deprecated * @see total_ttc */ - var $total; + public $total; - var $cond_reglement_code; - var $mode_reglement_code; - var $remise; - var $remise_percent; - var $remise_absolue; - var $fk_address; - var $address_type; - var $address; - var $availability_id; - var $availability_code; - var $demand_reason_id; - var $demand_reason_code; + public $cond_reglement_code; + public $mode_reglement_code; + public $remise; + public $remise_percent; + public $remise_absolue; + public $fk_address; + public $address_type; + public $address; + public $availability_id; + public $availability_code; + public $demand_reason_id; + public $demand_reason_code; - var $products=array(); - var $extraparams=array(); + public $products=array(); + public $extraparams=array(); /** * @var PropaleLigne[] */ - var $lines = array(); - var $line; + public $lines = array(); + public $line; - var $labelstatut=array(); - var $labelstatut_short=array(); + public $labelstatut=array(); + public $labelstatut_short=array(); - var $specimen; + public $specimen; // Multicurrency - var $fk_multicurrency; - var $multicurrency_code; - var $multicurrency_tx; - var $multicurrency_total_ht; - var $multicurrency_total_tva; - var $multicurrency_total_ttc; + public $fk_multicurrency; + public $multicurrency_code; + public $multicurrency_tx; + public $multicurrency_total_ht; + public $multicurrency_total_tva; + public $multicurrency_total_ttc; + + public $oldcopy; /** * Draft status @@ -1641,6 +1643,13 @@ class Propal extends CommonObject $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->date = $date; + $this->datep = $date; // deprecated + } + if (! $notrigger && empty($error)) { // Call trigger @@ -1651,9 +1660,6 @@ class Propal extends CommonObject if (! $error) { - $this->date = $date; - $this->datep = $date; // deprecated - $this->db->commit(); return 1; } @@ -1697,6 +1703,13 @@ class Propal extends CommonObject $error++; } + + if (! $error) + { + $this->oldcopy= clone $this; + $this->fin_validite = $date_fin_validite; + } + if (! $notrigger && empty($error)) { // Call trigger @@ -1707,8 +1720,6 @@ class Propal extends CommonObject if (! $error) { - $this->fin_validite = $date_fin_validite; - $this->db->commit(); return 1; } @@ -1753,6 +1764,12 @@ class Propal extends CommonObject $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->date_livraison = $date_livraison; + } + if (! $notrigger && empty($error)) { // Call trigger @@ -1763,8 +1780,6 @@ class Propal extends CommonObject if (! $error) { - $this->date_livraison = $date_livraison; - $this->db->commit(); return 1; } @@ -1809,6 +1824,12 @@ class Propal extends CommonObject $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->fk_availability = $id; + } + if (! $notrigger && empty($error)) { // Call trigger @@ -1819,8 +1840,6 @@ class Propal extends CommonObject if (! $error) { - $this->fk_availability = $id; - $this->db->commit(); return 1; } @@ -1865,6 +1884,14 @@ class Propal extends CommonObject $error++; } + + if (! $error) + { + $this->oldcopy= clone $this; + $this->fk_input_reason = $id; + } + + if (! $notrigger && empty($error)) { // Call trigger @@ -1875,8 +1902,6 @@ class Propal extends CommonObject if (! $error) { - $this->fk_input_reason = $id; - $this->db->commit(); return 1; } @@ -1920,6 +1945,12 @@ class Propal extends CommonObject $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->ref_client = $ref_client; + } + if (! $notrigger && empty($error)) { // Call trigger @@ -1930,8 +1961,6 @@ class Propal extends CommonObject if (! $error) { - $this->ref_client = $ref_client; - $this->db->commit(); return 1; } @@ -1983,6 +2012,13 @@ class Propal extends CommonObject $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->remise_percent = $remise; + $this->update_price(1); + } + if (! $notrigger && empty($error)) { // Call trigger @@ -1993,9 +2029,6 @@ class Propal extends CommonObject if (! $error) { - $this->remise_percent = $remise; - $this->update_price(1); - $this->db->commit(); return 1; } @@ -2045,6 +2078,13 @@ class Propal extends CommonObject $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->remise_absolue = $remise; + $this->update_price(1); + } + if (! $notrigger && empty($error)) { // Call trigger @@ -2055,9 +2095,6 @@ class Propal extends CommonObject if (! $error) { - $this->remise_absolue = $remise; - $this->update_price(1); - $this->db->commit(); return 1; } @@ -2201,6 +2238,14 @@ class Propal extends CommonObject $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 @@ -2211,8 +2256,6 @@ class Propal extends CommonObject if ( ! $error ) { - $this->statut = $statut; - $this->db->commit(); return 1; } @@ -2254,6 +2297,12 @@ class Propal extends CommonObject $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->statut=self::STATUS_BILLED; + } + if (! $notrigger && empty($error)) { // Call trigger @@ -2264,8 +2313,6 @@ class Propal extends CommonObject if (! $error) { - $this->statut=self::STATUS_BILLED; - $this->db->commit(); return 1; } @@ -2320,6 +2367,13 @@ class Propal extends CommonObject $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->statut = self::STATUS_DRAFT; + $this->brouillon = 1; + } + if (! $notrigger && empty($error)) { // Call trigger @@ -2330,9 +2384,6 @@ class Propal extends CommonObject if (! $error) { - $this->statut = self::STATUS_DRAFT; - $this->brouillon = 1; - $this->db->commit(); return 1; } @@ -2669,6 +2720,12 @@ class Propal extends CommonObject $this->errors[]=$this->db->error(); $error++; } + + if (! $error) + { + $this->oldcopy= clone $this; + $this->availability_id = $availability_id; + } if (! $notrigger && empty($error)) { @@ -2680,8 +2737,6 @@ class Propal extends CommonObject if (! $error) { - $this->availability_id = $availability_id; - $this->db->commit(); return 1; } @@ -2732,6 +2787,12 @@ class Propal extends CommonObject $this->errors[]=$this->db->error(); $error++; } + + if (! $error) + { + $this->oldcopy= clone $this; + $this->demand_reason_id = $demand_reason_id; + } if (! $notrigger && empty($error)) { @@ -2743,8 +2804,6 @@ class Propal extends CommonObject if (! $error) { - $this->demand_reason_id = $demand_reason_id; - $this->db->commit(); return 1; } diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index 19778f39547..ac6386c0574 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -57,99 +57,99 @@ class Commande extends CommonOrder * Client ID * @var int */ - var $socid; + public $socid; - var $ref_client; - var $ref_int; - var $contactid; + public $ref_client; + public $ref_int; + public $contactid; /** * Status of the order. Check the following constants: * @var int * @see Commande::STATUS_CANCELED, Commande::STATUS_DRAFT, Commande::STATUS_ACCEPTED, Commande::STATUS_CLOSED */ - var $statut; + public $statut; /** * @deprecated * @see billed */ - var $facturee; - var $billed; // billed or not + public $facturee; + public $billed; // billed or not - var $brouillon; - var $cond_reglement_code; + public $brouillon; + public $cond_reglement_code; - var $fk_account; + public $fk_account; /** * It holds the label of the payment mode. Use it in case translation cannot be found. * @var string */ - var $mode_reglement; + public $mode_reglement; /** * Payment mode id * @var int */ - var $mode_reglement_id; + public $mode_reglement_id; /** * Payment mode code * @var string */ - var $mode_reglement_code; + public $mode_reglement_code; /** * Availability delivery time id * @var int */ - var $availability_id; + public $availability_id; /** * Availability delivery time code * @var string */ - var $availability_code; + public $availability_code; /** * Label of availability delivery time. Use it in case translation cannot be found. * @var string */ - var $availability; + public $availability; - var $demand_reason_id; - var $demand_reason_code; - var $address; - var $date; // Date commande + public $demand_reason_id; + public $demand_reason_code; + public $address; + public $date; // Date commande /** * @deprecated * @see date */ - var $date_commande; - var $date_livraison; // Date livraison souhaitee - var $fk_remise_except; - var $remise_percent; - var $remise_absolue; - var $info_bits; - var $rang; - var $special_code; - var $source; // Origin of order - var $extraparams=array(); + public $date_commande; + public $date_livraison; // Date livraison souhaitee + public $fk_remise_except; + public $remise_percent; + public $remise_absolue; + public $info_bits; + public $rang; + public $special_code; + public $source; // Origin of order + public $extraparams=array(); - var $linked_objects=array(); + public $linked_objects=array(); - var $user_author_id; + public $user_author_id; /** * @var OrderLine[] */ - var $lines = array(); + public $lines = array(); // Multicurrency - var $fk_multicurrency; - var $multicurrency_code; - var $multicurrency_tx; - var $multicurrency_total_ht; - var $multicurrency_total_tva; - var $multicurrency_total_ttc; + public $fk_multicurrency; + public $multicurrency_code; + public $multicurrency_tx; + public $multicurrency_total_ht; + public $multicurrency_total_tva; + public $multicurrency_total_ttc; - var $oldcopy; + public $oldcopy; /** * ERR Not enough stock @@ -2715,14 +2715,14 @@ class Commande extends CommonOrder dol_syslog(get_class($this)."::classifyBilled", LOG_DEBUG); if ($this->db->query($sql)) { - + if (! $error) { $this->oldcopy= clone $this; $this->facturee=1; // deprecated $this->billed=1; } - + if (! $notrigger && empty($error)) { // Call trigger @@ -2794,7 +2794,7 @@ class Commande extends CommonOrder $this->facturee=1; // deprecated $this->billed=1; } - + // Call trigger $result=$this->call_trigger('ORDER_CLASSIFY_UNBILLED',$user); if ($result < 0) $error++; diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 4acea81db36..309a0ff4f49 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -59,63 +59,63 @@ class Facture extends CommonInvoice */ protected $table_ref_field = 'facnumber'; - var $socid; + public $socid; - var $author; - var $fk_user_author; - var $fk_user_valid; - var $date; // Date invoice - var $date_creation; // Creation date - var $date_validation; // Validation date - var $datem; - var $ref_client; - var $ref_int; + public $author; + public $fk_user_author; + public $fk_user_valid; + public $date; // Date invoice + public $date_creation; // Creation date + public $date_validation; // Validation date + public $datem; + public $ref_client; + public $ref_int; //Check constants for types - var $type = self::TYPE_STANDARD; + public $type = self::TYPE_STANDARD; //var $amount; - var $remise_absolue; - var $remise_percent; - var $total_ht=0; - var $total_tva=0; - var $total_ttc=0; - var $revenuestamp; + public $remise_absolue; + public $remise_percent; + public $total_ht=0; + public $total_tva=0; + public $total_ttc=0; + public $revenuestamp; //! Fermeture apres paiement partiel: discount_vat, badcustomer, abandon //! Fermeture alors que aucun paiement: replaced (si remplace), abandon - var $close_code; + public $close_code; //! Commentaire si mis a paye sans paiement complet - var $close_note; + public $close_note; //! 1 if invoice paid COMPLETELY, 0 otherwise (do not use it anymore, use statut and close_code) - var $paye; + public $paye; //! id of source invoice if replacement invoice or credit note - var $fk_facture_source; - var $linked_objects=array(); - var $date_lim_reglement; - var $cond_reglement_code; // Code in llx_c_paiement - var $mode_reglement_code; // Code in llx_c_paiement - var $fk_bank; // Field to store bank id to use when payment mode is withdraw + public $fk_facture_source; + public $linked_objects=array(); + public $date_lim_reglement; + public $cond_reglement_code; // Code in llx_c_paiement + public $mode_reglement_code; // Code in llx_c_paiement + public $fk_bank; // Field to store bank id to use when payment mode is withdraw /** * @deprecated */ - var $products=array(); + public $products=array(); /** * @var FactureLigne[] */ - var $lines=array(); - var $line; - var $extraparams=array(); - var $specimen; + public $lines=array(); + public $line; + public $extraparams=array(); + public $specimen; - var $fac_rec; + public $fac_rec; // Multicurrency - var $fk_multicurrency; - var $multicurrency_code; - var $multicurrency_tx; - var $multicurrency_total_ht; - var $multicurrency_total_tva; - var $multicurrency_total_ttc; + public $fk_multicurrency; + public $multicurrency_code; + public $multicurrency_tx; + public $multicurrency_total_ht; + public $multicurrency_total_tva; + public $multicurrency_total_ttc; /** * @var int Situation cycle reference number @@ -142,6 +142,8 @@ class Facture extends CommonInvoice */ public $tab_next_situation_invoice=array(); + public $oldcopy; + /** * Standard invoice */ @@ -1512,8 +1514,8 @@ class Facture extends CommonInvoice if (! $error) { - $this->ref_client = $ref_client; + } if (! $notrigger && empty($error)) { @@ -2887,6 +2889,13 @@ class Facture extends CommonInvoice $error++; } + if (! $error) + { + $this->oldcopy= clone $this; + $this->remise_absolue = $remise; + $this->update_price(1); + } + if (! $notrigger && empty($error)) { // Call trigger @@ -2897,9 +2906,6 @@ class Facture extends CommonInvoice if (! $error) { - $this->remise_absolue = $remise; - $this->update_price(1); - $this->db->commit(); return 1; }