api_rollerstorage.class.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. ApiBbusLog::LunaToursLog("checkPermissionLT_START");
  98. global $conf;
  99. $permissionArray = [];
  100. $sqlProductsBasicService = "SELECt p.rowid, p.label, p.description, p.price_ttc, p.price, p.tva_tx FROM llx_product as p
  101. INNER JOIN llx_product_extrafields as pe ON pe.fk_object = p.rowid
  102. INNER JOIN llx_bbus_basicservices as bs ON bs.basic_service_id = pe.basic_service::integer
  103. WHERE bs.server_host = '{$conf->global->LOCAL_SERVER_HOST}'";
  104. $productsBasicServices = $this->runQuery($sqlProductsBasicService);
  105. foreach ($productsBasicServices as $key => $item) {
  106. if ($this->lunatours->localCheckAvailablePlacesForCheckPermission($item->rowid)) {
  107. $permissionArray[$item->rowid] = $item;
  108. }
  109. }
  110. return $permissionArray;
  111. }
  112. /**
  113. * getAllAvailablePlaces
  114. *
  115. * Return a result.
  116. *
  117. * @return array|mixed Data without useless information
  118. *
  119. * @param string date_from
  120. * @param string date_to
  121. * @param int product_id
  122. *
  123. * @url POST getAllAvailablePlaces
  124. */
  125. public function getAllAvailablePlaces($date_from, $date_to, $product_id)
  126. {
  127. ApiBbusLog::LunaToursLog("getAllAvailablePlaces");
  128. return $this->lunatours->localAvailablePlaces($date_from, $date_to, $product_id);
  129. }
  130. /**
  131. * FirstStepPreOrder
  132. *
  133. * Return a result.
  134. *
  135. * @return array|mixed Data without useless information
  136. *
  137. * @param int $fk_event //selected evet from llx_event
  138. * @param int $reservations //numbers of reservations
  139. * @param int $product_id //Product
  140. *
  141. * @url POST FirstStepPreOrder
  142. */
  143. public function FirstStepPreOrder(int $fk_event, int $reservations, int $product_id)
  144. {
  145. global $conf;
  146. ApiBbusLog::LunaToursLog("FirstStepPreOrder");
  147. if (!DolibarrApiAccess::$user->rights->facture->creer) {
  148. ApiBbusLog::LunaToursLog("{$sendId} Insufficient rights");
  149. throw new RestException(401, 'Insufficient rights');
  150. }
  151. $this->lunatours->createLog($fk_event, $reservations, $product_id);
  152. $createdPreOrderOBJ = $this->lunatours->localcreatedpreorderobj($fk_event, $reservations, $product_id);
  153. ApiBbusLog::LunaToursLog(json_encode($createdPreOrderOBJ));
  154. return $createdPreOrderOBJ;
  155. }
  156. /**
  157. * Validate and save invoice
  158. *
  159. * Return an array with details
  160. *
  161. * @return array|mixed data without useless information
  162. *
  163. * @param array $payment Invoice payment
  164. * @param array $preorder preorder
  165. * @param array $invoice Invoice
  166. * @param string $cardPaymentLog Card payment log data
  167. * @param string $note Name, phone, email
  168. * @param array $company_invoice Invoice data
  169. *
  170. * @url POST validateandsaveinvoice
  171. * @access protected
  172. * @throws RestException 401 Not allowed
  173. * @throws RestException 404 Not found
  174. * @throws RestException 506 No available spaces
  175. *
  176. */
  177. public function validateandsaveinvoice(array $payment, array $preorder, array $invoice, string $cardPaymentLog = '', $note = '', $company_invoice = [])
  178. {
  179. ApiBbusLog::LunaToursLog("validateandsaveinvoice: START");
  180. $createdInvoiceArray = $this->lunatours->localValidateAndSaveInvoice($invoice, $preorder, $payment, $cardPaymentLog, $company_invoice);
  181. ApiBbusLog::LunaToursLog(json_encode($preorder));
  182. foreach ($preorder as $order) {
  183. $sql = "SELECT fk_product, fk_event FROM llx_booking_preorder WHERE rowid = {$order}";
  184. $data = $this->db->query($sql);
  185. if ($this->db->num_rows($data) > 0) {
  186. while ($row = $this->db->fetch_object($data)) {
  187. $bookingHistory = $this->lunatours->saveEventData((int)$row->fk_event, $createdInvoiceArray[0]['invoice']['id'], null, $createdInvoiceArray[0]['invoice']['ref'], $note);
  188. ApiBbusLog::LunaToursLog("______________________________________________________________________________");
  189. $this->lunatours->updateBbticket($createdInvoiceArray[0]['invoice'], $bookingHistory);
  190. $this->lunatours->deletePreorder($preorder);
  191. }
  192. }
  193. }
  194. ApiBbusLog::LunaToursLog("validateandsaveinvoice: END");
  195. return $createdInvoiceArray;
  196. }
  197. /**
  198. * Check user and eventdetail type permissions
  199. *
  200. * Return a result.
  201. *
  202. * @return array|mixed Data without useless information
  203. *
  204. * @url POST checkPermission
  205. */
  206. public function checkPermission()
  207. {
  208. global $user, $db;
  209. $permissionArray = [];
  210. dol_include_once('/eventwizard/class/eventdetails.class.php');
  211. $sqlBasicService = "SELECT pe.basic_service FROM llx_product as p
  212. INNER JOIN llx_product_extrafields as pe ON pe.fk_object = p.rowid
  213. GROUP BY pe.basic_service";
  214. $resultBasicServices = $db->query($sqlBasicService);
  215. if ($db->num_rows($resultBasicServices) > 0) {
  216. while ($row = $db->fetch_object($resultBasicServices)) {
  217. $basicServices[] = $row->basic_service;
  218. }
  219. }
  220. $sqlBasicServicesTable = "SELECT basic_service_id, ref FROM llx_bbus_basicServices WHERE server_host = 'excelia' ORDER BY basic_service_id ASC";
  221. $resultBasicServicesObj = $db->query($sqlBasicServicesTable);
  222. if ($db->num_rows($resultBasicServicesObj) > 0) {
  223. while ($bsrow = $db->fetch_object($resultBasicServicesObj)) {
  224. foreach ($basicServices as $item) {
  225. if ($bsrow->basic_service_id == $item) {
  226. $permissionArray[$item] = $bsrow->ref;
  227. }
  228. }
  229. }
  230. }
  231. return $permissionArray;
  232. }
  233. }