dolibarr  13.0.2
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-2019 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 = !((!empty($conf->facture->enabled) && $user->rights->facture->lire)
66  || (!empty($conf->commande->enabled) && $user->rights->commande->lire)
67  || (!empty($conf->propal->enabled) && $user->rights->propale->lire)
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->rights->societe->client->voir ? '1' : '0').'.cache';
88  $now = dol_now();
89  $nbofperiod = 3;
90 
91  if (!empty($conf->global->MAIN_BOX_ACTIVITY_DURATION)) $nbofperiod = $conf->global->MAIN_BOX_ACTIVITY_DURATION;
92  $textHead = $langs->trans("Activity").' - '.$langs->trans("LastXMonthRolling", $nbofperiod);
93  $this->info_box_head = array(
94  'text' => $textHead,
95  'limit'=> dol_strlen($textHead),
96  );
97 
98  // compute the year limit to show
99  $tmpdate = dol_time_plus_duree(dol_now(), -1 * $nbofperiod, "m");
100 
101 
102  // list the summary of the propals
103  if (!empty($conf->propal->enabled) && $user->rights->propale->lire)
104  {
105  include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
106  $propalstatic = new Propal($this->db);
107 
108  $cachedir = DOL_DATA_ROOT.'/propale/temp';
109  $filename = '/boxactivity-propal'.$fileid;
110  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
111  $data = array();
112  if ($refresh)
113  {
114  $sql = "SELECT p.fk_statut, SUM(p.total) as Mnttot, COUNT(*) as nb";
115  $sql .= " FROM (".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."propal as p";
116  if (!$user->rights->societe->client->voir && !$user->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
117  $sql .= ")";
118  $sql .= " WHERE p.entity IN (".getEntity('propal').")";
119  $sql .= " AND p.fk_soc = s.rowid";
120  if (!$user->rights->societe->client->voir && !$user->socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
121  if ($user->socid) $sql .= " AND s.rowid = ".$user->socid;
122  $sql .= " AND p.datep >= '".$this->db->idate($tmpdate)."'";
123  $sql .= " AND p.date_cloture IS NULL"; // just unclosed
124  $sql .= " GROUP BY p.fk_statut";
125  $sql .= " ORDER BY p.fk_statut DESC";
126 
127  $result = $this->db->query($sql);
128  if ($result)
129  {
130  $num = $this->db->num_rows($result);
131 
132  $j = 0;
133  while ($j < $num) {
134  $data[$j] = $this->db->fetch_object($result);
135  $j++;
136  }
137  if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
138  dol_filecache($cachedir, $filename, $data);
139  }
140  $this->db->free($result);
141  } else {
142  dol_print_error($this->db);
143  }
144  } else {
145  $data = dol_readcachefile($cachedir, $filename);
146  }
147 
148  if (!empty($data))
149  {
150  $j = 0;
151  while ($j < count($data))
152  {
153  $this->info_box_contents[$line][0] = array(
154  'td' => 'class="left" width="16"',
155  'url' => DOL_URL_ROOT."/comm/propal/list.php?mainmenu=commercial&amp;leftmenu=propals&amp;search_status=".$data[$j]->fk_statut,
156  'tooltip' => $langs->trans("Proposals")."&nbsp;".$propalstatic->LibStatut($data[$j]->fk_statut, 0),
157  'logo' => 'object_propal'
158  );
159 
160  $this->info_box_contents[$line][1] = array(
161  'td' => '',
162  'text' => $langs->trans("Proposals")."&nbsp;".$propalstatic->LibStatut($data[$j]->fk_statut, 0),
163  );
164 
165  $this->info_box_contents[$line][2] = array(
166  'td' => 'class="right"',
167  'text' => $data[$j]->nb,
168  'tooltip' => $langs->trans("Proposals")."&nbsp;".$propalstatic->LibStatut($data[$j]->fk_statut, 0),
169  'url' => DOL_URL_ROOT."/comm/propal/list.php?mainmenu=commercial&amp;leftmenu=propals&amp;search_status=".$data[$j]->fk_statut,
170  );
171  $totalnb += $data[$j]->nb;
172 
173  $this->info_box_contents[$line][3] = array(
174  'td' => 'class="nowraponall right"',
175  'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
176  );
177  $this->info_box_contents[$line][4] = array(
178  'td' => 'class="right" width="18"',
179  'text' => $propalstatic->LibStatut($data[$j]->fk_statut, 3),
180  );
181 
182  $line++;
183  $j++;
184  }
185  }
186  }
187 
188  // list the summary of the orders
189  if (!empty($conf->commande->enabled) && $user->rights->commande->lire) {
190  include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
191  $commandestatic = new Commande($this->db);
192 
193  $langs->load("orders");
194 
195  $cachedir = DOL_DATA_ROOT.'/commande/temp';
196  $filename = '/boxactivity-order'.$fileid;
197  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
198  $data = array();
199 
200  if ($refresh) {
201  $sql = "SELECT c.fk_statut, sum(c.total_ttc) as Mnttot, count(*) as nb";
202  $sql .= " FROM (".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."commande as c";
203  if (!$user->rights->societe->client->voir && !$user->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
204  $sql .= ")";
205  $sql .= " WHERE c.entity IN (".getEntity('commande').")";
206  $sql .= " AND c.fk_soc = s.rowid";
207  if (!$user->rights->societe->client->voir && !$user->socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
208  if ($user->socid) $sql .= " AND s.rowid = ".$user->socid;
209  $sql .= " AND c.date_commande >= '".$this->db->idate($tmpdate)."'";
210  $sql .= " GROUP BY c.fk_statut";
211  $sql .= " ORDER BY c.fk_statut DESC";
212 
213  $result = $this->db->query($sql);
214  if ($result) {
215  $num = $this->db->num_rows($result);
216  $j = 0;
217  while ($j < $num) {
218  $data[$j] = $this->db->fetch_object($result);
219  $j++;
220  }
221  if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
222  dol_filecache($cachedir, $filename, $data);
223  }
224  $this->db->free($result);
225  } else {
226  dol_print_error($this->db);
227  }
228  } else {
229  $data = dol_readcachefile($cachedir, $filename);
230  }
231 
232  if (!empty($data)) {
233  $j = 0;
234  while ($j < count($data)) {
235  $this->info_box_contents[$line][0] = array(
236  'td' => 'class="left" width="16"',
237  'url' => DOL_URL_ROOT."/commande/list.php?mainmenu=commercial&amp;leftmenu=orders&amp;search_status=".$data[$j]->fk_statut,
238  'tooltip' => $langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
239  'logo' => 'object_order',
240  );
241 
242  $this->info_box_contents[$line][1] = array(
243  'td' => '',
244  'text' =>$langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
245  );
246 
247  $this->info_box_contents[$line][2] = array(
248  'td' => 'class="right"',
249  'text' => $data[$j]->nb,
250  'tooltip' => $langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
251  'url' => DOL_URL_ROOT."/commande/list.php?mainmenu=commercial&amp;leftmenu=orders&amp;search_status=".$data[$j]->fk_statut,
252  );
253  $totalnb += $data[$j]->nb;
254 
255  $this->info_box_contents[$line][3] = array(
256  'td' => 'class="nowraponall right"',
257  'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
258  );
259  $this->info_box_contents[$line][4] = array(
260  'td' => 'class="right" width="18"',
261  'text' => $commandestatic->LibStatut($data[$j]->fk_statut, 0, 3),
262  );
263 
264  $line++;
265  $j++;
266  }
267  }
268  }
269 
270 
271  // list the summary of the bills
272  if (!empty($conf->facture->enabled) && $user->rights->facture->lire)
273  {
274  include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
275  $facturestatic = new Facture($this->db);
276 
277  // part 1
278  $cachedir = DOL_DATA_ROOT.'/facture/temp';
279  $filename = '/boxactivity-invoice'.$fileid;
280 
281  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
282  $data = array();
283  if ($refresh)
284  {
285  $sql = "SELECT f.fk_statut, SUM(f.total_ttc) as Mnttot, COUNT(*) as nb";
286  $sql .= " FROM (".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
287  if (!$user->rights->societe->client->voir && !$user->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
288  $sql .= ")";
289  $sql .= " WHERE f.entity IN (".getEntity('invoice').')';
290  if (!$user->rights->societe->client->voir && !$user->socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
291  if ($user->socid) $sql .= " AND s.rowid = ".$user->socid;
292  $sql .= " AND f.fk_soc = s.rowid";
293  $sql .= " AND f.datef >= '".$this->db->idate($tmpdate)."' AND f.paye=1";
294  $sql .= " GROUP BY f.fk_statut";
295  $sql .= " ORDER BY f.fk_statut DESC";
296 
297  $result = $this->db->query($sql);
298  if ($result) {
299  $num = $this->db->num_rows($result);
300  $j = 0;
301  while ($j < $num) {
302  $data[$j] = $this->db->fetch_object($result);
303  $j++;
304  }
305  if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
306  dol_filecache($cachedir, $filename, $data);
307  }
308  $this->db->free($result);
309  } else {
310  dol_print_error($this->db);
311  }
312  } else {
313  $data = dol_readcachefile($cachedir, $filename);
314  }
315 
316  if (!empty($data)) {
317  $j = 0;
318  while ($j < count($data)) {
319  $billurl = "search_status=2&amp;paye=1&amp;year=".$data[$j]->annee;
320  $this->info_box_contents[$line][0] = array(
321  'td' => 'class="left" width="16"',
322  'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
323  'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
324  'logo' => 'bill',
325  );
326 
327  $this->info_box_contents[$line][1] = array(
328  'td' => '',
329  'text' => $langs->trans("Bills")."&nbsp;".$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0)." ".$data[$j]->annee,
330  );
331 
332  $this->info_box_contents[$line][2] = array(
333  'td' => 'class="right"',
334  'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
335  'text' => $data[$j]->nb,
336  'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
337  );
338 
339  $this->info_box_contents[$line][3] = array(
340  'td' => 'class="nowraponall right"',
341  'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency)
342  );
343 
344  // We add only for the current year
345  $totalnb += $data[$j]->nb;
346 
347  $this->info_box_contents[$line][4] = array(
348  'td' => 'class="right" width="18"',
349  'text' => $facturestatic->LibStatut(1, $data[$j]->fk_statut, 3),
350  );
351  $line++;
352  $j++;
353  }
354  if (count($data) == 0)
355  $this->info_box_contents[$line][0] = array(
356  'td' => 'class="center"',
357  'text'=>$langs->trans("NoRecordedInvoices"),
358  );
359  }
360 
361  // part 2
362  $cachedir = DOL_DATA_ROOT.'/facture/temp';
363  $filename = '/boxactivity-invoice2'.$fileid;
364 
365  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
366 
367  $data = array();
368  if ($refresh) {
369  $sql = "SELECT f.fk_statut, SUM(f.total_ttc) as Mnttot, COUNT(*) as nb";
370  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
371  $sql .= " WHERE f.entity IN (".getEntity('invoice').')';
372  $sql .= " AND f.fk_soc = s.rowid";
373  $sql .= " AND f.datef >= '".$this->db->idate($tmpdate)."' AND f.paye=0";
374  $sql .= " GROUP BY f.fk_statut";
375  $sql .= " ORDER BY f.fk_statut DESC";
376 
377  $result = $this->db->query($sql);
378  if ($result) {
379  $num = $this->db->num_rows($result);
380  $j = 0;
381  while ($j < $num) {
382  $data[$j] = $this->db->fetch_object($result);
383  $j++;
384  }
385  if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
386  dol_filecache($cachedir, $filename, $data);
387  }
388  $this->db->free($result);
389  } else {
390  dol_print_error($this->db);
391  }
392  } else {
393  $data = dol_readcachefile($cachedir, $filename);
394  }
395 
396  if (!empty($data)) {
397  $alreadypaid = -1;
398 
399  $j = 0;
400  while ($j < count($data)) {
401  $billurl = "search_status=".$data[$j]->fk_statut."&amp;paye=0";
402  $this->info_box_contents[$line][0] = array(
403  'td' => 'class="left" width="16"',
404  'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
405  'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
406  'logo' => 'bill',
407  );
408 
409  $this->info_box_contents[$line][1] = array(
410  'td' => '',
411  'text' => $langs->trans("Bills")."&nbsp;".$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
412  );
413 
414  $this->info_box_contents[$line][2] = array(
415  'td' => 'class="right"',
416  'text' => $data[$j]->nb,
417  'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
418  'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
419  );
420  $totalnb += $data[$j]->nb;
421  $this->info_box_contents[$line][3] = array(
422  'td' => 'class="nowraponall right"',
423  'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
424  );
425  $this->info_box_contents[$line][4] = array(
426  'td' => 'class="right" width="18"',
427  'text' => $facturestatic->LibStatut(0, $data[$j]->fk_statut, 3, $alreadypaid),
428  );
429  $line++;
430  $j++;
431  }
432  if (count($data) == 0) {
433  $this->info_box_contents[$line][0] = array(
434  'td' => 'class="center"',
435  'text'=>$langs->trans("NoRecordedInvoices"),
436  );
437  }
438  }
439  }
440 
441  // Add the sum in the bottom of the boxes
442  $this->info_box_contents[$line][0] = array('tr' => 'class="liste_total_wrap"');
443  $this->info_box_contents[$line][1] = array('td' => 'class="liste_total left" ', 'text' => $langs->trans("Total")."&nbsp;".$textHead);
444  $this->info_box_contents[$line][2] = array('td' => 'class="liste_total right" ', 'text' => $totalnb);
445  $this->info_box_contents[$line][3] = array('td' => 'class="liste_total right" ', 'text' => '');
446  $this->info_box_contents[$line][4] = array('td' => 'class="liste_total right" ', 'text' => "");
447  }
448 
449 
458  public function showBox($head = null, $contents = null, $nooutput = 0)
459  {
460  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
461  }
462 }
__construct($db, $param)
Constructor.
dol_cache_refresh($directory, $filename, $cachetime)
Test if Refresh needed.
Definition: files.lib.php:2943
dol_now($mode= 'auto')
Return date for now.
Class ModeleBoxes.
$conf db
API class for accounts.
Definition: inc.php:54
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...
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
Class to manage customers orders.
loadBox($max=5)
Charge les donnees en memoire pour affichage ulterieur.
Class to manage the box of customer activity (invoice, order, proposal)
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_readcachefile($directory, $filename)
Read object from cachefile.
Definition: files.lib.php:2958
Class to manage invoices.
dol_time_plus_duree($time, $duration_value, $duration_unit)
Add a delay to a date.
Definition: date.lib.php:114
dol_filecache($directory, $filename, $object)
Store object in file.
Definition: files.lib.php:2927
Class to manage proposals.