bbtickethandler.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. {
  78. $helper = new ApiBBusHelper();
  79. $ticketIds = [];
  80. $this->getProductAssociationFils($object);
  81. $params = '{"ref":"' . $object->invoice_number . '"}';
  82. $this->datec = $this->curlRunner('bbus/curlgetdatecfromfacture', $params, 'POST', true);
  83. $booking_history = $helper->getbookingHistoryId($object->invoice_number);
  84. foreach ($object->fk_product as $record) {
  85. $ticket = $this->createTicketObject($this->pere[0], $record, null, $object->invoice_number, 10000, $booking_history);
  86. $insertedTicket = $ticket->create($this->user);
  87. if ($insertedTicket == -1) {
  88. dol_syslog("Nem sikerult a ticketek mentese. facture_id: " . $object->invoice_number);
  89. }
  90. $ticketIds[$insertedTicket] = $record;
  91. }
  92. return $ticketIds;
  93. }
  94. public function addTicketForPrinting($object)
  95. {
  96. $ticketIds = [];
  97. $this->getProductAssociationFils($object);
  98. $this->getDatecFromFacture($object);
  99. $booking_history_id = $this->getBookingHistoryId($object->invoice_number);
  100. foreach ($object->fk_product as $record) {
  101. $GroupUsersObj = new GroupUsers($this->db);
  102. $groupUsersResult = $GroupUsersObj->fetchAll('DESC', 'rowid', 1, 0, ['customsql' => "fk_user = {$this->user->id}"]);
  103. foreach ($groupUsersResult as $group) {
  104. $group_id = $group->fk_settlements_group;
  105. }
  106. $ticket = $this->createTicketObject($this->pere[0], $record, $object->fk_facture, $object->invoice_number, $group_id, $booking_history_id);
  107. $insertedTicket = $ticket->create($this->user);
  108. if ($insertedTicket == -1) {
  109. dol_syslog("Nem sikerult a ticketek mentese. facture_id: " . $object->fk_facture);
  110. }
  111. $ticketIds[$insertedTicket] = $record;
  112. }
  113. return $ticketIds;
  114. }
  115. private function getBookingHistoryId($invoice_number)
  116. {
  117. $sql = "SELECT rowid FROM llx_booking_bookinghistory WHERE invoice_number = '{$invoice_number}'";
  118. $result = $this->db->query($sql);
  119. if ($this->db->num_rows($result) > 0) {
  120. while ($row = $this->db->fetch_object($result)) {
  121. return $row->rowid;
  122. }
  123. }
  124. return null;
  125. }
  126. private function getProductAssociationFils($object)
  127. {
  128. $arrayForIN = implode(",", $object->fk_product);
  129. $sql = "SELECT fk_product_pere FROM " . $this->db->prefix() . "product_association WHERE fk_product_fils IN (" . $arrayForIN . ")";
  130. $result = $this->db->query($sql);
  131. if (pg_num_rows($result) == 0) {
  132. $this->pere[] = $object->fk_product[0];
  133. } else {
  134. while ($sqlDataResult = pg_fetch_assoc($result)) {
  135. $this->pere[0] = $sqlDataResult['fk_product_pere'];
  136. }
  137. }
  138. }
  139. private function getProductAssociationPere($object)
  140. {
  141. $sql = "SELECT fk_product_pere FROM " . $this->db->prefix() . "product_association WHERE fk_product_fils = " . $object->fk_product;
  142. $result = $this->db->query($sql);
  143. if (pg_num_rows($result) == 0) {
  144. $this->pere[] = $object->fk_product;
  145. } else {
  146. while ($sqlDataResult = pg_fetch_assoc($result)) {
  147. $this->pere[] = $sqlDataResult['fk_product_pere'];
  148. }
  149. }
  150. }
  151. private function getDatecFromFacture($object)
  152. {
  153. $sql = "SELECT datec FROM " . $this->db->prefix() . "facture WHERE rowid = " . $object->fk_facture;
  154. $result = $this->db->query($sql);
  155. while ($sqlDataResult = pg_fetch_assoc($result)) {
  156. $datec = $sqlDataResult['datec'];
  157. }
  158. $this->datec = $datec;
  159. }
  160. private function getDurationFromProductsByFilsID($filsId)
  161. {
  162. $sql = "SELECT validperiod, occasions FROM " . $this->db->prefix() . "product_extrafields WHERE fk_object = " . $filsId;
  163. $result = $this->db->query($sql);
  164. while ($sqlDataResult = pg_fetch_assoc($result)) {
  165. $this->validperiod = $sqlDataResult['validperiod'] ? $sqlDataResult['validperiod'] : 366;
  166. $this->occasions = $sqlDataResult['occasions'];
  167. }
  168. }
  169. /* private function getPeriodByPeriodSelector($sqlDataResult){
  170. $sql = "SELECT occasion_duration FROM " . $this->db->prefix() . "bbus_bboccasionsdurations WHERE occasion_key = " . $sqlDataResult['period_selector'];
  171. echo $sql;
  172. $result = $this->db->query($sql);
  173. while ($sqlDataResult = pg_fetch_assoc($result)) {
  174. $this->validperiod = $sqlDataResult['occasion_duration'];
  175. }
  176. }
  177. */
  178. private function createTicketObject($pere, $ticket_id, $facture_id = null, $ref = null, $group_id = null, $booking_history = null)
  179. {
  180. $this->getDurationFromProductsByFilsID($ticket_id);
  181. $ticket = new BbTicket($this->db);
  182. $ticket->fk_facture = $facture_id;
  183. $ticket->bundle_id = $pere;
  184. $ticket->usable_occasions = $this->occasions;
  185. $ticket->usage = '0';
  186. $ticket->available_at = $this->getAvailableAtDate($this->datec, $this->validperiod);
  187. $ticket->invoice_number = $ref;
  188. $ticket->fk_settlements_group_id = $group_id;
  189. $ticket->ticket_id = $ticket_id;
  190. $ticket->booking_history_id = $booking_history;
  191. return $ticket;
  192. }
  193. private function createTicketObject2($pere, $object, $group_id)
  194. {
  195. $this->getDurationFromProductsByFilsID($object->fk_product);
  196. $ticket = new BbTicket($this->db);
  197. $ticket->fk_facture = $object->fk_facture;
  198. $ticket->bundle_id = $pere;
  199. $ticket->usable_occasions = $this->occasions;
  200. $ticket->usage = '0';
  201. $ticket->available_at = $this->getAvailableAtDate($this->datec, $this->validperiod);
  202. $ticket->ticket_id = $object->fk_product;
  203. $ticket->fk_settlements_group_id = $group_id;
  204. return $ticket;
  205. }
  206. private function getAvailableAtDate($date, $validperiod)
  207. {
  208. $available_at = date('Y-m-d H:i:s', strtotime($date . ' +' . $validperiod . ' days'));
  209. return $available_at;
  210. }
  211. public function getCorrectBasicServiceArray($SERVER_HOST)
  212. {
  213. global $db;
  214. $basicservicesArray = [];
  215. $basicServicesSQL = "SELECT bs.rowid FROM llx_bbus_basicservices as bs WHERE server_host = '{$SERVER_HOST}' ORDER BY rowid ASC";
  216. $basicServiceResult = $db->query($basicServicesSQL);
  217. if ($db->num_rows($basicServiceResult) > 0) {
  218. while ($basicServiceRow = $db->fetch_object($basicServiceResult)) {
  219. $basicservicesArray[] = $basicServiceRow->rowid;
  220. }
  221. }
  222. return $basicservicesArray;
  223. }
  224. }