products.php 12 KB

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