26if (!defined(
'NOTOKENRENEWAL')) {
27 define(
'NOTOKENRENEWAL',
'1');
29if (!defined(
'NOREQUIREMENU')) {
30 define(
'NOREQUIREMENU',
'1');
32if (!defined(
'NOREQUIREHTML')) {
33 define(
'NOREQUIREHTML',
'1');
35if (!defined(
'NOREQUIREAJAX')) {
36 define(
'NOREQUIREAJAX',
'1');
38if (!defined(
'NOBROWSERNOTIF')) {
39 define(
'NOBROWSERNOTIF',
'1');
43require
'../../main.inc.php';
51require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
52require_once DOL_DOCUMENT_ROOT.
"/product/class/product.class.php";
53require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
55$category =
GETPOST(
'category',
'alphanohtml');
56$action =
GETPOST(
'action',
'aZ09');
57$term =
GETPOST(
'term',
'alpha');
58$search_term =
GETPOST(
'search_term',
'alpha');
63if (!$user->hasRight(
'takepos',
'run')) {
68$hookmanager->initHooks(array(
'takeposproductsearch'));
80if ($action ==
'getProducts' && $user->hasRight(
'takepos',
'run')) {
81 $tosell = GETPOSTISSET(
'tosell') ?
GETPOSTINT(
'tosell') :
'';
82 $limit = GETPOSTISSET(
'limit') ?
GETPOSTINT(
'limit') : 0;
83 $offset = GETPOSTISSET(
'offset') ?
GETPOSTINT(
'offset') : 0;
89 $result = $thirdparty->fetch(
GETPOSTINT(
'thirdpartyid'));
91 $pricelevel = $thirdparty->price_level;
96 if ($category ==
"supplements") {
98 if (empty($category)) {
99 echo
'Error, the category to use for supplements is not defined. Go into setup of module TakePOS.';
104 $result =
$object->fetch($category);
108 $filter =
'(o.tosell:=:'.((int) $tosell).
')';
110 $prods =
$object->getObjectsInCateg(
"product", 0, $limit, $offset,
getDolGlobalString(
'TAKEPOS_SORTPRODUCTFIELD'),
'ASC', $filter);
113 if (is_array($prods) && count($prods) > 0) {
114 $productChildrenNb = 0;
115 foreach ($prods as $prod) {
116 '@phan-var-force Product $prod';
119 $productChildrenNb = $prod->hasFatherOrChild(1);
122 if ($productChildrenNb == 0) {
124 $prod->load_stock(
'nobatch,novirtual');
125 if ($prod->stock_warehouse[
getDolGlobalString(
'CASHDESK_ID_WAREHOUSE'.$_SESSION[
'takeposterminal'])]->real <= 0) {
130 unset($prod->fields);
133 $prod->price_formated =
price(
price2num(empty($prod->multiprices[$pricelevel]) ? $prod->price : $prod->multiprices[$pricelevel],
'MT'), 1, $langs, 1, -1, -1, $conf->currency);
134 $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);
139 echo json_encode($res);
141 echo
'Failed to load category with id='.dol_escape_htmltag($category);
143} elseif ($action ==
'search' && $search_term !=
'' && $user->hasRight(
'takepos',
'run')) {
147 $result = $thirdparty->fetch(0,
'',
'', $search_term);
149 if ($result && $thirdparty->id > 0) {
152 'rowid' => $thirdparty->id,
153 'name' => $thirdparty->name,
154 'barcode' => $thirdparty->barcode,
155 'object' =>
'thirdparty'
157 echo json_encode($rows);
163 $result = $thirdparty->fetch(
GETPOSTINT(
'thirdpartyid'));
165 $pricelevel = $thirdparty->price_level;
170 $filteroncategids =
'';
175 if (is_array($arrayofcateg) && count($arrayofcateg) > 0) {
176 foreach ($arrayofcateg as $val) {
177 $filteroncategids .= ($filteroncategids ?
', ' :
'').$val[
'id'];
183 if (
isModEnabled(
'barcode') && !empty($barcode_rules)) {
184 $barcode_rules_list = array();
187 $barcode_char_nb = 0;
188 $barcode_rules_arr = explode(
'+', $barcode_rules);
189 foreach ($barcode_rules_arr as $barcode_rules_values) {
190 $barcode_rules_values_arr = explode(
':', $barcode_rules_values);
191 if (count($barcode_rules_values_arr) == 2) {
192 $char_nb = intval($barcode_rules_values_arr[1]);
193 $barcode_rules_list[] = array(
'code' => $barcode_rules_values_arr[0],
'char_nb' => $char_nb);
194 $barcode_char_nb += $char_nb;
198 $barcode_value_list = array();
201 if ($barcode_length == $barcode_char_nb) {
205 foreach ($barcode_rules_list as $barcode_rule_arr) {
206 $code = $barcode_rule_arr[
'code'];
207 $char_nb = $barcode_rule_arr[
'char_nb'];
208 $barcode_value_list[$code] = substr($search_term, $barcode_offset, $char_nb);
209 $barcode_offset += $char_nb;
212 if (isset($barcode_value_list[
'ref'])) {
214 $sql =
"SELECT rowid, ref, label, tosell, tobuy, barcode, price, price_ttc";
215 $sql .=
" FROM " . $db->prefix() .
"product as p";
216 $sql .=
" WHERE entity IN (" .
getEntity(
'product') .
")";
217 $sql .=
" AND ref = '" . $db->escape($barcode_value_list[
'ref']) .
"'";
218 if ($filteroncategids) {
219 $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).
"))";
221 $sql .=
" AND tosell = 1";
222 $sql .=
" AND (barcode IS NULL OR barcode <> '" . $db->escape($search_term) .
"')";
224 $resql = $db->query($sql);
225 if ($resql && $db->num_rows($resql) == 1) {
226 if ($obj = $db->fetch_object($resql)) {
228 if (isset($barcode_value_list[
'qu'])) {
229 $qty_str = $barcode_value_list[
'qu'];
230 if (isset($barcode_value_list[
'qd'])) {
231 $qty_str .=
'.' . $barcode_value_list[
'qd'];
233 $qty = (float) $qty_str;
237 $objProd->fetch($obj->rowid);
239 $ig =
'../public/theme/common/nophoto.png';
241 $image = $objProd->show_photos(
'product', $conf->product->multidir_output[$objProd->entity],
'small', 1);
244 preg_match(
'@src="([^"]+)"@', $image, $match);
245 $file = array_pop($match);
248 if (!defined(
'INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
249 $ig = $file.
'&cache=1';
251 $ig = $file.
'&cache=1&publictakepos=1&modulepart=product';
257 'rowid' => $obj->rowid,
259 'label' => $obj->label,
260 'tosell' => $obj->tosell,
261 'tobuy' => $obj->tobuy,
262 'barcode' => $search_term,
263 'price' => empty($objProd->multiprices[$pricelevel]) ? $obj->price : $objProd->multiprices[$pricelevel],
264 'price_ttc' => empty($objProd->multiprices_ttc[$pricelevel]) ? $obj->price_ttc : $objProd->multiprices_ttc[$pricelevel],
265 'object' =>
'product',
274 if (count($rows) == 1) {
275 echo json_encode($rows);
281 $sql =
'SELECT p.rowid, p.ref, p.label, p.tosell, p.tobuy, p.barcode, p.price, p.price_ttc' ;
283 if (
getDolGlobalInt(
'CASHDESK_ID_WAREHOUSE'.$_SESSION[
'takeposterminal'])) {
286 $sql .=
', SUM(ps.reel) as reel';
294 $parameters = array();
295 $reshook = $hookmanager->executeHooks(
'printFieldListSelect', $parameters);
297 $sql .= $hookmanager->resPrint;
300 $sql .=
' FROM '.MAIN_DB_PREFIX.
'product as p';
307 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product_stock as ps';
308 $sql .=
' ON (p.rowid = ps.fk_product';
310 $sql .=
" AND ps.fk_entrepot = ".((int)
getDolGlobalInt(
"CASHDESK_ID_WAREHOUSE".$_SESSION[
'takeposterminal']));
316 $parameters = array();
317 $reshook = $hookmanager->executeHooks(
'printFieldListTables', $parameters);
319 $sql .= $hookmanager->resPrint;
322 $sql .=
' WHERE p.entity IN ('.getEntity(
'product').
')';
323 if ($filteroncategids) {
324 $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).
'))';
326 $sql .=
' AND p.tosell = 1';
328 $sql .=
' AND ps.reel > 0';
330 $sql .=
natural_search(array(
'ref',
'label',
'barcode'), $search_term);
332 $parameters = array();
333 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters);
335 $sql .= $hookmanager->resPrint;
339 $sql .=
' GROUP BY p.rowid, p.ref, p.label, p.tosell, p.tobuy, p.barcode, p.price, p.price_ttc';
341 $parameters = array();
342 $reshook = $hookmanager->executeHooks(
'printFieldListSelect', $parameters);
344 $sql .= $hookmanager->resPrint;
346 $sql .=
' HAVING SUM(ps.reel) > 0';
350 $sql .= $db->plimit($search_limit, $search_start);
352 $resql = $db->query($sql);
356 while ($obj = $db->fetch_object($resql)) {
358 $objProd->fetch($obj->rowid);
359 $image = $objProd->show_photos(
'product', $conf->product->multidir_output[$objProd->entity],
'small', 1);
362 preg_match(
'@src="([^"]+)"@', $image, $match);
363 $file = array_pop($match);
366 $ig =
'../public/theme/common/nophoto.png';
368 if (!defined(
'INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
369 $ig = $file.
'&cache=1';
371 $ig = $file.
'&cache=1&publictakepos=1&modulepart=product';
376 'rowid' => $obj->rowid,
378 'label' => $obj->label,
379 'tosell' => $obj->tosell,
380 'tobuy' => $obj->tobuy,
381 'barcode' => $obj->barcode,
382 'price' => empty($objProd->multiprices[$pricelevel]) ? $obj->price : $objProd->multiprices[$pricelevel],
383 'price_ttc' => empty($objProd->multiprices_ttc[$pricelevel]) ? $obj->price_ttc : $objProd->multiprices_ttc[$pricelevel],
384 'object' =>
'product',
387 'price_formated' =>
price(
price2num(empty($objProd->multiprices[$pricelevel]) ? $obj->
price : $objProd->multiprices[$pricelevel],
'MT'), 1, $langs, 1, -1, -1, $conf->currency),
388 '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)
391 $parameters = array();
392 $parameters[
'row'] = $row;
393 $parameters[
'obj'] = $obj;
394 $reshook = $hookmanager->executeHooks(
'completeAjaxReturnArray', $parameters);
397 if (count($hookmanager->resArray)) {
398 $row = $hookmanager->resArray;
405 if (count($hookmanager->resArray)) {
406 $rows[] = $hookmanager->resArray;
412 echo json_encode($rows);
414 echo
'Failed to search product : '.$db->lasterror();
416} elseif ($action ==
"opendrawer" && $term !=
'' && $user->hasRight(
'takepos',
'run')) {
419 require_once DOL_DOCUMENT_ROOT.
'/core/class/dolreceiptprinter.class.php';
427 $printer->initPrinter(
getDolGlobalInt(
'TAKEPOS_PRINTER_TO_USE'.$term));
429 if ($printer->getPrintConnector()) {
433 print
'Failed to init printer with ID='.getDolGlobalInt(
'TAKEPOS_PRINTER_TO_USE'.$term);
436} elseif ($action ==
"printinvoiceticket" && $term !=
'' && $id > 0 && $user->hasRight(
'takepos',
'run') && $user->hasRight(
'facture',
'lire')) {
439 require_once DOL_DOCUMENT_ROOT.
'/core/class/dolreceiptprinter.class.php';
440 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
449} elseif ($action ==
'getInvoice' && $user->hasRight(
'takepos',
'run')) {
452 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
460} elseif ($action ==
'thecheck' && $user->hasRight(
'takepos',
'run')) {
463 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
464 require_once DOL_DOCUMENT_ROOT.
'/core/class/dolreceiptprinter.class.php';
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
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 '.
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.
natural_search($fields, $value, $mode=0, $nofirstand=0, $sqltoadd='')
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
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.
isModEnabled($module)
Is Dolibarr module enabled.
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.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.