From c6dcccba18a447d7a3314e498df1e8cc733d8051 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 15 Jul 2017 14:55:04 +0200
Subject: [PATCH] Fix better warning when record will be saved into a waiting
 account.

---
 htdocs/accountancy/bookkeeping/list.php       | 12 ++--
 htdocs/accountancy/journal/bankjournal.php    | 68 ++++++++++++-------
 .../tables/llx_accounting_bookkeeping.key.sql |  2 +
 htdocs/langs/en_US/accountancy.lang           |  4 +-
 4 files changed, 56 insertions(+), 30 deletions(-)

diff --git a/htdocs/accountancy/bookkeeping/list.php b/htdocs/accountancy/bookkeeping/list.php
index 0d9832cb802..cc4629e4ff9 100644
--- a/htdocs/accountancy/bookkeeping/list.php
+++ b/htdocs/accountancy/bookkeeping/list.php
@@ -377,28 +377,28 @@ print '<tr class="liste_titre_filter">';
 print '<td class="liste_titre"><input type="text" name="search_mvt_num" size="6" value="' . dol_escape_htmltag($search_mvt_num) . '"></td>';
 print '<td class="liste_titre center">';
 print '<div class="nowrap">';
-print $langs->trans('From') . ': ';
+print $langs->trans('From') . ' ';
 print $form->select_date($search_date_start, 'date_start', 0, 0, 1);
 print '</div>';
 print '<div class="nowrap">';
-print $langs->trans('to') . ': ';
+print $langs->trans('to') . ' ';
 print $form->select_date($search_date_end, 'date_end', 0, 0, 1);
 print '</div>';
 print '</td>';
 print '<td class="liste_titre"><input type="text" name="search_doc_ref" size="8" value="' . dol_escape_htmltag($search_doc_ref) . '"></td>';
 print '<td class="liste_titre">';
 print '<div class="nowrap">';
-print $langs->trans('From');
+print $langs->trans('From').' ';
 print $formaccounting->select_account($search_accountancy_code_start, 'search_accountancy_code_start', 1, array (), 1, 1, 'maxwidth200');
 print '</div>';
 print '<div class="nowrap">';
-print $langs->trans('to');
+print $langs->trans('to').' ';
 print $formaccounting->select_account($search_accountancy_code_end, 'search_accountancy_code_end', 1, array (), 1, 1, 'maxwidth200');
 print '</div>';
 print '</td>';
 print '<td class="liste_titre">';
 print '<div class="nowrap">';
-print $langs->trans('From');
+print $langs->trans('From').' ';
 // TODO For the moment we keep a fre input text instead of a combo. The select_auxaccount has problem because it does not
 // use setup of keypress to select thirdparty and this hang browser on large database.
 if (! empty($conf->global->ACCOUNTANCY_COMBO_FOR_AUX))
@@ -411,7 +411,7 @@ else
 }
 print '</div>';
 print '<div class="nowrap">';
-print $langs->trans('to');
+print $langs->trans('to').' ';
 // TODO For the moment we keep a fre input text instead of a combo. The select_auxaccount has problem because it does not
 // use setup of keypress to select thirdparty and this hang browser on large database.
 if (! empty($conf->global->ACCOUNTANCY_COMBO_FOR_AUX))
diff --git a/htdocs/accountancy/journal/bankjournal.php b/htdocs/accountancy/journal/bankjournal.php
index 8ef93d5b842..132b02356d7 100644
--- a/htdocs/accountancy/journal/bankjournal.php
+++ b/htdocs/accountancy/journal/bankjournal.php
@@ -118,6 +118,7 @@ if ($in_bookkeeping == 'already')
 if ($in_bookkeeping == 'notyet')
 	$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";
+//print $sql;
 
 $object = new Account($db);
 $paymentstatic = new Paiement($db);
