From 0b19f2e06861e8048f278861a5425909803dc543 Mon Sep 17 00:00:00 2001
From: aspangaro <alexandre.spangaro@gmail.com>
Date: Thu, 9 Jul 2015 07:46:58 +0200
Subject: [PATCH] Update list.php with elarifr modification

---
 htdocs/accountancy/customer/list.php | 223 +++++++++++++++++++++------
 htdocs/accountancy/supplier/list.php | 172 ++++++++++++++++-----
 2 files changed, 311 insertions(+), 84 deletions(-)

diff --git a/htdocs/accountancy/customer/list.php b/htdocs/accountancy/customer/list.php
index ac1c3429658..eb6446ae13d 100644
--- a/htdocs/accountancy/customer/list.php
+++ b/htdocs/accountancy/customer/list.php
@@ -1,7 +1,7 @@
 <?php
 /* Copyright (C) 2013-2014 Olivier Geffroy      <jeff@jeffinfo.com>
- * Copyright (C) 2013-2014 Alexandre Spangaro	<alexandre.spangaro@gmail.com>
- * Copyright (C) 2014      Ari Elbaz (elarifr)	<github@accedinfo.com>
+ * Copyright (C) 2013-2015 Alexandre Spangaro	<alexandre.spangaro@gmail.com>
+ * Copyright (C) 2014-2015 Ari Elbaz (elarifr)	<github@accedinfo.com>
  * Copyright (C) 2013-2014 Florian Henry		<florian.henry@open-concept.pro>
  * Copyright (C) 2014	   Juanjo Menent		<jmenent@2byte.es>
  *
@@ -31,16 +31,52 @@ require '../../main.inc.php';
 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/html.formventilation.class.php';
+require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
 
 // Langs
 $langs->load("compta");
 $langs->load("bills");
+$langs->load("other");
 $langs->load("main");
 $langs->load("accountancy");
 
 $action = GETPOST('action');
 $codeventil = GETPOST('codeventil', 'array');
 $mesCasesCochees = GETPOST('mesCasesCochees', 'array');
+$search_ref     = GETPOST('search_ref','alpha');
+$search_label   = GETPOST('search_label','alpha');
+$search_desc    = GETPOST('search_desc','alpha');
+
+$sortfield = GETPOST('sortfield','alpha');
+$sortorder = GETPOST('sortorder','alpha');
+//Should move to top with all GETPOST
+$page = GETPOST('page');
+if ($page < 0) $page = 0;
+
+if (! empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION)) {
+	$limit = $conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION;
+} else if ($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION <= 0) {
+	$limit = $conf->liste_limit;
+} else {
+	$limit = $conf->liste_limit;
+}
+$offset = $limit * $page;
+//End Should move to top with all GETPOST
+
+// TODO : remove comment 
+//elarifr we can not use only
+//$sql .= " ORDER BY l.rowid";
+// f.datef will order like FA08 FA09 FA10 FA05 FA06 FA07 FA04...
+// f.facnumber will not order properly invoice / avoir / accompte you can have All AC then All AV and all FA
+// l.rowid when an invoice is edited rowid are added at end of table & facturedet.rowid are not ordered
+//if (! $sortfield) $sortfield="l.rowid";
+if (! $sortfield) $sortfield="f.datef, f.facnumber, l.rowid";
+//if (! $sortorder) $sortorder="DESC";
+if (! $sortorder) {
+	if ($conf->global->ACCOUNTING_LIST_SORT_VENTILATION_TODO > 0) {
+		$sortorder = " DESC ";
+	}
+}
 
 // Security check
 if ($user->societe_id > 0)
@@ -50,8 +86,31 @@ if (! $user->rights->accounting->ventilation->dispatch)
 
 $formventilation = new FormVentilation($db);
 
+//Defaut AccountingAccount RowId Product / Service
+//at this time ACCOUNTING_SERVICE_SOLD_ACCOUNT & ACCOUNTING_PRODUCT_SOLD_ACCOUNT are account number not accountingacount rowid
+//so we need to get those default value rowid first
+$accounting = new AccountingAccount($db);
+//TODO: we should need to check if result is a really exist accountaccount rowid.....
+$aarowid_s = $accounting->fetch('', ACCOUNTING_SERVICE_SOLD_ACCOUNT);
+$aarowid_p = $accounting->fetch('', ACCOUNTING_PRODUCT_SOLD_ACCOUNT);
+
+// Purge search criteria
+if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter")) // Both test are required to be compatible with all browsers
+{
+    $search_ref='';
+    $search_label='';
+    $search_desc='';
+}
+/*
+ * View
+ */
+
 llxHeader('', $langs->trans("Ventilation"));
 
