<?php

namespace App\Listeners\Subscribes;


use App\Events\SalesSubscribesEvent;
use App\Models\ProductSalesSubscribes;
use App\Models\SalesSubscribes;
use App\Traits\GeneralTrait;
use App\Models\Journalizing;
use DB;

class SalesSubscribesListener
{


    use GeneralTrait;

    public function __construct()
    {

    }


    public function handle(SalesSubscribesEvent $event)
    {

        $ID=$event->ID;
        $Bill=SalesSubscribes::findOrFail($ID);

            if($event->requests['Payment_Method'] == 'Cash'){


                if(!empty($event->requests['Total_Price'])){

                    $paid = $event->requests['Total_Price'] ;

                }else{

                    $paid = $event->requests['Total_Price'] ;
                }

            }elseif($event->requests['Payment_Method'] == 'Later'){

                $paid = $event->requests['Total_Price'] ;

            }

        //Details


        if(!empty($event->requests['Unit'])){


            $P_Ar_Name=$event->requests['P_Ar_Name'];
            $P_En_Name=$event->requests['P_En_Name'];
            $Unit=$event->requests['Unit'];
            $P_Code=$event->requests['P_Code'];
            $Price=$event->requests['Price'];
            $Total=$event->requests['Total'];
            $Product=$event->requests['Product'];
            $VOne=$event->requests['VOne'];
            $VTwo=$event->requests['VTwo'];
            $V_Name=$event->requests['V_Name'];
            $VV_Name=$event->requests['VV_Name'];
            $Sub_Date=$event->requests['Sub_Date'];
            $Sub_Type=$event->requests['Sub_Type'];

            for($i=0 ; $i < count($Unit) ; $i++){


                $uu['Product_Code']=$P_Code[$i];
                $uu['P_Ar_Name']=$P_Ar_Name[$i];
                $uu['P_En_Name']=$P_En_Name[$i];
                $uu['V_Name']=$V_Name[$i];
                $uu['VV_Name']=$VV_Name[$i];
                $uu['Price']=$Price[$i];
                $uu['Sub_Date']=$Sub_Date[$i];
                $uu['Sub_Type']=$Sub_Type[$i];
                $uu['Price']=$Price[$i];
                $uu['Total']=$Total[$i];
                $uu['Product']=$Product[$i];
                $uu['V1']=$VOne[$i];
                $uu['V2']=$VTwo[$i];
                $uu['Unit']=$Unit[$i];
                $uu['Subscribe']=$ID;

                ProductSalesSubscribes::create($uu);


                if($Sub_Type[$i] == '7D'){

                    $da=7;
                }elseif($Sub_Type[$i] == '14D'){
                    $da=14;

                }elseif($Sub_Type[$i] == '21D'){
                    $da=21;

                }elseif($Sub_Type[$i] == '1M'){
                    $da=30;

                }elseif($Sub_Type[$i] == '2M'){
                    $da=60;

                }elseif($Sub_Type[$i] == '3M'){

                    $da=90;
                }elseif($Sub_Type[$i] == '4M'){

                    $da=120;
                }elseif($Sub_Type[$i] == '5M'){
                    $da=150;

                }elseif($Sub_Type[$i] == '6M'){

                    $da=180;
                }elseif($Sub_Type[$i] == '1Y'){
                    $da=365;

                }elseif($Sub_Type[$i] == '2Y'){
                    $da=730;

                }



                $newDate =date('Y-m-d', strtotime($Sub_Date[$i]. ' + '.$da.' days')); ;

                $data=[

                    'start_date'=>$newDate,
                    'end_date'=>$newDate,
                    'event_name_ar'=>'انتهاء اشتراك',
                    'event_name_en'=>'Expired Supscribe',
                    'type'=>'المبيعات',
                    'type_id'=>null,
                    'type_code'=>$Bill->Code,
                    'emp'=>null,
                    'client'=>$event->requests['Client'],
                    'product'=>$Product[$i],
                    'customer'=>null,

                ];

                $this->CreateEvent($data);


            }
        }



               //Jounralizing
$type_ar='مبيعات اشتراكات';
$type_en='Sales Subscribes';
$debitor=$paid;
$creditor=$paid;
$account_cred=0;
$account_debt=0;
$date=$event->requests['Date'];
$draw=$event->requests['Draw'];
$coin=$event->requests['Coin'];
$cost_center=null;
$note=$event->requests['Note'];
$total=$paid;
$joun_id=0;
$Code_Type=$Bill->Code;
$Code=0;

       $data=[

           'type_ar'=>$type_ar,
           'type_en'=>$type_en,
           'code'=>$Code,
           'code_type'=>$Code_Type,
           'date'=>$date,
           'draw'=>$draw,
           'coin'=>$coin,
           'cost_center'=>$cost_center,
           'note'=>$note,
           'debitor_cred'=>0,
           'creditor_cred'=>$creditor,
           'account_cred'=>$account_cred,
           'debitor_debt'=>$debitor,
           'creditor_debt'=>0,
           'account_debt'=>$account_debt,
           'total'=>$total,
           'joun_id'=>$joun_id,
       ];

        $joun_id=$this->CreateNewJun($data);
        $new=Journalizing::findOrFail($joun_id);
        $data['code']=$new->Code;
        $data['joun_id']=$joun_id;



        if($event->requests['Payment_Method'] == 'Cash'){



            $data['creditor_cred'] = $paid;
            $data['account_cred'] = $event->requests['Client'];
            $data['debitor_debt'] = $paid;
            $data['account_debt'] = $event->requests['Safe'];
            $this->JunDetails($data);

            $data['creditor_cred'] = $paid;
            $data['account_cred'] = 48;
            $data['debitor_debt'] = $paid;
            $data['account_debt'] = $event->requests['Client'];
            $this->JunDetails($data);



        }elseif(request('Payment_Method') == 'Later'){


            $data['creditor_cred'] = $event->requests['Total_Price'];
            $data['account_cred'] = 48;
            $data['debitor_debt'] = $event->requests['Total_Price'];
            $data['account_debt'] = $event->requests['Client'];
            $this->JunDetails($data);

                if($paid != 0 or $paid > 0){

                    $data['creditor_cred'] = $paid;
                    $data['account_cred'] = $event->requests['Client'];
                    $data['debitor_debt'] = $paid;
                    $data['account_debt'] = $event->requests['Safe'];
                    $this->JunDetails($data);


                }


            }


}

}
