dolibarr  18.0.0-alpha
box_activity.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2012 Charles-Fran├žois BENKE <charles.fr@benke.fr>
3  * Copyright (C) 2005-2015 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2014-2021 Frederic France <frederic.france@netlogic.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
26 include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
27 
32 {
33  public $boxcode = "activity";
34  public $boximg = "object_bill";
35  public $boxlabel = 'BoxGlobalActivity';
36  public $depends = array("facture");
37 
41  public $db;
42 
43  public $param;
44  public $enabled = 1;
45 
46  public $info_box_head = array();
47  public $info_box_contents = array();
48 
49 
56  public function __construct($db, $param)
57  {
58  global $conf, $user;
59 
60  $this->db = $db;
61 
62  // FIXME: Pb into some status
63  $this->enabled = ($conf->global->MAIN_FEATURES_LEVEL); // Not enabled by default due to bugs (see previous comments)
64 
65  $this->hidden = !((isModEnabled('facture') && $user->hasRight('facture', 'read'))
66  || (isModEnabled('commande') && $user->hasRight('commande', 'read'))
67  || (isModEnabled('propal') && $user->hasRight('propal', 'read'))
68  );
69  }
70 
77  public function loadBox($max = 5)
78  {
79  global $conf, $user, $langs;
80 
81  include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
82  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
83 
84  $totalnb = 0;
85  $line = 0;
86  $cachetime = 3600;
87  $fileid = '-e'.$conf->entity.'-u'.$user->id.'-s'.$user->socid.'-r'.($user->hasRight("societe", "client", "voir") ? '1' : '0').'.cache';
88  $now = dol_now();
89  $nbofperiod = 3;
90 
91  if (!empty($conf->global->MAIN_BOX_ACTIVITY_DURATION)) {
92  $nbofperiod = $conf->global->MAIN_BOX_ACTIVITY_DURATION;
93  }
94  $textHead = $langs->trans("Activity").' - '.$langs->trans("LastXMonthRolling", $nbofperiod);
95  $this->info_box_head = array(
96  'text' => $textHead,
97  'limit'=> dol_strlen($textHead),
98  );
99 
100  // compute the year limit to show
101  $tmpdate = dol_time_plus_duree(dol_now(), -1 * $nbofperiod, "m");
102 
103 
104  // list the summary of the propals
105  if (isModEnabled("propal") && $user->hasRight("propal", "lire")) {
106  include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
107  $propalstatic = new Propal($this->db);
108 
109  $cachedir = DOL_DATA_ROOT.'/propale/temp';
110  $filename = '/boxactivity-propal'.$fileid;
111  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
112  $data = array();
113  if ($refresh) {
114  $sql = "SELECT p.fk_statut, SUM(p.total_ttc) as Mnttot, COUNT(*) as nb";
115  $sql .= " FROM (".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."propal as p";
116  if (empty($user->rights->societe->client->voir) && !$user->socid) {
117  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
118  }
119  $sql .= ")";
120  $sql .= " WHERE p.entity IN (".getEntity('propal').")";
121  $sql .= " AND p.fk_soc = s.rowid";
122  if (empty($user->rights->societe->client->voir) && !$user->socid) {
123  $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
124  }
125  if ($user->socid) {
126  $sql .= " AND s.rowid = ".((int) $user->socid);
127  }
128  $sql .= " AND p.datep >= '".$this->db->idate($tmpdate)."'";
129  $sql .= " AND p.date_cloture IS NULL"; // just unclosed
130  $sql .= " GROUP BY p.fk_statut";
131  $sql .= " ORDER BY p.fk_statut DESC";
132 
133  $result = $this->db->query($sql);
134  if ($result) {
135  $num = $this->db->num_rows($result);
136 
137  $j = 0;
138  while ($j < $num) {
139  $data[$j] = $this->db->fetch_object($result);
140  $j++;
141  }
142  if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
143  dol_filecache($cachedir, $filename, $data);
144  }
145  $this->db->free($result);
146  } else {
147  dol_print_error($this->db);
148  }
149  } else {
150  $data = dol_readcachefile($cachedir, $filename);
151  }
152 
153  if (!empty($data)) {
154  $j = 0;
155  while ($j < count($data)) {
156  $this->info_box_contents[$line][0] = array(
157  'td' => 'class="left" width="16"',
158  'url' => DOL_URL_ROOT."/comm/propal/list.php?mainmenu=commercial&leftmenu=propals&search_status=".((int) $data[$j]->fk_statut),
159  'tooltip' => $langs->trans("Proposals")."&nbsp;".$propalstatic->LibStatut($data[$j]->fk_statut, 0),
160  'logo' => 'object_propal'
161  );
162 
163  $this->info_box_contents[$line][1] = array(
164  'td' => '',
165  'text' => $langs->trans("Proposals")."&nbsp;".$propalstatic->LibStatut($data[$j]->fk_statut, 0),
166  );
167 
168  $this->info_box_contents[$line][2] = array(
169  'td' => 'class="right"',
170  'text' => $data[$j]->nb,
171  'tooltip' => $langs->trans("Proposals")."&nbsp;".$propalstatic->LibStatut($data[$j]->fk_statut, 0),
172  'url' => DOL_URL_ROOT."/comm/propal/list.php?mainmenu=commercial&leftmenu=propals&search_status=".((int) $data[$j]->fk_statut),
173  );
174  $totalnb += $data[$j]->nb;
175 
176  $this->info_box_contents[$line][3] = array(
177  'td' => 'class="nowraponall right amount"',
178  'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
179  );
180  $this->info_box_contents[$line][4] = array(
181  'td' => 'class="right" width="18"',
182  'text' => $propalstatic->LibStatut($data[$j]->fk_statut, 3),
183  );
184 
185  $line++;
186  $j++;
187  }
188  if (count($data) == 0) {
189  $this->info_box_contents[$line][0] = array(
190  'td' => 'class="center"',
191  'text'=>$langs->trans("NoRecordedProposals"),
192  );
193  $line++;
194  }
195  }
196  }
197 
198  // list the summary of the orders
199  if (isModEnabled('commande') && $user->hasRight("commande", "lire")) {
200  include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
201  $commandestatic = new Commande($this->db);
202 
203  $langs->load("orders");
204 
205  $cachedir = DOL_DATA_ROOT.'/commande/temp';
206  $filename = '/boxactivity-order'.$fileid;
207  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
208  $data = array();
209 
210  if ($refresh) {
211  $sql = "SELECT c.fk_statut, sum(c.total_ttc) as Mnttot, count(*) as nb";
212  $sql .= " FROM (".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."commande as c";
213  if (empty($user->rights->societe->client->voir) && !$user->socid) {
214  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
215  }
216  $sql .= ")";
217  $sql .= " WHERE c.entity IN (".getEntity('commande').")";
218  $sql .= " AND c.fk_soc = s.rowid";
219  if (empty($user->rights->societe->client->voir) && !$user->socid) {
220  $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
221  }
222  if ($user->socid) {
223  $sql .= " AND s.rowid = ".((int) $user->socid);
224  }
225  $sql .= " AND c.date_commande >= '".$this->db->idate($tmpdate)."'";
226  $sql .= " GROUP BY c.fk_statut";
227  $sql .= " ORDER BY c.fk_statut DESC";
228 
229  $result = $this->db->query($sql);
230  if ($result) {
231  $num = $this->db->num_rows($result);
232  $j = 0;
233  while ($j < $num) {
234  $data[$j] = $this->db->fetch_object($result);
235  $j++;
236  }
237  if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
238  dol_filecache($cachedir, $filename, $data);
239  }
240  $this->db->free($result);
241  } else {
242  dol_print_error($this->db);
243  }
244  } else {
245  $data = dol_readcachefile($cachedir, $filename);
246  }
247 
248  if (!empty($data)) {
249  $j = 0;
250  while ($j < count($data)) {
251  $this->info_box_contents[$line][0] = array(
252  'td' => 'class="left" width="16"',
253  'url' => DOL_URL_ROOT."/commande/list.php?mainmenu=commercial&amp;leftmenu=orders&amp;search_status=".$data[$j]->fk_statut,
254  'tooltip' => $langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
255  'logo' => 'object_order',
256  );
257 
258  $this->info_box_contents[$line][1] = array(
259  'td' => '',
260  'text' =>$langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
261  );
262 
263  $this->info_box_contents[$line][2] = array(
264  'td' => 'class="right"',
265  'text' => $data[$j]->nb,
266  'tooltip' => $langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
267  'url' => DOL_URL_ROOT."/commande/list.php?mainmenu=commercial&amp;leftmenu=orders&amp;search_status=".$data[$j]->fk_statut,
268  );
269  $totalnb += $data[$j]->nb;
270 
271  $this->info_box_contents[$line][3] = array(
272  'td' => 'class="nowraponall right amount"',
273  'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
274  );
275  $this->info_box_contents[$line][4] = array(
276  'td' => 'class="right" width="18"',
277  'text' => $commandestatic->LibStatut($data[$j]->fk_statut, 0, 3),
278  );
279 
280  $line++;
281  $j++;
282  }
283  if (count($data) == 0) {
284  $this->info_box_contents[$line][0] = array(
285  'td' => 'class="center"',
286  'text'=>$langs->trans("NoRecordedOrders"),
287  );
288  $line++;
289  }
290  }
291  }
292 
293 
294  // list the summary of the bills
295  if (isModEnabled('facture') && $user->hasRight("facture", "lire")) {
296  include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
297  $facturestatic = new Facture($this->db);
298 
299  // part 1
300  $cachedir = DOL_DATA_ROOT.'/facture/temp';
301  $filename = '/boxactivity-invoice'.$fileid;
302 
303  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
304  $data = array();
305  if ($refresh) {
306  $sql = "SELECT f.fk_statut, SUM(f.total_ttc) as Mnttot, COUNT(*) as nb";
307  $sql .= " FROM (".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
308  if (empty($user->rights->societe->client->voir) && !$user->socid) {
309  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
310  }
311  $sql .= ")";
312  $sql .= " WHERE f.entity IN (".getEntity('invoice').')';
313  if (empty($user->rights->societe->client->voir) && !$user->socid) {
314  $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
315  }
316  if ($user->socid) {
317  $sql .= " AND s.rowid = ".((int) $user->socid);
318  }
319  $sql .= " AND f.fk_soc = s.rowid";
320  $sql .= " AND f.datef >= '".$this->db->idate($tmpdate)."' AND f.paye=1";
321  $sql .= " GROUP BY f.fk_statut";
322  $sql .= " ORDER BY f.fk_statut DESC";
323 
324  $result = $this->db->query($sql);
325  if ($result) {
326  $num = $this->db->num_rows($result);
327  $j = 0;
328  while ($j < $num) {
329  $data[$j] = $this->db->fetch_object($result);
330  $j++;
331  }
332  if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
333  dol_filecache($cachedir, $filename, $data);
334  }
335  $this->db->free($result);
336  } else {
337  dol_print_error($this->db);
338  }
339  } else {
340  $data = dol_readcachefile($cachedir, $filename);
341  }
342 
343  if (!empty($data)) {
344  $j = 0;
345  while ($j < count($data)) {
346  $billurl = "search_status=2&paye=1";
347  $this->info_box_contents[$line][0] = array(
348  'td' => 'class="left" width="16"',
349  'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
350  'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&mainmenu=accountancy&leftmenu=customers_bills",
351  'logo' => 'bill',
352  );
353 
354  $this->info_box_contents[$line][1] = array(
355  'td' => '',
356  'text' => $langs->trans("Bills")."&nbsp;".$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
357  );
358 
359  $this->info_box_contents[$line][2] = array(
360  'td' => 'class="right"',
361  'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
362  'text' => $data[$j]->nb,
363  'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&mainmenu=accountancy&leftmenu=customers_bills",
364  );
365 
366  $this->info_box_contents[$line][3] = array(
367  'td' => 'class="nowraponall right amount"',
368  'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency)
369  );
370 
371  // We add only for the current year
372  $totalnb += $data[$j]->nb;
373 
374  $this->info_box_contents[$line][4] = array(
375  'td' => 'class="right" width="18"',
376  'text' => $facturestatic->LibStatut(1, $data[$j]->fk_statut, 3),
377  );
378  $line++;
379  $j++;
380  }
381  if (count($data) == 0) {
382  $this->info_box_contents[$line][0] = array(
383  'td' => 'class="center"',
384  'text'=>$langs->trans("NoRecordedInvoices"),
385  );
386  $line++;
387  }
388  }
389 
390  // part 2
391  $cachedir = DOL_DATA_ROOT.'/facture/temp';
392  $filename = '/boxactivity-invoice2'.$fileid;
393 
394  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
395 
396  $data = array();
397  if ($refresh) {
398  $sql = "SELECT f.fk_statut, SUM(f.total_ttc) as Mnttot, COUNT(*) as nb";
399  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
400  $sql .= " WHERE f.entity IN (".getEntity('invoice').')';
401  $sql .= " AND f.fk_soc = s.rowid";
402  $sql .= " AND f.datef >= '".$this->db->idate($tmpdate)."' AND f.paye=0";
403  $sql .= " GROUP BY f.fk_statut";
404  $sql .= " ORDER BY f.fk_statut DESC";
405 
406  $result = $this->db->query($sql);
407  if ($result) {
408  $num = $this->db->num_rows($result);
409  $j = 0;
410  while ($j < $num) {
411  $data[$j] = $this->db->fetch_object($result);
412  $j++;
413  }
414  if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
415  dol_filecache($cachedir, $filename, $data);
416  }
417  $this->db->free($result);
418  } else {
419  dol_print_error($this->db);
420  }
421  } else {
422  $data = dol_readcachefile($cachedir, $filename);
423  }
424 
425  if (!empty($data)) {
426  $alreadypaid = -1;
427 
428  $j = 0;
429  while ($j < count($data)) {
430  $billurl = "search_status=".$data[$j]->fk_statut."&paye=0";
431  $this->info_box_contents[$line][0] = array(
432  'td' => 'class="left" width="16"',
433  'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
434  'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&mainmenu=accountancy&leftmenu=customers_bills",
435  'logo' => 'bill',
436  );
437 
438  $this->info_box_contents[$line][1] = array(
439  'td' => '',
440  'text' => $langs->trans("Bills")."&nbsp;".$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
441  );
442 
443  $this->info_box_contents[$line][2] = array(
444  'td' => 'class="right"',
445  'text' => $data[$j]->nb,
446  'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
447  'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
448  );
449  $totalnb += $data[$j]->nb;
450  $this->info_box_contents[$line][3] = array(
451  'td' => 'class="nowraponall right amount"',
452  'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
453  );
454  $this->info_box_contents[$line][4] = array(
455  'td' => 'class="right" width="18"',
456  'text' => $facturestatic->LibStatut(0, $data[$j]->fk_statut, 3, $alreadypaid),
457  );
458  $line++;
459  $j++;
460  }
461  if (count($data) == 0) {
462  $this->info_box_contents[$line][0] = array(
463  'td' => 'class="center"',
464  'text'=>$langs->trans("NoRecordedUnpaidInvoices"),
465  );
466  $line++;
467  }
468  }
469  }
470 
471  // Add the sum in the bottom of the boxes
472  $this->info_box_contents[$line][0] = array('tr' => 'class="liste_total_wrap"');
473  $this->info_box_contents[$line][1] = array('td' => 'class="liste_total left" ', 'text' => $langs->trans("Total")."&nbsp;".$textHead);
474  $this->info_box_contents[$line][2] = array('td' => 'class="liste_total right" ', 'text' => $totalnb);
475  $this->info_box_contents[$line][3] = array('td' => 'class="liste_total right" ', 'text' => '');
476  $this->info_box_contents[$line][4] = array('td' => 'class="liste_total right" ', 'text' => "");
477  }
478 
479 
488  public function showBox($head = null, $contents = null, $nooutput = 0)
489  {
490  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
491  }
492 }
box_activity
Class to manage the box of customer activity (invoice, order, proposal)
Definition: box_activity.php:31
db
$conf db
API class for accounts.
Definition: inc.php:41
$sql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:745
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4994
Facture
Class to manage invoices.
Definition: facture.class.php:60
box_activity\__construct
__construct($db, $param)
Constructor.
Definition: box_activity.php:56
dol_readcachefile
dol_readcachefile($directory, $filename)
Read object from cachefile.
Definition: files.lib.php:3264
box_activity\showBox
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
Definition: box_activity.php:488
dol_filecache
dol_filecache($directory, $filename, $object)
Store object in file.
Definition: files.lib.php:3231
Commande
Class to manage customers orders.
Definition: commande.class.php:46
box_activity\loadBox
loadBox($max=5)
Charge les donnees en memoire pour affichage ulterieur.
Definition: box_activity.php:77
dol_cache_refresh
dol_cache_refresh($directory, $filename, $cachetime)
Test if Refresh needed.
Definition: files.lib.php:3249
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3888
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:147
dol_time_plus_duree
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition: date.lib.php:122
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2947
price
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.
Definition: functions.lib.php:5708
Propal
Class to manage proposals.
Definition: propal.class.php:52
ModeleBoxes
Class ModeleBoxes.
Definition: modules_boxes.php:34