bbtickethandler.class.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  3. require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/api_curl.class.php';
  4. // require_once DOL_DOCUMENT_ROOT.'//class/.class.php';
  5. // require_once DOL_DOCUMENT_ROOT.'//class/.class.php';
  6. // require_once DOL_DOCUMENT_ROOT.'//class/.class.php';
  7. class BbTicketHandler
  8. {
  9. use CurlApi;
  10. private $user;
  11. private $db;
  12. private $datec;
  13. private $fils = [];
  14. private $pere = [];
  15. private $validperiod;
  16. private $occasions;
  17. public function __construct()
  18. {
  19. global $db, $user;
  20. $this->db = $db;
  21. $this->user = $user;
  22. }
  23. public function addTicket($object, $action)
  24. {
  25. if ($object->subprice >= 0) {
  26. $GroupUsersObj = new GroupUsers($this->db);
  27. $groupUsersResult = $GroupUsersObj->fetchAll('DESC', 'rowid', 1, 0, ['customsql' => "fk_user = {$this->user->id}"]);
  28. foreach($groupUsersResult as $group){
  29. $group_id = $group->fk_settlements_group;
  30. }
  31. $factureObj = new Facture($this->db);
  32. $result = $factureObj->fetch($object->fk_facture);
  33. if ($factureObj->fk_facture_source != '') {
  34. $oldTickets = new BbTicket($this->db);
  35. $resultOldBbtickets = $oldTickets->fetchAll('DESC', 'rowid', 0, 0, ['customsql' => "fk_facture = {$factureObj->fk_facture_source} AND ticket_id= {$object->fk_product}"]);
  36. //print_r($resultOldBbtickets);exit;
  37. foreach ($resultOldBbtickets as $ticket) {
  38. $newticket = new BbTicket($this->db);
  39. $newticket->fk_facture = $object->fk_facture;
  40. $newticket->bundle_id = $ticket->bundle_id;
  41. $newticket->usable_occasions = $ticket->usable_occasions;
  42. $newticket->usage = $ticket->usage;
  43. $newticket->available_at = $ticket->available_at;
  44. $newticket->validated_at = $ticket->validated_at;
  45. $newticket->expire_at = $ticket->expire_at;
  46. $newticket->ticket_id = $ticket->ticket_id;
  47. $newticket->fk_settlements_group_id = $group_id;
  48. if ($newticket->create($this->user) == -1) {
  49. dol_syslog("Nem sikerult a ticketek mentese. facture_id: " . $object->fk_facture);
  50. }
  51. }
  52. } else {
  53. $this->getProductAssociationPere($object);
  54. $this->getDatecFromFacture($object);
  55. foreach ($this->pere as $pere) {
  56. $ticket = $this->createTicketObject2($pere, $object, $group_id);
  57. if ($ticket->create($this->user) == -1) {
  58. dol_syslog("Nem sikerult a ticketek mentese. facture_id: " . $object->fk_facture);
  59. }
  60. }
  61. }
  62. }
  63. }
  64. public function deleteTicket($object)
  65. {
  66. global $user;
  67. $ticketObj = new BbTicket($this->db);
  68. $result = $ticketObj->fetchAll('ASC', 'rowid', 0, 0, ['customsql' => "fk_facture = {$object->fk_facture} AND ticket_id = {$object->fk_product}"]);
  69. foreach ($result as $key => $value) {
  70. $ticketDelObj = new BbTicket($this->db);
  71. if ($ticketDelObj->deleteLine($user, $key) == -1) {
  72. setEventMessages($ticketDelObj->error, $ticketDelObj->errors, 'errors');
  73. }
  74. }
  75. }
  76. public function addTicketForPrintingCrossShopping($object){
  77. $helper = new ApiBBusHelper();
  78. $ticketIds = [];
  79. $this->getProductAssociationFils($object);
  80. $params = '{"ref":"' . $object->invoice_number . '"}';
  81. $this->datec = $this->curlRunner('bbus/curlgetdatecfromfacture', $params, 'POST', true);
  82. $booking_history = $helper->getbookingHistoryId($object->invoice_number);
  83. foreach ($object->fk_product as $record) {
  84. $ticket = $this->createTicketObject($this->pere[0], $record, null, $object->invoice_number, 10000, $booking_history);
  85. $insertedTicket = $ticket->create($this->user);
  86. if ($insertedTicket == -1) {
  87. dol_syslog("Nem sikerult a ticketek mentese. facture_id: " . $object->invoice_number);
  88. }
  89. $ticketIds[$insertedTicket] = $record;
  90. }
  91. return $ticketIds;
  92. }
  93. public function addTicketForPrinting($object)
  94. {
  95. $ticketIds = [];
  96. $this->getProductAssociationFils($object);
  97. $this->getDatecFromFacture($object);
  98. foreach ($object->fk_product as $record) {
  99. $ticket = $this->createTicketObject($this->pere[0], $record, $object->fk_facture);
  100. $insertedTicket = $ticket->create($this->user);
  101. if ($insertedTicket == -1) {
  102. dol_syslog("Nem sikerult a ticketek mentese. facture_id: " . $object->fk_facture);
  103. }
  104. $ticketIds[$insertedTicket] = $record;
  105. }
  106. return $ticketIds;
  107. }
  108. private function getProductAssociationFils($object)
  109. {
  110. $arrayForIN = implode(",",$object->fk_product);
  111. $sql = "SELECT fk_product_pere FROM " . $this->db->prefix() . "product_association WHERE fk_product_fils IN (" . $arrayForIN . ")";
  112. $result = $this->db->query($sql);
  113. if (pg_num_rows($result) == 0) {
  114. $this->pere[] = $object->fk_product[0];
  115. } else {
  116. while ($sqlDataResult = pg_fetch_assoc($result)) {
  117. $this->pere[0] = $sqlDataResult['fk_product_pere'];
  118. }
  119. }
  120. }
  121. private function getProductAssociationPere($object)
  122. {
  123. $sql = "SELECT fk_product_pere FROM " . $this->db->prefix() . "product_association WHERE fk_product_fils = " . $object->fk_product;
  124. $result = $this->db->query($sql);
  125. if (pg_num_rows($result) == 0) {
  126. $this->pere[] = $object->fk_product;
  127. } else {
  128. while ($sqlDataResult = pg_fetch_assoc($result)) {
  129. $this->pere[] = $sqlDataResult['fk_product_pere'];
  130. }
  131. }
  132. }
  133. private function getDatecFromFacture($object)
  134. {
  135. $sql = "SELECT datec FROM " . $this->db->prefix() . "facture WHERE rowid = " . $object->fk_facture;
  136. $result = $this->db->query($sql);
  137. while ($sqlDataResult = pg_fetch_assoc($result)) {
  138. $datec = $sqlDataResult['datec'];
  139. }
  140. $this->datec = $datec;
  141. }
  142. private function getDurationFromProductsByFilsID($filsId)
  143. {
  144. $sql = "SELECT validperiod, occasions FROM " . $this->db->prefix() . "product_extrafields WHERE fk_object = " . $filsId;
  145. $result = $this->db->query($sql);
  146. while ($sqlDataResult = pg_fetch_assoc($result)) {
  147. $this->validperiod = $sqlDataResult['validperiod'] ? $sqlDataResult['validperiod'] : 366;
  148. $this->occasions = $sqlDataResult['occasions'];
  149. }
  150. }
  151. /* private function getPeriodByPeriodSelector($sqlDataResult){
  152. $sql = "SELECT occasion_duration FROM " . $this->db->prefix() . "bbus_bboccasionsdurations WHERE occasion_key = " . $sqlDataResult['period_selector'];
  153. echo $sql;
  154. $result = $this->db->query($sql);
  155. while ($sqlDataResult = pg_fetch_assoc($result)) {
  156. $this->validperiod = $sqlDataResult['occasion_duration'];
  157. }
  158. }
  159. */
  160. private function createTicketObject($pere, $ticket_id, $facture_id = null, $ref = null, $group_id = null, $booking_history = null)
  161. {
  162. $this->getDurationFromProductsByFilsID($ticket_id);
  163. $ticket = new BbTicket($this->db);
  164. $ticket->fk_facture = $facture_id;
  165. $ticket->bundle_id = $pere;
  166. $ticket->usable_occasions = $this->occasions;
  167. $ticket->usage = '0';
  168. $ticket->available_at = $this->getAvailableAtDate($this->datec, $this->validperiod);
  169. $ticket->invoice_number = $ref;
  170. $ticket->fk_settlements_group_id = $group_id;
  171. $ticket->ticket_id = $ticket_id;
  172. $ticket->booking_history_id = $booking_history;
  173. return $ticket;
  174. }
  175. private function createTicketObject2($pere, $object, $group_id)
  176. {
  177. $this->getDurationFromProductsByFilsID($object->fk_product);
  178. $ticket = new BbTicket($this->db);
  179. $ticket->fk_facture = $object->fk_facture;
  180. $ticket->bundle_id = $pere;
  181. $ticket->usable_occasions = $this->occasions;
  182. $ticket->usage = '0';
  183. $ticket->available_at = $this->getAvailableAtDate($this->datec, $this->validperiod);
  184. $ticket->ticket_id = $object->fk_product;
  185. $ticket->fk_settlements_group_id = $group_id;
  186. return $ticket;
  187. }
  188. private function getAvailableAtDate($date, $validperiod)
  189. {
  190. $available_at = date('Y-m-d H:i:s', strtotime($date . ' +' . $validperiod . ' days'));
  191. return $available_at;
  192. }
  193. }