dolibarr  16.0.5
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-2021 Frédéric France <frederic.france@netlogic.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 <https://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  public $boxcode = "productsalertstock";
39  public $boximg = "object_product";
40  public $boxlabel = "BoxProductsAlertStock";
41  public $depends = array("produit");
42 
46  public $db;
47 
48  public $param;
49 
50  public $info_box_head = array();
51  public $info_box_contents = array();
52 
53 
60  public 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'=>!empty($user->rights->stock->lire), 'module'=>'product|service|stock');
68  $showmode = isVisibleToUserType(($user->socid > 0 ? 1 : 0), $tmpentry, $listofmodulesforexternal);
69  $this->hidden = ($showmode != 1);
70  }
71 
78  public function loadBox($max = 5)
79  {
80  global $user, $langs, $conf, $hookmanager;
81 
82  $this->max = $max;
83 
84  include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
85  $productstatic = new Product($this->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  $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.barcode, p.seuil_stock_alerte, p.entity,";
91  $sql .= " p.accountancy_code_sell, p.accountancy_code_sell_intra, p.accountancy_code_sell_export,";
92  $sql .= " p.accountancy_code_buy, p.accountancy_code_buy_intra, p.accountancy_code_buy_export,";
93  $sql .= " SUM(".$this->db->ifsql("s.reel IS NULL", "0", "s.reel").") as total_stock";
94  $sql .= " FROM ".MAIN_DB_PREFIX."product as p";
95  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_stock as s on p.rowid = s.fk_product";
96  $sql .= ' WHERE p.entity IN ('.getEntity($productstatic->element).')';
97  $sql .= " AND p.seuil_stock_alerte > 0";
98  if (empty($user->rights->produit->lire)) {
99  $sql .= ' AND p.fk_product_type <> 0';
100  }
101  if (empty($user->rights->service->lire)) {
102  $sql .= ' AND p.fk_product_type <> 1';
103  }
104  // Add where from hooks
105  if (is_object($hookmanager)) {
106  $parameters = array('boxproductalertstocklist' => 1, 'boxcode' => $this->boxcode);
107  $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $productstatic); // Note that $action and $object may have been modified by hook
108  $sql .= $hookmanager->resPrint;
109  }
110  $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.barcode, p.seuil_stock_alerte, p.entity,";
111  $sql .= " p.accountancy_code_sell, p.accountancy_code_sell_intra, p.accountancy_code_sell_export,";
112  $sql .= " p.accountancy_code_buy, p.accountancy_code_buy_intra, p.accountancy_code_buy_export";
113  $sql .= " HAVING SUM(".$this->db->ifsql("s.reel IS NULL", "0", "s.reel").") < p.seuil_stock_alerte";
114  $sql .= $this->db->order('p.seuil_stock_alerte', 'DESC');
115  $sql .= $this->db->plimit($max, 0);
116 
117  $result = $this->db->query($sql);
118  if ($result) {
119  $langs->load("stocks");
120  $num = $this->db->num_rows($result);
121  $line = 0;
122  while ($line < $num) {
123  $objp = $this->db->fetch_object($result);
124  $datem = $this->db->jdate($objp->tms);
125  $price = '';
126  $price_base_type = '';
127 
128  // Multilangs
129  if (!empty($conf->global->MAIN_MULTILANGS)) { // si l'option est active
130  $sqld = "SELECT label";
131  $sqld .= " FROM ".MAIN_DB_PREFIX."product_lang";
132  $sqld .= " WHERE fk_product = ".((int) $objp->rowid);
133  $sqld .= " AND lang = '".$this->db->escape($langs->getDefaultLang())."'";
134  $sqld .= " LIMIT 1";
135 
136  $resultd = $this->db->query($sqld);
137  if ($resultd) {
138  $objtp = $this->db->fetch_object($resultd);
139  if (isset($objtp->label) && $objtp->label != '') {
140  $objp->label = $objtp->label;
141  }
142  }
143  }
144  $productstatic->id = $objp->rowid;
145  $productstatic->ref = $objp->ref;
146  $productstatic->type = $objp->fk_product_type;
147  $productstatic->label = $objp->label;
148  $productstatic->entity = $objp->entity;
149  $productstatic->barcode = $objp->barcode;
150  $productstatic->status = $objp->tosell;
151  $productstatic->status_buy = $objp->tobuy;
152  $productstatic->accountancy_code_sell = $objp->accountancy_code_sell;
153  $productstatic->accountancy_code_sell_intra = $objp->accountancy_code_sell_intra;
154  $productstatic->accountancy_code_sell_export = $objp->accountancy_code_sell_export;
155  $productstatic->accountancy_code_buy = $objp->accountancy_code_buy;
156  $productstatic->accountancy_code_buy_intra = $objp->accountancy_code_buy_intra;
157  $productstatic->accountancy_code_buy_export = $objp->accountancy_code_buy_export;
158 
159  $this->info_box_contents[$line][] = array(
160  'td' => '',
161  'text' => $productstatic->getNomUrl(1),
162  'asis' => 1,
163  );
164 
165  $this->info_box_contents[$line][] = array(
166  'td' => 'class="tdoverflowmax100 maxwidth150onsmartphone"',
167  'text' => $objp->label,
168  );
169 
170  if (empty($conf->dynamicprices->enabled) || empty($objp->fk_price_expression)) {
171  $price_base_type = $langs->trans($objp->price_base_type);
172  $price = ($objp->price_base_type == 'HT') ?price($objp->price) : $price = price($objp->price_ttc);
173  } else //Parse the dynamic price
174  {
175  $productstatic->fetch($objp->rowid, '', '', 1);
176  $priceparser = new PriceParser($this->db);
177  $price_result = $priceparser->parseProduct($productstatic);
178  if ($price_result >= 0) {
179  if ($objp->price_base_type == 'HT') {
180  $price_base_type = $langs->trans("HT");
181  } else {
182  $price_result = $price_result * (1 + ($productstatic->tva_tx / 100));
183  $price_base_type = $langs->trans("TTC");
184  }
185  $price = price($price_result);
186  }
187  }
188 
189  /*$this->info_box_contents[$line][] = array(
190  'td' => 'class="nowraponall right amount"',
191  'text' => $price.' '.$price_base_type,
192  );*/
193 
194  $this->info_box_contents[$line][] = array(
195  'td' => 'class="center nowraponall"',
196  'text' => price2num($objp->total_stock, 'MS').' / '.$objp->seuil_stock_alerte,
197  'text2'=>img_warning($langs->transnoentitiesnoconv("StockLowerThanLimit", $objp->seuil_stock_alerte)),
198  );
199 
200  $this->info_box_contents[$line][] = array(
201  'td' => 'class="right" width="18"',
202  'text' => '<span class="statusrefsell">'.$productstatic->LibStatut($objp->tosell, 3, 0).'</span>',
203  'asis' => 1
204  );
205 
206  $this->info_box_contents[$line][] = array(
207  'td' => 'class="right" width="18"',
208  'text' => '<span class="statusrefbuy">'.$productstatic->LibStatut($objp->tobuy, 3, 1).'</span>',
209  'asis' => 1
210  );
211 
212  $line++;
213  }
214  if ($num == 0) {
215  $this->info_box_contents[$line][0] = array(
216  'td' => 'class="center"',
217  'text'=>$langs->trans("NoTooLowStockProducts"),
218  );
219  }
220 
221  $this->db->free($result);
222  } else {
223  $this->info_box_contents[0][0] = array(
224  'td' => '',
225  'maxlength'=>500,
226  'text' => ($this->db->error().' sql='.$sql),
227  );
228  }
229  } else {
230  $this->info_box_contents[0][0] = array(
231  'td' => 'class="nohover opacitymedium left"',
232  'text' => $langs->trans("ReadPermissionNotAllowed")
233  );
234  }
235  }
236 
245  public function showBox($head = null, $contents = null, $nooutput = 0)
246  {
247  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
248  }
249 }
db
$conf db
API class for accounts.
Definition: inc.php:41
box_produits_alerte_stock\__construct
__construct($db, $param='')
Constructor.
Definition: box_produits_alerte_stock.php:60
box_produits_alerte_stock\showBox
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
Definition: box_produits_alerte_stock.php:245
img_warning
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
Definition: functions.lib.php:4521
isVisibleToUserType
isVisibleToUserType($type_user, &$menuentry, &$listofmodulesforexternal)
Function to test if an entry is enabled or not.
Definition: functions.lib.php:10217
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
PriceParser
Class to parse product price expressions.
Definition: price_parser.class.php:33
box_produits_alerte_stock
Class to manage the box to show too low stocks products.
Definition: box_produits_alerte_stock.php:36
Product
Class to manage products or services.
Definition: product.class.php:46
box_produits_alerte_stock\loadBox
loadBox($max=5)
Load data into info_box_contents array to show array later.
Definition: box_produits_alerte_stock.php:78
price
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.
Definition: functions.lib.php:5541
ModeleBoxes
Class ModeleBoxes.
Definition: modules_boxes.php:34