From 1e3038d63462fdc202625e2f1b1ef0ede1a1a4a3 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Tue, 4 Oct 2016 13:45:22 +0200
Subject: [PATCH] Debug reports

---
 htdocs/accountancy/bookkeeping/list.php       | 24 ++++++++++-----
 .../class/accountancycategory.class.php       |  6 ++--
 htdocs/accountancy/customer/index.php         | 29 +++++++++++++++----
 htdocs/accountancy/report/result.php          |  7 +++--
 htdocs/accountancy/supplier/index.php         | 19 +++++++++++-
 htdocs/compta/resultat/clientfourn.php        | 19 ++++++++----
 htdocs/compta/resultat/index.php              | 26 ++++++++++++-----
 htdocs/compta/stats/index.php                 |  2 +-
 8 files changed, 100 insertions(+), 32 deletions(-)

diff --git a/htdocs/accountancy/bookkeeping/list.php b/htdocs/accountancy/bookkeeping/list.php
index 17bbe55158c..cf7b068c974 100644
--- a/htdocs/accountancy/bookkeeping/list.php
+++ b/htdocs/accountancy/bookkeeping/list.php
@@ -210,7 +210,6 @@ if ($action == 'delbookkeepingyearconfirm') {
 		$deljournal=0;
 	}
 
