dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
29include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
30
31
36{
37 public $boxcode = "lastproducts";
38 public $boximg = "object_product";
39 public $boxlabel = "BoxLastProducts";
40 public $depends = array("produit");
41
48 public function __construct($db, $param)
49 {
50 global $user;
51
52 $this->db = $db;
53
54 $listofmodulesforexternal = explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL'));
55 $tmpentry = array('enabled' => (isModEnabled("product") || isModEnabled("service")), 'perms' => ($user->hasRight('produit', 'lire') || $user->hasRight('service', 'lire')), 'module' => 'product|service');
56 $showmode = isVisibleToUserType(($user->socid > 0 ? 1 : 0), $tmpentry, $listofmodulesforexternal);
57 $this->hidden = ($showmode != 1);
58 }
59
66 public function loadBox($max = 5)
67 {
68 global $user, $langs, $conf, $hookmanager;
69
70 $this->max = $max;
71
72 include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
73 $productstatic = new Product($this->db);
74
75 $this->info_box_head = array(
76 '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>',
77 );
78
79 if ($user->hasRight('produit', 'lire') || $user->hasRight('service', 'lire')) {
80 $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";
81 $sql .= ", p.accountancy_code_sell";
82 $sql .= ", p.accountancy_code_sell_intra";
83 $sql .= ", p.accountancy_code_sell_export";
84 $sql .= ", p.accountancy_code_buy";
85 $sql .= ", p.accountancy_code_buy_intra";
86 $sql .= ", p.accountancy_code_buy_export";
87 $sql .= ', p.barcode';
88 $sql .= " FROM ".MAIN_DB_PREFIX."product as p";
89 $sql .= ' WHERE p.entity IN ('.getEntity($productstatic->element).')';
90 if (!$user->hasRight('produit', 'lire')) {
91 $sql .= ' AND p.fk_product_type != 0';
92 }
93 if (!$user->hasRight('service', 'lire')) {
94 $sql .= ' AND p.fk_product_type != 1';
95 }
96 // Add where from hooks
97 if (is_object($hookmanager)) {
98 $parameters = array('boxproductlist' => 1, 'boxcode' => $this->boxcode);
99 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $productstatic); // Note that $action and $object may have been modified by hook
100 $sql .= $hookmanager->resPrint;
101 }
102 $sql .= $this->db->order('p.datec', 'DESC');
103 $sql .= $this->db->plimit($max, 0);
104
105 $result = $this->db->query($sql);
106 if ($result) {
107 $num = $this->db->num_rows($result);
108 $line = 0;
109 while ($line < $num) {
110 $objp = $this->db->fetch_object($result);
111 $datem = $this->db->jdate($objp->tms);
112
113 // Multilangs
114 if (getDolGlobalInt('MAIN_MULTILANGS')) { // si l'option est active
115 $sqld = "SELECT label";
116 $sqld .= " FROM ".MAIN_DB_PREFIX."product_lang";
117 $sqld .= " WHERE fk_product = ".((int) $objp->rowid);
118 $sqld .= " AND lang = '".$this->db->escape($langs->getDefaultLang())."'";
119 $sqld .= " LIMIT 1";
120
121 $resultd = $this->db->query($sqld);
122 if ($resultd) {
123 $objtp = $this->db->fetch_object($resultd);
124 if (isset($objtp->label) && $objtp->label != '') {
125 $objp->label = $objtp->label;
126 }
127 }
128 }
129 $productstatic->id = $objp->rowid;
130 $productstatic->ref = $objp->ref;
131 $productstatic->type = $objp->fk_product_type;
132 $productstatic->label = $objp->label;
133 $productstatic->entity = $objp->entity;
134 $productstatic->status = $objp->tosell;
135 $productstatic->status_buy = $objp->tobuy;
136 $productstatic->barcode = $objp->barcode;
137 $productstatic->accountancy_code_sell = $objp->accountancy_code_sell;
138 $productstatic->accountancy_code_sell_intra = $objp->accountancy_code_sell_intra;
139 $productstatic->accountancy_code_sell_export = $objp->accountancy_code_sell_export;
140 $productstatic->accountancy_code_buy = $objp->accountancy_code_buy;
141 $productstatic->accountancy_code_buy_intra = $objp->accountancy_code_buy_intra;
142 $productstatic->accountancy_code_buy_export = $objp->accountancy_code_buy_export;
143 $productstatic->date_modification = $datem;
144
145 $usercancreadprice = getDolGlobalString('MAIN_USE_ADVANCED_PERMS') ? $user->hasRight('product', 'product_advance', 'read_prices') : $user->hasRight('product', 'read');
146 if ($productstatic->isService()) {
147 $usercancreadprice = getDolGlobalString('MAIN_USE_ADVANCED_PERMS') ? $user->hasRight('service', 'service_advance', 'read_prices') : $user->hasRight('service', 'read');
148 }
149
150 $this->info_box_contents[$line][] = array(
151 'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
152 'text' => $productstatic->getNomUrl(1),
153 'asis' => 1,
154 );
155
156 $this->info_box_contents[$line][] = array(
157 'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
158 'text' => $objp->label,
159 );
160 $price = '';
161 $price_base_type = '';
162 if ($usercancreadprice) {
163 if (!isModEnabled('dynamicprices') || empty($objp->fk_price_expression)) {
164 $price_base_type = $langs->trans($objp->price_base_type);
165 $price = ($objp->price_base_type == 'HT') ? price($objp->price) : $price = price($objp->price_ttc);
166 } else {
167 //Parse the dynamic price
168 $productstatic->fetch($objp->rowid, '', '', 1);
169
170 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
171 $priceparser = new PriceParser($this->db);
172 $price_result = $priceparser->parseProduct($productstatic);
173 if ($price_result >= 0) {
174 if ($objp->price_base_type == 'HT') {
175 $price_base_type = $langs->trans("HT");
176 } else {
177 $price_result *= (1 + ($productstatic->tva_tx / 100));
178 $price_base_type = $langs->trans("TTC");
179 }
180 $price = price($price_result);
181 }
182 }
183 }
184 $this->info_box_contents[$line][] = array(
185 'td' => 'class="nowraponall right amount"',
186 'text' => $price,
187 );
188
189 $this->info_box_contents[$line][] = array(
190 'td' => 'class="nowrap"',
191 'text' => $price_base_type,
192 );
193
194 $this->info_box_contents[$line][] = array(
195 'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateModification").': '.dol_print_date($datem, 'dayhour', 'tzuserrel')).'"',
196 'text' => dol_print_date($datem, 'day', 'tzuserrel'),
197 );
198
199 $this->info_box_contents[$line][] = array(
200 'td' => 'class="right" width="18"',
201 'text' => '<span class="statusrefsell">'.$productstatic->LibStatut($objp->tosell, 3, 0).'</span>',
202 'asis' => 1
203 );
204
205 $this->info_box_contents[$line][] = array(
206 'td' => 'class="right" width="18"',
207 'text' => '<span class="statusrefbuy">'.$productstatic->LibStatut($objp->tobuy, 3, 1).'</span>',
208 'asis' => 1
209 );
210
211 $line++;
212 }
213 if ($num == 0) {
214 $this->info_box_contents[$line][0] = array(
215 'td' => 'class="center"',
216 'text' => $langs->trans("NoRecordedProducts"),
217 );
218 }
219
220 $this->db->free($result);
221 } else {
222 $this->info_box_contents[0][0] = array(
223 'td' => '',
224 'maxlength' => 500,
225 'text' => ($this->db->error().' sql='.$sql),
226 );
227 }
228 } else {
229 $this->info_box_contents[0][0] = array(
230 'td' => 'class="nohover left"',
231 'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
232 );
233 }
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 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 a 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...