dolibarr  7.0.0-beta
box_goodcustomers.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
5  * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
6  * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
28 include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
29 
30 
35 {
36  var $boxcode="goodcustomers";
37  var $boximg="object_company";
38  var $boxlabel="BoxGoodCustomers";
39  var $depends = array("societe");
40 
41  var $db;
42  var $enabled = 1;
43 
44  var $info_box_head = array();
45  var $info_box_contents = array();
46 
47 
54  function __construct($db,$param='')
55  {
56  global $conf, $user;
57 
58  $this->db = $db;
59 
60  // disable box for such cases
61  if (! empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) $this->enabled=0; // disabled by this option
62  if (empty($conf->global->MAIN_BOX_ENABLE_BEST_CUSTOMERS)) $this->enabled=0; // not enabled by default. Very slow on large database
63 
64  $this->hidden = ! ($user->rights->societe->lire);
65  }
66 
73  function loadBox($max=5)
74  {
75  global $user, $langs, $db, $conf;
76  $langs->load("boxes");
77 
78  $this->max=$max;
79 
80  include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
81  $thirdpartystatic=new Societe($db);
82 
83  $this->info_box_head = array('text' => $langs->trans("BoxTitleGoodCustomers",$max));
84 
85  if ($user->rights->societe->lire)
86  {
87 
88  $sql = "SELECT s.rowid, s.nom as name, s.logo, s.code_client, s.code_fournisseur, s.client, s.fournisseur, s.tms as datem, s.status as status,";
89  $sql.= " count(*) as nbfact, sum(". $db->ifsql('f.paye=1','1','0').") as nbfactpaye";
90  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture as f";
91  $sql.= ' WHERE s.entity IN ('.getEntity('societe').')';
92  $sql.= ' AND s.rowid = f.fk_soc';
93  $sql.= " GROUP BY s.rowid, s.nom, s.logo, s.code_client, s.code_fournisseur, s.client, s.fournisseur, s.tms, s.status";
94  $sql.= $db->order("nbfact","DESC");
95  $sql.= $db->plimit($max,0);
96 
97  dol_syslog(get_class($this)."::loadBox", LOG_DEBUG);
98  $result = $db->query($sql);
99  if ($result)
100  {
101  $num = $db->num_rows($result);
102 
103  $line = 0;
104  while ($line < $num)
105  {
106  $objp = $db->fetch_object($result);
107  $datem=$db->jdate($objp->tms);
108  $thirdpartystatic->id = $objp->rowid;
109  $thirdpartystatic->name = $objp->name;
110  $thirdpartystatic->code_client = $objp->code_client;
111  $thirdpartystatic->code_fournisseur = $objp->code_fournisseur;
112  $thirdpartystatic->client = $objp->client;
113  $thirdpartystatic->fournisseur = $objp->fournisseur;
114  $thirdpartystatic->logo = $objp->logo;
115  $nbfact = $objp->nbfact;
116  $nbimpaye = $objp->nbfact - $objp->nbfactpaye;
117 
118  $this->info_box_contents[$line][] = array(
119  'td' => '',
120  'text' => $thirdpartystatic->getNomUrl(1),
121  'asis' => 1,
122  );
123 
124  $this->info_box_contents[$line][] = array(
125  'td' => 'class="right"',
126  'text' => dol_print_date($datem, "day")
127  );
128 
129  $this->info_box_contents[$line][] = array(
130  'td' => 'class="right"',
131  'text' => $nbfact.( $nbimpaye != 0 ? ' ('.$nbimpaye.')':'')
132  );
133 
134  $this->info_box_contents[$line][] = array(
135  'td' => 'align="right" width="18"',
136  'text' => $thirdpartystatic->LibStatut($objp->status,3)
137  );
138 
139  $line++;
140  }
141 
142  if ($num==0) $this->info_box_contents[$line][0] = array('td' => 'align="center"','text'=>$langs->trans("NoRecordedCustomers"));
143 
144  $db->free($result);
145  }
146  else {
147  $this->info_box_contents[0][0] = array( 'td' => '',
148  'maxlength'=>500,
149  'text' => ($db->error().' sql='.$sql));
150  }
151  }
152  else {
153  $this->info_box_contents[0][0] = array(
154  'td' => 'align="left" class="nohover opacitymedium"',
155  'text' => $langs->trans("ReadPermissionNotAllowed")
156  );
157  }
158 
159  }
160 
169  function showBox($head = null, $contents = null, $nooutput=0)
170  {
171  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
172  }
173 }
174 
Class to manage the box to show last thirdparties.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
__construct($db, $param='')
Constructor.
Class ModeleBoxes.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage third parties objects (customers, suppliers, prospects...)
loadBox($max=5)
Load data for box to show them later.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).