1<?php
2/* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2009 Laurent Destailleur  <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2009 Regis Houssin        <regis.houssin@inodbox.com>
5 * Copyright (C) 2015      Frederic France      <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21/**
22 *	\file       htdocs/core/boxes/box_factures.php
23 *	\ingroup    factures
24 *	\brief      Module de generation de l'affichage de la box factures
25 */
26include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
27
28/**
29 * Class to manage the box to show last invoices
30 */
31class box_factures extends ModeleBoxes
32{
33	public $boxcode = "lastcustomerbills";
34	public $boximg = "object_bill";
35	public $boxlabel = "BoxLastCustomerBills";
36	public $depends = array("facture");
37
38	/**
39	 * @var DoliDB Database handler.
40	 */
41	public $db;
42
43	public $param;
44
45	public $info_box_head = array();
46	public $info_box_contents = array();
47
48
49	/**
50	 *  Constructor
51	 *
52	 *  @param  DoliDB  $db         Database handler
53	 *  @param  string  $param      More parameters
54	 */
55	public function __construct($db, $param)
56	{
57		global $user;
58
59		$this->db = $db;
60
61		$this->hidden = !($user->rights->facture->lire);
62	}
63
64	/**
65	 *  Load data into info_box_contents array to show array later.
66	 *
67	 *  @param	int		$max        Maximum number of records to load
68	 *  @return	void
69	 */
70	public function loadBox($max = 5)
71	{
72		global $conf, $user, $langs;
73
74		$this->max = $max;
75
76		include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
77		include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
78
79		$facturestatic = new Facture($this->db);
80		$societestatic = new Societe($this->db);
81
82		$langs->load("bills");
83
84		$text = $langs->trans("BoxTitleLast".(!empty($conf->global->MAIN_LASTBOX_ON_OBJECT_DATE) ? "" : "Modified")."CustomerBills", $max);
85		$this->info_box_head = array(
86			'text' => $text,
87			'limit'=> dol_strlen($text)
88		);
89
90		if ($user->rights->facture->lire) {
91			$sql = "SELECT f.rowid as facid";
92			$sql .= ", f.ref, f.type, f.total_ht";
93			$sql .= ", f.total_tva";
94			$sql .= ", f.total_ttc";
95			$sql .= ", f.datef as df";
96			$sql .= ", f.paye, f.fk_statut as status, f.datec, f.tms";
97			$sql .= ", f.date_lim_reglement as datelimite";
98			$sql .= ", s.rowid as socid, s.nom as name, s.name_alias";
99			$sql .= ", s.code_client, s.code_compta, s.client";
100			$sql .= ", s.logo, s.email, s.entity";
101			$sql .= ", s.tva_intra, s.siren as idprof1, s.siret as idprof2, s.ape as idprof3, s.idprof4, s.idprof5, s.idprof6";
102			$sql .= " FROM (".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
103			if (!$user->rights->societe->client->voir && !$user->socid) {
104				$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
105			}
106			$sql .= ")";
107			$sql .= " WHERE f.fk_soc = s.rowid";
108			$sql .= " AND f.entity IN (".getEntity('invoice').")";
109			if (!$user->rights->societe->client->voir && !$user->socid) {
110				$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
111			}
112			if ($user->socid) {
113				$sql .= " AND s.rowid = ".((int) $user->socid);
114			}
115			if (!empty($conf->global->MAIN_LASTBOX_ON_OBJECT_DATE)) {
116				$sql .= " ORDER BY f.datef DESC, f.ref DESC ";
117			} else {
118				$sql .= " ORDER BY f.tms DESC, f.ref DESC ";
119			}
120			$sql .= $this->db->plimit($max, 0);
121
122			$result = $this->db->query($sql);
123			if ($result) {
124				$num = $this->db->num_rows($result);
125				$now = dol_now();
126
127				$line = 0;
128				$l_due_date = $langs->trans('Late').' ('.$langs->trans('DateDue').': %s)';
129
130				while ($line < $num) {
131					$objp = $this->db->fetch_object($result);
132					$datelimite = $this->db->jdate($objp->datelimite);
133					$date = $this->db->jdate($objp->df);
134					$datem = $this->db->jdate($objp->tms);
135
136					$facturestatic->id = $objp->facid;
137					$facturestatic->ref = $objp->ref;
138					$facturestatic->type = $objp->type;
139					$facturestatic->total_ht = $objp->total_ht;
140					$facturestatic->total_tva = $objp->total_tva;
141					$facturestatic->total_ttc = $objp->total_ttc;
142					$facturestatic->statut = $objp->status;
143					$facturestatic->status = $objp->status;
144					$facturestatic->date_lim_reglement = $this->db->jdate($objp->datelimite);
145					$facturestatic->alreadypaid = $objp->paye;
146
147					$societestatic->id = $objp->socid;
148					$societestatic->name = $objp->name;
149					//$societestatic->name_alias = $objp->name_alias;
150					$societestatic->code_client = $objp->code_client;
151					$societestatic->code_compta = $objp->code_compta;
152					$societestatic->client = $objp->client;
153					$societestatic->logo = $objp->logo;
154					$societestatic->email = $objp->email;
155					$societestatic->entity = $objp->entity;
156					$societestatic->tva_intra = $objp->tva_intra;
157					$societestatic->idprof1 = $objp->idprof1;
158					$societestatic->idprof2 = $objp->idprof2;
159					$societestatic->idprof3 = $objp->idprof3;
160					$societestatic->idprof4 = $objp->idprof4;
161					$societestatic->idprof5 = $objp->idprof5;
162					$societestatic->idprof6 = $objp->idprof6;
163
164					$late = '';
165					if ($facturestatic->hasDelay()) {
166						$late = img_warning(sprintf($l_due_date, dol_print_date($datelimite, 'day', 'tzuserrel')));
167					}
168
169					$this->info_box_contents[$line][] = array(
170						'td' => 'class="nowraponall"',
171						'text' => $facturestatic->getNomUrl(1),
172						'text2'=> $late,
173						'asis' => 1,
174					);
175
176					$this->info_box_contents[$line][] = array(
177						'td' => 'class="tdoverflowmax200"',
178						'text' => $societestatic->getNomUrl(1, '', 40),
179						'asis' => 1,
180					);
181
182					$this->info_box_contents[$line][] = array(
183						'td' => 'class="right nowraponall amount"',
184						'text' => price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency),
185					);
186
187					$this->info_box_contents[$line][] = array(
188						'td' => 'class="right"',
189						'text' => dol_print_date($date, 'day', 'tzuserrel'),
190					);
191
192					$this->info_box_contents[$line][] = array(
193						'td' => 'class="right" width="18"',
194						'text' => $facturestatic->LibStatut($objp->paye, $objp->status, 3, $objp->paye),
195					);
196
197					$line++;
198				}
199
200				if ($num == 0) {
201					$this->info_box_contents[$line][0] = array(
202						'td' => 'class="center"',
203						'text'=>$langs->trans("NoRecordedInvoices"),
204					);
205				}
206
207				$this->db->free($result);
208			} else {
209				$this->info_box_contents[0][0] = array(
210					'td' => '',
211					'maxlength'=>500,
212					'text' => ($this->db->error().' sql='.$sql),
213				);
214			}
215		} else {
216			$this->info_box_contents[0][0] = array(
217				'td' => 'class="nohover opacitymedium left"',
218				'text' => $langs->trans("ReadPermissionNotAllowed")
219			);
220		}
221	}
222
223	/**
224	 *  Method to show box
225	 *
226	 *  @param  array   $head       Array with properties of box title
227	 *  @param  array   $contents   Array with properties of box lines
228	 *  @param	int		$nooutput	No print, only return string
229	 *	@return	string
230	 */
231	public function showBox($head = null, $contents = null, $nooutput = 0)
232	{
233		return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
234	}
235}
236