dolibarr 21.0.0-beta
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 $this->urltoaddentry = DOL_URL_ROOT.'/product/card.php?action=create';
59 $this->msgNoRecords = 'NoRecordedProducts';
60 }
61
68 public function loadBox($max = 5)
69 {
70 global $user, $langs, $conf, $hookmanager;
71
72 $this->max = $max;
73
74 include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
75 $productstatic = new Product($this->db);
76
77 $this->info_box_head = array(
78 '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>',
79 );
80
81 if ($user->hasRight('produit', 'lire') || $user->hasRight('service', 'lire')) {
82 $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";
83 $sql .= ", p.accountancy_code_sell";
84 $sql .= ", p.accountancy_code_sell_intra";
85 $sql .= ", p.accountancy_code_sell_export";
86 $sql .= ", p.accountancy_code_buy";
87 $sql .= ", p.accountancy_code_buy_intra";
88 $sql .= ", p.accountancy_code_buy_export";
89 $sql .= ', p.barcode';
90 $sql .= " FROM ".MAIN_DB_PREFIX."product as p";
91 $sql .= ' WHERE p.entity IN ('.getEntity($productstatic->element).')';
92 if (!$user->hasRight('produit', 'lire')) {
93 $sql .= ' AND p.fk_product_type != 0';
94 }
95 if (!$user->hasRight('service', 'lire')) {
96 $sql .= ' AND p.fk_product_type != 1';
97 }
98 // Add where from hooks
99 if (is_object($hookmanager)) {
100 $parameters = array('boxproductlist' => 1, 'boxcode' => $this->boxcode);
101 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $productstatic); // Note that $action and $object may have been modified by hook
102 $sql .= $hookmanager->resPrint;
103 }
104 $sql .= $this->db->order('p.datec', 'DESC');
105 $sql .= $this->db->plimit($max, 0);
106
107 $result = $this->db->query($sql);
108 if ($result) {
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
115 // Multilangs
116 if (getDolGlobalInt('MAIN_MULTILANGS')) { // si l'option est active
117 $sqld = "SELECT label";
118 $sqld .= " FROM ".MAIN_DB_PREFIX."product_lang";
119 $sqld .= " WHERE fk_product = ".((int) $objp->rowid);
120 $sqld .= " AND lang = '".$this->db->escape($langs->getDefaultLang())."'";
121 $sqld .= " LIMIT 1";
122
123 $resultd = $this->db->query($sqld);
124 if ($resultd) {
125 $objtp = $this->db->fetch_object($resultd);
126 if (isset($objtp->label) && $objtp->label != '') {
127 $objp->label = $objtp->label;
128 }
129 }
130 }
131 $productstatic->id = $objp->rowid;
132 $productstatic->ref = $objp->ref;
133 $productstatic->type = $objp->fk_product_type;
134 $productstatic->label = $objp->label;
135 $productstatic->entity = $objp->entity;
136 $productstatic->status = $objp->tosell;
137 $productstatic->status_buy = $objp->tobuy;
138 $productstatic->barcode = $objp->barcode;
139 $productstatic->accountancy_code_sell = $objp->accountancy_code_sell;
140 $productstatic->accountancy_code_sell_intra = $objp->accountancy_code_sell_intra;
141 $productstatic->accountancy_code_sell_export = $objp->accountancy_code_sell_export;
142 $productstatic->accountancy_code_buy = $objp->accountancy_code_buy;
143 $productstatic->accountancy_code_buy_intra = $objp->accountancy_code_buy_intra;
144 $productstatic->accountancy_code_buy_export = $objp->accountancy_code_buy_export;
145 $productstatic->date_modification = $datem;
146
147 $usercancreadprice = getDolGlobalString('MAIN_USE_ADVANCED_PERMS') ? $user->hasRight('product', 'product_advance', 'read_prices') : $user->hasRight('product', 'read');
148 if ($productstatic->isService()) {
149 $usercancreadprice = getDolGlobalString('MAIN_USE_ADVANCED_PERMS') ? $user->hasRight('service', 'service_advance', 'read_prices') : $user->hasRight('service', 'read');
150 }
151
152 $this->info_box_contents[$line][] = array(
153 'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
154 'text' => $productstatic->getNomUrl(1),
155 'asis' => 1,
156 );
157
158 $this->info_box_contents[$line][] = array(
159 'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
160 'text' => $objp->label,
161 );
162 $price = '';
163 $price_base_type = '';
164 if ($usercancreadprice) {
165 if (!isModEnabled('dynamicprices') || empty($objp->fk_price_expression)) {
166 $price_base_type = $langs->trans($objp->price_base_type);
167 $price = ($objp->price_base_type == 'HT') ? price($objp->price) : $price = price($objp->price_ttc);
168 } else {
169 //Parse the dynamic price
170 $productstatic->fetch($objp->rowid, '', '', 1);
171
172 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
173 $priceparser = new PriceParser($this->db);
174 $price_result = $priceparser->parseProduct($productstatic);
175 if ($price_result >= 0) {
176 if ($objp->price_base_type == 'HT') {
177 $price_base_type = $langs->trans("HT");
178 } else {
179 $price_result *= (1 + ($productstatic->tva_tx / 100));
180 $price_base_type = $langs->trans("TTC");
181 }
182 $price = price($price_result);
183 }
184 }
185 }
186 $this->info_box_contents[$line][] = array(
187 'td' => 'class="nowraponall right amount"',
188 'text' => $price,
189 );
190
191 $this->info_box_contents[$line][] = array(
192 'td' => 'class="nowrap"',
193 'text' => $price_base_type,
194 );
195
196 $this->info_box_contents[$line][] = array(
197 'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateModification").': '.dol_print_date($datem, 'dayhour', 'tzuserrel')).'"',
198 'text' => dol_print_date($datem, 'day', 'tzuserrel'),
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("NoRecordedProducts"),
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 left"',
233 'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
234 );
235 }
236 }
237
238
239
248 public function showBox($head = null, $contents = null, $nooutput = 0)
249 {
250 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
251 }
252}
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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79