From 6af28aafa0aea4065af3e8efedf24d52d7530642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vincent=20de=20Grandpr=C3=A9?= Date: Fri, 3 May 2024 07:13:21 -0400 Subject: [PATCH 1/2] Repair function to calculate invoice total from line items (#29568) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * CLI script to calculate invoice total from line items * php CLI est exécutable * Replaced sum() with price2num() and added support for localtax2. * Added function recalculateinvoicetotals to install/repair.php with more precise invoice qualification for regenerate. * phpcs correction * remove script file and added to authors of repair.php --- htdocs/install/repair.php | 73 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/htdocs/install/repair.php b/htdocs/install/repair.php index d9c5a808bb5..217193f04c0 100644 --- a/htdocs/install/repair.php +++ b/htdocs/install/repair.php @@ -6,6 +6,7 @@ * Copyright (C) 2021 Frédéric France * Copyright (C) 2023 Gauthier VERDOL * Copyright (C) 2024 MDW + * Copyright (C) 2024 Vincent de Grandpré * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -181,7 +182,7 @@ $oneoptionset = (GETPOST('standard', 'alpha') || GETPOST('restore_thirdparties_l || GETPOST('clean_perm_table', 'alpha') || GETPOST('force_disable_of_modules_not_found', 'alpha') || GETPOST('force_utf8_on_tables', 'alpha') || GETPOST('force_utf8mb4_on_tables', 'alpha') || GETPOST('force_collation_from_conf_on_tables', 'alpha') - || GETPOST('rebuild_sequences', 'alpha')); + || GETPOST('rebuild_sequences', 'alpha') || GETPOST('recalculateinvoicetotal', 'alpha')); if ($ok && $oneoptionset) { // Show wait message @@ -1648,10 +1649,76 @@ if ($ok && GETPOST('repair_supplier_order_duplicate_ref')) { } } +// Repair llx_invoice to calculate totals from line items +// WARNING : The process can be long on production environments due to restrictions. +// consider raising php_max_execution time if failing to execute completely. +if ($ok && GETPOST('recalculateinvoicetotal') == 'confirmed') { + $err = 0; + $db->begin(); + $sql = " + SELECT + f.rowid, + SUM(fd.total_ht) as total_ht + FROM ".MAIN_DB_PREFIX."facture f + LEFT JOIN ".MAIN_DB_PREFIX."facturedet fd + ON fd.fk_facture = f.rowid + WHERE f.total_ht = 0 + GROUP BY fd.fk_facture HAVING SUM(fd.total_ht) != 0"; + $resql = $db->query($sql); + if ($resql) { + $num = $db->num_rows($resql); + print "We found ".$num." factures qualified that will have their total recalculated because they are at zero and line items not at zero\n"; + dol_syslog("We found ".$num." factures qualified that will have their total recalculated because they are at zero and line items not at zero"); + + if ($num) { + $i = 0; + while ($i < $num) { + $obj = $db->fetch_object($resql); + $sql_calculs = " + SELECT + SUM(fd.total_ht) as 'total_ht', + SUM(fd.total_tva) as 'total_tva', + SUM(fd.total_localtax1) as 'localtax1', + SUM(fd.total_localtax2) as 'localtax2', + SUM(fd.total_ttc) as 'total_ttc' + FROM + ".MAIN_DB_PREFIX."facturedet fd + WHERE + fd.fk_facture = $obj->rowid"; + $ressql_calculs = $db->query($sql_calculs); + while ($obj_calcul = $db->fetch_object($ressql_calculs)) { + $sql_maj = " + UPDATE ".MAIN_DB_PREFIX."facture + SET + total_ht = ".($obj_calcul->total_ht ? price2num($obj_calcul->total_ht, 'MT') : 0).", + total_tva = ".($obj_calcul->total_tva ? price2num($obj_calcul->total_tva, 'MT') : 0).", + localtax1 = ".($obj_calcul->localtax1 ? price2num($obj_calcul->localtax1, 'MT') : 0).", + localtax2 = ".($obj_calcul->localtax2 ? price2num($obj_calcul->localtax2, 'MT') : 0).", + total_ttc = ".($obj_calcul->total_ttc ? price2num($obj_calcul->total_ttc, 'MT') : 0)." + WHERE + rowid = $obj->rowid"; + $db->query($sql_maj); + } + $i++; + } + } else { + print "Pas de factures à traiter\n"; + } + } else { + dol_print_error($db); + dol_syslog("calculate_total_and_taxes.php: Error"); + $err++; + } + + if ($err == 0) { + $db->commit(); + } else { + $db->rollback(); + } +} + print ''; - - if (empty($actiondone)) { print '
'.$langs->trans("ErrorWrongParameters").'
'; } From 62f513ea3ee2f1088ce940531bcf407f98445e23 Mon Sep 17 00:00:00 2001 From: lamrani abdelwadoud Date: Fri, 3 May 2024 13:16:53 +0200 Subject: [PATCH 2/2] add column to expedition table (#29561) * add column to expedition table * add to table also and change default value --- htdocs/install/mysql/migration/19.0.0-20.0.0.sql | 2 ++ htdocs/install/mysql/tables/llx_expedition.sql | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/htdocs/install/mysql/migration/19.0.0-20.0.0.sql b/htdocs/install/mysql/migration/19.0.0-20.0.0.sql index 0a4829fd61b..29f4951a289 100644 --- a/htdocs/install/mysql/migration/19.0.0-20.0.0.sql +++ b/htdocs/install/mysql/migration/19.0.0-20.0.0.sql @@ -316,3 +316,5 @@ DELETE FROM llx_c_action_trigger WHERE code = 'BILLREC_AUTOCREATEBILL'; -- element_element, see https://github.com/Dolibarr/dolibarr/pull/29329 ALTER TABLE element_element ADD COLUMN relationtype varchar(64) DEFAULT NULL AFTER targettype; + +ALTER TABLE llx_expedition ADD COLUMN signed_status smallint DEFAULT 0 AFTER billed; diff --git a/htdocs/install/mysql/tables/llx_expedition.sql b/htdocs/install/mysql/tables/llx_expedition.sql index f5c910af583..16f6705fdbe 100644 --- a/htdocs/install/mysql/tables/llx_expedition.sql +++ b/htdocs/install/mysql/tables/llx_expedition.sql @@ -44,7 +44,7 @@ create table llx_expedition tracking_number varchar(50), fk_statut smallint DEFAULT 0, -- 0 = draft, 1 = validated, 2 = billed or closed depending on WORKFLOW_BILL_ON_SHIPMENT option billed smallint DEFAULT 0, - + signed_status smallint DEFAULT 0, --0 = not signed, 1 = signed height float, -- height width float, -- with size_units integer, -- unit of all sizes (height, width, depth)