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
19/**
20 *	\file       htdocs/takepos/ajax/ajax.php
21 *	\brief      Ajax search component for TakePos. It search products of a category.
22 */
23
24//if (! defined('NOREQUIREUSER'))	define('NOREQUIREUSER','1');	// Not disabled cause need to load personalized language
25//if (! defined('NOREQUIREDB'))		define('NOREQUIREDB','1');		// Not disabled cause need to load personalized language
26//if (! defined('NOREQUIRESOC'))		define('NOREQUIRESOC', '1');
27//if (! defined('NOREQUIRETRAN'))		define('NOREQUIRETRAN','1');
28if (!defined('NOCSRFCHECK'))		define('NOCSRFCHECK', '1');
29if (!defined('NOTOKENRENEWAL'))	define('NOTOKENRENEWAL', '1');
30if (!defined('NOREQUIREMENU'))		define('NOREQUIREMENU', '1');
31if (!defined('NOREQUIREHTML'))		define('NOREQUIREHTML', '1');
32if (!defined('NOREQUIREAJAX'))		define('NOREQUIREAJAX', '1');
33if (!defined('NOBROWSERNOTIF')) define('NOBROWSERNOTIF', '1');
34
35require '../../main.inc.php'; // Load $user and permissions
36require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
37
38$category = GETPOST('category', 'alpha');
39$action = GETPOST('action', 'aZ09');
40$term = GETPOST('term', 'alpha');
41$id = GETPOST('id', 'int');
42
43if (empty($user->rights->takepos->run)) {
44	accessforbidden();
45}
46
47
48/*
49 * View
50 */
51
52if ($action == 'getProducts') {
53	$object = new Categorie($db);
54	if ($category == "supplements") $category = $conf->global->TAKEPOS_SUPPLEMENTS_CATEGORY;
55	$result = $object->fetch($category);
56	if ($result > 0)
57	{
58		$prods = $object->getObjectsInCateg("product", 0, 0, 0, $conf->global->TAKEPOS_SORTPRODUCTFIELD, 'ASC');
59		// Removed properties we don't need
60		if (is_array($prods) && count($prods) > 0)
61		{
62			foreach ($prods as $prod)
63			{
64				unset($prod->fields);
65				unset($prod->db);
66			}
67		}
68		echo json_encode($prods);
69	} else {
70		echo 'Failed to load category with id='.$category;
71	}
72} elseif ($action == 'search' && $term != '') {
73	// Change thirdparty with barcode
74	require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
75
76	$thirdparty = new Societe($db);
77	$result = $thirdparty->fetch('', '', '', $term);
78
79	if ($result && $thirdparty->id > 0) {
80		$rows = array();
81			$rows[] = array(
82				'rowid' => $thirdparty->id,
83				'name' => $thirdparty->name,
84				'barcode' => $thirdparty->barcode,
85		  'object' => 'thirdparty'
86			);
87			echo json_encode($rows);
88			exit;
89	}
90
91	// Define $filteroncategids, the filter on category ID if there is a Root category defined.
92	$filteroncategids = '';
93	if ($conf->global->TAKEPOS_ROOT_CATEGORY_ID > 0) {	// A root category is defined, we must filter on products inside this category tree
94		$object = new Categorie($db);
95		//$result = $object->fetch($conf->global->TAKEPOS_ROOT_CATEGORY_ID);
96		$arrayofcateg = $object->get_full_arbo('product', $conf->global->TAKEPOS_ROOT_CATEGORY_ID, 1);
97		if (is_array($arrayofcateg) && count($arrayofcateg) > 0) {
98			foreach ($arrayofcateg as $val)
99			{
100				$filteroncategids .= ($filteroncategids ? ', ' : '').$val['id'];
101			}
102		}
103	}
104
105	$sql = 'SELECT rowid, ref, label, tosell, tobuy, barcode, price FROM '.MAIN_DB_PREFIX.'product as p';
106	$sql .= ' WHERE entity IN ('.getEntity('product').')';
107	if ($filteroncategids) {
108		$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 ('.$filteroncategids.'))';
109	}
110	$sql .= ' AND tosell = 1';
111	$sql .= natural_search(array('ref', 'label', 'barcode'), $term);
112	$resql = $db->query($sql);
113	if ($resql)
114	{
115		$rows = array();
116		while ($obj = $db->fetch_object($resql)) {
117			$rows[] = array(
118				'rowid' => $obj->rowid,
119				'ref' => $obj->ref,
120				'label' => $obj->label,
121				'tosell' => $obj->tosell,
122				'tobuy' => $obj->tobuy,
123				'barcode' => $obj->barcode,
124				'price' => $obj->price,
125			'object' => 'product'
126				//'price_formated' => price(price2num($obj->price, 'MU'), 1, $langs, 1, -1, -1, $conf->currency)
127			);
128		}
129		echo json_encode($rows);
130	} else {
131		echo 'Failed to search product : '.$db->lasterror();
132	}
133} elseif ($action == "opendrawer" && $term != '') {
134	require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
135	$printer = new dolReceiptPrinter($db);
136	// check printer for terminal
137	if ($conf->global->{'TAKEPOS_PRINTER_TO_USE'.$term} > 0) {
138		$printer->initPrinter($conf->global->{'TAKEPOS_PRINTER_TO_USE'.$term});
139		// open cashdrawer
140		$printer->pulse();
141		$printer->close();
142	}
143} elseif ($action == "printinvoiceticket" && $term != '' && $id > 0 && !empty($user->rights->facture->lire)) {
144	require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
145	require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
146	$printer = new dolReceiptPrinter($db);
147	// check printer for terminal
148	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) {
149		$object = new Facture($db);
150		$object->fetch($id);
151		$ret = $printer->sendToPrinter($object, $conf->global->{'TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term}, $conf->global->{'TAKEPOS_PRINTER_TO_USE'.$term});
152	}
153} elseif ($action == 'getInvoice') {
154	require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
155
156	$object = new Facture($db);
157	if ($id > 0) {
158		$object->fetch($id);
159	}
160
161	echo json_encode($object);
162} elseif ($action == 'thecheck') {
163	$place = GETPOST('place', 'alpha');
164	require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
165	require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
166	$printer = new dolReceiptPrinter($db);
167	$printer->sendToPrinter($object, $conf->global->{'TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term}, $conf->global->{'TAKEPOS_PRINTER_TO_USE'.$term});
168}
169