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