products.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. /**
  21. * \file htdocs/product/ajax/products.php
  22. * \brief File to return Ajax response on product list request.
  23. */
  24. if (!defined('NOTOKENRENEWAL')) {
  25. define('NOTOKENRENEWAL', 1); // Disables token renewal
  26. }
  27. if (!defined('NOREQUIREMENU')) {
  28. define('NOREQUIREMENU', '1');
  29. }
  30. if (!defined('NOREQUIREHTML')) {
  31. define('NOREQUIREHTML', '1');
  32. }
  33. if (!defined('NOREQUIREAJAX')) {
  34. define('NOREQUIREAJAX', '1');
  35. }
  36. if (!defined('NOREQUIRESOC')) {
  37. define('NOREQUIRESOC', '1');
  38. }
  39. if (empty($_GET['keysearch']) && !defined('NOREQUIREHTML')) {
  40. define('NOREQUIREHTML', '1');
  41. }
  42. // Load Dolibarr environment
  43. require '../../main.inc.php';
  44. $htmlname = GETPOST('htmlname', 'aZ09');
  45. $socid = GETPOST('socid', 'int');
  46. $type = GETPOST('type', 'int');
  47. $mode = GETPOST('mode', 'int');
  48. $status = ((GETPOST('status', 'int') >= 0) ? GETPOST('status', 'int') : - 1); // status buy when mode = customer , status purchase when mode = supplier
  49. $status_purchase = ((GETPOST('status_purchase', 'int') >= 0) ? GETPOST('status_purchase', 'int') : - 1); // status purchase when mode = customer
  50. $outjson = (GETPOST('outjson', 'int') ? GETPOST('outjson', 'int') : 0);
  51. $price_level = GETPOST('price_level', 'int');
  52. $action = GETPOST('action', 'aZ09');
  53. $id = GETPOST('id', 'int');
  54. $price_by_qty_rowid = GETPOST('pbq', 'int');
  55. $finished = GETPOST('finished', 'int');
  56. $alsoproductwithnosupplierprice = GETPOST('alsoproductwithnosupplierprice', 'int');
  57. $warehouseStatus = GETPOST('warehousestatus', 'alpha');
  58. $hidepriceinlabel = GETPOST('hidepriceinlabel', 'int');
  59. // Security check
  60. restrictedArea($user, 'produit|service', 0, 'product&product');
  61. /*
  62. * View
  63. */
  64. // print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
  65. // print_r($_GET);
  66. if ($action == 'fetch' && !empty($id)) {
  67. // action='fetch' is used to get product information on a product. So when action='fetch', id must be the product id.
  68. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  69. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  70. top_httphead('application/json');
  71. $outjson = array();
  72. $object = new Product($db);
  73. $ret = $object->fetch($id);
  74. if ($ret > 0) {
  75. $outref = $object->ref;
  76. $outlabel = $object->label;
  77. $outlabel_trans = '';
  78. $outdesc = $object->description;
  79. $outdesc_trans = '';
  80. $outtype = $object->type;
  81. $outprice_ht = null;
  82. $outprice_ttc = null;
  83. $outpricebasetype = null;
  84. $outtva_tx_formated = 0;
  85. $outtva_tx = 0;
  86. $outdefault_vat_code = '';
  87. $outqty = 1;
  88. $outdiscount = 0;
  89. $mandatory_period = $object->mandatory_period;
  90. $found = false;
  91. $price_level = 1;
  92. if ($socid > 0) {
  93. $thirdpartytemp = new Societe($db);
  94. $thirdpartytemp->fetch($socid);
  95. //Load translation description and label
  96. if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE)) {
  97. $newlang = $thirdpartytemp->default_lang;
  98. if (!empty($newlang)) {
  99. $outputlangs = new Translate("", $conf);
  100. $outputlangs->setDefaultLang($newlang);
  101. $outdesc_trans = (!empty($object->multilangs[$outputlangs->defaultlang]["description"])) ? $object->multilangs[$outputlangs->defaultlang]["description"] : $object->description;
  102. $outlabel_trans = (!empty($object->multilangs[$outputlangs->defaultlang]["label"])) ? $object->multilangs[$outputlangs->defaultlang]["label"] : $object->label;
  103. } else {
  104. $outdesc_trans = $object->description;
  105. $outlabel_trans = $object->label;
  106. }
  107. }
  108. if (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) {
  109. $price_level = $thirdpartytemp->price_level;
  110. }
  111. }
  112. // Price by qty
  113. 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
  114. $sql = "SELECT price, unitprice, quantity, remise_percent";
  115. $sql .= " FROM ".MAIN_DB_PREFIX."product_price_by_qty";
  116. $sql .= " WHERE rowid = ".((int) $price_by_qty_rowid);
  117. $result = $db->query($sql);
  118. if ($result) {
  119. $objp = $db->fetch_object($result);
  120. if ($objp) {
  121. $found = true;
  122. $outprice_ht = price($objp->unitprice);
  123. $outprice_ttc = price($objp->unitprice * (1 + ($object->tva_tx / 100)));
  124. $outpricebasetype = $object->price_base_type;
  125. $outtva_tx_formated = price($object->tva_tx);
  126. $outtva_tx = price2num($object->tva_tx);
  127. $outdefault_vat_code = $object->default_vat_code;
  128. $outqty = $objp->quantity;
  129. $outdiscount = $objp->remise_percent;
  130. }
  131. }
  132. }
  133. // Multiprice (1 price per level)
  134. 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)
  135. $sql = "SELECT price, price_ttc, price_base_type,";
  136. $sql .= " tva_tx, default_vat_code"; // Vat rate and code will be used if PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL is on.
  137. $sql .= " FROM ".MAIN_DB_PREFIX."product_price ";
  138. $sql .= " WHERE fk_product = ".((int) $id);
  139. $sql .= " AND entity IN (".getEntity('productprice').")";
  140. $sql .= " AND price_level = ".((int) $price_level);
  141. $sql .= " ORDER BY date_price";
  142. $sql .= " DESC LIMIT 1";
  143. $result = $db->query($sql);
  144. if ($result) {
  145. $objp = $db->fetch_object($result);
  146. if ($objp) {
  147. $found = true;
  148. $outprice_ht = price($objp->price); // formated for langage user because is inserted into input field
  149. $outprice_ttc = price($objp->price_ttc); // formated for langage user because is inserted into input field
  150. $outpricebasetype = $objp->price_base_type;
  151. if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) {
  152. $outtva_tx_formated = price($objp->tva_tx); // formated for langage user because is inserted into input field
  153. $outtva_tx = price2num($objp->tva_tx); // international numeric
  154. $outdefault_vat_code = $objp->default_vat_code;
  155. } else {
  156. // The common and default behaviour.
  157. $outtva_tx_formated = price($object->tva_tx);
  158. $outtva_tx = price2num($object->tva_tx);
  159. $outdefault_vat_code = $object->default_vat_code;
  160. }
  161. }
  162. }
  163. }
  164. // Price by customer
  165. if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES) && !empty($socid)) {
  166. require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
  167. $prodcustprice = new Productcustomerprice($db);
  168. $filter = array('t.fk_product' => $object->id, 't.fk_soc' => $socid);
  169. $result = $prodcustprice->fetchAll('', '', 0, 0, $filter);
  170. if ($result) {
  171. if (count($prodcustprice->lines) > 0) {
  172. $found = true;
  173. $outprice_ht = price($prodcustprice->lines[0]->price);
  174. $outprice_ttc = price($prodcustprice->lines[0]->price_ttc);
  175. $outpricebasetype = $prodcustprice->lines[0]->price_base_type;
  176. $outtva_tx_formated = price($prodcustprice->lines[0]->tva_tx);
  177. $outtva_tx = price2num($prodcustprice->lines[0]->tva_tx);
  178. $outdefault_vat_code = $prodcustprice->lines[0]->default_vat_code;
  179. }
  180. }
  181. }
  182. if (!$found) {
  183. $outprice_ht = price($object->price);
  184. $outprice_ttc = price($object->price_ttc);
  185. $outpricebasetype = $object->price_base_type;
  186. $outtva_tx_formated = price($object->tva_tx);
  187. $outtva_tx = price2num($object->tva_tx);
  188. $outdefault_vat_code = $object->default_vat_code;
  189. }
  190. $outjson = array(
  191. 'ref' => $outref,
  192. 'label' => $outlabel,
  193. 'label_trans' => $outlabel_trans,
  194. 'desc' => $outdesc,
  195. 'desc_trans' => $outdesc_trans,
  196. 'type' => $outtype,
  197. 'price_ht' => $outprice_ht,
  198. 'price_ttc' => $outprice_ttc,
  199. 'pricebasetype' => $outpricebasetype,
  200. 'tva_tx_formated' => $outtva_tx_formated,
  201. 'tva_tx' => $outtva_tx,
  202. 'default_vat_code' => $outdefault_vat_code,
  203. 'qty' => $outqty,
  204. 'discount' => $outdiscount,
  205. 'mandatory_period' => $mandatory_period,
  206. 'array_options'=>$object->array_options);
  207. }
  208. echo json_encode($outjson);
  209. } else {
  210. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  211. $langs->loadLangs(array("main", "products"));
  212. top_httphead();
  213. if (empty($htmlname)) {
  214. print json_encode(array());
  215. return;
  216. }
  217. // Filter on the product to search can be:
  218. // Into an array with key $htmlname123 (we take first one found). Which page use this ?
  219. // Into a var with name $htmlname can be 'prodid', 'productid', ...
  220. $match = preg_grep('/('.preg_quote($htmlname, '/').'[0-9]+)/', array_keys($_GET));
  221. sort($match);
  222. $idprod = (empty($match[0]) ? '' : $match[0]); // Take first key found into GET array with matching $htmlname123
  223. if (GETPOST($htmlname, 'alpha') == '' && (!$idprod || !GETPOST($idprod, 'alpha'))) {
  224. print json_encode(array());
  225. return;
  226. }
  227. // When used from jQuery, the search term is added as GET param "term".
  228. $searchkey = (($idprod && GETPOST($idprod, 'alpha')) ? GETPOST($idprod, 'alpha') : (GETPOST($htmlname, 'alpha') ? GETPOST($htmlname, 'alpha') : ''));
  229. if (!isset($form) || !is_object($form)) {
  230. $form = new Form($db);
  231. }
  232. if (empty($mode) || $mode == 1) { // mode=1: customer
  233. $arrayresult = $form->select_produits_list("", $htmlname, $type, 0, $price_level, $searchkey, $status, $finished, $outjson, $socid, '1', 0, '', $hidepriceinlabel, $warehouseStatus, $status_purchase);
  234. } elseif ($mode == 2) { // mode=2: supplier
  235. $arrayresult = $form->select_produits_fournisseurs_list($socid, "", $htmlname, $type, "", $searchkey, $status, $outjson, 0, $alsoproductwithnosupplierprice);
  236. }
  237. $db->close();
  238. if ($outjson) {
  239. print json_encode($arrayresult);
  240. }
  241. }