From b186fa87590ac54664dba60f755c2aab5c98fac4 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 1 Jul 2017 13:31:44 +0200
Subject: [PATCH] Fix pgsql compatibility

---
 htdocs/accountancy/customer/index.php      | 19 ++++++------
 htdocs/accountancy/expensereport/index.php | 31 ++++++++++---------
 htdocs/accountancy/supplier/index.php      | 36 ++++++++++------------
 3 files changed, 42 insertions(+), 44 deletions(-)

diff --git a/htdocs/accountancy/customer/index.php b/htdocs/accountancy/customer/index.php
index 9af3d0d19d9..f1bea3bd4a1 100644
--- a/htdocs/accountancy/customer/index.php
+++ b/htdocs/accountancy/customer/index.php
@@ -23,7 +23,7 @@
 /**
  * \file htdocs/accountancy/customer/index.php
  * \ingroup Advanced accountancy
- * \brief Home customer ventilation
+ * \brief Home customer journalization page
  */
 
 require '../../main.inc.php';
@@ -58,7 +58,7 @@ if ($year == 0) {
 }
 
 // Validate History
-$action = GETPOST('action','alpha');
+$action = GETPOST('action','aZ09');
 
 
 
@@ -97,7 +97,7 @@ if ($action == 'validatehistory') {
 		$sql1 .= " AND fd.fk_code_ventilation = 0";
 	}
 
-	dol_syslog("htdocs/accountancy/customer/index.php sql=" . $sql, LOG_DEBUG);
+	dol_syslog('htdocs/accountancy/customer/index.php');
 
 	$resql1 = $db->query($sql1);
 	if (! $resql1) {
@@ -173,7 +173,7 @@ print $langs->trans("DescVentilCustomer") . '<br>';
 print $langs->trans("DescVentilMore", $langs->transnoentitiesnoconv("ValidateHistory"), $langs->transnoentitiesnoconv("ToBind")) . '<br>';
 print '<br>';
 //print '<div class="inline-block divButAction">';
-// TODO Remove this. Should be done into the repair.php script
+// TODO Remove this. Should be done always or into the repair.php script.
 if ($conf->global->MAIN_FEATURES_LEVEL > 1) print '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=fixaccountancycode">' . $langs->trans("CleanFixHistory", $year_current) . '</a>';
 //print '</div>';
 
@@ -189,7 +189,7 @@ if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
 }
 $sql .= " AND f.entity IN (" . getEntity('facture', 0) . ")";	// We don't share object for accountancy
 
-dol_syslog("htdocs/accountancy/customer/index.php sql=" . $sql, LOG_DEBUG);
+dol_syslog('htdocs/accountancy/customer/index.php');
 $result = $db->query($sql);
 if ($result) {
 	$row = $db->fetch_row($result);
@@ -233,7 +233,7 @@ if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
 }
 $sql .= " GROUP BY fd.fk_code_ventilation,aa.account_number,aa.label";
 
