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.
'/core/lib/geturl.lib.php';
52require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
53require_once DOL_DOCUMENT_ROOT.
"/product/class/product.class.php";
54require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
56$category =
GETPOST(
'category',
'alphanohtml');
57$action =
GETPOST(
'action',
'aZ09');
58$term =
GETPOST(
'term',
'alpha');
59$search_term =
GETPOST(
'search_term',
'alpha');
64if (!$user->hasRight(
'takepos',
'run')) {
69$hookmanager->initHooks(array(
'takeposproductsearch'));
81if ($action ==
'getProducts' && $user->hasRight(
'takepos',
'run')) {
82 $tosell = GETPOSTISSET(
'tosell') ?
GETPOSTINT(
'tosell') :
'';
83 $limit = GETPOSTISSET(
'limit') ?
GETPOSTINT(
'limit') : 0;
84 $offset = GETPOSTISSET(
'offset') ?
GETPOSTINT(
'offset') : 0;
90 $result = $thirdparty->fetch(
GETPOSTINT(
'thirdpartyid'));
92 $pricelevel = $thirdparty->price_level;
97 if ($category ==
"supplements") {
99 if (empty($category)) {
100 echo
'Error, the category to use for supplements is not defined. Go into setup of module TakePOS.';
105 $result =
$object->fetch($category);
109 $filter =
'(o.tosell:=:'.((int) $tosell).
')';
111 $prods =
$object->getObjectsInCateg(
"product", 0, $limit, $offset,
getDolGlobalString(
'TAKEPOS_SORTPRODUCTFIELD'),
'ASC', $filter);
114 if (is_array($prods) && count($prods) > 0) {
115 $productChildrenNb = 0;
116 foreach ($prods as
$prod) {
117 '@phan-var-force Product $prod';
120 $productChildrenNb =
$prod->hasFatherOrChild(1);
123 if ($productChildrenNb == 0) {
125 $prod->load_stock(
'nobatch,novirtual');
126 if (
$prod->stock_warehouse[
getDolGlobalString(
'CASHDESK_ID_WAREHOUSE'.$_SESSION[
'takeposterminal'])]->real <= 0) {
131 unset(
$prod->fields);
135 $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);
140 echo json_encode($res);
142 echo
'Failed to load category with id='.dol_escape_htmltag($category);
144} elseif ($action ==
'search' && $search_term !=
'' && $user->hasRight(
'takepos',
'run')) {
148 $result = $thirdparty->fetch(0,
'',
'', $search_term);
150 if ($result && $thirdparty->id > 0) {
153 'rowid' => $thirdparty->id,
154 'name' => $thirdparty->name,
155 'barcode' => $thirdparty->barcode,
156 'object' =>
'thirdparty'
158 echo json_encode($rows);
164 $result = $thirdparty->fetch(
GETPOSTINT(
'thirdpartyid'));
166 $pricelevel = $thirdparty->price_level;
171 $filteroncategids =
'';
176 if (is_array($arrayofcateg) && count($arrayofcateg) > 0) {
177 foreach ($arrayofcateg as $val) {
178 $filteroncategids .= ($filteroncategids ?
', ' :
'').$val[
'id'];
184 if (
isModEnabled(
'barcode') && !empty($barcode_rules)) {
185 $barcode_rules_list = array();
188 $barcode_char_nb = 0;
189 $barcode_rules_arr = explode(
'+', $barcode_rules);
190 foreach ($barcode_rules_arr as $barcode_rules_values) {
191 $barcode_rules_values_arr = explode(
':', $barcode_rules_values);
192 if (count($barcode_rules_values_arr) == 2) {
193 $char_nb = intval($barcode_rules_values_arr[1]);
194 $barcode_rules_list[] = array(
'code' => $barcode_rules_values_arr[0],
'char_nb' => $char_nb);
195 $barcode_char_nb += $char_nb;
199 $barcode_value_list = array();
202 if ($barcode_length == $barcode_char_nb) {
206 foreach ($barcode_rules_list as $barcode_rule_arr) {
207 $code = $barcode_rule_arr[
'code'];
208 $char_nb = $barcode_rule_arr[
'char_nb'];
209 $barcode_value_list[$code] = substr($search_term, $barcode_offset, $char_nb);
210 $barcode_offset += $char_nb;
213 if (isset($barcode_value_list[
'ref'])) {
215 $sql =
"SELECT rowid, ref, label, tosell, tobuy, barcode, price, price_ttc";
216 $sql .=
" FROM " .
$db->prefix() .
"product as p";
217 $sql .=
" WHERE entity IN (" .
getEntity(
'product') .
")";
218 $sql .=
" AND ref = '" .
$db->escape($barcode_value_list[
'ref']) .
"'";
219 if ($filteroncategids) {
220 $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).
"))";
222 $sql .=
" AND tosell = 1";
223 $sql .=
" AND (barcode IS NULL OR barcode <> '" .
$db->escape($search_term) .
"')";
225 $resql =
$db->query($sql);
226 if ($resql &&
$db->num_rows($resql) == 1) {
227 if ($obj =
$db->fetch_object($resql)) {
229 if (isset($barcode_value_list[
'qu'])) {
230 $qty_str = $barcode_value_list[
'qu'];
231 if (isset($barcode_value_list[
'qd'])) {
232 $qty_str .=
'.' . $barcode_value_list[
'qd'];
234 $qty = (float) $qty_str;
238 $objProd->fetch($obj->rowid);
240 $ig =
'../public/theme/common/nophoto.png';
242 $image = $objProd->show_photos(
'product',
$conf->product->multidir_output[$objProd->entity],
'small', 1);
245 preg_match(
'@src="([^"]+)"@', $image, $match);
246 $file = array_pop($match);
249 if (!defined(
'INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
250 $ig = $file.
'&cache=1';
252 $ig = $file.
'&cache=1&publictakepos=1&modulepart=product';
258 'rowid' => $obj->rowid,
260 'label' => $obj->label,
261 'tosell' => $obj->tosell,
262 'tobuy' => $obj->tobuy,
263 'barcode' => $search_term,
264 'price' => empty($objProd->multiprices[$pricelevel]) ? $obj->price : $objProd->multiprices[$pricelevel],
265 'price_ttc' => empty($objProd->multiprices_ttc[$pricelevel]) ? $obj->price_ttc : $objProd->multiprices_ttc[$pricelevel],
266 'object' =>
'product',
275 if (count($rows) == 1) {
276 echo json_encode($rows);
282 $sql =
'SELECT p.rowid, p.ref, p.label, p.tosell, p.tobuy, p.barcode, p.price, p.price_ttc' ;
284 if (
getDolGlobalInt(
'CASHDESK_ID_WAREHOUSE'.$_SESSION[
'takeposterminal'])) {
287 $sql .=
', SUM(ps.reel) as reel';
295 $parameters = array();
296 $reshook = $hookmanager->executeHooks(
'printFieldListSelect', $parameters);
298 $sql .= $hookmanager->resPrint;
301 $sql .=
' FROM '.MAIN_DB_PREFIX.
'product as p';
308 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product_stock as ps';
309 $sql .=
' ON (p.rowid = ps.fk_product';
311 $sql .=
" AND ps.fk_entrepot = ".((int)
getDolGlobalInt(
"CASHDESK_ID_WAREHOUSE".$_SESSION[
'takeposterminal']));
317 $parameters = array();
318 $reshook = $hookmanager->executeHooks(
'printFieldListTables', $parameters);
320 $sql .= $hookmanager->resPrint;
323 $sql .=
' WHERE p.entity IN ('.getEntity(
'product').
')';
324 if ($filteroncategids) {
325 $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).
'))';
327 $sql .=
' AND p.tosell = 1';
329 $sql .=
' AND ps.reel > 0';
331 $sql .=
natural_search(array(
'ref',
'label',
'barcode'), $search_term);
333 $parameters = array();
334 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters);
336 $sql .= $hookmanager->resPrint;
340 $sql .=
' GROUP BY p.rowid, p.ref, p.label, p.tosell, p.tobuy, p.barcode, p.price, p.price_ttc';
342 $parameters = array();
343 $reshook = $hookmanager->executeHooks(
'printFieldListSelect', $parameters);
345 $sql .= $hookmanager->resPrint;
347 $sql .=
' HAVING SUM(ps.reel) > 0';
351 $sql .=
$db->plimit($search_limit, $search_start);
353 $resql =
$db->query($sql);
357 while ($obj =
$db->fetch_object($resql)) {
359 $objProd->fetch($obj->rowid);
360 $image = $objProd->show_photos(
'product',
$conf->product->multidir_output[$objProd->entity],
'small', 1);
363 preg_match(
'@src="([^"]+)"@', $image, $match);
364 $file = array_pop($match);
367 $ig =
'../public/theme/common/nophoto.png';
369 if (!defined(
'INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
370 $ig = $file.
'&cache=1';
372 $ig = $file.
'&cache=1&publictakepos=1&modulepart=product';
377 'rowid' => $obj->rowid,
379 'label' => $obj->label,
380 'tosell' => $obj->tosell,
381 'tobuy' => $obj->tobuy,
382 'barcode' => $obj->barcode,
383 'price' => empty($objProd->multiprices[$pricelevel]) ? $obj->price : $objProd->multiprices[$pricelevel],
384 'price_ttc' => empty($objProd->multiprices_ttc[$pricelevel]) ? $obj->price_ttc : $objProd->multiprices_ttc[$pricelevel],
385 'object' =>
'product',
388 'price_formated' =>
price(
price2num(empty($objProd->multiprices[$pricelevel]) ? $obj->
price : $objProd->multiprices[$pricelevel],
'MT'), 1, $langs, 1, -1, -1,
$conf->currency),
389 '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)
392 $parameters = array();
393 $parameters[
'row'] = $row;
394 $parameters[
'obj'] = $obj;
395 $reshook = $hookmanager->executeHooks(
'completeAjaxReturnArray', $parameters);
398 if (count($hookmanager->resArray)) {
399 $row = $hookmanager->resArray;
406 if (count($hookmanager->resArray)) {
407 $rows[] = $hookmanager->resArray;
413 echo json_encode($rows);
415 echo
'Failed to search product : '.$db->lasterror();
417} elseif ($action ==
"opendrawer" && $term !=
'' && $user->hasRight(
'takepos',
'run')) {
420 require_once DOL_DOCUMENT_ROOT.
'/takepos/class/dolreceiptprinter.class.php';
428 $printer->initPrinter(
getDolGlobalInt(
'TAKEPOS_PRINTER_TO_USE'.$term));
430 if ($printer->getPrintConnector()) {
434 print
'Failed to init printer with ID='.getDolGlobalInt(
'TAKEPOS_PRINTER_TO_USE'.$term);
437} elseif ($action ==
"printinvoiceticket" && $term !=
'' && $id > 0 && $user->hasRight(
'takepos',
'run') && $user->hasRight(
'facture',
'lire')) {
440 require_once DOL_DOCUMENT_ROOT.
'/takepos/class/dolreceiptprinter.class.php';
441 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
449 $resultinccounter = 0;
450 $templateidtouse = 0;
452 $url = DOL_URL_ROOT.
"/blockedlog/ajax/block-add.php?id=".((int)
$object->id).
'&element='.urlencode(
$object->element).
"&action=DOC_PREVIEW&token=".newToken();
454 $result =
getURLContent($url,
'GET',
'', 1, array(), array(
'http',
'https'), 2);
456 if ((
string) $result[
'http_code'] ==
'200') {
460 $templateidtouse =
getDolGlobalInt(
'TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term);
463 if ($resultinccounter) {
467 print
'Failed to update print counter for object ID='.$object->id;
470} elseif ($action ==
'getInvoice' && $user->hasRight(
'takepos',
'run')) {
473 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
481} elseif ($action ==
'thecheck' && $user->hasRight(
'takepos',
'run')) {
484 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
485 require_once DOL_DOCUMENT_ROOT.
'/takepos/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.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1, $timeoutconnect=0, $timeoutresponse=0, $otherCurlOptions=array(), $morelogsuffix='')
Function to get a content from an URL (use proxy if proxy defined).
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.