From efb87f011c5e8be7083a8f2f1fe5c72fc09ee4c2 Mon Sep 17 00:00:00 2001 From: Zakaria Boushaba <48571684+Boushabazakaria@users.noreply.github.com> Date: Wed, 7 Jan 2026 20:21:07 +0100 Subject: [PATCH 1/4] FIX: Correct due date calculation for payment term 45J FDM on 15th (#36808) Co-authored-by: Zakaria Boushaba --- htdocs/core/class/commoninvoice.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/class/commoninvoice.class.php b/htdocs/core/class/commoninvoice.class.php index 079a81e8b6e..ac6afab13a4 100644 --- a/htdocs/core/class/commoninvoice.class.php +++ b/htdocs/core/class/commoninvoice.class.php @@ -1159,7 +1159,7 @@ abstract class CommonInvoice extends CommonObject $diff = $date_piece - $date_lim_current; - if ($diff < 0) { + if ($diff <= 0) { $datelim = $date_lim_current; } else { $datelim = $date_lim_next; From d175489fde6bef48a41e1157546aad45a0c6421a Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 7 Jan 2026 20:21:35 +0100 Subject: [PATCH 2/4] Add more phpunits --- test/phpunit/CommonInvoiceTest.php | 79 ++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 5 deletions(-) diff --git a/test/phpunit/CommonInvoiceTest.php b/test/phpunit/CommonInvoiceTest.php index 6c8de7d4972..605cdd5c788 100644 --- a/test/phpunit/CommonInvoiceTest.php +++ b/test/phpunit/CommonInvoiceTest.php @@ -64,16 +64,85 @@ class CommonInvoiceTest extends CommonClassTest $localobject = new Facture($db); $localobject->fetch(1); - $localobject->date = dol_mktime(12, 0, 0, 1, 1, 2010); $result = 0; - // TODO Insert payment terms + print "\n"; + // Add 45 days, take end of month, add 15 days + $localobject->date = dol_mktime(12, 0, 0, 1, 1, 2010); + $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST1', 'TEST1', 45, 1, 15)"; + $resql = $db->query($sql); + $id = $db->last_insert_id("c_payment_term"); + $result = $localobject->calculate_date_lim_reglement($id); + print __METHOD__." date=".dol_print_date($localobject->date, 'dayhour', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'dayhour', 'gmt')."\n"; + $this->assertEquals('15/03/2010 12:00', dol_print_date($result, 'dayhour', 'gmt')); + + // Add 45 days, take end of month, add 15 days + $localobject->date = dol_mktime(12, 0, 0, 3, 31, 2010); + $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST1b', 'TEST1b', 45, 1, 15)"; + $resql = $db->query($sql); + $id = $db->last_insert_id("c_payment_term"); + $result = $localobject->calculate_date_lim_reglement($id); + print __METHOD__." date=".dol_print_date($localobject->date, 'dayhour', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'dayhour', 'gmt')."\n"; + $this->assertEquals('15/06/2010 12:00', dol_print_date($result, 'dayhour', 'gmt')); + + // Test on the mode type_cdr = 2 + + // 2010-01-01 Add 45 days, go to the next 15th + $localobject->date = dol_mktime(12, 0, 0, 1, 1, 2010); + $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST2', 'TEST2', 45, 2, 15)"; + $resql = $db->query($sql); + $id = $db->last_insert_id("c_payment_term"); + if ($id <= 0) { + die(1); + } + $result = $localobject->calculate_date_lim_reglement($id); + print __METHOD__." date=".dol_print_date($localobject->date, 'dayhour', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'dayhour', 'gmt')."\n"; + $this->assertEquals('15/02/2010 00:00', dol_print_date($result, 'dayhour', 'gmt')); + + // 2010-03-30 Add 45 days, go to the next 15th + $localobject->date = dol_mktime(12, 0, 0, 3, 30, 2010); + $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST3', 'TEST3', 45, 2, 15)"; + $resql = $db->query($sql); + $id = $db->last_insert_id("c_payment_term"); + if ($id <= 0) { + die(1); + } + $result = $localobject->calculate_date_lim_reglement($id); + print __METHOD__." date=".dol_print_date($localobject->date, 'dayhour', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'dayhour', 'gmt')."\n"; + $this->assertEquals('15/05/2010 00:00', dol_print_date($result, 'dayhour', 'gmt')); + + // 2010-03-31 Add 45 days, go to the next 15th + $localobject->date = dol_mktime(12, 0, 0, 3, 31, 2010); + $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST3b', 'TEST3b', 45, 2, 15)"; + $resql = $db->query($sql); + $id = $db->last_insert_id("c_payment_term"); + if ($id <= 0) { + die(1); + } + $result = $localobject->calculate_date_lim_reglement($id); + print __METHOD__." date=".dol_print_date($localobject->date, 'dayhour', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'dayhour', 'gmt')."\n"; + $this->assertEquals('15/05/2010 00:00', dol_print_date($result, 'dayhour', 'gmt')); + + // 2010-04-01 Add 45 days, go to the next 15th + $localobject->date = dol_mktime(12, 0, 0, 4, 1, 2010); + $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST3c', 'TEST3c', 45, 2, 15)"; + $resql = $db->query($sql); + $id = $db->last_insert_id("c_payment_term"); + if ($id <= 0) { + die(1); + } + $result = $localobject->calculate_date_lim_reglement($id); + print __METHOD__." date=".dol_print_date($localobject->date, 'dayhour', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'dayhour', 'gmt')."\n"; + $this->assertEquals('15/06/2010 00:00', dol_print_date($result, 'dayhour', 'gmt')); - //$result=$localobject->calculate_date_lim_reglement(1); - //print __METHOD__." result=".$result."\n"; - $this->assertEquals($result, 0); return $result; } } From aab17ffe0cdbccda46b7f74536e07df75cbc92d2 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 8 Jan 2026 13:00:26 +0100 Subject: [PATCH 3/4] Fix CI --- test/phpunit/CommonInvoiceTest.php | 36 +++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/test/phpunit/CommonInvoiceTest.php b/test/phpunit/CommonInvoiceTest.php index 605cdd5c788..b98e20e2641 100644 --- a/test/phpunit/CommonInvoiceTest.php +++ b/test/phpunit/CommonInvoiceTest.php @@ -75,9 +75,9 @@ class CommonInvoiceTest extends CommonClassTest $resql = $db->query($sql); $id = $db->last_insert_id("c_payment_term"); $result = $localobject->calculate_date_lim_reglement($id); - print __METHOD__." date=".dol_print_date($localobject->date, 'dayhour', 'gmt')."\n"; - print __METHOD__." result=".dol_print_date($result, 'dayhour', 'gmt')."\n"; - $this->assertEquals('15/03/2010 12:00', dol_print_date($result, 'dayhour', 'gmt')); + print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; + $this->assertEquals('2010-03-15 12:00:00', dol_print_date($result, 'standard', 'gmt')); // Add 45 days, take end of month, add 15 days $localobject->date = dol_mktime(12, 0, 0, 3, 31, 2010); @@ -85,9 +85,9 @@ class CommonInvoiceTest extends CommonClassTest $resql = $db->query($sql); $id = $db->last_insert_id("c_payment_term"); $result = $localobject->calculate_date_lim_reglement($id); - print __METHOD__." date=".dol_print_date($localobject->date, 'dayhour', 'gmt')."\n"; - print __METHOD__." result=".dol_print_date($result, 'dayhour', 'gmt')."\n"; - $this->assertEquals('15/06/2010 12:00', dol_print_date($result, 'dayhour', 'gmt')); + print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; + $this->assertEquals('2010-06-15 12:00:00', dol_print_date($result, 'standard', 'gmt')); // Test on the mode type_cdr = 2 @@ -100,9 +100,9 @@ class CommonInvoiceTest extends CommonClassTest die(1); } $result = $localobject->calculate_date_lim_reglement($id); - print __METHOD__." date=".dol_print_date($localobject->date, 'dayhour', 'gmt')."\n"; - print __METHOD__." result=".dol_print_date($result, 'dayhour', 'gmt')."\n"; - $this->assertEquals('15/02/2010 00:00', dol_print_date($result, 'dayhour', 'gmt')); + print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; + $this->assertEquals('2010-02-15 00:00:00', dol_print_date($result, 'standard', 'gmt')); // 2010-03-30 Add 45 days, go to the next 15th $localobject->date = dol_mktime(12, 0, 0, 3, 30, 2010); @@ -113,9 +113,9 @@ class CommonInvoiceTest extends CommonClassTest die(1); } $result = $localobject->calculate_date_lim_reglement($id); - print __METHOD__." date=".dol_print_date($localobject->date, 'dayhour', 'gmt')."\n"; - print __METHOD__." result=".dol_print_date($result, 'dayhour', 'gmt')."\n"; - $this->assertEquals('15/05/2010 00:00', dol_print_date($result, 'dayhour', 'gmt')); + print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; + $this->assertEquals('2010-05-15 00:00:00', dol_print_date($result, 'standard', 'gmt')); // 2010-03-31 Add 45 days, go to the next 15th $localobject->date = dol_mktime(12, 0, 0, 3, 31, 2010); @@ -126,9 +126,9 @@ class CommonInvoiceTest extends CommonClassTest die(1); } $result = $localobject->calculate_date_lim_reglement($id); - print __METHOD__." date=".dol_print_date($localobject->date, 'dayhour', 'gmt')."\n"; - print __METHOD__." result=".dol_print_date($result, 'dayhour', 'gmt')."\n"; - $this->assertEquals('15/05/2010 00:00', dol_print_date($result, 'dayhour', 'gmt')); + print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; + $this->assertEquals('2010-05-15 00:00:00', dol_print_date($result, 'standard', 'gmt')); // 2010-04-01 Add 45 days, go to the next 15th $localobject->date = dol_mktime(12, 0, 0, 4, 1, 2010); @@ -139,9 +139,9 @@ class CommonInvoiceTest extends CommonClassTest die(1); } $result = $localobject->calculate_date_lim_reglement($id); - print __METHOD__." date=".dol_print_date($localobject->date, 'dayhour', 'gmt')."\n"; - print __METHOD__." result=".dol_print_date($result, 'dayhour', 'gmt')."\n"; - $this->assertEquals('15/06/2010 00:00', dol_print_date($result, 'dayhour', 'gmt')); + print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; + $this->assertEquals('2010-06-15 00:00:00', dol_print_date($result, 'standard', 'gmt')); return $result; } From 2e47d661e576b9cdde8484a84715297675171965 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 8 Jan 2026 13:06:30 +0100 Subject: [PATCH 4/4] Fix CI --- test/phpunit/CommonInvoiceTest.php | 140 +++++++++++++++-------------- 1 file changed, 71 insertions(+), 69 deletions(-) diff --git a/test/phpunit/CommonInvoiceTest.php b/test/phpunit/CommonInvoiceTest.php index b98e20e2641..db95b263518 100644 --- a/test/phpunit/CommonInvoiceTest.php +++ b/test/phpunit/CommonInvoiceTest.php @@ -69,79 +69,81 @@ class CommonInvoiceTest extends CommonClassTest print "\n"; - // Add 45 days, take end of month, add 15 days - $localobject->date = dol_mktime(12, 0, 0, 1, 1, 2010); - $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST1', 'TEST1', 45, 1, 15)"; - $resql = $db->query($sql); - $id = $db->last_insert_id("c_payment_term"); - $result = $localobject->calculate_date_lim_reglement($id); - print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; - print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; - $this->assertEquals('2010-03-15 12:00:00', dol_print_date($result, 'standard', 'gmt')); + if (in_array($db->type, array('mysql', 'mysqli'))) { + // Add 45 days, take end of month, add 15 days + $localobject->date = dol_mktime(12, 0, 0, 1, 1, 2010); + $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST1', 'TEST1', 45, 1, 15)"; + $resql = $db->query($sql); + $id = $db->last_insert_id("c_payment_term"); + $result = $localobject->calculate_date_lim_reglement($id); + print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; + $this->assertEquals('2010-03-15 12:00:00', dol_print_date($result, 'standard', 'gmt')); - // Add 45 days, take end of month, add 15 days - $localobject->date = dol_mktime(12, 0, 0, 3, 31, 2010); - $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST1b', 'TEST1b', 45, 1, 15)"; - $resql = $db->query($sql); - $id = $db->last_insert_id("c_payment_term"); - $result = $localobject->calculate_date_lim_reglement($id); - print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; - print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; - $this->assertEquals('2010-06-15 12:00:00', dol_print_date($result, 'standard', 'gmt')); + // Add 45 days, take end of month, add 15 days + $localobject->date = dol_mktime(12, 0, 0, 3, 31, 2010); + $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST1b', 'TEST1b', 45, 1, 15)"; + $resql = $db->query($sql); + $id = $db->last_insert_id("c_payment_term"); + $result = $localobject->calculate_date_lim_reglement($id); + print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; + $this->assertEquals('2010-06-15 12:00:00', dol_print_date($result, 'standard', 'gmt')); - // Test on the mode type_cdr = 2 + // Test on the mode type_cdr = 2 - // 2010-01-01 Add 45 days, go to the next 15th - $localobject->date = dol_mktime(12, 0, 0, 1, 1, 2010); - $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST2', 'TEST2', 45, 2, 15)"; - $resql = $db->query($sql); - $id = $db->last_insert_id("c_payment_term"); - if ($id <= 0) { - die(1); + // 2010-01-01 Add 45 days, go to the next 15th + $localobject->date = dol_mktime(12, 0, 0, 1, 1, 2010); + $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST2', 'TEST2', 45, 2, 15)"; + $resql = $db->query($sql); + $id = $db->last_insert_id("c_payment_term"); + if ($id <= 0) { + die(1); + } + $result = $localobject->calculate_date_lim_reglement($id); + print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; + $this->assertEquals('2010-02-15 00:00:00', dol_print_date($result, 'standard', 'gmt')); + + // 2010-03-30 Add 45 days, go to the next 15th + $localobject->date = dol_mktime(12, 0, 0, 3, 30, 2010); + $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST3', 'TEST3', 45, 2, 15)"; + $resql = $db->query($sql); + $id = $db->last_insert_id("c_payment_term"); + if ($id <= 0) { + die(1); + } + $result = $localobject->calculate_date_lim_reglement($id); + print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; + $this->assertEquals('2010-05-15 00:00:00', dol_print_date($result, 'standard', 'gmt')); + + // 2010-03-31 Add 45 days, go to the next 15th + $localobject->date = dol_mktime(12, 0, 0, 3, 31, 2010); + $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST3b', 'TEST3b', 45, 2, 15)"; + $resql = $db->query($sql); + $id = $db->last_insert_id("c_payment_term"); + if ($id <= 0) { + die(1); + } + $result = $localobject->calculate_date_lim_reglement($id); + print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; + $this->assertEquals('2010-05-15 00:00:00', dol_print_date($result, 'standard', 'gmt')); + + // 2010-04-01 Add 45 days, go to the next 15th + $localobject->date = dol_mktime(12, 0, 0, 4, 1, 2010); + $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST3c', 'TEST3c', 45, 2, 15)"; + $resql = $db->query($sql); + $id = $db->last_insert_id("c_payment_term"); + if ($id <= 0) { + die(1); + } + $result = $localobject->calculate_date_lim_reglement($id); + print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; + print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; + $this->assertEquals('2010-06-15 00:00:00', dol_print_date($result, 'standard', 'gmt')); } - $result = $localobject->calculate_date_lim_reglement($id); - print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; - print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; - $this->assertEquals('2010-02-15 00:00:00', dol_print_date($result, 'standard', 'gmt')); - - // 2010-03-30 Add 45 days, go to the next 15th - $localobject->date = dol_mktime(12, 0, 0, 3, 30, 2010); - $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST3', 'TEST3', 45, 2, 15)"; - $resql = $db->query($sql); - $id = $db->last_insert_id("c_payment_term"); - if ($id <= 0) { - die(1); - } - $result = $localobject->calculate_date_lim_reglement($id); - print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; - print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; - $this->assertEquals('2010-05-15 00:00:00', dol_print_date($result, 'standard', 'gmt')); - - // 2010-03-31 Add 45 days, go to the next 15th - $localobject->date = dol_mktime(12, 0, 0, 3, 31, 2010); - $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST3b', 'TEST3b', 45, 2, 15)"; - $resql = $db->query($sql); - $id = $db->last_insert_id("c_payment_term"); - if ($id <= 0) { - die(1); - } - $result = $localobject->calculate_date_lim_reglement($id); - print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; - print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; - $this->assertEquals('2010-05-15 00:00:00', dol_print_date($result, 'standard', 'gmt')); - - // 2010-04-01 Add 45 days, go to the next 15th - $localobject->date = dol_mktime(12, 0, 0, 4, 1, 2010); - $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_payment_term(code, libelle, nbjour, type_cdr, decalage) VALUES ('TEST3c', 'TEST3c', 45, 2, 15)"; - $resql = $db->query($sql); - $id = $db->last_insert_id("c_payment_term"); - if ($id <= 0) { - die(1); - } - $result = $localobject->calculate_date_lim_reglement($id); - print __METHOD__." date=".dol_print_date($localobject->date, 'standard', 'gmt')."\n"; - print __METHOD__." result=".dol_print_date($result, 'standard', 'gmt')."\n"; - $this->assertEquals('2010-06-15 00:00:00', dol_print_date($result, 'standard', 'gmt')); return $result; }