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