<?php
namespace App\Http\Controllers;
use Illuminate\Support\Collection;
use PDF;
use Mail;
use App\Mail\ReportMail;
use App\Models\Settings\Configuration;
use App\Models\Settings\State;
use App\Models\Facility\Facility;
use App\Models\Sales\ReportData;
use App\Helpers\ReportClass;
use App\Models\Sales\Invoice;
use App\Models\Sales\Transaction;
use App\Models\Sales\Ticket;
use App\Models\Sales\TicketSeat;
use App\Models\Sales\PaymentMethod;
use App\Models\Sales\PaymentType;
use App\Models\Sales\FeeWaiverApproval;
use App\Models\Sales\ShowCancel;


class StatusCheckController extends Controller
{
    public function checkstatus()
    {
        $checkcount =0;
        //$transactions = Transaction::where('status',0)->where('status_code',NULL)->where('checkcount','<',3)->whereNotNull('public_user_id')->get();
        $transactions = Transaction::where('id',52)->get();  ///for testing
        foreach($transactions as $transaction)
        {
            $transaction_id = $transaction->id;
            $mid = $transaction->mid;
            $encykey = $transaction->encykey;
            $order_id = $transaction->order_id;
            //generate merchant request 
            include 'worldline/AWLMEAPI.php';
            //generate merchant request from Helper file
            //create an Object of the above included class
            $obj = new \AWLMEAPI();
            //create an object of Request Message
            $pgMeTrnRefNo ="";
            $resMsgDTO = $obj->getTransactionStatus($mid , $order_id , $pgMeTrnRefNo , $encykey);
            //dd($resMsgDTO);
            $trnsrefno = $resMsgDTO->getPgMeTrnRefNo();
            $trnsamount = $resMsgDTO->getTrnAmt();
            $trnsstatuscode = $resMsgDTO->getStatusCode();
            $trnsstatusdesc = $resMsgDTO->getStatusDesc();
            $trnsreqdate = $resMsgDTO->getTrnReqDate();
            $trnsresponsecode = $resMsgDTO->getResponseCode();
            $trns_rrn = $resMsgDTO->getRrn();
            $trnsauthzcode = $resMsgDTO->getAuthZCode();
            //dd($trnsstatuscode);
            ///checkcount ++
            ///update transaction table
            $checkcount++;
            $transaction_checkcount_upd = Transaction::where('id',$transaction_id)->first();
            $transaction_checkcount_upd->checkcount = $checkcount;
            $transaction_checkcount_upd->save();
            /////////
            if($trnsstatuscode=="S")
            {
                ///update transaction table
                $transaction_upd = Transaction::where('id',$transaction_id)->first();
                $transaction_upd->status = 1;
                $transaction_upd->transaction_refno = $trnsrefno;
                $transaction_upd->status_code = $trnsstatuscode;
                $transaction_upd->status_desc = $trnsstatusdesc;
                $transaction_upd->transaction_req_date = $trnsreqdate;
                $transaction_upd->response_code = $trnsresponsecode;
                $transaction_upd->rrn = $trns_rrn;
                $transaction_upd->auth_zcode = $trnsauthzcode;
                $transaction_upd->save();
                ////update invoice table
                $invocies = Invoice::where('transaction_id',$transaction_id)->first();
                $invoice_id = $invoices->id;
                $invoices->status = 1;
                $invoices->save();
                ////update ticket table
                $ticekts = Ticket::where('invoice_id',$invoice_id)->get();
                foreach($ticekts as $ticket)
                {
                    $ticket_id = $ticket->id;
                    $ticket_upd = Ticket::where('id',$ticket_id)->first();
                    $ticket_upd->status = 1;
                    $ticket_upd->save();
                    ////update ticket seat table
                    $ticket_seats = TicketSeat::where('ticket_id',$ticket_id)->get();
                    foreach($ticket_seats as $ticket_seat)
                    {
                        $ticket_seat_id = $ticket_seat->id;
                        $ticket_seat_upd = TicketSeat::where('id',$ticket_seat_id)->first();
                        $ticket_seat_upd->status = 1;
                        $ticket_seat_upd->save();
                    }
                }
            }
            if($trnsstatuscode=="S")
            {
                ///update transaction table
                $transaction_upd = Transaction::where('id',$transaction_id)->first();
                $transaction_upd->status = 2;
                $transaction_upd->transaction_refno = $trnsrefno;
                $transaction_upd->status_code = $trnsstatuscode;
                $transaction_upd->status_desc = $trnsstatusdesc;
                $transaction_upd->transaction_req_date = $trnsreqdate;
                $transaction_upd->response_code = $trnsresponsecode;
                $transaction_upd->rrn = $trns_rrn;
                $transaction_upd->auth_zcode = $trnsauthzcode;
                $transaction_upd->save();
            }

        }
    }
    
}
