dolibarr 20.0.2
products.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
3 * Copyright (C) 2005-2013 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2020 Josep Lluís Amador <joseplluis@lliuretic.cat>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
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
27if (!defined('NOTOKENRENEWAL')) {
28 define('NOTOKENRENEWAL', 1); // Disables token renewal
29}
30if (!defined('NOREQUIREMENU')) {
31 define('NOREQUIREMENU', '1');
32}
33if (!defined('NOREQUIREHTML')) {
34 define('NOREQUIREHTML', '1');
35}
36if (!defined('NOREQUIREAJAX')) {
37 define('NOREQUIREAJAX', '1');
38}
39if (empty($_GET['keysearch']) && !defined('NOREQUIREHTML')) { // Keep $_GET here, GETPOST is not yet defined
40 define('NOREQUIREHTML', '1');
41}
42
43// Load Dolibarr environment
44require '../../main.inc.php';
45
46$htmlname = GETPOST('htmlname', 'aZ09');
47$socid = GETPOSTINT('socid');
48// type can be empty string or 0 or 1
49$type = GETPOST('type', 'int');
50$mode = GETPOSTINT('mode');
51$status = ((GETPOSTINT('status') >= 0) ? GETPOSTINT('status') : - 1); // status buy when mode = customer , status purchase when mode = supplier
52$status_purchase = ((GETPOSTINT('status_purchase') >= 0) ? GETPOSTINT('status_purchase') : - 1); // status purchase when mode = customer
53$outjson = (GETPOSTINT('outjson') ? GETPOSTINT('outjson') : 0);
54$price_level = GETPOSTINT('price_level');
55$action = GETPOST('action', 'aZ09');
56$id = GETPOSTINT('id');
57$price_by_qty_rowid = GETPOSTINT('pbq');
58$finished = GETPOSTINT('finished');
59$alsoproductwithnosupplierprice = GETPOSTINT('alsoproductwithnosupplierprice');
60$warehouseStatus = GETPOST('warehousestatus', 'alpha');
61$hidepriceinlabel = GETPOSTINT('hidepriceinlabel');
62
63// Security check
64restrictedArea($user, 'produit|service|commande|propal|facture', 0, 'product&product');
65
66
67/*
68 * View
69 */
70
71// print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
72
73if ($action == 'fetch' && !empty($id)) {
74 // action='fetch' is used to get product information on a product. So when action='fetch', id must be the product id.
75 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
76 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
77
78 top_httphead('application/json');
79
80 $outjson = array();
81
82 $object = new Product($db);
83 $ret = $object->fetch($id);
84 if ($ret > 0) {
85 $outref = $object->ref;
86 $outlabel = $object->label;
87 $outlabel_trans = '';
88 $outdesc = $object->description;
89 $outdesc_trans = '';
90 $outtype = $object->type;
91 $outprice_ht = null;
92 $outprice_ttc = null;
93 $outpricebasetype = null;
94 $outtva_tx_formated = 0;
95 $outtva_tx = 0;
96 $outdefault_vat_code = '';
97 $outqty = 1;
98 $outdiscount = 0;
99 $mandatory_period = $object->mandatory_period;
100 $found = false;
101
102 $price_level = 1;
103 if ($socid > 0) {
104 $needchangeaccordingtothirdparty = 0;
105 if (getDolGlobalInt('MAIN_MULTILANGS') && getDolGlobalString('PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE')) {
106 $needchangeaccordingtothirdparty = 1;
107 }
108 if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')) {
109 $needchangeaccordingtothirdparty = 1;
110 }
111 if ($needchangeaccordingtothirdparty) {
112 $thirdpartytemp = new Societe($db);
113 $thirdpartytemp->fetch($socid);
114
115 //Load translation description and label according to thirdparty language
116 if (getDolGlobalInt('MAIN_MULTILANGS') && getDolGlobalString('PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE')) {
117 $newlang = $thirdpartytemp->default_lang;
118
119 if (!empty($newlang)) {
120 $outputlangs = new Translate("", $conf);
121 $outputlangs->setDefaultLang($newlang);
122 $outdesc_trans = (!empty($object->multilangs[$outputlangs->defaultlang]["description"])) ? $object->multilangs[$outputlangs->defaultlang]["description"] : $object->description;
123 $outlabel_trans = (!empty($object->multilangs[$outputlangs->defaultlang]["label"])) ? $object->multilangs[$outputlangs->defaultlang]["label"] : $object->label;
124 } else {
125 $outdesc_trans = $object->description;
126 $outlabel_trans = $object->label;
127 }
128 }
129
130 //Set price level according to thirdparty
131 if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')) {
132 $price_level = $thirdpartytemp->price_level;
133 }
134 }
135 }
136
137 // Price by qty
138 if (!empty($price_by_qty_rowid) && $price_by_qty_rowid >= 1 && (getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES'))) { // If we need a particular price related to qty
139 $sql = "SELECT price, unitprice, quantity, remise_percent";
140 $sql .= " FROM ".MAIN_DB_PREFIX."product_price_by_qty";
141 $sql .= " WHERE rowid = ".((int) $price_by_qty_rowid);
142
143 $result = $db->query($sql);
144 if ($result) {
145 $objp = $db->fetch_object($result);
146 if ($objp) {
147 $found = true;
148 $outprice_ht = price($objp->unitprice);
149 $outprice_ttc = price($objp->unitprice * (1 + ($object->tva_tx / 100)));
150
151 $outpricebasetype = $object->price_base_type;
152 $outtva_tx_formated = price($object->tva_tx);
153 $outtva_tx = price2num($object->tva_tx);
154 $outdefault_vat_code = $object->default_vat_code;
155
156 $outqty = $objp->quantity;
157 $outdiscount = $objp->remise_percent;
158 }
159 }
160 }
161
162 // Multiprice (1 price per level)
163 if (!$found && isset($price_level) && $price_level >= 1 && (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES'))) { // If we need a particular price level (from 1 to 6)
164 $sql = "SELECT price, price_ttc, price_base_type,";
165 $sql .= " tva_tx, default_vat_code"; // Vat rate and code will be used if PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL is on.
166 $sql .= " FROM ".MAIN_DB_PREFIX."product_price ";
167 $sql .= " WHERE fk_product = ".((int) $id);
168 $sql .= " AND entity IN (".getEntity('productprice').")";
169 $sql .= " AND price_level = ".((int) $price_level);
170 $sql .= " ORDER BY date_price";
171 $sql .= " DESC LIMIT 1";
172
173 $result = $db->query($sql);
174 if ($result) {
175 $objp = $db->fetch_object($result);
176 if ($objp) {
177 $found = true;
178 $outprice_ht = price($objp->price); // formatted for language user because is inserted into input field
179 $outprice_ttc = price($objp->price_ttc); // formatted for language user because is inserted into input field
180 $outpricebasetype = $objp->price_base_type;
181 if (getDolGlobalString('PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL')) {
182 $outtva_tx_formated = price($objp->tva_tx); // formatted for language user because is inserted into input field
183 $outtva_tx = price2num($objp->tva_tx); // international numeric
184 $outdefault_vat_code = $objp->default_vat_code;
185 } else {
186 // The common and default behaviour.
187 $outtva_tx_formated = price($object->tva_tx);
188 $outtva_tx = price2num($object->tva_tx);
189 $outdefault_vat_code = $object->default_vat_code;
190 }
191 }
192 }
193 }
194
195 // Price by customer
196 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') && !empty($socid)) {
197 require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
198
199 $prodcustprice = new ProductCustomerPrice($db);
200
201 $filter = array('t.fk_product' => $object->id, 't.fk_soc' => $socid);
202
203 $result = $prodcustprice->fetchAll('', '', 0, 0, $filter);
204 if ($result) {
205 if (count($prodcustprice->lines) > 0) {
206 $found = true;
207 $outprice_ht = price($prodcustprice->lines[0]->price);
208 $outprice_ttc = price($prodcustprice->lines[0]->price_ttc);
209 $outpricebasetype = $prodcustprice->lines[0]->price_base_type;
210 $outtva_tx_formated = price($prodcustprice->lines[0]->tva_tx);
211 $outtva_tx = price2num($prodcustprice->lines[0]->tva_tx);
212 $outdefault_vat_code = $prodcustprice->lines[0]->default_vat_code;
213 }
214 }
215 }
216
217 if (!$found) {
218 $outprice_ht = price($object->price);
219 $outprice_ttc = price($object->price_ttc);
220 $outpricebasetype = $object->price_base_type;
221 $outtva_tx_formated = price($object->tva_tx);
222 $outtva_tx = price2num($object->tva_tx);
223 $outdefault_vat_code = $object->default_vat_code;
224 }
225
226 // VAT to use and default VAT for product are set to same value by default
227 $product_outtva_tx_formated = $outtva_tx_formated;
228 $product_outtva_tx = $outtva_tx;
229 $product_outdefault_vat_code = $outdefault_vat_code;
230
231 // If we ask the price according to buyer, we change it.
232 if (GETPOSTINT('addalsovatforthirdpartyid')) {
233 $thirdparty_buyer = new Societe($db);
234 $thirdparty_buyer->fetch($socid);
235
236 $tmpvatwithcode = get_default_tva($mysoc, $thirdparty_buyer, $id, 0);
237
238 if (!is_numeric($tmpvatwithcode) || $tmpvatwithcode != -1) {
239 $reg =array();
240 if (preg_match('/(.+)\s\‍((.+)\‍)/', $tmpvatwithcode, $reg)) {
241 $outtva_tx = price2num($reg[1]);
242 $outtva_tx_formated = price($outtva_tx);
243 $outdefault_vat_code = $reg[2];
244 } else {
245 $outtva_tx = price2num($tmpvatwithcode);
246 $outtva_tx_formated = price($outtva_tx);
247 $outdefault_vat_code = '';
248 }
249 }
250 }
251
252 $outjson = array(
253 'ref' => $outref,
254 'label' => $outlabel,
255 'label_trans' => $outlabel_trans,
256 'desc' => $outdesc,
257 'desc_trans' => $outdesc_trans,
258 'type' => $outtype,
259 'price_ht' => $outprice_ht,
260 'price_ttc' => $outprice_ttc,
261 'pricebasetype' => $outpricebasetype,
262 'product_tva_tx_formated' => $product_outtva_tx_formated,
263 'product_tva_tx' => $product_outtva_tx,
264 'product_default_vat_code' => $product_outdefault_vat_code,
265
266 'tva_tx_formated' => $outtva_tx_formated,
267 'tva_tx' => $outtva_tx,
268 'default_vat_code' => $outdefault_vat_code,
269
270 'qty' => $outqty,
271 'discount' => $outdiscount,
272 'mandatory_period' => $mandatory_period,
273 'array_options'=>$object->array_options
274 );
275 }
276
277 echo json_encode($outjson);
278} else {
279 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
280
281 $langs->loadLangs(array("main", "products"));
282
283 top_httphead();
284
285 if (empty($htmlname)) {
286 print json_encode(array());
287 return;
288 }
289
290 // Filter on the product to search can be:
291 // Into an array with key $htmlname123 (we take first one found). Which page use this ?
292 // Into a var with name $htmlname can be 'prodid', 'productid', ...
293 $match = preg_grep('/('.preg_quote($htmlname, '/').'[0-9]+)/', array_keys($_GET));
294 sort($match);
295
296 $idprod = (empty($match[0]) ? '' : $match[0]); // Take first key found into GET array with matching $htmlname123
297
298 if (GETPOST($htmlname, 'alpha') == '' && (!$idprod || !GETPOST($idprod, 'alpha'))) {
299 print json_encode(array());
300 return;
301 }
302
303 // When used from jQuery, the search term is added as GET param "term".
304 $searchkey = (($idprod && GETPOST($idprod, 'alpha')) ? GETPOST($idprod, 'alpha') : (GETPOST($htmlname, 'alpha') ? GETPOST($htmlname, 'alpha') : ''));
305
306 if (!isset($form) || !is_object($form)) {
307 $form = new Form($db);
308 }
309
310 if (empty($mode) || $mode == 1) { // mode=1: customer
311 $arrayresult = $form->select_produits_list("", $htmlname, $type, 0, $price_level, $searchkey, $status, $finished, $outjson, $socid, '1', 0, '', $hidepriceinlabel, $warehouseStatus, $status_purchase);
312 } elseif ($mode == 2) { // mode=2: supplier
313 $arrayresult = $form->select_produits_fournisseurs_list($socid, "", $htmlname, $type, "", $searchkey, $status, $outjson, 0, $alsoproductwithnosupplierprice);
314 }
315
316 $db->close();
317
318 if ($outjson) {
319 print json_encode($arrayresult);
320 }
321}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage generation of HTML components Only common components must be here.
File of class to manage predefined price products or services by customer.
Class to manage products or services.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage translations.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
get_default_tva(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that return vat rate of a product line (according to seller, buyer and product vat rate) VAT...
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.