From 4366fc6a777baf32b4209eb8fb54812b93ca93fc Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 3 Jul 2023 13:08:50 +0200 Subject: [PATCH] Fix error management, missing transaction --- htdocs/core/class/commonobject.class.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index d9a7648251c..c9d8f89703a 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -3568,6 +3568,8 @@ abstract class CommonObject $this->multicurrency_total_tva = 0; $this->multicurrency_total_ttc = 0; + $this->db->begin(); + $num = $this->db->num_rows($resql); $i = 0; while ($i < $num) { @@ -3633,16 +3635,17 @@ 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. TODO This works on the company currency but not on multicurrency + if ($forcedroundingmode == '1') { // Check if we need adjustement onto line for vat. TODO This works on the company currency but not on foreign currency $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) { - $errmsg = 'A rounding difference was detected into TOTAL but is too high to be corrected. Some data in your line may be corrupted. Try to edit each line manually.'; + $errmsg = 'A rounding difference was detected into TOTAL but is too high to be corrected. Some data in your lines may be corrupted. Try to edit each line manually to fix this before restarting.'; dol_syslog($errmsg, LOG_WARNING); - dol_print_error('', $errmsg); - exit; + $this->error = $errmsg; + $error++; + break; } $sqlfix = "UPDATE ".$this->db->prefix().$this->table_element_line." SET ".$fieldtva." = ".price2num($obj->total_tva - $diff).", total_ttc = ".price2num($obj->total_ttc - $diff)." WHERE rowid = ".((int) $obj->rowid); 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); @@ -3703,7 +3706,7 @@ abstract class CommonObject $fieldtva = 'total_tva'; } - if (empty($nodatabaseupdate)) { + if (!$error && empty($nodatabaseupdate)) { $sql = "UPDATE ".$this->db->prefix().$this->table_element.' SET'; $sql .= " ".$fieldht." = ".((float) price2num($this->total_ht, 'MT', 1)).","; $sql .= " ".$fieldtva." = ".((float) price2num($this->total_tva, 'MT', 1)).","; @@ -3726,8 +3729,10 @@ abstract class CommonObject } if (!$error) { + $this->db->commit(); return 1; } else { + $this->db->rollback(); return -1; } } else {