* Copyright (C) 2024 László Szollősi * * 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 * along with this program. If not, see . */ use Luracast\Restler\RestException; dol_include_once('/rollerstorage/class/rollerhistory.class.php'); require_once DOL_DOCUMENT_ROOT . '/custom/settlements/class/groupusers.class.php'; require_once DOL_DOCUMENT_ROOT . '/custom/rollerstorage/class/rollerhistory.class.php'; require_once DOL_DOCUMENT_ROOT . '/custom/rollerstorage/class/lunatours.class.php'; require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/api_bbus_log.class.php'; require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/api_bbus.class.php'; require_once DOL_DOCUMENT_ROOT . '/api/class/api_access.class.php'; /** * \file rollerstorage/class/api_rollerstorage.class.php * \ingroup rollerstorage * \brief File for API management of rollerhistory. */ /** * API class for rollerstorage rollerhistory * * @access protected * @class DolibarrApiAccess {@requires user,external} */ class RollerstorageApi extends DolibarrApi { /** * @var RollerHistory $rollerhistory {@type RollerHistory} */ public $rollerhistory; public $lunatours; /** * Constructor * * @url GET / * */ public function __construct() { global $db; $this->db = $db; $this->rollerhistory = new RollerHistory($this->db); $this->lunatours = new Lunatours($db); } private function runQuery($sql) { $array = []; $result = $this->db->query($sql); if ($this->db->num_rows($result) > 0) { while ($row = $this->db->fetch_object($result)) { $array[] = $row; } } return $array; } /** * Get app config * * Return an array with config params. * * @return array|mixed Data without useless information * * @url GET config * * @throws RestException 401 * @throws RestException 403 * @throws RestException 404 */ public function config(): array { $ApiBbus = new BBus(); return $ApiBbus->config(); } /** * checkPermissionLT * * Return a result. * * @return array|mixed Data without useless information * * @url POST checkPermissionLT */ public function checkPermissionLT() { ApiBbusLog::LunaToursLog("checkPermissionLT_START"); global $conf; $permissionArray = []; $sqlProductsBasicService = "SELECt p.rowid, p.label, p.description, p.price_ttc, p.price, p.tva_tx FROM llx_product as p INNER JOIN llx_product_extrafields as pe ON pe.fk_object = p.rowid INNER JOIN llx_bbus_basicservices as bs ON bs.basic_service_id = pe.basic_service::integer WHERE bs.server_host = '{$conf->global->LOCAL_SERVER_HOST}'"; $productsBasicServices = $this->runQuery($sqlProductsBasicService); foreach ($productsBasicServices as $key => $item) { if ($this->lunatours->localCheckAvailablePlacesForCheckPermission($item->rowid)) { $permissionArray[$item->rowid] = $item; } } return $permissionArray; } /** * getAllAvailablePlaces * * Return a result. * * @return array|mixed Data without useless information * * @param string date_from * @param string date_to * @param int product_id * * @url POST getAllAvailablePlaces */ public function getAllAvailablePlaces($date_from, $date_to, $product_id) { ApiBbusLog::LunaToursLog("getAllAvailablePlaces"); return $this->lunatours->localAvailablePlaces($date_from, $date_to, $product_id); } /** * FirstStepPreOrder * * Return a result. * * @return array|mixed Data without useless information * * @param int $fk_event //selected evet from llx_event * @param int $reservations //numbers of reservations * @param int $product_id //Product * * @url POST FirstStepPreOrder */ public function FirstStepPreOrder(int $fk_event, int $reservations, int $product_id) { global $conf; ApiBbusLog::LunaToursLog("FirstStepPreOrder"); if (!DolibarrApiAccess::$user->rights->facture->creer) { ApiBbusLog::LunaToursLog("{$sendId} Insufficient rights"); throw new RestException(401, 'Insufficient rights'); } $this->lunatours->createLog($fk_event, $reservations, $product_id); $createdPreOrderOBJ = $this->lunatours->localcreatedpreorderobj($fk_event, $reservations, $product_id); ApiBbusLog::LunaToursLog(json_encode($createdPreOrderOBJ)); return $createdPreOrderOBJ; } /** * Validate and save invoice * * Return an array with details * * @return array|mixed data without useless information * * @param array $payment Invoice payment * @param array $preorder preorder * @param array $invoice Invoice * @param string $cardPaymentLog Card payment log data * @param string $note Name, phone, email * @param array $company_invoice Invoice data * * @url POST validateandsaveinvoice * @access protected * @throws RestException 401 Not allowed * @throws RestException 404 Not found * @throws RestException 506 No available spaces * */ public function validateandsaveinvoice(array $payment, array $preorder, array $invoice, string $cardPaymentLog = '', $note = '', $company_invoice = []) { ApiBbusLog::LunaToursLog("validateandsaveinvoice: START"); $createdInvoiceArray = $this->lunatours->localValidateAndSaveInvoice($invoice, $preorder, $payment, $cardPaymentLog, $company_invoice); ApiBbusLog::LunaToursLog(json_encode($preorder)); foreach ($preorder as $order) { $sql = "SELECT fk_product, fk_event FROM llx_booking_preorder WHERE rowid = {$order}"; $data = $this->db->query($sql); if ($this->db->num_rows($data) > 0) { while ($row = $this->db->fetch_object($data)) { $bookingHistory = $this->lunatours->saveEventData((int)$row->fk_event, $createdInvoiceArray[0]['invoice']['id'], null, $createdInvoiceArray[0]['invoice']['ref'], $note); ApiBbusLog::LunaToursLog("______________________________________________________________________________"); $this->lunatours->updateBbticket($createdInvoiceArray[0]['invoice'], $bookingHistory); $this->lunatours->deletePreorder($preorder); } } } ApiBbusLog::LunaToursLog("validateandsaveinvoice: END"); return $createdInvoiceArray; } /** * Check user and eventdetail type permissions * * Return a result. * * @return array|mixed Data without useless information * * @url POST checkPermission */ public function checkPermission() { global $user, $db; $permissionArray = []; dol_include_once('/eventwizard/class/eventdetails.class.php'); $sqlBasicService = "SELECT pe.basic_service FROM llx_product as p INNER JOIN llx_product_extrafields as pe ON pe.fk_object = p.rowid GROUP BY pe.basic_service"; $resultBasicServices = $db->query($sqlBasicService); if ($db->num_rows($resultBasicServices) > 0) { while ($row = $db->fetch_object($resultBasicServices)) { $basicServices[] = $row->basic_service; } } $sqlBasicServicesTable = "SELECT basic_service_id, ref FROM llx_bbus_basicServices WHERE server_host = 'excelia' ORDER BY basic_service_id ASC"; $resultBasicServicesObj = $db->query($sqlBasicServicesTable); if ($db->num_rows($resultBasicServicesObj) > 0) { while ($bsrow = $db->fetch_object($resultBasicServicesObj)) { foreach ($basicServices as $item) { if ($bsrow->basic_service_id == $item) { $permissionArray[$item] = $bsrow->ref; } } } } return $permissionArray; } }