dolibarr  16.0.5
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 <https://www.gnu.org/licenses/>.
20  */
21 
28 include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
29 
30 
35 {
36  public $boxcode = "goodcustomers";
37  public $boximg = "object_company";
38  public $boxlabel = "BoxGoodCustomers";
39  public $depends = array("societe");
40 
44  public $db;
45 
46  public $enabled = 1;
47 
48  public $info_box_head = array();
49  public $info_box_contents = array();
50 
51 
58  public 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)) {
66  $this->enabled = 0; // disabled by this option
67  }
68  if (empty($conf->global->MAIN_BOX_ENABLE_BEST_CUSTOMERS)) {
69  $this->enabled = 0; // not enabled by default. Very slow on large database
70  }
71 
72  $this->hidden = empty($user->rights->societe->lire);
73  }
74 
81  public function loadBox($max = 5)
82  {
83  global $user, $langs, $conf;
84  $langs->load("boxes");
85 
86  $this->max = $max;
87 
88  include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
89  $thirdpartystatic = new Societe($this->db);
90 
91  $this->info_box_head = array('text' => $langs->trans("BoxTitleGoodCustomers", $max));
92 
93  if ($user->rights->societe->lire) {
94  $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,";
95  $sql .= " count(*) as nbfact, sum(".$this->db->ifsql('f.paye=1', '1', '0').") as nbfactpaye";
96  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture as f";
97  $sql .= ' WHERE s.entity IN ('.getEntity('societe').')';
98  $sql .= ' AND s.rowid = f.fk_soc';
99  $sql .= " GROUP BY s.rowid, s.nom, s.logo, s.code_client, s.code_fournisseur, s.client, s.fournisseur, s.tms, s.status";
100  $sql .= $this->db->order("nbfact", "DESC");
101  $sql .= $this->db->plimit($max, 0);
102 
103  dol_syslog(get_class($this)."::loadBox", LOG_DEBUG);
104  $result = $this->db->query($sql);
105  if ($result) {
106  $num = $this->db->num_rows($result);
107 
108  $line = 0;
109  while ($line < $num) {
110  $objp = $this->db->fetch_object($result);
111  $datem = $this->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' => 'class="tdoverflowmax150"',
124  'text' => $thirdpartystatic->getNomUrl(1),
125  'asis' => 1,
126  );
127 
128  $this->info_box_contents[$line][] = array(
129  'td' => 'class="center nowraponall"',
130  'text' => dol_print_date($datem, "day", 'tzuserrel')
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' => 'class="right" width="18"',
140  'text' => $thirdpartystatic->LibStatut($objp->status, 3)
141  );
142 
143  $line++;
144  }
145 
146  if ($num == 0) {
147  $this->info_box_contents[$line][0] = array(
148  'td' => 'class="center opacitymedium"',
149  'text'=>$langs->trans("NoRecordedCustomers")
150  );
151  }
152 
153  $this->db->free($result);
154  } else {
155  $this->info_box_contents[0][0] = array(
156  'td' => '',
157  'maxlength'=>500,
158  'text' => ($this->db->error().' sql='.$sql),
159  );
160  }
161  } else {
162  $this->info_box_contents[0][0] = array(
163  'td' => 'class="nohover opacitymedium left"',
164  'text' => $langs->trans("ReadPermissionNotAllowed")
165  );
166  }
167  }
168 
177  public function showBox($head = null, $contents = null, $nooutput = 0)
178  {
179  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
180  }
181 }
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
db
$conf db
API class for accounts.
Definition: inc.php:41
box_goodcustomers
Class to manage the box to show last thirdparties.
Definition: box_goodcustomers.php:34
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
box_goodcustomers\__construct
__construct($db, $param='')
Constructor.
Definition: box_goodcustomers.php:58
box_goodcustomers\showBox
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
Definition: box_goodcustomers.php:177
box_goodcustomers\loadBox
loadBox($max=5)
Load data for box to show them later.
Definition: box_goodcustomers.php:81
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
ModeleBoxes
Class ModeleBoxes.
Definition: modules_boxes.php:34