From fa6c3eff80f3415c546df74e429c646ac6cdd58a Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 30 Sep 2017 12:53:24 +0200 Subject: [PATCH] More PHPunit on invoice deletion --- htdocs/compta/facture/class/facture.class.php | 30 +++++++++++------ test/phpunit/FactureTest.php | 33 +++++++++++++++++-- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index b7603400c61..b22816f1473 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -1737,7 +1737,7 @@ class Facture extends CommonInvoice * @param User $user User making the deletion. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers * @param int $idwarehouse Id warehouse to use for stock change. - * @return int <0 if KO, >0 if OK + * @return int <0 if KO, 0=Refused, >0 if OK */ function delete($user, $notrigger=0, $idwarehouse=-1) { @@ -1748,9 +1748,13 @@ class Facture extends CommonInvoice dol_syslog(get_class($this)."::delete rowid=".$rowid, LOG_DEBUG); - // TODO Test if there is at least one payment. If yes, refuse to delete. + // Test to avoid invoice deletion (allowed if draft) + $test = $this->is_erasable(); + + if ($test <= 0) return $test; $error=0; + $this->db->begin(); if (! $error && ! $notrigger) @@ -3356,9 +3360,9 @@ class Facture extends CommonInvoice /** * Return if an invoice can be deleted * Rule is: - * If hidden option INVOICE_CAN_ALWAYS_BE_REMOVED is on, we can - * If invoice has a definitive ref, is last, without payment and not dipatched into accountancy -> yes end of rule * If invoice is draft and ha a temporary ref -> yes + * If hidden option INVOICE_CAN_ALWAYS_BE_REMOVED is on, we can. If hidden option INVOICE_CAN_NEVER_BE_REMOVED is on, we can't. + * If invoice has a definitive ref, is last, without payment and not dipatched into accountancy -> yes end of rule * * @return int <0 if KO, 0=no, 1=yes */ @@ -3366,30 +3370,36 @@ class Facture extends CommonInvoice { global $conf; + // on verifie si la facture est en numerotation provisoire + $facref = substr($this->ref, 1, 4); + + if ($this->statut == self::STATUS_DRAFT && $facref == 'PROV') // If draft invoice and ref not yet defined + { + return 1; + } + if (! empty($conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED)) return 1; if (! empty($conf->global->INVOICE_CAN_NEVER_BE_REMOVED)) return 0; - // on verifie si la facture est en numerotation provisoire - $facref = substr($this->ref, 1, 4); + // TODO Test if there is at least one payment. If yes, refuse to delete. + // ... // If not a draft invoice and not temporary invoice if ($facref != 'PROV') { $maxfacnumber = $this->getNextNumRef($this->thirdparty,'last'); $ventilExportCompta = $this->getVentilExportCompta(); + // If there is no invoice into the reset range and not already dispatched, we can delete if ($maxfacnumber == '' && $ventilExportCompta == 0) return 1; // If invoice to delete is last one and not already dispatched, we can delete if ($maxfacnumber == $this->ref && $ventilExportCompta == 0) return 1; + if ($this->situation_cycle_ref) { $last = $this->is_last_in_cycle(); return $last; } } - else if ($this->statut == self::STATUS_DRAFT && $facref == 'PROV') // Si facture brouillon et provisoire - { - return 1; - } return 0; } diff --git a/test/phpunit/FactureTest.php b/test/phpunit/FactureTest.php index 8c3d4a2374a..eed67935032 100644 --- a/test/phpunit/FactureTest.php +++ b/test/phpunit/FactureTest.php @@ -272,12 +272,41 @@ class FactureTest extends PHPUnit_Framework_TestCase $langs=$this->savlangs; $db=$this->savdb; + // Force default setup + unset($conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED); + unset($conf->global->INVOICE_CAN_NEVER_BE_REMOVED); + $localobject=new Facture($this->savdb); $result=$localobject->fetch($id); - $result=$localobject->delete($user); + // Create another invoice and validate it after $localobject + $localobject2=new Facture($this->savdb); + $result=$localobject2->initAsSpecimen(); + $result=$localobject2->create($user); + $result=$localobject2->validate($user); + print 'Invoice localobject ref = '.$localobject->ref."\n"; + print 'Invoice $localobject2 created with ref = '.$localobject2->ref."\n"; + + $conf->global->INVOICE_CAN_NEVER_BE_REMOVED = 1; + + $result=$localobject2->delete($user); // Deletion is KO, option INVOICE_CAN_NEVER_BE_REMOVED is on print __METHOD__." id=".$id." result=".$result."\n"; - $this->assertGreaterThanOrEqual(0, $result); + $this->assertEquals(0, $result, 'Deletion should fail, option INVOICE_CAN_NEVER_BE_REMOVED is on'); + + unset($conf->global->INVOICE_CAN_NEVER_BE_REMOVED); + + $result=$localobject->delete($user); // Deletion is KO, it is not last invoice + print __METHOD__." id=".$id." result=".$result."\n"; + $this->assertEquals(0, $result, 'Deletion should fail, it is not last invoice'); + + $result=$localobject2->delete($user); // Deletion is OK, it is last invoice + print __METHOD__." id=".$id." result=".$result."\n"; + $this->assertGreaterThan(0, $result, 'Deletion should work, it is last invoice'); + + $result=$localobject->delete($user); // Deletion is KO, it is not last invoice + print __METHOD__." id=".$id." result=".$result."\n"; + $this->assertGreaterThan(0, $result, 'Deletion should work, it is again last invoice'); + return $result; }