diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index 2744c3eec4d..c843da68998 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -315,9 +315,8 @@ class Propal extends CommonObject function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $remise_percent=0, $price_base_type='HT', $pu_ttc=0, $info_bits=0, $type=0, $rang=-1, $special_code=0, $fk_parent_line=0, $fk_fournprice=0, $pa_ht=0, $label='',$date_start='', $date_end='',$array_option=0) { global $mysoc; - $propalid=$this->id; - dol_syslog(get_class($this)."::addline propalid=$propalid, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_except=$remise_percent, price_base_type=$price_base_type, pu_ttc=$pu_ttc, info_bits=$info_bits, type=$type"); + dol_syslog(get_class($this)."::addline propalid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_except=$remise_percent, price_base_type=$price_base_type, pu_ttc=$pu_ttc, info_bits=$info_bits, type=$type"); include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; // Clean parameters @@ -386,7 +385,7 @@ class Propal extends CommonObject // Insert line $this->line=new PropaleLigne($this->db); - $this->line->fk_propal=$propalid; + $this->line->fk_propal=$this->id; $this->line->label=$label; $this->line->desc=$desc; $this->line->qty=$qty; @@ -435,7 +434,7 @@ class Propal extends CommonObject if (! empty($fk_parent_line)) $this->line_order(true,'DESC'); // Mise a jour informations denormalisees au niveau de la propale meme - $result=$this->update_price(1); + $result=$this->update_price(1,'auto'); // This method is designed to add line from user input so total calculation must be done using 'auto' mode. if ($result > 0) { $this->db->commit(); diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index dd5bb59c678..b3d90fd562b 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -1052,9 +1052,7 @@ class Commande extends CommonOrder { global $mysoc, $conf, $langs; - $commandeid=$this->id; - - dol_syslog(get_class($this)."::addline commandeid=$commandeid, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent, info_bits=$info_bits, fk_remise_except=$fk_remise_except, price_base_type=$price_base_type, pu_ttc=$pu_ttc, date_start=$date_start, date_end=$date_end, type=$type", LOG_DEBUG); + dol_syslog(get_class($this)."::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent, info_bits=$info_bits, fk_remise_except=$fk_remise_except, price_base_type=$price_base_type, pu_ttc=$pu_ttc, date_start=$date_start, date_end=$date_end, type=$type", LOG_DEBUG); include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; @@ -1143,7 +1141,7 @@ class Commande extends CommonOrder // Insert line $this->line=new OrderLine($this->db); - $this->line->fk_commande=$commandeid; + $this->line->fk_commande=$this->id; $this->line->label=$label; $this->line->desc=$desc; $this->line->qty=$qty; @@ -1190,8 +1188,7 @@ class Commande extends CommonOrder if (! empty($fk_parent_line)) $this->line_order(true,'DESC'); // Mise a jour informations denormalisees au niveau de la commande meme - $this->id=$commandeid; // TODO A virer - $result=$this->update_price(1); + $result=$this->update_price(1,'auto'); // This method is designed to add line from user input so total calculation must be done using 'auto' mode. if ($result > 0) { $this->db->commit(); diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 65736e06097..5924b47dbd7 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -411,7 +411,7 @@ class Facture extends CommonInvoice /* * Insert lines of predefined invoices - */ + */ if (! $error && $this->fac_rec > 0) { foreach ($_facrec->lines as $i => $val) @@ -2010,9 +2010,7 @@ class Facture extends CommonInvoice { global $mysoc, $conf, $langs; - $facid=$this->id; - - dol_syslog(get_class($this)."::Addline facid=$facid,desc=$desc,pu_ht=$pu_ht,qty=$qty,txtva=$txtva, txlocaltax1=$txlocaltax1, txlocaltax2=$txlocaltax2, fk_product=$fk_product,remise_percent=$remise_percent,date_start=$date_start,date_end=$date_end,ventil=$ventil,info_bits=$info_bits,fk_remise_except=$fk_remise_except,price_base_type=$price_base_type,pu_ttc=$pu_ttc,type=$type", LOG_DEBUG); + dol_syslog(get_class($this)."::addline facid=$this->id,desc=$desc,pu_ht=$pu_ht,qty=$qty,txtva=$txtva, txlocaltax1=$txlocaltax1, txlocaltax2=$txlocaltax2, fk_product=$fk_product,remise_percent=$remise_percent,date_start=$date_start,date_end=$date_end,ventil=$ventil,info_bits=$info_bits,fk_remise_except=$fk_remise_except,price_base_type=$price_base_type,pu_ttc=$pu_ttc,type=$type", LOG_DEBUG); include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; // Clean parameters @@ -2058,21 +2056,6 @@ class Facture extends CommonInvoice $localtaxes_type=getLocalTaxesFromRate($txtva,0,$mysoc); - /*if (is_object($invoiceline) && $invoiceline->total_ttc && $invoiceline->total_ht) // We already have details calculated - { - $pu=($price_base_type=='HT'?price2num($invoiceline->total_ttc/$qty,'MU'):price2num($invoiceline->total_ht/$qty,'MU')); - - $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, '', $localtaxes_type); - // Now overwrite already calculated fields - $tabprice[0] = $array_calculated['total_ht']; - $tabprice[1] = $array_calculated['total_vat']; - $tabprice[2] = $array_calculated['total_ttc']; - $tabprice[9] = $array_calculated['total_localtax1']; - $tabprice[10] = $array_calculated['total_localtax2']; - $tabprice[3] = $array_calculated['pu_ht']; - } - else*/ - $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, '', $localtaxes_type); $total_ht = $tabprice[0]; @@ -2106,7 +2089,7 @@ class Facture extends CommonInvoice // Insert line $this->line=new FactureLigne($this->db); - $this->line->fk_facture=$facid; + $this->line->fk_facture=$this->id; $this->line->label=$label; // deprecated $this->line->desc=$desc; $this->line->qty= ($this->type==self::TYPE_CREDIT_NOTE?abs($qty):$qty); // For credit note, quantity is always positive and unit price negative @@ -2150,8 +2133,7 @@ class Facture extends CommonInvoice if (! empty($fk_parent_line)) $this->line_order(true,'DESC'); // Mise a jour informations denormalisees au niveau de la facture meme - $this->id=$facid; // TODO To move this we must remove parameter facid into this function declaration - $result=$this->update_price(1); + $result=$this->update_price(1,'auto'); // This method is designed to add line from user input so total calculation must be done using 'auto' mode. if ($result > 0) { $this->db->commit(); diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index ec8a9be586b..5bb313ab6f7 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -1515,21 +1515,23 @@ abstract class CommonObject * Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines). * Must be called at end of methods addline or updateline. * - * @param int $exclspec Exclude special product (product_type=9) - * @param int $roundingadjust -1=Use default method (MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND if defined, or 0), 0=Force use total of rounding, 1=Force use rounding of total + * @param int $exclspec >0 = Exclude special product (product_type=9) + * @param string $roundingadjust 'none'=Do nothing, 'auto'=Use default method (MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND if defined, or '0'), '0'=Force use total of rounding, '1'=Force use rounding of total * @param int $nodatabaseupdate 1=Do not update database. Update only properties of object. * @param Societe $seller If roundingadjust is 0, it means we recalculate total for lines before calculating total for object. For this, we need seller object. * @return int <0 if KO, >0 if OK */ - function update_price($exclspec=0,$roundingadjust=-1,$nodatabaseupdate=0,$seller='') + function update_price($exclspec=0,$roundingadjust='none',$nodatabaseupdate=0,$seller='') { global $conf; include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; + if ($roundingadjust == '-1') $roundingadjust='auto'; // For backward compatibility + $forcedroundingmode=$roundingadjust; - if ($forcedroundingmode < 0 && isset($conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND)) $forcedroundingmode=$conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND; - if ($forcedroundingmode < 0) $forcedroundingmode=0; + if ($forcedroundingmode == 'auto' && isset($conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND)) $forcedroundingmode=$conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND; + if ($forcedroundingmode == 'auto') $forcedroundingmode='0'; $error=0; @@ -1575,17 +1577,17 @@ abstract class CommonObject { $obj = $this->db->fetch_object($resql); - // By default, no adjustement is required ($forcedroundingmode = -1) - if ($forcedroundingmode == 0) // Check if we need adjustement onto line for vat + // Note: There is no check on detail line and no check on total, if $forcedroundingmode = 'none' + + if ($forcedroundingmode == '0') // Check if data on line are consistent. This may solve lines that were not consistent because set with $forcedroundingmode='auto' { $localtax_array=array($obj->localtax1_type,$obj->localtax1_tx,$obj->localtax2_type,$obj->localtax2_tx); $tmpcal=calcul_price_total($obj->qty, $obj->up, $obj->remise_percent, $obj->vatrate, $obj->localtax1_tx, $obj->localtax2_tx, 0, 'HT', $obj->info_bits, $obj->product_type, $seller, $localtax_array); $diff=price2num($tmpcal[1] - $obj->total_tva, 'MT', 1); if ($diff) { - if (abs($diff) > 0.1) { dol_syslog('','A rounding difference was detected', LOG_WARNING); } $sqlfix="UPDATE ".MAIN_DB_PREFIX.$this->table_element_line." SET ".$fieldtva." = ".$tmpcal[1].", total_ttc = ".$tmpcal[2]." WHERE rowid = ".$obj->rowid; - dol_syslog('We found a difference of '.$diff.' for line rowid = '.$obj->rowid.". We fix the vat and total_ttc by running sqlfix = ".$sqlfix); + dol_syslog('We found unconsistent data into detailed line (difference of '.$diff.') for line rowid = '.$obj->rowid." (total vat of line calculated=".$tmpcal[1].", database=".$obj->total_tva."). We fix the total_vat and total_ttc of line by running sqlfix = ".$sqlfix); $resqlfix=$this->db->query($sqlfix); if (! $resqlfix) dol_print_error($this->db,'Failed to update line'); $obj->total_tva = $tmpcal[1]; @@ -1606,17 +1608,16 @@ abstract class CommonObject $total_tva_by_vats[$obj->vatrate] += $obj->total_tva; $total_ttc_by_vats[$obj->vatrate] += $obj->total_ttc; - if ($forcedroundingmode == 1) // Check if we need adjustement onto line for vat + if ($forcedroundingmode == '1') // Check if we need adjustement onto line for vat { $tmpvat=price2num($total_ht_by_vats[$obj->vatrate] * $obj->vatrate / 100, 'MT', 1); $diff=price2num($total_tva_by_vats[$obj->vatrate]-$tmpvat, 'MT', 1); //print 'Line '.$i.' rowid='.$obj->rowid.' vat_rate='.$obj->vatrate.' total_ht='.$obj->total_ht.' total_tva='.$obj->total_tva.' total_ttc='.$obj->total_ttc.' total_ht_by_vats='.$total_ht_by_vats[$obj->vatrate].' total_tva_by_vats='.$total_tva_by_vats[$obj->vatrate].' (new calculation = '.$tmpvat.') total_ttc_by_vats='.$total_ttc_by_vats[$obj->vatrate].($diff?" => DIFF":"")."
\n"; if ($diff) { - if (abs($diff) > 0.1) { dol_syslog('','A rounding difference was detected but is too high to be corrected', LOG_WARNING); exit; } + if (abs($diff) > 0.1) { dol_syslog('','A rounding difference was detected into TOTAL but is too high to be corrected', LOG_WARNING); exit; } $sqlfix="UPDATE ".MAIN_DB_PREFIX.$this->table_element_line." SET ".$fieldtva." = ".($obj->total_tva - $diff).", total_ttc = ".($obj->total_ttc - $diff)." WHERE rowid = ".$obj->rowid; - //print 'We found a difference of '.$diff.' for line rowid = '.$obj->rowid.". Run sqlfix = ".$sqlfix."
\n"; - dol_syslog('We found a difference of '.$diff.' for line rowid = '.$obj->rowid.". We fix the vat and total_ttc by running sqlfix = ".$sqlfix); + dol_syslog('We found a difference of '.$diff.' for line rowid = '.$obj->rowid.". We fix the total_vat and total_ttc of line by running sqlfix = ".$sqlfix); $resqlfix=$this->db->query($sqlfix); if (! $resqlfix) dol_print_error($this->db,'Failed to update line'); $this->total_tva -= $diff; diff --git a/htdocs/core/lib/price.lib.php b/htdocs/core/lib/price.lib.php index 7be5f3e2ee5..ac9845f22a1 100644 --- a/htdocs/core/lib/price.lib.php +++ b/htdocs/core/lib/price.lib.php @@ -299,7 +299,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocalt } } - //print "Price.lib::calcul_price_total ".$result[0]."-".$result[1]."-".$result[2]; + dol_syslog('Price.lib::calcul_price_total MAIN_ROUNDING_RULE_TOT='.$conf->global->MAIN_ROUNDING_RULE_TOT.' pu='.$pu.' qty='.$qty.' price_base_type='.$price_base_type.' total_ht='.$result[0].'-total_vat='.$result[1].'-total_ttc='.$result[2]); return $result; } diff --git a/htdocs/fourn/facture/fiche.php b/htdocs/fourn/facture/fiche.php index a787930471a..aa69e259353 100644 --- a/htdocs/fourn/facture/fiche.php +++ b/htdocs/fourn/facture/fiche.php @@ -989,7 +989,7 @@ elseif ($action == 'calculate') $object->fetch($id); $object->fetch_thirdparty(); - $result=$object->update_price(0, ($calculationrule=='totalofround'?0:1), 0, $object->thirdparty); + $result=$object->update_price(0, (($calculationrule=='totalofround')?'0':'1'), 0, $object->thirdparty); if ($result <= 0) { dol_print_error($db,$result);