From f8bc456807d3eb8ddda833aadd5840000def615a Mon Sep 17 00:00:00 2001
From: aspangaro <alexandre.spangaro@gmail.com>
Date: Sun, 3 Apr 2016 15:32:46 +0200
Subject: [PATCH] FIX: Accountancy - Update balance to add description of the
 accounting account + bugfix

---
 htdocs/accountancy/bookkeeping/balance.php    | 42 +++++++++--------
 .../accountancy/class/bookkeeping.class.php   | 46 ++++++++++++++++++-
 htdocs/core/lib/accounting.lib.php            | 12 ++---
 3 files changed, 73 insertions(+), 27 deletions(-)

diff --git a/htdocs/accountancy/bookkeeping/balance.php b/htdocs/accountancy/bookkeeping/balance.php
index 3e4b69d6624..5038c93fc82 100644
--- a/htdocs/accountancy/bookkeeping/balance.php
+++ b/htdocs/accountancy/bookkeeping/balance.php
@@ -1,7 +1,7 @@
 <?php
-/* Copyright (C) 2013-2014 Olivier Geffroy		<jeff@jeffinfo.com>
- * Copyright (C) 2013-2016 Florian Henry		<florian.henry@open-concept.pro>
- * Copyright (C) 2013-2015 Alexandre Spangaro	<aspangaro.dolibarr@gmail.com>
+/* Copyright (C) 2016 		Olivier Geffroy		<jeff@jeffinfo.com>
+ * Copyright (C) 2016 		Florian Henry		<florian.henry@open-concept.pro>
+ * Copyright (C) 2016 		Alexandre Spangaro	<aspangaro.dolibarr@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,9 +19,9 @@
  */
 
 /**
- * \file htdocs/accountancy/bookkeeping/balance.php
- * \ingroup Accounting Expert
- * \brief Balance of book keeping
+ *  \file 		htdocs/accountancy/bookkeeping/balance.php
+ *  \ingroup 	Advanced accountancy
+ *  \brief 		Balance of book keeping
  */
 require '../../main.inc.php';
 
