dolibarr  17.0.4
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&amp;leftmenu=propals&amp;search_status=".$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&amp;leftmenu=propals&amp;search_status=".$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  }
189  }
190 
191  // list the summary of the orders
192  if (isModEnabled('commande') && $user->hasRight("commande", "lire")) {
193  include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
194  $commandestatic = new Commande($this->db);
195 
196  $langs->load("orders");
197 
198  $cachedir = DOL_DATA_ROOT.'/commande/temp';
199  $filename = '/boxactivity-order'.$fileid;
200  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
201  $data = array();
202 
203  if ($refresh) {
204  $sql = "SELECT c.fk_statut, sum(c.total_ttc) as Mnttot, count(*) as nb";
205  $sql .= " FROM (".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."commande as c";
206  if (empty($user->rights->societe->client->voir) && !$user->socid) {
207  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
208  }
209  $sql .= ")";
210  $sql .= " WHERE c.entity IN (".getEntity('commande').")";
211  $sql .= " AND c.fk_soc = s.rowid";
212  if (empty($user->rights->societe->client->voir) && !$user->socid) {
213  $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
214  }
215  if ($user->socid) {
216  $sql .= " AND s.rowid = ".((int) $user->socid);
217  }
218  $sql .= " AND c.date_commande >= '".$this->db->idate($tmpdate)."'";
219  $sql .= " GROUP BY c.fk_statut";
220  $sql .= " ORDER BY c.fk_statut DESC";
221 
222  $result = $this->db->query($sql);
223  if ($result) {
224  $num = $this->db->num_rows($result);
225  $j = 0;
226  while ($j < $num) {
227  $data[$j] = $this->db->fetch_object($result);
228  $j++;
229  }
230  if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
231  dol_filecache($cachedir, $filename, $data);
232  }
233  $this->db->free($result);
234  } else {
235  dol_print_error($this->db);
236  }
237  } else {
238  $data = dol_readcachefile($cachedir, $filename);
239  }
240 
241  if (!empty($data)) {
242  $j = 0;
243  while ($j < count($data)) {
244  $this->info_box_contents[$line][0] = array(
245  'td' => 'class="left" width="16"',
246  'url' => DOL_URL_ROOT."/commande/list.php?mainmenu=commercial&amp;leftmenu=orders&amp;search_status=".$data[$j]->fk_statut,
247  'tooltip' => $langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
248  'logo' => 'object_order',
249  );
250 
251  $this->info_box_contents[$line][1] = array(
252  'td' => '',
253  'text' =>$langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
254  );
255 
256  $this->info_box_contents[$line][2] = array(
257  'td' => 'class="right"',
258  'text' => $data[$j]->nb,
259  'tooltip' => $langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
260  'url' => DOL_URL_ROOT."/commande/list.php?mainmenu=commercial&amp;leftmenu=orders&amp;search_status=".$data[$j]->fk_statut,
261  );
262  $totalnb += $data[$j]->nb;
263 
264  $this->info_box_contents[$line][3] = array(
265  'td' => 'class="nowraponall right amount"',
266  'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
267  );
268  $this->info_box_contents[$line][4] = array(
269  'td' => 'class="right" width="18"',
270  'text' => $commandestatic->LibStatut($data[$j]->fk_statut, 0, 3),
271  );
272 
273  $line++;
274  $j++;
275  }
276  }
277  }
278 
279 
280  // list the summary of the bills
281  if (isModEnabled('facture') && $user->hasRight("facture", "lire")) {
282  include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
283  $facturestatic = new Facture($this->db);
284 
285  // part 1
286  $cachedir = DOL_DATA_ROOT.'/facture/temp';
287  $filename = '/boxactivity-invoice'.$fileid;
288 
289  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
290  $data = array();
291  if ($refresh) {
292  $sql = "SELECT f.fk_statut, SUM(f.total_ttc) as Mnttot, COUNT(*) as nb";
293  $sql .= " FROM (".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
294  if (empty($user->rights->societe->client->voir) && !$user->socid) {
295  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
296  }
297  $sql .= ")";
298  $sql .= " WHERE f.entity IN (".getEntity('invoice').')';
299  if (empty($user->rights->societe->client->voir) && !$user->socid) {
300  $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
301  }
302  if ($user->socid) {
303  $sql .= " AND s.rowid = ".((int) $user->socid);
304  }
305  $sql .= " AND f.fk_soc = s.rowid";
306  $sql .= " AND f.datef >= '".$this->db->idate($tmpdate)."' AND f.paye=1";
307  $sql .= " GROUP BY f.fk_statut";
308  $sql .= " ORDER BY f.fk_statut DESC";
309 
310  $result = $this->db->query($sql);
311  if ($result) {
312  $num = $this->db->num_rows($result);
313  $j = 0;
314  while ($j < $num) {
315  $data[$j] = $this->db->fetch_object($result);
316  $j++;
317  }
318  if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
319  dol_filecache($cachedir, $filename, $data);
320  }
321  $this->db->free($result);
322  } else {
323  dol_print_error($this->db);
324  }
325  } else {
326  $data = dol_readcachefile($cachedir, $filename);
327  }
328 
329  if (!empty($data)) {
330  $j = 0;
331  while ($j < count($data)) {
332  $billurl = "search_status=2&amp;paye=1";
333  $this->info_box_contents[$line][0] = array(
334  'td' => 'class="left" width="16"',
335  'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
336  'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
337  'logo' => 'bill',
338  );
339 
340  $this->info_box_contents[$line][1] = array(
341  'td' => '',
342  'text' => $langs->trans("Bills")."&nbsp;".$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
343  );
344 
345  $this->info_box_contents[$line][2] = array(
346  'td' => 'class="right"',
347  'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
348  'text' => $data[$j]->nb,
349  'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
350  );
351 
352  $this->info_box_contents[$line][3] = array(
353  'td' => 'class="nowraponall right amount"',
354  'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency)
355  );
356 
357  // We add only for the current year
358  $totalnb += $data[$j]->nb;
359 
360  $this->info_box_contents[$line][4] = array(
361  'td' => 'class="right" width="18"',
362  'text' => $facturestatic->LibStatut(1, $data[$j]->fk_statut, 3),
363  );
364  $line++;
365  $j++;
366  }
367  if (count($data) == 0) {
368  $this->info_box_contents[$line][0] = array(
369  'td' => 'class="center"',
370  'text'=>$langs->trans("NoRecordedInvoices"),
371  );
372  }
373  }
374 
375  // part 2
376  $cachedir = DOL_DATA_ROOT.'/facture/temp';
377  $filename = '/boxactivity-invoice2'.$fileid;
378 
379  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
380 
381  $data = array();
382  if ($refresh) {
383  $sql = "SELECT f.fk_statut, SUM(f.total_ttc) as Mnttot, COUNT(*) as nb";
384  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
385  $sql .= " WHERE f.entity IN (".getEntity('invoice').')';
386  $sql .= " AND f.fk_soc = s.rowid";
387  $sql .= " AND f.datef >= '".$this->db->idate($tmpdate)."' AND f.paye=0";
388  $sql .= " GROUP BY f.fk_statut";
389  $sql .= " ORDER BY f.fk_statut DESC";
390 
391  $result = $this->db->query($sql);
392  if ($result) {
393  $num = $this->db->num_rows($result);
394  $j = 0;
395  while ($j < $num) {
396  $data[$j] = $this->db->fetch_object($result);
397  $j++;
398  }
399  if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
400  dol_filecache($cachedir, $filename, $data);
401  }
402  $this->db->free($result);
403  } else {
404  dol_print_error($this->db);
405  }
406  } else {
407  $data = dol_readcachefile($cachedir, $filename);
408  }
409 
410  if (!empty($data)) {
411  $alreadypaid = -1;
412 
413  $j = 0;
414  while ($j < count($data)) {
415  $billurl = "search_status=".$data[$j]->fk_statut."&amp;paye=0";
416  $this->info_box_contents[$line][0] = array(
417  'td' => 'class="left" width="16"',
418  'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
419  'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
420  'logo' => 'bill',
421  );
422 
423  $this->info_box_contents[$line][1] = array(
424  'td' => '',
425  'text' => $langs->trans("Bills")."&nbsp;".$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
426  );
427 
428  $this->info_box_contents[$line][2] = array(
429  'td' => 'class="right"',
430  'text' => $data[$j]->nb,
431  'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
432  'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
433  );
434  $totalnb += $data[$j]->nb;
435  $this->info_box_contents[$line][3] = array(
436  'td' => 'class="nowraponall right amount"',
437  'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
438  );
439  $this->info_box_contents[$line][4] = array(
440  'td' => 'class="right" width="18"',
441  'text' => $facturestatic->LibStatut(0, $data[$j]->fk_statut, 3, $alreadypaid),
442  );
443  $line++;
444  $j++;
445  }
446  if (count($data) == 0) {
447  $this->info_box_contents[$line][0] = array(
448  'td' => 'class="center"',
449  'text'=>$langs->trans("NoRecordedInvoices"),
450  );
451  }
452  }
453  }
454 
455  // Add the sum in the bottom of the boxes
456  $this->info_box_contents[$line][0] = array('tr' => 'class="liste_total_wrap"');
457  $this->info_box_contents[$line][1] = array('td' => 'class="liste_total left" ', 'text' => $langs->trans("Total")."&nbsp;".$textHead);
458  $this->info_box_contents[$line][2] = array('td' => 'class="liste_total right" ', 'text' => $totalnb);
459  $this->info_box_contents[$line][3] = array('td' => 'class="liste_total right" ', 'text' => '');
460  $this->info_box_contents[$line][4] = array('td' => 'class="liste_total right" ', 'text' => "");
461  }
462 
463 
472  public function showBox($head = null, $contents = null, $nooutput = 0)
473  {
474  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
475  }
476 }
Class to manage customers orders.
Class to manage invoices.
Class ModeleBoxes.
Class to manage proposals.
Class to manage the box of customer activity (invoice, order, proposal)
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
__construct($db, $param)
Constructor.
loadBox($max=5)
Charge les donnees en memoire pour affichage ulterieur.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition: date.lib.php:121
dol_filecache($directory, $filename, $object)
Store object in file.
Definition: files.lib.php:3192
dol_readcachefile($directory, $filename)
Read object from cachefile.
Definition: files.lib.php:3225
dol_cache_refresh($directory, $filename, $cachetime)
Test if Refresh needed.
Definition: files.lib.php:3210
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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_now($mode='auto')
Return date for now.
isModEnabled($module)
Is Dolibarr module enabled.
$conf db
API class for accounts.
Definition: inc.php:41