+//debug
+//print_r($aarowid_s);
+//print_r($aarowid_p);
+
 print  '<script type="text/javascript">
 			$(function () {
 				$(\'#select-all\').click(function(event) {
@@ -68,6 +127,7 @@ print  '<script type="text/javascript">
 			    });
 			});
 			 </script>';
+
 /*
  * Action
  */
@@ -123,7 +183,13 @@ $offset = $limit * $page;
 
 $sql = "SELECT f.facnumber, f.rowid as facid, l.fk_product, l.description, l.total_ht, l.rowid, l.fk_code_ventilation,";
 $sql .= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.fk_product_type as type, p.accountancy_code_sell as code_sell";
+// A REVOIR elarifr si vraiment necessaire de rajouter , p.fk_product_type as type. le type produit / service est de facto defini pour chaque ligne de facturedet.product_type
+// il est donc plus logique de se servir de l.product_type au lieu de p.fk_product_type
 $sql .= " , aa.rowid as aarowid";
+// we need f.datef to reorder lines
+$sql .= " , f.datef";
+// we need to use llx_facturedet l.product_type as used at the time on invoice. if llx_product fk_product_type is changed later it could not change the sell already made !
+$sql .= " , l.product_type as type_l";
 $sql .= " FROM " . MAIN_DB_PREFIX . "facture as f";
 $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "facturedet as l ON f.rowid = l.fk_facture";
 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = l.fk_product";
@@ -131,14 +197,27 @@ $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accountingaccount as aa ON p.accountan
 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_system as accsys ON accsys.pcg_version = aa.fk_pcg_version";
 $sql .= " WHERE f.fk_statut > 0 AND fk_code_ventilation <= 0";
 $sql .= " AND (accsys.rowid='" . $conf->global->CHARTOFACCOUNTS . "' OR p.accountancy_code_sell IS NULL OR p.accountancy_code_sell ='')";
+// Add search filter like
+if (strlen(trim($search_ref))) {
+	$sql .= " AND (p.ref like '%" . $search_ref . "%')";
+}
+if (strlen(trim($search_label))) {
+	$sql .= " AND (p.label like '%" . $search_label . "%')";
+}
+if (strlen(trim($search_desc))) {
+	$sql .= " AND (l.description like '%" . $search_desc . "%')";
+}
 if (! empty($conf->multicompany->enabled)) {
 	$sql .= " AND f.entity IN (" . getEntity("facture", 1) . ")";
 }
+//TODO: Remove comment
+//replaced by default value $sortfield,$sortorder
+//$sql .= " ORDER BY l.rowid";
+//if ($conf->global->ACCOUNTING_LIST_SORT_VENTILATION_TODO > 0) {
+//	$sql .= " DESC ";
+//}
+$sql.= $db->order($sortfield,$sortorder);
 
-$sql .= " ORDER BY l.rowid";
-if ($conf->global->ACCOUNTING_LIST_SORT_VENTILATION_TODO > 0) {
-	$sql .= " DESC ";
-}
 $sql .= $db->plimit($limit + 1, $offset);
 
 dol_syslog("/accountancy/customer/list.php sql=" . $sql, LOG_DEBUG);
