dolibarr 20.0.2
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-2023 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
27include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
28include_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
47 public function __construct($db, $param)
48 {
49 global $user;
50
51 $this->db = $db;
52
53 $listofmodulesforexternal = explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL'));
54 $tmpentry = array('enabled'=>(isModEnabled("product") || isModEnabled("service")), 'perms'=>($user->hasRight('produit', 'lire') || $user->hasRight('service', 'lire')), 'module'=>'product|service');
55 $showmode = isVisibleToUserType(($user->socid > 0 ? 1 : 0), $tmpentry, $listofmodulesforexternal);
56 $this->hidden = ($showmode != 1);
57 }
58
65 public function loadBox($max = 5)
66 {
67 global $user, $langs, $conf, $hookmanager;
68
69 $this->max = $max;
70
71 include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
72 $productstatic = new Product($this->db);
73
74 $this->info_box_head = array(
75 'text' => $langs->trans("BoxTitleLastProducts", $max).'<a class="paddingleft" href="'.DOL_URL_ROOT.'/product/list.php?sortfield=p.tms&sortorder=DESC"><span class="badge">...</span></a>',
76 );
77
78 if ($user->hasRight('produit', 'lire') || $user->hasRight('service', 'lire')) {
79 $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";
80 $sql .= ", p.accountancy_code_sell";
81 $sql .= ", p.accountancy_code_sell_intra";
82 $sql .= ", p.accountancy_code_sell_export";
83 $sql .= ", p.accountancy_code_buy";
84 $sql .= ", p.accountancy_code_buy_intra";
85 $sql .= ", p.accountancy_code_buy_export";
86 $sql .= ', p.barcode';
87 $sql .= " FROM ".MAIN_DB_PREFIX."product as p";
88 $sql .= ' WHERE p.entity IN ('.getEntity($productstatic->element).')';
89 if (!$user->hasRight('produit', 'lire')) {
90 $sql .= ' AND p.fk_product_type != 0';
91 }
92 if (!$user->hasRight('service', 'lire')) {
93 $sql .= ' AND p.fk_product_type != 1';
94 }
95 // Add where from hooks
96 if (is_object($hookmanager)) {
97 $parameters = array('boxproductlist' => 1, 'boxcode' => $this->boxcode);
98 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $productstatic); // Note that $action and $object may have been modified by hook
99 $sql .= $hookmanager->resPrint;
100 }
101 $sql .= $this->db->order('p.datec', 'DESC');
102 $sql .= $this->db->plimit($max, 0);
103
104 $result = $this->db->query($sql);
105 if ($result) {
106 $num = $this->db->num_rows($result);
107 $line = 0;
108 while ($line < $num) {
109 $objp = $this->db->fetch_object($result);
110 $datem = $this->db->jdate($objp->tms);
111
112 // Multilangs
113 if (getDolGlobalInt('MAIN_MULTILANGS')) { // si l'option est active
114 $sqld = "SELECT label";
115 $sqld .= " FROM ".MAIN_DB_PREFIX."product_lang";
116 $sqld .= " WHERE fk_product = ".((int) $objp->rowid);
117 $sqld .= " AND lang = '".$this->db->escape($langs->getDefaultLang())."'";
118 $sqld .= " LIMIT 1";
119
120 $resultd = $this->db->query($sqld);
121 if ($resultd) {
122 $objtp = $this->db->fetch_object($resultd);
123 if (isset($objtp->label) && $objtp->label != '') {
124 $objp->label = $objtp->label;
125 }
126 }
127 }
128 $productstatic->id = $objp->rowid;
129 $productstatic->ref = $objp->ref;
130 $productstatic->type = $objp->fk_product_type;
131 $productstatic->label = $objp->label;
132 $productstatic->entity = $objp->entity;
133 $productstatic->status = $objp->tosell;
134 $productstatic->status_buy = $objp->tobuy;
135 $productstatic->barcode = $objp->barcode;
136 $productstatic->accountancy_code_sell = $objp->accountancy_code_sell;
137 $productstatic->accountancy_code_sell_intra = $objp->accountancy_code_sell_intra;
138 $productstatic->accountancy_code_sell_export = $objp->accountancy_code_sell_export;
139 $productstatic->accountancy_code_buy = $objp->accountancy_code_buy;
140 $productstatic->accountancy_code_buy_intra = $objp->accountancy_code_buy_intra;
141 $productstatic->accountancy_code_buy_export = $objp->accountancy_code_buy_export;
142 $productstatic->date_modification = $datem;
143
144 $usercancreadprice = getDolGlobalString('MAIN_USE_ADVANCED_PERMS') ? $user->hasRight('product', 'product_advance', 'read_prices') : $user->hasRight('product', 'read');
145 if ($productstatic->isService()) {
146 $usercancreadprice = getDolGlobalString('MAIN_USE_ADVANCED_PERMS') ? $user->hasRight('service', 'service_advance', 'read_prices') : $user->hasRight('service', 'read');
147 }
148
149 $this->info_box_contents[$line][] = array(
150 'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
151 'text' => $productstatic->getNomUrl(1),
152 'asis' => 1,
153 );
154
155 $this->info_box_contents[$line][] = array(
156 'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
157 'text' => $objp->label,
158 );
159 $price = '';
160 $price_base_type = '';
161 if ($usercancreadprice) {
162 if (!isModEnabled('dynamicprices') || empty($objp->fk_price_expression)) {
163 $price_base_type = $langs->trans($objp->price_base_type);
164 $price = ($objp->price_base_type == 'HT') ? price($objp->price) : $price = price($objp->price_ttc);
165 } else {
166 //Parse the dynamic price
167 $productstatic->fetch($objp->rowid, '', '', 1);
168
169 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
170 $priceparser = new PriceParser($this->db);
171 $price_result = $priceparser->parseProduct($productstatic);
172 if ($price_result >= 0) {
173 if ($objp->price_base_type == 'HT') {
174 $price_base_type = $langs->trans("HT");
175 } else {
176 $price_result = $price_result * (1 + ($productstatic->tva_tx / 100));
177 $price_base_type = $langs->trans("TTC");
178 }
179 $price = price($price_result);
180 }
181 }
182 }
183 $this->info_box_contents[$line][] = array(
184 'td' => 'class="nowraponall right amount"',
185 'text' => $price,
186 );
187
188 $this->info_box_contents[$line][] = array(
189 'td' => 'class="nowrap"',
190 'text' => $price_base_type,
191 );
192
193 $this->info_box_contents[$line][] = array(
194 'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateModification").': '.dol_print_date($datem, 'dayhour', 'tzuserrel')).'"',
195 'text' => dol_print_date($datem, 'day', 'tzuserrel'),
196 );
197
198 $this->info_box_contents[$line][] = array(
199 'td' => 'class="right" width="18"',
200 'text' => '<span class="statusrefsell">'.$productstatic->LibStatut($objp->tosell, 3, 0).'</span>',
201 'asis' => 1
202 );
203
204 $this->info_box_contents[$line][] = array(
205 'td' => 'class="right" width="18"',
206 'text' => '<span class="statusrefbuy">'.$productstatic->LibStatut($objp->tobuy, 3, 1).'</span>',
207 'asis' => 1
208 );
209
210 $line++;
211 }
212 if ($num == 0) {
213 $this->info_box_contents[$line][0] = array(
214 'td' => 'class="center"',
215 'text'=>$langs->trans("NoRecordedProducts"),
216 );
217 }
218
219 $this->db->free($result);
220 } else {
221 $this->info_box_contents[0][0] = array(
222 'td' => '',
223 'maxlength'=>500,
224 'text' => ($this->db->error().' sql='.$sql),
225 );
226 }
227 } else {
228 $this->info_box_contents[0][0] = array(
229 'td' => 'class="nohover left"',
230 'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
231 );
232 }
233 }
234
243 public function showBox($head = null, $contents = null, $nooutput = 0)
244 {
245 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
246 }
247}
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.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...