dolibarr 20.0.0
box_factures_imp.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2007 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2015-2019 Frederic France <frederic.france@netlogic.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28require_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
29require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
30
31
36{
37 public $boxcode = "oldestunpaidcustomerbills";
38 public $boximg = "object_bill";
39 public $boxlabel = "BoxOldestUnpaidCustomerBills";
40 public $depends = array("facture");
41
48 public function __construct($db, $param)
49 {
50 global $user;
51
52 $this->db = $db;
53
54 $this->hidden = !($user->hasRight('facture', 'lire'));
55 }
56
63 public function loadBox($max = 5)
64 {
65 global $conf, $user, $langs;
66
67 $this->max = $max;
68 //$this->max = 1000;
69
70 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
71 include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
72
73 $facturestatic = new Facture($this->db);
74 $societestatic = new Societe($this->db);
75
76 $langs->load("bills");
77
78 $textHead = $langs->trans("BoxTitleOldestUnpaidCustomerBills");
79 $this->info_box_head = array(
80 'text' => $langs->trans("BoxTitleOldestUnpaidCustomerBills", $this->max).'<a class="paddingleft valignmiddle" href="'.DOL_URL_ROOT.'/compta/facture/list.php?search_status=1&sortfield=f.date_lim_reglement,f.ref&sortorder=ASC,ASC"><span class="badge">...</span></a>',
81 'limit' => dol_strlen($textHead));
82
83 if ($user->hasRight('facture', 'lire')) {
84 $sql1 = "SELECT s.rowid as socid, s.nom as name, s.name_alias, s.code_client, s.client";
85 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
86 $sql1 .= ", spe.accountancy_code_customer as code_compta_client";
87 } else {
88 $sql1 .= ", s.code_compta as code_compta_client";
89 }
90 $sql1 .= ", s.logo, s.email, s.entity";
91 $sql1 .= ", s.tva_intra, s.siren as idprof1, s.siret as idprof2, s.ape as idprof3, s.idprof4, s.idprof5, s.idprof6";
92 $sql1 .= ", f.ref, f.date_lim_reglement as datelimit";
93 $sql1 .= ", f.type";
94 $sql1 .= ", f.datef as date";
95 $sql1 .= ", f.total_ht";
96 $sql1 .= ", f.total_tva";
97 $sql1 .= ", f.total_ttc";
98 $sql1 .= ", f.paye, f.fk_statut as status, f.rowid as facid";
99 $sql1 .= ", SUM(pf.amount) as am";
100 $sql2 = " FROM ".MAIN_DB_PREFIX."societe as s";
101 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
102 $sql2 .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
103 }
104 if (!$user->hasRight('societe', 'client', 'voir')) {
105 $sql2 .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
106 }
107 $sql2 .= ", ".MAIN_DB_PREFIX."facture as f";
108 $sql2 .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
109 $sql2 .= " WHERE f.fk_soc = s.rowid";
110 $sql2 .= " AND f.entity IN (".getEntity('invoice').")";
111 $sql2 .= " AND f.paye = 0";
112 $sql2 .= " AND fk_statut = 1";
113 if (!$user->hasRight('societe', 'client', 'voir')) {
114 $sql2 .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
115 }
116 if ($user->socid) {
117 $sql2 .= " AND s.rowid = ".((int) $user->socid);
118 }
119 $sql3 = " GROUP BY s.rowid, s.nom, s.name_alias, s.code_client, s.client, s.logo, s.email, s.entity, s.tva_intra, s.siren, s.siret, s.ape, s.idprof4, s.idprof5, s.idprof6,";
120 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
121 $sql3 .= " spe.accountancy_code_customer as code_compta,";
122 } else {
123 $sql3 .= " s.code_compta,";
124 }
125 $sql3 .= " f.rowid, f.ref, f.date_lim_reglement,";
126 $sql3 .= " f.type, f.datef, f.total_ht, f.total_tva, f.total_ttc, f.paye, f.fk_statut";
127 $sql3 .= " ORDER BY date_lim_reglement ASC, f.ref ASC";
128 $sql3 .= $this->db->plimit($this->max + 1, 0);
129
130 $sql = $sql1.$sql2.$sql3;
131
132 $result = $this->db->query($sql);
133 if ($result) {
134 $num = $this->db->num_rows($result);
135
136 $line = 0;
137 $l_due_date = $langs->trans('Late').' ('.strtolower($langs->trans('DateDue')).': %s)';
138
139 while ($line < min($num, $this->max)) {
140 $objp = $this->db->fetch_object($result);
141
142 $date = $this->db->jdate($objp->date);
143 $datelimit = $this->db->jdate($objp->datelimit);
144
145 $facturestatic->id = $objp->facid;
146 $facturestatic->ref = $objp->ref;
147 $facturestatic->type = $objp->type;
148 $facturestatic->total_ht = $objp->total_ht;
149 $facturestatic->total_tva = $objp->total_tva;
150 $facturestatic->total_ttc = $objp->total_ttc;
151 $facturestatic->date = $date;
152 $facturestatic->date_lim_reglement = $datelimit;
153 $facturestatic->statut = $objp->status;
154 $facturestatic->status = $objp->status;
155
156 $facturestatic->paye = $objp->paye;
157 $facturestatic->paid = $objp->paye;
158 $facturestatic->alreadypaid = $objp->am;
159 $facturestatic->totalpaid = $objp->am;
160
161 $societestatic->id = $objp->socid;
162 $societestatic->name = $objp->name;
163 //$societestatic->name_alias = $objp->name_alias;
164 $societestatic->code_client = $objp->code_client;
165 $societestatic->code_compta = $objp->code_compta_client;
166 $societestatic->code_compta_client = $objp->code_compta_client;
167 $societestatic->client = $objp->client;
168 $societestatic->logo = $objp->logo;
169 $societestatic->email = $objp->email;
170 $societestatic->entity = $objp->entity;
171 $societestatic->tva_intra = $objp->tva_intra;
172
173 $societestatic->idprof1 = !empty($objp->idprof1) ? $objp->idprof1 : '';
174 $societestatic->idprof2 = !empty($objp->idprof2) ? $objp->idprof2 : '';
175 $societestatic->idprof3 = !empty($objp->idprof3) ? $objp->idprof3 : '';
176 $societestatic->idprof4 = !empty($objp->idprof4) ? $objp->idprof4 : '';
177 $societestatic->idprof5 = !empty($objp->idprof5) ? $objp->idprof5 : '';
178 $societestatic->idprof6 = !empty($objp->idprof6) ? $objp->idprof6 : '';
179
180 $late = '';
181 if ($facturestatic->hasDelay()) {
182 // @phan-suppress-next-line PhanPluginPrintfVariableFormatString
183 $late = img_warning(sprintf($l_due_date, dol_print_date($datelimit, 'day', 'tzuserrel')));
184 }
185
186 $this->info_box_contents[$line][] = array(
187 'td' => 'class="nowraponall"',
188 'text' => $facturestatic->getNomUrl(1),
189 'text2' => $late,
190 'asis' => 1,
191 );
192
193 $this->info_box_contents[$line][] = array(
194 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
195 'text' => $societestatic->getNomUrl(1, '', 44),
196 'asis' => 1,
197 );
198
199 $this->info_box_contents[$line][] = array(
200 'td' => 'class="nowraponall right amount"',
201 'text' => price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency),
202 );
203
204 $this->info_box_contents[$line][] = array(
205 'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateDue").': '.dol_print_date($datelimit, 'day', 'tzuserrel')).'"',
206 'text' => dol_print_date($datelimit, 'day', 'tzuserrel'),
207 );
208
209 $this->info_box_contents[$line][] = array(
210 'td' => 'class="right" width="18"',
211 'text' => $facturestatic->LibStatut($objp->paye, $objp->status, 3, $objp->am, $objp->type),
212 );
213
214 $line++;
215 }
216 if ($this->max < $num) {
217 $this->info_box_contents[$line][] = array('td' => 'colspan="6"', 'text' => '...');
218 $line++;
219 }
220
221 if ($num == 0) {
222 $this->info_box_contents[$line][0] = array(
223 'td' => 'class="center" colspan="3"',
224 'text' => '<span class="opacitymedium">'.$langs->trans("NoUnpaidCustomerBills").'</span>'
225 );
226 } else {
227 $sql = "SELECT SUM(f.total_ht) as total_ht ".$sql2;
228
229 $result = $this->db->query($sql);
230 $objp = $this->db->fetch_object($result);
231 $totalamount = $objp->total_ht;
232
233 // Add the sum à the bottom of the boxes
234 $this->info_box_contents[$line][] = array(
235 'tr' => 'class="liste_total_wrap"',
236 'td' => 'class="liste_total"',
237 'text' => $langs->trans("Total"),
238 );
239 $this->info_box_contents[$line][] = array(
240 'td' => 'class="liste_total"',
241 'text' => "&nbsp;",
242 );
243 $this->info_box_contents[$line][] = array(
244 'td' => 'class="right liste_total" ',
245 'text' => price($totalamount, 0, $langs, 0, -1, -1, $conf->currency),
246 );
247 $this->info_box_contents[$line][] = array(
248 'td' => 'class="liste_total"',
249 'text' => "&nbsp;",
250 );
251 $this->info_box_contents[$line][] = array(
252 'td' => 'class="liste_total"',
253 'text' => "&nbsp;",
254 );
255
256 $this->db->free($result);
257 }
258 } else {
259 $this->info_box_contents[0][0] = array(
260 'td' => '',
261 'maxlength' => 500,
262 'text' => ($this->db->error().' sql='.$sql),
263 );
264 }
265 } else {
266 $this->info_box_contents[0][0] = array(
267 'td' => 'class="nohover left"',
268 'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
269 );
270 }
271 }
272
281 public function showBox($head = null, $contents = null, $nooutput = 0)
282 {
283 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
284 }
285}
Class to manage invoices.
Class ModeleBoxes.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage the box to show not paid sales invoices.
loadBox($max=5)
Load data into info_box_contents array to show array later.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
__construct($db, $param)
Constructor.
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...