dolibarr  17.0.4
box_produits.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) 2015-2021 Frederic France <frederic.france@netlogic.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
28 include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
29 
30 
35 {
36  public $boxcode = "lastproducts";
37  public $boximg = "object_product";
38  public $boxlabel = "BoxLastProducts";
39  public $depends = array("produit");
40 
44  public $db;
45 
46  public $param;
47 
48  public $info_box_head = array();
49  public $info_box_contents = array();
50 
51 
58  public function __construct($db, $param)
59  {
60  global $conf, $user;
61 
62  $this->db = $db;
63 
64  $listofmodulesforexternal = explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL);
65  $tmpentry = array('enabled'=>(isModEnabled("product") || isModEnabled("service")), 'perms'=>(!empty($user->rights->produit->lire) || !empty($user->rights->service->lire)), 'module'=>'product|service');
66  $showmode = isVisibleToUserType(($user->socid > 0 ? 1 : 0), $tmpentry, $listofmodulesforexternal);
67  $this->hidden = ($showmode != 1);
68  }
69 
76  public function loadBox($max = 5)
77  {
78  global $user, $langs, $conf, $hookmanager;
79 
80  $this->max = $max;
81 
82  include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
83  $productstatic = new Product($this->db);
84 
85  $this->info_box_head = array('text' => $langs->trans("BoxTitleLastProducts", $max));
86 
87  if ($user->rights->produit->lire || $user->rights->service->lire) {
88  $sql = "SELECT p.rowid, p.label, p.ref, p.price, p.price_base_type, p.price_ttc, p.fk_product_type, p.tms, p.tosell, p.tobuy, p.fk_price_expression, p.entity";
89  $sql .= ", p.accountancy_code_sell";
90  $sql .= ", p.accountancy_code_sell_intra";
91  $sql .= ", p.accountancy_code_sell_export";
92  $sql .= ", p.accountancy_code_buy";
93  $sql .= ", p.accountancy_code_buy_intra";
94  $sql .= ", p.accountancy_code_buy_export";
95  $sql .= ', p.barcode';
96  $sql .= " FROM ".MAIN_DB_PREFIX."product as p";
97  $sql .= ' WHERE p.entity IN ('.getEntity($productstatic->element).')';
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('boxproductlist' => 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 .= $this->db->order('p.datec', 'DESC');
111  $sql .= $this->db->plimit($max, 0);
112 
113  $result = $this->db->query($sql);
114  if ($result) {
115  $num = $this->db->num_rows($result);
116  $line = 0;
117  while ($line < $num) {
118  $objp = $this->db->fetch_object($result);
119  $datem = $this->db->jdate($objp->tms);
120 
121  // Multilangs
122  if (getDolGlobalInt('MAIN_MULTILANGS')) { // si l'option est active
123  $sqld = "SELECT label";
124  $sqld .= " FROM ".MAIN_DB_PREFIX."product_lang";
125  $sqld .= " WHERE fk_product = ".((int) $objp->rowid);
126  $sqld .= " AND lang = '".$this->db->escape($langs->getDefaultLang())."'";
127  $sqld .= " LIMIT 1";
128 
129  $resultd = $this->db->query($sqld);
130  if ($resultd) {
131  $objtp = $this->db->fetch_object($resultd);
132  if (isset($objtp->label) && $objtp->label != '') {
133  $objp->label = $objtp->label;
134  }
135  }
136  }
137  $productstatic->id = $objp->rowid;
138  $productstatic->ref = $objp->ref;
139  $productstatic->type = $objp->fk_product_type;
140  $productstatic->label = $objp->label;
141  $productstatic->entity = $objp->entity;
142  $productstatic->status = $objp->tosell;
143  $productstatic->status_buy = $objp->tobuy;
144  $productstatic->barcode = $objp->barcode;
145  $productstatic->accountancy_code_sell = $objp->accountancy_code_sell;
146  $productstatic->accountancy_code_sell_intra = $objp->accountancy_code_sell_intra;
147  $productstatic->accountancy_code_sell_export = $objp->accountancy_code_sell_export;
148  $productstatic->accountancy_code_buy = $objp->accountancy_code_buy;
149  $productstatic->accountancy_code_buy_intra = $objp->accountancy_code_buy_intra;
150  $productstatic->accountancy_code_buy_export = $objp->accountancy_code_buy_export;
151  $productstatic->date_modification = $datem;
152 
153  $usercancreadprice = getDolGlobalString('MAIN_USE_ADVANCED_PERMS')?$user->hasRight('product', 'product_advance', 'read_prices'):$user->hasRight('product', 'read');
154  if ($productstatic->isService()) {
155  $usercancreadprice = getDolGlobalString('MAIN_USE_ADVANCED_PERMS')?$user->hasRight('service', 'service_advance', 'read_prices'):$user->hasRight('service', 'read');
156  }
157 
158  $this->info_box_contents[$line][] = array(
159  'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
160  'text' => $productstatic->getNomUrl(1),
161  'asis' => 1,
162  );
163 
164  $this->info_box_contents[$line][] = array(
165  'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
166  'text' => $objp->label,
167  );
168  $price = '';
169  $price_base_type = '';
170  if ($usercancreadprice) {
171  if (!isModEnabled('dynamicprices') || empty($objp->fk_price_expression)) {
172  $price_base_type = $langs->trans($objp->price_base_type);
173  $price = ($objp->price_base_type == 'HT') ?price($objp->price) : $price = price($objp->price_ttc);
174  } else {
175  //Parse the dynamic price
176  $productstatic->fetch($objp->rowid, '', '', 1);
177 
178  require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
179  $priceparser = new PriceParser($this->db);
180  $price_result = $priceparser->parseProduct($productstatic);
181  if ($price_result >= 0) {
182  if ($objp->price_base_type == 'HT') {
183  $price_base_type = $langs->trans("HT");
184  } else {
185  $price_result = $price_result * (1 + ($productstatic->tva_tx / 100));
186  $price_base_type = $langs->trans("TTC");
187  }
188  $price = price($price_result);
189  }
190  }
191  }
192  $this->info_box_contents[$line][] = array(
193  'td' => 'class="nowraponall right amount"',
194  'text' => $price,
195  );
196 
197  $this->info_box_contents[$line][] = array(
198  'td' => 'class="nowrap"',
199  'text' => $price_base_type,
200  );
201 
202  $this->info_box_contents[$line][] = array(
203  'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateModification").': '.dol_print_date($datem, 'dayhour', 'tzuserrel')).'"',
204  'text' => dol_print_date($datem, 'day', 'tzuserrel'),
205  );
206 
207  $this->info_box_contents[$line][] = array(
208  'td' => 'class="right" width="18"',
209  'text' => '<span class="statusrefsell">'.$productstatic->LibStatut($objp->tosell, 3, 0).'</span>',
210  'asis' => 1
211  );
212 
213  $this->info_box_contents[$line][] = array(
214  'td' => 'class="right" width="18"',
215  'text' => '<span class="statusrefbuy">'.$productstatic->LibStatut($objp->tobuy, 3, 1).'</span>',
216  'asis' => 1
217  );
218 
219  $line++;
220  }
221  if ($num == 0) {
222  $this->info_box_contents[$line][0] = array(
223  'td' => 'class="center"',
224  'text'=>$langs->trans("NoRecordedProducts"),
225  );
226  }
227 
228  $this->db->free($result);
229  } else {
230  $this->info_box_contents[0][0] = array(
231  'td' => '',
232  'maxlength'=>500,
233  'text' => ($this->db->error().' sql='.$sql),
234  );
235  }
236  } else {
237  $this->info_box_contents[0][0] = array(
238  'td' => 'class="nohover opacitymedium left"',
239  'text' => $langs->trans("ReadPermissionNotAllowed")
240  );
241  }
242  }
243 
252  public function showBox($head = null, $contents = null, $nooutput = 0)
253  {
254  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
255  }
256 }
Class ModeleBoxes.
Class to parse product price expressions.
Class to manage products or services.
Class to manage the box to show last products.
__construct($db, $param)
Constructor.
loadBox($max=5)
Load data into info_box_contents array to show array later.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
isVisibleToUserType($type_user, &$menuentry, &$listofmodulesforexternal)
Function to test if an entry is enabled or not.
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.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
$conf db
API class for accounts.
Definition: inc.php:41