dolibarr 21.0.0-beta
ajax.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2001-2004 Andreu Bisquerra <jove@bisquerra.com>
3 * Copyright (C) 2020 Thibault FOUCART <support@ptibogxiv.net>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
25if (!defined('NOTOKENRENEWAL')) {
26 define('NOTOKENRENEWAL', '1');
27}
28if (!defined('NOREQUIREMENU')) {
29 define('NOREQUIREMENU', '1');
30}
31if (!defined('NOREQUIREHTML')) {
32 define('NOREQUIREHTML', '1');
33}
34if (!defined('NOREQUIREAJAX')) {
35 define('NOREQUIREAJAX', '1');
36}
37if (!defined('NOBROWSERNOTIF')) {
38 define('NOBROWSERNOTIF', '1');
39}
40
41// Load Dolibarr environment
42require '../../main.inc.php'; // Load $user and permissions
43require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
44require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php";
45require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
54$category = GETPOST('category', 'alphanohtml'); // Can be id of category or 'supplements'
55$action = GETPOST('action', 'aZ09');
56$term = GETPOST('term', 'alpha');
57$id = GETPOSTINT('id');
58$search_start = GETPOSTINT('search_start');
59$search_limit = GETPOSTINT('search_limit');
60
61if (!$user->hasRight('takepos', 'run')) {
63}
64
65// Initialize a technical object to manage hooks. Note that conf->hooks_modules contains array of hooks
66$hookmanager->initHooks(array('takeposproductsearch')); // new context for product search hooks
67
68$pricelevel = 1; // default price level if PRODUIT_MULTIPRICES. TODO Get price level from thirdparty.
69
70
71
72/*
73 * View
74 */
75
76$thirdparty = new Societe($db);
77
78if ($action == 'getProducts' && $user->hasRight('takepos', 'run')) {
79 $tosell = GETPOSTISSET('tosell') ? GETPOSTINT('tosell') : '';
80 $limit = GETPOSTISSET('limit') ? GETPOSTINT('limit') : 0;
81 $offset = GETPOSTISSET('offset') ? GETPOSTINT('offset') : 0;
82
83 top_httphead('application/json');
84
85 // Search
86 if (GETPOSTINT('thirdpartyid') > 0) {
87 $result = $thirdparty->fetch(GETPOSTINT('thirdpartyid'));
88 if ($result > 0) {
89 $pricelevel = $thirdparty->price_level;
90 }
91 }
92
93 $object = new Categorie($db);
94 if ($category == "supplements") {
95 $category = getDolGlobalInt('TAKEPOS_SUPPLEMENTS_CATEGORY');
96 if (empty($category)) {
97 echo 'Error, the category to use for supplements is not defined. Go into setup of module TakePOS.';
98 exit;
99 }
100 }
101
102 $result = $object->fetch($category);
103 if ($result > 0) {
104 $filter = '';
105 if ($tosell != '') {
106 $filter = '(o.tosell:=:'.((int) $tosell).')';
107 }
108 $prods = $object->getObjectsInCateg("product", 0, $limit, $offset, getDolGlobalString('TAKEPOS_SORTPRODUCTFIELD'), 'ASC', $filter);
109 // Removed properties we don't need
110 $res = array();
111 if (is_array($prods) && count($prods) > 0) {
112 foreach ($prods as $prod) {
113 if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1) {
114 // remove products without stock
115 $prod->load_stock('nobatch,novirtual');
116 if ($prod->stock_warehouse[getDolGlobalString('CASHDESK_ID_WAREHOUSE'.$_SESSION['takeposterminal'])]->real <= 0) {
117 continue;
118 }
119 }
120 unset($prod->fields);
121 unset($prod->db);
122
123 $prod->price_formated = price(price2num(empty($prod->multiprices[$pricelevel]) ? $prod->price : $prod->multiprices[$pricelevel], 'MT'), 1, $langs, 1, -1, -1, $conf->currency);
124 $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);
125
126 $res[] = $prod;
127 }
128 }
129 echo json_encode($res);
130 } else {
131 echo 'Failed to load category with id='.dol_escape_htmltag($category);
132 }
133} elseif ($action == 'search' && $term != '' && $user->hasRight('takepos', 'run')) {
134 top_httphead('application/json');
135
136 // Search barcode into thirdparties. If found, it means we want to change thirdparties.
137 $result = $thirdparty->fetch('', '', '', $term);
138
139 if ($result && $thirdparty->id > 0) {
140 $rows = array();
141 $rows[] = array(
142 'rowid' => $thirdparty->id,
143 'name' => $thirdparty->name,
144 'barcode' => $thirdparty->barcode,
145 'object' => 'thirdparty'
146 );
147 echo json_encode($rows);
148 exit;
149 }
150
151 // Search
152 if (GETPOSTINT('thirdpartyid') > 0) {
153 $result = $thirdparty->fetch(GETPOSTINT('thirdpartyid'));
154 if ($result > 0) {
155 $pricelevel = $thirdparty->price_level;
156 }
157 }
158
159 // Define $filteroncategids, the filter on category ID if there is a Root category defined.
160 $filteroncategids = '';
161 if (getDolGlobalInt('TAKEPOS_ROOT_CATEGORY_ID') > 0) { // A root category is defined, we must filter on products inside this category tree
162 $object = new Categorie($db);
163 //$result = $object->fetch($conf->global->TAKEPOS_ROOT_CATEGORY_ID);
164 $arrayofcateg = $object->get_full_arbo('product', getDolGlobalInt('TAKEPOS_ROOT_CATEGORY_ID'), 1);
165 if (is_array($arrayofcateg) && count($arrayofcateg) > 0) {
166 foreach ($arrayofcateg as $val) {
167 $filteroncategids .= ($filteroncategids ? ', ' : '').$val['id'];
168 }
169 }
170 }
171
172 $barcode_rules = getDolGlobalString('TAKEPOS_BARCODE_RULE_TO_INSERT_PRODUCT');
173 if (isModEnabled('barcode') && !empty($barcode_rules)) {
174 $barcode_rules_list = array();
175
176 // get barcode rules
177 $barcode_char_nb = 0;
178 $barcode_rules_arr = explode('+', $barcode_rules);
179 foreach ($barcode_rules_arr as $barcode_rules_values) {
180 $barcode_rules_values_arr = explode(':', $barcode_rules_values);
181 if (count($barcode_rules_values_arr) == 2) {
182 $char_nb = intval($barcode_rules_values_arr[1]);
183 $barcode_rules_list[] = array('code' => $barcode_rules_values_arr[0], 'char_nb' => $char_nb);
184 $barcode_char_nb += $char_nb;
185 }
186 }
187
188 $barcode_value_list = array();
189 $barcode_offset = 0;
190 $barcode_length = dol_strlen($term);
191 if ($barcode_length == $barcode_char_nb) {
192 $rows = array();
193
194 // split term with barcode rules
195 foreach ($barcode_rules_list as $barcode_rule_arr) {
196 $code = $barcode_rule_arr['code'];
197 $char_nb = $barcode_rule_arr['char_nb'];
198 $barcode_value_list[$code] = substr($term, $barcode_offset, $char_nb);
199 $barcode_offset += $char_nb;
200 }
201
202 if (isset($barcode_value_list['ref'])) {
203 // search product from reference
204 $sql = "SELECT rowid, ref, label, tosell, tobuy, barcode, price, price_ttc";
205 $sql .= " FROM " . $db->prefix() . "product as p";
206 $sql .= " WHERE entity IN (" . getEntity('product') . ")";
207 $sql .= " AND ref = '" . $db->escape($barcode_value_list['ref']) . "'";
208 if ($filteroncategids) {
209 $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)."))";
210 }
211 $sql .= " AND tosell = 1";
212 $sql .= " AND (barcode IS NULL OR barcode <> '" . $db->escape($term) . "')";
213
214 $resql = $db->query($sql);
215 if ($resql && $db->num_rows($resql) == 1) {
216 if ($obj = $db->fetch_object($resql)) {
217 $qty = 1;
218 if (isset($barcode_value_list['qu'])) {
219 $qty_str = $barcode_value_list['qu'];
220 if (isset($barcode_value_list['qd'])) {
221 $qty_str .= '.' . $barcode_value_list['qd'];
222 }
223 $qty = (float) $qty_str;
224 }
225
226 $objProd = new Product($db);
227 $objProd->fetch($obj->rowid);
228
229 $ig = '../public/theme/common/nophoto.png';
230 if (!getDolGlobalString('TAKEPOS_HIDE_PRODUCT_IMAGES')) {
231 $image = $objProd->show_photos('product', $conf->product->multidir_output[$objProd->entity], 'small', 1);
232
233 $match = array();
234 preg_match('@src="([^"]+)"@', $image, $match);
235 $file = array_pop($match);
236
237 if ($file != '') {
238 if (!defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
239 $ig = $file.'&cache=1';
240 } else {
241 $ig = $file.'&cache=1&publictakepos=1&modulepart=product';
242 }
243 }
244 }
245
246 $rows[] = array(
247 'rowid' => $obj->rowid,
248 'ref' => $obj->ref,
249 'label' => $obj->label,
250 'tosell' => $obj->tosell,
251 'tobuy' => $obj->tobuy,
252 'barcode' => $term, // there is only one product matches the barcode rule and so the term is considered as the barcode of this product
253 'price' => empty($objProd->multiprices[$pricelevel]) ? $obj->price : $objProd->multiprices[$pricelevel],
254 'price_ttc' => empty($objProd->multiprices_ttc[$pricelevel]) ? $obj->price_ttc : $objProd->multiprices_ttc[$pricelevel],
255 'object' => 'product',
256 'img' => $ig,
257 'qty' => $qty,
258 );
259 }
260 $db->free($resql);
261 }
262 }
263
264 if (count($rows) == 1) {
265 echo json_encode($rows);
266 exit();
267 }
268 }
269 }
270
271 $sql = 'SELECT p.rowid, p.ref, p.label, p.tosell, p.tobuy, p.barcode, p.price, p.price_ttc' ;
272 if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1) {
273 if (getDolGlobalInt('CASHDESK_ID_WAREHOUSE'.$_SESSION['takeposterminal'])) {
274 $sql .= ', ps.reel';
275 } else {
276 $sql .= ', SUM(ps.reel) as reel';
277 }
278 }
279 /* this will be possible when field archive will be supported into llx_product_price
280 if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
281 $sql .= ', pp.price_level, pp.price as multiprice_ht, pp.price_ttc as multiprice_ttc';
282 }*/
283 // Add fields from hooks
284 $parameters = array();
285 $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters);
286 if ($reshook >= 0) {
287 $sql .= $hookmanager->resPrint;
288 }
289
290 $sql .= ' FROM '.MAIN_DB_PREFIX.'product as p';
291 /* this will be possible when field archive will be supported into llx_product_price
292 if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
293 $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);
294 $sql .= " AND archive = 0";
295 }*/
296 if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1) {
297 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock as ps';
298 $sql .= ' ON (p.rowid = ps.fk_product';
299 if (getDolGlobalString('CASHDESK_ID_WAREHOUSE'.$_SESSION['takeposterminal'])) {
300 $sql .= " AND ps.fk_entrepot = ".((int) getDolGlobalInt("CASHDESK_ID_WAREHOUSE".$_SESSION['takeposterminal']));
301 }
302 $sql .= ')';
303 }
304
305 // Add tables from hooks
306 $parameters = array();
307 $reshook = $hookmanager->executeHooks('printFieldListTables', $parameters);
308 if ($reshook >= 0) {
309 $sql .= $hookmanager->resPrint;
310 }
311
312 $sql .= ' WHERE p.entity IN ('.getEntity('product').')';
313 if ($filteroncategids) {
314 $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).'))';
315 }
316 $sql .= ' AND p.tosell = 1';
317 if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1 && getDolGlobalInt('CASHDESK_ID_WAREHOUSE'.$_SESSION['takeposterminal'])) {
318 $sql .= ' AND ps.reel > 0';
319 }
320 $sql .= natural_search(array('ref', 'label', 'barcode'), $term);
321 // Add where from hooks
322 $parameters = array();
323 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters);
324 if ($reshook >= 0) {
325 $sql .= $hookmanager->resPrint;
326 }
327
328 if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1 && !getDolGlobalInt('CASHDESK_ID_WAREHOUSE'.$_SESSION['takeposterminal'])) {
329 $sql .= ' GROUP BY p.rowid, p.ref, p.label, p.tosell, p.tobuy, p.barcode, p.price, p.price_ttc';
330 // Add fields from hooks
331 $parameters = array();
332 $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters);
333 if ($reshook >= 0) {
334 $sql .= $hookmanager->resPrint;
335 }
336 $sql .= ' HAVING SUM(ps.reel) > 0';
337 }
338
339 // load only one page of products
340 $sql.= $db->plimit($search_limit, $search_start);
341
342 $resql = $db->query($sql);
343 if ($resql) {
344 $rows = array();
345
346 while ($obj = $db->fetch_object($resql)) {
347 $objProd = new Product($db);
348 $objProd->fetch($obj->rowid);
349 $image = $objProd->show_photos('product', $conf->product->multidir_output[$objProd->entity], 'small', 1);
350
351 $match = array();
352 preg_match('@src="([^"]+)"@', $image, $match);
353 $file = array_pop($match);
354
355 if ($file == "") {
356 $ig = '../public/theme/common/nophoto.png';
357 } else {
358 if (!defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
359 $ig = $file.'&cache=1';
360 } else {
361 $ig = $file.'&cache=1&publictakepos=1&modulepart=product';
362 }
363 }
364
365 $row = array(
366 'rowid' => $obj->rowid,
367 'ref' => $obj->ref,
368 'label' => $obj->label,
369 'tosell' => $obj->tosell,
370 'tobuy' => $obj->tobuy,
371 'barcode' => $obj->barcode,
372 'price' => empty($objProd->multiprices[$pricelevel]) ? $obj->price : $objProd->multiprices[$pricelevel],
373 'price_ttc' => empty($objProd->multiprices_ttc[$pricelevel]) ? $obj->price_ttc : $objProd->multiprices_ttc[$pricelevel],
374 'object' => 'product',
375 'img' => $ig,
376 'qty' => 1,
377 'price_formated' => price(price2num(empty($objProd->multiprices[$pricelevel]) ? $obj->price : $objProd->multiprices[$pricelevel], 'MT'), 1, $langs, 1, -1, -1, $conf->currency),
378 '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)
379 );
380 // Add entries to row from hooks
381 $parameters=array();
382 $parameters['row'] = $row;
383 $parameters['obj'] = $obj;
384 $reshook = $hookmanager->executeHooks('completeAjaxReturnArray', $parameters);
385 if ($reshook > 0) {
386 // replace
387 if (count($hookmanager->resArray)) {
388 $row = $hookmanager->resArray;
389 } else {
390 $row = array();
391 }
392 $rows[] = $row;
393 } else {
394 // add
395 if (count($hookmanager->resArray)) {
396 $rows[] = $hookmanager->resArray;
397 }
398 $rows[] = $row;
399 }
400 }
401
402 echo json_encode($rows);
403 } else {
404 echo 'Failed to search product : '.$db->lasterror();
405 }
406} elseif ($action == "opendrawer" && $term != '' && $user->hasRight('takepos', 'run')) {
407 top_httphead('application/html');
408 require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
409 $printer = new dolReceiptPrinter($db);
410 // check printer for terminal
411 if (getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term) > 0) {
412 $printer->initPrinter(getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term));
413 // open cashdrawer
414 if ($printer->getPrintConnector()) {
415 $printer->pulse();
416 $printer->close();
417 } else {
418 print 'Failed to init printer with ID='.getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term);
419 }
420 }
421} elseif ($action == "printinvoiceticket" && $term != '' && $id > 0 && $user->hasRight('takepos', 'run') && $user->hasRight('facture', 'lire')) {
422 top_httphead('application/html');
423
424 require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
425 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
426 $printer = new dolReceiptPrinter($db);
427 // check printer for terminal
428 if ((getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term) > 0 || getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") && getDolGlobalInt('TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term) > 0) {
429 $object = new Facture($db);
430 $object->fetch($id);
431 $ret = $printer->sendToPrinter($object, getDolGlobalString('TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term), getDolGlobalString('TAKEPOS_PRINTER_TO_USE'.$term));
432 }
433} elseif ($action == 'getInvoice' && $user->hasRight('takepos', 'run')) {
434 top_httphead('application/json');
435
436 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
437
438 $object = new Facture($db);
439 if ($id > 0) {
440 $object->fetch($id);
441 }
442
443 echo json_encode($object);
444} elseif ($action == 'thecheck' && $user->hasRight('takepos', 'run')) {
445 top_httphead('application/html');
446
447 $place = GETPOST('place', 'alpha');
448 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
449 require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
450
451 $object = new Facture($db);
452
453 $printer = new dolReceiptPrinter($db);
454 $printer->sendToPrinter($object, getDolGlobalString('TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term), getDolGlobalString('TAKEPOS_PRINTER_TO_USE'.$term));
455}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class to manage categories.
Class to manage invoices.
Class to manage products or services.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Receipt Printers.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
natural_search($fields, $value, $mode=0, $nofirstand=0)
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.