-dol_syslog("htdocs/accountancy/customer/index.php sql=" . $sql, LOG_DEBUG);
+dol_syslog('htdocs/accountancy/customer/index.php sql=' . $sql, LOG_DEBUG);
 $resql = $db->query($sql);
 if ($resql) {
 	$num = $db->num_rows($resql);
@@ -289,7 +289,7 @@ if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
 $sql .= " AND aa.account_number IS NOT NULL";
 $sql .= " GROUP BY fd.fk_code_ventilation,aa.account_number,aa.label";
 
-dol_syslog("htdocs/accountancy/customer/index.php sql=" . $sql, LOG_DEBUG);
+dol_syslog('htdocs/accountancy/customer/index.php');
 $resql = $db->query($sql);
 if ($resql) {
 	$num = $db->num_rows($resql);
@@ -347,7 +347,6 @@ if ($conf->global->MAIN_FEATURES_LEVEL > 0) // This part of code looks strange.
 	dol_syslog('htdocs/accountancy/customer/index.php');
 	$resql = $db->query($sql);
 	if ($resql) {
-		$i = 0;
 		$num = $db->num_rows($resql);
 
 		while ($row = $db->fetch_row($resql)) {
@@ -357,7 +356,6 @@ if ($conf->global->MAIN_FEATURES_LEVEL > 0) // This part of code looks strange.
 			}
 			print '<td align="right"><b>' . price($row[13]) . '</b></td>';
 			print '</tr>';
-			$i ++;
 		}
 		$db->free($resql);
 	} else {
@@ -365,6 +363,7 @@ if ($conf->global->MAIN_FEATURES_LEVEL > 0) // This part of code looks strange.
 	}
 	print "</table>\n";
 
+	
 	if (! empty($conf->margin->enabled)) {
 		print "<br>\n";
 		print '<table class="noborder" width="100%">';
@@ -390,7 +389,7 @@ if ($conf->global->MAIN_FEATURES_LEVEL > 0) // This part of code looks strange.
 			$sql .= " AND f.type IN (" . Facture::TYPE_STANDARD . "," . Facture::TYPE_REPLACEMENT . "," . Facture::TYPE_CREDIT_NOTE . "," . Facture::TYPE_DEPOSIT . "," . Facture::TYPE_SITUATION . ")";
 		}
 
-		dol_syslog('htdocs/accountancy/customer/index.php:: $sql=' . $sql);
+		dol_syslog('htdocs/accountancy/customer/index.php');
 		$resql = $db->query($sql);
 		if ($resql) {
 			$num = $db->num_rows($resql);
diff --git a/htdocs/accountancy/expensereport/index.php b/htdocs/accountancy/expensereport/index.php
index fdbf4d1ce4b..b17aeedb4bd 100644
--- a/htdocs/accountancy/expensereport/index.php
+++ b/htdocs/accountancy/expensereport/index.php
@@ -69,7 +69,7 @@ if ($action == 'validatehistory') {
 
 	// First clean corrupted data
 	$sqlclean = "UPDATE " . MAIN_DB_PREFIX . "expensereport_det as erd";
-	$sqlclean .= " SET erd.fk_code_ventilation = 0";
+	$sqlclean .= " SET fk_code_ventilation = 0";
 	$sqlclean .= ' WHERE erd.fk_code_ventilation NOT IN ';
 	$sqlclean .= '	(SELECT accnt.rowid ';
 	$sqlclean .= '	FROM ' . MAIN_DB_PREFIX . 'accounting_account as accnt';
@@ -93,6 +93,8 @@ if ($action == 'validatehistory') {
 		$sql1 .= " AND erd.fk_code_ventilation = 0";
 	}
 
+	dol_syslog('htdocs/accountancy/expensereport/index.php');
+	
 	$resql1 = $db->query($sql1);
 	if (! $resql1) {
 		$error ++;
@@ -107,7 +109,7 @@ if ($action == 'validatehistory') {
 	$db->begin();
 
 	$sql1 = "UPDATE " . MAIN_DB_PREFIX . "expensereport_det as erd";
-	$sql1 .= " SET erd.fk_code_ventilation = 0";
+	$sql1 .= " SET fk_code_ventilation = 0";
 	$sql1 .= ' WHERE erd.fk_code_ventilation NOT IN ';
 	$sql1 .= '	(SELECT accnt.rowid ';
 	$sql1 .= '	FROM ' . MAIN_DB_PREFIX . 'accounting_account as accnt';
@@ -129,8 +131,9 @@ if ($action == 'validatehistory') {
 	$error = 0;
 	$db->begin();
 
+	// Now clean
 	$sql1 = "UPDATE " . MAIN_DB_PREFIX . "expensereport_det as erd";
-	$sql1.= " SET erd.fk_code_ventilation = 0";
+	$sql1.= " SET fk_code_ventilation = 0";
 	$sql1.= " WHERE erd.fk_expensereport IN ( SELECT er.rowid FROM " . MAIN_DB_PREFIX . "expensereport as er";
 	$sql1.= " WHERE er.date_debut >= '" . $db->idate(dol_get_first_day($year_current, 1, false)) . "'";
 	$sql1.= " AND er.date_debut <= '" . $db->idate(dol_get_last_day($year_current, 12, false)) . "'";
@@ -166,25 +169,25 @@ print $langs->trans("DescVentilExpenseReportMore", $langs->transnoentitiesnoconv
 print '<br>';
 
 //print '<div class="inline-block divButAction">';
-// TODO Remove this. Should be done always.
+// TODO Remove this. Should be done always or into the repair.php script.
 if ($conf->global->MAIN_FEATURES_LEVEL > 1) print '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=fixaccountancycode">' . $langs->trans("CleanFixHistory", $year_current) . '</a>';
 //print '</div>';
 
 
-$buttonbind = '<a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=validatehistory">' . $langs->trans("ValidateHistory") . '</a>';
-$buttonreset = '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=cleanaccountancycode">' . $langs->trans("CleanHistory", $year_current) . '</a>';
 
 
 
 $y = $year_current;
 
-print '<br>';
+$buttonbind = '<a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=validatehistory">' . $langs->trans("ValidateHistory") . '</a>';
+$buttonreset = '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=cleanaccountancycode">' . $langs->trans("CleanHistory", $year_current) . '</a>';
+
 
 print_fiche_titre($langs->trans("OverviewOfAmountOfLinesNotBound"), $buttonbind, '');
 
 
 print '<table class="noborder" width="100%">';
-print '<tr class="liste_titre"><td width="200" align="left">' . $langs->trans("Account") . '</td>';
+print '<tr class="liste_titre"><td width="200">' . $langs->trans("Account") . '</td>';
 print '<td width="200" align="left">' . $langs->trans("Label") . '</td>';
 for($i = 1; $i <= 12; $i ++) {
     print '<td width="60" align="right">' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '</td>';
@@ -196,7 +199,7 @@ $sql .= "  " . $db->ifsql('aa.label IS NULL', "'".$langs->trans('NotMatch')."'",
 for($i = 1; $i <= 12; $i ++) {
     $sql .= "  SUM(" . $db->ifsql('MONTH(er.date_debut)=' . $i, 'erd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ",";
 }
-$sql .= " ROUND(SUM(erd.total_ht),2) as total";
+$sql .= " SUM(erd.total_ht) as total";
 $sql .= " FROM " . MAIN_DB_PREFIX . "expensereport_det as erd";
 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "expensereport as er ON er.rowid = erd.fk_expensereport";
 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = erd.fk_code_ventilation";
@@ -237,7 +240,7 @@ print_fiche_titre($langs->trans("OverviewOfAmountOfLinesBound"), $buttonreset, '
 
 
 print '<table class="noborder" width="100%">';
-print '<tr class="liste_titre"><td width="200" align="left">' . $langs->trans("Account") . '</td>';
+print '<tr class="liste_titre"><td width="200">' . $langs->trans("Account") . '</td>';
 print '<td width="200" align="left">' . $langs->trans("Label") . '</td>';
 for($i = 1; $i <= 12; $i ++) {
     print '<td width="60" align="right">' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '</td>';
@@ -260,7 +263,7 @@ $sql .= " AND er.entity IN (" . getEntity('expensereport', 0) . ")";     // We d
 $sql .= " AND aa.account_number IS NOT NULL";
 $sql .= " GROUP BY erd.fk_code_ventilation,aa.account_number,aa.label";
 
-dol_syslog('/accountancy/expensereport/index.php:: sql=' . $sql);
+dol_syslog('htdocs/accountancy/expensereport/index.php');
 $resql = $db->query($sql);
 if ($resql) {
     $num = $db->num_rows($resql);
@@ -304,7 +307,7 @@ if ($conf->global->MAIN_FEATURES_LEVEL > 0) // This part of code looks strange.
     for($i = 1; $i <= 12; $i ++) {
     	$sql .= " SUM(" . $db->ifsql('MONTH(er.date_create)=' . $i, 'erd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ",";
     }
-    $sql .= " ROUND(SUM(erd.total_ht),2) as total";
+    $sql .= " SUM(erd.total_ht) as total";
     $sql .= " FROM " . MAIN_DB_PREFIX . "expensereport_det as erd";
     $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "expensereport as er ON er.rowid = erd.fk_expensereport";
     $sql .= " WHERE er.date_debut >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'";
@@ -312,14 +315,12 @@ if ($conf->global->MAIN_FEATURES_LEVEL > 0) // This part of code looks strange.
     $sql .= " AND er.fk_statut > 0 ";
     $sql .= " AND er.entity IN (" . getEntity('expensereport', 0) . ")";     // We don't share object for accountancy
     
-    dol_syslog('/accountancy/expensereport/index.php:: sql=' . $sql);
+    dol_syslog('htdocs/accountancy/expensereport/index.php');
     $resql = $db->query($sql);
     if ($resql) {
     	$num = $db->num_rows($resql);
     
     	while ( $row = $db->fetch_row($resql)) {
-    
-    
     		print '<tr><td>' . $row[0] . '</td>';
     			for($i = 1; $i <= 12; $i ++) {
     			print '<td align="right">' . price($row[$i]) . '</td>';
diff --git a/htdocs/accountancy/supplier/index.php b/htdocs/accountancy/supplier/index.php
index fb5f28fc27d..f862feb5ba3 100644
--- a/htdocs/accountancy/supplier/index.php
+++ b/htdocs/accountancy/supplier/index.php
@@ -21,12 +21,13 @@
 /**
  * \file		htdocs/accountancy/supplier/index.php
  * \ingroup		Advanced accountancy
- * \brief		Home supplier ventilation
+ * \brief		Home supplier journalization page
  */
 
 require '../../main.inc.php';
 require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
 require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
+require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
 
 // Langs
 $langs->load("compta");
@@ -55,7 +56,7 @@ if ($year == 0) {
 }
 
 // Validate History
-$action = GETPOST('action', 'alpha');
+$action = GETPOST('action', 'aZ09');
 
 
 /*
@@ -93,6 +94,8 @@ if ($action == 'validatehistory') {
 		$sql1 .= " AND fd.fk_code_ventilation = 0";
 	}
 
+	dol_syslog('htdocs/accountancy/supplier/index.php');
+
 	$resql1 = $db->query($sql1);
 	if (! $resql1) {
 		$error ++;
@@ -159,33 +162,28 @@ llxHeader('', $langs->trans("SuppliersVentilation"));
 $textprevyear = '<a href="' . $_SERVER["PHP_SELF"] . '?year=' . ($year_current - 1) . '">' . img_previous() . '</a>';
 $textnextyear = '&nbsp;<a href="' . $_SERVER["PHP_SELF"] . '?year=' . ($year_current + 1) . '">' . img_next() . '</a>';
 
-print load_fiche_titre($langs->trans("SuppliersVentilation") . "&nbsp;" . $textprevyear . "&nbsp;" . $langs->trans("Year") . "&nbsp;" . $year_start . "&nbsp;" . $textnextyear, '', 'title_accountancy');
+print load_fiche_titre($langs->trans("SuppliersVentilation") . " " . $textprevyear . "&nbsp;" . $langs->trans("Year") . "&nbsp;" . $year_start . "&nbsp;" . $textnextyear, '', 'title_accountancy');
 
 print $langs->trans("DescVentilSupplier") . '<br>';
 print $langs->trans("DescVentilMore", $langs->transnoentitiesnoconv("ValidateHistory"), $langs->transnoentitiesnoconv("ToBind")) . '<br>';
 print '<br>';
 
 //print '<div class="inline-block divButAction">';
-// TODO Remove this. Should be done always.
+// TODO Remove this. Should be done always or into the repair.php script.
 if ($conf->global->MAIN_FEATURES_LEVEL > 1) print '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=fixaccountancycode">' . $langs->trans("CleanFixHistory", $year_current) . '</a>';
 //print '</div>';
 
-$buttonbind = '<a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=validatehistory">' . $langs->trans("ValidateHistory") . '</a>';
-$buttonreset = '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=cleanaccountancycode">' . $langs->trans("CleanHistory", $year_current) . '</a>';
-
-
 
 $y = $year_current;
 
-$var = true;
-
+$buttonbind = '<a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=validatehistory">' . $langs->trans("ValidateHistory") . '</a>';
+$buttonreset = '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=cleanaccountancycode">' . $langs->trans("CleanHistory", $year_current) . '</a>';
 
-print '<br>';
 
 print_fiche_titre($langs->trans("OverviewOfAmountOfLinesNotBound"), $buttonbind, '');
 
 print '<table class="noborder" width="100%">';
-print '<tr class="liste_titre"><td width="200" align="left">' . $langs->trans("Account") . '</td>';
+print '<tr class="liste_titre"><td width="200">' . $langs->trans("Account") . '</td>';
 print '<td width="200" align="left">' . $langs->trans("Label") . '</td>';
 for($i = 1; $i <= 12; $i ++) {
 	print '<td width="60" align="right">' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '</td>';
@@ -197,7 +195,7 @@ $sql .= "  " . $db->ifsql('aa.label IS NULL', "'".$langs->trans('NotMatch')."'",
 for($i = 1; $i <= 12; $i ++) {
 	$sql .= "  SUM(" . $db->ifsql('MONTH(ff.datef)=' . $i, 'ffd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ",";
 }
-$sql .= "  ROUND(SUM(ffd.total_ht),2) as total";
+$sql .= "  SUM(ffd.total_ht) as total";
 $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as ffd";
 $sql .= "  LEFT JOIN " . MAIN_DB_PREFIX . "facture_fourn as ff ON ff.rowid = ffd.fk_facture_fourn";
 $sql .= "  LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = ffd.fk_code_ventilation";
@@ -208,7 +206,7 @@ $sql .= " AND ff.entity IN (" . getEntity('facture_fourn', 0) . ")";     // We d
 $sql .= " AND aa.account_number IS NULL";
 $sql .= " GROUP BY ffd.fk_code_ventilation,aa.account_number,aa.label";
 
-dol_syslog('/accountancy/supplier/index.php:: sql=' . $sql);
+dol_syslog('htdocs/accountancy/supplier/index.php');
 $resql = $db->query($sql);
 if ($resql) {
 	$num = $db->num_rows($resql);
@@ -239,7 +237,7 @@ print '<br>';
 print_fiche_titre($langs->trans("OverviewOfAmountOfLinesBound"), $buttonreset, '');
 
 print '<table class="noborder" width="100%">';
-print '<tr class="liste_titre"><td width="200" align="left">' . $langs->trans("Account") . '</td>';
+print '<tr class="liste_titre"><td width="200">' . $langs->trans("Account") . '</td>';
 print '<td width="200" align="left">' . $langs->trans("Label") . '</td>';
 for($i = 1; $i <= 12; $i ++) {
     print '<td width="60" align="right">' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '</td>';
@@ -251,7 +249,7 @@ $sql .= "  " . $db->ifsql('aa.label IS NULL', "'".$langs->trans('NotMatch')."'",
 for($i = 1; $i <= 12; $i ++) {
     $sql .= "  SUM(" . $db->ifsql('MONTH(ff.datef)=' . $i, 'ffd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ",";
 }
-$sql .= "  ROUND(SUM(ffd.total_ht),2) as total";
+$sql .= "  SUM(ffd.total_ht) as total";
 $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as ffd";
 $sql .= "  LEFT JOIN " . MAIN_DB_PREFIX . "facture_fourn as ff ON ff.rowid = ffd.fk_facture_fourn";
 $sql .= "  LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = ffd.fk_code_ventilation";
@@ -262,7 +260,7 @@ $sql .= " AND ff.entity IN (" . getEntity('facture_fourn', 0) . ")";     // We d
 $sql .= " AND aa.account_number IS NOT NULL";
 $sql .= " GROUP BY ffd.fk_code_ventilation,aa.account_number,aa.label";
 
-dol_syslog('/accountancy/supplier/index.php:: sql=' . $sql);
+dol_syslog('htdocs/accountancy/supplier/index.php');
 $resql = $db->query($sql);
 if ($resql) {
     $num = $db->num_rows($resql);
@@ -306,7 +304,7 @@ if ($conf->global->MAIN_FEATURES_LEVEL > 0) // This part of code looks strange.
     for($i = 1; $i <= 12; $i ++) {
     	$sql .= "  SUM(" . $db->ifsql('MONTH(ff.datef)=' . $i, 'ffd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ",";
     }
-    $sql .= "  ROUND(SUM(ffd.total_ht),2) as total";
+    $sql .= "  SUM(ffd.total_ht) as total";
     $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as ffd";
     $sql .= "  LEFT JOIN " . MAIN_DB_PREFIX . "facture_fourn as ff ON ff.rowid = ffd.fk_facture_fourn";
     $sql .= " WHERE ff.datef >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'";
@@ -314,7 +312,7 @@ if ($conf->global->MAIN_FEATURES_LEVEL > 0) // This part of code looks strange.
     $sql .= "  AND ff.fk_statut > 0 ";
     $sql .= " AND ff.entity IN (" . getEntity('facture_fourn', 0) . ")";     // We don't share object for accountancy
 
-    dol_syslog('/accountancy/supplier/index.php:: sql=' . $sql);
+    dol_syslog('htdocs/accountancy/supplier/index.php');
     $resql = $db->query($sql);
     if ($resql) {
     	$num = $db->num_rows($resql);
-- 
GitLab