24 if (!defined(
'NOCSRFCHECK')) {
25 define(
'NOCSRFCHECK',
'1');
27 if (!defined(
'NOTOKENRENEWAL')) {
28 define(
'NOTOKENRENEWAL',
'1');
30 if (!defined(
'NOREQUIREMENU')) {
31 define(
'NOREQUIREMENU',
'1');
33 if (!defined(
'NOREQUIREHTML')) {
34 define(
'NOREQUIREHTML',
'1');
36 if (!defined(
'NOREQUIREAJAX')) {
37 define(
'NOREQUIREAJAX',
'1');
39 if (!defined(
'NOBROWSERNOTIF')) {
40 define(
'NOBROWSERNOTIF',
'1');
43 require
'../../main.inc.php';
44 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
45 require_once DOL_DOCUMENT_ROOT.
"/product/class/product.class.php";
47 $category =
GETPOST(
'category',
'alphanohtml');
48 $action =
GETPOST(
'action',
'aZ09');
49 $term =
GETPOST(
'term',
'alpha');
51 $search_start =
GETPOST(
'search_start',
'int');
52 $search_limit =
GETPOST(
'search_limit',
'int');
54 if (empty($user->rights->takepos->run)) {
59 $hookmanager->initHooks(array(
'takeposproductsearch'));
65 if ($action ==
'getProducts') {
67 if ($category ==
"supplements") {
70 $result = $object->fetch($category);
72 $prods = $object->getObjectsInCateg(
"product", 0, 0, 0,
getDolGlobalString(
'TAKEPOS_SORTPRODUCTFIELD'),
'ASC');
75 if (is_array($prods) && count($prods) > 0) {
76 foreach ($prods as $prod) {
79 $prod->load_stock(
'nobatch,novirtual');
80 if ($prod->stock_warehouse[
getDolGlobalString(
'CASHDESK_ID_WAREHOUSE'.$_SESSION[
'takeposterminal'])]->real <= 0) {
89 echo json_encode($res);
91 echo
'Failed to load category with id='.$category;
93 } elseif ($action ==
'search' && $term !=
'') {
95 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
98 $result = $thirdparty->fetch(
'',
'',
'', $term);
100 if ($result && $thirdparty->id > 0) {
103 'rowid' => $thirdparty->id,
104 'name' => $thirdparty->name,
105 'barcode' => $thirdparty->barcode,
106 'object' =>
'thirdparty'
108 echo json_encode($rows);
113 $filteroncategids =
'';
114 if ($conf->global->TAKEPOS_ROOT_CATEGORY_ID > 0) {
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'];
126 if (
isModEnabled(
'barcode') && !empty($barcode_rules)) {
127 $barcode_rules_list = array();
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;
141 $barcode_value_list = array();
144 if ($barcode_length == $barcode_char_nb) {
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;
155 if (isset($barcode_value_list[
'ref'])) {
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).
"))";
164 $sql .=
" AND tosell = 1";
165 $sql .=
" AND (barcode IS NULL OR barcode != '" . $db->escape($term) .
"')";
167 $resql = $db->query($sql);
169 if ($obj = $db->fetch_object(
$resql)) {
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'];
176 $qty = floatval($qty_str);
179 $ig =
'../public/theme/common/nophoto.png';
180 if (empty($conf->global->TAKEPOS_HIDE_PRODUCT_IMAGES)) {
182 $objProd->fetch($obj->rowid);
183 $image = $objProd->show_photos(
'product', $conf->product->multidir_output[$objProd->entity],
'small', 1);
186 preg_match(
'@src="([^"]+)"@', $image, $match);
187 $file = array_pop($match);
190 if (!defined(
'INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
191 $ig = $file.
'&cache=1';
193 $ig = $file.
'&cache=1&publictakepos=1&modulepart=product';
199 'rowid' => $obj->rowid,
201 'label' => $obj->label,
202 'tosell' => $obj->tosell,
203 'tobuy' => $obj->tobuy,
204 'barcode' => $obj->barcode,
205 'price' => $obj->price,
206 'object' =>
'product',
215 if (count($rows) == 1) {
216 echo json_encode($rows);
222 $sql =
'SELECT p.rowid, p.ref, p.label, p.tosell, p.tobuy, p.barcode, p.price' ;
228 $parameters = array();
229 $reshook = $hookmanager->executeHooks(
'printFieldListSelect', $parameters);
231 $sql .= $hookmanager->resPrint;
234 $sql .=
' FROM '.MAIN_DB_PREFIX.
'product as p';
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']));
242 $parameters = array();
243 $reshook = $hookmanager->executeHooks(
'printFieldListTables', $parameters);
245 $sql .= $hookmanager->resPrint;
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).
'))';
252 $sql .=
' AND tosell = 1';
254 $sql .=
' AND ps.reel > 0';
258 $parameters = array();
259 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters);
261 $sql .= $hookmanager->resPrint;
265 $sql.= $db->plimit($search_limit, $search_start);
267 $resql = $db->query($sql);
271 while ($obj = $db->fetch_object(
$resql)) {
273 $objProd->fetch($obj->rowid);
274 $image = $objProd->show_photos(
'product', $conf->product->multidir_output[$objProd->entity],
'small', 1);
277 preg_match(
'@src="([^"]+)"@', $image, $match);
278 $file = array_pop($match);
281 $ig =
'../public/theme/common/nophoto.png';
283 if (!defined(
'INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
284 $ig = $file.
'&cache=1';
286 $ig = $file.
'&cache=1&publictakepos=1&modulepart=product';
291 'rowid' => $obj->rowid,
293 'label' => $obj->label,
294 'tosell' => $obj->tosell,
295 'tobuy' => $obj->tobuy,
296 'barcode' => $obj->barcode,
297 'price' => $obj->price,
298 'object' =>
'product',
305 $parameters[
'row'] = $row;
306 $parameters[
'obj'] = $obj;
307 $reshook = $hookmanager->executeHooks(
'completeAjaxReturnArray', $parameters);
310 if (count($hookmanager->resArray)) {
311 $row = $hookmanager->resArray;
317 if (count($hookmanager->resArray)) {
318 $rows[] = $hookmanager->resArray;
324 echo json_encode($rows);
326 echo
'Failed to search product : '.$db->lasterror();
328 } elseif ($action ==
"opendrawer" && $term !=
'') {
329 require_once DOL_DOCUMENT_ROOT.
'/core/class/dolreceiptprinter.class.php';
332 if ($conf->global->{
'TAKEPOS_PRINTER_TO_USE'.$term} > 0) {
333 $printer->initPrinter($conf->global->{
'TAKEPOS_PRINTER_TO_USE'.$term});
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';
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) {
346 $ret = $printer->sendToPrinter($object, $conf->global->{
'TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term}, $conf->global->{
'TAKEPOS_PRINTER_TO_USE'.$term});
348 } elseif ($action ==
'getInvoice') {
349 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
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';
362 $printer->sendToPrinter($object, $conf->global->{
'TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term}, $conf->global->{
'TAKEPOS_PRINTER_TO_USE'.$term});