dolibarr  20.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@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 
49  public function __construct($db, $param = '')
50  {
51  global $conf, $user;
52 
53  $this->db = $db;
54 
55  $listofmodulesforexternal = explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL'));
56  $tmpentry = array('enabled'=>((isModEnabled("product") || isModEnabled("service")) && isModEnabled('stock')), 'perms'=>$user->hasRight('stock', 'lire'), 'module'=>'product|service|stock');
57  $showmode = isVisibleToUserType(($user->socid > 0 ? 1 : 0), $tmpentry, $listofmodulesforexternal);
58  $this->hidden = ($showmode != 1);
59  }
60 
67  public function loadBox($max = 5)
68  {
69  global $user, $langs, $conf, $hookmanager;
70 
71  $this->max = $max;
72 
73  include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
74  $productstatic = new Product($this->db);
75 
76  $this->info_box_head = array('text' => $langs->trans("BoxTitleProductsAlertStock", $max));
77 
78  if (($user->hasRight('produit', 'lire') || $user->hasRight('service', 'lire')) && $user->hasRight('stock', 'lire')) {
79  $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,";
80  $sql .= " p.accountancy_code_sell, p.accountancy_code_sell_intra, p.accountancy_code_sell_export,";
81  $sql .= " p.accountancy_code_buy, p.accountancy_code_buy_intra, p.accountancy_code_buy_export,";
82  $sql .= " SUM(".$this->db->ifsql("s.reel IS NULL", "0", "s.reel").") as total_stock";
83  $sql .= " FROM ".MAIN_DB_PREFIX."product as p";
84  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_stock as s on p.rowid = s.fk_product";
85  $sql .= ' WHERE p.entity IN ('.getEntity($productstatic->element).')';
86  $sql .= " AND p.seuil_stock_alerte > 0";
87  if (!$user->hasRight('produit', 'lire')) {
88  $sql .= ' AND p.fk_product_type <> 0';
89  }
90  if (!$user->hasRight('service', 'lire')) {
91  $sql .= ' AND p.fk_product_type <> 1';
92  }
93  // Add where from hooks
94  if (is_object($hookmanager)) {
95  $parameters = array('boxproductalertstocklist' => 1, 'boxcode' => $this->boxcode);
96  $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $productstatic); // Note that $action and $object may have been modified by hook
97  $sql .= $hookmanager->resPrint;
98  }
99  $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,";
100  $sql .= " p.accountancy_code_sell, p.accountancy_code_sell_intra, p.accountancy_code_sell_export,";
101  $sql .= " p.accountancy_code_buy, p.accountancy_code_buy_intra, p.accountancy_code_buy_export";
102  $sql .= " HAVING SUM(".$this->db->ifsql("s.reel IS NULL", "0", "s.reel").") < p.seuil_stock_alerte";
103  $sql .= $this->db->order('p.seuil_stock_alerte', 'DESC');
104  $sql .= $this->db->plimit($max, 0);
105 
106  $result = $this->db->query($sql);
107  if ($result) {
108  $langs->load("stocks");
109  $num = $this->db->num_rows($result);
110  $line = 0;
111  while ($line < $num) {
112  $objp = $this->db->fetch_object($result);
113  $datem = $this->db->jdate($objp->tms);
114  $price = '';
115  $price_base_type = '';
116 
117  // Multilangs
118  if (getDolGlobalInt('MAIN_MULTILANGS')) { // si l'option est active
119  $sqld = "SELECT label";
120  $sqld .= " FROM ".MAIN_DB_PREFIX."product_lang";
121  $sqld .= " WHERE fk_product = ".((int) $objp->rowid);
122  $sqld .= " AND lang = '".$this->db->escape($langs->getDefaultLang())."'";
123  $sqld .= " LIMIT 1";
124 
125  $resultd = $this->db->query($sqld);
126  if ($resultd) {
127  $objtp = $this->db->fetch_object($resultd);
128  if (isset($objtp->label) && $objtp->label != '') {
129  $objp->label = $objtp->label;
130  }
131  }
132  }
133  $productstatic->id = $objp->rowid;
134  $productstatic->ref = $objp->ref;
135  $productstatic->type = $objp->fk_product_type;
136  $productstatic->label = $objp->label;
137  $productstatic->entity = $objp->entity;
138  $productstatic->barcode = $objp->barcode;
139  $productstatic->status = $objp->tosell;
140  $productstatic->status_buy = $objp->tobuy;
141  $productstatic->accountancy_code_sell = $objp->accountancy_code_sell;
142  $productstatic->accountancy_code_sell_intra = $objp->accountancy_code_sell_intra;
143  $productstatic->accountancy_code_sell_export = $objp->accountancy_code_sell_export;
144  $productstatic->accountancy_code_buy = $objp->accountancy_code_buy;
145  $productstatic->accountancy_code_buy_intra = $objp->accountancy_code_buy_intra;
146  $productstatic->accountancy_code_buy_export = $objp->accountancy_code_buy_export;
147 
148  $this->info_box_contents[$line][] = array(
149  'td' => '',
150  'text' => $productstatic->getNomUrl(1),
151  'asis' => 1,
152  );
153 
154  $this->info_box_contents[$line][] = array(
155  'td' => 'class="tdoverflowmax100 maxwidth150onsmartphone"',
156  'text' => $objp->label,
157  );
158 
159  if (!isModEnabled('dynamicprices') || empty($objp->fk_price_expression)) {
160  $price_base_type = $langs->trans($objp->price_base_type);
161  $price = ($objp->price_base_type == 'HT') ? price($objp->price) : $price = price($objp->price_ttc);
162  } else { //Parse the dynamic price
163  $productstatic->fetch($objp->rowid, '', '', 1);
164 
165  require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
166  $priceparser = new PriceParser($this->db);
167  $price_result = $priceparser->parseProduct($productstatic);
168  if ($price_result >= 0) {
169  if ($objp->price_base_type == 'HT') {
170  $price_base_type = $langs->trans("HT");
171  } else {
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="nowraponall right amount"',
181  'text' => $price.' '.$price_base_type,
182  );*/
183 
184  $this->info_box_contents[$line][] = array(
185  'td' => 'class="center nowraponall"',
186  'text' => price2num($objp->total_stock, 'MS').' / '.$objp->seuil_stock_alerte,
187  'text2'=>img_warning($langs->transnoentitiesnoconv("StockLowerThanLimit", $objp->seuil_stock_alerte)),
188  );
189 
190  $this->info_box_contents[$line][] = array(
191  'td' => 'class="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' => 'class="right" width="18"',
198  'text' => '<span class="statusrefbuy">'.$productstatic->LibStatut($objp->tobuy, 3, 1).'</span>',
199  'asis' => 1
200  );
201 
202  $line++;
203  }
204  if ($num == 0) {
205  $this->info_box_contents[$line][0] = array(
206  'td' => 'class="center"',
207  'text'=>$langs->trans("NoTooLowStockProducts"),
208  );
209  }
210 
211  $this->db->free($result);
212  } else {
213  $this->info_box_contents[0][0] = array(
214  'td' => '',
215  'maxlength'=>500,
216  'text' => ($this->db->error().' sql='.$sql),
217  );
218  }
219  } else {
220  $this->info_box_contents[0][0] = array(
221  'td' => 'class="nohover left"',
222  'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
223  );
224  }
225  }
226 
235  public function showBox($head = null, $contents = null, $nooutput = 0)
236  {
237  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
238  }
239 }
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('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('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') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
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 a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.