From 453466f23f44ea0304ba827a74748eac05140e06 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 3 Mar 2016 21:14:20 +0100 Subject: [PATCH] Fix draft invoice must be destroyed once converted into template --- .../facture/class/facture-rec.class.php | 2 +- htdocs/compta/facture/fiche-rec.php | 44 +++++++++++++++---- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/htdocs/compta/facture/class/facture-rec.class.php b/htdocs/compta/facture/class/facture-rec.class.php index ac2345e250a..87e2fea156c 100644 --- a/htdocs/compta/facture/class/facture-rec.class.php +++ b/htdocs/compta/facture/class/facture-rec.class.php @@ -77,7 +77,7 @@ class FactureRec extends Facture * * @param User $user User object * @param int $facid Id of source invoice - * @return int <0 if KO, id of invoice if OK + * @return int <0 if KO, id of invoice created if OK */ function create($user, $facid) { diff --git a/htdocs/compta/facture/fiche-rec.php b/htdocs/compta/facture/fiche-rec.php index 4e753433419..81eeed8a5b9 100644 --- a/htdocs/compta/facture/fiche-rec.php +++ b/htdocs/compta/facture/fiche-rec.php @@ -123,7 +123,7 @@ if ($action == 'add') $object->date_when = $date_next_execution; // Get first contract linked to invoice used to generate template - if (GETPOST('facid','int') > 0) + if ($id > 0) { $srcObject = new Facture($db); $srcObject->fetch(GETPOST('facid','int')); @@ -133,22 +133,48 @@ if ($action == 'add') if (! empty($srcObject->linkedObjectsIds['contrat'])) { $contractidid = reset($srcObject->linkedObjectsIds['contrat']); - } - $object->origin = 'contrat'; - $object->origin_id = $contractidid; - $object->linked_objects[$object->origin] = $object->origin_id; + $object->origin = 'contrat'; + $object->origin_id = $contractidid; + $object->linked_objects[$object->origin] = $object->origin_id; + } } - if ($object->create($user, $id) > 0) - { - $id = $object->id; + $db->begin(); - header("Location: " . $_SERVER['PHP_SELF'] . '?facid=' . $id); + $oldinvoice = new Facture($db); + $oldinvoice->fetch($id); + + $result = $object->create($user, $oldinvoice->id); + if ($result > 0) + { + $result=$oldinvoice->delete(0, 1); + if ($result < 0) + { + $error++; + setEventMessages($oldinvoice->error, $oldinvoice->errors, 'errors'); + $action = "create"; + } + } + else + { + $error++; + setEventMessages($object->error, $object->errors, 'errors'); + $action = "create"; + } + + if (! $error) + { + $db->commit(); + + header("Location: " . $_SERVER['PHP_SELF'] . '?facid=' . $object->id); exit; } else { + $db->rollback(); + + $error++; setEventMessages($object->error, $object->errors, 'errors'); $action = "create"; }