api_rollerstorage.class.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2024 László Szollősi
  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. use Luracast\Restler\RestException;
  19. dol_include_once('/rollerstorage/class/rollerhistory.class.php');
  20. require_once DOL_DOCUMENT_ROOT . '/custom/settlements/class/groupusers.class.php';
  21. require_once DOL_DOCUMENT_ROOT . '/custom/rollerstorage/class/rollerhistory.class.php';
  22. require_once DOL_DOCUMENT_ROOT . '/custom/rollerstorage/class/lunatours.class.php';
  23. require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/api_bbus_log.class.php';
  24. require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/api_bbus.class.php';
  25. require_once DOL_DOCUMENT_ROOT . '/api/class/api_access.class.php';
  26. /**
  27. * \file rollerstorage/class/api_rollerstorage.class.php
  28. * \ingroup rollerstorage
  29. * \brief File for API management of rollerhistory.
  30. */
  31. /**
  32. * API class for rollerstorage rollerhistory
  33. *
  34. * @access protected
  35. * @class DolibarrApiAccess {@requires user,external}
  36. */
  37. class RollerstorageApi extends DolibarrApi
  38. {
  39. /**
  40. * @var RollerHistory $rollerhistory {@type RollerHistory}
  41. */
  42. public $rollerhistory;
  43. public $lunatours;
  44. /**
  45. * Constructor
  46. *
  47. * @url GET /
  48. *
  49. */
  50. public function __construct()
  51. {
  52. global $db;
  53. $this->db = $db;
  54. $this->rollerhistory = new RollerHistory($this->db);
  55. $this->lunatours = new Lunatours($db);
  56. }
  57. private function runQuery($sql)
  58. {
  59. $array = [];
  60. $result = $this->db->query($sql);
  61. if ($this->db->num_rows($result) > 0) {
  62. while ($row = $this->db->fetch_object($result)) {
  63. $array[] = $row;
  64. }
  65. }
  66. return $array;
  67. }
  68. /**
  69. * Get app config
  70. *
  71. * Return an array with config params.
  72. *
  73. * @return array|mixed Data without useless information
  74. *
  75. * @url GET config
  76. *
  77. * @throws RestException 401
  78. * @throws RestException 403
  79. * @throws RestException 404
  80. */
  81. public function config(): array
  82. {
  83. $ApiBbus = new BBus();
  84. return $ApiBbus->config();
  85. }
  86. /**
  87. * checkPermissionLT
  88. *
  89. * Return a result.
  90. *
  91. * @return array|mixed Data without useless information
  92. *
  93. * @url POST checkPermissionLT
  94. */
  95. public function checkPermissionLT()
  96. {
  97. global $conf, $user, $conf;
  98. //ApiBbusLog::LunaToursLog("checkPermissionLT_START");
  99. $memcached = new Memcached();
  100. $memcached->addServer($conf->global->CONF_MEMCACHED, 11211);
  101. $key = '"' . $user->api_key . '"';
  102. $cacheValue = $memcached->get($key);
  103. if ($cacheValue === false) {
  104. $permissionArray = [];
  105. $sqlProductsBasicService = "SELECt p.rowid, p.label, p.description, p.price_ttc, p.price, p.tva_tx FROM llx_product as p
  106. INNER JOIN llx_product_extrafields as pe ON pe.fk_object = p.rowid
  107. INNER JOIN llx_bbus_basicservices as bs ON bs.basic_service_id = pe.basic_service::integer
  108. WHERE bs.server_host = '{$conf->global->LOCAL_SERVER_HOST}'";
  109. $productsBasicServices = $this->runQuery($sqlProductsBasicService);
  110. $permissionArray = $this->lunatours->createPermissionArray($productsBasicServices);
  111. $cacheValue = json_encode($permissionArray);
  112. $memcached->set($key, $cacheValue, 600); // 5 percig tároljuk
  113. //ApiBbusLog::LunaToursLog("Adat betöltve az adatbázisból: " . $cacheValue);
  114. } else {
  115. //ApiBbusLog::LunaToursLog("Adat betöltve a cache-ből: " . $cacheValue);
  116. }
  117. return json_decode($cacheValue, true);
  118. }
  119. /**
  120. * getAllAvailablePlaces
  121. *
  122. * Return a result.
  123. *
  124. * @return array|mixed Data without useless information
  125. *
  126. * @param string date_from
  127. * @param string date_to
  128. * @param int product_id
  129. *
  130. * @url POST getAllAvailablePlaces
  131. */
  132. public function getAllAvailablePlaces($date_from, $date_to, $product_id)
  133. {
  134. ApiBbusLog::LunaToursLog("getAllAvailablePlaces");
  135. return $this->lunatours->localAvailablePlaces($date_from, $date_to, $product_id);
  136. }
  137. /**
  138. * FirstStepPreOrder
  139. *
  140. * Return a result.
  141. *
  142. * @return array|mixed Data without useless information
  143. *
  144. * @param int $fk_event //selected evet from llx_event
  145. * @param int $reservations //numbers of reservations
  146. * @param int $product_id //Product
  147. *
  148. * @url POST FirstStepPreOrder
  149. */
  150. public function FirstStepPreOrder(int $fk_event, int $reservations, int $product_id)
  151. {
  152. global $conf;
  153. ApiBbusLog::LunaToursLog("FirstStepPreOrder");
  154. if (!DolibarrApiAccess::$user->rights->facture->creer) {
  155. ApiBbusLog::LunaToursLog("{$sendId} Insufficient rights");
  156. throw new RestException(401, 'Insufficient rights');
  157. }
  158. $this->lunatours->createLog($fk_event, $reservations, $product_id);
  159. $createdPreOrderOBJ = $this->lunatours->localcreatedpreorderobj($fk_event, $reservations, $product_id);
  160. ApiBbusLog::LunaToursLog(json_encode($createdPreOrderOBJ));
  161. return $createdPreOrderOBJ;
  162. }
  163. /**
  164. * Validate and save invoice
  165. *
  166. * Return an array with details
  167. *
  168. * @return array|mixed data without useless information
  169. *
  170. * @param array $payment Invoice payment
  171. * @param array $preorder preorder
  172. * @param array $invoice Invoice
  173. * @param string $cardPaymentLog Card payment log data
  174. * @param string $note Name, phone, email
  175. * @param array $company_invoice Invoice data
  176. *
  177. * @url POST validateandsaveinvoice
  178. * @access protected
  179. * @throws RestException 401 Not allowed
  180. * @throws RestException 404 Not found
  181. * @throws RestException 506 No available spaces
  182. *
  183. */
  184. public function validateandsaveinvoice(array $payment, array $preorder, array $invoice, string $cardPaymentLog = '', $note = '', $company_invoice = [])
  185. {
  186. ApiBbusLog::LunaToursLog("validateandsaveinvoice: START");
  187. $createdInvoiceArray = $this->lunatours->localValidateAndSaveInvoice($invoice, $preorder, $payment, $cardPaymentLog, $company_invoice);
  188. ApiBbusLog::LunaToursLog(json_encode($preorder));
  189. foreach ($preorder as $order) {
  190. $sql = "SELECT fk_product, fk_event FROM llx_booking_preorder WHERE rowid = {$order}";
  191. $data = $this->db->query($sql);
  192. if ($this->db->num_rows($data) > 0) {
  193. while ($row = $this->db->fetch_object($data)) {
  194. $bookingHistory = $this->lunatours->saveEventData((int)$row->fk_event, $createdInvoiceArray[0]['invoice']['id'], null, $createdInvoiceArray[0]['invoice']['ref'], $note);
  195. ApiBbusLog::LunaToursLog("______________________________________________________________________________");
  196. $this->lunatours->updateBbticket($createdInvoiceArray[0]['invoice'], $bookingHistory);
  197. $this->lunatours->deletePreorder($preorder);
  198. }
  199. }
  200. }
  201. ApiBbusLog::LunaToursLog("validateandsaveinvoice: END");
  202. $this->lunatours->emailSender($note, $createdInvoiceArray);
  203. return $createdInvoiceArray;
  204. }
  205. /**
  206. * Check user and eventdetail type permissions
  207. *
  208. * Return a result.
  209. *
  210. * @return array|mixed Data without useless information
  211. *
  212. * @url POST checkPermission
  213. */
  214. public function checkPermission()
  215. {
  216. global $user, $db, $conf;
  217. $permissionArray = [];
  218. dol_include_once('/eventwizard/class/eventdetails.class.php');
  219. $sqlBasicService = "SELECT pe.basic_service FROM llx_product as p
  220. INNER JOIN llx_product_extrafields as pe ON pe.fk_object = p.rowid
  221. GROUP BY pe.basic_service";
  222. $resultBasicServices = $db->query($sqlBasicService);
  223. if ($db->num_rows($resultBasicServices) > 0) {
  224. while ($row = $db->fetch_object($resultBasicServices)) {
  225. $basicServices[] = $row->basic_service;
  226. }
  227. }
  228. $sqlBasicServicesTable = "SELECT basic_service_id, ref FROM llx_bbus_basicServices WHERE server_host = '{$conf->global->LOCAL_SERVER_HOST}' ORDER BY basic_service_id ASC";
  229. $resultBasicServicesObj = $db->query($sqlBasicServicesTable);
  230. if ($db->num_rows($resultBasicServicesObj) > 0) {
  231. while ($bsrow = $db->fetch_object($resultBasicServicesObj)) {
  232. foreach ($basicServices as $item) {
  233. if ($bsrow->basic_service_id == $item) {
  234. $permissionArray[$item] = $bsrow->ref;
  235. }
  236. }
  237. }
  238. }
  239. return $permissionArray;
  240. }
  241. }