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  *
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 
28 require_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
29 require_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.'/fourn/facture/list.php?sortfield=f.tms&sortorder=DESC"><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";
87  } else {
88  $sql1 .= ", s.code_compta";
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 datelimite";
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 datelimite 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  $datelimite = $this->db->jdate($objp->datelimite);
143 
144  $facturestatic->id = $objp->facid;
145  $facturestatic->ref = $objp->ref;
146  $facturestatic->type = $objp->type;
147  $facturestatic->total_ht = $objp->total_ht;
148  $facturestatic->total_tva = $objp->total_tva;
149  $facturestatic->total_ttc = $objp->total_ttc;
150  $facturestatic->statut = $objp->status;
151  $facturestatic->status = $objp->status;
152  $facturestatic->date = $this->db->jdate($objp->date);
153  $facturestatic->date_lim_reglement = $this->db->jdate($objp->datelimite);
154 
155  $facturestatic->paye = $objp->paye;
156  $facturestatic->alreadypaid = $objp->am;
157 
158  $societestatic->id = $objp->socid;
159  $societestatic->name = $objp->name;
160  //$societestatic->name_alias = $objp->name_alias;
161  $societestatic->code_client = $objp->code_client;
162  $societestatic->code_compta = $objp->code_compta;
163  $societestatic->client = $objp->client;
164  $societestatic->logo = $objp->logo;
165  $societestatic->email = $objp->email;
166  $societestatic->entity = $objp->entity;
167  $societestatic->tva_intra = $objp->tva_intra;
168  $societestatic->idprof1 = $objp->idprof1;
169  $societestatic->idprof2 = $objp->idprof2;
170  $societestatic->idprof3 = $objp->idprof3;
171  $societestatic->idprof4 = $objp->idprof4;
172  $societestatic->idprof5 = $objp->idprof5;
173  $societestatic->idprof6 = $objp->idprof6;
174 
175  $late = '';
176  if ($facturestatic->hasDelay()) {
177  // @phan-suppress-next-line PhanPluginPrintfVariableFormatString
178  $late = img_warning(sprintf($l_due_date, dol_print_date($datelimite, 'day', 'tzuserrel')));
179  }
180 
181  $this->info_box_contents[$line][] = array(
182  'td' => 'class="nowraponall"',
183  'text' => $facturestatic->getNomUrl(1),
184  'text2' => $late,
185  'asis' => 1,
186  );
187 
188  $this->info_box_contents[$line][] = array(
189  'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
190  'text' => $societestatic->getNomUrl(1, '', 44),
191  'asis' => 1,
192  );
193 
194  $this->info_box_contents[$line][] = array(
195  'td' => 'class="nowraponall right amount"',
196  'text' => price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency),
197  );
198 
199  $this->info_box_contents[$line][] = array(
200  'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateDue").': '.dol_print_date($datelimite, 'day', 'tzuserrel')).'"',
201  'text' => dol_print_date($datelimite, 'day', 'tzuserrel'),
202  );
203 
204  $this->info_box_contents[$line][] = array(
205  'td' => 'class="right" width="18"',
206  'text' => $facturestatic->LibStatut($objp->paye, $objp->status, 3, $objp->am),
207  );
208 
209  $line++;
210  }
211  if ($this->max < $num) {
212  $this->info_box_contents[$line][] = array('td' => 'colspan="6"', 'text' => '...');
213  $line++;
214  }
215 
216  if ($num == 0) {
217  $this->info_box_contents[$line][0] = array(
218  'td' => 'class="center"',
219  'text' => '<span class="opacitymedium">'.$langs->trans("NoUnpaidCustomerBills").'</span>'
220  );
221  }
222 
223  $sql = "SELECT SUM(f.total_ht) as total_ht ".$sql2;
224 
225  $result = $this->db->query($sql);
226  $objp = $this->db->fetch_object($result);
227  $totalamount = $objp->total_ht;
228 
229  // Add the sum à the bottom of the boxes
230  $this->info_box_contents[$line][] = array(
231  'tr' => 'class="liste_total_wrap"',
232  'td' => 'class="liste_total"',
233  'text' => $langs->trans("Total"),
234  );
235  $this->info_box_contents[$line][] = array(
236  'td' => 'class="liste_total"',
237  'text' => "&nbsp;",
238  );
239  $this->info_box_contents[$line][] = array(
240  'td' => 'class="right liste_total" ',
241  'text' => price($totalamount, 0, $langs, 0, -1, -1, $conf->currency),
242  );
243  $this->info_box_contents[$line][] = array(
244  'td' => 'class="liste_total"',
245  'text' => "&nbsp;",
246  );
247  $this->info_box_contents[$line][] = array(
248  'td' => 'class="liste_total"',
249  'text' => "&nbsp;",
250  );
251 
252  $this->db->free($result);
253  } else {
254  $this->info_box_contents[0][0] = array(
255  'td' => '',
256  'maxlength' => 500,
257  'text' => ($this->db->error().' sql='.$sql),
258  );
259  }
260  } else {
261  $this->info_box_contents[0][0] = array(
262  'td' => 'class="nohover left"',
263  'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
264  );
265  }
266  }
267 
276  public function showBox($head = null, $contents = null, $nooutput = 0)
277  {
278  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
279  }
280 }
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.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:751
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...