dolibarr  9.0.0
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@inodbox.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 
44  public $db;
45 
46  var $enabled = 1;
47 
48  var $info_box_head = array();
49  var $info_box_contents = array();
50 
51 
58  function __construct($db,$param='')
59  {
60  global $conf, $user;
61 
62  $this->db = $db;
63 
64  // disable box for such cases
65  if (! empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) $this->enabled=0; // disabled by this option
66  if (empty($conf->global->MAIN_BOX_ENABLE_BEST_CUSTOMERS)) $this->enabled=0; // not enabled by default. Very slow on large database
67 
68  $this->hidden = ! ($user->rights->societe->lire);
69  }
70 
77  function loadBox($max=5)
78  {
79  global $user, $langs, $db, $conf;
80  $langs->load("boxes");
81 
82  $this->max=$max;
83 
84  include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
85  $thirdpartystatic=new Societe($db);
86 
87  $this->info_box_head = array('text' => $langs->trans("BoxTitleGoodCustomers",$max));
88 
89  if ($user->rights->societe->lire)
90  {
91 
92  $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,";
93  $sql.= " count(*) as nbfact, sum(". $db->ifsql('f.paye=1','1','0').") as nbfactpaye";
94  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture as f";
95  $sql.= ' WHERE s.entity IN ('.getEntity('societe').')';
96  $sql.= ' AND s.rowid = f.fk_soc';
97  $sql.= " GROUP BY s.rowid, s.nom, s.logo, s.code_client, s.code_fournisseur, s.client, s.fournisseur, s.tms, s.status";
98  $sql.= $db->order("nbfact","DESC");
99  $sql.= $db->plimit($max,0);
100 
101  dol_syslog(get_class($this)."::loadBox", LOG_DEBUG);
102  $result = $db->query($sql);
103  if ($result)
104  {
105  $num = $db->num_rows($result);
106 
107  $line = 0;
108  while ($line < $num)
109  {
110  $objp = $db->fetch_object($result);
111  $datem=$db->jdate($objp->tms);
112  $thirdpartystatic->id = $objp->rowid;
113  $thirdpartystatic->name = $objp->name;
114  $thirdpartystatic->code_client = $objp->code_client;
115  $thirdpartystatic->code_fournisseur = $objp->code_fournisseur;
116  $thirdpartystatic->client = $objp->client;
117  $thirdpartystatic->fournisseur = $objp->fournisseur;
118  $thirdpartystatic->logo = $objp->logo;
119  $nbfact = $objp->nbfact;
120  $nbimpaye = $objp->nbfact - $objp->nbfactpaye;
121 
122  $this->info_box_contents[$line][] = array(
123  'td' => '',
124  'text' => $thirdpartystatic->getNomUrl(1),
125  'asis' => 1,
126  );
127 
128  $this->info_box_contents[$line][] = array(
129  'td' => 'class="right"',
130  'text' => dol_print_date($datem, "day")
131  );
132 
133  $this->info_box_contents[$line][] = array(
134  'td' => 'class="right"',
135  'text' => $nbfact.( $nbimpaye != 0 ? ' ('.$nbimpaye.')':'')
136  );
137 
138  $this->info_box_contents[$line][] = array(
139  'td' => 'align="right" width="18"',
140  'text' => $thirdpartystatic->LibStatut($objp->status,3)
141  );
142 
143  $line++;
144  }
145 
146  if ($num==0) $this->info_box_contents[$line][0] = array('td' => 'align="center"','text'=>$langs->trans("NoRecordedCustomers"));
147 
148  $db->free($result);
149  }
150  else {
151  $this->info_box_contents[0][0] = array( 'td' => '',
152  'maxlength'=>500,
153  'text' => ($db->error().' sql='.$sql));
154  }
155  }
156  else {
157  $this->info_box_contents[0][0] = array(
158  'td' => 'align="left" class="nohover opacitymedium"',
159  'text' => $langs->trans("ReadPermissionNotAllowed")
160  );
161  }
162  }
163 
172  function showBox($head = null, $contents = null, $nooutput=0)
173  {
174  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
175  }
176 }
177 
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).