| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
- require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/api_curl.class.php';
- // require_once DOL_DOCUMENT_ROOT.'//class/.class.php';
- // require_once DOL_DOCUMENT_ROOT.'//class/.class.php';
- // require_once DOL_DOCUMENT_ROOT.'//class/.class.php';
- class BbTicketHandler
- {
- use CurlApi;
- private $user;
- private $db;
- private $datec;
- private $fils = [];
- private $pere = [];
- private $validperiod;
- private $occasions;
- public function __construct()
- {
- global $db, $user;
- $this->db = $db;
- $this->user = $user;
- }
- public function addTicket($object, $action)
- {
- if ($object->subprice >= 0) {
- $GroupUsersObj = new GroupUsers($this->db);
- $groupUsersResult = $GroupUsersObj->fetchAll('DESC', 'rowid', 1, 0, ['customsql' => "fk_user = {$this->user->id}"]);
- foreach($groupUsersResult as $group){
- $group_id = $group->fk_settlements_group;
- }
- $factureObj = new Facture($this->db);
- $result = $factureObj->fetch($object->fk_facture);
- if ($factureObj->fk_facture_source != '') {
- $oldTickets = new BbTicket($this->db);
- $resultOldBbtickets = $oldTickets->fetchAll('DESC', 'rowid', 0, 0, ['customsql' => "fk_facture = {$factureObj->fk_facture_source} AND ticket_id= {$object->fk_product}"]);
- //print_r($resultOldBbtickets);exit;
- foreach ($resultOldBbtickets as $ticket) {
- $newticket = new BbTicket($this->db);
- $newticket->fk_facture = $object->fk_facture;
- $newticket->bundle_id = $ticket->bundle_id;
- $newticket->usable_occasions = $ticket->usable_occasions;
- $newticket->usage = $ticket->usage;
- $newticket->available_at = $ticket->available_at;
- $newticket->validated_at = $ticket->validated_at;
- $newticket->expire_at = $ticket->expire_at;
- $newticket->ticket_id = $ticket->ticket_id;
- $newticket->fk_settlements_group_id = $group_id;
- if ($newticket->create($this->user) == -1) {
- dol_syslog("Nem sikerult a ticketek mentese. facture_id: " . $object->fk_facture);
- }
- }
- } else {
- $this->getProductAssociationPere($object);
- $this->getDatecFromFacture($object);
- foreach ($this->pere as $pere) {
- $ticket = $this->createTicketObject2($pere, $object, $group_id);
- if ($ticket->create($this->user) == -1) {
- dol_syslog("Nem sikerult a ticketek mentese. facture_id: " . $object->fk_facture);
- }
- }
- }
- }
- }
- public function deleteTicket($object)
- {
- global $user;
- $ticketObj = new BbTicket($this->db);
- $result = $ticketObj->fetchAll('ASC', 'rowid', 0, 0, ['customsql' => "fk_facture = {$object->fk_facture} AND ticket_id = {$object->fk_product}"]);
- foreach ($result as $key => $value) {
- $ticketDelObj = new BbTicket($this->db);
- if ($ticketDelObj->deleteLine($user, $key) == -1) {
- setEventMessages($ticketDelObj->error, $ticketDelObj->errors, 'errors');
- }
- }
- }
- public function addTicketForPrintingCrossShopping($object){
- $helper = new ApiBBusHelper();
- $ticketIds = [];
- $this->getProductAssociationFils($object);
- $params = '{"ref":"' . $object->invoice_number . '"}';
- $this->datec = $this->curlRunner('bbus/curlgetdatecfromfacture', $params, 'POST', true);
- $booking_history = $helper->getbookingHistoryId($object->invoice_number);
- foreach ($object->fk_product as $record) {
- $ticket = $this->createTicketObject($this->pere[0], $record, null, $object->invoice_number, 10000, $booking_history);
- $insertedTicket = $ticket->create($this->user);
- if ($insertedTicket == -1) {
- dol_syslog("Nem sikerult a ticketek mentese. facture_id: " . $object->invoice_number);
- }
- $ticketIds[$insertedTicket] = $record;
- }
- return $ticketIds;
- }
- public function addTicketForPrinting($object)
- {
- $ticketIds = [];
- $this->getProductAssociationFils($object);
- $this->getDatecFromFacture($object);
- $booking_history_id = $this->getBookingHistoryId($object->invoice_number);
- foreach ($object->fk_product as $record) {
- $GroupUsersObj = new GroupUsers($this->db);
- $groupUsersResult = $GroupUsersObj->fetchAll('DESC', 'rowid', 1, 0, ['customsql' => "fk_user = {$this->user->id}"]);
- foreach($groupUsersResult as $group){
- $group_id = $group->fk_settlements_group;
- }
- $ticket = $this->createTicketObject($this->pere[0], $record, $object->fk_facture, $object->invoice_number, $group_id, $booking_history_id);
- $insertedTicket = $ticket->create($this->user);
- if ($insertedTicket == -1) {
- dol_syslog("Nem sikerult a ticketek mentese. facture_id: " . $object->fk_facture);
- }
- $ticketIds[$insertedTicket] = $record;
- }
- return $ticketIds;
- }
- private function getBookingHistoryId($invoice_number){
- $sql = "SELECT rowid FROM llx_booking_bookinghistory WHERE invoice_number = '{$invoice_number}'";
- $result = $this->db->query($sql);
- if($this->db->num_rows($result) > 0){
- while($row = $this->db->fetch_object($result)){
- return $row->rowid;
- }
- }
- return null;
- }
- private function getProductAssociationFils($object)
- {
- $arrayForIN = implode(",",$object->fk_product);
- $sql = "SELECT fk_product_pere FROM " . $this->db->prefix() . "product_association WHERE fk_product_fils IN (" . $arrayForIN . ")";
- $result = $this->db->query($sql);
- if (pg_num_rows($result) == 0) {
- $this->pere[] = $object->fk_product[0];
- } else {
- while ($sqlDataResult = pg_fetch_assoc($result)) {
- $this->pere[0] = $sqlDataResult['fk_product_pere'];
- }
- }
- }
- private function getProductAssociationPere($object)
- {
- $sql = "SELECT fk_product_pere FROM " . $this->db->prefix() . "product_association WHERE fk_product_fils = " . $object->fk_product;
- $result = $this->db->query($sql);
- if (pg_num_rows($result) == 0) {
- $this->pere[] = $object->fk_product;
- } else {
- while ($sqlDataResult = pg_fetch_assoc($result)) {
- $this->pere[] = $sqlDataResult['fk_product_pere'];
- }
- }
- }
- private function getDatecFromFacture($object)
- {
- $sql = "SELECT datec FROM " . $this->db->prefix() . "facture WHERE rowid = " . $object->fk_facture;
- $result = $this->db->query($sql);
- while ($sqlDataResult = pg_fetch_assoc($result)) {
- $datec = $sqlDataResult['datec'];
- }
- $this->datec = $datec;
- }
- private function getDurationFromProductsByFilsID($filsId)
- {
- $sql = "SELECT validperiod, occasions FROM " . $this->db->prefix() . "product_extrafields WHERE fk_object = " . $filsId;
- $result = $this->db->query($sql);
- while ($sqlDataResult = pg_fetch_assoc($result)) {
- $this->validperiod = $sqlDataResult['validperiod'] ? $sqlDataResult['validperiod'] : 366;
- $this->occasions = $sqlDataResult['occasions'];
- }
- }
- /* private function getPeriodByPeriodSelector($sqlDataResult){
- $sql = "SELECT occasion_duration FROM " . $this->db->prefix() . "bbus_bboccasionsdurations WHERE occasion_key = " . $sqlDataResult['period_selector'];
- echo $sql;
- $result = $this->db->query($sql);
- while ($sqlDataResult = pg_fetch_assoc($result)) {
- $this->validperiod = $sqlDataResult['occasion_duration'];
- }
- }
- */
- private function createTicketObject($pere, $ticket_id, $facture_id = null, $ref = null, $group_id = null, $booking_history = null)
- {
- $this->getDurationFromProductsByFilsID($ticket_id);
- $ticket = new BbTicket($this->db);
- $ticket->fk_facture = $facture_id;
- $ticket->bundle_id = $pere;
- $ticket->usable_occasions = $this->occasions;
- $ticket->usage = '0';
- $ticket->available_at = $this->getAvailableAtDate($this->datec, $this->validperiod);
- $ticket->invoice_number = $ref;
- $ticket->fk_settlements_group_id = $group_id;
- $ticket->ticket_id = $ticket_id;
- $ticket->booking_history_id = $booking_history;
- return $ticket;
- }
- private function createTicketObject2($pere, $object, $group_id)
- {
- $this->getDurationFromProductsByFilsID($object->fk_product);
- $ticket = new BbTicket($this->db);
- $ticket->fk_facture = $object->fk_facture;
- $ticket->bundle_id = $pere;
- $ticket->usable_occasions = $this->occasions;
- $ticket->usage = '0';
- $ticket->available_at = $this->getAvailableAtDate($this->datec, $this->validperiod);
- $ticket->ticket_id = $object->fk_product;
- $ticket->fk_settlements_group_id = $group_id;
- return $ticket;
- }
- private function getAvailableAtDate($date, $validperiod)
- {
- $available_at = date('Y-m-d H:i:s', strtotime($date . ' +' . $validperiod . ' days'));
- return $available_at;
- }
- }
|