From 8146049b4d847352e438ac8d4c10f0c4f649e0a4 Mon Sep 17 00:00:00 2001
From: Darkjeff <jeff@jeffinfo.com>
Date: Tue, 27 Jun 2017 12:12:25 +0200
Subject: [PATCH] update bank journal

add in bookkeeping
add export control
correct some bug with salary
---
 htdocs/accountancy/journal/bankjournal.php | 131 +++++++--------------
 1 file changed, 42 insertions(+), 89 deletions(-)

diff --git a/htdocs/accountancy/journal/bankjournal.php b/htdocs/accountancy/journal/bankjournal.php
index bc27ebfa24f..bf4c5a45f02 100644
--- a/htdocs/accountancy/journal/bankjournal.php
+++ b/htdocs/accountancy/journal/bankjournal.php
@@ -72,10 +72,10 @@ $date_startyear = GETPOST('date_startyear');
 $date_endmonth = GETPOST('date_endmonth');
 $date_endday = GETPOST('date_endday');
 $date_endyear = GETPOST('date_endyear');
-$action = GETPOST('action','aZ09');
+$in_bookkeeping = GETPOST('in_bookkeeping');
 
 $now = dol_now();
-$action = GETPOST('action','aZ09');
+$action = GETPOST('action','alpha');
 
 // Security check
 if ($user->societe_id > 0 && empty($id_journal))
@@ -121,6 +121,8 @@ $sql .= " WHERE ba.fk_accountancy_journal=" . $id_journal;
 $sql .= ' AND ba.entity IN ('.getEntity('bank_account', 0).')';		// We don't share object for accountancy
 if ($date_start && $date_end)
 	$sql .= " AND b.dateo >= '" . $db->idate($date_start) . "' AND b.dateo <= '" . $db->idate($date_end) . "'";
+if ($in_bookkeeping == 'yes')    
+	$sql .= " AND (b.rowid NOT IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab  WHERE ab.doc_type='bank') )";	
 $sql .= " ORDER BY b.datev";
 
 $object = new Account($db);
