From 3148841ecf47e1047f8509e31b0e15db9506033b Mon Sep 17 00:00:00 2001
From: Alexandre SPANGARO <alexandre.spangaro@gmail.com>
Date: Sun, 26 Mar 2017 21:26:05 +0200
Subject: [PATCH] Fix : integration of expense report in accounatncy bank
 journal

---
 htdocs/accountancy/journal/bankjournal.php | 90 ++++++++++++++++++----
 1 file changed, 77 insertions(+), 13 deletions(-)

diff --git a/htdocs/accountancy/journal/bankjournal.php b/htdocs/accountancy/journal/bankjournal.php
index 4aab9a09fcc..012a773b875 100644
--- a/htdocs/accountancy/journal/bankjournal.php
+++ b/htdocs/accountancy/journal/bankjournal.php
@@ -4,7 +4,7 @@
  * Copyright (C) 2011		Juanjo Menent		<jmenent@2byte.es>
  * Copyright (C) 2012		Regis Houssin		<regis@dolibarr.fr>
  * Copyright (C) 2013		Christophe Battarel	<christophe.battarel@altairis.fr>
- * Copyright (C) 2013-2016	Alexandre Spangaro	<aspangaro.dolibarr@gmail.com>
+ * Copyright (C) 2013-2017	Alexandre Spangaro	<aspangaro@zendsi.com>
  * Copyright (C) 2013-2014	Florian Henry		<florian.henry@open-concept.pro>
  * Copyright (C) 2013-2014	Olivier Geffroy		<jeff@jeffinfo.com>
  *
@@ -48,6 +48,9 @@ require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
 require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.class.php';
 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php';
 require_once DOL_DOCUMENT_ROOT . '/societe/class/client.class.php';
+require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php';
+require_once DOL_DOCUMENT_ROOT . '/expensereport/class/paymentexpensereport.class.php';
+
 
 // Langs
 $langs->load("companies");
@@ -57,6 +60,8 @@ $langs->load("banks");
 $langs->load('bills');
 $langs->load('donations');
 $langs->load("accountancy");
+$langs->load("trips");
+$langs->load("hrm");
 
 $id_bank_account = GETPOST('id_account', 'int');
 
@@ -103,11 +108,14 @@ $idpays = $p[0];
 
 $sql  = "SELECT b.rowid , b.dateo as do, b.datev as dv, b.amount, b.label, b.rappro, b.num_releve, b.num_chq, b.fk_type,";
 $sql .= " ba.courant, ba.ref as baref, ba.account_number,";
-$sql .= " soc.code_compta, soc.code_compta_fournisseur, soc.rowid as socid, soc.nom as name, bu1.type as typeop";
+$sql .= " soc.code_compta, soc.code_compta_fournisseur, soc.rowid as socid, soc.nom as name, bu1.type as typeop,";
+$sql .= " u.accountancy_code, u.rowid as userid, u.lastname as name, u.firstname as firstname, bu2.type as typeop";
 $sql .= " FROM " . MAIN_DB_PREFIX . "bank as b";
 $sql .= " JOIN " . MAIN_DB_PREFIX . "bank_account as ba on b.fk_account=ba.rowid";
 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu1 ON bu1.fk_bank = b.rowid AND bu1.type='company'";
+$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu2 ON bu2.fk_bank = b.rowid AND bu2.type='user'";
 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as soc on bu1.url_id=soc.rowid";
+$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as u on bu2.url_id=u.rowid";
 $sql .= " WHERE ba.rowid=" . $id_bank_account;
 $sql .= ' AND ba.entity IN ('.getEntity('bank_account', 0).')';        // We don't share object for accountancy
 if ($date_start && $date_end)
@@ -123,6 +131,8 @@ $chargestatic = new ChargeSociales($db);
 $paymentdonstatic = new PaymentDonation($db);
 $paymentvatstatic = new TVA($db);
 $paymentsalstatic = new PaymentSalary($db);
+$paymentsalstatic = new PaymentSalary($db);
+$paymentexpensereportstatic = new PaymentExpenseReport($db);
 
 // Get code of finance journal
 $bank_code_journal = new Account($db);
