dolibarr  19.0.0-dev
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'=>((isModEnabled("product") || isModEnabled("service")) && isModEnabled('stock')), '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->hasRight('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 (getDolGlobalInt('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 (!isModEnabled('dynamicprices') || 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  $productstatic->fetch($objp->rowid, '', '', 1);
175 
176  require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
177  $priceparser = new PriceParser($this->db);
178  $price_result = $priceparser->parseProduct($productstatic);
179  if ($price_result >= 0) {
180  if ($objp->price_base_type == 'HT') {
181  $price_base_type = $langs->trans("HT");
182  } else {
183  $price_result = $price_result * (1 + ($productstatic->tva_tx / 100));
184  $price_base_type = $langs->trans("TTC");
185  }
186  $price = price($price_result);
187  }
188  }
189 
190  /*$this->info_box_contents[$line][] = array(
191  'td' => 'class="nowraponall right amount"',
192  'text' => $price.' '.$price_base_type,
193  );*/
194 
195  $this->info_box_contents[$line][] = array(
196  'td' => 'class="center nowraponall"',
197  'text' => price2num($objp->total_stock, 'MS').' / '.$objp->seuil_stock_alerte,
198  'text2'=>img_warning($langs->transnoentitiesnoconv("StockLowerThanLimit", $objp->seuil_stock_alerte)),
199  );
200 
201  $this->info_box_contents[$line][] = array(
202  'td' => 'class="right" width="18"',
203  'text' => '<span class="statusrefsell">'.$productstatic->LibStatut($objp->tosell, 3, 0).'</span>',
204  'asis' => 1
205  );
206 
207  $this->info_box_contents[$line][] = array(
208  'td' => 'class="right" width="18"',
209  'text' => '<span class="statusrefbuy">'.$productstatic->LibStatut($objp->tobuy, 3, 1).'</span>',
210  'asis' => 1
211  );
212 
213  $line++;
214  }
215  if ($num == 0) {
216  $this->info_box_contents[$line][0] = array(
217  'td' => 'class="center"',
218  'text'=>$langs->trans("NoTooLowStockProducts"),
219  );
220  }
221 
222  $this->db->free($result);
223  } else {
224  $this->info_box_contents[0][0] = array(
225  'td' => '',
226  'maxlength'=>500,
227  'text' => ($this->db->error().' sql='.$sql),
228  );
229  }
230  } else {
231  $this->info_box_contents[0][0] = array(
232  'td' => 'class="nohover opacitymedium left"',
233  'text' => $langs->trans("ReadPermissionNotAllowed")
234  );
235  }
236  }
237 
246  public function showBox($head = null, $contents = null, $nooutput = 0)
247  {
248  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
249  }
250 }
Class ModeleBoxes.
Class to parse product price expressions.
Class to manage products or services.
Class to manage the box to show too low stocks products.
loadBox($max=5)
Load data into info_box_contents array to show array later.
__construct($db, $param='')
Constructor.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->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') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
isVisibleToUserType($type_user, &$menuentry, &$listofmodulesforexternal)
Function to test if an entry is enabled or not.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
isModEnabled($module)
Is Dolibarr module enabled.