@@ -158,10 +159,10 @@ if ($result) {
 	$tabtp = array ();
 	$tabtype = array ();
 
-	// Loop on each line into bank account. For each line, we should get:
-	// on line tabpay = line into bank
-	// one line for bank jounral = tabbq
-	// one line for thirdparty journal = tabtp
+	// Loop on each line into llx_bank table. For each line, we should get:
+	// one line tabpay = line into bank
+	// one line for bank record = tabbq
+	// one line for thirdparty record = tabtp
 	$i = 0;
 	while ( $i < $num )
 	{
@@ -202,11 +203,16 @@ if ($result) {
 		}
 		$links = $object->get_url($obj->rowid);
 
-		/*var_dump($i);
-		var_dump($links);*/
+		//var_dump($i);
+		//var_dump($tabpay);
+
+		// By default
+		$tabpay[$obj->rowid]['type'] = 'unknown';	// Can be SOLD, miscellaneous entry, payment of patient, or old record with no links in bank_url.
+		$tabtype[$obj->rowid] = 'unknown';
 
 		// get_url may return -1 which is not traversable
 		if (is_array($links) && count($links) > 0) {
+
 			// Now loop on each link of record in bank.
 			foreach ($links as $key => $val) {
 
@@ -218,6 +224,14 @@ if ($result) {
 					$tabpay[$obj->rowid]['type'] = $links[$key]['type'];
 					$tabtype[$obj->rowid] = $links[$key]['type'];
 				}
+				elseif (in_array($links[$key]['type'], array('company', 'user')))
+				{
+					if ($tabpay[$obj->rowid]['type'] == 'unknown')
+					{
+						// We can guess here it is a bank record for a company or a user.
+						// But we won't be able to record somewhere else than into a waiting account, because there is no other journal to record the contreparty.
+					}
+				}
 
 				if ($links[$key]['type'] == 'payment') {
 					$paymentstatic->id = $links[$key]['url_id'];
@@ -306,10 +320,6 @@ if ($result) {
 				}
 			}
 		}
-		else
-		{
-			$tabpay[$obj->rowid]['type'] = 'unknown';	// Can be SOLD, miscellaneous entry, payment of patient, or old record with no links in bank_url.
-		}
 
 		$tabbq[$obj->rowid][$compta_bank] += $obj->amount;
 
@@ -586,13 +596,16 @@ if (! $error && $action == 'writebookkeeping') {
 						$bookkeeping->label_compte = '';
 						$bookkeeping->doc_ref = '';
 					} else {
-						// Temporary account
-						$bookkeeping->label_operation = '';
-						$bookkeeping->subledger_account = '';
-						$bookkeeping->subledger_label = '';
-						$bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_SUSPENSE;
-						$bookkeeping->label_compte = '';
-						$bookkeeping->doc_ref = $k;
+						if ($tabtype[$key] == 'unknown')	// Unknown transaction, we will use a waiting account for thirdparty.
+						{
+							// Temporary account
+							$bookkeeping->label_operation = '';
+							$bookkeeping->subledger_account = '';
+							$bookkeeping->subledger_label = '';
+							$bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_SUSPENSE;
+							$bookkeeping->label_compte = '';
+							$bookkeeping->doc_ref = $k;
+						}
 					}
 
 					$result = $bookkeeping->create($user);
@@ -1054,12 +1067,21 @@ if (empty($action) || $action == 'view') {
 					$accounttoshow = length_accounta($account_ledger);
 					if (empty($accounttoshow) || $accounttoshow == 'NotDefined')
 					{
-						$errorstring='ThirdpartyDefaultAccountNotDefined';
-						if ($tabtype[$key] == 'payment') $errorstring='MainAccountForCustomersNotDefined';
-						if ($tabtype[$key] == 'payment_supplier') $errorstring='MainAccountForSuppliersNotDefined';
-						if ($tabtype[$key] == 'payment_expensereport') $errorstring='MainAccountForUsersNotDefined';
-						if ($tabtype[$key] == 'payment_salary') $errorstring='MainAccountForUsersNotDefined';
-						print '<span class="error">'.$langs->trans($errorstring).'</span>';
+						if ($tabtype[$key] == 'unknown')
+						{
+							// We will accept writing, but into a waiting account
+							print '<span class="warning">'.$langs->trans('UnknownAccountForThirdparty', length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE)).'</span>';	// We will a waiting account
+						}
+						else
+						{
+							// We will refuse writing
+							$errorstring='UnknownAccountForThirdpartyBlocking';
+							if ($tabtype[$key] == 'payment') $errorstring='MainAccountForCustomersNotDefined';
+							if ($tabtype[$key] == 'payment_supplier') $errorstring='MainAccountForSuppliersNotDefined';
+							if ($tabtype[$key] == 'payment_expensereport') $errorstring='MainAccountForUsersNotDefined';
+							if ($tabtype[$key] == 'payment_salary') $errorstring='MainAccountForUsersNotDefined';
+							print '<span class="error">'.$langs->trans($errorstring).'</span>';
+						}
 					}
 					else print $accounttoshow;
 					print "</td>";
diff --git a/htdocs/install/mysql/tables/llx_accounting_bookkeeping.key.sql b/htdocs/install/mysql/tables/llx_accounting_bookkeeping.key.sql
index 1fc77301528..8e921a9964c 100644
--- a/htdocs/install/mysql/tables/llx_accounting_bookkeeping.key.sql
+++ b/htdocs/install/mysql/tables/llx_accounting_bookkeeping.key.sql
@@ -20,3 +20,5 @@ ALTER TABLE llx_accounting_bookkeeping ADD INDEX idx_accounting_bookkeeping_doc_
 ALTER TABLE llx_accounting_bookkeeping ADD INDEX idx_accounting_bookkeeping_fk_docdet (fk_docdet);
 ALTER TABLE llx_accounting_bookkeeping ADD INDEX idx_accounting_bookkeeping_numero_compte (numero_compte);
 ALTER TABLE llx_accounting_bookkeeping ADD INDEX idx_accounting_bookkeeping_code_journal (code_journal);
+
+-- TODO Add a key for unicity
\ No newline at end of file
diff --git a/htdocs/langs/en_US/accountancy.lang b/htdocs/langs/en_US/accountancy.lang
index 8cfdbed4728..72fa8d0c191 100644
--- a/htdocs/langs/en_US/accountancy.lang
+++ b/htdocs/langs/en_US/accountancy.lang
@@ -51,7 +51,7 @@ AccountancyAreaDescExpenseReport=STEP %s: Define default accounting accounts for
 AccountancyAreaDescSal=STEP %s: Define default accounting accounts for payment of salaries. For this, use the menu entry %s.    
 AccountancyAreaDescContrib=STEP %s: Define default accounting accounts for special expences (miscellaneous taxes). For this, use the menu entry %s.    
 AccountancyAreaDescDonation=STEP %s: Define default accounting accounts for donation. For this, use the menu entry %s.
-AccountancyAreaDescMisc=STEP %s: Define default accounting accounts for miscellaneous transactions. For this, use the menu entry %s.
+AccountancyAreaDescMisc=STEP %s: Define mandatory default account and default accounting accounts for miscellaneous transactions. For this, use the menu entry %s.
 AccountancyAreaDescLoan=STEP %s: Define default accounting accounts for loans. For this, use the menu entry %s. 
 AccountancyAreaDescBank=STEP %s: Define accounting accounts for each bank and financial accounts. For this, go on the card of each financial account. You can start from page %s.
 AccountancyAreaDescProd=STEP %s: Define accounting accounts on your products/services. For this, use the menu entry %s.
@@ -176,6 +176,8 @@ AddCompteFromBK=Add accounting accounts to the group
 ReportThirdParty=List third party account
 DescThirdPartyReport=Consult here the list of the third party customers and suppliers and their accounting accounts
 ListAccounts=List of the accounting accounts
+UnknownAccountForThirdparty=Unknown third party account. We will use %s.
+UnknownAccountForThirdpartyBlocking=Unknown third party account. Blocking error.
 
 Pcgtype=Class of account
 Pcgsubtype=Subclass of account
-- 
GitLab