@@ -143,6 +153,7 @@ if ($result) {
 	$account_transfer = (! empty($conf->global->ACCOUNTING_ACCOUNT_TRANSFER_CASH) ? $conf->global->ACCOUNTING_ACCOUNT_TRANSFER_CASH : $langs->trans("CodeNotDef"));
 
 	$tabcompany = array();
+	$tabuser = array();
 	$tabpay = array ();
 	$tabbq = array ();
 	$tabtp = array ();
@@ -169,6 +180,15 @@ if ($result) {
 				'name' => $obj->name,
 		        'code_compta' => $compta_soc,
 		);
+		
+		$compta_user = (! empty($obj->accountancy_code) ? $obj->accountancy_code : $account_employee);
+
+		$tabuser[$obj->rowid] = array (
+				'id' => $obj->userid,
+				'lastname' => $obj->lastname,
+				'firstname' => $obj->firstname,
+		        'accountancy_code' => $compta_user,
+		);
 
 		// Variable bookkeeping
 		$tabpay[$obj->rowid]["date"] = $obj->do;
@@ -187,7 +207,7 @@ if ($result) {
 		    // Now loop on each link of record in bank.
 			foreach ( $links as $key => $val ) {
 			    
-			    if (in_array($links[$key]['type'], array('sc', 'payment_sc', 'payment', 'payment_supplier', 'payment_vat')))     // So we excluded 'company' here
+			    if (in_array($links[$key]['type'], array('sc', 'payment_sc', 'payment', 'payment_supplier', 'payment_vat', 'payment_expensereport')))     // So we excluded 'company' here
 			    {
 			        // We save tabtype for a future use, to remember what kind of payment it is 
 			        $tabtype[$obj->rowid] = $links[$key]['type'];
@@ -211,7 +231,7 @@ if ($result) {
 					$userstatic->id = $links[$key]['url_id'];
 					$userstatic->name = $links[$key]['label'];
 					$tabpay[$obj->rowid]["soclib"] = $userstatic->getNomUrl(1, '', 30);
-					// $tabtp[$obj->rowid][$compta_user] += $obj->amount;
+					$tabtp[$obj->rowid][$compta_user] += $obj->amount;
 				} else if ($links[$key]['type'] == 'sc') {
 					$chargestatic->id = $links[$key]['url_id'];
 					$chargestatic->ref = $links[$key]['url_id'];
@@ -256,7 +276,12 @@ if ($result) {
 					$paymentsalstatic->ref = $links[$key]['url_id'];
 					$paymentsalstatic->label = $links[$key]['label'];
 					$tabpay[$obj->rowid]["lib"] .= ' ' . $paymentsalstatic->getNomUrl(2);
-					$tabtp[$obj->rowid][$account_employee] += $obj->amount;
+					// $tabtp[$obj->rowid][$account_employee] += $obj->amount;
+				} else if ($links[$key]['type'] == 'payment_expensereport') {
+					$paymentexpensereportstatic->id = $links[$key]['url_id'];
+					$paymentexpensereportstatic->fk_expensereport = $links[$key]['url_id'];
+					$tabpay[$obj->rowid]["lib"] .= ' ' . $paymentexpensereportstatic->getNomUrl(2);
+					$tabpay[$obj->rowid]["fk_expensereport"] = $paymentexpensereportstatic->id;
 				} else if ($links[$key]['type'] == 'banktransfert') {
 					$tabpay[$obj->rowid]["lib"] .= ' ' . $langs->trans("BankTransfer");
 					$tabtp[$obj->rowid][$account_transfer] += $obj->amount;
@@ -335,10 +360,10 @@ if (! $error && $action == 'writebookkeeping') {
     
     			if ($tabtype[$key] == 'payment') {
     			    $bookkeeping->code_tiers = $tabcompany[$key]['code_compta'];
-    			    	
+
     				$sqlmid = 'SELECT fac.facnumber';
-    				$sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture fac ";
-    				$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON  payfac.fk_facture=fac.rowid";
+    				$sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture fac";
+    				$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON payfac.fk_facture=fac.rowid";
     				$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement as pay ON  payfac.fk_paiement=pay.rowid";
     				$sqlmid .= " WHERE pay.fk_bank=" . $key;
     				dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
@@ -349,10 +374,10 @@ if (! $error && $action == 'writebookkeeping') {
     				}
     			} else if ($tabtype[$key] == 'payment_supplier') {
     			    $bookkeeping->code_tiers = $tabcompany[$key]['code_compta'];
-    			    	
+
     				$sqlmid = 'SELECT facf.ref_supplier, facf.ref';
-    				$sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf ";
-    				$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfacf ON  payfacf.fk_facturefourn=facf.rowid";
+    				$sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf";
+    				$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid";
     				$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn as payf ON  payfacf.fk_paiementfourn=payf.rowid";
     				$sqlmid .= " WHERE payf.fk_bank=" . $key;
     				dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
@@ -361,6 +386,19 @@ if (! $error && $action == 'writebookkeeping') {
     					$objmid = $db->fetch_object($resultmid);
     					$bookkeeping->doc_ref = $objmid->ref_supplier . ' (' . $objmid->ref . ')'; // Ref on invoice
     				}
+    			} else if ($tabtype[$key] == 'payment_expensereport') {
+    			    $bookkeeping->code_tiers = $tabuser[$key]['accountancy_code'];
+
+    				$sqlmid = 'SELECT e.ref';
+    				$sqlmid .= " FROM " . MAIN_DB_PREFIX . "expensereport as e";
+    				$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "payment_expensereport as payer ON payer.fk_expensereport=e.rowid";
+    				$sqlmid .= " WHERE payer.fk_expensereport=" . $val["fk_expensereport"];
+    				dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
+    				$resultmid = $db->query($sqlmid);
+    				if ($resultmid) {
+    					$objmid = $db->fetch_object($resultmid);
+    					$bookkeeping->doc_ref = $objmid->ref; // Ref of expensereport
+    				}
     			}
     
     			$result = $bookkeeping->create($user);
@@ -481,6 +519,7 @@ if ($action == 'export_csv') {
 	include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
 
 	$companystatic = new Client($db);
+	$userstatic = new User($db);
 
 	// Model Cegid Expert Export
 	if ($conf->global->ACCOUNTING_EXPORT_MODELCSV == 2)
@@ -504,12 +543,19 @@ if ($action == 'export_csv') {
 				$reflabel = $langs->trans('Donation');
 			}
 			if ($reflabel == '(SubscriptionPayment)') {
-				$reflabel = $langs->trans('Donation');
+				$reflabel = $langs->trans('Subscription');
+			}
+			if ($reflabel == '(ExpenseReportPayment)') {
+				$reflabel = $langs->trans('Employee');
 			}
 			
 			$companystatic->id = $tabcompany[$key]['id'];
 			$companystatic->name = $tabcompany[$key]['name'];
 
+			$userstatic->id = $tabuser[$key]['id'];
+			$userstatic->lastname = $tabuser[$key]['lastname'];
+			$userstatic->firstname = $tabuser[$key]['firstname'];
+
 			// Bank
 			foreach ( $tabbq[$key] as $k => $mt ) {
 				print $date . $sep;
@@ -646,6 +692,7 @@ $form = new Form($db);
 if (empty($action) || $action == 'view') {
 	$invoicestatic = new Facture($db);
 	$invoicesupplierstatic = new FactureFournisseur($db);
+	$expensereportstatic = new ExpenseReport($db);
 	
 	llxHeader('', $langs->trans("FinanceJournal"));
 
@@ -723,7 +770,10 @@ if (empty($action) || $action == 'view') {
 			$reflabel = $langs->trans('Donation');
 		}
 		if ($reflabel == '(SubscriptionPayment)') {
-			$reflabel = $langs->trans('SubscriptionPayment');
+			$reflabel = $langs->trans('Subscription');
+		}
+		if ($reflabel == '(ExpenseReportPayment)') {
+			$reflabel = $langs->trans('Employee');
 		}
 		
 		$ref=$reflabel;
@@ -755,6 +805,20 @@ if (empty($action) || $action == 'view') {
 		    }
 		    else dol_print_error($db);
 		}
+		elseif ($tabtype[$key] == 'payment_expensereport')
+		{
+		    $sqlmid = 'SELECT payer.fk_expensereport as id';
+		    $sqlmid .= " FROM " . MAIN_DB_PREFIX . "payment_expensereport as payer";
+		    $sqlmid .= " WHERE payer.fk_expensereport=" . $val["fk_expensereport"];
+		    dol_syslog("accountancy/journal/bankjournal.php::sqlmid=" . $sqlmid, LOG_DEBUG);
+		    $resultmid = $db->query($sqlmid);
+		    if ($resultmid) {
+		        $objmid = $db->fetch_object($resultmid);
+		        $expensereportstatic->fetch($objmid->id);
+		        $ref=$langs->trans("ExpenseReport").' '.$expensereportstatic->getNomUrl(1);
+		    }
+		    else dol_print_error($db);
+		}
 
 
 		/*$invoicestatic->id = $key;
-- 
GitLab