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