bbtickethandler.class.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. $booking_history_id = $this->getBookingHistoryId($object->invoice_number);
  99. foreach ($object->fk_product as $record) {
  100. $GroupUsersObj = new GroupUsers($this->db);
  101. $groupUsersResult = $GroupUsersObj->fetchAll('DESC', 'rowid', 1, 0, ['customsql' => "fk_user = {$this->user->id}"]);
  102. foreach($groupUsersResult as $group){
  103. $group_id = $group->fk_settlements_group;
  104. }
  105. $ticket = $this->createTicketObject($this->pere[0], $record, $object->fk_facture, $object->invoice_number, $group_id, $booking_history_id);
  106. $insertedTicket = $ticket->create($this->user);
  107. if ($insertedTicket == -1) {
  108. dol_syslog("Nem sikerult a ticketek mentese. facture_id: " . $object->fk_facture);
  109. }
  110. $ticketIds[$insertedTicket] = $record;
  111. }
  112. return $ticketIds;
  113. }
  114. private function getBookingHistoryId($invoice_number){
  115. $sql = "SELECT rowid FROM llx_booking_bookinghistory WHERE invoice_number = '{$invoice_number}'";
  116. $result = $this->db->query($sql);
  117. if($this->db->num_rows($result) > 0){
  118. while($row = $this->db->fetch_object($result)){
  119. return $row->rowid;
  120. }
  121. }
  122. return null;
  123. }
  124. private function getProductAssociationFils($object)
  125. {
  126. $arrayForIN = implode(",",$object->fk_product);
  127. $sql = "SELECT fk_product_pere FROM " . $this->db->prefix() . "product_association WHERE fk_product_fils IN (" . $arrayForIN . ")";
  128. $result = $this->db->query($sql);
  129. if (pg_num_rows($result) == 0) {
  130. $this->pere[] = $object->fk_product[0];
  131. } else {
  132. while ($sqlDataResult = pg_fetch_assoc($result)) {
  133. $this->pere[0] = $sqlDataResult['fk_product_pere'];
  134. }
  135. }
  136. }
  137. private function getProductAssociationPere($object)
  138. {
  139. $sql = "SELECT fk_product_pere FROM " . $this->db->prefix() . "product_association WHERE fk_product_fils = " . $object->fk_product;
  140. $result = $this->db->query($sql);
  141. if (pg_num_rows($result) == 0) {
  142. $this->pere[] = $object->fk_product;
  143. } else {
  144. while ($sqlDataResult = pg_fetch_assoc($result)) {
  145. $this->pere[] = $sqlDataResult['fk_product_pere'];
  146. }
  147. }
  148. }
  149. private function getDatecFromFacture($object)
  150. {
  151. $sql = "SELECT datec FROM " . $this->db->prefix() . "facture WHERE rowid = " . $object->fk_facture;
  152. $result = $this->db->query($sql);
  153. while ($sqlDataResult = pg_fetch_assoc($result)) {
  154. $datec = $sqlDataResult['datec'];
  155. }
  156. $this->datec = $datec;
  157. }
  158. private function getDurationFromProductsByFilsID($filsId)
  159. {
  160. $sql = "SELECT validperiod, occasions FROM " . $this->db->prefix() . "product_extrafields WHERE fk_object = " . $filsId;
  161. $result = $this->db->query($sql);
  162. while ($sqlDataResult = pg_fetch_assoc($result)) {
  163. $this->validperiod = $sqlDataResult['validperiod'] ? $sqlDataResult['validperiod'] : 366;
  164. $this->occasions = $sqlDataResult['occasions'];
  165. }
  166. }
  167. /* private function getPeriodByPeriodSelector($sqlDataResult){
  168. $sql = "SELECT occasion_duration FROM " . $this->db->prefix() . "bbus_bboccasionsdurations WHERE occasion_key = " . $sqlDataResult['period_selector'];
  169. echo $sql;
  170. $result = $this->db->query($sql);
  171. while ($sqlDataResult = pg_fetch_assoc($result)) {
  172. $this->validperiod = $sqlDataResult['occasion_duration'];
  173. }
  174. }
  175. */
  176. private function createTicketObject($pere, $ticket_id, $facture_id = null, $ref = null, $group_id = null, $booking_history = null)
  177. {
  178. $this->getDurationFromProductsByFilsID($ticket_id);
  179. $ticket = new BbTicket($this->db);
  180. $ticket->fk_facture = $facture_id;
  181. $ticket->bundle_id = $pere;
  182. $ticket->usable_occasions = $this->occasions;
  183. $ticket->usage = '0';
  184. $ticket->available_at = $this->getAvailableAtDate($this->datec, $this->validperiod);
  185. $ticket->invoice_number = $ref;
  186. $ticket->fk_settlements_group_id = $group_id;
  187. $ticket->ticket_id = $ticket_id;
  188. $ticket->booking_history_id = $booking_history;
  189. return $ticket;
  190. }
  191. private function createTicketObject2($pere, $object, $group_id)
  192. {
  193. $this->getDurationFromProductsByFilsID($object->fk_product);
  194. $ticket = new BbTicket($this->db);
  195. $ticket->fk_facture = $object->fk_facture;
  196. $ticket->bundle_id = $pere;
  197. $ticket->usable_occasions = $this->occasions;
  198. $ticket->usage = '0';
  199. $ticket->available_at = $this->getAvailableAtDate($this->datec, $this->validperiod);
  200. $ticket->ticket_id = $object->fk_product;
  201. $ticket->fk_settlements_group_id = $group_id;
  202. return $ticket;
  203. }
  204. private function getAvailableAtDate($date, $validperiod)
  205. {
  206. $available_at = date('Y-m-d H:i:s', strtotime($date . ' +' . $validperiod . ' days'));
  207. return $available_at;
  208. }
  209. }