dolibarr  17.0.4
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  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
26 if (!defined('NOTOKENRENEWAL')) {
27  define('NOTOKENRENEWAL', 1); // Disables token renewal
28 }
29 if (!defined('NOREQUIREMENU')) {
30  define('NOREQUIREMENU', '1');
31 }
32 if (!defined('NOREQUIREHTML')) {
33  define('NOREQUIREHTML', '1');
34 }
35 if (!defined('NOREQUIREAJAX')) {
36  define('NOREQUIREAJAX', '1');
37 }
38 if (!defined('NOREQUIRESOC')) {
39  define('NOREQUIRESOC', '1');
40 }
41 if (empty($_GET['keysearch']) && !defined('NOREQUIREHTML')) {
42  define('NOREQUIREHTML', '1');
43 }
44 
45 // Load Dolibarr environment
46 require '../../main.inc.php';
47 
48 $htmlname = GETPOST('htmlname', 'aZ09');
49 $socid = GETPOST('socid', 'int');
50 $type = GETPOST('type', 'int');
51 $mode = GETPOST('mode', 'int');
52 $status = ((GETPOST('status', 'int') >= 0) ? GETPOST('status', 'int') : - 1); // status buy when mode = customer , status purchase when mode = supplier
53 $status_purchase = ((GETPOST('status_purchase', 'int') >= 0) ? GETPOST('status_purchase', 'int') : - 1); // status purchase when mode = customer
54 $outjson = (GETPOST('outjson', 'int') ? GETPOST('outjson', 'int') : 0);
55 $price_level = GETPOST('price_level', 'int');
56 $action = GETPOST('action', 'aZ09');
57 $id = GETPOST('id', 'int');
58 $price_by_qty_rowid = GETPOST('pbq', 'int');
59 $finished = GETPOST('finished', 'int');
60 $alsoproductwithnosupplierprice = GETPOST('alsoproductwithnosupplierprice', 'int');
61 $warehouseStatus = GETPOST('warehousestatus', 'alpha');
62 $hidepriceinlabel = GETPOST('hidepriceinlabel', 'int');
63 
64 // Security check
65 restrictedArea($user, 'produit|service', 0, 'product&product');
66 
67 
68 /*
69  * View
70  */
71 
72 // print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
73 // print_r($_GET);
74 
75 if ($action == 'fetch' && !empty($id)) {
76  // action='fetch' is used to get product information on a product. So when action='fetch', id must be the product id.
77  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
78  require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
79 
80  top_httphead('application/json');
81 
82  $outjson = array();
83 
84  $object = new Product($db);
85  $ret = $object->fetch($id);
86  if ($ret > 0) {
87  $outref = $object->ref;
88  $outlabel = $object->label;
89  $outlabel_trans = '';
90  $outdesc = $object->description;
91  $outdesc_trans = '';
92  $outtype = $object->type;
93  $outprice_ht = null;
94  $outprice_ttc = null;
95  $outpricebasetype = null;
96  $outtva_tx_formated = 0;
97  $outtva_tx = 0;
98  $outdefault_vat_code = '';
99  $outqty = 1;
100  $outdiscount = 0;
101  $mandatory_period = $object->mandatory_period;
102  $found = false;
103 
104  $price_level = 1;
105  if ($socid > 0) {
106  $thirdpartytemp = new Societe($db);
107  $thirdpartytemp->fetch($socid);
108 
109  //Load translation description and label
110  if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE)) {
111  $newlang = $thirdpartytemp->default_lang;
112 
113  if (!empty($newlang)) {
114  $outputlangs = new Translate("", $conf);
115  $outputlangs->setDefaultLang($newlang);
116  $outdesc_trans = (!empty($object->multilangs[$outputlangs->defaultlang]["description"])) ? $object->multilangs[$outputlangs->defaultlang]["description"] : $object->description;
117  $outlabel_trans = (!empty($object->multilangs[$outputlangs->defaultlang]["label"])) ? $object->multilangs[$outputlangs->defaultlang]["label"] : $object->label;
118  } else {
119  $outdesc_trans = $object->description;
120  $outlabel_trans = $object->label;
121  }
122  }
123 
124  if (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) {
125  $price_level = $thirdpartytemp->price_level;
126  }
127  }
128 
129  // Price by qty
130  if (!empty($price_by_qty_rowid) && $price_by_qty_rowid >= 1 && (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))) { // If we need a particular price related to qty
131  $sql = "SELECT price, unitprice, quantity, remise_percent";
132  $sql .= " FROM ".MAIN_DB_PREFIX."product_price_by_qty";
133  $sql .= " WHERE rowid = ".((int) $price_by_qty_rowid);
134 
135  $result = $db->query($sql);
136  if ($result) {
137  $objp = $db->fetch_object($result);
138  if ($objp) {
139  $found = true;
140  $outprice_ht = price($objp->unitprice);
141  $outprice_ttc = price($objp->unitprice * (1 + ($object->tva_tx / 100)));
142 
143  $outpricebasetype = $object->price_base_type;
144  $outtva_tx_formated = price($object->tva_tx);
145  $outtva_tx = price2num($object->tva_tx);
146  $outdefault_vat_code = $object->default_vat_code;
147 
148  $outqty = $objp->quantity;
149  $outdiscount = $objp->remise_percent;
150  }
151  }
152  }
153 
154  // Multiprice (1 price per level)
155  if (!$found && isset($price_level) && $price_level >= 1 && (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))) { // If we need a particular price level (from 1 to 6)
156  $sql = "SELECT price, price_ttc, price_base_type,";
157  $sql .= " tva_tx, default_vat_code"; // Vat rate and code will be used if PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL is on.
158  $sql .= " FROM ".MAIN_DB_PREFIX."product_price ";
159  $sql .= " WHERE fk_product = ".((int) $id);
160  $sql .= " AND entity IN (".getEntity('productprice').")";
161  $sql .= " AND price_level = ".((int) $price_level);
162  $sql .= " ORDER BY date_price";
163  $sql .= " DESC LIMIT 1";
164 
165  $result = $db->query($sql);
166  if ($result) {
167  $objp = $db->fetch_object($result);
168  if ($objp) {
169  $found = true;
170  $outprice_ht = price($objp->price); // formated for langage user because is inserted into input field
171  $outprice_ttc = price($objp->price_ttc); // formated for langage user because is inserted into input field
172  $outpricebasetype = $objp->price_base_type;
173  if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) {
174  $outtva_tx_formated = price($objp->tva_tx); // formated for langage user because is inserted into input field
175  $outtva_tx = price2num($objp->tva_tx); // international numeric
176  $outdefault_vat_code = $objp->default_vat_code;
177  } else {
178  // The common and default behaviour.
179  $outtva_tx_formated = price($object->tva_tx);
180  $outtva_tx = price2num($object->tva_tx);
181  $outdefault_vat_code = $object->default_vat_code;
182  }
183  }
184  }
185  }
186 
187  // Price by customer
188  if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES) && !empty($socid)) {
189  require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
190 
191  $prodcustprice = new Productcustomerprice($db);
192 
193  $filter = array('t.fk_product' => $object->id, 't.fk_soc' => $socid);
194 
195  $result = $prodcustprice->fetchAll('', '', 0, 0, $filter);
196  if ($result) {
197  if (count($prodcustprice->lines) > 0) {
198  $found = true;
199  $outprice_ht = price($prodcustprice->lines[0]->price);
200  $outprice_ttc = price($prodcustprice->lines[0]->price_ttc);
201  $outpricebasetype = $prodcustprice->lines[0]->price_base_type;
202  $outtva_tx_formated = price($prodcustprice->lines[0]->tva_tx);
203  $outtva_tx = price2num($prodcustprice->lines[0]->tva_tx);
204  $outdefault_vat_code = $prodcustprice->lines[0]->default_vat_code;
205  }
206  }
207  }
208 
209  if (!$found) {
210  $outprice_ht = price($object->price);
211  $outprice_ttc = price($object->price_ttc);
212  $outpricebasetype = $object->price_base_type;
213  $outtva_tx_formated = price($object->tva_tx);
214  $outtva_tx = price2num($object->tva_tx);
215  $outdefault_vat_code = $object->default_vat_code;
216  }
217 
218  $outjson = array(
219  'ref' => $outref,
220  'label' => $outlabel,
221  'label_trans' => $outlabel_trans,
222  'desc' => $outdesc,
223  'desc_trans' => $outdesc_trans,
224  'type' => $outtype,
225  'price_ht' => $outprice_ht,
226  'price_ttc' => $outprice_ttc,
227  'pricebasetype' => $outpricebasetype,
228  'tva_tx_formated' => $outtva_tx_formated,
229  'tva_tx' => $outtva_tx,
230  'default_vat_code' => $outdefault_vat_code,
231  'qty' => $outqty,
232  'discount' => $outdiscount,
233  'mandatory_period' => $mandatory_period,
234  'array_options'=>$object->array_options);
235  }
236 
237  echo json_encode($outjson);
238 } else {
239  require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
240 
241  $langs->loadLangs(array("main", "products"));
242 
243  top_httphead();
244 
245  if (empty($htmlname)) {
246  print json_encode(array());
247  return;
248  }
249 
250  // Filter on the product to search can be:
251  // Into an array with key $htmlname123 (we take first one found). Which page use this ?
252  // Into a var with name $htmlname can be 'prodid', 'productid', ...
253  $match = preg_grep('/('.preg_quote($htmlname, '/').'[0-9]+)/', array_keys($_GET));
254  sort($match);
255 
256  $idprod = (empty($match[0]) ? '' : $match[0]); // Take first key found into GET array with matching $htmlname123
257 
258  if (GETPOST($htmlname, 'alpha') == '' && (!$idprod || !GETPOST($idprod, 'alpha'))) {
259  print json_encode(array());
260  return;
261  }
262 
263  // When used from jQuery, the search term is added as GET param "term".
264  $searchkey = (($idprod && GETPOST($idprod, 'alpha')) ? GETPOST($idprod, 'alpha') : (GETPOST($htmlname, 'alpha') ? GETPOST($htmlname, 'alpha') : ''));
265 
266  if (!isset($form) || !is_object($form)) {
267  $form = new Form($db);
268  }
269 
270  if (empty($mode) || $mode == 1) { // mode=1: customer
271  $arrayresult = $form->select_produits_list("", $htmlname, $type, 0, $price_level, $searchkey, $status, $finished, $outjson, $socid, '1', 0, '', $hidepriceinlabel, $warehouseStatus, $status_purchase);
272  } elseif ($mode == 2) { // mode=2: supplier
273  $arrayresult = $form->select_produits_fournisseurs_list($socid, "", $htmlname, $type, "", $searchkey, $status, $outjson, 0, $alsoproductwithnosupplierprice);
274  }
275 
276  $db->close();
277 
278  if ($outjson) {
279  print json_encode($arrayresult);
280  }
281 }
Class to manage generation of HTML components Only common components must be here.
Class to manage products or services.
File of class to manage predefined price products or services by customer.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage translations.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
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 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.
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1440
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.