diff --git a/htdocs/compta/facture/class/facture-rec.class.php b/htdocs/compta/facture/class/facture-rec.class.php index 14ec77600c7..ac2345e250a 100644 --- a/htdocs/compta/facture/class/facture-rec.class.php +++ b/htdocs/compta/facture/class/facture-rec.class.php @@ -163,9 +163,7 @@ class FactureRec extends Facture { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_rec"); - /* - * Lines - */ + // Add lines $num=count($facsrc->lines); for ($i = 0; $i < $num; $i++) { @@ -192,6 +190,20 @@ class FactureRec extends Facture $error++; } } + + // Add object linked + if (! $error && $this->id && is_array($this->linked_objects) && ! empty($this->linked_objects)) + { + foreach($this->linked_objects as $origin => $origin_id) + { + $ret = $this->add_object_linked($origin, $origin_id); + if (! $ret) + { + $this->error=$this->db->lasterror(); + $error++; + } + } + } if ($error) { @@ -205,7 +217,7 @@ class FactureRec extends Facture } else { - $this->error=$this->db->error().' sql='.$sql; + $this->error=$this->db->lasterror(); $this->db->rollback(); return -2; } diff --git a/htdocs/compta/facture/fiche-rec.php b/htdocs/compta/facture/fiche-rec.php index 76f796f78b6..4e753433419 100644 --- a/htdocs/compta/facture/fiche-rec.php +++ b/htdocs/compta/facture/fiche-rec.php @@ -121,12 +121,31 @@ if ($action == 'add') $date_next_execution = dol_mktime($rehour, $remin, 0, $remonth, $reday, $reyear); $object->date_when = $date_next_execution; + + // Get first contract linked to invoice used to generate template + if (GETPOST('facid','int') > 0) + { + $srcObject = new Facture($db); + $srcObject->fetch(GETPOST('facid','int')); + + $srcObject->fetchObjectLinked(); + + 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; + } if ($object->create($user, $id) > 0) { $id = $object->id; - header("Location: " . $_SERVER['PHP_SELF'] . '?facid=' . $id); - exit; + + header("Location: " . $_SERVER['PHP_SELF'] . '?facid=' . $id); + exit; } else { @@ -932,6 +951,16 @@ else print ''; + + + print '