From ce84990414d00d4e5a74f73bf68bdb456f84628a Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Tue, 16 May 2017 02:43:05 +0200
Subject: [PATCH] NEW On invoices generated by template, we save if of source
 template.

---
 htdocs/compta/facture/class/facture-rec.class.php |  4 +++-
 htdocs/compta/facture/class/facture.class.php     |  5 ++++-
 htdocs/compta/facture/fiche-rec.php               |  5 +++--
 htdocs/install/mysql/migration/5.0.0-6.0.0.sql    |  2 +-
 htdocs/install/mysql/tables/llx_facture.sql       |  3 ++-
 htdocs/langs/en_US/other.lang                     | 12 ++++++++++++
 6 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/htdocs/compta/facture/class/facture-rec.class.php b/htdocs/compta/facture/class/facture-rec.class.php
index ef23a30b878..927be3fa25e 100644
--- a/htdocs/compta/facture/class/facture-rec.class.php
+++ b/htdocs/compta/facture/class/facture-rec.class.php
@@ -446,7 +446,7 @@ class FactureRec extends CommonInvoice
 		}
 		else
 		{
-			$this->error=$this->db-lasterror();
+			$this->error=$this->db->lasterror();
 			return -3;
 		}
 	}
@@ -849,6 +849,8 @@ class FactureRec extends CommonInvoice
 
 			    $facture = new Facture($db);
 				$facture->fac_rec = $facturerec->id;    // We will create $facture from this recurring invoice
+				$facture->fk_fac_rec_source = $facturerec->id;    // We will create $facture from this recurring invoice
+				
 			    $facture->type = self::TYPE_STANDARD;
 			    $facture->brouillon = 1;
 			    $facture->date = $facturerec->date_when;	// We could also use dol_now here but we prefer date_when so invoice has real date when we would like even if we generate later.
diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php
index 844e217cccb..e46b5718d22 100644
--- a/htdocs/compta/facture/class/facture.class.php
+++ b/htdocs/compta/facture/class/facture.class.php
@@ -279,6 +279,8 @@ class Facture extends CommonInvoice
 		// Create invoice from a template invoice
 		if ($this->fac_rec > 0)
 		{
+		    $this->fk_fac_rec_source = $this->fac_rec;
+		    
 			require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture-rec.class.php';
 			$_facrec = new FactureRec($this->db);
 			$result=$_facrec->fetch($this->fac_rec);
@@ -386,7 +388,7 @@ class Facture extends CommonInvoice
 		$sql.= ", note_public";
 		$sql.= ", ref_client, ref_int";
         $sql.= ", fk_account";
-		$sql.= ", fk_facture_source, fk_user_author, fk_projet";
+		$sql.= ", fk_fac_rec_source, fk_facture_source, fk_user_author, fk_projet";
 		$sql.= ", fk_cond_reglement, fk_mode_reglement, date_lim_reglement, model_pdf";
 		$sql.= ", situation_cycle_ref, situation_counter, situation_final";
 		$sql.= ", fk_incoterms, location_incoterms";
@@ -410,6 +412,7 @@ class Facture extends CommonInvoice
 		$sql.= ", ".($this->ref_client?"'".$this->db->escape($this->ref_client)."'":"null");
 		$sql.= ", ".($this->ref_int?"'".$this->db->escape($this->ref_int)."'":"null");
 		$sql.= ", ".($this->fk_account>0?$this->fk_account:'NULL');
+		$sql.= ", ".($this->fk_fac_rec_source?"'".$this->db->escape($this->fk_fac_rec_source)."'":"null");
 		$sql.= ", ".($this->fk_facture_source?"'".$this->db->escape($this->fk_facture_source)."'":"null");
 		$sql.= ", ".($user->id > 0 ? "'".$user->id."'":"null");
 		$sql.= ", ".($this->fk_project?$this->fk_project:"null");
diff --git a/htdocs/compta/facture/fiche-rec.php b/htdocs/compta/facture/fiche-rec.php
index acf97a3893a..773f04d2ca0 100644
--- a/htdocs/compta/facture/fiche-rec.php
+++ b/htdocs/compta/facture/fiche-rec.php
@@ -44,6 +44,7 @@ require_once DOL_DOCUMENT_ROOT . '/core/lib/invoice.lib.php';
 $langs->load('bills');
 $langs->load('compta');
 $langs->load('admin');
+$langs->load('other');
 
 // Security check
 $id=(GETPOST('facid','int')?GETPOST('facid','int'):GETPOST('id','int'));
@@ -940,7 +941,7 @@ if ($action == 'create')
 		    '__INVOICE_YEAR__' =>  $langs->trans("PreviousYearOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date($object->date,'%Y').')',
 		    '__INVOICE_NEXT_YEAR__' => $langs->trans("NextYearOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($object->date, 1, 'y'),'%Y').')'
 		);
-		$substitutionarray['__(TRANSKEY)__']=$langs->trans("TransKey");
+		$substitutionarray['__(TransKey)__']=$langs->trans("TransKey");
 		
 		$htmltext = '<i>'.$langs->trans("FollowingConstantsWillBeSubstituted").':<br>';
 		foreach($substitutionarray as $key => $val)
@@ -1258,7 +1259,7 @@ else
 		    '__INVOICE_YEAR__' =>  $langs->trans("PreviousYearOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date($dateexample,'%Y').')',
 		    '__INVOICE_NEXT_YEAR__' => $langs->trans("NextYearOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($dateexample, 1, 'y'),'%Y').')'
 		);
-		$substitutionarray['__(TRANSKEY)__']=$langs->trans("TransKey");
+		$substitutionarray['__(TransKey)__']=$langs->trans("TransKey");
 		
 		$htmltext = '<i>'.$langs->trans("FollowingConstantsWillBeSubstituted").':<br>';
 		foreach($substitutionarray as $key => $val)
diff --git a/htdocs/install/mysql/migration/5.0.0-6.0.0.sql b/htdocs/install/mysql/migration/5.0.0-6.0.0.sql
index 7572e0d2030..88260498f28 100644
--- a/htdocs/install/mysql/migration/5.0.0-6.0.0.sql
+++ b/htdocs/install/mysql/migration/5.0.0-6.0.0.sql
@@ -278,7 +278,7 @@ insert into llx_c_tva(fk_pays,taux,code,recuperableonly,localtax1,localtax1_type
 
 ALTER TABLE llx_events MODIFY COLUMN ip varchar(250);
 
-
+ALTER TABLE llx_facture ADD COLUMN fk_fac_rec_source integer;
 
 
 
diff --git a/htdocs/install/mysql/tables/llx_facture.sql b/htdocs/install/mysql/tables/llx_facture.sql
index aaf467a06b0..014572cd7e6 100644
--- a/htdocs/install/mysql/tables/llx_facture.sql
+++ b/htdocs/install/mysql/tables/llx_facture.sql
@@ -63,7 +63,8 @@ create table llx_facture
   fk_user_modif         integer,                                -- user making last change
   fk_user_valid			integer,								-- user validating
 
-  fk_facture_source		integer,								-- facture origine si facture avoir
+  fk_fac_rec_source		integer,								-- facture rec source
+  fk_facture_source		integer,								-- facture origin if credit notes or replacement invoice
   fk_projet				integer DEFAULT NULL,					-- projet auquel est associee la facture
 
   fk_account			integer,								-- bank account
diff --git a/htdocs/langs/en_US/other.lang b/htdocs/langs/en_US/other.lang
index a37927566e3..11a09af3373 100644
--- a/htdocs/langs/en_US/other.lang
+++ b/htdocs/langs/en_US/other.lang
@@ -9,6 +9,18 @@ BirthdayDate=Birthday date
 DateToBirth=Date of birth
 BirthdayAlertOn=birthday alert active
 BirthdayAlertOff=birthday alert inactive
+TransKey=Translation of the key TransKey
+MonthOfInvoice=Month (number 1-12) of invoice date
+TextMonthOfInvoice=Month (tex) of invoice date
+PreviousMonthOfInvoice=Previous month (number 1-12) of invoice date
+TextPreviousMonthOfInvoice=Previous month (text) of invoice date
+NextMonthOfInvoice=Following month (number 1-12) of invoice date
+TextNextMonthOfInvoice=Following month (text) of invoice date
+
+YearOfInvoice=Year of invoice date
+PreviousYearOfInvoice=Previous year of invoice date
+NextYearOfInvoice=Following year of invoice date
+
 Notify_FICHINTER_ADD_CONTACT=Added contact to Intervention
 Notify_FICHINTER_VALIDATE=Intervention validated
 Notify_FICHINTER_SENTBYMAIL=Intervention sent by mail
-- 
GitLab