bbtickethandler.class.php 7.7 KB

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