<?php

namespace App\Http\Resources\Reports\Accounting;


use App\Models\Employess;
use App\Models\GeneralDaily;
use App\Models\PaymentVoucherDetails;
use App\Models\Purchases;
use App\Models\ReciptVoucherDetails;
use App\Models\Sales;
use Illuminate\Http\Resources\Json\JsonResource;
use DB;
use Auth;
use DateTime;

class VendorDebtRestructuringFilterApiResources extends JsonResource
{


    public function toArray($request)
    {


        $lang = $request->header('lang', '');
        $Date_Type = $request->get('Date_Type');

        if($Date_Type == 1){
            $type='Date';
        }else{
            $type='Later_Due';
        }

        $newID=0;

        if(app()->getLocale() == 'ar' ){
            $Name=$this->Name;


        }else{
            $Name=$this->NameEn;


        }


        $date7=date("Y-m-d", strtotime("-6 days", strtotime(date('Y-m-d'))));
        $date7Minus=date("Y-m-d", strtotime("-7 days", strtotime(date('Y-m-d'))));
        $date14=date("Y-m-d", strtotime("-13 days", strtotime(date('Y-m-d'))));
        $date14Minus=date("Y-m-d", strtotime("-14 days", strtotime(date('Y-m-d'))));
        $date21=date("Y-m-d", strtotime("-20 days", strtotime(date('Y-m-d'))));
        $date21Minus=date("Y-m-d", strtotime("-21 days", strtotime(date('Y-m-d'))));
        $date31=date("Y-m-d", strtotime("-30 days", strtotime(date('Y-m-d'))));
        $date31Minus=date("Y-m-d", strtotime("-31 days", strtotime(date('Y-m-d'))));
        $date3M=date("Y-m-d", strtotime("-89 days", strtotime(date('Y-m-d'))));
        $date3MMinus=date("Y-m-d", strtotime("-90 days", strtotime(date('Y-m-d'))));
        $date6M=date("Y-m-d", strtotime("-179 days", strtotime(date('Y-m-d'))));
        $date6MMinus=date("Y-m-d", strtotime("-180 days", strtotime(date('Y-m-d'))));
        $date9M=date("Y-m-d", strtotime("-269 days", strtotime(date('Y-m-d'))));
        $date9MMinus=date("Y-m-d", strtotime("-270 days", strtotime(date('Y-m-d'))));
        $date12M=date("Y-m-d", strtotime("-360 days", strtotime(date('Y-m-d'))));
        $date12MMinus=date("Y-m-d", strtotime("-361 days", strtotime(date('Y-m-d'))));
        $dateY=date("Y-m-d", strtotime("-1000 days", strtotime(date('Y-m-d'))));
        $dateYMinus=date("Y-m-d", strtotime("-1000 days", strtotime(date('Y-m-d'))));

        $Count7=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')
            ->whereBetween($type,[$date7,date('Y-m-d')])
            ->where('Edit',0)
            ->where('Delete',0)
            ->count();

        $Count14=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')
            ->whereBetween($type,[$date14,$date7Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->count();

        $Count21=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')
            ->whereBetween($type,[$date21,$date14Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->count();

        $Count31=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')
            ->whereBetween($type,[$date31,$date21Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->count();

        $Count3M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')
            ->whereBetween($type,[$date3M,$date31Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->count();

        $Count6M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date6M,$date3MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->count();

        $Count9M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date9M,$date6MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->count();

        $Count12M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date12M,$date9MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->count();

        $CountY=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$dateY,$date12MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->count();

        $Total7=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')
            ->whereBetween($type,[$date7,date('Y-m-d')])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Total_Net');

        $Total14=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date14,$date7Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Total_Net');

        $Total21=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date21,$date14Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Total_Net');

        $Total31=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date31,$date21Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Total_Net');

        $Total3M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date3M,$date31Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Total_Net');

        $Total6M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date6M,$date3MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Total_Net');

        $Total9M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date9M,$date6MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Total_Net');

        $Total12M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date12M,$date9MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Total_Net');

        $TotalY=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$dateY,$date12MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Total_Net');


        $TotalPay7=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')
            ->whereBetween($type,[$date7,date('Y-m-d')])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Pay');

        $TotalPay14=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date14,$date7Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Pay');

        $TotalPay21=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date21,$date14Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Pay');

        $TotalPay31=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date31,$date21Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Pay');

        $TotalPay3M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date3M,$date31Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Pay');

        $TotalPay6M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date6M,$date3MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Pay');

        $TotalPay9M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date9M,$date6MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Pay');

        $TotalPay12M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date12M,$date9MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Pay');

        $TotalPayY=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$dateY,$date12MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get()->sum('Pay');







        $Get7=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date7,date('Y-m-d')])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get('Code');

        $Get14=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date14,$date7Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get('Code');

        $Get21=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date21,$date14Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get('Code');

        $Get31=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date31,$date21Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get('Code');

        $Get3M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date3M,$date31Minus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get('Code');

        $Get6M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date6M,$date3MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get('Code');

        $Get9M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date9M,$date6MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get('Code');

        $Get12M=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$date12M,$date9MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get('Code');

        $GetY=Purchases::
        where('Vendor',$this->id)
            ->where('Payment_Method','Later')

            ->whereBetween($type,[$dateY,$date12MMinus])
            ->where('Edit',0)
            ->where('Delete',0)
            ->get('Code');

        $new7=[];
        for($i=0 ; $i < count($Get7) ; $i++){

            array_push($new7,$Get7[$i]['Code']);

        }
        $Totrecipt7=PaymentVoucherDetails::whereRelation('PV_ID','Status',1)->whereRelation('PV_ID','Delete',null)->whereRelation('PV_ID','Edit',null)->whereIn('Bill_Num',$new7)->get()->sum('Debitor');


        $new14=[];
        for($i=0 ; $i < count($Get14) ; $i++){

            array_push($new14,$Get14[$i]['Code']);

        }
        $Totrecipt14=PaymentVoucherDetails::whereRelation('PV_ID','Status',1)->whereRelation('PV_ID','Delete',null)->whereRelation('PV_ID','Edit',null)->whereIn('Bill_Num',$new14)->get()->sum('Debitor');

        $new21=[];
        for($i=0 ; $i < count($Get21) ; $i++){

            array_push($new21,$Get21[$i]['Code']);

        }
        $Totrecipt21=PaymentVoucherDetails::whereRelation('PV_ID','Status',1)->whereRelation('PV_ID','Delete',null)->whereRelation('PV_ID','Edit',null)->whereIn('Bill_Num',$new21)->get()->sum('Debitor');

        $new31=[];
        for($i=0 ; $i < count($Get31) ; $i++){

            array_push($new31,$Get31[$i]['Code']);

        }
        $Totrecipt31=PaymentVoucherDetails::whereRelation('PV_ID','Status',1)->whereRelation('PV_ID','Delete',null)->whereRelation('PV_ID','Edit',null)->whereIn('Bill_Num',$new31)->get()->sum('Debitor');

        $new3M=[];
        for($i=0 ; $i < count($Get3M) ; $i++){

            array_push($new3M,$Get3M[$i]['Code']);

        }
        $Totrecipt3M=PaymentVoucherDetails::whereRelation('PV_ID','Status',1)->whereRelation('PV_ID','Delete',null)->whereRelation('PV_ID','Edit',null)->whereIn('Bill_Num',$new3M)->get()->sum('Debitor');

        $new6M=[];
        for($i=0 ; $i < count($Get6M) ; $i++){

            array_push($new6M,$Get6M[$i]['Code']);

        }
        $Totrecipt6M=PaymentVoucherDetails::whereRelation('PV_ID','Status',1)->whereRelation('PV_ID','Delete',null)->whereRelation('PV_ID','Edit',null)->whereIn('Bill_Num',$new6M)->get()->sum('Debitor');

        $new9M=[];
        for($i=0 ; $i < count($Get9M) ; $i++){

            array_push($new9M,$Get9M[$i]['Code']);

        }
        $Totrecipt9M=PaymentVoucherDetails::whereRelation('PV_ID','Status',1)->whereRelation('PV_ID','Delete',null)->whereRelation('PV_ID','Edit',null)->whereIn('Bill_Num',$new9M)->get()->sum('Debitor');

        $new12M=[];
        for($i=0 ; $i < count($Get12M) ; $i++){

            array_push($new12M,$Get12M[$i]['Code']);

        }
        $Totrecipt12M=PaymentVoucherDetails::whereRelation('PV_ID','Status',1)->whereRelation('PV_ID','Delete',null)->whereRelation('PV_ID','Edit',null)->whereIn('Bill_Num',$new12M)->get()->sum('Debitor');

        $newY=[];
        for($i=0 ; $i < count($GetY) ; $i++){

            array_push($newY,$GetY[$i]['Code']);

        }
        $TotreciptY=PaymentVoucherDetails::whereRelation('PV_ID','Status',1)->whereRelation('PV_ID','Delete',null)->whereRelation('PV_ID','Edit',null)->whereIn('Bill_Num',$newY)->get()->sum('Debitor');




        $data=[

            'name'=>$Name
            ,'count_bill_7'=>$Count7
            ,'total_bill_7'=>$Total7 - ($Totrecipt7 + $TotalPay7)
            ,'count_bill_14'=>$Count14
            ,'total_bill_14'=>$Total14 - ($Totrecipt14 + $TotalPay14)
            ,'count_bill_21'=>$Count21
            ,'total_bill_21'=>$Total21 - ($Totrecipt21 + $TotalPay21)
            ,'count_bill_31'=>$Count31
            ,'total_bill_31'=>$Total31 - ($Totrecipt31 + $TotalPay31)
            ,'count_bill_3M'=>$Count3M
            ,'total_bill_3M'=>$Total3M - ($Totrecipt3M + $TotalPay3M)
            ,'count_bill_6M'=>$Count6M
            ,'total_bill_6M'=>$Total6M - ($Totrecipt6M + $TotalPay6M)
            ,'count_bill_9M'=>$Count9M
            ,'total_bill_9M'=>$Total9M - ($Totrecipt9M + $TotalPay9M)
            ,'count_bill_12M'=>$Count12M
            ,'total_bill_12M'=>$Total12M - ($Totrecipt12M + $TotalPay12M)
            ,'count_bill_Y'=>$CountY
            ,'total_bill_Y'=>$TotalY - ($TotreciptY + $TotalPayY)


        ];




        return $data ;
    }

}
