<?php

namespace app\Helpers;
use App\Models\Sales\Ticket;
use App\Models\Facility\Facility;
use App\Models\Sales\Invoice;
use App\Models\Sales\TicketSeat;
use App\Models\Settings\Configuration;
use App\Models\Sales\CancelTicket;
use App\Models\Facility\ShowTimings;
use App\Models\Mail\SMSLog;

/**
* class MSG91 to send SMS on Mobile Numbers.
* @author Shashank Tiwari
*/
class SMSClass {

    function __construct() {
       
    }

    private $API_KEY = '$2a$10$8YjLA.lwclMz8AvLY4GHYubefWlKuwzf3xGFXeHvyfBetUgE2VGy';
    private $SMS_URL = 'http://sms.alp-ts.com:8888/alp-sms-api';
    //private $SENDER_ID = "VERIFY";
    //private $ROUTE_NO = 4;
    //private $RESPONSE_TYPE = 'json';

    public function sendSMS($OTP, $mobileNumber, $country_code='+91'){
        $isError = 0;
        $errorMessage = true;
        $isError = '';
        $output = '';
        $curl_response_code ='';

        $message = "Welcome to KSSTM Online ticketing, Your OTP is : $OTP KSSTM";
        $postData = "<?xml version='1.0' encoding='UTF-8'?><smsRequest><head><templateid>1707165330305606203</templateid><apikey>$2a$10$8YjLA.lwclMz8AvLY4GHYubefWlKuwzf3xGFXeHvyfBetUgE2VGy</apikey><message>Welcome to KSSTM Online ticketing, Your OTP is : $OTP KSSTM</message><unicode>0</unicode><route>T</route></head><request><number>$mobileNumber</number></request></smsRequest>";
     
        $url = $this->SMS_URL;
        $headers = array(
           "Content-Type: application/xml",
           "Accept: application/xml",
        );
     
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_POSTFIELDS => $postData
        ));

      
     
     
        //Ignore SSL certificate verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     
     
        //get response
        $output = curl_exec($ch);
        $curl_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     
        //Print error if any
        if (curl_errno($ch)) {
            $isError = true;
            $errorMessage = curl_error($ch);
        }
        curl_close($ch);
        ////insert into sms log table//////////////////
        $smstime = date("Y-m-d H:i:s");
        $sms_functionality = "Send OTP through SMS";
        $insert_smslog = SMSLog::create([
                'sms_content' => $message,
                'sms_functionality' => $sms_functionality,
                'curl_output' => $output,
                'curl_output_code' => $curl_response_code,
                'mobile_number' => $mobileNumber,
                'sms_time' => $smstime,
            ]);
        //////////////////////////////////////////
        if($isError){
            return array('error' => 1 , 'message' => $errorMessage);
        }else{
            return array('error' => 0, 'message' => $output );
        }
    }

    public function resendSMS($mobileNumber, $country_code='+91'){
        $isError = 0;
        $errorMessage = true;
        $isError = '';
        $output = '';
        $curl_response_code ='';
        //get OTP from session variable
        $OTP = \Session::get('OTP');

        if($OTP)
        {
            $postDataarr=[];
            $postDataarr['route']='otp';
            $postDataarr['variables_values']=$OTP;
            $postDataarr['numbers']=$mobileNumber;
            $postData = json_encode($postDataarr);

            $url = "https://www.fast2sms.com/dev/bulkV2";
            $headers = array(
               "Content-Type: application/json",
               "authorization: 7OWKBErFvCjA0txJRyzVXl8pN1sou6M4Y53wGHdimbaUcZ2eLhskod7KYJRUWzyfw3q02CLAjFcvGIO4",
            );
         
            $ch = curl_init();
            curl_setopt_array($ch, array(
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POST => true,
                CURLOPT_HTTPHEADER => $headers,
                CURLOPT_POSTFIELDS => $postData
            ));

          
         
         
            //Ignore SSL certificate verification
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
         
         
            //get response
            $output = curl_exec($ch);
            $curl_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
         
            //Print error if any
            if (curl_errno($ch)) {
                $isError = true;
                $errorMessage = curl_error($ch);
            }
            curl_close($ch);

             ////insert into sms log table//////////////////
            $smstime = date("Y-m-d H:i:s");
            $sms_functionality = "Send OTP through SMS";
            $insert_smslog = SMSLog::create([
                    'sms_content' => $OTP,
                    'sms_functionality' => $sms_functionality,
                    'curl_output' => $output,
                    'curl_output_code' => $curl_response_code,
                    'mobile_number' => $mobileNumber,
                    'sms_time' => $smstime,
                ]);
            //////////////////////////////////////////
            if($isError){
                return array('error' => 1 , 'message' => $errorMessage);
            }else{
                return array('error' => 0, 'message' => $output );
            }


        }
        else
        {
            return array('error' => 1, 'message' => "No Otp in session" );
        }

       
     
        
       
    }

    public function sendSMSRegistration($mobileNumber,$country_code='+91')
    {
        $isError = 0;
        $errorMessage = true;
        $isError = '';
        $output = '';
        $curl_response_code = "";
        //Your message to send, Adding URL encoding.
        $message = "You have Registered successfully on KSSTM Online Ticketing Application.";
        $postData = "<?xml version='1.0' encoding='UTF-8'?><smsRequest><head><templateid>1707165330323034317</templateid><apikey>$2a$10$8YjLA.lwclMz8AvLY4GHYubefWlKuwzf3xGFXeHvyfBetUgE2VGy</apikey><message> You have Registered successfully on KSSTM Online Ticketing Application.</message><unicode>0</unicode><route>T</route></head><request><number>$mobileNumber</number></request></smsRequest>";
     
        $url = $this->SMS_URL;
        $headers = array(
           "Content-Type: application/xml",
           "Accept: application/xml",
        );
     
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_POSTFIELDS => $postData
        ));
        //Ignore SSL certificate verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        //get response
        $output = curl_exec($ch);
        $curl_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        //Print error if any
        if (curl_errno($ch)) {
            $isError = true;
            $errorMessage = curl_error($ch);
        }
        curl_close($ch);
        ////insert into sms log table//////////////////
        $smstime = date("Y-m-d H:i:s");
        $sms_functionality = "Send Registration status through SMS";
        $insert_smslog = SMSLog::create([
                'sms_content' => $message,
                'sms_functionality' => $sms_functionality,
                'curl_output' => $output,
                'curl_output_code' => $curl_response_code,
                'mobile_number' => $mobileNumber,
                'sms_time' => $smstime,
            ]);
        //////////////////////////////////////////
        if($isError)
        {
            return array('error' => 1 , 'message' => $errorMessage);
        }
        else
        {
            return array('error' => 0, 'message' => $output );
        }
    }

    public function sendSMSticket($invoice_id, $mobileNumber, $country_code='+91')
    {
        $isError = 0;
        $errorMessage = true;
        $isError = '';
        $output = '';
        $curl_response_code = "";
        //Your message to send, Adding URL encoding.
        
        $invoices = Invoice::where('id',$invoice_id)->where('status',1)->first();
        if(isset($invoices))
        {
            $invoice_date = $invoices->invoice_date;
            $invoice_date_new = date("d-M-Y", strtotime($invoice_date));
            $show_date = $invoices->show_date;
            $show_date = date("d-M-Y", strtotime($show_date));
            $invoice_month = date('m',strtotime($invoice_date));
            $invoice_year = date('Y',strtotime($invoice_date));
            $invoice_no = $invoices->invoice_no;
            $org_code = $invoices->organisation->short_code;
            if($invoice_month<4)
                $invoice_year--;
            $nextyear = $invoice_year+1;
            $invoice_number = $org_code.'/'.$invoice_no.'/'.$invoice_year.'-'.$nextyear;

            $org_name = $invoices->organisation->organisation_name;
            $cityname = $invoices->organisation->city;
            $center = $org_name." ".$cityname;
        }
        $message = "Welcome to KSSTM Online ticketing, You have successfully booked ticket $invoice_number on $show_date at $center.";
        $postData = "<?xml version='1.0' encoding='UTF-8'?><smsRequest><head><templateid>1707165330332554513</templateid><apikey>$2a$10$8YjLA.lwclMz8AvLY4GHYubefWlKuwzf3xGFXeHvyfBetUgE2VGy</apikey><message>Welcome to KSSTM Online ticketing, You have successfully booked ticket $invoice_number on $show_date at $center. </message><unicode>0</unicode><route>T</route></head><request><number>$mobileNumber</number></request></smsRequest>";
     
        $url = $this->SMS_URL;
        $headers = array(
           "Content-Type: application/xml",
           "Accept: application/xml",
        );
        
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_POSTFIELDS => $postData
        ));
        //Ignore SSL certificate verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        //get response
        $output = curl_exec($ch);
        $curl_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        //Print error if any
        if (curl_errno($ch)) {
            $isError = true;
            $errorMessage = curl_error($ch);
        }
        curl_close($ch);
        ////insert into sms log table//////////////////
        $smstime = date("Y-m-d H:i:s");
        $sms_functionality = "Send ticket booking status through SMS";
        $insert_smslog = SMSLog::create([
                'sms_content' => $message,
                'sms_functionality' => $sms_functionality,
                'curl_output' => $output,
                'curl_output_code' => $curl_response_code,
                'mobile_number' => $mobileNumber,
                'sms_time' => $smstime,
            ]);
        //////////////////////////////////////////
        if($isError)
        {
            return array('error' => 1 , 'message' => $errorMessage);
        }
        else
        {
            return array('error' => 0, 'message' => $output );
        }
    }

    public function sendSMSpassword($password,$mobileNumber,$country_code='+91')
    {
        $isError = 0;
        $errorMessage = true;
        $isError = '';
        $output = '';
        $curl_response_code = "";
        //Your message to send, Adding URL encoding.
        $message = "Your Password is reset for this mobile number on KSSTM Online Ticketing Application. Your new Password is $password";
        $postData = "<?xml version='1.0' encoding='UTF-8'?><smsRequest><head><templateid>1707165330341920175</templateid><apikey>$2a$10$8YjLA.lwclMz8AvLY4GHYubefWlKuwzf3xGFXeHvyfBetUgE2VGy</apikey><message> Your Password is reset for this mobile number on KSSTM Online Ticketing Application. Your new Password is $password </message><unicode>0</unicode><route>T</route></head><request><number>$mobileNumber</number></request></smsRequest>";
     
        $url = $this->SMS_URL;
        $headers = array(
           "Content-Type: application/xml",
           "Accept: application/xml",
        );
     
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_POSTFIELDS => $postData
        ));
        //Ignore SSL certificate verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        //get response
        $output = curl_exec($ch);
        $curl_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        //Print error if any
        if (curl_errno($ch)) {
            $isError = true;
            $errorMessage = curl_error($ch);
        }
        curl_close($ch);
        ////insert into sms log table//////////////////
        $smstime = date("Y-m-d H:i:s");
        $sms_functionality = "Send reset password through SMS";
        $insert_smslog = SMSLog::create([
                'sms_content' => $message,
                'sms_functionality' => $sms_functionality,
                'curl_output' => $output,
                'curl_output_code' => $curl_response_code,
                'mobile_number' => $mobileNumber,
                'sms_time' => $smstime,
            ]);
        //////////////////////////////////////////
        if($isError)
        {
            return array('error' => 1 , 'message' => $errorMessage);
        }
        else
        {
            return array('error' => 0, 'message' => $output );
        }
    }

    public function sendSMSCancelTicket($invoice_id, $mobileNumber, $country_code='+91')
    {
        $isError = 0;
        $errorMessage = true;
        $isError = '';
        $output = '';
        $curl_response_code = "";
        //Your message to send, Adding URL encoding.
        
        $invoices = Invoice::where('id',$invoice_id)->first();
        if(isset($invoices))
        {
            $invoice_date = $invoices->invoice_date;
            $invoice_date_new = date("d-M-Y", strtotime($invoice_date));
            $show_date = $invoices->show_date;
            $show_date = date("d-M-Y", strtotime($show_date));
            $invoice_month = date('m',strtotime($invoice_date));
            $invoice_year = date('Y',strtotime($invoice_date));
            $invoice_no = $invoices->invoice_no;
            $org_code = $invoices->organisation->short_code;
            if($invoice_month<4)
                $invoice_year--;
            $nextyear = $invoice_year+1;
            $invoice_number = $org_code.'/'.$invoice_no.'/'.$invoice_year.'-'.$nextyear;

            $org_name = $invoices->organisation->organisation_name;
            $cityname = $invoices->organisation->city;
            $center = $org_name." ".$cityname;
        }
        //dd("here");
        $cancelticket = CancelTicket::where('invoice_id',$invoice_id)->where('refund_status',0)->first();
        //dd($cancelticket);
        if(isset($cancelticket))
        {
            $refund_amount  = $cancelticket->refund_amount;
        }
        //dd($refund_amount);
        $message = "Your ticket $invoice_number has been cancelled. Refund of Rs. $refund_amount will be credited to your account.";
        $postData = "<?xml version='1.0' encoding='UTF-8'?><smsRequest><head><templateid>1707165330353047846</templateid><apikey>$2a$10$8YjLA.lwclMz8AvLY4GHYubefWlKuwzf3xGFXeHvyfBetUgE2VGy</apikey><message>Your ticket $invoice_number has been cancelled. Refund of Rs. $refund_amount will be credited to your account. </message><unicode>0</unicode><route>T</route></head><request><number>$mobileNumber</number></request></smsRequest>";
     
        $url = $this->SMS_URL;
        $headers = array(
           "Content-Type: application/xml",
           "Accept: application/xml",
        );
     
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_POSTFIELDS => $postData
        ));
        //Ignore SSL certificate verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        //get response
        $output = curl_exec($ch);
        $curl_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        //Print error if any
        if (curl_errno($ch)) {
            $isError = true;
            $errorMessage = curl_error($ch);
        }
        curl_close($ch);

       
        ////insert into sms log table//////////////////
        $smstime = date("Y-m-d H:i:s");
        $sms_functionality = "Send cancel ticket status through SMS";
        $insert_smslog = SMSLog::create([
                'sms_content' => $message,
                'sms_functionality' => $sms_functionality,
                'curl_output' => $output,
                'curl_output_code' => $curl_response_code,
                'mobile_number' => $mobileNumber,
                'sms_time' => $smstime,
            ]);
        //////////////////////////////////////////
        if($isError)
        {
            return array('error' => 1 , 'message' => $errorMessage);
        }
        else
        {
            return array('error' => 0, 'message' => $output );
        }
    }  

    public function sendSMSCancelShows($facility_id, $show_time_id, $show_date, $mobileNumber, $country_code='+91')
    {
        $isError = 0;
        $errorMessage = true;
        $isError = '';
        $output = '';
        $curl_response_code = "";
        //Your message to send, Adding URL encoding.
        
        $facility = Facility::where('id',$facility_id)->where('active_flag',1)->first();
        if(isset($facility))
        {
            $facility_name = $faciltiy->facility_name;
        }
        $showtimings = ShowTimings::where('id',$show_time_id)->where('active_flag',1)->first();
        if(isset($showtimings))
        {
            $from_time = $showtimings->from_time;
            $from_time = date("g:i A", strtotime($from_time));
            $to_time = $showtimings->to_time;
            $to_time = date("g:i A", strtotime($to_time));
            $show_time = $from_time ." - ".$to_time;
        }
        $message = "We are sorry to inform you that $facility_name show has been cancelled for $show_date, $show_time";
        $postData = "<?xml version='1.0' encoding='UTF-8'?><smsRequest><head><templateid>1707165330361790342</templateid><apikey>$2a$10$8YjLA.lwclMz8AvLY4GHYubefWlKuwzf3xGFXeHvyfBetUgE2VGy</apikey><message>We are sorry to inform you that $facility_name show has been cancelled for $show_date, $show_time </message><unicode>0</unicode><route>T</route></head><request><number>$mobileNumber</number></request></smsRequest>";
     
        $url = $this->SMS_URL;
        $headers = array(
           "Content-Type: application/xml",
           "Accept: application/xml",
        );
     
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_POSTFIELDS => $postData
        ));
        //Ignore SSL certificate verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        //get response
        $output = curl_exec($ch);
        $curl_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        //Print error if any
        if (curl_errno($ch)) {
            $isError = true;
            $errorMessage = curl_error($ch);
        }
        curl_close($ch);
        ////insert into sms log table//////////////////
        $smstime = date("Y-m-d H:i:s");
        $sms_functionality = "Send cancel show through SMS";
        $insert_smslog = SMSLog::create([
                'sms_content' => $message,
                'sms_functionality' => $sms_functionality,
                'curl_output' => $output,
                'curl_output_code' => $curl_response_code,
                'mobile_number' => $mobileNumber,
                'sms_time' => $smstime,
            ]);
        //////////////////////////////////////////
        if($isError)
        {
            return array('error' => 1 , 'message' => $errorMessage);
        }
        else
        {
            return array('error' => 0, 'message' => $output );
        }
    }      

}
?>