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