From 6489cd61642b641b07972ef2f8159722de0cd73d Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Fri, 7 Oct 2016 12:10:16 +0200
Subject: [PATCH] Debug accounting module

---
 htdocs/accountancy/admin/account.php          | 63 ++++++++++---------
 htdocs/accountancy/admin/card.php             | 27 +++++---
 .../class/accountingaccount.class.php         | 17 +++--
 htdocs/core/class/smtps.class.php             |  5 ++
 htdocs/langs/en_US/accountancy.lang           |  4 +-
 5 files changed, 69 insertions(+), 47 deletions(-)

diff --git a/htdocs/accountancy/admin/account.php b/htdocs/accountancy/admin/account.php
index ec9f2b6ff58..c59d565e690 100644
--- a/htdocs/accountancy/admin/account.php
+++ b/htdocs/accountancy/admin/account.php
@@ -22,9 +22,8 @@
  * \ingroup     Advanced accountancy
  * \brief		List accounting account
  */
-require '../../main.inc.php';
 
-// Class
+require '../../main.inc.php';
 require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/html.formventilation.class.php';
@@ -35,6 +34,7 @@ $langs->load("accountancy");
 
 $mesg = '';
 $action = GETPOST('action');
+$cancel = GETPOST('cancel');
 $id = GETPOST('id', 'int');
 $rowid = GETPOST('rowid', 'int');
 $search_account = GETPOST("search_account");
@@ -80,34 +80,39 @@ $parameters=array();
 $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action);    // Note that $action and $object may have been modified by some hooks
 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
 
-include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
-
-if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") ||GETPOST("button_removefilter")) // All test are required to be compatible with all browsers
+if (empty($reshook))
 {
-	$search_account = "";
-	$search_label = "";
-	$search_accountparent = "";
-	$search_pcgtype = "";
-	$search_pcgsubtype = "";
-}
-
-if ($action == 'disable') {
-	if ($accounting->fetch($id)) {
-		$result = $accounting->account_desactivate($id);
-	}
-	
-	$action = 'update';
-	if ($result < 0) {
-		setEventMessages($accounting->error, $accounting->errors, 'errors');
-	}
-} else if ($action == 'enable') {
-	if ($accounting->fetch($id)) {
-		$result = $accounting->account_activate($id);
-	}
-	$action = 'update';
-	if ($result < 0) {
-		setEventMessages($accounting->error, $accounting->errors, 'errors');
-	}
+    if (! empty($cancel)) $action = '';
+    
+    include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
+    
+    if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") ||GETPOST("button_removefilter")) // All test are required to be compatible with all browsers
+    {
+    	$search_account = "";
+    	$search_label = "";
+    	$search_accountparent = "";
+    	$search_pcgtype = "";
+    	$search_pcgsubtype = "";
+    }
+    
+    if ($action == 'disable') {
+    	if ($accounting->fetch($id)) {
+    		$result = $accounting->account_desactivate($id);
+    	}
+    	
+    	$action = 'update';
+    	if ($result < 0) {
+    		setEventMessages($accounting->error, $accounting->errors, 'errors');
+    	}
+    } else if ($action == 'enable') {
+    	if ($accounting->fetch($id)) {
+    		$result = $accounting->account_activate($id);
+    	}
+    	$action = 'update';
+    	if ($result < 0) {
+    		setEventMessages($accounting->error, $accounting->errors, 'errors');
+    	}
+    }
 }
 
 
diff --git a/htdocs/accountancy/admin/card.php b/htdocs/accountancy/admin/card.php
index 934e5f1f081..ec41611a4cd 100644
--- a/htdocs/accountancy/admin/card.php
+++ b/htdocs/accountancy/admin/card.php
@@ -22,9 +22,8 @@
  * \ingroup     Advanced accountancy
  * \brief 		Card of accounting account
  */
-require '../../main.inc.php';
 
-// Class
+require '../../main.inc.php';
 require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/html.formventilation.class.php';
@@ -47,7 +46,11 @@ $cancel = GETPOST('cancel');
 
 $object = new AccountingAccount($db);
 
