ticket_checker.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. use Luracast\Restler\RestException;
  3. require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  4. require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/basicservices.class.php';
  5. class TicketChecker
  6. {
  7. private $db;
  8. private $mac;
  9. private $code;
  10. private $lat;
  11. private $lon;
  12. private $printingTime;
  13. private $status = 0;
  14. private $device_id;
  15. private $service_location_id;
  16. private $product_id;
  17. private $fils = array();
  18. private $ticketIdForCheck;
  19. private $basic_service_id;
  20. private $ticket_id;
  21. private $bbTicketRowId;
  22. private $usage;
  23. private $facture_id;
  24. private $bundle_id;
  25. private $merged_tickets;
  26. private $imei;
  27. private $validated_at;
  28. private $expire_at;
  29. private $duration;
  30. private $productObj;
  31. private $factureObj;
  32. private $bbticketNaploObj;
  33. private $bbDevicesObj;
  34. private $BbDevicesServiceLocationObj;
  35. private $BbDevicesServiceLocationProductObj;
  36. private $bbticketObj;
  37. private $bbticketInvoicePrintingObj;
  38. public function __construct()
  39. {
  40. global $db, $conf, $user;
  41. $this->db = $db;
  42. $this->user = $user;
  43. $this->productObj = new Product($this->db);
  44. $this->factureObj = new Facture($this->db);
  45. $this->bbticketNaploObj = new BbTicketNaplo($this->db);
  46. $this->bbDevicesObj = new BbDevices($this->db);
  47. $this->BbDevicesServiceLocationObj = new BbDevicesServiceLocation($this->db);
  48. $this->BbDevicesServiceLocationProductObj = new BbDevicesServiceLocationProduct($this->db);
  49. $this->bbticketObj = new BbTicket($this->db);
  50. $this->bbticketInvoicePrintingObj = new BbTicketInvoicePrinting($this->db);
  51. }
  52. public function setMac($mac)
  53. {
  54. $this->mac = $mac;
  55. }
  56. public function setCode($code)
  57. {
  58. $code_and_timstamp = explode('_', $code);
  59. $this->code = $code_and_timstamp[0];
  60. }
  61. public function setLat($lat)
  62. {
  63. $this->lat = $lat;
  64. }
  65. public function setLon($lon)
  66. {
  67. $this->lon = $lon;
  68. }
  69. public function setTimestamp($code, $log, $logId)
  70. {
  71. global $user;
  72. $code_and_timstamp = explode('_', $code);
  73. $this->printingTime = $code_and_timstamp[1];
  74. if (empty($this->printingTime)) {
  75. ApiBbusLog::$log("{$logId} Timestamp is empty!");
  76. throw new RestException(404, 'Timestamp is empty!');
  77. }
  78. }
  79. public function setImei($imei, $logId)
  80. {
  81. global $user;
  82. $this->imei = $imei;
  83. if (empty($this->imei)) {
  84. ApiBbusLog::ticketvalidationByPhone("{$logId} IMEI is empty!");
  85. throw new RestException(404, 'IMEI is empty!');
  86. }
  87. }
  88. public function SetTicketidFromPhone($ticketid, $logId)
  89. {
  90. $this->ticket_id = $ticketid;
  91. ApiBbusLog::ticketvalidationByPhone("{$logId} Ticketid: {$this->ticket_id}");
  92. if (empty($this->ticket_id)) {
  93. ApiBbusLog::ticketvalidationByPhone("{$logId} Ticketid is empty!");
  94. throw new RestException(404, 'Ticketid is empty!');
  95. }
  96. }
  97. public function set_device_id()
  98. {
  99. $result = $this->bbDevicesObj->fetchAll('ASC', 'date_creation', 1, 0, ["customsql" => "device_id = '" . $this->mac . "'"]);
  100. $this->setId($result, 'BbDevices', 'device_id', 'id');
  101. }
  102. public function set_service_location_id()
  103. {
  104. $result = $this->BbDevicesServiceLocationObj->fetchAll('ASC', 'date_creation', 1, 0, ['customsql' => "device_id = " . $this->device_id]);
  105. $this->setId($result, 'device_servicelocation', 'service_location_id', 'id');
  106. }
  107. public function set_basic_service_id()
  108. {
  109. $result = $this->BbDevicesServiceLocationProductObj->fetchAll('ASC', 'date_creation', 1, 0, ['customsql' => "devicesservicelocation_id = " . $this->service_location_id]);
  110. $this->setId($result, 'device_service_location_product', 'basic_service_id', 'basic_service');
  111. }
  112. public function get_Basic_service_array()
  113. {
  114. $productRowIdsArray = [];
  115. $sql = "SELECT fk_object FROM " . $this->db->prefix() . $this->productObj->table_element . "_extrafields WHERE basic_service = '{$this->basic_service_id}'";
  116. $productsRowIDs = $this->db->query($sql);
  117. while ($row = pg_fetch_assoc($productsRowIDs)) {
  118. $productRowIdsArray[] = $row['fk_object'];
  119. }
  120. return $productRowIdsArray;
  121. }
  122. public function setTicketId($logId)
  123. {
  124. $this->set_device_id();
  125. $this->set_service_location_id();
  126. $this->set_basic_service_id();
  127. $this->set_ticketIDForCheck();
  128. if (!isset($this->ticketIdForCheck)) {
  129. ApiBbusLog::getByBarcode("{$logId} Invalid ticket");
  130. throw new RestException(404, 'Invalid ticket');
  131. }
  132. $sqlBbticketInvoicePrinting = "SELECT * FROM " . $this->db->prefix() . $this->bbticketInvoicePrintingObj->table_element . " WHERE printing_date_timestamp ILIKE '%{$this->printingTime}%' AND product_id = {$this->ticketIdForCheck}";
  133. //print $sqlBbticketInvoicePrinting;exit;
  134. $resultBBT = $this->db->query($sqlBbticketInvoicePrinting);
  135. while ($row = pg_fetch_assoc($resultBBT)) {
  136. $result2[$row['rowid']] = (object) $row;
  137. }
  138. //print_r($result2);exit;
  139. $this->setId($result2, 'bbticketinvoiceprinting', 'ticket_id', 'ticket_id');
  140. }
  141. public function set_ticketIDForCheck()
  142. {
  143. $basic_service_array = $this->get_Basic_service_array();
  144. foreach ($this->fils as $elem) {
  145. if (in_array($elem, $basic_service_array)) {
  146. $this->ticketIdForCheck = $elem;
  147. }
  148. }
  149. }
  150. public function check5Minutes($logId)
  151. {
  152. $result = $this->bbticketNaploObj->fetchAll('DESC', 'rowid', 1, 0, ["customsql" => "ticket_row_id = {$this->bbTicketRowId}"]);
  153. if ($result > 0) {
  154. foreach ($result as $ticket) {
  155. if (strtotime("+5 minutes", $ticket->date_creation) > dol_now()) {
  156. ApiBbusLog::getByBarcode("{$logId} Too fast!");
  157. throw new RestException(401, 'Too fast');
  158. }
  159. }
  160. }
  161. }
  162. public function setFilsArray()
  163. {
  164. $sql = "SELECT fdet.fk_product FROM " . $this->db->prefix() . $this->factureObj->table_element . " AS f
  165. INNER JOIN " . $this->db->prefix() . $this->factureObj->table_element_line . " as fdet ON fdet.fk_facture = f.rowid
  166. INNER JOIN " . $this->db->prefix() . $this->productObj->table_element . " AS p ON p.rowid = fdet.fk_product
  167. WHERE f.rowid = {$this->facture_id}";
  168. $result = $this->db->query($sql);
  169. while ($sqlDataResult = pg_fetch_assoc($result)) {
  170. $this->fils[] = $sqlDataResult['fk_product'];
  171. }
  172. }
  173. public function setFactureId($log, $logId)
  174. {
  175. $sqlFacture = "SELECT rowid FROM " . $this->db->prefix() . $this->factureObj->table_element . " WHERE ref ILIKE '%{$this->code}%'";
  176. $result = $this->db->query($sqlFacture);
  177. if (pg_num_rows($result) > 0) {
  178. while ($adatok = pg_fetch_assoc($result)) {
  179. $factureRowid = $adatok['rowid'];
  180. }
  181. } else {
  182. ApiBbusLog::$log("{$logId} Invoice not found.");
  183. throw new RestException(404, 'Invoice not found.');
  184. }
  185. $sql = "SELECT rowid FROM " . $this->db->prefix() . $this->factureObj->table_element . " WHERE fk_facture_source = {$factureRowid} AND type = 2";
  186. $res = $this->db->query($sql);
  187. if (pg_num_rows($res) > 0) {
  188. ApiBbusLog::$log("{$logId} Invoice has a Credit account.");
  189. throw new RestException(404, 'Invoice has a Credit account.');
  190. }
  191. $this->facture_id = $factureRowid;
  192. }
  193. public function setBbTicketRowId($id)
  194. {
  195. $this->bbTicketRowId = $id;
  196. }
  197. public function saveData($selectedTicket)
  198. {
  199. global $user;
  200. if ($this->newTicket($selectedTicket)) {
  201. $this->validated_at = date('Y-m-d H:i:s');
  202. $this->setDurationByProductId($selectedTicket->ticket_id);
  203. $this->setExpireAt($selectedTicket);
  204. $this->setUsage($selectedTicket);
  205. if ($this->merged_tickets) {
  206. $tickets = $this->getAllTicketsByfactureIdAndTimestamp();
  207. foreach ($tickets as $key => $value) {
  208. $sql = "UPDATE " . $this->db->prefix() . $this->bbticketObj->table_element . " SET validated_at = '" . $this->validated_at . "', expire_at = '" . $this->expire_at . "'";
  209. if ($key == $this->bbTicketRowId && $this->usage != 0) {
  210. $sql .= ", usage = '" . $this->usage . "'";
  211. }
  212. $sql .= " WHERE rowid = " . $key;
  213. $this->bbTicketRowId = $key;
  214. $this->saveAndCreateNaplo($sql);
  215. }
  216. } else {
  217. $sql = "UPDATE " . $this->db->prefix() . $this->bbticketObj->table_element . " SET usage = '" . $this->usage . "', validated_at = '" . $this->validated_at . "', expire_at = '" . $this->expire_at . "' WHERE rowid = " . $this->bbTicketRowId;
  218. $this->saveAndCreateNaplo($sql);
  219. }
  220. } else {
  221. $this->setUsage($selectedTicket);
  222. $sql = "UPDATE " . $this->db->prefix() . $this->bbticketObj->table_element . " SET usage = '" . $this->usage . "' WHERE rowid = " . $this->bbTicketRowId;
  223. $this->saveAndCreateNaplo($sql);
  224. }
  225. }
  226. public function saveCoordinates($logId)
  227. {
  228. global $db, $user;
  229. $bbTicketValidationCoordsObj = new BbTicketValidationCoords($db);
  230. $bbTicketValidationCoordsObj->fk_ticket = $this->bbTicketRowId;
  231. $bbTicketValidationCoordsObj->device_id = $this->device_id;
  232. $bbTicketValidationCoordsObj->location_id = $this->service_location_id;
  233. $bbTicketValidationCoordsObj->lat = $this->lat;
  234. $bbTicketValidationCoordsObj->lon = $this->lon;
  235. if ($bbTicketValidationCoordsObj->create($user) < 0) {
  236. ApiBbusLog::getByBarcode("{$logId} ##ERROR## Nem sikerult menteni a jegy koordinatait.");
  237. dol_syslog("{$logId} ##ERROR## Nem sikerult menteni a jegy koordinatait.", LOG_DEBUG | LOG_INFO | LOG_WARNING | LOG_ERR);
  238. }
  239. ApiBbusLog::getByBarcode("{$logId} Koordinatak mentve");
  240. }
  241. private function newTicket($selectedTicket)
  242. {
  243. return is_null($selectedTicket->validated_at) && is_null($selectedTicket->expire_at) || empty($selectedTicket->validated_at) && empty($selectedTicket->expire_at);
  244. }
  245. private function setExpireAt($selectedTicket)
  246. {
  247. $discountArray = $this->getDiscountFromfacturedet($selectedTicket->ticket_id);
  248. if (!empty($discountArray)) {
  249. $this->expire_at = date('Y-m-d H:i:s', strtotime($this->validated_at . ' +' . substr($this->duration, 0, -1) . ' ' . $this->getIntervalTimeByDuration() . ' +' . $discountArray['value'] . ' hours'));
  250. } else {
  251. $this->expire_at = date('Y-m-d H:i:s', strtotime($this->validated_at . ' +' . substr($this->duration, 0, -1) . ' ' . $this->getIntervalTimeByDuration()));
  252. }
  253. }
  254. private function getDiscountFromFActuredet($ticket_id)
  255. {
  256. $sql = "SELECT fdete.discount_hours AS value FROM " . $this->db->prefix() . $this->factureObj->table_element . " AS f
  257. INNER JOIN " . $this->db->prefix() . $this->factureObj->table_element_line . " AS fdet ON f.rowid = fdet.fk_facture
  258. INNER JOIN " . $this->db->prefix() . "facturedet_extrafields AS fdete ON fdete.fk_object = fdet.rowid
  259. WHERE f.rowid = {$this->facture_id} AND fdet.fk_product = {$ticket_id}";
  260. $res = $this->db->query($sql);
  261. if (pg_num_rows($res) > 0) {
  262. while ($adatok = pg_fetch_assoc($res)) {
  263. return $adatok;
  264. }
  265. } else {
  266. return [];
  267. }
  268. }
  269. private function setDurationByProductId($ticket_id)
  270. {
  271. $sql = "SELECT p.duration as duration FROM " . $this->db->prefix() . $this->productObj->table_element . " AS p WHERE rowid = " . $ticket_id;
  272. $result = $this->getDataFromDatabase($sql, "Product");
  273. $this->duration = $this->getIdFromResult($result, 'duration');
  274. }
  275. private function getIdFromResult($res, $fieldName)
  276. {
  277. while ($adatok = pg_fetch_assoc($res)) {
  278. return $adatok[$fieldName];
  279. }
  280. }
  281. private function getIntervalTimeByDiscountPeriod($period)
  282. {
  283. switch ($period) {
  284. case 'hour':
  285. return 'hours';
  286. case 'day':
  287. return 'days';
  288. default:
  289. return 'hours';
  290. }
  291. }
  292. private function getIntervalTimeByDuration()
  293. {
  294. $duration = $this->duration[-1];
  295. switch ($duration) {
  296. case 'h':
  297. return 'hours';
  298. case 'd':
  299. return 'days';
  300. case 'w':
  301. return 'weeks';
  302. case 'm':
  303. return 'months';
  304. case 'y':
  305. return 'years';
  306. default:
  307. return 'hours';
  308. }
  309. }
  310. private function setUsage($selectedTicket)
  311. {
  312. $this->usage = $selectedTicket->usage;
  313. if ($selectedTicket->usable_occasions !== 0) {
  314. $this->usage++;
  315. }
  316. }
  317. private function getAllTicketsByfactureIdAndTimestamp()
  318. {
  319. $tickets = [];
  320. $sql = "SELECT ip.ticket_id, t.usable_occasions FROM " . $this->db->prefix() . $this->bbticketInvoicePrintingObj->table_element . " as ip
  321. INNER JOIN " . $this->db->prefix() . $this->bbticketObj->table_element . " as t ON t.rowid = ip.ticket_id
  322. WHERE ip.printing_date_timestamp = '{$this->printingTime}' AND ip.fk_facture = {$this->facture_id}";
  323. $res = $this->db->query($sql);
  324. while ($result = pg_fetch_assoc($res)) {
  325. $tickets[$result['ticket_id']] = $result['usable_occasions'];
  326. }
  327. return $tickets;
  328. }
  329. public function checkExceptionHandlers($selectedTicket, $log, $logId)
  330. {
  331. $now = strtotime(date("Y-m-d H:i:s"));
  332. if ($selectedTicket->usage == $selectedTicket->usable_occasions && $selectedTicket->usable_occasions != 0) {
  333. ApiBbusLog::$log("{$logId} Out of usage!");
  334. $this->ExceptionHandler(2, 'Out of usage!');
  335. }
  336. if ($selectedTicket->available_at < $now || ((!is_null($selectedTicket->expire_at) && !empty($selectedTicket->expire_at)) && $selectedTicket->expire_at < $now)) {
  337. ApiBbusLog::$log("{$logId} Expired!");
  338. $this->ExceptionHandler(1, 'Expired');
  339. }
  340. }
  341. private function ExceptionHandler($status, $message)
  342. {
  343. $this->status = $status;
  344. $this->saveBbTicketNaplo();
  345. throw new RestException(401, $message);
  346. }
  347. private function saveAndCreateNaplo($sql)
  348. {
  349. $this->bbticketUpdate($sql);
  350. $this->saveBbTicketNaplo();
  351. }
  352. private function bbticketUpdate($sql)
  353. {
  354. $updated = $this->db->query($sql);
  355. if (!$updated) {
  356. dol_syslog("Nem sikerult a ticketek updateje. facture_id: " . $this->facture_id, LOG_DEBUG | LOG_INFO | LOG_WARNING | LOG_ERR);
  357. throw new RestException(404, 'Update failed');
  358. }
  359. }
  360. private function saveBbTicketNaplo()
  361. {
  362. global $user;
  363. $bbTicketNaplo = new BbTicketNaplo($this->db);
  364. $bbTicketNaplo->ticket_row_id = $this->bbTicketRowId;
  365. $bbTicketNaplo->bbservicelocation_id = isset($this->service_location_id) ? $this->service_location_id : null;
  366. $bbTicketNaplo->device_id = isset($this->device_id) ? $this->device_id : null;
  367. $bbTicketNaplo->status = $this->status;
  368. if ($bbTicketNaplo->create($user) < 0) {
  369. print_r($bbTicketNaplo->error);exit;
  370. dol_syslog('Nem sikerult menteni a bbticketNaplo tablaba a rekordot.', LOG_DEBUG | LOG_INFO | LOG_WARNING | LOG_ERR);
  371. throw new RestException(500, 'Nem sikerult menteni a bbticketNaplo tablaba a rekordot.');
  372. }
  373. }
  374. public function setMergedTickets($selectedTicket, $log, $logId)
  375. {
  376. $this->bundle_id = $selectedTicket->bundle_id;
  377. $sql = "SELECT merged_tickets FROM " . $this->db->prefix() . "product_extrafields WHERE fk_object = " . $this->bundle_id;
  378. $result = $this->getDataFromDatabase($sql, "Product_extrafields");
  379. while ($adatok = pg_fetch_assoc($result)) {
  380. $this->merged_tickets = $adatok['merged_tickets'] == 1 ? true : false;
  381. if($this->merged_tickets){
  382. ApiBbusLog::$log("{$logId} Merged ticket");
  383. }
  384. }
  385. }
  386. public function checkBbTicketInvoicePrinting()
  387. {
  388. $result = $this->bbticketInvoicePrintingObj->fetchAll('', '', 0, 0, ['customsql' => "printing_date_timestamp = '{$this->printingTime}' AND fk_facture = {$this->facture_id} AND ticket_id = {$this->ticket_id}"]);
  389. $this->checkResult($result, 'bbticketinvoiceprinting');
  390. }
  391. public function getDataOfTheSelectedTicket()
  392. {
  393. $result = $this->bbticketObj->fetchAll('ASC', 'rowid', 0, 0, ['customsql' => "rowid = {$this->ticket_id} AND fk_ticket IS NULL"]);
  394. $this->checkResult($result, 'bbticket');
  395. foreach ($result as $device) {
  396. $selectedTicket = $device;
  397. }
  398. return $selectedTicket;
  399. }
  400. #_______________________________________________________________
  401. private function getDataFromDatabase($sql, $name)
  402. {
  403. $res = $this->db->query($sql);
  404. $this->checkValidation($res, $name);
  405. return $res;
  406. }
  407. private function checkValidation($res, $name)
  408. {
  409. if (pg_num_rows($res) == 0) {
  410. throw new RestException(404, $name . ' not found');
  411. }
  412. }
  413. private function setId($result, $tableName, $variableName, $fieldName)
  414. {
  415. $this->checkResult($result, $tableName);
  416. foreach ($result as $device) {
  417. $this->$variableName = $device->$fieldName;
  418. }
  419. }
  420. private function checkResult($result, $tableName)
  421. {
  422. if (!is_array($result) || empty($result)) {
  423. dol_syslog("A megadott szuresi adatokhoz nem tartozik rekord ({$tableName}).", LOG_DEBUG | LOG_INFO | LOG_WARNING | LOG_ERR);
  424. throw new RestException(404, "A megadott szuresi adatokhoz nem tartozik rekord ({$tableName}).");
  425. }
  426. }
  427. public function getTicketTypeByProductId($product_id){
  428. $productObj = new Product($this->db);
  429. $result = $productObj->fetch($product_id);
  430. if($result > 0){
  431. $basicServicesObj = new BasicServices($this->db);
  432. $resultBS = $basicServicesObj->fetch($productObj->array_options['options_basic_service']);
  433. if($resultBS > 0){
  434. return $basicServicesObj->server_host;
  435. }
  436. }else{
  437. return false;
  438. }
  439. }
  440. }