-
 	if (! empty($delyear) || ! empty($deljournal)) {
 		$result = $object->deleteByYearAndJournal($delyear,$deljournal);
 		if ($result < 0) {
@@ -219,6 +218,12 @@ if ($action == 'delbookkeepingyearconfirm') {
 		Header("Location: list.php");
 		exit;
 	}
+	else
+	{
+	    setEventMessages("NothingDeleted", null, 'warnings');
+	    Header("Location: list.php");
+	    exit;
+	}
 }
 if ($action == 'delmouvconfirm') {
 
@@ -227,7 +232,11 @@ if ($action == 'delmouvconfirm') {
 	if (! empty($mvt_num)) {
 		$result = $object->deleteMvtNum($mvt_num);
 		if ($result < 0) {
-			setEventMessages($object->error, $object->errors, 'errors');
+		    setEventMessages($object->error, $object->errors, 'errors');
+		}
+		else
+		{
+		    setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
 		}
 		Header("Location: list.php");
 		exit;
@@ -257,7 +266,8 @@ if ($action == 'export_csv') {
  * View
  */
 
-$title_page = $langs->trans("Bookkeeping") . ' ' . dol_print_date($search_date_start) . '-' . dol_print_date($search_date_end);
+$title_page = $langs->trans("Bookkeeping");
+if ($search_date_start || $search_date_end) $title_page .= ' ' . dol_print_date($search_date_start) . '-' . dol_print_date($search_date_end);
 llxHeader('', $title_page);
 
 // List
@@ -315,9 +325,9 @@ print_barre_liste($title_page, $page, $_SERVER["PHP_SELF"], $options, $sortfield
 print '<form method="GET" id="searchFormList" action="' . $_SERVER["PHP_SELF"] . '">';
 print '<div class="tabsAction">' . "\n";
 print '<div class="inline-block divButAction"><a class="butAction" href="./listbyaccount.php">' . $langs->trans("Bookkeeping") . ' ' . strtolower($langs->trans("By")) . ' ' . strtolower($langs->trans("AccountAccounting")) . '</a></div>';
-print '<div class="inline-block divButAction"><input type="submit" name="button_delmvt" class="butAction" value="' . $langs->trans("DelBookKeeping") . '" /></div>';
 print '<div class="inline-block divButAction"><a class="butAction" href="./card.php?action=create">' . $langs->trans("NewAccountingMvt") . '</a></div>';
 print '<div class="inline-block divButAction"><input type="submit" name="button_export_csv" class="butAction" value="' . $langs->trans("Export") . '" /></div>';
+print '<div class="inline-block divButAction"><input type="submit" name="button_delmvt" class="butActionDelete" value="' . $langs->trans("DelBookKeeping") . '" /></div>';
 
 print '</div>';
 
@@ -328,11 +338,11 @@ print_liste_field_titre($langs->trans("Docdate"), $_SERVER['PHP_SELF'], "t.doc_d
 print_liste_field_titre($langs->trans("Docref"), $_SERVER['PHP_SELF'], "t.doc_ref", "", $options, "", $sortfield, $sortorder);
 print_liste_field_titre($langs->trans("AccountAccountingShort"), $_SERVER['PHP_SELF'], "t.numero_compte", "", $options, "", $sortfield, $sortorder);
 print_liste_field_titre($langs->trans("Code_tiers"), $_SERVER['PHP_SELF'], "t.code_tiers", "", $options, "", $sortfield, $sortorder);
-print_liste_field_titre($langs->trans("Labelcompte"), $_SERVER['PHP_SELF'], "bk_label_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("Codejournal"), $_SERVER['PHP_SELF'], "t.code_journal", "", $options, 'align="center"', $sortfield, $sortorder);
-print_liste_field_titre($langs->trans("Action"), $_SERVER["PHP_SELF"], "", $options, "", 'width="60" align="center"', $sortfield, $sortorder);
+print_liste_field_titre($langs->trans("Codejournal"), $_SERVER['PHP_SELF'], "t.code_journal", "", $options, 'align="right"', $sortfield, $sortorder);
+print_liste_field_titre('', $_SERVER["PHP_SELF"], "", $options, "", 'width="60" align="center"', $sortfield, $sortorder);
 print "</tr>\n";
 
 print '<tr class="liste_titre">';
diff --git a/htdocs/accountancy/class/accountancycategory.class.php b/htdocs/accountancy/class/accountancycategory.class.php
index ff7f764ea31..40290bd845b 100644
--- a/htdocs/accountancy/class/accountancycategory.class.php
+++ b/htdocs/accountancy/class/accountancycategory.class.php
@@ -230,7 +230,7 @@ class AccountancyCategory
 	/**
 	 * Function to know all category from accounting account
 	 *
-	 * @return array Result in table
+	 * @return array       Result in table
 	 */
 	public function getCatsCpts() {
 		global $mysoc;
@@ -283,7 +283,7 @@ class AccountancyCategory
 			$this->error = "Error " . $this->db->lasterror();
 			dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR);
 
-			return - 1;
+			return -1;
 		}
 	}
 
@@ -380,7 +380,7 @@ class AccountancyCategory
 		} else {
 			$this->error = "Error " . $this->db->lasterror();
 			$this->errors[] = $this->error;
-			dol_syslog(__METHOD__ . " " . implode(',' . $this->errors), LOG_ERR);
+			dol_syslog(__METHOD__ . " " . implode(',', $this->errors), LOG_ERR);
 
 			return - 1;
 		}
diff --git a/htdocs/accountancy/customer/index.php b/htdocs/accountancy/customer/index.php
index be23efb56ea..10181ba4ac2 100644
--- a/htdocs/accountancy/customer/index.php
+++ b/htdocs/accountancy/customer/index.php
@@ -44,7 +44,7 @@ if ($user->societe_id > 0)
 if (! $user->rights->accounting->ventilation->read)
 	accessforbidden();
 
-	// Filter
+// Filter
 $year = $_GET["year"];
 if ($year == 0) {
 	$year_current = strftime("%Y", time());
@@ -56,11 +56,29 @@ if ($year == 0) {
 
 // Validate History
 $action = GETPOST('action');
+
+
+
+/*
+ * Actions
+ */
+
 if ($action == 'validatehistory') {
 
 	$error = 0;
 	$db->begin();
 
+	// First clean corrupted data
+	$sqlclean = "UPDATE " . MAIN_DB_PREFIX . "facturedet as fd";
+	$sqlclean .= " SET fd.fk_code_ventilation = 0";
+	$sqlclean .= ' WHERE fd.fk_code_ventilation NOT IN ';
+	$sqlclean .= '	(SELECT accnt.rowid ';
+	$sqlclean .= '	FROM ' . MAIN_DB_PREFIX . 'accounting_account as accnt';
+	$sqlclean .= '	INNER JOIN ' . MAIN_DB_PREFIX . 'accounting_system as syst';
+	$sqlclean .= '	ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=' . $conf->global->CHARTOFACCOUNTS . ')';
+	$resql = $db->query($sqlclean);
+
+	// Now make the binding
 	if ($db->type == 'pgsql') {
 		$sql1 = "UPDATE " . MAIN_DB_PREFIX . "facturedet";
 		$sql1 .= " SET fk_code_ventilation = accnt.rowid";
@@ -113,12 +131,13 @@ if ($action == 'validatehistory') {
 } elseif ($action == 'cleanaccountancycode') {
 	$error = 0;
 	$db->begin();
-
+	
+	// Now clean
 	$sql1 = "UPDATE " . MAIN_DB_PREFIX . "facturedet as fd";
 	$sql1 .= " SET fd.fk_code_ventilation = 0";
 	$sql1 .= " WHERE fd.fk_facture IN ( SELECT f.rowid FROM " . MAIN_DB_PREFIX . "facture as f";
 	$sql1 .= " WHERE f.datef >= '" . $db->idate(dol_get_first_day($year_current, 1, false)) . "'";
-	$sql1 .= "  AND f.datef <= '" . $db->idate(dol_get_last_day($year_current, 12, false)) . "')";
+	$sql1 .= " AND f.datef <= '" . $db->idate(dol_get_last_day($year_current, 12, false)) . "')";
 
 	dol_syslog("htdocs/accountancy/customer/index.php fixaccountancycode", LOG_DEBUG);
 
@@ -151,8 +170,8 @@ print '<br>';
 print '<div class="inline-block divButAction">';
 print '<a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=validatehistory">' . $langs->trans("ValidateHistory") . '</a>';
 print '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=cleanaccountancycode">' . $langs->trans("CleanHistory", $year_current) . '</a>';
-// TODO Remove this. Should be done always.
-print '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=fixaccountancycode">' . $langs->trans("CleanFixHistory", $year_current) . '</a>';
+// TODO Remove this. Should be done into the repair.php script
+if ($conf->global->MAIN_FEATURES_LEVEL > 0) print '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=fixaccountancycode">' . $langs->trans("CleanFixHistory", $year_current) . '</a>';
 print '</div>';
 
 $sql = "SELECT count(*) FROM " . MAIN_DB_PREFIX . "facturedet as fd";
diff --git a/htdocs/accountancy/report/result.php b/htdocs/accountancy/report/result.php
index c9c1a13fd14..b7a3c847439 100644
--- a/htdocs/accountancy/report/result.php
+++ b/htdocs/accountancy/report/result.php
@@ -103,14 +103,17 @@ foreach($months as $k => $v){
 print	'</tr>';
 
 $cats = $AccCat->getCatsCpts();
+if ($cats < 0) dol_print_error($db, $AccCat->error, $AccCat->errors);
+
 $catsCalcule = $AccCat->getCatsCal();
+if ($catsCalcule < 0) dol_print_error($db, $AccCat->error, $AccCat->errors);
 
 $j=1;
 $sommes = array();
 
-if(!empty($cats))
+if (!empty($cats))
 {
-	foreach ( $cats as $name_cat => $cpts )
+	foreach ($cats as $name_cat => $cpts)
 	{
 		print "<tr class='liste_titre'>";
 		print '<td colspan="17">' . $name_cat . '</td>';
diff --git a/htdocs/accountancy/supplier/index.php b/htdocs/accountancy/supplier/index.php
index 2506267ac15..a02ae9aa184 100644
--- a/htdocs/accountancy/supplier/index.php
+++ b/htdocs/accountancy/supplier/index.php
@@ -54,11 +54,28 @@ if ($year == 0) {
 
 // Validate History
 $action = GETPOST('action');
+
+
+/*
+ * Actions
+ */
+
 if ($action == 'validatehistory') {
 
 	$error = 0;
 	$db->begin();
 
+	// First clean corrupted data
+	$sqlclean = "UPDATE " . MAIN_DB_PREFIX . "facturedet as fd";
+	$sqlclean .= " SET fd.fk_code_ventilation = 0";
+	$sqlclean .= ' WHERE fd.fk_code_ventilation NOT IN ';
+	$sqlclean .= '	(SELECT accnt.rowid ';
+	$sqlclean .= '	FROM ' . MAIN_DB_PREFIX . 'accounting_account as accnt';
+	$sqlclean .= '	INNER JOIN ' . MAIN_DB_PREFIX . 'accounting_system as syst';
+	$sqlclean .= '	ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=' . $conf->global->CHARTOFACCOUNTS . ')';
+	$resql = $db->query($sqlclean);
+	
+	// Now make the binding
 	if ($db->type == 'pgsql') {
 		$sql1 = "UPDATE " . MAIN_DB_PREFIX . "facture_fourn_det";
 		$sql1 .= " SET fk_code_ventilation = accnt.rowid";
@@ -148,7 +165,7 @@ print '<div class="inline-block divButAction">';
 print '<a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=validatehistory">' . $langs->trans("ValidateHistory") . '</a>';
 print '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=cleanaccountancycode">' . $langs->trans("CleanHistory", $year_current) . '</a>';
 // TODO Remove this. Should be done always.
-print '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=fixaccountancycode">' . $langs->trans("CleanFixHistory", $year_current) . '</a>';
+if ($conf->global->MAIN_FEATURES_LEVEL > 0) print '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=fixaccountancycode">' . $langs->trans("CleanFixHistory", $year_current) . '</a>';
 print '</div>';
 
 $y = $year_current;
diff --git a/htdocs/compta/resultat/clientfourn.php b/htdocs/compta/resultat/clientfourn.php
index 95ea2bfe46c..63565a22ea6 100644
--- a/htdocs/compta/resultat/clientfourn.php
+++ b/htdocs/compta/resultat/clientfourn.php
@@ -710,13 +710,22 @@ if (! empty($conf->expensereport->enabled))
 if (! empty($conf->don->enabled))
 {
 	print '<tr><td colspan="4">'.$langs->trans("Donation").'</td></tr>';
-	$sql = "SELECT p.societe as name, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";
-	$sql.= " FROM ".MAIN_DB_PREFIX."don as p";
-	$sql.= " WHERE p.entity = ".$conf->entity;
 	if ($modecompta == 'CREANCES-DETTES')
-	   $sql.= " AND fk_statut in (1,2)";
+	{
+    	$sql = "SELECT p.societe as name, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";
+    	$sql.= " FROM ".MAIN_DB_PREFIX."don as p";
+    	$sql.= " WHERE p.entity = ".$conf->entity;
+    	$sql.= " AND fk_statut in (1,2)";
+	}
 	else
-	   $sql.= " AND fk_statut=2";
+	{
+	    $sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";
+	    $sql.= " FROM ".MAIN_DB_PREFIX."don as p";
+	    $sql.= " INNER JOIN ".MAIN_DB_PREFIX."payment_donation as pe ON pe.fk_donation = p.rowid";
+	    $sql.= " INNER JOIN ".MAIN_DB_PREFIX."c_paiement as c ON pe.fk_typepayment = c.id";
+	    $sql.= " WHERE p.entity = ".getEntity('donation',1);
+	    $sql.= " AND fk_statut >= 2";	 
+	}
 	if (! empty($date_start) && ! empty($date_end))
 		$sql.= " AND p.datedon >= '".$db->idate($date_start)."' AND p.datedon <= '".$db->idate($date_end)."'";
 	$sql.= " GROUP BY p.societe, p.firstname, p.lastname, dm";
diff --git a/htdocs/compta/resultat/index.php b/htdocs/compta/resultat/index.php
index 861b44edd6c..e01feaf742e 100644
--- a/htdocs/compta/resultat/index.php
+++ b/htdocs/compta/resultat/index.php
@@ -474,9 +474,11 @@ $parameters["mode"] = $modecompta;
 $hookmanager->initHooks(array('externalbalance'));
 $reshook=$hookmanager->executeHooks('addReportInfo',$parameters,$object,$action);    // Note that $action and $object may have been modified by some hooks
 
+
 /*
  * Salaries
  */
+
 if (! empty($conf->salaries->enabled))
 {
     if ($modecompta == 'CREANCES-DETTES') {
@@ -580,15 +582,23 @@ if (! empty($conf->don->enabled))
 {
     $subtotal_ht = 0;
     $subtotal_ttc = 0;
-    $sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";
-    $sql.= " FROM ".MAIN_DB_PREFIX."don as p";
-    $sql.= " WHERE p.entity = ".$conf->entity;
-	if ($modecompta == 'CREANCES-DETTES')
-	   $sql.= " AND fk_statut in (1,2)";
-	else
-	   $sql.= " AND fk_statut=2";
-    $sql.= " GROUP BY p.societe, p.firstname, p.lastname, dm";
     
+    if ($modecompta == 'CREANCES-DETTES') {
+        $sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";
+        $sql.= " FROM ".MAIN_DB_PREFIX."don as p";
+        $sql.= " WHERE p.entity = ".$conf->entity;
+        $sql.= " AND fk_statut in (1,2)";
+    }
+    else {
+        $sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";
+        $sql.= " FROM ".MAIN_DB_PREFIX."don as p";
+		$sql.= " INNER JOIN ".MAIN_DB_PREFIX."payment_donation as pe ON pe.fk_donation = p.rowid";
+		$sql.= " INNER JOIN ".MAIN_DB_PREFIX."c_paiement as c ON pe.fk_typepayment = c.id";
+		$sql.= " WHERE p.entity = ".getEntity('donation',1);
+   	    $sql.= " AND fk_statut >= 2";
+    }
+    $sql.= " GROUP BY p.societe, p.firstname, p.lastname, dm";
+
     dol_syslog("get donation payments");
     $result=$db->query($sql);
     if ($result)
diff --git a/htdocs/compta/stats/index.php b/htdocs/compta/stats/index.php
index eb9a425c2af..18cf29a8c3e 100644
--- a/htdocs/compta/stats/index.php
+++ b/htdocs/compta/stats/index.php
@@ -135,7 +135,7 @@ else {
 	dol_print_error($db);
 }
 
-// On ajoute les paiements anciennes version, non lies par paiement_facture
+// On ajoute les paiements anciennes version, non lies par paiement_facture (very old versions)
 if ($modecompta != 'CREANCES-DETTES')
 {
 	$sql = "SELECT date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount_ttc";
-- 
GitLab