-// Action
+
+/*
+ * Action
+ */
+
 if ($action == 'add' && $user->rights->accounting->chartofaccount)
 {
 	if (! $cancel) {
@@ -85,18 +88,28 @@ if ($action == 'add' && $user->rights->accounting->chartofaccount)
 		$object->active = 1;
 		
 		$res = $object->create($user);
-		
 		if ($res == - 3) {
 			$error = 1;
 			$action = "create";
+			setEventMessages($object->error, $object->errors, 'errors');
 		}
-		if ($res == - 4) {
+		elseif ($res == - 4) {
 			$error = 2;
 			$action = "create";
+			setEventMessages($object->error, $object->errors, 'errors');
+		}
+		elseif ($res < 0)
+		{
+		    $error++;
+		    setEventMessages($object->error, $object->errors, 'errors');
+		    $action = "create";
+		}
+		if (! $error)
+		{
+		    header("Location: account.php");
+		    exit;
 		}
 	}
-	header("Location: account.php");
-	exit;
 } else if ($action == 'edit' && $user->rights->accounting->chartofaccount) {
 	if (! $cancel) {
 		$result = $object->fetch($id);
diff --git a/htdocs/accountancy/class/accountingaccount.class.php b/htdocs/accountancy/class/accountingaccount.class.php
index 00a16a227e3..b63d7ce2fda 100644
--- a/htdocs/accountancy/class/accountingaccount.class.php
+++ b/htdocs/accountancy/class/accountingaccount.class.php
@@ -172,16 +172,15 @@ class AccountingAccount extends CommonObject
 		
 		$sql .= " '" . $this->db->idate($now) . "'";
 		$sql .= ", " . $conf->entity;
-		$sql .= ", " . (! isset($this->fk_pcg_version) ? 'NULL' : "'" . $this->db->escape($this->fk_pcg_version) . "'");
-		$sql .= ", " . (! isset($this->pcg_type) ? 'NULL' : "'" . $this->db->escape($this->pcg_type) . "'");
-		$sql .= ", " . (! isset($this->pcg_subtype) ? 'NULL' : "'" . $this->pcg_subtype . "'");
-		$sql .= ", " . (! isset($this->account_number) ? 'NULL' : "'" . $this->account_number . "'");
-		$sql .= ", " . (! isset($this->account_parent) ? 'NULL' : "'" . $this->db->escape($this->account_parent) . "'");
-		$sql .= ", " . (! isset($this->label) ? 'NULL' : "'" . $this->db->escape($this->label) . "'");
-		$sql .= ", " . (! isset($this->account_category) ? 'NULL' : "'" . $this->db->escape($this->account_category) . "'");
+		$sql .= ", " . (empty($this->fk_pcg_version) ? 'NULL' : "'" . $this->db->escape($this->fk_pcg_version) . "'");
+		$sql .= ", " . (empty($this->pcg_type) ? 'NULL' : "'" . $this->db->escape($this->pcg_type) . "'");
+		$sql .= ", " . (empty($this->pcg_subtype) ? 'NULL' : "'" . $this->pcg_subtype . "'");
+		$sql .= ", " . (empty($this->account_number) ? 'NULL' : "'" . $this->account_number . "'");
+		$sql .= ", " . (empty($this->account_parent) ? 'NULL' : "'" . $this->db->escape($this->account_parent) . "'");
+		$sql .= ", " . (empty($this->label) ? 'NULL' : "'" . $this->db->escape($this->label) . "'");
+		$sql .= ", " . (empty($this->account_category) ? 'NULL' : "'" . $this->db->escape($this->account_category) . "'");
 		$sql .= ", " . $user->id;
-		$sql .= ", " . (! isset($this->active) ? 'NULL' : "'" . $this->db->escape($this->active) . "'");
-		
+		$sql .= ", " . (! isset($this->active) ? 'NULL' : $this->db->escape($this->active));
 		$sql .= ")";
 		
 		$this->db->begin();
diff --git a/htdocs/core/class/smtps.class.php b/htdocs/core/class/smtps.class.php
index 83e30a05413..39a45ad783b 100644
--- a/htdocs/core/class/smtps.class.php
+++ b/htdocs/core/class/smtps.class.php
@@ -396,6 +396,11 @@ class SMTPs
 		$host=$this->getHost();
 		$host=preg_replace('@tcp://@i','',$host);	// Remove prefix
 		$host=preg_replace('@ssl://@i','',$host);	// Remove prefix
+		if (!empty($conf->global->MAIN_MAIL_EMAIL_STARTTLS))
+		{
+		    $host=preg_replace('@tls://@i','',$host);	// Remove prefix
+		    $host='tls://'.$host;
+		}
 		if ( $_retVal = $this->socket_send_str('EHLO ' . $host, '250') )
 		{
 			if (!empty($conf->global->MAIN_MAIL_EMAIL_STARTTLS))
diff --git a/htdocs/langs/en_US/accountancy.lang b/htdocs/langs/en_US/accountancy.lang
index ab9877d38b1..a37389623b4 100644
--- a/htdocs/langs/en_US/accountancy.lang
+++ b/htdocs/langs/en_US/accountancy.lang
@@ -16,8 +16,8 @@ BackToChartofaccounts=Return chart of accounts
 
 AccountancyArea=Accountancy area
 AccountancyAreaDescIntro=Usage of the accountancy module is done in several step:
-AccountancyAreaDescActionOnce=The following action are usually executed one time only, or once per year...
-AccountancyAreaDescActionFreq=The following action are usually executed every month, week or day for very large companies...
+AccountancyAreaDescActionOnce=The following actions are usually executed one time only, or once per year...
+AccountancyAreaDescActionFreq=The following actions are usually executed every month, week or day for very large companies...
 AccountancyAreaDescChart=STEP %s: Create or check your chart of account from menu %s
 AccountancyAreaDescProd=STEP %s: Check the binding between products/services and accounting account is done. Complete missing bindings. This will save you time in future for the next steps by suggesting you the correct default accounting account on your invoice lines.<br>For this you can use the menu entry %s.
 AccountancyAreaDescBank=STEP %s: Check the binding between bank accounts and accounting account is done. Complete missing bindings. This will save you time in future for the next steps by suggesting you the correct default accounting account on your payment lines.<br>For this, go on the card of each financial account. You can start from page %s.
-- 
GitLab