dolibarr  16.0.5
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 (!defined('NOCSRFCHECK')) {
42  define('NOCSRFCHECK', '1');
43 }
44 if (empty($_GET['keysearch']) && !defined('NOREQUIREHTML')) {
45  define('NOREQUIREHTML', '1');
46 }
47 
48 require '../../main.inc.php';
49 
50 $htmlname = GETPOST('htmlname', 'aZ09');
51 $socid = GETPOST('socid', 'int');
52 $type = GETPOST('type', 'int');
53 $mode = GETPOST('mode', 'int');
54 $status = ((GETPOST('status', 'int') >= 0) ? GETPOST('status', 'int') : - 1); // status buy when mode = customer , status purchase when mode = supplier
55 $status_purchase = ((GETPOST('status_purchase', 'int') >= 0) ? GETPOST('status_purchase', 'int') : - 1); // status purchase when mode = customer
56 $outjson = (GETPOST('outjson', 'int') ? GETPOST('outjson', 'int') : 0);
57 $price_level = GETPOST('price_level', 'int');
58 $action = GETPOST('action', 'aZ09');
59 $id = GETPOST('id', 'int');
60 $price_by_qty_rowid = GETPOST('pbq', 'int');
61 $finished = GETPOST('finished', 'int');
62 $alsoproductwithnosupplierprice = GETPOST('alsoproductwithnosupplierprice', 'int');
63 $warehouseStatus = GETPOST('warehousestatus', 'alpha');
64 $hidepriceinlabel = GETPOST('hidepriceinlabel', 'int');
65 
66 // Security check
67 restrictedArea($user, 'produit|service', 0, 'product&product');
68 
69 
70 /*
71  * View
72  */
73 
74 // print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
75 // print_r($_GET);
76 
77 if ($action == 'fetch' && !empty($id)) {
78  // action='fetch' is used to get product information on a product. So when action='fetch', id must be the product id.
79  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
80  require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
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 = 0;
97  $outdefault_vat_code = '';
98  $outqty = 1;
99  $outdiscount = 0;
100  $mandatory_period = $object->mandatory_period;
101  $found = false;
102 
103  $price_level = 1;
104  if ($socid > 0) {
105  $thirdpartytemp = new Societe($db);
106  $thirdpartytemp->fetch($socid);
107 
108  //Load translation description and label
109  if (!empty($conf->global->MAIN_MULTILANGS) && !empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE)) {
110  $newlang = $thirdpartytemp->default_lang;
111 
112  if (!empty($newlang)) {
113  $outputlangs = new Translate("", $conf);
114  $outputlangs->setDefaultLang($newlang);
115  $outdesc_trans = (!empty($object->multilangs[$outputlangs->defaultlang]["description"])) ? $object->multilangs[$outputlangs->defaultlang]["description"] : $object->description;
116  $outlabel_trans = (!empty($object->multilangs[$outputlangs->defaultlang]["label"])) ? $object->multilangs[$outputlangs->defaultlang]["label"] : $object->label;
117  } else {
118  $outdesc_trans = $object->description;
119  $outlabel_trans = $object->label;
120  }
121  }
122 
123  if (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) {
124  $price_level = $thirdpartytemp->price_level;
125  }
126  }
127 
128  // Price by qty
129  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
130  $sql = "SELECT price, unitprice, quantity, remise_percent";
131  $sql .= " FROM ".MAIN_DB_PREFIX."product_price_by_qty";
132  $sql .= " WHERE rowid = ".((int) $price_by_qty_rowid);
133 
134  $result = $db->query($sql);
135  if ($result) {
136  $objp = $db->fetch_object($result);
137  if ($objp) {
138  $found = true;
139  $outprice_ht = price($objp->unitprice);
140  $outprice_ttc = price($objp->unitprice * (1 + ($object->tva_tx / 100)));
141 
142  $outpricebasetype = $object->price_base_type;
143  $outtva_tx = $object->tva_tx;
144  $outdefault_vat_code = $object->default_vat_code;
145 
146  $outqty = $objp->quantity;
147  $outdiscount = $objp->remise_percent;
148  }
149  }
150  }
151 
152  // Multiprice (1 price per level)
153  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)
154  $sql = "SELECT price, price_ttc, price_base_type,";
155  $sql .= " tva_tx, default_vat_code"; // Vat rate and code will be used if PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL is on.
156  $sql .= " FROM ".MAIN_DB_PREFIX."product_price ";
157  $sql .= " WHERE fk_product = ".((int) $id);
158  $sql .= " AND entity IN (".getEntity('productprice').")";
159  $sql .= " AND price_level = ".((int) $price_level);
160  $sql .= " ORDER BY date_price";
161  $sql .= " DESC LIMIT 1";
162 
163  $result = $db->query($sql);
164  if ($result) {
165  $objp = $db->fetch_object($result);
166  if ($objp) {
167  $found = true;
168  $outprice_ht = price($objp->price);
169  $outprice_ttc = price($objp->price_ttc);
170  $outpricebasetype = $objp->price_base_type;
171  if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) {
172  $outtva_tx = $objp->tva_tx;
173  $outdefault_vat_code = $objp->default_vat_code;
174  } else {
175  // The common and default behaviour.
176  $outtva_tx = $object->tva_tx;
177  $outdefault_vat_code = $object->default_vat_code;
178  }
179  }
180  }
181  }
182 
183  // Price by customer
184  if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES) && !empty($socid)) {
185  require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
186 
187  $prodcustprice = new Productcustomerprice($db);
188 
189  $filter = array('t.fk_product' => $object->id, 't.fk_soc' => $socid);
190 
191  $result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
192  if ($result) {
193  if (count($prodcustprice->lines) > 0) {
194  $found = true;
195  $outprice_ht = price($prodcustprice->lines[0]->price);
196  $outprice_ttc = price($prodcustprice->lines[0]->price_ttc);
197  $outpricebasetype = $prodcustprice->lines[0]->price_base_type;
198  $outtva_tx = $prodcustprice->lines[0]->tva_tx;
199  $outdefault_vat_code = $prodcustprice->lines[0]->default_vat_code;
200  }
201  }
202  }
203 
204  if (!$found) {
205  $outprice_ht = price($object->price);
206  $outprice_ttc = price($object->price_ttc);
207  $outpricebasetype = $object->price_base_type;
208  $outtva_tx = $object->tva_tx;
209  $outdefault_vat_code = $object->default_vat_code;
210  }
211 
212  $outjson = array(
213  'ref' => $outref,
214  'label' => $outlabel,
215  'label_trans' => $outlabel_trans,
216  'desc' => $outdesc,
217  'desc_trans' => $outdesc_trans,
218  'type' => $outtype,
219  'price_ht' => $outprice_ht,
220  'price_ttc' => $outprice_ttc,
221  'pricebasetype' => $outpricebasetype,
222  'tva_tx' => $outtva_tx,
223  'default_vat_code' => $outdefault_vat_code,
224  'qty' => $outqty,
225  'discount' => $outdiscount,
226  'mandatory_period' => $mandatory_period,
227  'array_options'=>$object->array_options);
228  }
229 
230  echo json_encode($outjson);
231 } else {
232  require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
233 
234  $langs->loadLangs(array("main", "products"));
235 
236  top_httphead();
237 
238  if (empty($htmlname)) {
239  print json_encode(array());
240  return;
241  }
242 
243  // Filter on the product to search can be:
244  // Into an array with key $htmlname123 (we take first one found). Which page use this ?
245  // Into a var with name $htmlname can be 'prodid', 'productid', ...
246  $match = preg_grep('/('.preg_quote($htmlname, '/').'[0-9]+)/', array_keys($_GET));
247  sort($match);
248 
249  $idprod = (empty($match[0]) ? '' : $match[0]); // Take first key found into GET array with matching $htmlname123
250 
251  if (GETPOST($htmlname, 'alpha') == '' && (!$idprod || !GETPOST($idprod, 'alpha'))) {
252  print json_encode(array());
253  return;
254  }
255 
256  // When used from jQuery, the search term is added as GET param "term".
257  $searchkey = (($idprod && GETPOST($idprod, 'alpha')) ? GETPOST($idprod, 'alpha') : (GETPOST($htmlname, 'alpha') ? GETPOST($htmlname, 'alpha') : ''));
258 
259  if (!isset($form) || !is_object($form)) {
260  $form = new Form($db);
261  }
262 
263  if (empty($mode) || $mode == 1) { // mode=1: customer
264  $arrayresult = $form->select_produits_list("", $htmlname, $type, 0, $price_level, $searchkey, $status, $finished, $outjson, $socid, '1', 0, '', $hidepriceinlabel, $warehouseStatus, $status_purchase);
265  } elseif ($mode == 2) { // mode=2: supplier
266  $arrayresult = $form->select_produits_fournisseurs_list($socid, "", $htmlname, $type, "", $searchkey, $status, $outjson, 0, $alsoproductwithnosupplierprice);
267  }
268 
269  $db->close();
270 
271  if ($outjson) {
272  print json_encode($arrayresult);
273  }
274 }
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
restrictedArea
restrictedArea($user, $features, $objectid=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.
Definition: security.lib.php:234
Productcustomerprice
File of class to manage predefined price products or services by customer.
Definition: productcustomerprice.class.php:29
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
Translate
Class to manage translations.
Definition: translate.class.php:30
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
top_httphead
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1407
Product
Class to manage products or services.
Definition: product.class.php:46
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
price
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.
Definition: functions.lib.php:5541