1<?php
2/* Copyright (C) 2002-2005	Rodolphe Quiedeville	<rodolphe@quiedeville.org>
3 * Copyright (C) 2004		Eric Seigne				<eric.seigne@ryxeo.com>
4 * Copyright (C) 2004-2012	Laurent Destailleur		<eldy@users.sourceforge.net>
5 * Copyright (C) 2005-2012	Regis Houssin			<regis.houssin@inodbox.com>
6 * Copyright (C) 2012		Vinicius Nogueira       <viniciusvgn@gmail.com>
7 * Copyright (C) 2012		Juanjo Menent			<jmenent@2byte.es>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23/**
24 *		\file       htdocs/fourn/facture/impayees.php
25 *		\ingroup    facture
26 *		\brief      Page to list all unpaid invoices
27 */
28
29require '../../main.inc.php';
30require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
31require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
32require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
33
34if (!$user->rights->fournisseur->facture->lire) accessforbidden();
35
36$langs->loadLangs(array("companies", "bills"));
37
38$socid = GETPOST('socid', 'int');
39$option = GETPOST('option');
40
41// Security check
42if ($user->socid > 0)
43{
44	$action = '';
45	$socid = $user->socid;
46}
47
48$sortfield = GETPOST("sortfield", 'alpha');
49$sortorder = GETPOST("sortorder", 'alpha');
50
51$search_ref = GETPOST('search_ref', 'alpha');
52$search_ref_supplier = GETPOST('search_ref_supplier', 'alpha');
53$search_company = GETPOST('search_company', 'alpha');
54$search_amount_no_tax = GETPOST('search_amount_no_tax', 'alpha');
55$search_amount_all_tax = GETPOST('search_amount_all_tax', 'alpha');
56
57$limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
58$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
59if (empty($page) || $page == -1) { $page = 0; }     // If $page is not defined, or '' or -1
60$offset = $limit * $page;
61$pageprev = $page - 1;
62$pagenext = $page + 1;
63if (!$sortfield) $sortfield = "f.date_lim_reglement";
64if (!$sortorder) $sortorder = "ASC";
65
66if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) // Both test are required to be compatible with all browsers
67{
68	$search_ref = "";
69	$search_ref_supplier = "";
70	$search_company = "";
71	$search_amount_no_tax = "";
72	$search_amount_all_tax = "";
73}
74
75/*
76 * View
77 */
78
79$now = dol_now();
80
81llxHeader('', $langs->trans("BillsSuppliersUnpaid"));
82
83$title = $langs->trans("BillsSuppliersUnpaid");
84
85$facturestatic = new FactureFournisseur($db);
86$companystatic = new Societe($db);
87
88if ($user->rights->fournisseur->facture->lire)
89{
90	$sql = "SELECT s.rowid as socid, s.nom as name,";
91	$sql .= " f.rowid, f.ref, f.ref_supplier, f.total_ht, f.total_ttc,";
92	$sql .= " f.datef as df, f.date_lim_reglement as datelimite, ";
93	$sql .= " f.paye as paye, f.rowid as facid, f.fk_statut";
94	$sql .= " ,sum(pf.amount) as am";
95	if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user ";
96	$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
97	if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
98	$sql .= ",".MAIN_DB_PREFIX."facture_fourn as f";
99	$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf ON f.rowid=pf.fk_facturefourn ";
100	$sql .= " WHERE f.entity = ".$conf->entity;
101	$sql .= " AND f.fk_soc = s.rowid";
102	$sql .= " AND f.paye = 0 AND f.fk_statut = 1";
103	if ($option == 'late') $sql .= " AND f.date_lim_reglement < '".$db->idate(dol_now() - $conf->facture->fournisseur->warning_delay)."'";
104	if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
105	if ($socid) $sql .= " AND s.rowid = ".$socid;
106
107	if (GETPOST('filtre'))
108	{
109		$filtrearr = explode(",", GETPOST('filtre'));
110		foreach ($filtrearr as $fil)
111		{
112			$filt = explode(":", $fil);
113			$sql .= " AND ".$filt[0]." = ".$filt[1];
114		}
115	}
116
117	if ($search_ref)
118	{
119		$sql .= " AND f.ref LIKE '%".$db->escape($search_ref)."%'";
120	}
121	if ($search_ref_supplier)
122	{
123		$sql .= " AND f.ref_supplier LIKE '%".$db->escape($search_ref_supplier)."%'";
124	}
125
126	if ($search_company)
127	{
128		$sql .= " AND s.nom LIKE '%".$db->escape($search_company)."%'";
129	}
130
131	if ($search_amount_no_tax)
132	{
133		$sql .= " AND f.total_ht = '".$db->escape($search_amount_no_tax)."'";
134	}
135
136	if ($search_amount_all_tax)
137	{
138		$sql .= " AND f.total_ttc = '".$db->escape($search_amount_all_tax)."'";
139	}
140
141	if (dol_strlen(GETPOST('sf_re')) > 0)
142	{
143		$sql .= " AND f.ref_supplier LIKE '%".$db->escape(GETPOST('sf_re'))."%'";
144	}
145
146	$sql .= " GROUP BY s.rowid, s.nom, f.rowid, f.ref, f.ref_supplier, f.total_ht, f.total_ttc, f.datef, f.date_lim_reglement, f.paye, f.fk_statut";
147	if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user ";
148	$sql .= $db->order($sortfield, $sortorder);
149	if (!in_array("f.ref_supplier", explode(',', $sortfield))) $sql .= ", f.ref_supplier DESC";
150
151	$resql = $db->query($sql);
152	if ($resql)
153	{
154		$num = $db->num_rows($resql);
155
156		if ($socid)
157		{
158			$soc = new Societe($db);
159			$soc->fetch($socid);
160		}
161
162		$param = '';
163		if ($socid) $param .= "&socid=".$socid;
164
165		if ($search_ref)         	$param .= '&amp;search_ref='.urlencode($search_ref);
166		if ($search_ref_supplier)	$param .= '&amp;search_ref_supplier='.urlencode($search_ref_supplier);
167		if ($search_company)     	$param .= '&amp;search_company='.urlencode($search_company);
168		if ($search_amount_no_tax)	$param .= '&amp;search_amount_no_tax='.urlencode($search_amount_no_tax);
169		if ($search_amount_all_tax) $param .= '&amp;search_amount_all_tax='.urlencode($search_amount_all_tax);
170
171		$param .= ($option ? "&option=".$option : "");
172		if (!empty($late)) $param .= '&late='.urlencode($late);
173		$urlsource = str_replace('&amp;', '&', $param);
174
175		$titre = ($socid ? $langs->trans("BillsSuppliersUnpaidForCompany", $soc->name) : $langs->trans("BillsSuppliersUnpaid"));
176
177		if ($option == 'late') $titre .= ' ('.$langs->trans("Late").')';
178		else $titre .= ' ('.$langs->trans("All").')';
179
180		$link = '';
181		if (empty($option)) $link = '<a href="'.$_SERVER["PHP_SELF"].'?option=late'.($socid ? '&socid='.$socid : '').'">'.$langs->trans("ShowUnpaidLateOnly").'</a>';
182		elseif ($option == 'late') $link = '<a href="'.$_SERVER["PHP_SELF"].'?'.($socid ? '&socid='.$socid : '').'">'.$langs->trans("ShowUnpaidAll").'</a>';
183		print load_fiche_titre($titre, $link);
184
185		print_barre_liste('', '', $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', 0); // We don't want pagination on this page
186		$i = 0;
187		print '<form method="get" action="'.$_SERVER["PHP_SELF"].'">';
188
189		print '<table class="liste centpercent">';
190		print '<tr class="liste_titre">';
191		print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "f.rowid", "", $param, "", $sortfield, $sortorder);
192		print_liste_field_titre("RefSupplier", $_SERVER["PHP_SELF"], "f.ref_supplier", "", $param, "", $sortfield, $sortorder);
193		print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "f.datef", "", $param, '', $sortfield, $sortorder, 'center ');
194		print_liste_field_titre("DateDue", $_SERVER["PHP_SELF"], "f.date_lim_reglement", "", $param, '', $sortfield, $sortorder, 'center ');
195		print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", $param, "", $sortfield, $sortorder);
196		print_liste_field_titre("AmountHT", $_SERVER["PHP_SELF"], "f.total_ht", "", $param, '', $sortfield, $sortorder, 'right ');
197		print_liste_field_titre("AmountTTC", $_SERVER["PHP_SELF"], "f.total_ttc", "", $param, '', $sortfield, $sortorder, 'right ');
198		print_liste_field_titre("AlreadyPaid", $_SERVER["PHP_SELF"], "am", "", $param, '', $sortfield, $sortorder, 'right ');
199		print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "fk_statut,paye,am", "", $param, '', $sortfield, $sortorder, 'right ');
200		print "</tr>\n";
201
202		// Lines with filter fields
203		print '<tr class="liste_titre">';
204		print '<td class="liste_titre">';
205		print '<input class="flat" size="8" type="text" name="search_ref" value="'.$search_ref.'"></td>';
206		print '<td class="liste_titre">';
207		print '<input class="flat" size="8" type="text" name="search_ref_supplier" value="'.$search_ref_supplier.'"></td>';
208		print '<td class="liste_titre">&nbsp;</td>';
209		print '<td class="liste_titre">&nbsp;</td>';
210		print '<td class="liste_titre left">';
211		print '<input class="flat" type="text" size="6" name="search_company" value="'.$search_company.'">';
212		print '</td><td class="liste_titre right">';
213		print '<input class="flat" type="text" size="8" name="search_amount_no_tax" value="'.$search_amount_no_tax.'">';
214		print '</td><td class="liste_titre right">';
215		print '<input class="flat" type="text" size="8" name="search_amount_all_tax" value="'.$search_amount_all_tax.'">';
216		print '</td>';
217		print '<td class="liste_titre maxwidthsearch">';
218		$searchpicto = $form->showFilterAndCheckAddButtons(0);
219		print $searchpicto;
220		print '</td>';
221		print "</tr>\n";
222
223		if ($num > 0)
224		{
225			$total_ht = 0;
226			$total_ttc = 0;
227			$total_paid = 0;
228
229			while ($i < $num)
230			{
231				$objp = $db->fetch_object($resql);
232
233				$facturestatic->statut = $objp->fk_statut;
234				$facturestatic->date_echeance = $db->jdate($objp->datelimite);
235
236
237
238				print '<tr class="oddeven">';
239				$classname = "impayee";
240
241				print '<td class="nowrap">';
242				$facturestatic->id = $objp->facid;
243				$facturestatic->ref = $objp->ref;
244				print $facturestatic->getNomUrl(1);
245				print "</td>\n";
246
247				print '<td class="nowrap">'.dol_trunc($objp->ref_supplier, 12).'</td>';
248
249				print '<td class="nowrap center">'.dol_print_date($db->jdate($objp->df), 'day')."</td>\n";
250				print '<td class="nowrap center">'.dol_print_date($db->jdate($objp->datelimite), 'day');
251				if ($facturestatic->hasDelay()) {
252					print img_warning($langs->trans("Late"));
253				}
254				print "</td>\n";
255
256				print '<td>';
257				$companystatic->id = $objp->socid;
258				$companystatic->name = $objp->name;
259				print $companystatic->getNomUrl(1, 'supplier', 32);
260				print '</td>';
261
262				print "<td class=\"right\">".price($objp->total_ht)."</td>";
263				print "<td class=\"right\">".price($objp->total_ttc)."</td>";
264				print "<td class=\"right\">".price($objp->am)."</td>";
265
266				// Show invoice status
267				print '<td class="right nowrap">';
268				print $facturestatic->LibStatut($objp->paye, $objp->fk_statut, 5, $objp->am);
269				print '</td>';
270
271				print "</tr>\n";
272				$total_ht += $objp->total_ht;
273				$total_ttc += $objp->total_ttc;
274				$total_paid += $objp->am;
275
276				$i++;
277			}
278
279			print '<tr class="liste_total">';
280			print "<td colspan=\"5\" class=\"left\">".$langs->trans("Total").": </td>";
281			print "<td class=\"right\"><b>".price($total_ht)."</b></td>";
282			print "<td class=\"right\"><b>".price($total_ttc)."</b></td>";
283			print "<td class=\"right\"><b>".price($total_paid)."</b></td>";
284			print '<td class="center">&nbsp;</td>';
285			print "</tr>\n";
286		}
287
288		print "</table>";
289
290		print '</form>';
291
292		$db->free($resql);
293	} else {
294		dol_print_error($db);
295	}
296}
297
298// End of page
299llxFooter();
300$db->close();
301