dolibarr  16.0.5
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  *
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 
24 if (!defined('NOCSRFCHECK')) {
25  define('NOCSRFCHECK', '1');
26 }
27 if (!defined('NOTOKENRENEWAL')) {
28  define('NOTOKENRENEWAL', '1');
29 }
30 if (!defined('NOREQUIREMENU')) {
31  define('NOREQUIREMENU', '1');
32 }
33 if (!defined('NOREQUIREHTML')) {
34  define('NOREQUIREHTML', '1');
35 }
36 if (!defined('NOREQUIREAJAX')) {
37  define('NOREQUIREAJAX', '1');
38 }
39 if (!defined('NOBROWSERNOTIF')) {
40  define('NOBROWSERNOTIF', '1');
41 }
42 
43 require '../../main.inc.php'; // Load $user and permissions
44 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
45 require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php";
46 
47 $category = GETPOST('category', 'alphanohtml'); // Can be id of category or 'supplements'
48 $action = GETPOST('action', 'aZ09');
49 $term = GETPOST('term', 'alpha');
50 $id = GETPOST('id', 'int');
51 $search_start = GETPOST('search_start', 'int');
52 $search_limit = GETPOST('search_limit', 'int');
53 
54 if (empty($user->rights->takepos->run)) {
56 }
57 
58 // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array of hooks
59 $hookmanager->initHooks(array('takeposproductsearch')); // new context for product search hooks
60 
61 /*
62  * View
63  */
64 
65 if ($action == 'getProducts') {
66  $object = new Categorie($db);
67  if ($category == "supplements") {
68  $category = getDolGlobalInt('TAKEPOS_SUPPLEMENTS_CATEGORY');
69  }
70  $result = $object->fetch($category);
71  if ($result > 0) {
72  $prods = $object->getObjectsInCateg("product", 0, 0, 0, getDolGlobalString('TAKEPOS_SORTPRODUCTFIELD'), 'ASC');
73  // Removed properties we don't need
74  $res = array();
75  if (is_array($prods) && count($prods) > 0) {
76  foreach ($prods as $prod) {
77  if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1) {
78  // remove products without stock
79  $prod->load_stock('nobatch,novirtual');
80  if ($prod->stock_warehouse[getDolGlobalString('CASHDESK_ID_WAREHOUSE'.$_SESSION['takeposterminal'])]->real <= 0) {
81  continue;
82  }
83  }
84  unset($prod->fields);
85  unset($prod->db);
86  $res[] = $prod;
87  }
88  }
89  echo json_encode($res);
90  } else {
91  echo 'Failed to load category with id='.$category;
92  }
93 } elseif ($action == 'search' && $term != '') {
94  // Change thirdparty with barcode
95  require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
96 
97  $thirdparty = new Societe($db);
98  $result = $thirdparty->fetch('', '', '', $term);
99 
100  if ($result && $thirdparty->id > 0) {
101  $rows = array();
102  $rows[] = array(
103  'rowid' => $thirdparty->id,
104  'name' => $thirdparty->name,
105  'barcode' => $thirdparty->barcode,
106  'object' => 'thirdparty'
107  );
108  echo json_encode($rows);
109  exit;
110  }
111 
112  // Define $filteroncategids, the filter on category ID if there is a Root category defined.
113  $filteroncategids = '';
114  if ($conf->global->TAKEPOS_ROOT_CATEGORY_ID > 0) { // A root category is defined, we must filter on products inside this category tree
115  $object = new Categorie($db);
116  //$result = $object->fetch($conf->global->TAKEPOS_ROOT_CATEGORY_ID);
117  $arrayofcateg = $object->get_full_arbo('product', $conf->global->TAKEPOS_ROOT_CATEGORY_ID, 1);
118  if (is_array($arrayofcateg) && count($arrayofcateg) > 0) {
119  foreach ($arrayofcateg as $val) {
120  $filteroncategids .= ($filteroncategids ? ', ' : '').$val['id'];
121  }
122  }
123  }
124 
125  $barcode_rules = getDolGlobalString('TAKEPOS_BARCODE_RULE_TO_INSERT_PRODUCT');
126  if (isModEnabled('barcode') && !empty($barcode_rules)) {
127  $barcode_rules_list = array();
128 
129  // get barcode rules
130  $barcode_char_nb = 0;
131  $barcode_rules_arr = explode('+', $barcode_rules);
132  foreach ($barcode_rules_arr as $barcode_rules_values) {
133  $barcode_rules_values_arr = explode(':', $barcode_rules_values);
134  if (count($barcode_rules_values_arr) == 2) {
135  $char_nb = intval($barcode_rules_values_arr[1]);
136  $barcode_rules_list[] = array('code' => $barcode_rules_values_arr[0], 'char_nb' => $char_nb);
137  $barcode_char_nb += $char_nb;
138  }
139  }
140 
141  $barcode_value_list = array();
142  $barcode_offset = 0;
143  $barcode_length = dol_strlen($term);
144  if ($barcode_length == $barcode_char_nb) {
145  $rows = array();
146 
147  // split term with barcode rules
148  foreach ($barcode_rules_list as $barcode_rule_arr) {
149  $code = $barcode_rule_arr['code'];
150  $char_nb = $barcode_rule_arr['char_nb'];
151  $barcode_value_list[$code] = substr($term, $barcode_offset, $char_nb);
152  $barcode_offset += $char_nb;
153  }
154 
155  if (isset($barcode_value_list['ref'])) {
156  // search product from reference
157  $sql = "SELECT rowid, ref, label, tosell, tobuy, barcode, price";
158  $sql .= " FROM " . $db->prefix() . "product as p";
159  $sql .= " WHERE entity IN (" . getEntity('product') . ")";
160  $sql .= " AND ref = '" . $db->escape($barcode_value_list['ref']) . "'";
161  if ($filteroncategids) {
162  $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)."))";
163  }
164  $sql .= " AND tosell = 1";
165  $sql .= " AND (barcode IS NULL OR barcode != '" . $db->escape($term) . "')";
166 
167  $resql = $db->query($sql);
168  if ($resql && $db->num_rows($resql) == 1) {
169  if ($obj = $db->fetch_object($resql)) {
170  $qty = 1;
171  if (isset($barcode_value_list['qu'])) {
172  $qty_str = $barcode_value_list['qu'];
173  if (isset($barcode_value_list['qd'])) {
174  $qty_str .= '.' . $barcode_value_list['qd'];
175  }
176  $qty = floatval($qty_str);
177  }
178 
179  $ig = '../public/theme/common/nophoto.png';
180  if (empty($conf->global->TAKEPOS_HIDE_PRODUCT_IMAGES)) {
181  $objProd = new Product($db);
182  $objProd->fetch($obj->rowid);
183  $image = $objProd->show_photos('product', $conf->product->multidir_output[$objProd->entity], 'small', 1);
184 
185  $match = array();
186  preg_match('@src="([^"]+)"@', $image, $match);
187  $file = array_pop($match);
188 
189  if ($file != '') {
190  if (!defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
191  $ig = $file.'&cache=1';
192  } else {
193  $ig = $file.'&cache=1&publictakepos=1&modulepart=product';
194  }
195  }
196  }
197 
198  $rows[] = array(
199  'rowid' => $obj->rowid,
200  'ref' => $obj->ref,
201  'label' => $obj->label,
202  'tosell' => $obj->tosell,
203  'tobuy' => $obj->tobuy,
204  'barcode' => $obj->barcode,
205  'price' => $obj->price,
206  'object' => 'product',
207  'img' => $ig,
208  'qty' => $qty,
209  );
210  }
211  $db->free($resql);
212  }
213  }
214 
215  if (count($rows) == 1) {
216  echo json_encode($rows);
217  exit();
218  }
219  }
220  }
221 
222  $sql = 'SELECT p.rowid, p.ref, p.label, p.tosell, p.tobuy, p.barcode, p.price' ;
223  if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1) {
224  $sql .= ', ps.reel';
225  }
226 
227  // Add fields from hooks
228  $parameters = array();
229  $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters);
230  if ($reshook >= 0) {
231  $sql .= $hookmanager->resPrint;
232  }
233 
234  $sql .= ' FROM '.MAIN_DB_PREFIX.'product as p';
235  if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1) {
236  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock as ps';
237  $sql .= ' ON (p.rowid = ps.fk_product';
238  $sql .= " AND ps.fk_entrepot = ".((int) getDolGlobalInt("CASHDESK_ID_WAREHOUSE".$_SESSION['takeposterminal']));
239  }
240 
241  // Add tables from hooks
242  $parameters = array();
243  $reshook = $hookmanager->executeHooks('printFieldListTables', $parameters);
244  if ($reshook >= 0) {
245  $sql .= $hookmanager->resPrint;
246  }
247 
248  $sql .= ' WHERE entity IN ('.getEntity('product').')';
249  if ($filteroncategids) {
250  $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).'))';
251  }
252  $sql .= ' AND tosell = 1';
253  if (getDolGlobalInt('TAKEPOS_PRODUCT_IN_STOCK') == 1) {
254  $sql .= ' AND ps.reel > 0';
255  }
256  $sql .= natural_search(array('ref', 'label', 'barcode'), $term);
257  // Add where from hooks
258  $parameters = array();
259  $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters);
260  if ($reshook >= 0) {
261  $sql .= $hookmanager->resPrint;
262  }
263 
264  // load only one page of products
265  $sql.= $db->plimit($search_limit, $search_start);
266 
267  $resql = $db->query($sql);
268  if ($resql) {
269  $rows = array();
270 
271  while ($obj = $db->fetch_object($resql)) {
272  $objProd = new Product($db);
273  $objProd->fetch($obj->rowid);
274  $image = $objProd->show_photos('product', $conf->product->multidir_output[$objProd->entity], 'small', 1);
275 
276  $match = array();
277  preg_match('@src="([^"]+)"@', $image, $match);
278  $file = array_pop($match);
279 
280  if ($file == "") {
281  $ig = '../public/theme/common/nophoto.png';
282  } else {
283  if (!defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
284  $ig = $file.'&cache=1';
285  } else {
286  $ig = $file.'&cache=1&publictakepos=1&modulepart=product';
287  }
288  }
289 
290  $row = array(
291  'rowid' => $obj->rowid,
292  'ref' => $obj->ref,
293  'label' => $obj->label,
294  'tosell' => $obj->tosell,
295  'tobuy' => $obj->tobuy,
296  'barcode' => $obj->barcode,
297  'price' => $obj->price,
298  'object' => 'product',
299  'img' => $ig,
300  'qty' => 1,
301  //'price_formated' => price(price2num($obj->price, 'MU'), 1, $langs, 1, -1, -1, $conf->currency)
302  );
303  // Add entries to row from hooks
304  $parameters=array();
305  $parameters['row'] = $row;
306  $parameters['obj'] = $obj;
307  $reshook = $hookmanager->executeHooks('completeAjaxReturnArray', $parameters);
308  if ($reshook > 0) {
309  // replace
310  if (count($hookmanager->resArray)) {
311  $row = $hookmanager->resArray;
312  } else {
313  $row = array();
314  }
315  } else {
316  // add
317  if (count($hookmanager->resArray)) {
318  $rows[] = $hookmanager->resArray;
319  }
320  $rows[] = $row;
321  }
322  }
323 
324  echo json_encode($rows);
325  } else {
326  echo 'Failed to search product : '.$db->lasterror();
327  }
328 } elseif ($action == "opendrawer" && $term != '') {
329  require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
330  $printer = new dolReceiptPrinter($db);
331  // check printer for terminal
332  if ($conf->global->{'TAKEPOS_PRINTER_TO_USE'.$term} > 0) {
333  $printer->initPrinter($conf->global->{'TAKEPOS_PRINTER_TO_USE'.$term});
334  // open cashdrawer
335  $printer->pulse();
336  $printer->close();
337  }
338 } elseif ($action == "printinvoiceticket" && $term != '' && $id > 0 && !empty($user->rights->facture->lire)) {
339  require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
340  require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
341  $printer = new dolReceiptPrinter($db);
342  // check printer for terminal
343  if (($conf->global->{'TAKEPOS_PRINTER_TO_USE'.$term} > 0 || $conf->global->TAKEPOS_PRINT_METHOD == "takeposconnector") && $conf->global->{'TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term} > 0) {
344  $object = new Facture($db);
345  $object->fetch($id);
346  $ret = $printer->sendToPrinter($object, $conf->global->{'TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term}, $conf->global->{'TAKEPOS_PRINTER_TO_USE'.$term});
347  }
348 } elseif ($action == 'getInvoice') {
349  require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
350 
351  $object = new Facture($db);
352  if ($id > 0) {
353  $object->fetch($id);
354  }
355 
356  echo json_encode($object);
357 } elseif ($action == 'thecheck') {
358  $place = GETPOST('place', 'alpha');
359  require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
360  require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
361  $printer = new dolReceiptPrinter($db);
362  $printer->sendToPrinter($object, $conf->global->{'TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term}, $conf->global->{'TAKEPOS_PRINTER_TO_USE'.$term});
363 }
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
dolReceiptPrinter
Class to manage Receipt Printers.
Definition: dolreceiptprinter.class.php:119
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
Categorie
Class to manage categories.
Definition: categorie.class.php:47
Facture
Class to manage invoices.
Definition: facture.class.php:60
getEntity
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Definition: functions.lib.php:148
getDolGlobalString
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:80
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:105
natural_search
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...
Definition: functions.lib.php:9420
Product
Class to manage products or services.
Definition: product.class.php:46
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:93