products.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 (!defined('NOCSRFCHECK')) {
  40. define('NOCSRFCHECK', '1');
  41. }
  42. if (empty($_GET['keysearch']) && !defined('NOREQUIREHTML')) {
  43. define('NOREQUIREHTML', '1');
  44. }
  45. require '../../main.inc.php';
  46. $htmlname = GETPOST('htmlname', 'aZ09');
  47. $socid = GETPOST('socid', 'int');
  48. $type = GETPOST('type', 'int');
  49. $mode = GETPOST('mode', 'int');
  50. $status = ((GETPOST('status', 'int') >= 0) ? GETPOST('status', 'int') : - 1); // status buy when mode = customer , status purchase when mode = supplier
  51. $status_purchase = ((GETPOST('status_purchase', 'int') >= 0) ? GETPOST('status_purchase', 'int') : - 1); // status purchase when mode = customer
  52. $outjson = (GETPOST('outjson', 'int') ? GETPOST('outjson', 'int') : 0);
  53. $price_level = GETPOST('price_level', 'int');
  54. $action = GETPOST('action', 'aZ09');
  55. $id = GETPOST('id', 'int');
  56. $price_by_qty_rowid = GETPOST('pbq', 'int');
  57. $finished = GETPOST('finished', 'int');
  58. $alsoproductwithnosupplierprice = GETPOST('alsoproductwithnosupplierprice', 'int');
  59. $warehouseStatus = GETPOST('warehousestatus', 'alpha');
  60. $hidepriceinlabel = GETPOST('hidepriceinlabel', 'int');
  61. // Security check
  62. restrictedArea($user, 'produit|service', 0, 'product&product');
  63. /*
  64. * View
  65. */
  66. // print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
  67. // print_r($_GET);
  68. if ($action == 'fetch' && !empty($id)) {
  69. // action='fetch' is used to get product information on a product. So when action='fetch', id must be the product id.
  70. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  71. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  72. $outjson = array();
  73. $object = new Product($db);
  74. $ret = $object->fetch($id);
  75. if ($ret > 0) {
  76. $outref = $object->ref;
  77. $outlabel = $object->label;
  78. $outlabel_trans = '';
  79. $outdesc = $object->description;
  80. $outdesc_trans = '';
  81. $outtype = $object->type;
  82. $outprice_ht = null;
  83. $outprice_ttc = null;
  84. $outpricebasetype = null;
  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 (!empty($conf->global->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 = $object->tva_tx;
  126. $outdefault_vat_code = $object->default_vat_code;
  127. $outqty = $objp->quantity;
  128. $outdiscount = $objp->remise_percent;
  129. }
  130. }
  131. }
  132. // Multiprice (1 price per level)
  133. 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)
  134. $sql = "SELECT price, price_ttc, price_base_type,";
  135. $sql .= " tva_tx, default_vat_code"; // Vat rate and code will be used if PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL is on.
  136. $sql .= " FROM ".MAIN_DB_PREFIX."product_price ";
  137. $sql .= " WHERE fk_product = ".((int) $id);
  138. $sql .= " AND entity IN (".getEntity('productprice').")";
  139. $sql .= " AND price_level = ".((int) $price_level);
  140. $sql .= " ORDER BY date_price";
  141. $sql .= " DESC LIMIT 1";
  142. $result = $db->query($sql);
  143. if ($result) {
  144. $objp = $db->fetch_object($result);
  145. if ($objp) {
  146. $found = true;
  147. $outprice_ht = price($objp->price);
  148. $outprice_ttc = price($objp->price_ttc);
  149. $outpricebasetype = $objp->price_base_type;
  150. if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) {
  151. $outtva_tx = $objp->tva_tx;
  152. $outdefault_vat_code = $objp->default_vat_code;
  153. } else {
  154. // The common and default behaviour.
  155. $outtva_tx = $object->tva_tx;
  156. $outdefault_vat_code = $object->default_vat_code;
  157. }
  158. }
  159. }
  160. }
  161. // Price by customer
  162. if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES) && !empty($socid)) {
  163. require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
  164. $prodcustprice = new Productcustomerprice($db);
  165. $filter = array('t.fk_product' => $object->id, 't.fk_soc' => $socid);
  166. $result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
  167. if ($result) {
  168. if (count($prodcustprice->lines) > 0) {
  169. $found = true;
  170. $outprice_ht = price($prodcustprice->lines[0]->price);
  171. $outprice_ttc = price($prodcustprice->lines[0]->price_ttc);
  172. $outpricebasetype = $prodcustprice->lines[0]->price_base_type;
  173. $outtva_tx = $prodcustprice->lines[0]->tva_tx;
  174. $outdefault_vat_code = $prodcustprice->lines[0]->default_vat_code;
  175. }
  176. }
  177. }
  178. if (!$found) {
  179. $outprice_ht = price($object->price);
  180. $outprice_ttc = price($object->price_ttc);
  181. $outpricebasetype = $object->price_base_type;
  182. $outtva_tx = $object->tva_tx;
  183. $outdefault_vat_code = $object->default_vat_code;
  184. }
  185. $outjson = array(
  186. 'ref' => $outref,
  187. 'label' => $outlabel,
  188. 'label_trans' => $outlabel_trans,
  189. 'desc' => $outdesc,
  190. 'desc_trans' => $outdesc_trans,
  191. 'type' => $outtype,
  192. 'price_ht' => $outprice_ht,
  193. 'price_ttc' => $outprice_ttc,
  194. 'pricebasetype' => $outpricebasetype,
  195. 'tva_tx' => $outtva_tx,
  196. 'default_vat_code' => $outdefault_vat_code,
  197. 'qty' => $outqty,
  198. 'discount' => $outdiscount,
  199. 'mandatory_period' => $mandatory_period,
  200. 'array_options'=>$object->array_options);
  201. }
  202. echo json_encode($outjson);
  203. } else {
  204. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  205. $langs->loadLangs(array("main", "products"));
  206. top_httphead();
  207. if (empty($htmlname)) {
  208. print json_encode(array());
  209. return;
  210. }
  211. // Filter on the product to search can be:
  212. // Into an array with key $htmlname123 (we take first one found). Which page use this ?
  213. // Into a var with name $htmlname can be 'prodid', 'productid', ...
  214. $match = preg_grep('/('.preg_quote($htmlname, '/').'[0-9]+)/', array_keys($_GET));
  215. sort($match);
  216. $idprod = (empty($match[0]) ? '' : $match[0]); // Take first key found into GET array with matching $htmlname123
  217. if (GETPOST($htmlname, 'alpha') == '' && (!$idprod || !GETPOST($idprod, 'alpha'))) {
  218. print json_encode(array());
  219. return;
  220. }
  221. // When used from jQuery, the search term is added as GET param "term".
  222. $searchkey = (($idprod && GETPOST($idprod, 'alpha')) ? GETPOST($idprod, 'alpha') : (GETPOST($htmlname, 'alpha') ? GETPOST($htmlname, 'alpha') : ''));
  223. if (!isset($form) || !is_object($form)) {
  224. $form = new Form($db);
  225. }
  226. if (empty($mode) || $mode == 1) { // mode=1: customer
  227. $arrayresult = $form->select_produits_list("", $htmlname, $type, 0, $price_level, $searchkey, $status, $finished, $outjson, $socid, '1', 0, '', $hidepriceinlabel, $warehouseStatus, $status_purchase);
  228. } elseif ($mode == 2) { // mode=2: supplier
  229. $arrayresult = $form->select_produits_fournisseurs_list($socid, "", $htmlname, $type, "", $searchkey, $status, $outjson, 0, $alsoproductwithnosupplierprice);
  230. }
  231. $db->close();
  232. if ($outjson) {
  233. print json_encode($arrayresult);
  234. }
  235. }