dolibarr  7.0.0-beta
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@capnetworks.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 
43  var $db;
44  var $param;
45 
46  var $info_box_head = array();
47  var $info_box_contents = array();
48 
49 
56  function __construct($db,$param='')
57  {
58  global $conf,$user;
59 
60  $this->db = $db;
61 
62  $listofmodulesforexternal=explode(',',$conf->global->MAIN_MODULES_FOR_EXTERNAL);
63  $tmpentry=array('enabled'=>((! empty($conf->product->enabled) || ! empty($conf->service->enabled)) && ! empty($conf->stock->enabled)), 'perms'=>($user->rights->stock->lire), 'module'=>'product|service|stock');
64  $showmode=isVisibleToUserType(($user->societe_id > 0 ? 1 : 0), $tmpentry, $listofmodulesforexternal);
65  $this->hidden=($showmode != 1);
66  }
67 
74  function loadBox($max=5)
75  {
76  global $user, $langs, $db, $conf, $hookmanager;
77 
78  $this->max=$max;
79 
80  include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
81  $productstatic=new Product($db);
82 
83  $this->info_box_head = array('text' => $langs->trans("BoxTitleProductsAlertStock",$max));
84 
85  if (($user->rights->produit->lire || $user->rights->service->lire) && $user->rights->stock->lire)
86  {
87  $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,";
88  $sql.= " SUM(".$db->ifsql("s.reel IS NULL","0","s.reel").") as total_stock";
89  $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
90  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_stock as s on p.rowid = s.fk_product";
91  $sql.= ' WHERE p.entity IN ('.getEntity($productstatic->element, 1).')';
92  $sql.= " AND p.tosell = 1 AND p.seuil_stock_alerte > 0";
93  if (empty($user->rights->produit->lire)) $sql.=' AND p.fk_product_type != 0';
94  if (empty($user->rights->service->lire)) $sql.=' AND p.fk_product_type != 1';
95  // Add where from hooks
96  if (is_object($hookmanager))
97  {
98  $parameters=array('boxproductalertstocklist'=>1);
99  $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
100  $sql.=$hookmanager->resPrint;
101  }
102  $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";
103  $sql.= " HAVING SUM(".$db->ifsql("s.reel IS NULL","0","s.reel").") < p.seuil_stock_alerte";
104  $sql.= $db->order('p.seuil_stock_alerte', 'DESC');
105  $sql.= $db->plimit($max, 0);
106 
107  $result = $db->query($sql);
108  if ($result)
109  {
110  $langs->load("stocks");
111  $num = $db->num_rows($result);
112  $line = 0;
113  while ($line < $num) {
114  $objp = $db->fetch_object($result);
115  $datem=$db->jdate($objp->tms);
116 
117  // Multilangs
118  if (! empty($conf->global->MAIN_MULTILANGS)) // si l'option est active
119  {
120  $sqld = "SELECT label";
121  $sqld.= " FROM ".MAIN_DB_PREFIX."product_lang";
122  $sqld.= " WHERE fk_product=".$objp->rowid;
123  $sqld.= " AND lang='". $langs->getDefaultLang() ."'";
124  $sqld.= " LIMIT 1";
125 
126  $resultd = $db->query($sqld);
127  if ($resultd)
128  {
129  $objtp = $db->fetch_object($resultd);
130  if (isset($objtp->label) && $objtp->label != '')
131  $objp->label = $objtp->label;
132  }
133  }
134  $productstatic->id = $objp->rowid;
135  $productstatic->ref = $objp->ref;
136  $productstatic->type = $objp->fk_product_type;
137  $productstatic->label = $objp->label;
138  $productstatic->entity = $objp->entity;
139 
140  $this->info_box_contents[$line][] = array(
141  'td' => '',
142  'text' => $productstatic->getNomUrl(1),
143  'asis' => 1,
144  );
145 
146  $this->info_box_contents[$line][] = array(
147  'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
148  'text' => $objp->label,
149  );
150 
151  if (empty($conf->dynamicprices->enabled) || empty($objp->fk_price_expression))
152  {
153  $price_base_type=$langs->trans($objp->price_base_type);
154  $price=($objp->price_base_type == 'HT')?price($objp->price):$price=price($objp->price_ttc);
155  }
156  else //Parse the dynamic price
157  {
158  $productstatic->fetch($objp->rowid, '', '', 1);
159  $priceparser = new PriceParser($this->db);
160  $price_result = $priceparser->parseProduct($productstatic);
161  if ($price_result >= 0) {
162  if ($objp->price_base_type == 'HT')
163  {
164  $price_base_type=$langs->trans("HT");
165  }
166  else
167  {
168  $price_result = $price_result * (1 + ($productstatic->tva_tx / 100));
169  $price_base_type=$langs->trans("TTC");
170  }
171  $price=price($price_result);
172  }
173  }
174 
175  $this->info_box_contents[$line][] = array(
176  'td' => 'class="right"',
177  'text' => $price,
178  );
179 
180  $this->info_box_contents[$line][] = array(
181  'td' => 'class="nowrap"',
182  'text' => $price_base_type,
183  );
184 
185  $this->info_box_contents[$line][] = array(
186  'td' => 'align="center"',
187  'text' => $objp->total_stock . ' / '.$objp->seuil_stock_alerte,
188  'text2'=>img_warning($langs->transnoentitiesnoconv("StockLowerThanLimit", $objp->seuil_stock_alerte)));
189 
190  $this->info_box_contents[$line][] = array(
191  'td' => 'align="right" width="18"',
192  'text' => '<span class="statusrefsell">'.$productstatic->LibStatut($objp->tosell,3,0).'<span>',
193  'asis' => 1
194  );
195 
196  $this->info_box_contents[$line][] = array(
197  'td' => 'align="right" width="18"',
198  'text' => '<span class="statusrefbuy">'.$productstatic->LibStatut($objp->tobuy,3,0).'<span>',
199  'asis' => 1
200  );
201 
202  $line++;
203  }
204  if ($num==0)
205  $this->info_box_contents[$line][0] = array(
206  'td' => 'align="center"',
207  'text'=>$langs->trans("NoTooLowStockProducts"),
208  );
209 
210  $db->free($result);
211  }
212  else
213  {
214  $this->info_box_contents[0][0] = array(
215  'td' => '',
216  'maxlength'=>500,
217  'text' => ($db->error().' sql='.$sql),
218  );
219  }
220  }
221  else {
222  $this->info_box_contents[0][0] = array(
223  'td' => 'align="left" class="nohover opacitymedium"',
224  'text' => $langs->trans("ReadPermissionNotAllowed")
225  );
226  }
227  }
228 
237  function showBox($head = null, $contents = null, $nooutput=0)
238  {
239  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
240  }
241 
242 }
243 
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.
img_warning($titlealt= 'default', $moreatt= '')
Show warning logo.
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.