@@ -156,58 +235,96 @@ if ($result) {
 	print '<input type="hidden" name="action" value="ventil">';
 
 	print '<table class="noborder" width="100%">';
-	print '<tr class="liste_titre"><td>' . $langs->trans("Invoice") . '</td>';
-	print '<td>' . $langs->trans("Ref") . '</td>';
-	print '<td>' . $langs->trans("Label") . '</td>';
-	print '<td>' . $langs->trans("Description") . '</td>';
-	print '<td align="right">' . $langs->trans("Amount") . '</td>';
-	print '<td align="right">' . $langs->trans("AccountAccounting") . '</td>';
-	print '<td align="center">' . $langs->trans("IntoAccount") . '</td>';
-	print '<td align="center">' . $langs->trans("Ventilate") . '<br><label id="select-all">'.$langs->trans('All').'</label>/<label id="unselect-all">'.$langs->trans('None').'</label>'.'</td>';
+	print '<tr class="liste_titre">';
+	print_liste_field_titre($langs->trans("Invoice"), $_SERVER["PHP_SELF"],"f.facnumber","",$param,'',$sortfield,$sortorder);
+	print_liste_field_titre($langs->trans("Ref"), $_SERVER["PHP_SELF"],"p.ref","",$param,'',$sortfield,$sortorder);
+	print_liste_field_titre($langs->trans("Label"), $_SERVER["PHP_SELF"],"p.label","",$param,'',$sortfield,$sortorder);
+	print_liste_field_titre($langs->trans("Description"), $_SERVER["PHP_SELF"],"l.description","",$param,'',$sortfield,$sortorder);
+    print_liste_field_titre($langs->trans("Amount"),'','','','','align="right"');
+	print_liste_field_titre($langs->trans("AccountAccounting"),'','','','','align="center"');
+	print_liste_field_titre($langs->trans("IntoAccount"),'','','','','align="center"');
+	print_liste_field_titre('');
+	print_liste_field_titre($langs->trans("Ventilate") . '<br><label id="select-all">'.$langs->trans('All').'</label>/<label id="unselect-all">'.$langs->trans('None').'</label>','','','','','align="center"');
 	print '</tr>';
 
+//	We add search filter
+/*	But Hit Enter will validate ventilation....
+	print '<tr class="liste_titre">';
+	print '<td class="liste_titre" >&nbsp;</td>';
+	print '<td class="liste_titre"><input type="text" class="flat" size="10" name="search_ref" value="' . $search_ref . '"></td>';
+	print '<td class="liste_titre"><input type="text" class="flat" size="20" name="search_label" value="' . $search_label . '"></td>';
+	print '<td class="liste_titre"><input type="text" class="flat" size="30" name="search_desc" value="' . $search_desc . '"></td>';
+
+	print '<td class="liste_titre" colspan="3">&nbsp;</td>';
+	print '<td align="right" colspan="2" class="liste_titre">';
+	print '<input type="image" class="liste_titre" src="'.img_picto($langs->trans("Search"),'search.png','','',1).'" name="button_search" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
+	print '&nbsp;';
+	print '<input type="image" class="liste_titre" src="'.img_picto($langs->trans("Search"),'searchclear.png','','',1).'" name="button_removefilter" value="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'" title="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'">';
+	print '</td>';
+	print '</tr>';
+*/
 	$facture_static = new Facture($db);
 	$product_static = new Product($db);
 	$form = new Form($db);
 
-	$var = True;
+	$var = true;
 	while ( $i < min($num_lines, $limit) ) {
 		$objp = $db->fetch_object($result);
 		$var = ! $var;
 
 		// product_type: 0 = service ? 1 = product
-		// if product does not exist we use the value of product_type provided in facturedet to define if this is a product or service
-		// issue : if we change product_type value in product DB it should differ from the value stored in facturedet DB !
-		$code_sell_notset = '';
-
-		if (empty($objp->code_sell)) {
-			$code_sell_notset = 'color:red';
-
-			if (! empty($objp->type)) {
-				if ($objp->type == 1) {
-					$objp->code_sell = (! empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
-				} else {
-					$objp->code_sell = (! empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
-				}
-			} else {
-				$code_sell_notset = 'color:blue';
-
-				if ($objp->type == 1) {
-					$objp->code_sell = (! empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
-				} else {
-					$objp->code_sell = (! empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
-				}
+		// C'est le contraire dans les base !!!!!!  IT IS INVERTED IN LLX_PRODUCT & LLX_FACTUREDET
+		// elarifr define account numbercode comptable si pas defini dans la fiche produit a partir des lignes de facturation
+		// product_type: 1 = service ? 0 = product
+		// because some modules like subtotal module l.product_type can be other than 0 or 1   ! and we don't put in account lines with product_type=9
+		// first we check product.fk_product_type as type and l.product_type as type_l
+		// if product does not exist we use the value of l.product_type provided in facturedet to define if this is a product or service
+		// issue : if we change product_type value in product DB it should differ from the value stored in facturedet DB ! so we report both and user make choice of accounting account.
+		$objp->code_sell_l = '';
+		$objp->code_sell_p = '';
+		$objp->aarowid_suggest = '';
+		$code_sell_p_l_differ = '';
+
+		//check if code_sell defined in product or set default value according p.fk_product_type do not care lines when product_type value not 0 || 1
+		//and we set suggested accounting account rowid as $objp->aarowid_s
+		$code_sell_p_notset = '';
+		$objp->aarowid_suggest = $objp->aarowid;
+		if ( ! empty($objp->code_sell)) {
+			$objp->code_sell_p = $objp->code_sell;
+		} else {
+			$code_sell_p_notset = 'color:red';
+			if ($objp->type == 1) {
+				$objp->code_sell_p = (! empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
+			}
+			elseif ($objp->type == 0) {
+				$objp->code_sell_p = (! empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
 			}
 		}
+		// check facturedet.product_type & set default value according l.type_l, do not care lines when product_type value not 0 || 1
+//		if ( ! empty($objp->type_l)) {
+//			$objp->code_sell_l = $objp->type_l;
+//		} else {
+			if ($objp->type_l == 1) {
+				$objp->code_sell_l = (! empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
+				if ($objp->aarowid == '') $objp->aarowid_suggest = $aarowid_s;
+			}
+			elseif ($objp->type_l == 0) {
+				$objp->code_sell_l = (! empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
+				if ($objp->aarowid == '') $objp->aarowid_suggest = $aarowid_p;
+			}
+//		}
+
+
+		//if not same code for product fk_prouct_type and facturedet.product_type, product has been change after sale and must report
+		if ($objp->code_sell_l <> $objp->code_sell_p) $code_sell_p_l_differ = 'color:red';
 
 		print "<tr $bc[$var]>";
 
-		// Ref facture
+		// Ref Invoice
 		$facture_static->ref = $objp->facnumber;
 		$facture_static->id = $objp->facid;
 		print '<td>' . $facture_static->getNomUrl(1) . '</td>';
-
-		// Ref produit
+		// Ref Customer Invoice
 		$product_static->ref = $objp->product_ref;
 		$product_static->id = $objp->product_id;
 		$product_static->type = $objp->type;
@@ -218,27 +335,39 @@ if ($result) {
 			print '&nbsp;';
 		print '</td>';
 
-		print '<td>' . dol_trunc($objp->product_label, 24) . '</td>';
-		print '<td>' . nl2br(dol_trunc($objp->description, 32)) . '</td>';
+		print '<td style="' . $code_sell_p_l_differ . '">' . dol_trunc($objp->product_label, 24) . '</td>';
+		//TODO: we should set a user defined value to adjust user square / wide screen size
+		$trunclength = defined('ACCOUNTING_LENGTH_DESCRIPTION') ? ACCOUNTING_LENGTH_DESCRIPTION : 32;
+		print '<td style="' . $code_sell_p_l_differ . '">' . nl2br(dol_trunc($objp->description, $trunclength)) . '</td>';
 
 		print '<td align="right">';
 		print price($objp->total_ht);
 		print '</td>';
 
-		print '<td align="center" style="' . $code_sell_notset . '">';
-		print $objp->code_sell;
+		print '<td align="center" style="' . $code_sell_p_notset . '">';
+		// if not same kind of product_type stored in product & facturedet we display both account and let user choose
+		if ($objp->code_sell_l == $objp->code_sell_p) {
+			print $objp->code_sell_l;
+		} else {
+			print 'lines='.$objp->code_sell_l . '<br />product=' . $objp->code_sell_p;
+		}
 		print '</td>';
 
 		// Colonne choix du compte
 		print '<td align="center">';
-		print $formventilation->select_account($objp->aarowid, 'codeventil[]', 1);
+		// TODO: we should set a user defined value to adjust user square / wide screen size
+		// $trunclengthform = defined('ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT') ? ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT : 50;
+		print $formventilation->select_account($objp->aarowid_suggest, 'codeventil[]', 1);
 		print '</td>';
 
+		print '<td align="center">' . $objp->rowid . '</td>';
 		// Colonne choix ligne a ventiler
 		print '<td align="center">';
-		print '<input type="checkbox" name="mesCasesCochees[]" value="' . $objp->rowid . "_" . $i . '"' . ($objp->aarowid ? "checked" : "") . '/>';
+		//TODO checked only if account exist in product, if only suggested do not check, user must validate 
+		print '<input type="checkbox" name="mesCasesCochees[]" value="' . $objp->rowid . "_" . $i . '"' . ($objp->aarowid_suggest ? "checked" : "") . '/>';
 		print '</td>';
-
+//debug
+//print '</tr><tr><td colspan=6>Product: p.type='. $objp->type .' - p.code_sell='. $objp->code_sell .'  --- Check code_sell_product=' . $objp->code_sell_p .'  ---Check facturedet l.type_l='. $objp->type_l .' - code_sell_lines=' . $objp->code_sell_l . '  -- aarowid_suggest=' . $objp->aarowid_suggest.'</td>';
 		print '</tr>';
 		$i ++;
 	}
@@ -250,5 +379,5 @@ if ($result) {
 	print $db->error();
 }
 
-$db->close();
 llxFooter();
+$db->close();
diff --git a/htdocs/accountancy/supplier/list.php b/htdocs/accountancy/supplier/list.php
index bf14ebc185e..02057b31255 100644
--- a/htdocs/accountancy/supplier/list.php
+++ b/htdocs/accountancy/supplier/list.php
@@ -1,7 +1,7 @@
 <?php
 /* Copyright (C) 2013-2014	Olivier Geffroy			<jeff@jeffinfo.com>
- * Copyright (C) 2013-2014	Alexandre Spangaro		<alexandre.spangaro@gmail.com>
- * Copyright (C) 2014		Ari Elbaz (elarifr)		<github@accedinfo.com>
+ * Copyright (C) 2013-2015	Alexandre Spangaro		<alexandre.spangaro@gmail.com>
+ * Copyright (C) 2014-2015	Ari Elbaz (elarifr)		<github@accedinfo.com>
  * Copyright (C) 2013-2014	Florian Henry			<florian.henry@open-concept.pro>
  * Copyright (C) 2014		Juanjo Menent			<jmenent@2byte.es>s
  *
@@ -31,6 +31,7 @@ require '../../main.inc.php';
 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/html.formventilation.class.php';
+require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
 
 // Langs
 $langs->load("compta");
@@ -42,6 +43,42 @@ $langs->load("accountancy");
 $action = GETPOST('action');
 $codeventil = GETPOST('codeventil', 'array');
 $mesCasesCochees = GETPOST('mesCasesCochees', 'array');
+$search_ref     = GETPOST('search_ref','alpha');
+$search_label   = GETPOST('search_label','alpha');
+$search_desc    = GETPOST('search_desc','alpha');
+
+$sortfield = GETPOST('sortfield','alpha');
+$sortorder = GETPOST('sortorder','alpha');
+//Should move to top with all GETPOST
+$page = GETPOST('page');
+if ($page < 0) $page = 0;
+
+
+if (! empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION)) {
+	$limit = $conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION;
+} else if ($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION <= 0) {
+	$limit = $conf->liste_limit;
+} else {
+	$limit = $conf->liste_limit;
+}
+$offset = $limit * $page;
+//End Should move to top with all GETPOST
+
+
+// TODO : remove comment 
+//elarifr we can not use only
+//$sql .= " ORDER BY l.rowid";
+// f.datef will order like FA08 FA09 FA10 FA05 FA06 FA07 FA04...
+// f.ref will not order properly invoice / avoir / accompte you can have All AC then All AV and all FA
+// l.rowid when an invoice is edited rowid are added at end of table & facturedet.rowid are not ordered
+//if (! $sortfield) $sortfield="l.rowid";
+if (! $sortfield) $sortfield="f.datef, f.ref, l.rowid";
+//if (! $sortorder) $sortorder="DESC";
+if (! $sortorder) {
+	if ($conf->global->ACCOUNTING_LIST_SORT_VENTILATION_TODO > 0) {
+		$sortorder = " DESC ";
+	}
+}
 
 // Security check
 if ($user->societe_id > 0)
@@ -51,8 +88,31 @@ if (! $user->rights->accounting->ventilation->dispatch)
 
 $formventilation = new FormVentilation($db);
 
+//Defaut AccountingAccount RowId Product / Service
+//at this time ACCOUNTING_SERVICE_SOLD_ACCOUNT & ACCOUNTING_PRODUCT_SOLD_ACCOUNT are account number not accountingacount rowid
+//so we need to get those default value rowid first
+$accounting = new AccountingAccount($db);
+
+//TODO: we should need to check if result is a really exist accountaccount rowid.....
+$aarowid_s = $accounting->fetch('', ACCOUNTING_SERVICE_BUY_ACCOUNT);
+$aarowid_p = $accounting->fetch('', ACCOUNTING_PRODUCT_BUY_ACCOUNT);
+
+// Purge search criteria
+if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter")) // Both test are required to be compatible with all browsers
+{
+    $search_ref='';
+    $search_label='';
+    $search_desc='';
+}
+
+/*
+ * View
+ */
 llxHeader('', $langs->trans("Ventilation"));
 
+//debug
+//print_r($aarowid_s);
+//print_r($aarowid_p);
 print  '<script type="text/javascript">
 			$(function () {
 				$(\'#select-all\').click(function(event) {
@@ -91,7 +151,7 @@ if ($action == 'ventil') {
 			$sql .= " SET fk_code_ventilation = " . $monCompte;
 			$sql .= " WHERE rowid = " . $monId;
 
-			dol_syslog('accountancy/supplier/list.php:: sql=' . $sql);
+			dol_syslog('accountancy/supplier/list.php:: sql=' . $sql, LOG_DEBUG);
 			if ($db->query($sql)) {
 				print '<div><font color="green">' . $langs->trans("Lineofinvoice") . ' ' . $monId . ' ' . $langs->trans("VentilatedinAccount") . ' : ' . $monCompte . '</font></div>';
 			} else {
@@ -126,6 +186,9 @@ $offset = $limit * $page;
 
 $sql = "SELECT f.ref, f.rowid as facid, f.ref_supplier, l.fk_product, l.description, l.total_ht as price, l.rowid, l.fk_code_ventilation, ";
 $sql .= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.fk_product_type as type, p.accountancy_code_buy as code_buy";
+$sql .= " , aa.rowid as aarowid";
+$sql .= " , f.datef";
+$sql .= " , l.product_type as type_l";
 $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn as f";
 $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "facture_fourn_det as l ON f.rowid = l.fk_facture_fourn";
 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = l.fk_product";
@@ -134,14 +197,22 @@ $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_system as accsys ON accsys.
 $sql .= " WHERE f.fk_statut > 0 AND fk_code_ventilation <= 0";
 $sql .= " AND (accsys.rowid='".$conf->global->CHARTOFACCOUNTS."' OR p.accountancy_code_sell IS NULL OR p.accountancy_code_buy ='')";
 
+// Add search filter like
+if (strlen(trim($search_ref))) {
+	$sql .= " AND (p.ref like '%" . $search_ref . "%')";
+}
+if (strlen(trim($search_label))) {
+	$sql .= " AND (p.label like '%" . $search_label . "%')";
+}
+if (strlen(trim($search_desc))) {
+	$sql .= " AND (l.description like '%" . $search_desc . "%')";
+}
 if (! empty($conf->multicompany->enabled)) {
 	$sql .= " AND f.entity IN (" . getEntity("facture_fourn", 1) . ")";
 }
 
-$sql .= " ORDER BY l.rowid";
-if ($conf->global->ACCOUNTING_LIST_SORT_VENTILATION_TODO > 0) {
-	$sql .= " DESC ";
-}
+$sql.= $db->order($sortfield,$sortorder);
+
 $sql .= $db->plimit($limit + 1, $offset);
 
 dol_syslog('accountancy/supplier/list.php:: $sql=' . $sql);
@@ -153,20 +224,22 @@ if ($result) {
 	// TODO : print_barre_liste always use $conf->liste_limit and do not care about custom limit in list...
 	print_barre_liste($langs->trans("InvoiceLines"), $page, $_SERVER["PHP_SELF"], "", $sortfield, $sortorder, '', $num_lines);
 
-	print '<td align="left"><br><b>' . $langs->trans("DescVentilTodoSupplier") . '</b></br></td>';
+	print '<br><b>' . $langs->trans("DescVentilTodoCustomer") . '</b></br>';
 
 	print '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">' . "\n";
 	print '<input type="hidden" name="action" value="ventil">';
 
 	print '<table class="noborder" width="100%">';
-	print '<tr class="liste_titre"><td>Facture</td>';
-	print '<td align="left">' . $langs->trans("Ref") . '</td>';
-	print '<td align="left">' . $langs->trans("Label") . '</td>';
-	print '<td>' . $langs->trans("Description") . '</td>';
-	print '<td align="right">' . $langs->trans("Amount") . '</td>';
-	print '<td align="right">' . $langs->trans("Compte") . '</td>';
-	print '<td align="center">' . $langs->trans("IntoAccount") . '</td>';
-	print '<td align="center">' . $langs->trans("Ventilate") . '<br><label id="select-all">'.$langs->trans('All').'</label>/<label id="unselect-all">'.$langs->trans('None').'</label>'.'</td>';
+	print '<tr class="liste_titre">';
+	print_liste_field_titre($langs->trans("Invoice"), $_SERVER["PHP_SELF"],"f.ref","",$param,'',$sortfield,$sortorder);
+	print_liste_field_titre($langs->trans("Ref"), $_SERVER["PHP_SELF"],"p.ref","",$param,'',$sortfield,$sortorder);
+	print_liste_field_titre($langs->trans("Label"), $_SERVER["PHP_SELF"],"p.label","",$param,'',$sortfield,$sortorder);
+	print_liste_field_titre($langs->trans("Description"), $_SERVER["PHP_SELF"],"l.description","",$param,'',$sortfield,$sortorder);
+    print_liste_field_titre($langs->trans("Amount"),'','','','','align="right"');
+	print_liste_field_titre($langs->trans("AccountAccounting"),'','','','','align="center"');
+	print_liste_field_titre($langs->trans("IntoAccount"),'','','','','align="center"');
+    print_liste_field_titre('');
+	print_liste_field_titre($langs->trans("Ventilate") . '<br><label id="select-all">'.$langs->trans('All').'</label>/<label id="unselect-all">'.$langs->trans('None').'</label>','','','','','align="center"');
 	print "</tr>\n";
 
 	$facturefourn_static = new FactureFournisseur($db);
@@ -181,27 +254,48 @@ if ($result) {
 		// product_type: 0 = service ? 1 = product
 		// if product does not exist we use the value of product_type provided in facturedet to define if this is a product or service
 		// issue : if we change product_type value in product DB it should differ from the value stored in facturedet DB !
-		$code_buy_notset = '';
-		
-		if (empty($objp->code_buy)) {
-			$code_buy_notset = 'color:red';
+		$objp->code_buy_l = '';
+		$objp->code_buy_p = '';
+		$objp->aarowid_suggest = '';
+		$code_buy_p_l_differ = '';
+
+		$code_buy_p_notset = '';
+
+		$objp->aarowid_suggest = $objp->aarowid;
+		if ( ! empty($objp->code_buy)) {
+			$objp->code_buy_p = $objp->code_buy;
+		} else {
+			$code_buy_p_notset = 'color:red';
 			if ($objp->type == 1) {
-				$objp->code_buy = (! empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
-			} else {
-				$objp->code_buy = (! empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : $langs->trans("CodeNotDef"));					
+				$objp->code_buy_p = (! empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
+
+
+			}
+
+			elseif ($objp->type == 0) {
+				$objp->code_buy_p = (! empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
 			}
-		}else {
-			$code_buy_notset = 'color:blue';
 		}
 		
+			if ($objp->type_l == 1) {
+				$objp->code_buy_l = (! empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
+				if ($objp->aarowid == '') $objp->aarowid_suggest = $aarowid_s;
+			}
+			elseif ($objp->type_l == 0) {
+				$objp->code_buy_l = (! empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
+				if ($objp->aarowid == '') $objp->aarowid_suggest = $aarowid_p;
+			}
+		
+		if ($objp->code_buy_l <> $objp->code_buy_p) $code_buy_p_l_differ = 'color:red';
+		
 		print "<tr $bc[$var]>";
 
-		// Ref facture
+		// Ref Invoice
 		$facturefourn_static->ref = $objp->ref;
 		$facturefourn_static->id = $objp->facid;
 		print '<td>' . $facturefourn_static->getNomUrl(1) . '</td>';
 
-		// Ref facture supplier
+		// Ref Supplier Invoice
 		$productfourn_static->ref = $objp->product_ref;
 		$productfourn_static->id = $objp->product_id;
 		$productfourn_static->type = $objp->type;
@@ -212,29 +306,33 @@ if ($result) {
 			print '&nbsp;';
 		print '</td>';
 
-		// print '<td><a href="'.DOL_URL_ROOT.'/fourn/facture/card.php?facid='.$objp->facid.'">'.$objp->ref.'</a></td>';
-
-		// print '<td><a href="'.DOL_URL_ROOT.'/fourn/facture/card.php?facid='.$objp->facid.'">'.$objp->ref_supplier.'</a></td>';
+		print '<td style="' . $code_buy_p_l_differ . '">' . dol_trunc($objp->product_label, 24) . '</td>';
 
-		print '<td>' . dol_trunc($objp->product_label, 24) . '</td>';
-
-		print '<td>' . stripslashes(nl2br($objp->description)) . '</td>';
+		// TODO: we should set a user defined value to adjust user square / wide screen size
+		$trunclength = defined('ACCOUNTING_LENGTH_DESCRIPTION') ? ACCOUNTING_LENGTH_DESCRIPTION : 32;
+		print '<td style="' . $code_buy_p_l_differ . '">' . nl2br(dol_trunc($objp->description, $trunclength)) . '</td>';
 
 		print '<td align="right">';
 		print price($objp->price);
 		print '</td>';
 
-		print '<td align="center" style="' . $code_buy_notset . '">';
-		print $objp->code_buy;
+		print '<td align="center" style="' . $code_buy_p_notset . '">';
+		//if not same kind of product_type stored in product & facturedt we display both account and let user choose
+		if ($objp->code_buy_l == $objp->code_buy_p) {
+			print $objp->code_buy_l;
+		} else {
+			print 'lines='.$objp->code_buy_l . '<br />product=' . $objp->code_buy_p;
+		}
 		print '</td>';
 
 		// Colonne choix du compte
 		print '<td align="center">';
-		print $formventilation->select_account($objp->aarowid, 'codeventil[]', 1);
+		print $formventilation->select_account($objp->aarowid_suggest, 'codeventil[]', 1);
 		print '</td>';
+		print '<td align="center">' . $objp->rowid . '</td>';
 		// Colonne choix ligne a ventiler
 		print '<td align="center">';
-		print '<input type="checkbox" name="mesCasesCochees[]" value="' . $objp->rowid . "_" . $i . '"' . ($objp->aarowid ? "checked" : "") . '/>';
+		print '<input type="checkbox" name="mesCasesCochees[]" value="' . $objp->rowid . "_" . $i . '"' . ($objp->aarowid_suggest ? "checked" : "") . '/>';
 		print '</td>';
 
 		print "</tr>";
-- 
GitLab