@@ -137,7 +137,6 @@ else {
 	/*
 	 * List
 	 */
-
 	$nbtotalofrecords = 0;
 	if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
 		$nbtotalofrecords = $object->fetchAllBalance($sortorder, $sortfield, 0, 0, $filter);
@@ -150,13 +149,12 @@ else {
 	if ($result < 0) {
 		setEventMessages($object->error, $object->errors, 'errors');
 	}
-
-	print_barre_liste($title_page, $page, $_SERVER["PHP_SELF"], $options, $sortfield, $sortorder, '', $result, $nbtotalofrecords);
-
+	
+	print_barre_liste($title_page, $page, $_SERVER["PHP_SELF"], $options, $sortfield, $sortorder, '', $result);
+	
 	print '<form method="GET" id="searchFormList" action="' . $_SERVER["PHP_SELF"] . '">';
 	print '<div class="tabsAction">' . "\n";
 	print '<div class="inline-block divButAction"><input type="submit" name="button_export_csv" class="butAction" value="' . $langs->trans("Export") . '" /></div>';
-
 	print '</div>';
 
 	print '<div class="liste_titre">';
@@ -169,6 +167,7 @@ else {
 	print '<table class="noborder" width="100%">';
 	print '<tr class="liste_titre">';
 	print_liste_field_titre($langs->trans("Numerocompte"), $_SERVER['PHP_SELF'], "t.numero_compte", "", $options, "", $sortfield, $sortorder);
+	print_liste_field_titre($langs->trans("Labelcompte"), $_SERVER['PHP_SELF'], "t.label_compte", "", $options, "", $sortfield, $sortorder);
 	print_liste_field_titre($langs->trans("Debit"), $_SERVER['PHP_SELF'], "t.debit", "", $options, 'align="right"', $sortfield, $sortorder);
 	print_liste_field_titre($langs->trans("Credit"), $_SERVER['PHP_SELF'], "t.credit", "", $options, 'align="right"', $sortfield, $sortorder);
 	print_liste_field_titre($langs->trans("Solde"), $_SERVER["PHP_SELF"], "", $options, "", 'width="60" align="center"', $sortfield, $sortorder);
@@ -176,11 +175,11 @@ else {
 	print "</tr>\n";
 
 	print '<tr class="liste_titre">';
-	print '<td>';
+	print '<td colspan="2">';
 	print $langs->trans('From');
 	print $formventilation->select_account($search_accountancy_code_start, 'search_accountancy_code_start', 1, array (), 1, 1, '');
 	print '<br>';
-	print $langs->trans('To');
+	print $langs->trans('to');
 	print $formventilation->select_account($search_accountancy_code_end, 'search_accountancy_code_end', 1, array (), 1, 1, '');
 	print '</td>';
 
@@ -203,23 +202,28 @@ else {
 
 	foreach ( $object->lines as $line ) {
 		$var = ! $var;
-
+		$link = '';
 		$total_debit += $line->debit;
 		$total_credit += $line->credit;
-
+		$description = $object->get_compte_desc($line->numero_compte); // Search description of the account
+		if(empty($description)){
+			$link = '<a href="../admin/card.php?action=create&compte=' . length_accountg($line->numero_compte) . '">' . img_edit_add() .'</a>';
+		}
 		print "<tr $bc[$var]>";
 
 		print '<td>' . length_accountg($line->numero_compte) . '</td>';
-		print '<td align="right">' . price($line->debit) . '</td>';
-		print '<td align="right">' . price($line->credit) . '</td>';
-		print '<td align="right">' . price($line->credit - $line->debit) . '</td>';
-		print '<td align="center">';
+		print '<td>' . $description . '</td>';
+		print '<td align="right">' .  number_format($line->debit, 2, ',', ' ') . '</td>';
+        print '<td align="right">' . number_format($line->credit, 2, ',', ' ') . '</td>';
+        print '<td align="right">' . number_format($line->credit - $line->debit, 2, ',', ' ') . '</td>';
+		print '<td align="center">' . $link;
 		print '</td>';
 		print "</tr>\n";
 	}
 
 	print '<tr class="liste_total">';
 	print '<td></td>';
+	print '<td></td>';
 	print '<td  align="right">';
 	print price($total_debit);
 	print '</td>';
diff --git a/htdocs/accountancy/class/bookkeeping.class.php b/htdocs/accountancy/class/bookkeeping.class.php
index 4a0ebbe8988..ebf2b1d8f20 100644
--- a/htdocs/accountancy/class/bookkeeping.class.php
+++ b/htdocs/accountancy/class/bookkeeping.class.php
@@ -1,7 +1,7 @@
-<?php
+<?php
 /* Copyright (C) 2007-2012  Laurent Destailleur <eldy@users.sourceforge.net>
  * Copyright (C) 2014       Juanjo Menent       <jmenent@2byte.es>
- * Copyright (C) 2015-2016 Florian Henry       <florian.henry@open-concept.pro>
+ * Copyright (C) 2015-2016  Florian Henry       <florian.henry@open-concept.pro>
  * Copyright (C) 2015       Raphaël Doursenaud  <rdoursenaud@gpcsolutions.fr>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -1207,6 +1207,48 @@ class BookKeeping extends CommonObject
 			return - 1;
 		}
 	}
+	
+	/**
+	* Description of accounting account
+	*
+	* @param 	string 	$account	Accounting account
+	* @return 	string 	
+	*/
+	function get_compte_desc($account = null)
+	{	
+		global $conf;
+		$pcgver = $conf->global->CHARTOFACCOUNTS;
+
+		$sql  = "SELECT aa.account_number, aa.label, aa.rowid, aa.fk_pcg_version, cat.label as category";
+		$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa ";
+		$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
+		$sql .= " AND aa.account_number = '" . $account . "'";
+		$sql .= " AND asy.rowid = " . $pcgver;
+		$sql .= " AND aa.active = 1";
+		$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_accounting_category as cat ON aa.fk_accounting_category = cat.rowid";
+
+		dol_syslog(get_class($this) . "::select_account sql=" . $sql, LOG_DEBUG);
+		$resql = $this->db->query($sql);
+		if ($resql) {
+			$obj = '';
+			if ($this->db->num_rows($resql)) {
+				$obj = $this->db->fetch_object($resql);	
+			}
+			
+			if(empty($obj->category)){				
+				return $obj->label;
+			}else{
+				return $obj->label.' ('.$obj->category.')';
+				
+			}
+		} else {
+			$this->error = "Error " . $this->db->lasterror();
+			dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR);
+
+			return -1;
+		}
+	}
+	
 }
 
 /**
diff --git a/htdocs/core/lib/accounting.lib.php b/htdocs/core/lib/accounting.lib.php
index 502e83010f7..9c750719102 100644
--- a/htdocs/core/lib/accounting.lib.php
+++ b/htdocs/core/lib/accounting.lib.php
@@ -1,6 +1,6 @@
 <?php
 /* Copyright (C) 2013-2014 Olivier Geffroy      <jeff@jeffinfo.com>
- * Copyright (C) 2013-2014 Alexandre Spangaro   <aspangaro.dolibarr@gmail.com>
+ * Copyright (C) 2013-2016 Alexandre Spangaro   <aspangaro.dolibarr@gmail.com>
  * Copyright (C) 2014 	   Florian Henry        <florian.henry@open-concept.pro>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -18,9 +18,9 @@
  */
 
 /**
- * 		\file			htdocs/core/lib/account.lib.php
- * 		\ingroup		Accounting Expert
- * 		\brief			Library of accountancy functions
+ * 	\file		htdocs/core/lib/accounting.lib.php
+ * 	\ingroup	Advanced accountancy
+ * 	\brief		Library of accountancy functions
  */
 
 /**
@@ -120,7 +120,7 @@ function length_accountg($account)
 		// Clean parameters
 		$i = strlen($account);
 
-		if ($i >= 2) {
+		if ($i >= 1) {
 			while ( $i < $g ) {
 				$account .= '0';
 
@@ -152,7 +152,7 @@ function length_accounta($accounta)
 		// Clean parameters
 		$i = strlen($accounta);
 
-		if ($i >= 2) {
+		if ($i >= 1) {
 			while ( $i < $a ) {
 				$accounta .= '0';
 
-- 
GitLab