dolibarr  9.0.0
box_produits_alerte_stock.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2005-2012 Maxime Kohlhaas <mko@atm-consulting.fr>
6  * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
7  * Copyright (C) 2015 Juanjo Menent <jmenent@2byte.es>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
29 include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
30 include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
31 
32 
37 {
38  var $boxcode="productsalertstock";
39  var $boximg="object_product";
40  var $boxlabel="BoxProductsAlertStock";
41  var $depends = array("produit");
42 
46  public $db;
47 
48  var $param;
49 
50  var $info_box_head = array();
51  var $info_box_contents = array();
52 
53 
60  function __construct($db,$param='')
61  {
62  global $conf,$user;
63 
64  $this->db = $db;
65 
66  $listofmodulesforexternal=explode(',',$conf->global->MAIN_MODULES_FOR_EXTERNAL);
67  $tmpentry=array('enabled'=>((! empty($conf->product->enabled) || ! empty($conf->service->enabled)) && ! empty($conf->stock->enabled)), 'perms'=>($user->rights->stock->lire), 'module'=>'product|service|stock');
68  $showmode=isVisibleToUserType(($user->societe_id > 0 ? 1 : 0), $tmpentry, $listofmodulesforexternal);
69  $this->hidden=($showmode != 1);
70  }
71 
78  function loadBox($max=5)
79  {
80  global $user, $langs, $db, $conf, $hookmanager;
81 
82  $this->max=$max;
83 
84  include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
85  $productstatic=new Product($db);
86 
87  $this->info_box_head = array('text' => $langs->trans("BoxTitleProductsAlertStock",$max));
88 
89  if (($user->rights->produit->lire || $user->rights->service->lire) && $user->rights->stock->lire)
90  {
91  $sql = "SELECT p.rowid, p.label, p.price, p.ref, p.price_base_type, p.price_ttc, p.fk_product_type, p.tms, p.tosell, p.tobuy, p.seuil_stock_alerte, p.entity,";
92  $sql.= " SUM(".$db->ifsql("s.reel IS NULL","0","s.reel").") as total_stock";
93  $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
94  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_stock as s on p.rowid = s.fk_product";
95  $sql.= ' WHERE p.entity IN ('.getEntity($productstatic->element).')';
96  $sql.= " AND p.tosell = 1 AND p.seuil_stock_alerte > 0";
97  if (empty($user->rights->produit->lire)) $sql.=' AND p.fk_product_type != 0';
98  if (empty($user->rights->service->lire)) $sql.=' AND p.fk_product_type != 1';
99  // Add where from hooks
100  if (is_object($hookmanager))
101  {
102  $parameters=array('boxproductalertstocklist'=>1);
103  $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
104  $sql.=$hookmanager->resPrint;
105  }
106  $sql.= " GROUP BY p.rowid, p.ref, p.label, p.price, p.price_base_type, p.price_ttc, p.fk_product_type, p.tms, p.tosell, p.tobuy, p.seuil_stock_alerte, p.entity";
107  $sql.= " HAVING SUM(".$db->ifsql("s.reel IS NULL","0","s.reel").") < p.seuil_stock_alerte";
108  $sql.= $db->order('p.seuil_stock_alerte', 'DESC');
109  $sql.= $db->plimit($max, 0);
110 
111  $result = $db->query($sql);
112  if ($result)
113  {
114  $langs->load("stocks");
115  $num = $db->num_rows($result);
116  $line = 0;
117  while ($line < $num) {
118  $objp = $db->fetch_object($result);
119  $datem=$db->jdate($objp->tms);
120 
121  // Multilangs
122  if (! empty($conf->global->MAIN_MULTILANGS)) // si l'option est active
123  {
124  $sqld = "SELECT label";
125  $sqld.= " FROM ".MAIN_DB_PREFIX."product_lang";
126  $sqld.= " WHERE fk_product=".$objp->rowid;
127  $sqld.= " AND lang='". $langs->getDefaultLang() ."'";
128  $sqld.= " LIMIT 1";
129 
130  $resultd = $db->query($sqld);
131  if ($resultd)
132  {
133  $objtp = $db->fetch_object($resultd);
134  if (isset($objtp->label) && $objtp->label != '')
135  $objp->label = $objtp->label;
136  }
137  }
138  $productstatic->id = $objp->rowid;
139  $productstatic->ref = $objp->ref;
140  $productstatic->type = $objp->fk_product_type;
141  $productstatic->label = $objp->label;
142  $productstatic->entity = $objp->entity;
143 
144  $this->info_box_contents[$line][] = array(
145  'td' => '',
146  'text' => $productstatic->getNomUrl(1),
147  'asis' => 1,
148  );
149 
150  $this->info_box_contents[$line][] = array(
151  'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
152  'text' => $objp->label,
153  );
154 
155  if (empty($conf->dynamicprices->enabled) || empty($objp->fk_price_expression))
156  {
157  $price_base_type=$langs->trans($objp->price_base_type);
158  $price=($objp->price_base_type == 'HT')?price($objp->price):$price=price($objp->price_ttc);
159  }
160  else //Parse the dynamic price
161  {
162  $productstatic->fetch($objp->rowid, '', '', 1);
163  $priceparser = new PriceParser($this->db);
164  $price_result = $priceparser->parseProduct($productstatic);
165  if ($price_result >= 0) {
166  if ($objp->price_base_type == 'HT')
167  {
168  $price_base_type=$langs->trans("HT");
169  }
170  else
171  {
172  $price_result = $price_result * (1 + ($productstatic->tva_tx / 100));
173  $price_base_type=$langs->trans("TTC");
174  }
175  $price=price($price_result);
176  }
177  }
178 
179  $this->info_box_contents[$line][] = array(
180  'td' => 'class="right"',
181  'text' => $price,
182  );
183 
184  $this->info_box_contents[$line][] = array(
185  'td' => 'class="nowrap"',
186  'text' => $price_base_type,
187  );
188 
189  $this->info_box_contents[$line][] = array(
190  'td' => 'align="center"',
191  'text' => $objp->total_stock . ' / '.$objp->seuil_stock_alerte,
192  'text2'=>img_warning($langs->transnoentitiesnoconv("StockLowerThanLimit", $objp->seuil_stock_alerte)));
193 
194  $this->info_box_contents[$line][] = array(
195  'td' => 'align="right" width="18"',
196  'text' => '<span class="statusrefsell">'.$productstatic->LibStatut($objp->tosell,3,0).'<span>',
197  'asis' => 1
198  );
199 
200  $this->info_box_contents[$line][] = array(
201  'td' => 'align="right" width="18"',
202  'text' => '<span class="statusrefbuy">'.$productstatic->LibStatut($objp->tobuy,3,0).'<span>',
203  'asis' => 1
204  );
205 
206  $line++;
207  }
208  if ($num==0)
209  $this->info_box_contents[$line][0] = array(
210  'td' => 'align="center"',
211  'text'=>$langs->trans("NoTooLowStockProducts"),
212  );
213 
214  $db->free($result);
215  }
216  else
217  {
218  $this->info_box_contents[0][0] = array(
219  'td' => '',
220  'maxlength'=>500,
221  'text' => ($db->error().' sql='.$sql),
222  );
223  }
224  }
225  else {
226  $this->info_box_contents[0][0] = array(
227  'td' => 'align="left" class="nohover opacitymedium"',
228  'text' => $langs->trans("ReadPermissionNotAllowed")
229  );
230  }
231  }
232 
241  function showBox($head = null, $contents = null, $nooutput=0)
242  {
243  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
244  }
245 }
246 
img_warning($titlealt='default', $moreatt='')
Show warning logo.
Class to parse product price expressions.
Class to manage products or services.
loadBox($max=5)
Load data into info_box_contents array to show array later.
Class ModeleBoxes.
isVisibleToUserType($type_user, &$menuentry, &$listofmodulesforexternal)
Function to test if an entry is enabled or not.
__construct($db, $param='')
Constructor.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
Class to manage the box to show too low stocks products.