ticket_checker.class.php 18 KB

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