dolibarr  20.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@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 
41  public $enabled = 1;
42 
49  public function __construct($db, $param = '')
50  {
51  global $user;
52 
53  $this->db = $db;
54 
55  // disable box for such cases
56  if (getDolGlobalString('SOCIETE_DISABLE_CUSTOMERS')) {
57  $this->enabled = 0; // disabled by this option
58  }
59  if (!getDolGlobalString('MAIN_BOX_ENABLE_BEST_CUSTOMERS')) {
60  $this->enabled = 0; // not enabled by default. Very slow on large database
61  }
62 
63  $this->hidden = !$user->hasRight('societe', 'lire');
64  }
65 
72  public function loadBox($max = 5)
73  {
74  global $user, $langs, $conf;
75  $langs->load("boxes");
76 
77  $this->max = $max;
78 
79  include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
80  $thirdpartystatic = new Societe($this->db);
81 
82  $this->info_box_head = array('text' => $langs->trans("BoxTitleGoodCustomers", $max));
83 
84  if ($user->hasRight('societe', 'lire')) {
85  $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,";
86  $sql .= " count(*) as nbfact, sum(".$this->db->ifsql('f.paye=1', '1', '0').") as nbfactpaye";
87  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture as f";
88  $sql .= ' WHERE s.entity IN ('.getEntity('societe').')';
89  $sql .= ' AND s.rowid = f.fk_soc';
90  $sql .= " GROUP BY s.rowid, s.nom, s.logo, s.code_client, s.code_fournisseur, s.client, s.fournisseur, s.tms, s.status";
91  $sql .= $this->db->order("nbfact", "DESC");
92  $sql .= $this->db->plimit($max, 0);
93 
94  dol_syslog(get_class($this)."::loadBox", LOG_DEBUG);
95  $result = $this->db->query($sql);
96  if ($result) {
97  $num = $this->db->num_rows($result);
98 
99  $line = 0;
100  while ($line < $num) {
101  $objp = $this->db->fetch_object($result);
102  $datem = $this->db->jdate($objp->tms);
103  $thirdpartystatic->id = $objp->rowid;
104  $thirdpartystatic->name = $objp->name;
105  $thirdpartystatic->code_client = $objp->code_client;
106  $thirdpartystatic->code_fournisseur = $objp->code_fournisseur;
107  $thirdpartystatic->client = $objp->client;
108  $thirdpartystatic->fournisseur = $objp->fournisseur;
109  $thirdpartystatic->logo = $objp->logo;
110  $nbfact = $objp->nbfact;
111  $nbimpaye = $objp->nbfact - $objp->nbfactpaye;
112 
113  $this->info_box_contents[$line][] = array(
114  'td' => 'class="tdoverflowmax150"',
115  'text' => $thirdpartystatic->getNomUrl(1),
116  'asis' => 1,
117  );
118 
119  $this->info_box_contents[$line][] = array(
120  'td' => 'class="center nowraponall"',
121  'text' => dol_print_date($datem, "day", 'tzuserrel')
122  );
123 
124  $this->info_box_contents[$line][] = array(
125  'td' => 'class="right"',
126  'text' => $nbfact.($nbimpaye != 0 ? ' ('.$nbimpaye.')' : '')
127  );
128 
129  $this->info_box_contents[$line][] = array(
130  'td' => 'class="right" width="18"',
131  'text' => $thirdpartystatic->LibStatut($objp->status, 3)
132  );
133 
134  $line++;
135  }
136 
137  if ($num == 0) {
138  $this->info_box_contents[$line][0] = array(
139  'td' => 'class="center"',
140  'text'=> '<span class="opacitymedium">'.$langs->trans("NoRecordedCustomers").'</span>'
141  );
142  }
143 
144  $this->db->free($result);
145  } else {
146  $this->info_box_contents[0][0] = array(
147  'td' => '',
148  'maxlength'=>500,
149  'text' => ($this->db->error().' sql='.$sql),
150  );
151  }
152  } else {
153  $this->info_box_contents[0][0] = array(
154  'td' => 'class="nohover left"',
155  'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
156  );
157  }
158  }
159 
168  public function showBox($head = null, $contents = null, $nooutput = 0)
169  {
170  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
171  }
172 }
Class ModeleBoxes.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage the box to show top-selling customers.
loadBox($max=5)
Load data for box to show them later.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
__construct($db, $param='')
Constructor.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.