ajax.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. /* Copyright (C) 2001-2004 Andreu Bisquerra <jove@bisquerra.com>
  3. * Copyright (C) 2020 Thibault FOUCART <support@ptibogxiv.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/takepos/ajax/ajax.php
  20. * \brief Ajax search component for TakePos. It search products of a category.
  21. */
  22. if (!defined('NOTOKENRENEWAL')) {
  23. define('NOTOKENRENEWAL', '1');
  24. }
  25. if (!defined('NOREQUIREMENU')) {
  26. define('NOREQUIREMENU', '1');
  27. }
  28. if (!defined('NOREQUIREHTML')) {
  29. define('NOREQUIREHTML', '1');
  30. }
  31. if (!defined('NOREQUIREAJAX')) {
  32. define('NOREQUIREAJAX', '1');
  33. }
  34. if (!defined('NOBROWSERNOTIF')) {
  35. define('NOBROWSERNOTIF', '1');
  36. }
  37. // Load Dolibarr environment
  38. require '../../main.inc.php'; // Load $user and permissions
  39. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  40. require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php";
  41. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  42. $category = GETPOST('category', 'alphanohtml'); // Can be id of category or 'supplements'
  43. $action = GETPOST('action', 'aZ09');
  44. $term = GETPOST('term', 'alpha');
  45. $id = GETPOST('id', 'int');
  46. $search_start = GETPOST('search_start', 'int');
  47. $search_limit = GETPOST('search_limit', 'int');
  48. if (!$user->hasRight('takepos', 'run')) {
  49. accessforbidden();
  50. }
  51. // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array of hooks
  52. $hookmanager->initHooks(array('takeposproductsearch')); // new context for product search hooks
  53. $pricelevel = 1; // default price level if PRODUIT_MULTIPRICES. TODO Get price level from thirdparty.
  54. /*
  55. * View
  56. */
  57. $thirdparty = new Societe($db);
  58. if ($action == 'getProducts') {
  59. $tosell = GETPOSTISSET('tosell') ? GETPOST('tosell', 'int') : '';
  60. $limit = GETPOSTISSET('limit') ? GETPOST('limit', 'int') : 0;
  61. $offset = GETPOSTISSET('offset') ? GETPOST('offset', 'int') : 0;
  62. top_httphead('application/json');
  63. // Search
  64. if (GETPOSTINT('thirdpartyid') > 0) {
  65. $result = $thirdparty->fetch(GETPOSTINT('thirdpartyid'));
  66. if ($result > 0) {
  67. $pricelevel = $thirdparty->price_level;
  68. }
  69. }
  70. $object = new Categorie($db);
  71. if ($category == "supplements") {
  72. $category = getDolGlobalInt('TAKEPOS_SUPPLEMENTS_CATEGORY');
  73. if (empty($category)) {
  74. echo 'Error, the category to use for supplements is not defined. Go into setup of module TakePOS.';
  75. exit;
  76. }
  77. }
  78. $result = $object->fetch($category);
  79. if ($result > 0) {
  80. $filter = array();
  81. if ($tosell != '') {
  82. $filter = array('customsql' => 'o.tosell = '.((int) $tosell));
  83. }
  84. $prods = $object->getObjectsInCateg("product", 0, $limit, $offset, getDolGlobalString('TAKEPOS_SORTPRODUCTFIELD'), 'ASC', $filter);
  85. // Removed properties we don't need
  86. $res = array();
  87. if (is_array($prods) && count($prods) > 0) {
  88. foreach ($prods as $prod) {
  89. if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1) {
  90. // remove products without stock
  91. $prod->load_stock('nobatch,novirtual');
  92. if ($prod->stock_warehouse[getDolGlobalString('CASHDESK_ID_WAREHOUSE'.$_SESSION['takeposterminal'])]->real <= 0) {
  93. continue;
  94. }
  95. }
  96. unset($prod->fields);
  97. unset($prod->db);
  98. $prod->price_formated = price(price2num(empty($prod->multiprices[$pricelevel]) ? $prod->price : $prod->multiprices[$pricelevel], 'MT'), 1, $langs, 1, -1, -1, $conf->currency);
  99. $prod->price_ttc_formated = price(price2num(empty($prod->multiprices_ttc[$pricelevel]) ? $prod->price_ttc : $prod->multiprices_ttc[$pricelevel], 'MT'), 1, $langs, 1, -1, -1, $conf->currency);
  100. $res[] = $prod;
  101. }
  102. }
  103. echo json_encode($res);
  104. } else {
  105. echo 'Failed to load category with id='.dol_escape_htmltag($category);
  106. }
  107. } elseif ($action == 'search' && $term != '') {
  108. top_httphead('application/json');
  109. // Search barcode into thirdparties. If found, it means we want to change thirdparties.
  110. $result = $thirdparty->fetch('', '', '', $term);
  111. if ($result && $thirdparty->id > 0) {
  112. $rows = array();
  113. $rows[] = array(
  114. 'rowid' => $thirdparty->id,
  115. 'name' => $thirdparty->name,
  116. 'barcode' => $thirdparty->barcode,
  117. 'object' => 'thirdparty'
  118. );
  119. echo json_encode($rows);
  120. exit;
  121. }
  122. // Search
  123. if (GETPOSTINT('thirdpartyid') > 0) {
  124. $result = $thirdparty->fetch(GETPOSTINT('thirdpartyid'));
  125. if ($result > 0) {
  126. $pricelevel = $thirdparty->price_level;
  127. }
  128. }
  129. // Define $filteroncategids, the filter on category ID if there is a Root category defined.
  130. $filteroncategids = '';
  131. if ($conf->global->TAKEPOS_ROOT_CATEGORY_ID > 0) { // A root category is defined, we must filter on products inside this category tree
  132. $object = new Categorie($db);
  133. //$result = $object->fetch($conf->global->TAKEPOS_ROOT_CATEGORY_ID);
  134. $arrayofcateg = $object->get_full_arbo('product', $conf->global->TAKEPOS_ROOT_CATEGORY_ID, 1);
  135. if (is_array($arrayofcateg) && count($arrayofcateg) > 0) {
  136. foreach ($arrayofcateg as $val) {
  137. $filteroncategids .= ($filteroncategids ? ', ' : '').$val['id'];
  138. }
  139. }
  140. }
  141. $barcode_rules = getDolGlobalString('TAKEPOS_BARCODE_RULE_TO_INSERT_PRODUCT');
  142. if (isModEnabled('barcode') && !empty($barcode_rules)) {
  143. $barcode_rules_list = array();
  144. // get barcode rules
  145. $barcode_char_nb = 0;
  146. $barcode_rules_arr = explode('+', $barcode_rules);
  147. foreach ($barcode_rules_arr as $barcode_rules_values) {
  148. $barcode_rules_values_arr = explode(':', $barcode_rules_values);
  149. if (count($barcode_rules_values_arr) == 2) {
  150. $char_nb = intval($barcode_rules_values_arr[1]);
  151. $barcode_rules_list[] = array('code' => $barcode_rules_values_arr[0], 'char_nb' => $char_nb);
  152. $barcode_char_nb += $char_nb;
  153. }
  154. }
  155. $barcode_value_list = array();
  156. $barcode_offset = 0;
  157. $barcode_length = dol_strlen($term);
  158. if ($barcode_length == $barcode_char_nb) {
  159. $rows = array();
  160. // split term with barcode rules
  161. foreach ($barcode_rules_list as $barcode_rule_arr) {
  162. $code = $barcode_rule_arr['code'];
  163. $char_nb = $barcode_rule_arr['char_nb'];
  164. $barcode_value_list[$code] = substr($term, $barcode_offset, $char_nb);
  165. $barcode_offset += $char_nb;
  166. }
  167. if (isset($barcode_value_list['ref'])) {
  168. // search product from reference
  169. $sql = "SELECT rowid, ref, label, tosell, tobuy, barcode, price, price_ttc";
  170. $sql .= " FROM " . $db->prefix() . "product as p";
  171. $sql .= " WHERE entity IN (" . getEntity('product') . ")";
  172. $sql .= " AND ref = '" . $db->escape($barcode_value_list['ref']) . "'";
  173. if ($filteroncategids) {
  174. $sql .= " AND EXISTS (SELECT cp.fk_product FROM " . $db->prefix() . "categorie_product as cp WHERE cp.fk_product = p.rowid AND cp.fk_categorie IN (".$db->sanitize($filteroncategids)."))";
  175. }
  176. $sql .= " AND tosell = 1";
  177. $sql .= " AND (barcode IS NULL OR barcode <> '" . $db->escape($term) . "')";
  178. $resql = $db->query($sql);
  179. if ($resql && $db->num_rows($resql) == 1) {
  180. if ($obj = $db->fetch_object($resql)) {
  181. $qty = 1;
  182. if (isset($barcode_value_list['qu'])) {
  183. $qty_str = $barcode_value_list['qu'];
  184. if (isset($barcode_value_list['qd'])) {
  185. $qty_str .= '.' . $barcode_value_list['qd'];
  186. }
  187. $qty = floatval($qty_str);
  188. }
  189. $objProd = new Product($db);
  190. $objProd->fetch($obj->rowid);
  191. $ig = '../public/theme/common/nophoto.png';
  192. if (!getDolGlobalString('TAKEPOS_HIDE_PRODUCT_IMAGES')) {
  193. $image = $objProd->show_photos('product', $conf->product->multidir_output[$objProd->entity], 'small', 1);
  194. $match = array();
  195. preg_match('@src="([^"]+)"@', $image, $match);
  196. $file = array_pop($match);
  197. if ($file != '') {
  198. if (!defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
  199. $ig = $file.'&cache=1';
  200. } else {
  201. $ig = $file.'&cache=1&publictakepos=1&modulepart=product';
  202. }
  203. }
  204. }
  205. $rows[] = array(
  206. 'rowid' => $obj->rowid,
  207. 'ref' => $obj->ref,
  208. 'label' => $obj->label,
  209. 'tosell' => $obj->tosell,
  210. 'tobuy' => $obj->tobuy,
  211. 'barcode' => $obj->barcode,
  212. 'price' => empty($objProd->multiprices[$pricelevel]) ? $obj->price : $objProd->multiprices[$pricelevel],
  213. 'price_ttc' => empty($objProd->multiprices_ttc[$pricelevel]) ? $obj->price_ttc : $objProd->multiprices_ttc[$pricelevel],
  214. 'object' => 'product',
  215. 'img' => $ig,
  216. 'qty' => $qty,
  217. );
  218. }
  219. $db->free($resql);
  220. }
  221. }
  222. if (count($rows) == 1) {
  223. echo json_encode($rows);
  224. exit();
  225. }
  226. }
  227. }
  228. $sql = 'SELECT p.rowid, p.ref, p.label, p.tosell, p.tobuy, p.barcode, p.price, p.price_ttc' ;
  229. if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1) {
  230. if (getDolGlobalInt('CASHDESK_ID_WAREHOUSE'.$_SESSION['takeposterminal'])) {
  231. $sql .= ', ps.reel';
  232. } else {
  233. $sql .= ', SUM(ps.reel) as reel';
  234. }
  235. }
  236. /* this will be possible when field archive will be supported into llx_product_price
  237. if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
  238. $sql .= ', pp.price_level, pp.price as multiprice_ht, pp.price_ttc as multiprice_ttc';
  239. }*/
  240. // Add fields from hooks
  241. $parameters = array();
  242. $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters);
  243. if ($reshook >= 0) {
  244. $sql .= $hookmanager->resPrint;
  245. }
  246. $sql .= ' FROM '.MAIN_DB_PREFIX.'product as p';
  247. /* this will be possible when field archive will be supported into llx_product_price
  248. if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
  249. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_price as pp ON pp.fk_product = p.rowid AND pp.entity = ".((int) $conf->entity)." AND pp.price_level = ".((int) $pricelevel);
  250. $sql .= " AND archive = 0";
  251. }*/
  252. if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1) {
  253. $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'product_stock as ps';
  254. $sql .= ' ON (p.rowid = ps.fk_product';
  255. if (getDolGlobalString('CASHDESK_ID_WAREHOUSE'.$_SESSION['takeposterminal'])) {
  256. $sql .= " AND ps.fk_entrepot = ".((int) getDolGlobalInt("CASHDESK_ID_WAREHOUSE".$_SESSION['takeposterminal']));
  257. }
  258. $sql .= ')';
  259. }
  260. // Add tables from hooks
  261. $parameters = array();
  262. $reshook = $hookmanager->executeHooks('printFieldListTables', $parameters);
  263. if ($reshook >= 0) {
  264. $sql .= $hookmanager->resPrint;
  265. }
  266. $sql .= ' WHERE p.entity IN ('.getEntity('product').')';
  267. if ($filteroncategids) {
  268. $sql .= ' AND EXISTS (SELECT cp.fk_product FROM '.MAIN_DB_PREFIX.'categorie_product as cp WHERE cp.fk_product = p.rowid AND cp.fk_categorie IN ('.$db->sanitize($filteroncategids).'))';
  269. }
  270. $sql .= ' AND p.tosell = 1';
  271. if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1 && getDolGlobalInt('CASHDESK_ID_WAREHOUSE'.$_SESSION['takeposterminal'])) {
  272. $sql .= ' AND ps.reel > 0';
  273. }
  274. $sql .= natural_search(array('ref', 'label', 'barcode'), $term);
  275. // Add where from hooks
  276. $parameters = array();
  277. $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters);
  278. if ($reshook >= 0) {
  279. $sql .= $hookmanager->resPrint;
  280. }
  281. if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1 && !getDolGlobalInt('CASHDESK_ID_WAREHOUSE'.$_SESSION['takeposterminal'])) {
  282. $sql .= ' GROUP BY p.rowid, p.ref, p.label, p.tosell, p.tobuy, p.barcode, p.price, p.price_ttc';
  283. // Add fields from hooks
  284. $parameters = array();
  285. $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters);
  286. if ($reshook >= 0) {
  287. $sql .= $hookmanager->resPrint;
  288. }
  289. $sql .= ' HAVING SUM(ps.reel) > 0';
  290. }
  291. // load only one page of products
  292. $sql.= $db->plimit($search_limit, $search_start);
  293. $resql = $db->query($sql);
  294. if ($resql) {
  295. $rows = array();
  296. while ($obj = $db->fetch_object($resql)) {
  297. $objProd = new Product($db);
  298. $objProd->fetch($obj->rowid);
  299. $image = $objProd->show_photos('product', $conf->product->multidir_output[$objProd->entity], 'small', 1);
  300. $match = array();
  301. preg_match('@src="([^"]+)"@', $image, $match);
  302. $file = array_pop($match);
  303. if ($file == "") {
  304. $ig = '../public/theme/common/nophoto.png';
  305. } else {
  306. if (!defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
  307. $ig = $file.'&cache=1';
  308. } else {
  309. $ig = $file.'&cache=1&publictakepos=1&modulepart=product';
  310. }
  311. }
  312. $row = array(
  313. 'rowid' => $obj->rowid,
  314. 'ref' => $obj->ref,
  315. 'label' => $obj->label,
  316. 'tosell' => $obj->tosell,
  317. 'tobuy' => $obj->tobuy,
  318. 'barcode' => $obj->barcode,
  319. 'price' => empty($objProd->multiprices[$pricelevel]) ? $obj->price : $objProd->multiprices[$pricelevel],
  320. 'price_ttc' => empty($objProd->multiprices_ttc[$pricelevel]) ? $obj->price_ttc : $objProd->multiprices_ttc[$pricelevel],
  321. 'object' => 'product',
  322. 'img' => $ig,
  323. 'qty' => 1,
  324. 'price_formated' => price(price2num(empty($objProd->multiprices[$pricelevel]) ? $obj->price : $objProd->multiprices[$pricelevel], 'MT'), 1, $langs, 1, -1, -1, $conf->currency),
  325. 'price_ttc_formated' => price(price2num(empty($objProd->multiprices_ttc[$pricelevel]) ? $obj->price_ttc : $objProd->multiprices_ttc[$pricelevel], 'MT'), 1, $langs, 1, -1, -1, $conf->currency)
  326. );
  327. // Add entries to row from hooks
  328. $parameters=array();
  329. $parameters['row'] = $row;
  330. $parameters['obj'] = $obj;
  331. $reshook = $hookmanager->executeHooks('completeAjaxReturnArray', $parameters);
  332. if ($reshook > 0) {
  333. // replace
  334. if (count($hookmanager->resArray)) {
  335. $row = $hookmanager->resArray;
  336. } else {
  337. $row = array();
  338. }
  339. } else {
  340. // add
  341. if (count($hookmanager->resArray)) {
  342. $rows[] = $hookmanager->resArray;
  343. }
  344. $rows[] = $row;
  345. }
  346. }
  347. echo json_encode($rows);
  348. } else {
  349. echo 'Failed to search product : '.$db->lasterror();
  350. }
  351. } elseif ($action == "opendrawer" && $term != '') {
  352. require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
  353. $printer = new dolReceiptPrinter($db);
  354. // check printer for terminal
  355. if (getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term) > 0) {
  356. $printer->initPrinter(getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term));
  357. // open cashdrawer
  358. $printer->pulse();
  359. $printer->close();
  360. }
  361. } elseif ($action == "printinvoiceticket" && $term != '' && $id > 0 && $user->hasRight('facture', 'lire')) {
  362. require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
  363. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  364. $printer = new dolReceiptPrinter($db);
  365. // check printer for terminal
  366. if ((getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term) > 0 || getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") && getDolGlobalInt('TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term) > 0) {
  367. $object = new Facture($db);
  368. $object->fetch($id);
  369. $ret = $printer->sendToPrinter($object, getDolGlobalString('TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term), getDolGlobalString('TAKEPOS_PRINTER_TO_USE'.$term));
  370. }
  371. } elseif ($action == 'getInvoice') {
  372. top_httphead('application/json');
  373. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  374. $object = new Facture($db);
  375. if ($id > 0) {
  376. $object->fetch($id);
  377. }
  378. echo json_encode($object);
  379. } elseif ($action == 'thecheck') {
  380. $place = GETPOST('place', 'alpha');
  381. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  382. require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
  383. $object = new Facture($db);
  384. $printer = new dolReceiptPrinter($db);
  385. $printer->sendToPrinter($object, getDolGlobalString('TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term), getDolGlobalString('TAKEPOS_PRINTER_TO_USE'.$term));
  386. }