dolibarr 21.0.0-beta
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
55$htmlname = GETPOST('htmlname', 'aZ09');
56$socid = GETPOSTINT('socid');
57// type can be empty string or 0 or 1
58$type = GETPOST('type', 'int');
59$mode = GETPOSTINT('mode');
60$status = ((GETPOSTINT('status') >= 0) ? GETPOSTINT('status') : - 1); // status buy when mode = customer , status purchase when mode = supplier
61$status_purchase = ((GETPOSTINT('status_purchase') >= 0) ? GETPOSTINT('status_purchase') : - 1); // status purchase when mode = customer
62$outjson = (GETPOSTINT('outjson') ? GETPOSTINT('outjson') : 0);
63$price_level = GETPOSTINT('price_level');
64$action = GETPOST('action', 'aZ09');
65$id = GETPOSTINT('id');
66$price_by_qty_rowid = GETPOSTINT('pbq');
67$finished = GETPOSTINT('finished');
68$alsoproductwithnosupplierprice = GETPOSTINT('alsoproductwithnosupplierprice');
69$warehouseStatus = GETPOST('warehousestatus', 'alpha');
70$hidepriceinlabel = GETPOSTINT('hidepriceinlabel');
71$warehouseId = GETPOST('warehouseid', 'int');
72
73// Security check
74restrictedArea($user, 'produit|service|commande|propal|facture', 0, 'product&product');
75
76/*
77 * Actions
78 */
79
80// None
81
82
83/*
84 * View
85 */
86
87// print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
88
89if ($action == 'fetch' && !empty($id)) {
90 // action='fetch' is used to get product information on a product. So when action='fetch', id must be the product id.
91 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
92 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
93
94 top_httphead('application/json');
95
96 $outjson = array();
97
98 $object = new Product($db);
99 $ret = $object->fetch($id);
100 if ($ret > 0) {
101 $outref = $object->ref;
102 $outlabel = $object->label;
103 $outlabel_trans = '';
104 $outdesc = $object->description;
105 $outdesc_trans = '';
106 $outtype = $object->type;
107 $outprice_ht = null;
108 $outprice_ttc = null;
109 $outpricebasetype = null;
110 $outtva_tx_formated = 0;
111 $outtva_tx = 0;
112 $outdefault_vat_code = '';
113 $outqty = 1;
114 $outdiscount = 0;
115 $mandatory_period = $object->mandatory_period;
116 $found = false;
117
118 $price_level = 1;
119 if ($socid > 0) {
120 $needchangeaccordingtothirdparty = 0;
121 if (getDolGlobalInt('MAIN_MULTILANGS') && getDolGlobalString('PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE')) {
122 $needchangeaccordingtothirdparty = 1;
123 }
124 if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
125 $needchangeaccordingtothirdparty = 1;
126 }
127 if ($needchangeaccordingtothirdparty) {
128 $thirdpartytemp = new Societe($db);
129 $thirdpartytemp->fetch($socid);
130
131 //Load translation description and label according to thirdparty language
132 if (getDolGlobalInt('MAIN_MULTILANGS') && getDolGlobalString('PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE')) {
133 $newlang = $thirdpartytemp->default_lang;
134
135 if (!empty($newlang)) {
136 $outputlangs = new Translate("", $conf);
137 $outputlangs->setDefaultLang($newlang);
138 $outdesc_trans = (!empty($object->multilangs[$outputlangs->defaultlang]["description"])) ? $object->multilangs[$outputlangs->defaultlang]["description"] : $object->description;
139 $outlabel_trans = (!empty($object->multilangs[$outputlangs->defaultlang]["label"])) ? $object->multilangs[$outputlangs->defaultlang]["label"] : $object->label;
140 } else {
141 $outdesc_trans = $object->description;
142 $outlabel_trans = $object->label;
143 }
144 }
145
146 //Set price level according to thirdparty
147 if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
148 $price_level = $thirdpartytemp->price_level;
149 }
150 }
151 }
152
153 // Price by qty
154 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
155 $sql = "SELECT price, unitprice, quantity, remise_percent";
156 $sql .= " FROM ".MAIN_DB_PREFIX."product_price_by_qty";
157 $sql .= " WHERE rowid = ".((int) $price_by_qty_rowid);
158
159 $result = $db->query($sql);
160 if ($result) {
161 $objp = $db->fetch_object($result);
162 if ($objp) {
163 $found = true;
164 $outprice_ht = price($objp->unitprice);
165 $outprice_ttc = price($objp->unitprice * (1 + ($object->tva_tx / 100)));
166
167 $outpricebasetype = $object->price_base_type;
168 $outtva_tx_formated = price($object->tva_tx);
169 $outtva_tx = price2num($object->tva_tx);
170 $outdefault_vat_code = $object->default_vat_code;
171
172 $outqty = $objp->quantity;
173 $outdiscount = $objp->remise_percent;
174 }
175 }
176 }
177
178 // Multiprice (1 price per level)
179 if (!$found && isset($price_level) && $price_level >= 1 && (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES'))) { // If we need a particular price level (from 1 to 6)
180 $sql = "SELECT price, price_ttc, price_base_type,";
181 $sql .= " tva_tx, default_vat_code"; // Vat rate and code will be used if PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL is on.
182 $sql .= " FROM ".MAIN_DB_PREFIX."product_price ";
183 $sql .= " WHERE fk_product = ".((int) $id);
184 $sql .= " AND entity IN (".getEntity('productprice').")";
185 $sql .= " AND price_level = ".((int) $price_level);
186 $sql .= " ORDER BY date_price";
187 $sql .= " DESC LIMIT 1";
188
189 $result = $db->query($sql);
190 if ($result) {
191 $objp = $db->fetch_object($result);
192 if ($objp) {
193 $found = true;
194 $outprice_ht = price($objp->price); // formatted for language user because is inserted into input field
195 $outprice_ttc = price($objp->price_ttc); // formatted for language user because is inserted into input field
196 $outpricebasetype = $objp->price_base_type;
197 if (getDolGlobalString('PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL')) {
198 $outtva_tx_formated = price($objp->tva_tx); // formatted for language user because is inserted into input field
199 $outtva_tx = price2num($objp->tva_tx); // international numeric
200 $outdefault_vat_code = $objp->default_vat_code;
201 } else {
202 // The common and default behaviour.
203 $outtva_tx_formated = price($object->tva_tx);
204 $outtva_tx = price2num($object->tva_tx);
205 $outdefault_vat_code = $object->default_vat_code;
206 }
207 }
208 }
209 }
210
211 // Price by customer
212 if ((getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) && !empty($socid)) {
213 require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
214
215 $prodcustprice = new ProductCustomerPrice($db);
216
217 $filter = array('t.fk_product' => $object->id, 't.fk_soc' => $socid);
218
219 $result = $prodcustprice->fetchAll('', '', 0, 0, $filter);
220 if ($result) {
221 if (count($prodcustprice->lines) > 0) {
222 $found = true;
223 $outprice_ht = price($prodcustprice->lines[0]->price);
224 $outprice_ttc = price($prodcustprice->lines[0]->price_ttc);
225 $outpricebasetype = $prodcustprice->lines[0]->price_base_type;
226 $outtva_tx_formated = price($prodcustprice->lines[0]->tva_tx);
227 $outtva_tx = price2num($prodcustprice->lines[0]->tva_tx);
228 $outdefault_vat_code = $prodcustprice->lines[0]->default_vat_code;
229 }
230 }
231 }
232
233 if (!$found) {
234 $outprice_ht = price($object->price);
235 $outprice_ttc = price($object->price_ttc);
236 $outpricebasetype = $object->price_base_type;
237 $outtva_tx_formated = price($object->tva_tx);
238 $outtva_tx = price2num($object->tva_tx);
239 $outdefault_vat_code = $object->default_vat_code;
240 }
241
242 // VAT to use and default VAT for product are set to same value by default
243 $product_outtva_tx_formated = $outtva_tx_formated;
244 $product_outtva_tx = $outtva_tx;
245 $product_outdefault_vat_code = $outdefault_vat_code;
246
247 // If we ask the price according to buyer, we change it.
248 if (GETPOSTINT('addalsovatforthirdpartyid')) {
249 $thirdparty_buyer = new Societe($db);
250 $thirdparty_buyer->fetch($socid);
251
252 $tmpvatwithcode = get_default_tva($mysoc, $thirdparty_buyer, $id, 0);
253
254 if (!is_numeric($tmpvatwithcode) || $tmpvatwithcode != -1) {
255 $reg =array();
256 if (preg_match('/(.+)\s\‍((.+)\‍)/', $tmpvatwithcode, $reg)) {
257 $outtva_tx = price2num($reg[1]);
258 $outtva_tx_formated = price($outtva_tx);
259 $outdefault_vat_code = $reg[2];
260 } else {
261 $outtva_tx = price2num($tmpvatwithcode);
262 $outtva_tx_formated = price($outtva_tx);
263 $outdefault_vat_code = '';
264 }
265 }
266 }
267
268 $outjson = array(
269 'ref' => $outref,
270 'label' => $outlabel,
271 'label_trans' => $outlabel_trans,
272 'desc' => $outdesc,
273 'desc_trans' => $outdesc_trans,
274 'type' => $outtype,
275 'price_ht' => $outprice_ht,
276 'price_ttc' => $outprice_ttc,
277 'pricebasetype' => $outpricebasetype,
278 'product_tva_tx_formated' => $product_outtva_tx_formated,
279 'product_tva_tx' => $product_outtva_tx,
280 'product_default_vat_code' => $product_outdefault_vat_code,
281
282 'tva_tx_formated' => $outtva_tx_formated,
283 'tva_tx' => $outtva_tx,
284 'default_vat_code' => $outdefault_vat_code,
285
286 'qty' => $outqty,
287 'discount' => $outdiscount,
288 'mandatory_period' => $mandatory_period,
289 'array_options'=>$object->array_options
290 );
291 }
292
293 echo json_encode($outjson);
294} else {
295 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
296
297 $langs->loadLangs(array("main", "products"));
298
299 top_httphead();
300
301 if (empty($htmlname)) {
302 print json_encode(array());
303 return;
304 }
305
306 // Filter on the product to search can be:
307 // Into an array with key $htmlname123 (we take first one found). Which page use this ?
308 // Into a var with name $htmlname can be 'prodid', 'productid', ...
309 $match = preg_grep('/('.preg_quote($htmlname, '/').'[0-9]+)/', array_keys($_GET));
310 sort($match);
311
312 $idprod = (empty($match[0]) ? '' : $match[0]); // Take first key found into GET array with matching $htmlname123
313
314 if (GETPOST($htmlname, 'alpha') == '' && (!$idprod || !GETPOST($idprod, 'alpha'))) {
315 print json_encode(array());
316 return;
317 }
318
319 // When used from jQuery, the search term is added as GET param "term".
320 $searchkey = (($idprod && GETPOST($idprod, 'alpha')) ? GETPOST($idprod, 'alpha') : (GETPOST($htmlname, 'alpha') ? GETPOST($htmlname, 'alpha') : ''));
321
322 if (!isset($form) || !is_object($form)) {
323 $form = new Form($db);
324 }
325
326 $arrayresult = [];
327 if (empty($mode) || $mode == 1) { // mode=1: customer
328 $arrayresult = $form->select_produits_list("", $htmlname, $type, getDolGlobalInt('PRODUIT_LIMIT_SIZE', 1000), $price_level, $searchkey, $status, $finished, $outjson, $socid, '1', 0, '', $hidepriceinlabel, $warehouseStatus, $status_purchase, $warehouseId);
329 } elseif ($mode == 2) { // mode=2: supplier
330 $arrayresult = $form->select_produits_fournisseurs_list($socid, "", $htmlname, $type, "", $searchkey, $status, $outjson, getDolGlobalInt('PRODUIT_LIMIT_SIZE', 1000), $alsoproductwithnosupplierprice);
331 }
332
333 $db->close();
334
335 if ($outjson) {
336 print json_encode($arrayresult);
337 }
338}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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 a 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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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.