@@ -366,6 +368,7 @@ if (! $error && $action == 'writebookkeeping') {
 					$bookkeeping->fk_docdet = $val["fk_bank"];
 					$bookkeeping->numero_compte = $k;
 					$bookkeeping->label_operation = $val["label"];
+					$bookkeeping->label_compte = $langs->trans("Bank");
 					$bookkeeping->montant = ($mt < 0 ? - $mt : $mt);
 					$bookkeeping->sens = ($mt >= 0) ? 'D' : 'C';
 					$bookkeeping->debit = ($mt >= 0 ? $mt : 0);
@@ -473,6 +476,17 @@ if (! $error && $action == 'writebookkeeping') {
 					$bookkeeping->date_create = $now;
 
 					if (in_array($tabtype[$key], array('sc', 'payment_sc'))) {   // If payment is payment of social contribution
+						$sqlmid = 'SELECT ch.libelle, t.libelle as labelc';
+						$sqlmid .= " FROM " . MAIN_DB_PREFIX . "chargesociales ch ";
+						$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementcharge as paych ON  paych.fk_charge=ch.rowid";
+						$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "c_chargesociales as t ON  ch.fk_type=t.id";
+						$sqlmid .= " WHERE paych.fk_bank=" . $key;
+						dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
+						$resultmid = $db->query($sqlmid);
+						if ($resultmid) {
+    					$objmid = $db->fetch_object($resultmid);
+    					$bookkeeping->label_compte = $objmid->labelc;
+    					$bookkeeping->doc_ref = $objmid->libelle ;
 						$bookkeeping->subledger_account = '';
 						$bookkeeping->numero_compte = $k;
 					} else if ($tabtype[$key] == 'payment') {	// If payment is payment of customer invoice, we get ref of invoice
@@ -597,7 +611,6 @@ if (! $error && $action == 'writebookkeeping') {
 }
 
 // Export
-/*
 if ($action == 'export_csv') {
 	$sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
 
@@ -606,14 +619,10 @@ if ($action == 'export_csv') {
 	$companystatic = new Client($db);
 	$userstatic = new User($db);
 
-	// Model Cegid Expert Export
-	if ($conf->global->ACCOUNTING_EXPORT_MODELCSV == 2)
-	{
-		$sep = ";";
-
+// Bank
 		foreach ( $tabpay as $key => $val ) {
-			$date = dol_print_date($db->jdate($val["date"]), '%d%m%Y');
-
+			$date = dol_print_date($db->jdate($val["date"]), 'day');
+			
 			$reflabel = $val["ref"];
 			if ($reflabel == '(SupplierInvoicePayment)') {
 				$reflabel = $langs->trans('Supplier');
@@ -637,88 +646,16 @@ if ($action == 'export_csv') {
 			$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;
-				print $journal . $sep;
-				print length_accountg(html_entity_decode($k)) . $sep;
-				print $sep;
-				print ($mt < 0 ? 'C' : 'D') . $sep;
-				print ($mt <= 0 ? price(- $mt) : $mt) . $sep;
-				if ($companystatic->name == '') {
-					print $langs->trans('Bank')." - ". utf8_decode($val["ref"]) . $sep;
-				} else {
-					print $langs->trans("Bank") .' - '.utf8_decode($companystatic->name) . $sep;
-				}
-				print utf8_decode($reflabel) . $sep;
-				print "\n";
-			}
-
-			// Third party
-			if (is_array($tabtp[$key])) {
-				foreach ( $tabtp[$key] as $k => $mt ) {
-					if ($mt) {
-						print $date . $sep;
-						print $journal . $sep;
-						if ($tabtype[$key] == 'payment') {
-							print length_accountg($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER) . $sep;
-							print length_accounta(html_entity_decode($k)) . $sep;
-						} else if ($tabtype[$key] == 'payment_supplier') {
-							print length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER) . $sep;
-							print length_accounta(html_entity_decode($k)) . $sep;
-						} else {
-							print length_accountg(html_entity_decode($k)) . $sep;
-							print $sep;
-						}
-						print ($mt < 0 ? 'D' : 'C') . $sep;
-						print ($mt <= 0 ? price(- $mt) : $mt) . $sep;
-						if ($companystatic->name == '') {
-							print $langs->trans('ThirdParty')." - ". utf8_decode($val["ref"]) . $sep;
-						} else {
-							print $langs->trans('ThirdParty')." - ". utf8_decode($companystatic->name) . $sep;
-						}
-						print utf8_decode($reflabel) . $sep;
-						print "\n";
-					}
-				}
-			} else {
-				foreach ( $tabbq[$key] as $k => $mt ) {
-					print $date . $sep;
-					print $journal . $sep;
-					print length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE) . $sep;
-					print $sep;
-					print ($mt < 0 ? 'D' : 'C') . $sep;
-					print ($mt <= 0 ? price(- $mt) : $mt) . $sep;
-					if ($companystatic->name == '') {
-						print $langs->trans('ThirdParty')." - ". utf8_decode($val["ref"]) . $sep;
-					} else {
-						print $langs->trans('ThirdParty')." - ". utf8_decode($companystatic->name) . $sep;
-					}
-					print utf8_decode($reflabel) . $sep;
-					print "\n";
-				}
-			}
-		}
-	} else {
-		// Model Classic Export
-		foreach ( $tabpay as $key => $val ) {
-			$date = dol_print_date($db->jdate($val["date"]), 'day');
-
-			$companystatic->id = $tabcompany[$key]['id'];
-			$companystatic->name = $tabcompany[$key]['name'];
-
 			// Bank
 			foreach ( $tabbq[$key] as $k => $mt ) {
 				print '"' . $journal . '"' . $sep;
 				print '"' . $date . '"' . $sep;
 				print '"' . $val["type_payment"] . '"' . $sep;
 				print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
+				print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
+				print "  " . $sep;
 				if ($companystatic->name == '') {
-					print '"' . $langs->trans('Bank') . " - " . utf8_decode($val["ref"]) . '"' . $sep;
+					print '"' . $langs->trans('Bank') . " - " . utf8_decode($reflabel) . '"' . $sep;
 				} else {
 					print '"' . $langs->trans("Bank") . ' - ' . utf8_decode($companystatic->name) . '"' . $sep;
 				}
@@ -734,9 +671,21 @@ if ($action == 'export_csv') {
 						print '"' . $journal . '"' . $sep;
 						print '"' . $date . '"' . $sep;
 						print '"' . $val["type_payment"] . '"' . $sep;
+						print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
+						
+						if ($tabtype[$key] == 'payment_supplier') {
+						print '"' . $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER . '"' . $sep;
+						} else if($tabtype[$key] == 'payment') {
+						print '"' . $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER . '"' . $sep;
+						} else {
+						print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
+						}
+						
+						
+						
 						print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
 						if ($companystatic->name == '') {
-							print '"' . $langs->trans('ThirdParty') . " - " . utf8_decode($val["ref"]) . '"' . $sep;
+							print '"' . $langs->trans('ThirdParty') . " - " . utf8_decode($reflabel) . '"' . $sep;
 						} else {
 							print '"' . $langs->trans('ThirdParty') . " - " . utf8_decode($companystatic->name) . '"' . $sep;
 						}
@@ -751,8 +700,10 @@ if ($action == 'export_csv') {
 					print '"' . $date . '"' . $sep;
 					print '"' . $val["ref"] . '"' . $sep;
 					print '"' . length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE) . '"' . $sep;
+					print '"' . length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE) . '"' . $sep;
+					print "  " . $sep;
 					if ($companystatic->name == '') {
-						print '"' . $langs->trans("Bank") . ' - ' . utf8_decode($val["ref"]) . '"' . $sep;
+						print '"' . $langs->trans("Bank") . ' - ' . utf8_decode($reflabel) . '"' . $sep;
 					} else {
 						print '"' . $langs->trans("Bank") . ' - ' . utf8_decode($companystatic->name) . '"' . $sep;
 					}
@@ -763,8 +714,6 @@ if ($action == 'export_csv') {
 			}
 		}
 	}
-}
-*/
 
 
 /*
@@ -802,6 +751,7 @@ if (empty($action) || $action == 'view') {
 
 	print '<div class="tabsAction tabsActionNoBottom">';
 	print '<input type="button" class="butAction" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
+	print '<input type="button" class="butAction" value="' . $langs->trans("Export") . '" onclick="launch_export();" />';
 	print '</div>';
 
 	// TODO Avoid using js. We can use a direct link with $param
@@ -863,6 +813,9 @@ if (empty($action) || $action == 'view') {
 		if ($reflabel == '(ExpenseReportPayment)') {
 			$reflabel = $langs->trans('Employee');
 		}
+		if ($reflabel == '(payment_salary)') {
+			$reflabel = $langs->trans('Employee');
+		}
 
 		$ref=$reflabel;
 		if ($tabtype[$key] == 'payment')
-- 
GitLab