2018-09-28 13:31:41 +02:00
< ? php
/* Copyright ( C ) 2001 - 2004 Andreu Bisquerra < jove @ bisquerra . com >
2021-03-20 09:28:21 +01:00
* Copyright ( C ) 2020 Thibault FOUCART < support @ ptibogxiv . net >
2018-09-28 13:31:41 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2018-09-28 13:31:41 +02:00
*/
/**
2019-09-27 10:19:27 +02:00
* \file htdocs / takepos / ajax / ajax . php
2018-09-28 13:31:41 +02:00
* \brief Ajax search component for TakePos . It search products of a category .
*/
2021-02-26 21:17:52 +01:00
if ( ! defined ( 'NOCSRFCHECK' )) {
define ( 'NOCSRFCHECK' , '1' );
}
if ( ! defined ( 'NOTOKENRENEWAL' )) {
define ( 'NOTOKENRENEWAL' , '1' );
}
if ( ! defined ( 'NOREQUIREMENU' )) {
define ( 'NOREQUIREMENU' , '1' );
}
if ( ! defined ( 'NOREQUIREHTML' )) {
define ( 'NOREQUIREHTML' , '1' );
}
if ( ! defined ( 'NOREQUIREAJAX' )) {
define ( 'NOREQUIREAJAX' , '1' );
}
if ( ! defined ( 'NOBROWSERNOTIF' )) {
define ( 'NOBROWSERNOTIF' , '1' );
}
2018-09-28 13:31:41 +02:00
2020-04-10 10:59:32 +02:00
require '../../main.inc.php' ; // Load $user and permissions
2018-09-28 13:31:41 +02:00
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
2021-09-09 03:49:07 +02:00
require_once DOL_DOCUMENT_ROOT . " /product/class/product.class.php " ;
2018-09-28 13:31:41 +02:00
2021-03-14 11:48:39 +01:00
$category = GETPOST ( 'category' , 'alphanohtml' ); // Can be id of category or 'supplements'
2020-09-16 19:39:50 +02:00
$action = GETPOST ( 'action' , 'aZ09' );
2019-01-27 11:55:16 +01:00
$term = GETPOST ( 'term' , 'alpha' );
2019-11-03 15:06:20 +01:00
$id = GETPOST ( 'id' , 'int' );
2022-04-29 09:45:36 +02:00
$search_start = GETPOST ( 'search_start' , 'int' );
$search_limit = GETPOST ( 'search_limit' , 'int' );
2018-09-28 13:31:41 +02:00
2020-03-11 20:14:33 +01:00
if ( empty ( $user -> rights -> takepos -> run )) {
2020-03-23 15:46:16 +01:00
accessforbidden ();
2020-03-11 20:14:33 +01:00
}
2022-04-20 08:13:01 +02:00
// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array of hooks
2022-04-29 10:43:35 +02:00
$hookmanager -> initHooks ( array ( 'takeposproductsearch' )); // new context for product search hooks
2018-09-28 13:31:41 +02:00
/*
* View
*/
2019-12-01 18:33:57 +01:00
if ( $action == 'getProducts' ) {
2020-10-31 14:32:18 +01:00
$object = new Categorie ( $db );
2021-02-26 21:17:52 +01:00
if ( $category == " supplements " ) {
2021-07-08 12:17:32 +02:00
$category = getDolGlobalInt ( 'TAKEPOS_SUPPLEMENTS_CATEGORY' );
2021-02-26 21:17:52 +01:00
}
2020-10-31 14:32:18 +01:00
$result = $object -> fetch ( $category );
2021-02-26 21:17:52 +01:00
if ( $result > 0 ) {
2021-07-08 12:17:32 +02:00
$prods = $object -> getObjectsInCateg ( " product " , 0 , 0 , 0 , getDolGlobalString ( 'TAKEPOS_SORTPRODUCTFIELD' ), 'ASC' );
2020-10-31 14:32:18 +01:00
// Removed properties we don't need
2021-12-13 10:32:07 +01:00
$res = array ();
2021-02-26 21:17:52 +01:00
if ( is_array ( $prods ) && count ( $prods ) > 0 ) {
foreach ( $prods as $prod ) {
2022-04-20 08:13:01 +02:00
if ( getDolGlobalInt ( 'TAKEPOS_PRODUCT_IN_STOCK' ) == 1 ) {
// remove products without stock
2021-12-13 10:32:07 +01:00
$prod -> load_stock ( 'nobatch,novirtual' );
2022-04-20 08:13:01 +02:00
if ( $prod -> stock_warehouse [ getDolGlobalString ( 'CASHDESK_ID_WAREHOUSE' . $_SESSION [ 'takeposterminal' ])] -> real <= 0 ) {
2021-12-13 10:32:07 +01:00
continue ;
}
}
2020-10-31 14:32:18 +01:00
unset ( $prod -> fields );
unset ( $prod -> db );
2021-12-13 10:32:07 +01:00
$res [] = $prod ;
2020-10-31 14:32:18 +01:00
}
}
2021-12-13 10:32:07 +01:00
echo json_encode ( $res );
2020-10-31 14:32:18 +01:00
} else {
echo 'Failed to load category with id=' . $category ;
}
2020-05-21 15:05:19 +02:00
} elseif ( $action == 'search' && $term != '' ) {
2020-08-29 16:08:42 +02:00
// Change thirdparty with barcode
2020-08-29 16:05:14 +02:00
require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
2020-08-29 16:08:42 +02:00
$thirdparty = new Societe ( $db );
$result = $thirdparty -> fetch ( '' , '' , '' , $term );
2020-10-31 14:32:18 +01:00
if ( $result && $thirdparty -> id > 0 ) {
2020-10-31 18:51:30 +01:00
$rows = array ();
$rows [] = array (
'rowid' => $thirdparty -> id ,
'name' => $thirdparty -> name ,
'barcode' => $thirdparty -> barcode ,
2022-05-24 12:59:27 +02:00
'object' => 'thirdparty'
2020-10-31 18:51:30 +01:00
);
2020-08-29 16:08:42 +02:00
echo json_encode ( $rows );
exit ;
}
2020-08-29 16:05:14 +02:00
2019-11-28 14:29:58 +01:00
// Define $filteroncategids, the filter on category ID if there is a Root category defined.
$filteroncategids = '' ;
if ( $conf -> global -> TAKEPOS_ROOT_CATEGORY_ID > 0 ) { // A root category is defined, we must filter on products inside this category tree
$object = new Categorie ( $db );
//$result = $object->fetch($conf->global->TAKEPOS_ROOT_CATEGORY_ID);
$arrayofcateg = $object -> get_full_arbo ( 'product' , $conf -> global -> TAKEPOS_ROOT_CATEGORY_ID , 1 );
if ( is_array ( $arrayofcateg ) && count ( $arrayofcateg ) > 0 ) {
2021-02-26 21:17:52 +01:00
foreach ( $arrayofcateg as $val ) {
2019-11-28 14:29:58 +01:00
$filteroncategids .= ( $filteroncategids ? ', ' : '' ) . $val [ 'id' ];
}
}
}
2022-04-20 08:13:01 +02:00
$barcode_rules = getDolGlobalString ( 'TAKEPOS_BARCODE_RULE_TO_INSERT_PRODUCT' );
2022-08-26 17:03:20 +02:00
if ( isModEnabled ( 'barcode' ) && ! empty ( $barcode_rules )) {
2022-04-20 08:13:01 +02:00
$barcode_rules_list = array ();
// get barcode rules
$barcode_char_nb = 0 ;
$barcode_rules_arr = explode ( '+' , $barcode_rules );
foreach ( $barcode_rules_arr as $barcode_rules_values ) {
$barcode_rules_values_arr = explode ( ':' , $barcode_rules_values );
if ( count ( $barcode_rules_values_arr ) == 2 ) {
$char_nb = intval ( $barcode_rules_values_arr [ 1 ]);
$barcode_rules_list [] = array ( 'code' => $barcode_rules_values_arr [ 0 ], 'char_nb' => $char_nb );
$barcode_char_nb += $char_nb ;
}
}
$barcode_value_list = array ();
$barcode_offset = 0 ;
$barcode_length = dol_strlen ( $term );
if ( $barcode_length == $barcode_char_nb ) {
$rows = array ();
// split term with barcode rules
foreach ( $barcode_rules_list as $barcode_rule_arr ) {
$code = $barcode_rule_arr [ 'code' ];
$char_nb = $barcode_rule_arr [ 'char_nb' ];
$barcode_value_list [ $code ] = substr ( $term , $barcode_offset , $char_nb );
$barcode_offset += $char_nb ;
}
if ( isset ( $barcode_value_list [ 'ref' ])) {
// search product from reference
$sql = " SELECT rowid, ref, label, tosell, tobuy, barcode, price " ;
$sql .= " FROM " . $db -> prefix () . " product as p " ;
$sql .= " WHERE entity IN ( " . getEntity ( 'product' ) . " ) " ;
$sql .= " AND ref = ' " . $db -> escape ( $barcode_value_list [ 'ref' ]) . " ' " ;
if ( $filteroncategids ) {
$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 ) . " )) " ;
}
$sql .= " AND tosell = 1 " ;
$sql .= " AND (barcode IS NULL OR barcode != ' " . $db -> escape ( $term ) . " ') " ;
$resql = $db -> query ( $sql );
if ( $resql && $db -> num_rows ( $resql ) == 1 ) {
if ( $obj = $db -> fetch_object ( $resql )) {
$qty = 1 ;
if ( isset ( $barcode_value_list [ 'qu' ])) {
$qty_str = $barcode_value_list [ 'qu' ];
if ( isset ( $barcode_value_list [ 'qd' ])) {
$qty_str .= '.' . $barcode_value_list [ 'qd' ];
}
$qty = floatval ( $qty_str );
}
$ig = '../public/theme/common/nophoto.png' ;
if ( empty ( $conf -> global -> TAKEPOS_HIDE_PRODUCT_IMAGES )) {
$objProd = new Product ( $db );
$objProd -> fetch ( $obj -> rowid );
$image = $objProd -> show_photos ( 'product' , $conf -> product -> multidir_output [ $objProd -> entity ], 'small' , 1 );
$match = array ();
preg_match ( '@src="([^"]+)"@' , $image , $match );
$file = array_pop ( $match );
if ( $file != '' ) {
if ( ! defined ( 'INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE' )) {
$ig = $file . '&cache=1' ;
} else {
$ig = $file . '&cache=1&publictakepos=1&modulepart=product' ;
}
}
}
$rows [] = array (
'rowid' => $obj -> rowid ,
'ref' => $obj -> ref ,
'label' => $obj -> label ,
'tosell' => $obj -> tosell ,
'tobuy' => $obj -> tobuy ,
'barcode' => $obj -> barcode ,
'price' => $obj -> price ,
'object' => 'product' ,
'img' => $ig ,
'qty' => $qty ,
);
}
$db -> free ( $resql );
}
}
if ( count ( $rows ) == 1 ) {
echo json_encode ( $rows );
exit ();
}
}
}
2022-04-28 10:53:16 +02:00
$sql = 'SELECT p.rowid, p.ref, p.label, p.tosell, p.tobuy, p.barcode, p.price' ;
2022-04-20 08:13:01 +02:00
if ( getDolGlobalInt ( 'TAKEPOS_PRODUCT_IN_STOCK' ) == 1 ) {
2021-12-20 11:06:53 +01:00
$sql .= ', ps.reel' ;
2021-12-13 10:32:07 +01:00
}
2022-04-29 18:52:46 +02:00
2022-04-29 19:30:04 +02:00
// Add fields from hooks
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'printFieldListSelect' , $parameters );
if ( $reshook >= 0 ) {
$sql .= $hookmanager -> resPrint ;
}
2022-04-20 08:13:01 +02:00
2021-12-13 10:32:07 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'product as p' ;
2022-04-20 08:13:01 +02:00
if ( getDolGlobalInt ( 'TAKEPOS_PRODUCT_IN_STOCK' ) == 1 ) {
2021-12-13 10:32:07 +01:00
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product_stock as ps' ;
2021-12-20 11:06:53 +01:00
$sql .= ' ON (p.rowid = ps.fk_product' ;
2021-12-31 09:29:56 +01:00
$sql .= " AND ps.fk_entrepot = " . (( int ) getDolGlobalInt ( " CASHDESK_ID_WAREHOUSE " . $_SESSION [ 'takeposterminal' ]));
2021-12-13 10:32:07 +01:00
}
2022-04-20 08:13:01 +02:00
// Add tables from hooks
2022-04-29 19:30:04 +02:00
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'printFieldListTables' , $parameters );
if ( $reshook >= 0 ) {
$sql .= $hookmanager -> resPrint ;
}
2022-04-20 08:13:01 +02:00
2020-10-31 14:32:18 +01:00
$sql .= ' WHERE entity IN (' . getEntity ( 'product' ) . ')' ;
if ( $filteroncategids ) {
2021-03-22 12:00:41 +01:00
$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 ) . '))' ;
2020-10-31 14:32:18 +01:00
}
2022-04-20 08:13:01 +02:00
$sql .= ' AND tosell = 1' ;
if ( getDolGlobalInt ( 'TAKEPOS_PRODUCT_IN_STOCK' ) == 1 ) {
2021-12-20 10:42:10 +01:00
$sql .= ' AND ps.reel > 0' ;
2021-12-13 10:32:07 +01:00
}
2022-04-20 08:13:01 +02:00
$sql .= natural_search ( array ( 'ref' , 'label' , 'barcode' ), $term );
// Add where from hooks
2022-04-29 19:30:04 +02:00
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'printFieldListWhere' , $parameters );
if ( $reshook >= 0 ) {
$sql .= $hookmanager -> resPrint ;
}
2022-04-20 08:13:01 +02:00
2022-04-29 09:45:36 +02:00
// load only one page of products
2022-04-29 11:09:32 +02:00
$sql .= $db -> plimit ( $search_limit , $search_start );
2022-04-29 09:45:36 +02:00
2020-10-31 14:32:18 +01:00
$resql = $db -> query ( $sql );
2021-02-26 21:17:52 +01:00
if ( $resql ) {
2020-10-31 14:32:18 +01:00
$rows = array ();
2022-05-24 12:59:27 +02:00
2020-10-31 14:32:18 +01:00
while ( $obj = $db -> fetch_object ( $resql )) {
2021-09-09 03:49:07 +02:00
$objProd = new Product ( $db );
$objProd -> fetch ( $obj -> rowid );
$image = $objProd -> show_photos ( 'product' , $conf -> product -> multidir_output [ $objProd -> entity ], 'small' , 1 );
$match = array ();
preg_match ( '@src="([^"]+)"@' , $image , $match );
$file = array_pop ( $match );
if ( $file == " " ) {
$ig = '../public/theme/common/nophoto.png' ;
} else {
if ( ! defined ( 'INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE' )) {
$ig = $file . '&cache=1' ;
} else {
$ig = $file . '&cache=1&publictakepos=1&modulepart=product' ;
}
}
2022-04-28 08:42:44 +02:00
$row = array (
2020-10-31 14:32:18 +01:00
'rowid' => $obj -> rowid ,
'ref' => $obj -> ref ,
'label' => $obj -> label ,
'tosell' => $obj -> tosell ,
'tobuy' => $obj -> tobuy ,
'barcode' => $obj -> barcode ,
'price' => $obj -> price ,
2021-09-09 03:49:07 +02:00
'object' => 'product' ,
'img' => $ig ,
2022-04-20 08:13:01 +02:00
'qty' => 1 ,
2020-10-31 14:32:18 +01:00
//'price_formated' => price(price2num($obj->price, 'MU'), 1, $langs, 1, -1, -1, $conf->currency)
);
2022-04-20 12:01:17 +02:00
// Add entries to row from hooks
$parameters = array ();
2022-04-28 08:39:02 +02:00
$parameters [ 'row' ] = $row ;
2022-04-20 12:01:17 +02:00
$parameters [ 'obj' ] = $obj ;
2022-04-29 18:16:48 +02:00
$reshook = $hookmanager -> executeHooks ( 'completeAjaxReturnArray' , $parameters );
if ( $reshook > 0 ) {
// replace
2022-05-24 12:59:27 +02:00
if ( count ( $hookmanager -> resArray )) {
$row = $hookmanager -> resArray ;
} else {
$row = array ();
}
2022-04-29 18:16:48 +02:00
} else {
// add
2022-05-24 12:59:27 +02:00
if ( count ( $hookmanager -> resArray )) {
$rows [] = $hookmanager -> resArray ;
}
$rows [] = $row ;
2022-04-29 18:16:48 +02:00
}
2020-10-31 14:32:18 +01:00
}
2022-05-24 12:59:27 +02:00
2020-10-31 14:32:18 +01:00
echo json_encode ( $rows );
2020-05-21 15:05:19 +02:00
} else {
2019-10-20 15:16:40 +02:00
echo 'Failed to search product : ' . $db -> lasterror ();
}
2019-11-03 15:06:20 +01:00
} elseif ( $action == " opendrawer " && $term != '' ) {
2020-10-31 14:32:18 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/dolreceiptprinter.class.php' ;
$printer = new dolReceiptPrinter ( $db );
// check printer for terminal
if ( $conf -> global -> { 'TAKEPOS_PRINTER_TO_USE' . $term } > 0 ) {
$printer -> initPrinter ( $conf -> global -> { 'TAKEPOS_PRINTER_TO_USE' . $term });
// open cashdrawer
$printer -> pulse ();
$printer -> close ();
}
2020-04-10 10:59:32 +02:00
} elseif ( $action == " printinvoiceticket " && $term != '' && $id > 0 && ! empty ( $user -> rights -> facture -> lire )) {
2020-10-31 14:32:18 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/dolreceiptprinter.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
$printer = new dolReceiptPrinter ( $db );
// check printer for terminal
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 ) {
$object = new Facture ( $db );
$object -> fetch ( $id );
$ret = $printer -> sendToPrinter ( $object , $conf -> global -> { 'TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES' . $term }, $conf -> global -> { 'TAKEPOS_PRINTER_TO_USE' . $term });
}
2020-03-11 20:14:33 +01:00
} elseif ( $action == 'getInvoice' ) {
2020-04-10 10:59:32 +02:00
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
2020-03-09 17:40:32 +01:00
$object = new Facture ( $db );
if ( $id > 0 ) {
$object -> fetch ( $id );
}
echo json_encode ( $object );
2020-05-02 22:00:04 +02:00
} elseif ( $action == 'thecheck' ) {
$place = GETPOST ( 'place' , 'alpha' );
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/class/dolreceiptprinter.class.php' ;
2020-10-31 14:32:18 +01:00
$printer = new dolReceiptPrinter ( $db );
$printer -> sendToPrinter ( $object , $conf -> global -> { 'TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES' . $term }, $conf -> global -> { 'TAKEPOS_PRINTER_TO_USE' . $term });
2019-02-03 15:21:21 +01:00
}