dolibarr  13.0.2
box_customers_outstanding_bill_reached.php
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  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
28 
29 
34 {
35  public $boxcode = "customersoutstandingbillreached";
36  public $boximg = "object_company";
37  public $boxlabel = "BoxCustomersOutstandingBillReached";
38  public $depends = array("facture","societe");
39 
43  public $db;
44 
45  public $enabled = 1;
46 
47  public $info_box_head = array();
48  public $info_box_contents = array();
49 
50 
57  public function __construct($db, $param = '')
58  {
59  global $conf, $user;
60 
61  $this->db = $db;
62 
63  // disable box for such cases
64  if (!empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) $this->enabled = 0; // disabled by this option
65 
66  $this->hidden = !($user->rights->societe->lire && empty($user->socid));
67  }
68 
75  public function loadBox($max = 5)
76  {
77  global $user, $langs, $conf;
78  $langs->load("boxes");
79 
80  $this->max = $max;
81 
82  include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
83  $thirdpartystatic = new Societe($this->db);
84 
85  $this->info_box_head = array('text' => $langs->trans("BoxTitleLastOutstandingBillReached", $max));
86 
87  if ($user->rights->societe->lire)
88  {
89  $sql = "SELECT s.rowid as socid, s.nom as name, s.name_alias";
90  $sql .= ", s.code_client, s.code_compta, s.client";
91  $sql .= ", s.logo, s.email, s.entity";
92  $sql .= ", s.outstanding_limit";
93  $sql .= ", s.datec, s.tms, s.status";
94  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
95  if (!$user->rights->societe->client->voir && !$user->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
96  $sql .= " WHERE s.client IN (1, 3)";
97  $sql .= " AND s.entity IN (".getEntity('societe').")";
98  if (!$user->rights->societe->client->voir && !$user->socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
99  if ($user->socid) $sql .= " AND s.rowid = $user->socid";
100  $sql .= " AND s.outstanding_limit > 0";
101  $sql .= " AND s.rowid IN (SELECT fk_soc from ".MAIN_DB_PREFIX."facture as f WHERE f.fk_statut = 1 and f.fk_soc = s.rowid)";
102  $sql .= " ORDER BY s.tms DESC";
103  //$sql .= $this->db->plimit($max, 0);
104 
105  dol_syslog(get_class($this)."::loadBox", LOG_DEBUG);
106  $result = $this->db->query($sql);
107  if ($result)
108  {
109  $num = $this->db->num_rows($result);
110 
111  $nboutstandingbillreachedcustomers = 0;
112 
113  $line = 0;
114  while ($line < $num)
115  {
116  $objp = $this->db->fetch_object($result);
117  $datec = $this->db->jdate($objp->datec);
118  $datem = $this->db->jdate($objp->tms);
119 
120  $thirdpartystatic->id = $objp->socid;
121  $thirdpartystatic->name = $objp->name;
122  //$thirdpartystatic->name_alias = $objp->name_alias;
123  $thirdpartystatic->code_client = $objp->code_client;
124  $thirdpartystatic->code_compta = $objp->code_compta;
125  $thirdpartystatic->client = $objp->client;
126  $thirdpartystatic->logo = $objp->logo;
127  $thirdpartystatic->email = $objp->email;
128  $thirdpartystatic->entity = $objp->entity;
129  $thirdpartystatic->outstanding_limit = $objp->outstanding_limit;
130 
131  $outstandingtotal = $thirdpartystatic->getOutstandingBills()['opened'];
132  $outstandinglimit = $thirdpartystatic->outstanding_limit;
133 
134  if ($outstandingtotal >= $outstandinglimit)
135  {
136  $this->info_box_contents[$nboutstandingbillreachedcustomers][] = array(
137  'td' => '',
138  'text' => $thirdpartystatic->getNomUrl(1, 'customer'),
139  'asis' => 1,
140  );
141 
142  $this->info_box_contents[$nboutstandingbillreachedcustomers][] = array(
143  'td' => 'class="right" width="18"',
144  'text' => $thirdpartystatic->LibStatut($objp->status, 3)
145  );
146  $nboutstandingbillreachedcustomers++;
147  }
148 
149  $line++;
150  }
151 
152  if ($num == 0 || $nboutstandingbillreachedcustomers == 0) $this->info_box_contents[$line][0] = array(
153  'td' => 'class="center"',
154  'text'=> '<span class="opacitymedium">'.$langs->trans("None").'</span>'
155  );
156 
157  $this->db->free($result);
158  } else {
159  $this->info_box_contents[0][0] = array(
160  'td' => '',
161  'maxlength'=>500,
162  'text' => ($this->db->error().' sql='.$sql)
163  );
164  }
165  } else {
166  $this->info_box_contents[0][0] = array(
167  'td' => 'class="nohover left"',
168  'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
169  );
170  }
171  }
172 
181  public function showBox($head = null, $contents = null, $nooutput = 0)
182  {
183  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
184  }
185 }
Class to manage the box to show last thirdparties.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
Class ModeleBoxes.
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage third parties objects (customers, suppliers, prospects...)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
loadBox($max=5)
Load data for box to show them later.