dolibarr  13.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  *
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')) define('NOTOKENRENEWAL', 1); // Disables token renewal
27 if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
28 if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
29 if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
30 if (!defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
31 if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', '1');
32 if (empty($_GET['keysearch']) && !defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
33 
34 require '../../main.inc.php';
35 
36 $htmlname = GETPOST('htmlname', 'alpha');
37 $socid = GETPOST('socid', 'int');
38 $type = GETPOST('type', 'int');
39 $mode = GETPOST('mode', 'int');
40 $status = ((GETPOST('status', 'int') >= 0) ? GETPOST('status', 'int') : - 1);
41 $outjson = (GETPOST('outjson', 'int') ? GETPOST('outjson', 'int') : 0);
42 $price_level = GETPOST('price_level', 'int');
43 $action = GETPOST('action', 'aZ09');
44 $id = GETPOST('id', 'int');
45 $price_by_qty_rowid = GETPOST('pbq', 'int');
46 $finished = GETPOST('finished', 'int');
47 $alsoproductwithnosupplierprice = GETPOST('alsoproductwithnosupplierprice', 'int');
48 $warehouseStatus = GETPOST('warehousestatus', 'alpha');
49 $hidepriceinlabel = GETPOST('hidepriceinlabel', 'int');
50 
51 
52 /*
53  * View
54  */
55 
56 // print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
57 
58 dol_syslog(join(',', $_GET));
59 // print_r($_GET);
60 
61 if (!empty($action) && $action == 'fetch' && !empty($id))
62 {
63  // action='fetch' is used to get product information on a product. So when action='fetch', id must be the product id.
64  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
65  require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
66 
67  $outjson = array();
68 
69  $object = new Product($db);
70  $ret = $object->fetch($id);
71  if ($ret > 0)
72  {
73  $outref = $object->ref;
74  $outlabel = $object->label;
75  $outdesc = $object->description;
76  $outtype = $object->type;
77  $outqty = 1;
78  $outdiscount = 0;
79 
80  $found = false;
81 
82  $price_level = 1;
83  if ($socid > 0 && (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))) {
84  $thirdpartytemp = new Societe($db);
85  $thirdpartytemp->fetch($socid);
86  $price_level = $thirdpartytemp->price_level;
87  }
88 
89  // Price by qty
90  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
91  {
92  $sql = "SELECT price, unitprice, quantity, remise_percent";
93  $sql .= " FROM ".MAIN_DB_PREFIX."product_price_by_qty ";
94  $sql .= " WHERE rowid=".$price_by_qty_rowid."";
95 
96  $result = $db->query($sql);
97  if ($result) {
98  $objp = $db->fetch_object($result);
99  if ($objp) {
100  $found = true;
101  $outprice_ht = price($objp->unitprice);
102  $outprice_ttc = price($objp->unitprice * (1 + ($object->tva_tx / 100)));
103  $outpricebasetype = $object->price_base_type;
104  $outtva_tx = $object->tva_tx;
105  $outqty = $objp->quantity;
106  $outdiscount = $objp->remise_percent;
107  }
108  }
109  }
110 
111  // Multiprice
112  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)
113  {
114  $sql = "SELECT price, price_ttc, price_base_type, tva_tx";
115  $sql .= " FROM ".MAIN_DB_PREFIX."product_price ";
116  $sql .= " WHERE fk_product = '".$id."'";
117  $sql .= " AND entity IN (".getEntity('productprice').")";
118  $sql .= " AND price_level = ".((int) $price_level);
119  $sql .= " ORDER BY date_price";
120  $sql .= " DESC LIMIT 1";
121 
122  $result = $db->query($sql);
123  if ($result) {
124  $objp = $db->fetch_object($result);
125  if ($objp) {
126  $found = true;
127  $outprice_ht = price($objp->price);
128  $outprice_ttc = price($objp->price_ttc);
129  $outpricebasetype = $objp->price_base_type;
130  $outtva_tx = $objp->tva_tx;
131  }
132  }
133  }
134 
135  // Price by customer
136  if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES) && !empty($socid)) {
137  require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
138 
139  $prodcustprice = new Productcustomerprice($db);
140 
141  $filter = array('t.fk_product' => $object->id, 't.fk_soc' => $socid);
142 
143  $result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
144  if ($result) {
145  if (count($prodcustprice->lines) > 0) {
146  $found = true;
147  $outprice_ht = price($prodcustprice->lines [0]->price);
148  $outprice_ttc = price($prodcustprice->lines [0]->price_ttc);
149  $outpricebasetype = $prodcustprice->lines [0]->price_base_type;
150  $outtva_tx = $prodcustprice->lines [0]->tva_tx;
151  }
152  }
153  }
154 
155  if (!$found) {
156  $outprice_ht = price($object->price);
157  $outprice_ttc = price($object->price_ttc);
158  $outpricebasetype = $object->price_base_type;
159  $outtva_tx = $object->tva_tx;
160  }
161 
162  $outjson = array('ref' => $outref, 'label' => $outlabel, 'desc' => $outdesc, 'type' => $outtype, 'price_ht' => $outprice_ht, 'price_ttc' => $outprice_ttc, 'pricebasetype' => $outpricebasetype, 'tva_tx' => $outtva_tx, 'qty' => $outqty, 'discount' => $outdiscount);
163  }
164 
165  echo json_encode($outjson);
166 } else {
167  require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
168 
169  $langs->loadLangs(array("main", "products"));
170 
171  top_httphead();
172 
173  if (empty($htmlname))
174  {
175  print json_encode(array());
176  return;
177  }
178 
179  $match = preg_grep('/('.$htmlname.'[0-9]+)/', array_keys($_GET));
180  sort($match);
181 
182  $idprod = (!empty($match[0]) ? $match[0] : '');
183 
184  if (GETPOST($htmlname, 'alpha') == '' && (!$idprod || !GETPOST($idprod, 'alpha')))
185  {
186  print json_encode(array());
187  return;
188  }
189 
190  // When used from jQuery, the search term is added as GET param "term".
191  $searchkey = (($idprod && GETPOST($idprod, 'alpha')) ? GETPOST($idprod, 'alpha') : (GETPOST($htmlname, 'alpha') ? GETPOST($htmlname, 'alpha') : ''));
192 
193  $form = new Form($db);
194 
195  if (empty($mode) || $mode == 1) { // mode=1: customer
196  $arrayresult = $form->select_produits_list("", $htmlname, $type, 0, $price_level, $searchkey, $status, $finished, $outjson, $socid, '1', 0, '', $hidepriceinlabel, $warehouseStatus);
197  } elseif ($mode == 2) { // mode=2: supplier
198  $arrayresult = $form->select_produits_fournisseurs_list($socid, "", $htmlname, $type, "", $searchkey, $status, $outjson, 0, $alsoproductwithnosupplierprice);
199  }
200 
201  $db->close();
202 
203  if ($outjson)
204  print json_encode($arrayresult);
205 }
File of class to manage predefined price products or services by customer.
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage products or services.
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype= 'text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1214
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...
Class to manage generation of HTML components Only common components must be here.
Class to manage third parties objects (customers, suppliers, prospects...)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
print
Draft customers invoices.
Definition: index.php:89