1<?php
2/* Copyright (C) 2005-2012	Laurent Destailleur	<eldy@users.sourceforge.net>
3 * Copyright (C) 2005-2012	Regis Houssin		<regis.houssin@inodbox.com>
4 * Copyright (C) 2013		Florian Henry		<florian.henry@open-concept.pro>
5 * Copyright (C) 2015      Juanjo Menent		<jmenent@2byte.es>
6 * Copyright (C) 2017      	Charlie Benke		<charlie@patas-monkey.com>
7 * Copyright (C) 2017       ATM-CONSULTING		<contact@atm-consulting.fr>
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 * or see https://www.gnu.org/
22 */
23
24/**
25 *	    \file       htdocs/core/lib/invoice.lib.php
26 *		\brief      Functions used by invoice module
27 * 		\ingroup	invoice
28 */
29
30/**
31 * Initialize the array of tabs for customer invoice
32 *
33 * @param	Facture		$object		Invoice object
34 * @return	array					Array of head tabs
35 */
36function facture_prepare_head($object)
37{
38	global $db, $langs, $conf;
39
40	$h = 0;
41	$head = array();
42
43	$head[$h][0] = DOL_URL_ROOT.'/compta/facture/card.php?facid='.$object->id;
44	$head[$h][1] = $langs->trans('CustomerInvoice');
45	$head[$h][2] = 'compta';
46	$h++;
47
48	if (empty($conf->global->MAIN_DISABLE_CONTACTS_TAB)) {
49		$nbContact = count($object->liste_contact(-1, 'internal')) + count($object->liste_contact(-1, 'external'));
50		$head[$h][0] = DOL_URL_ROOT.'/compta/facture/contact.php?facid='.$object->id;
51		$head[$h][1] = $langs->trans('ContactsAddresses');
52		if ($nbContact > 0) {
53			$head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>';
54		}
55		$head[$h][2] = 'contact';
56		$h++;
57	}
58
59	if (!empty($conf->prelevement->enabled)) {
60		$nbStandingOrders = 0;
61		$sql = "SELECT COUNT(pfd.rowid) as nb";
62		$sql .= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
63		$sql .= " WHERE pfd.fk_facture = ".$object->id;
64		$sql .= " AND pfd.ext_payment_id IS NULL";
65		$resql = $db->query($sql);
66		if ($resql) {
67			$obj = $db->fetch_object($resql);
68			if ($obj) {
69				$nbStandingOrders = $obj->nb;
70			}
71		} else {
72			dol_print_error($db);
73		}
74		$head[$h][0] = DOL_URL_ROOT.'/compta/facture/prelevement.php?facid='.$object->id;
75		$head[$h][1] = $langs->trans('StandingOrders');
76		if ($nbStandingOrders > 0) {
77			$head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbStandingOrders.'</span>';
78		}
79		$head[$h][2] = 'standingorders';
80		$h++;
81	}
82
83	// Show more tabs from modules
84	// Entries must be declared in modules descriptor with line
85	// $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__');   to add new tab
86	// $this->tabs = array('entity:-tabname);   												to remove a tab
87	complete_head_from_modules($conf, $langs, $object, $head, $h, 'invoice');
88
89	if (empty($conf->global->MAIN_DISABLE_NOTES_TAB)) {
90		$nbNote = 0;
91		if (!empty($object->note_private)) {
92			$nbNote++;
93		}
94		if (!empty($object->note_public)) {
95			$nbNote++;
96		}
97		$head[$h][0] = DOL_URL_ROOT.'/compta/facture/note.php?facid='.$object->id;
98		$head[$h][1] = $langs->trans('Notes');
99		if ($nbNote > 0) {
100			$head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
101		}
102		$head[$h][2] = 'note';
103		$h++;
104	}
105
106	require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
107	require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
108	$upload_dir = $conf->facture->dir_output."/".dol_sanitizeFileName($object->ref);
109	$nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
110	$nbLinks = Link::count($db, $object->element, $object->id);
111	$head[$h][0] = DOL_URL_ROOT.'/compta/facture/document.php?facid='.$object->id;
112	$head[$h][1] = $langs->trans('Documents');
113	if (($nbFiles + $nbLinks) > 0) {
114		$head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
115	}
116	$head[$h][2] = 'documents';
117	$h++;
118
119	$head[$h][0] = DOL_URL_ROOT.'/compta/facture/info.php?facid='.$object->id;
120	$head[$h][1] = $langs->trans('Info');
121	$head[$h][2] = 'info';
122	$h++;
123
124	complete_head_from_modules($conf, $langs, $object, $head, $h, 'invoice', 'remove');
125
126	return $head;
127}
128
129/**
130 * Return array head with list of tabs to view object informations.
131 *
132 * @return array head array with tabs
133 */
134function invoice_admin_prepare_head()
135{
136	global $langs, $conf, $user;
137
138	$h = 0;
139	$head = array();
140
141	$head[$h][0] = DOL_URL_ROOT.'/admin/facture.php';
142	$head[$h][1] = $langs->trans("Miscellaneous");
143	$head[$h][2] = 'general';
144	$h++;
145
146	$head[$h][0] = DOL_URL_ROOT.'/admin/payment.php';
147	$head[$h][1] = $langs->trans("Payments");
148	$head[$h][2] = 'payment';
149	$h++;
150
151	// Show more tabs from modules
152	// Entries must be declared in modules descriptor with line
153	// $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
154	// $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab
155	complete_head_from_modules($conf, $langs, null, $head, $h, 'invoice_admin');
156
157	$head[$h][0] = DOL_URL_ROOT.'/compta/facture/admin/facture_cust_extrafields.php';
158	$head[$h][1] = $langs->trans("ExtraFieldsCustomerInvoices");
159	$head[$h][2] = 'attributes';
160	$h++;
161
162	$head[$h][0] = DOL_URL_ROOT.'/compta/facture/admin/facturedet_cust_extrafields.php';
163	$head[$h][1] = $langs->trans("ExtraFieldsLines");
164	$head[$h][2] = 'attributeslines';
165	$h++;
166
167	$head[$h][0] = DOL_URL_ROOT.'/compta/facture/admin/facture_rec_cust_extrafields.php';
168	$head[$h][1] = $langs->trans("ExtraFieldsCustomerInvoicesRec");
169	$head[$h][2] = 'attributesrec';
170	$h++;
171
172	$head[$h][0] = DOL_URL_ROOT.'/compta/facture/admin/facturedet_rec_cust_extrafields.php';
173	$head[$h][1] = $langs->trans("ExtraFieldsLinesRec");
174	$head[$h][2] = 'attributeslinesrec';
175	$h++;
176
177	if ($conf->global->INVOICE_USE_SITUATION) {	// Warning, implementation is seriously bugged and a new one not compatible is expected to become stable
178		$head[$h][0] = DOL_URL_ROOT.'/admin/facture_situation.php';
179		$head[$h][1] = $langs->trans("InvoiceSituation");
180		$head[$h][2] = 'situation';
181		$h++;
182	}
183
184	complete_head_from_modules($conf, $langs, null, $head, $h, 'invoice_admin', 'remove');
185
186	return $head;
187}
188
189
190/**
191 * Return array head with list of tabs to view object informations.
192 *
193 * @param   Facture     $object     Invoice object
194 * @return array                    head array with tabs
195 */
196function invoice_rec_prepare_head($object)
197{
198	global $db, $langs, $conf;
199
200	$h = 0;
201	$head = array();
202
203	$head[$h][0] = DOL_URL_ROOT.'/compta/facture/card-rec.php?id='.$object->id;
204	$head[$h][1] = $langs->trans("RepeatableInvoice");
205	$head[$h][2] = 'card';
206	$h++;
207
208	// Show more tabs from modules
209	// Entries must be declared in modules descriptor with line
210	// $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__');   to add new tab
211	// $this->tabs = array('entity:-tabname);   												to remove a tab
212	complete_head_from_modules($conf, $langs, $object, $head, $h, 'invoice-rec');
213
214	complete_head_from_modules($conf, $langs, $object, $head, $h, 'invoice-rec', 'remove');
215
216	return $head;
217}
218
219/**
220 * Return a HTML table that contains a pie chart of customer invoices
221 *
222 * @param	int		$socid		(Optional) Show only results from the customer with this id
223 * @return	string				A HTML table that contains a pie chart of customer invoices
224 */
225function getCustomerInvoicePieChart($socid = 0)
226{
227	global $conf, $db, $langs, $user;
228
229	if (empty($conf->facture->enabled) || empty($user->rights->facture->lire)) {
230		return '';
231	}
232
233	$sql = "SELECT count(f.rowid), f.fk_statut";
234	$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
235	$sql .= ", ".MAIN_DB_PREFIX."facture as f";
236	if (!$user->rights->societe->client->voir && !$socid) {
237		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
238	}
239	$sql .= " WHERE f.fk_soc = s.rowid";
240	$sql .= " AND f.entity IN (".getEntity('facture').")";
241	if ($user->socid) {
242		$sql .= ' AND f.fk_soc = '.$user->socid;
243	}
244	if (!$user->rights->societe->client->voir && !$socid) {
245		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
246	}
247	$sql .= " GROUP BY f.fk_statut";
248
249	$resql = $db->query($sql);
250	if (!$resql) {
251		dol_print_error($db);
252		return '';
253	}
254
255	$num = $db->num_rows($resql);
256	$i = 0;
257
258	$total = 0;
259	$vals = [];
260
261	while ($i < $num) {
262		$row = $db->fetch_row($resql);
263		if ($row) {
264			$vals[$row[1]] = $row[0];
265			$total += $row[0];
266		}
267
268		$i++;
269	}
270
271	$db->free($resql);
272
273	include DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
274
275	$result = '<div class="div-table-responsive-no-min">';
276	$result .= '<table class="noborder nohover centpercent">';
277	$result .= '<tr class="liste_titre">';
278	$result .= '<td colspan="2">'.$langs->trans("Statistics").' - '.$langs->trans("CustomerInvoice").'</td>';
279	$result .= '</tr>';
280
281	$objectstatic = new Facture($db);
282	$array = [Facture::STATUS_DRAFT, Facture::STATUS_VALIDATED, Facture::STATUS_CLOSED, Facture::STATUS_ABANDONED];
283	$dataseries = [];
284
285	foreach ($array as $status) {
286		$objectstatic->statut = $status;
287		$objectstatic->paye = $status == Facture::STATUS_CLOSED ? -1 : 0;
288
289		$dataseries[] = [$objectstatic->getLibStatut(1), (isset($vals[$status]) ? (int) $vals[$status] : 0)];
290		if ($status == Facture::STATUS_DRAFT) {
291			$colorseries[$status] = '-'.$badgeStatus0;
292		}
293		if ($status == Facture::STATUS_VALIDATED) {
294			$colorseries[$status] = $badgeStatus1;
295		}
296		if ($status == Facture::STATUS_CLOSED) {
297			$colorseries[$status] = $badgeStatus9;
298		}
299		if ($status == Facture::STATUS_ABANDONED) {
300			$colorseries[$status] = $badgeStatus6;
301		}
302
303		if (!$conf->use_javascript_ajax) {
304			$result .= '<tr class="oddeven">';
305			$result .= '<td>'.$objectstatic->getLibStatut(0).'</td>';
306			$result .= '<td class="right"><a href="list.php?statut='.$status.'">'.(isset($vals[$status]) ? $vals[$status] : 0).'</a></td>';
307			$result .= '</tr>';
308		}
309	}
310
311	if ($conf->use_javascript_ajax) {
312		$dolgraph = new DolGraph();
313		$dolgraph->SetData($dataseries);
314		$dolgraph->SetDataColor(array_values($colorseries));
315		$dolgraph->setShowLegend(2);
316		$dolgraph->setShowPercent(1);
317		$dolgraph->SetType(['pie']);
318		$dolgraph->setHeight('150');
319		$dolgraph->setWidth('300');
320		$dolgraph->draw('idgraphcustomerinvoices');
321
322		$result .= '<tr>';
323		$result .= '<td align="center" colspan="2">'.$dolgraph->show($total ? 0 : 1).'</td>';
324		$result .= '</tr>';
325	}
326
327	$result .= '<tr class="liste_total">';
328	$result .= '<td>'.$langs->trans("Total").'</td>';
329	$result .= '<td class="right">'.$total.'</td>';
330	$result .= '</tr>';
331
332	$result .= '</table>';
333	$result .= '</div>';
334
335	return $result;
336}
337
338
339/**
340 * Return a HTML table that contains a pie chart of supplier invoices
341 *
342 * @param	int		$socid		(Optional) Show only results from the supplier with this id
343 * @return	string				A HTML table that contains a pie chart of supplier invoices
344 */
345function getPurchaseInvoicePieChart($socid = 0)
346{
347	global $conf, $db, $langs, $user;
348
349	if (!((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire) || (!empty($conf->supplier_invoice->enabled) && $user->rights->supplier_invoice->lire))) {
350		return '';
351	}
352
353	$sql = "SELECT count(f.rowid), f.fk_statut";
354	$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
355	$sql .= ", ".MAIN_DB_PREFIX."facture_fourn as f";
356	if (!$user->rights->societe->client->voir && !$socid) {
357		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
358	}
359	$sql .= " WHERE f.fk_soc = s.rowid";
360	$sql .= " AND f.entity IN (".getEntity('facture_fourn').")";
361	if ($user->socid) {
362		$sql .= ' AND f.fk_soc = '.$user->socid;
363	}
364	if (!$user->rights->societe->client->voir && !$socid) {
365		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
366	}
367	$sql .= " GROUP BY f.fk_statut";
368
369	$resql = $db->query($sql);
370	if (!$resql) {
371		dol_print_error($db);
372		return '';
373	}
374
375	$num = $db->num_rows($resql);
376	$i = 0;
377
378	$total = 0;
379	$vals = [];
380
381	while ($i < $num) {
382		$row = $db->fetch_row($resql);
383		if ($row) {
384			$vals[$row[1]] = $row[0];
385			$total += $row[0];
386		}
387
388		$i++;
389	}
390
391	$db->free($resql);
392
393	include DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
394
395	$result = '<div class="div-table-responsive-no-min">';
396	$result .= '<table class="noborder nohover centpercent">';
397
398	$result .= '<tr class="liste_titre">';
399	$result .= '<td colspan="2">'.$langs->trans("Statistics").' - '.$langs->trans("SupplierInvoice").'</td>';
400	$result .= '</tr>';
401
402	$objectstatic = new FactureFournisseur($db);
403	$array = [FactureFournisseur::STATUS_DRAFT, FactureFournisseur::STATUS_VALIDATED, FactureFournisseur::STATUS_CLOSED, FactureFournisseur::STATUS_ABANDONED];
404	$dataseries = [];
405
406	foreach ($array as $status) {
407		$objectstatic->statut = $status;
408		$objectstatic->paye = $status == FactureFournisseur::STATUS_CLOSED ? -1 : 0;
409
410		$dataseries[] = [$objectstatic->getLibStatut(1), (isset($vals[$status]) ? (int) $vals[$status] : 0)];
411		if ($status == FactureFournisseur::STATUS_DRAFT) {
412			$colorseries[$status] = '-'.$badgeStatus0;
413		}
414		if ($status == FactureFournisseur::STATUS_VALIDATED) {
415			$colorseries[$status] = $badgeStatus1;
416		}
417		if ($status == FactureFournisseur::STATUS_CLOSED) {
418			$colorseries[$status] = $badgeStatus9;
419		}
420		if ($status == FactureFournisseur::STATUS_ABANDONED) {
421			$colorseries[$status] = $badgeStatus6;
422		}
423
424		if (!$conf->use_javascript_ajax) {
425			$result .= '<tr class="oddeven">';
426			$result .= '<td>'.$objectstatic->getLibStatut(0).'</td>';
427			$result .= '<td class="right"><a href="list.php?statut='.$status.'">'.(isset($vals[$status]) ? $vals[$status] : 0).'</a></td>';
428			$result .= '</tr>';
429		}
430	}
431
432	if ($conf->use_javascript_ajax) {
433		$dolgraph = new DolGraph();
434		$dolgraph->SetData($dataseries);
435		$dolgraph->SetDataColor(array_values($colorseries));
436		$dolgraph->setShowLegend(2);
437		$dolgraph->setShowPercent(1);
438		$dolgraph->SetType(['pie']);
439		$dolgraph->setHeight('150');
440		$dolgraph->setWidth('300');
441		$dolgraph->draw('idgraphpurchaseinvoices');
442
443		$result .= '<tr>';
444		$result .= '<td align="center" colspan="2">'.$dolgraph->show($total ? 0 : 1).'</td>';
445		$result .= '</tr>';
446	}
447
448	$result .= '<tr class="liste_total">';
449	$result .= '<td>'.$langs->trans("Total").'</td>';
450	$result .= '<td class="right">'.$total.'</td>';
451	$result .= '</tr>';
452
453	$result .= '</table>';
454	$result .= '</div>';
455
456	return $result;
457}
458
459/**
460 * Return an HTML table that contains a pie chart of the number of customers or supplier invoices
461 *
462 * @param 	string 	$mode 		Can be 'customers' or 'suppliers'
463 * @return 	string 				A HTML table that contains a pie chart of customers or supplier invoices
464 */
465function getNumberInvoicesPieChart($mode)
466{
467	global $conf, $db, $langs, $user;
468	if (!empty($conf->facture->enabled) && !empty($user->rights->facture->lire)) {
469		include DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
470
471		$now = date_create(date('Y-m-d', dol_now()));
472		$datenowsub30 = date_create(date('Y-m-d', dol_now()));
473		$datenowsub15 = date_create(date('Y-m-d', dol_now()));
474		$datenowadd30 = date_create(date('Y-m-d', dol_now()));
475		$datenowadd15 = date_create(date('Y-m-d', dol_now()));
476		$interval30days = date_interval_create_from_date_string('30 days');
477		$interval15days = date_interval_create_from_date_string('15 days');
478		date_sub($datenowsub30, $interval30days);
479		date_sub($datenowsub15, $interval15days);
480		date_add($datenowadd30, $interval30days);
481		date_add($datenowadd15, $interval15days);
482
483		$sql = "SELECT sum(".$db->ifsql("f.date_lim_reglement < '".date_format($datenowsub30, 'Y-m-d')."'", 1, 0).") as nblate30";
484		$sql .= ", sum(".$db->ifsql("f.date_lim_reglement < '".date_format($datenowsub15, 'Y-m-d')."'", 1, 0).") as nblate15";
485		$sql .= ", sum(".$db->ifsql("f.date_lim_reglement < '".date_format($now, 'Y-m-d')."'", 1, 0).") as nblatenow";
486		$sql .= ", sum(".$db->ifsql("f.date_lim_reglement >= '".date_format($now, 'Y-m-d')."'", 1, 0).") as nbnotlatenow";
487		$sql .= ", sum(".$db->ifsql("f.date_lim_reglement > '".date_format($datenowadd15, 'Y-m-d')."'", 1, 0).") as nbnotlate15";
488		$sql .= ", sum(".$db->ifsql("f.date_lim_reglement > '".date_format($datenowadd30, 'Y-m-d')."'", 1, 0).") as nbnotlate30";
489		if ($mode == 'customers') {
490			$sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
491		} elseif ($mode == 'fourn' || $mode == 'suppliers') {
492			$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
493		} else {
494			return '';
495		}
496		$sql .= " WHERE f.type <> 2";
497		$sql .= " AND f.fk_statut = 1";
498		if (isset($user->socid) && $user->socid > 0) {
499			$sql .= " AND f.fk_soc = ".((int) $user->socid);
500		}
501
502		$resql = $db->query($sql);
503		if ($resql) {
504			$num = $db->num_rows($resql);
505			$i = 0;
506			$total = 0;
507			$dataseries = array();
508
509			while ($i < $num) {
510				$obj = $db->fetch_object($resql);
511				$dataseries = array(array($langs->trans('InvoiceLate30Days'), $obj->nblate30)
512									,array($langs->trans('InvoiceLate15Days'), $obj->nblate15 - $obj->nblate30)
513									,array($langs->trans('InvoiceLateMinus15Days'), $obj->nblatenow - $obj->nblate15)
514									,array($langs->trans('InvoiceNotLate'), $obj->nbnotlatenow - $obj->nbnotlate15)
515									,array($langs->trans('InvoiceNotLate15Days'), $obj->nbnotlate15 - $obj->nbnotlate30)
516									,array($langs->trans('InvoiceNotLate30Days'), $obj->nbnotlate30));
517				$i++;
518			}
519			foreach ($dataseries as $key=>$value) {
520				$total += $value[1];
521			}
522
523			$colorseries = array($badgeStatus8, $badgeStatus1, $badgeStatus3, $badgeStatus4, $badgeStatus11, '-'.$badgeStatus11);
524
525			$result = '<div class="div-table-responsive-no-min">';
526			$result .= '<table class="noborder nohover centpercent">';
527			$result .= '<tr class="liste_titre">';
528			$result .= '<td>'.$langs->trans("Statistics").' - ';
529			if ($mode == 'customers') {
530				$result .= $langs->trans("CustomerInvoice");
531			} elseif ($mode == 'fourn' || $mode == 'suppliers') {
532				$result .= $langs->trans("SupplierInvoice");
533			} else {
534				return '';
535			}
536			$result .= '</td>';
537			$result .= '</tr>';
538
539			if ($conf->use_javascript_ajax) {
540				$dolgraph = new DolGraph();
541				$dolgraph->SetData($dataseries);
542				$dolgraph->SetDataColor(array_values($colorseries));
543				$dolgraph->setShowLegend(2);
544				$dolgraph->setShowPercent(1);
545				$dolgraph->SetType(['pie']);
546				$dolgraph->setHeight('150');
547				$dolgraph->setWidth('300');
548				if ($mode == 'customers') {
549					$dolgraph->draw('idgraphcustomerinvoices');
550				} elseif ($mode == 'fourn' || $mode == 'suppliers') {
551					$dolgraph->draw('idgraphfourninvoices');
552				} else {
553					return '';
554				}
555				$result .= '<tr maxwidth="255">';
556				$result .= '<td class="center">'.$dolgraph->show($total ? 0 : $langs->trans("NoOpenInvoice")).'</td>';
557				$result .= '</tr>';
558			} else {
559				// Print text lines
560			}
561
562			$result .= '</table>';
563			$result .= '</div>';
564
565			return $result;
566		} else {
567			dol_print_error($db);
568		}
569	}
570}
571/**
572 * Return a HTML table that contains a list with customer invoice drafts
573 *
574 * @param	int		$maxCount	(Optional) The maximum count of elements inside the table
575 * @param	int		$socid		(Optional) Show only results from the customer with this id
576 * @return	string				A HTML table that contains a list with customer invoice drafts
577 */
578function getCustomerInvoiceDraftTable($maxCount = 500, $socid = 0)
579{
580	global $conf, $db, $langs, $user, $hookmanager;
581
582	$maxofloop = (empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD);
583
584	$result = '';
585	$tmpinvoice = new Facture($db);
586
587	$sql = "SELECT f.rowid, f.ref, f.datef as date, f.total_ht, f.total_tva, f.total_ttc, f.ref_client";
588	$sql .= ", f.type, f.fk_statut as status, f.paye";
589	$sql .= ", s.nom as name";
590	$sql .= ", s.rowid as socid, s.email";
591	$sql .= ", s.code_client, s.code_compta, s.code_fournisseur, s.code_compta_fournisseur";
592	$sql .= ", cc.rowid as country_id, cc.code as country_code";
593	if (!$user->rights->societe->client->voir && !$socid) {
594		$sql .= ", sc.fk_soc, sc.fk_user ";
595	}
596	$sql .= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."c_country as cc ON cc.rowid = s.fk_pays";
597	if (!$user->rights->societe->client->voir && !$socid) {
598		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
599	}
600	$sql .= " WHERE s.rowid = f.fk_soc AND f.fk_statut = ".Facture::STATUS_DRAFT;
601	$sql .= " AND f.entity IN (".getEntity('invoice').")";
602	if (!$user->rights->societe->client->voir && !$socid) {
603		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
604	}
605
606	if ($socid) {
607		$sql .= " AND f.fk_soc = ".((int) $socid);
608	}
609	// Add where from hooks
610	$parameters = array();
611	$reshook = $hookmanager->executeHooks('printFieldListWhereCustomerDraft', $parameters);
612	$sql .= $hookmanager->resPrint;
613
614	$sql .= " GROUP BY f.rowid, f.ref, f.datef, f.total_ht, f.total_tva, f.total_ttc, f.ref_client, f.type, f.fk_statut, f.paye,";
615	$sql .= " s.nom, s.rowid, s.email, s.code_client, s.code_compta, s.code_fournisseur, s.code_compta_fournisseur,";
616	$sql .= " cc.rowid, cc.code";
617	if (!$user->rights->societe->client->voir && !$socid) {
618		$sql .= ", sc.fk_soc, sc.fk_user";
619	}
620
621	// Add Group from hooks
622	$parameters = array();
623	$reshook = $hookmanager->executeHooks('printFieldListGroupByCustomerDraft', $parameters);
624	$sql .= $hookmanager->resPrint;
625
626	$resql = $db->query($sql);
627
628	if ($resql) {
629		$num = $db->num_rows($resql);
630		$nbofloop = min($num, $maxofloop);
631
632		$result .= '<div class="div-table-responsive-no-min">';
633		$result .= '<table class="noborder centpercent">';
634
635		$result .= '<tr class="liste_titre">';
636		$result .= '<th colspan="3">';
637		$result .= $langs->trans("CustomersDraftInvoices").' ';
638		$result .= '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?search_status='.Facture::STATUS_DRAFT.'">';
639		$result .= '<span class="badge marginleftonlyshort">'.$num.'</span>';
640		$result .= '</a>';
641		$result .= '</th>';
642		$result .= '</tr>';
643
644		if ($num) {
645			$companystatic = new Societe($db);
646
647			$i = 0;
648			$othernb = 0;
649			$tot_ttc = 0;
650			while ($i < $nbofloop) {
651				$obj = $db->fetch_object($resql);
652
653				if ($i >= $maxCount) {
654					$othernb += 1;
655					$i++;
656					$tot_ttc += $obj->total_ttc;
657					continue;
658				}
659
660				$tmpinvoice->id = $obj->rowid;
661				$tmpinvoice->ref = $obj->ref;
662				$tmpinvoice->date = $db->jdate($obj->date);
663				$tmpinvoice->type = $obj->type;
664				$tmpinvoice->total_ht = $obj->total_ht;
665				$tmpinvoice->total_tva = $obj->total_tva;
666				$tmpinvoice->total_ttc = $obj->total_ttc;
667				$tmpinvoice->ref_client = $obj->ref_client;
668				$tmpinvoice->statut = $obj->status;
669				$tmpinvoice->paye = $obj->paye;
670
671				$companystatic->id = $obj->socid;
672				$companystatic->name = $obj->name;
673				$companystatic->email = $obj->email;
674				$companystatic->country_id = $obj->country_id;
675				$companystatic->country_code = $obj->country_code;
676				$companystatic->client = 1;
677				$companystatic->code_client = $obj->code_client;
678				$companystatic->code_fournisseur = $obj->code_fournisseur;
679				$companystatic->code_compta = $obj->code_compta;
680				$companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
681
682				$result .= '<tr class="oddeven">';
683				$result .= '<td class="nowrap tdoverflowmax100">';
684				$result .= $tmpinvoice->getNomUrl(1, '');
685				$result .= '</td>';
686				$result .= '<td class="nowrap tdoverflowmax100">';
687				$result .= $companystatic->getNomUrl(1, 'customer');
688				$result .= '</td>';
689				$result .= '<td class="nowrap right"><span class="amount">'.price($obj->total_ttc).'</span></td>';
690				$result .= '</tr>';
691				$tot_ttc += $obj->total_ttc;
692				$i++;
693			}
694
695			if ($othernb) {
696				$result .= '<tr class="oddeven">';
697				$result .= '<td class="nowrap" colspan="3">';
698				$result .= '<span class="opacitymedium">'.$langs->trans("More").'...'.($othernb < $maxofloop ? ' ('.$othernb.')' : '').'</span>';
699				$result .= '</td>';
700				$result .= "</tr>\n";
701			}
702
703			$result .= '<tr class="liste_total"><td class="left">'.$langs->trans("Total").'</td>';
704			$result .= '<td colspan="2" class="right">'.price($tot_ttc).'</td>';
705			$result .= '</tr>';
706		} else {
707			$result .= '<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("NoInvoice").'</td></tr>';
708		}
709		$result .= "</table></div>";
710		$db->free($resql);
711	} else {
712		dol_print_error($db);
713	}
714
715	return $result;
716}
717
718/**
719 * Return a HTML table that contains a list with customer invoice drafts
720 *
721 * @param	int		$maxCount	(Optional) The maximum count of elements inside the table
722 * @param	int		$socid		(Optional) Show only results from the customer with this id
723 * @return	string				A HTML table that contains a list with customer invoice drafts
724 */
725function getDraftSupplierTable($maxCount = 500, $socid = 0)
726{
727	global $conf, $db, $langs, $user, $hookmanager;
728
729	$maxofloop = (empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD);
730
731	$result = '';
732	$facturesupplierstatic = new FactureFournisseur($db);
733
734	$sql = "SELECT f.ref, f.rowid, f.total_ht, f.total_tva, f.total_ttc, f.type, f.ref_supplier, f.fk_statut as status, f.paye";
735	$sql .= ", s.nom as name";
736	$sql .= ", s.rowid as socid, s.email";
737	$sql .= ", s.code_client, s.code_compta";
738	$sql .= ", s.code_fournisseur, s.code_compta_fournisseur";
739	$sql .= ", cc.rowid as country_id, cc.code as country_code";
740	$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f, ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."c_country as cc ON cc.rowid = s.fk_pays";
741	if (!$user->rights->societe->client->voir && !$socid) {
742		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
743	}
744	$sql .= " WHERE s.rowid = f.fk_soc AND f.fk_statut = ".FactureFournisseur::STATUS_DRAFT;
745	$sql .= " AND f.entity IN (".getEntity('invoice').')';
746	if (!$user->rights->societe->client->voir && !$socid) {
747		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
748	}
749	if ($socid) {
750		$sql .= " AND f.fk_soc = ".((int) $socid);
751	}
752	// Add where from hooks
753	$parameters = array();
754	$reshook = $hookmanager->executeHooks('printFieldListWhereSupplierDraft', $parameters);
755	$sql .= $hookmanager->resPrint;
756	$resql = $db->query($sql);
757
758	if ($resql) {
759		$num = $db->num_rows($resql);
760		$nbofloop = min($num, $maxofloop);
761
762		$result .= '<div class="div-table-responsive-no-min">';
763		$result .= '<table class="noborder centpercent">';
764
765		$result .= '<tr class="liste_titre">';
766		$result .= '<th colspan="3">';
767		$result .= $langs->trans("SuppliersDraftInvoices").' ';
768		$result .= '<a href="'.DOL_URL_ROOT.'/fourn/facture/list.php?search_status='.FactureFournisseur::STATUS_DRAFT.'">';
769		$result .= '<span class="badge marginleftonlyshort">'.$num.'</span>';
770		$result .= '</a>';
771		$result .= '</th>';
772		$result .= '</tr>';
773
774		if ($num) {
775			$companystatic = new Societe($db);
776
777			$i = 0;
778			$othernb = 0;
779			$tot_ttc = 0;
780			while ($i < $nbofloop) {
781				$obj = $db->fetch_object($resql);
782
783				if ($i >= $maxCount) {
784					$othernb += 1;
785					$i++;
786					$tot_ttc += $obj->total_ttc;
787					continue;
788				}
789
790				$facturesupplierstatic->ref = $obj->ref;
791				$facturesupplierstatic->id = $obj->rowid;
792				$facturesupplierstatic->total_ht = $obj->total_ht;
793				$facturesupplierstatic->total_tva = $obj->total_tva;
794				$facturesupplierstatic->total_ttc = $obj->total_ttc;
795				$facturesupplierstatic->ref_supplier = $obj->ref_supplier;
796				$facturesupplierstatic->type = $obj->type;
797				$facturesupplierstatic->statut = $obj->status;
798				$facturesupplierstatic->paye = $obj->paye;
799
800				$companystatic->id = $obj->socid;
801				$companystatic->name = $obj->name;
802				$companystatic->email = $obj->email;
803				$companystatic->country_id = $obj->country_id;
804				$companystatic->country_code = $obj->country_code;
805				$companystatic->fournisseur = 1;
806				$companystatic->code_client = $obj->code_client;
807				$companystatic->code_fournisseur = $obj->code_fournisseur;
808				$companystatic->code_compta = $obj->code_compta;
809				$companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
810
811				$result .= '<tr class="oddeven">';
812				$result .= '<td class="nowrap tdoverflowmax100">';
813				$result .= $facturesupplierstatic->getNomUrl(1, '');
814				$result .= '</td>';
815				$result .= '<td class="nowrap tdoverflowmax100">';
816				$result .= $companystatic->getNomUrl(1, 'supplier');
817				$result .= '</td>';
818				$result .= '<td class="right"><span class="amount">'.price($obj->total_ttc).'</span></td>';
819				$result .= '</tr>';
820				$tot_ttc += $obj->total_ttc;
821				$i++;
822			}
823
824			if ($othernb) {
825				$result .= '<tr class="oddeven">';
826				$result .= '<td class="nowrap" colspan="3">';
827				$result .= '<span class="opacitymedium">'.$langs->trans("More").'...'.($othernb < $maxofloop ? ' ('.$othernb.')' : '').'</span>';
828				$result .= '</td>';
829				$result .= "</tr>\n";
830			}
831
832			$result .= '<tr class="liste_total"><td class="left">'.$langs->trans("Total").'</td>';
833			$result .= '<td colspan="2" class="right">'.price($tot_ttc).'</td>';
834			$result .= '</tr>';
835		} else {
836			$result .= '<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("NoInvoice").'</td></tr>';
837		}
838		$result .= "</table></div>";
839		$db->free($resql);
840	} else {
841		dol_print_error($db);
842	}
843
844	return $result;
845}
846
847
848/**
849 * Return a HTML table that contains a list with latest edited customer invoices
850 *
851 * @param	int		$maxCount	(Optional) The maximum count of elements inside the table
852 * @param	int		$socid		(Optional) Show only results from the customer with this id
853 * @return	string				A HTML table that contains a list with latest edited customer invoices
854 */
855function getCustomerInvoiceLatestEditTable($maxCount = 5, $socid = 0)
856{
857	global $conf, $db, $langs, $user;
858
859	$sql = "SELECT f.rowid, f.entity, f.ref, f.fk_statut as status, f.paye, f.type, f.total_ht, f.total_tva, f.total_ttc, f.datec,";
860	$sql .= " s.nom as socname, s.rowid as socid, s.canvas, s.client";
861	$sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
862	$sql .= ", ".MAIN_DB_PREFIX."societe as s";
863	if (!$user->rights->societe->client->voir && !$socid) {
864		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
865	}
866	$sql .= " WHERE f.fk_soc = s.rowid";
867	$sql .= " AND f.entity IN (".getEntity('facture').")";
868	if ($socid) {
869		$sql .= " AND f.fk_soc = ".((int) $socid);
870	}
871	if (!$user->rights->societe->client->voir && !$socid) {
872		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
873	}
874	$sql .= " ORDER BY f.tms DESC";
875	$sql .= $db->plimit($maxCount, 0);
876
877	$resql = $db->query($sql);
878	if (!$resql) {
879		dol_print_error($db);
880	}
881
882	$num = $db->num_rows($resql);
883
884	$result = '<div class="div-table-responsive-no-min">';
885	$result .= '<table class="noborder centpercent">';
886
887	$result .= '<tr class="liste_titre">';
888	$result .= '<th colspan="3">'.$langs->trans("LastCustomersBills", $maxCount).'</th>';
889	$result .= '<th class="right">'.$langs->trans("AmountTTC").'</th>';
890	$result .= '<th class="right"></th>';
891	$result .= '</tr>';
892
893	if ($num < 1) {
894		$result .= '</table>';
895		$result .= '</div>';
896		return $result;
897	}
898
899	$formfile = new FormFile($db);
900	$objectstatic = new Facture($db);
901	$companystatic = new Societe($db);
902	$i = 0;
903
904	while ($i < $num) {
905		$obj = $db->fetch_object($resql);
906
907		$objectstatic->id = $obj->rowid;
908		$objectstatic->ref = $obj->ref;
909		$objectstatic->paye = $obj->paye;
910		$objectstatic->statut = $obj->status;
911		$objectstatic->total_ht = $obj->total_ht;
912		$objectstatic->total_tva = $obj->total_tva;
913		$objectstatic->total_ttc = $obj->total_ttc;
914		$objectstatic->type = $obj->type;
915
916		$companystatic->id = $obj->socid;
917		$companystatic->name = $obj->socname;
918		$companystatic->client	= $obj->client;
919		$companystatic->canvas	= $obj->canvas;
920
921		$filename = dol_sanitizeFileName($obj->ref);
922		$filedir = $conf->propal->multidir_output[$obj->entity].'/'.$filename;
923
924		$result .= '<tr class="nowrap">';
925
926		$result .= '<td class="oddeven">';
927		$result .= '<table class="nobordernopadding">';
928		$result .= '<tr class="nocellnopadd">';
929
930		$result .= '<td width="96" class="nobordernopadding nowrap">'.$objectstatic->getNomUrl(1).'</td>';
931		$result .= '<td width="16" class="nobordernopadding nowrap">&nbsp;</td>';
932		$result .= '<td width="16" class="nobordernopadding right">'.$formfile->getDocumentsLink($objectstatic->element, $filename, $filedir).'</td>';
933
934		$result .= '</tr>';
935		$result .= '</table>';
936		$result .= '</td>';
937
938		$result .= '<td class="tdoverflowmax150">'.$companystatic->getNomUrl(1, 'customer').'</td>';
939		$result .= '<td>'.dol_print_date($db->jdate($obj->datec), 'day').'</td>';
940		$result .= '<td class="right amount">'.price($obj->total_ttc).'</td>';
941		$result .= '<td class="right">'.$objectstatic->getLibStatut(5).'</td>';
942
943		$result .= '</tr>';
944
945		$i++;
946	}
947
948	$result .= '</table>';
949	$result .= '</div>';
950	return $result;
951}
952
953/**
954 * Return a HTML table that contains a list with latest edited supplier invoices
955 *
956 * @param	int		$maxCount	(Optional) The maximum count of elements inside the table
957 * @param	int		$socid		(Optional) Show only results from the supplier with this id
958 * @return	string				A HTML table that contains a list with latest edited supplier invoices
959 */
960function getPurchaseInvoiceLatestEditTable($maxCount = 5, $socid = 0)
961{
962	global $conf, $db, $langs, $user;
963
964	$sql = "SELECT f.rowid, f.entity, f.ref, f.fk_statut as status, f.paye, f.total_ht, f.total_tva, f.total_ttc, f.type, f.ref_supplier, f.datec,";
965	$sql .= " s.nom as socname, s.rowid as socid, s.canvas, s.client";
966	$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
967	$sql .= ", ".MAIN_DB_PREFIX."societe as s";
968	if (!$user->rights->societe->client->voir && !$socid) {
969		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
970	}
971	$sql .= " WHERE f.fk_soc = s.rowid";
972	$sql .= " AND f.entity IN (".getEntity('facture_fourn').")";
973	if ($socid) {
974		$sql .= " AND f.fk_soc = ".((int) $socid);
975	}
976	if (!$user->rights->societe->client->voir && !$socid) {
977		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
978	}
979	$sql .= " ORDER BY f.tms DESC";
980	$sql .= $db->plimit($maxCount, 0);
981
982	$resql = $db->query($sql);
983	if (!$resql) {
984		dol_print_error($db);
985		return '';
986	}
987
988	$num = $db->num_rows($resql);
989
990	$result = '<div class="div-table-responsive-no-min">';
991	$result .= '<table class="noborder centpercent">';
992	$result .= '<tr class="liste_titre">';
993	$result .= '<th colspan="3">'.$langs->trans("BoxTitleLastSupplierBills", $maxCount).'</th>';
994	$result .= '<th class="right">'.$langs->trans("AmountTTC").'</th>';
995	$result .= '<th class="right"></th>';
996	$result .= '</tr>';
997
998	if ($num < 1) {
999		$result .= '</table>';
1000		$result .= '</div>';
1001		return $result;
1002	}
1003
1004	$objectstatic = new FactureFournisseur($db);
1005	$companystatic = new Societe($db);
1006	$formfile = new FormFile($db);
1007	$i = 0;
1008
1009	while ($i < $num) {
1010		$obj = $db->fetch_object($resql);
1011
1012		$objectstatic->id = $obj->rowid;
1013		$objectstatic->ref = $obj->ref;
1014		$objectstatic->paye = $obj->paye;
1015		$objectstatic->statut = $obj->status;
1016		$objectstatic->total_ht = $obj->total_ht;
1017		$objectstatic->total_tva = $obj->total_tva;
1018		$objectstatic->total_ttc = $obj->total_ttc;
1019		$objectstatic->type = $obj->type;
1020
1021		$companystatic->id = $obj->socid;
1022		$companystatic->name = $obj->socname;
1023		$companystatic->client = $obj->client;
1024		$companystatic->canvas = $obj->canvas;
1025
1026		$filename = dol_sanitizeFileName($obj->ref);
1027		$filedir = $conf->propal->multidir_output[$obj->entity].'/'.$filename;
1028
1029		$result .= '<tr class="nowrap">';
1030
1031		$result .= '<td class="oddeven">';
1032		$result .= '<table class="nobordernopadding">';
1033		$result .= '<tr class="nocellnopadd">';
1034
1035		$result .= '<td width="96" class="nobordernopadding nowrap">'.$objectstatic->getNomUrl(1).'</td>';
1036		$result .= '<td width="16" class="nobordernopadding nowrap">&nbsp;</td>';
1037		$result .= '<td width="16" class="nobordernopadding right">'.$formfile->getDocumentsLink($objectstatic->element, $filename, $filedir).'</td>';
1038
1039		$result .= '</tr>';
1040		$result .= '</table>';
1041		$result .= '</td>';
1042
1043		$result .= '<td class="tdoverflowmax150">'.$companystatic->getNomUrl(1, 'supplier').'</td>';
1044
1045		$result .= '<td>'.dol_print_date($db->jdate($obj->datec), 'day').'</td>';
1046
1047		$result .= '<td class="amount right">'.price($obj->total_ttc).'</td>';
1048
1049		$result .= '<td class="right">'.$objectstatic->getLibStatut(5).'</td>';
1050
1051		$result .= '</tr>';
1052
1053		$i++;
1054	}
1055
1056	$result .= '</table>';
1057	$result .= '</div>';
1058	return $result;
1059}
1060
1061/**
1062 * Return a HTML table that contains of unpaid customers invoices
1063 *
1064 * @param	int		$maxCount	(Optional) The maximum count of elements inside the table
1065 * @param	int		$socid		(Optional) Show only results from the supplier with this id
1066 * @return	string				A HTML table that conatins a list with open (unpaid) supplier invoices
1067 */
1068function getCustomerInvoiceUnpaidOpenTable($maxCount = 500, $socid = 0)
1069{
1070	global $conf, $db, $langs, $user, $hookmanager;
1071
1072	$result = '';
1073
1074	if (!empty($conf->facture->enabled) && !empty($user->rights->facture->lire)) {
1075		$tmpinvoice = new Facture($db);
1076
1077		$sql = "SELECT f.rowid, f.ref, f.fk_statut as status, f.datef, f.type, f.total_ht, f.total_tva, f.total_ttc, f.paye, f.tms";
1078		$sql .= ", f.date_lim_reglement as datelimite";
1079		$sql .= ", s.nom as name";
1080		$sql .= ", s.rowid as socid, s.email";
1081		$sql .= ", s.code_client, s.code_compta";
1082		$sql .= ", s.code_fournisseur, s.code_compta_fournisseur";
1083		$sql .= ", cc.rowid as country_id, cc.code as country_code";
1084		$sql .= ", sum(pf.amount) as am";
1085		$sql .= " FROM ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."c_country as cc ON cc.rowid = s.fk_pays,".MAIN_DB_PREFIX."facture as f";
1086		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf on f.rowid=pf.fk_facture";
1087		if (!$user->rights->societe->client->voir && !$socid) {
1088			$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
1089		}
1090		$sql .= " WHERE s.rowid = f.fk_soc AND f.paye = 0 AND f.fk_statut = ".Facture::STATUS_VALIDATED;
1091		$sql .= " AND f.entity IN (".getEntity('invoice').')';
1092		if (!$user->rights->societe->client->voir && !$socid) {
1093			$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
1094		}
1095		if ($socid) {
1096			$sql .= " AND f.fk_soc = ".((int) $socid);
1097		}
1098		// Add where from hooks
1099		$parameters = array();
1100		$reshook = $hookmanager->executeHooks('printFieldListWhereCustomerUnpaid', $parameters);
1101		$sql .= $hookmanager->resPrint;
1102
1103		$sql .= " GROUP BY f.rowid, f.ref, f.fk_statut, f.datef, f.type, f.total_ht, f.total_tva, f.total_ttc, f.paye, f.tms, f.date_lim_reglement,";
1104		$sql .= " s.nom, s.rowid, s.email, s.code_client, s.code_compta, cc.rowid, cc.code";
1105		$sql .= ", s.code_fournisseur, s.code_compta_fournisseur";
1106		$sql .= " ORDER BY f.datef ASC, f.ref ASC";
1107
1108		$resql = $db->query($sql);
1109		if ($resql) {
1110			$num = $db->num_rows($resql);
1111			$i = 0;
1112			$othernb = 0;
1113
1114			$formfile = new FormFile($db);
1115
1116			print '<div class="div-table-responsive-no-min">';
1117			print '<table class="noborder centpercent">';
1118
1119			print '<tr class="liste_titre">';
1120			print '<th colspan="2">';
1121			print $langs->trans("BillsCustomersUnpaid", $num).' ';
1122			print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?search_status='.Facture::STATUS_VALIDATED.'">';
1123			print '<span class="badge">'.$num.'</span>';
1124			print '</a>';
1125			print '</th>';
1126
1127			print '<th class="right">'.$langs->trans("DateDue").'</th>';
1128			if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
1129				print '<th class="right">'.$langs->trans("AmountHT").'</th>';
1130			}
1131			print '<th class="right">'.$langs->trans("AmountTTC").'</th>';
1132			print '<th class="right">'.$langs->trans("Received").'</th>';
1133			print '<th width="16">&nbsp;</th>';
1134			print '</tr>';
1135			if ($num) {
1136				$societestatic = new Societe($db);
1137				$total_ttc = $totalam = $total = 0;
1138				while ($i < $num) {
1139					$obj = $db->fetch_object($resql);
1140
1141					if ($i >= $maxCount) {
1142						$othernb += 1;
1143						$i++;
1144						$total += $obj->total_ht;
1145						$total_ttc += $obj->total_ttc;
1146						continue;
1147					}
1148
1149					$tmpinvoice->ref = $obj->ref;
1150					$tmpinvoice->id = $obj->rowid;
1151					$tmpinvoice->total_ht = $obj->total_ht;
1152					$tmpinvoice->total_tva = $obj->total_tva;
1153					$tmpinvoice->total_ttc = $obj->total_ttc;
1154					$tmpinvoice->type = $obj->type;
1155					$tmpinvoice->statut = $obj->status;
1156					$tmpinvoice->paye = $obj->paye;
1157					$tmpinvoice->date_lim_reglement = $db->jdate($obj->datelimite);
1158
1159					$societestatic->id = $obj->socid;
1160					$societestatic->name = $obj->name;
1161					$societestatic->email = $obj->email;
1162					$societestatic->country_id = $obj->country_id;
1163					$societestatic->country_code = $obj->country_code;
1164					$societestatic->client = 1;
1165					$societestatic->code_client = $obj->code_client;
1166					$societestatic->code_fournisseur = $obj->code_fournisseur;
1167					$societestatic->code_compta = $obj->code_compta;
1168					$societestatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
1169
1170					print '<tr class="oddeven">';
1171					print '<td class="nowrap">';
1172
1173					print '<table class="nobordernopadding"><tr class="nocellnopadd">';
1174					print '<td class="nobordernopadding nowrap">';
1175					print $tmpinvoice->getNomUrl(1, '');
1176					print '</td>';
1177					print '<td width="20" class="nobordernopadding nowrap">';
1178					if ($tmpinvoice->hasDelay()) {
1179						print img_warning($langs->trans("Late"));
1180					}
1181					print '</td>';
1182					print '<td width="16" class="nobordernopadding hideonsmartphone right">';
1183					$filename = dol_sanitizeFileName($obj->ref);
1184					$filedir = $conf->facture->dir_output.'/'.dol_sanitizeFileName($obj->ref);
1185					$urlsource = $_SERVER['PHP_SELF'].'?facid='.$obj->rowid;
1186					print $formfile->getDocumentsLink($tmpinvoice->element, $filename, $filedir);
1187					print '</td></tr></table>';
1188
1189					print '</td>';
1190					print '<td class="nowrap tdoverflowmax100">';
1191					print $societestatic->getNomUrl(1, 'customer');
1192					print '</td>';
1193					print '<td class="right">'.dol_print_date($db->jdate($obj->datelimite), 'day').'</td>';
1194					if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
1195						print '<td class="right"><span class="amount">'.price($obj->total_ht).'</span></td>';
1196					}
1197					print '<td class="nowrap right"><span class="amount">'.price($obj->total_ttc).'</span></td>';
1198					print '<td class="nowrap right"><span class="amount">'.price($obj->am).'</span></td>';
1199					print '<td>'.$tmpinvoice->getLibStatut(3, $obj->am).'</td>';
1200					print '</tr>';
1201
1202					$total_ttc += $obj->total_ttc;
1203					$total += $obj->total_ht;
1204					$totalam += $obj->am;
1205
1206					$i++;
1207				}
1208
1209				if ($othernb) {
1210					$colspan = 6;
1211					if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
1212						$colspan++;
1213					}
1214					print '<tr class="oddeven">';
1215					print '<td class="nowrap" colspan="'.$colspan.'">';
1216					print '<span class="opacitymedium">'.$langs->trans("More").'... ('.$othernb.')</span>';
1217					print '</td>';
1218					print "</tr>\n";
1219				}
1220
1221				print '<tr class="liste_total"><td colspan="2">'.$langs->trans("Total").' &nbsp; <font style="font-weight: normal">('.$langs->trans("RemainderToTake").': '.price($total_ttc - $totalam).')</font> </td>';
1222				print '<td>&nbsp;</td>';
1223				if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
1224					print '<td class="right"><span class="amount">'.price($total).'</span></td>';
1225				}
1226				print '<td class="nowrap right"><span class="amount">'.price($total_ttc).'</span></td>';
1227				print '<td class="nowrap right"><span class="amount">'.price($totalam).'</span></td>';
1228				print '<td>&nbsp;</td>';
1229				print '</tr>';
1230			} else {
1231				$colspan = 6;
1232				if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
1233					$colspan++;
1234				}
1235				print '<tr class="oddeven"><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("NoInvoice").'</td></tr>';
1236			}
1237			print '</table></div><br>';
1238			$db->free($resql);
1239		} else {
1240			dol_print_error($db);
1241		}
1242	}
1243
1244	return $result;
1245}
1246
1247
1248/**
1249 * Return a HTML table that contains of unpaid purchase invoices
1250 *
1251 * @param	int		$maxCount	(Optional) The maximum count of elements inside the table
1252 * @param	int		$socid		(Optional) Show only results from the supplier with this id
1253 * @return	string				A HTML table that conatins a list with open (unpaid) supplier invoices
1254 */
1255function getPurchaseInvoiceUnpaidOpenTable($maxCount = 500, $socid = 0)
1256{
1257	global $conf, $db, $langs, $user, $hookmanager;
1258
1259	$result = '';
1260
1261	if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire) || (!empty($conf->supplier_invoice->enabled) && $user->rights->supplier_invoice->lire)) {
1262		$facstatic = new FactureFournisseur($db);
1263
1264		$sql = "SELECT ff.rowid, ff.ref, ff.fk_statut as status, ff.type, ff.libelle as label, ff.total_ht, ff.total_tva, ff.total_ttc, ff.paye";
1265		$sql .= ", ff.date_lim_reglement";
1266		$sql .= ", s.nom as name";
1267		$sql .= ", s.rowid as socid, s.email";
1268		$sql .= ", s.code_client, s.code_compta";
1269		$sql .= ", s.code_fournisseur, s.code_compta_fournisseur";
1270		$sql .= ", sum(pf.amount) as am";
1271		$sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture_fourn as ff";
1272		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf on ff.rowid=pf.fk_facturefourn";
1273		if (!$user->rights->societe->client->voir && !$socid) {
1274			$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
1275		}
1276		$sql .= " WHERE s.rowid = ff.fk_soc";
1277		$sql .= " AND ff.entity = ".$conf->entity;
1278		$sql .= " AND ff.paye = 0";
1279		$sql .= " AND ff.fk_statut = ".FactureFournisseur::STATUS_VALIDATED;
1280		if (!$user->rights->societe->client->voir && !$socid) {
1281			$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
1282		}
1283		if ($socid) {
1284			$sql .= " AND ff.fk_soc = ".((int) $socid);
1285		}
1286		// Add where from hooks
1287		$parameters = array();
1288		$reshook = $hookmanager->executeHooks('printFieldListWhereSupplierUnpaid', $parameters);
1289		$sql .= $hookmanager->resPrint;
1290
1291		$sql .= " GROUP BY ff.rowid, ff.ref, ff.fk_statut, ff.type, ff.libelle, ff.total_ht, ff.total_tva, ff.total_ttc, ff.paye, ff.date_lim_reglement,";
1292		$sql .= " s.nom, s.rowid, s.email, s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur";
1293		$sql .= " ORDER BY ff.date_lim_reglement ASC";
1294
1295		$resql = $db->query($sql);
1296		if ($resql) {
1297			$num = $db->num_rows($resql);
1298			$othernb = 0;
1299
1300			$formfile = new FormFile($db);
1301
1302			print '<div class="div-table-responsive-no-min">';
1303			print '<table class="noborder centpercent">';
1304
1305			print '<tr class="liste_titre">';
1306			print '<th colspan="2">';
1307			print $langs->trans("BillsSuppliersUnpaid", $num).' ';
1308			print '<a href="'.DOL_URL_ROOT.'/fourn/facture/list.php?search_status='.FactureFournisseur::STATUS_VALIDATED.'">';
1309			print '<span class="badge">'.$num.'</span>';
1310			print '</a>';
1311			print '</th>';
1312
1313			print '<th class="right">'.$langs->trans("DateDue").'</th>';
1314			if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
1315				print '<th class="right">'.$langs->trans("AmountHT").'</th>';
1316			}
1317			print '<th class="right">'.$langs->trans("AmountTTC").'</th>';
1318			print '<th class="right">'.$langs->trans("Paid").'</th>';
1319			print '<th width="16">&nbsp;</th>';
1320			print "</tr>\n";
1321			$societestatic = new Societe($db);
1322			if ($num) {
1323				$i = 0;
1324				$total = $total_ttc = $totalam = 0;
1325				while ($i < $num) {
1326					$obj = $db->fetch_object($resql);
1327
1328					if ($i >= $maxCount) {
1329						$othernb += 1;
1330						$i++;
1331						$total += $obj->total_ht;
1332						$total_ttc += $obj->total_ttc;
1333						continue;
1334					}
1335
1336					$facstatic->ref = $obj->ref;
1337					$facstatic->id = $obj->rowid;
1338					$facstatic->type = $obj->type;
1339					$facstatic->total_ht = $obj->total_ht;
1340					$facstatic->total_tva = $obj->total_tva;
1341					$facstatic->total_ttc = $obj->total_ttc;
1342					$facstatic->statut = $obj->status;
1343					$facstatic->paye = $obj->paye;
1344
1345					$societestatic->id = $obj->socid;
1346					$societestatic->name = $obj->name;
1347					$societestatic->email = $obj->email;
1348					$societestatic->client = 0;
1349					$societestatic->fournisseur = 1;
1350					$societestatic->code_client = $obj->code_client;
1351					$societestatic->code_fournisseur = $obj->code_fournisseur;
1352					$societestatic->code_compta = $obj->code_compta;
1353					$societestatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
1354
1355					print '<tr class="oddeven">';
1356					print '<td class="nowrap tdoverflowmax100">';
1357					print $facstatic->getNomUrl(1, '');
1358					print '</td>';
1359					print '<td class="nowrap tdoverflowmax100">'.$societestatic->getNomUrl(1, 'supplier').'</td>';
1360					print '<td class="right">'.dol_print_date($db->jdate($obj->date_lim_reglement), 'day').'</td>';
1361					if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
1362						print '<td class="right"><span class="amount">'.price($obj->total_ht).'</span></td>';
1363					}
1364					print '<td class="nowrap right"><span class="amount">'.price($obj->total_ttc).'</span></td>';
1365					print '<td class="nowrap right"><span class="amount">'.price($obj->am).'</span></td>';
1366					print '<td>'.$facstatic->getLibStatut(3, $obj->am).'</td>';
1367					print '</tr>';
1368					$total += $obj->total_ht;
1369					$total_ttc += $obj->total_ttc;
1370					$totalam += $obj->am;
1371					$i++;
1372				}
1373
1374				if ($othernb) {
1375					$colspan = 6;
1376					if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
1377						$colspan++;
1378					}
1379					print '<tr class="oddeven">';
1380					print '<td class="nowrap" colspan="'.$colspan.'">';
1381					print '<span class="opacitymedium">'.$langs->trans("More").'... ('.$othernb.')</span>';
1382					print '</td>';
1383					print "</tr>\n";
1384				}
1385
1386				print '<tr class="liste_total"><td colspan="2">'.$langs->trans("Total").' &nbsp; <font style="font-weight: normal">('.$langs->trans("RemainderToPay").': '.price($total_ttc - $totalam).')</font> </td>';
1387				print '<td>&nbsp;</td>';
1388				if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
1389					print '<td class="right">'.price($total).'</td>';
1390				}
1391				print '<td class="nowrap right">'.price($total_ttc).'</td>';
1392				print '<td class="nowrap right">'.price($totalam).'</td>';
1393				print '<td>&nbsp;</td>';
1394				print '</tr>';
1395			} else {
1396				$colspan = 6;
1397				if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) {
1398					$colspan++;
1399				}
1400				print '<tr class="oddeven"><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("NoInvoice").'</td></tr>';
1401			}
1402			print '</table></div><br>';
1403		} else {
1404			dol_print_error($db);
1405		}
1406	}
1407
1408	return $result;
1409}
1410