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