dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
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
29require_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
30require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
31
32
37{
38 public $boxcode = "oldestunpaidcustomerbills";
39 public $boximg = "object_bill";
40 public $boxlabel = "BoxOldestUnpaidCustomerBills";
41 public $depends = array("facture");
42
49 public function __construct($db, $param)
50 {
51 global $user;
52
53 $this->db = $db;
54
55 $this->hidden = !($user->hasRight('facture', 'lire'));
56 $this->urltoaddentry = DOL_URL_ROOT.'/compta/facture/card.php?action=create';
57 $this->msgNoRecords = 'NoUnpaidCustomerBills';
58 }
59
66 public function loadBox($max = 5)
67 {
68 global $conf, $user, $langs;
69
70 $this->max = $max;
71 //$this->max = 1000;
72
73 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
74 include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
75
76 $facturestatic = new Facture($this->db);
77 $societestatic = new Societe($this->db);
78
79 $langs->load("bills");
80
81 $textHead = $langs->trans("BoxTitleOldestUnpaidCustomerBills");
82 $this->info_box_head = array(
83 '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>',
84 'limit' => dol_strlen($textHead));
85
86 if ($user->hasRight('facture', 'lire')) {
87 $sql1 = "SELECT s.rowid as socid, s.nom as name, s.name_alias, s.code_client, s.client";
88 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
89 $sql1 .= ", spe.accountancy_code_customer as code_compta_client";
90 } else {
91 $sql1 .= ", s.code_compta as code_compta_client";
92 }
93 $sql1 .= ", s.logo, s.email, s.entity";
94 $sql1 .= ", s.tva_intra, s.siren as idprof1, s.siret as idprof2, s.ape as idprof3, s.idprof4, s.idprof5, s.idprof6";
95 $sql1 .= ", f.ref, f.date_lim_reglement as datelimit";
96 $sql1 .= ", f.type";
97 $sql1 .= ", f.datef as date";
98 $sql1 .= ", f.total_ht";
99 $sql1 .= ", f.total_tva";
100 $sql1 .= ", f.total_ttc";
101 $sql1 .= ", f.paye, f.fk_statut as status, f.rowid as facid";
102 $sql1 .= ", SUM(pf.amount) as am";
103 $sql2 = " FROM ".MAIN_DB_PREFIX."societe as s";
104 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
105 $sql2 .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
106 }
107 if (!$user->hasRight('societe', 'client', 'voir')) {
108 $sql2 .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
109 }
110 $sql2 .= ", ".MAIN_DB_PREFIX."facture as f";
111 $sql2 .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
112 $sql2 .= " WHERE f.fk_soc = s.rowid";
113 $sql2 .= " AND f.entity IN (".getEntity('invoice').")";
114 $sql2 .= " AND f.paye = 0";
115 $sql2 .= " AND fk_statut = 1";
116 if (!$user->hasRight('societe', 'client', 'voir')) {
117 $sql2 .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
118 }
119 if ($user->socid) {
120 $sql2 .= " AND s.rowid = ".((int) $user->socid);
121 }
122 $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,";
123 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
124 $sql3 .= " spe.accountancy_code_customer,";
125 } else {
126 $sql3 .= " s.code_compta,";
127 }
128 $sql3 .= " f.rowid, f.ref, f.date_lim_reglement,";
129 $sql3 .= " f.type, f.datef, f.total_ht, f.total_tva, f.total_ttc, f.paye, f.fk_statut";
130 $sql3 .= " ORDER BY date_lim_reglement ASC, f.ref ASC";
131 $sql3 .= $this->db->plimit($this->max + 1, 0);
132
133 $sql = $sql1.$sql2.$sql3;
134
135 $result = $this->db->query($sql);
136 if ($result) {
137 $num = $this->db->num_rows($result);
138
139 $line = 0;
140 $l_due_date = $langs->trans('Late').' ('.strtolower($langs->trans('DateDue')).': %s)';
141
142 while ($line < min($num, $this->max)) {
143 $objp = $this->db->fetch_object($result);
144
145 $date = $this->db->jdate($objp->date);
146 $datelimit = $this->db->jdate($objp->datelimit);
147
148 $facturestatic->id = $objp->facid;
149 $facturestatic->ref = $objp->ref;
150 $facturestatic->type = $objp->type;
151 $facturestatic->total_ht = $objp->total_ht;
152 $facturestatic->total_tva = $objp->total_tva;
153 $facturestatic->total_ttc = $objp->total_ttc;
154 $facturestatic->date = $date;
155 $facturestatic->date_lim_reglement = $datelimit;
156 $facturestatic->statut = $objp->status;
157 $facturestatic->status = $objp->status;
158
159 $facturestatic->paye = $objp->paye;
160 $facturestatic->paid = $objp->paye;
161 $facturestatic->alreadypaid = $objp->am;
162 $facturestatic->totalpaid = $objp->am;
163
164 $societestatic->id = $objp->socid;
165 $societestatic->name = $objp->name;
166 //$societestatic->name_alias = $objp->name_alias;
167 $societestatic->code_client = $objp->code_client;
168 $societestatic->code_compta = $objp->code_compta_client;
169 $societestatic->code_compta_client = $objp->code_compta_client;
170 $societestatic->client = $objp->client;
171 $societestatic->logo = $objp->logo;
172 $societestatic->email = $objp->email;
173 $societestatic->entity = $objp->entity;
174 $societestatic->tva_intra = $objp->tva_intra;
175
176 $societestatic->idprof1 = !empty($objp->idprof1) ? $objp->idprof1 : '';
177 $societestatic->idprof2 = !empty($objp->idprof2) ? $objp->idprof2 : '';
178 $societestatic->idprof3 = !empty($objp->idprof3) ? $objp->idprof3 : '';
179 $societestatic->idprof4 = !empty($objp->idprof4) ? $objp->idprof4 : '';
180 $societestatic->idprof5 = !empty($objp->idprof5) ? $objp->idprof5 : '';
181 $societestatic->idprof6 = !empty($objp->idprof6) ? $objp->idprof6 : '';
182
183 $late = '';
184 if ($facturestatic->hasDelay()) {
185 // @phan-suppress-next-line PhanPluginPrintfVariableFormatString
186 $late = img_warning(sprintf($l_due_date, dol_print_date($datelimit, 'day', 'tzuserrel')));
187 }
188
189 $this->info_box_contents[$line][] = array(
190 'td' => 'class="nowraponall"',
191 'text' => $facturestatic->getNomUrl(1),
192 'text2' => $late,
193 'asis' => 1,
194 );
195
196 $this->info_box_contents[$line][] = array(
197 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
198 'text' => $societestatic->getNomUrl(1, '', 44),
199 'asis' => 1,
200 );
201
202 $this->info_box_contents[$line][] = array(
203 'td' => 'class="nowraponall right amount"',
204 'text' => price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency),
205 );
206
207 $this->info_box_contents[$line][] = array(
208 'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateDue").': '.dol_print_date($datelimit, 'day', 'tzuserrel')).'"',
209 'text' => dol_print_date($datelimit, 'day', 'tzuserrel'),
210 );
211
212 $this->info_box_contents[$line][] = array(
213 'td' => 'class="right" width="18"',
214 'text' => $facturestatic->LibStatut($objp->paye, $objp->status, 3, $objp->am, $objp->type),
215 );
216
217 $line++;
218 }
219 if ($this->max < $num) {
220 $this->info_box_contents[$line][] = array('td' => 'colspan="6"', 'text' => '...');
221 $line++;
222 }
223
224 if ($num == 0) {
225 // $this->info_box_contents[$line][0] = array(
226 // 'td' => 'class="center" colspan="3"',
227 // 'text' => '<span class="opacitymedium">'.$langs->trans("NoUnpaidCustomerBills").'</span>'
228 // );
229 } else {
230 $sql = "SELECT SUM(f.total_ht) as total_ht ".$sql2;
231
232 $result = $this->db->query($sql);
233 $objp = $this->db->fetch_object($result);
234 $totalamount = $objp->total_ht;
235
236 // Add the sum à the bottom of the boxes
237 $this->info_box_contents[$line][] = array(
238 'tr' => 'class="liste_total_wrap"',
239 'td' => 'class="liste_total"',
240 'text' => $langs->trans("Total"),
241 );
242 $this->info_box_contents[$line][] = array(
243 'td' => 'class="liste_total"',
244 'text' => "&nbsp;",
245 );
246 $this->info_box_contents[$line][] = array(
247 'td' => 'class="right liste_total" ',
248 'text' => price($totalamount, 0, $langs, 0, -1, -1, $conf->currency),
249 );
250 $this->info_box_contents[$line][] = array(
251 'td' => 'class="liste_total"',
252 'text' => "&nbsp;",
253 );
254 $this->info_box_contents[$line][] = array(
255 'td' => 'class="liste_total"',
256 'text' => "&nbsp;",
257 );
258
259 $this->db->free($result);
260 }
261 } else {
262 $this->info_box_contents[0][0] = array(
263 'td' => '',
264 'maxlength' => 500,
265 'text' => ($this->db->error().' sql='.$sql),
266 );
267 }
268 } else {
269 $this->info_box_contents[0][0] = array(
270 'td' => 'class="nohover left"',
271 'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
272 );
273 }
274 }
275
276
277
286 public function showBox($head = null, $contents = null, $nooutput = 0)
287 {
288 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
289 }
290}
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 a 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...