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