<?php

namespace App\Http\Resources\Reports\Accounting;


use App\Models\Employess;
use App\Models\GeneralDaily;
use Illuminate\Http\Resources\Json\JsonResource;
use DB;
use Auth;
use DateTime;

class ClientVendorStatementApiResources extends JsonResource
{


    public function toArray($request)
    {


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

        $from=$request->get('from');
$to=$request->get('to');
        $DBBEFORE=0;
        $CRBEFORE=0;
        $DBTOTAL=0;
        $CRTOTAL=0;
        $DBBALANCE=0;
        $CRBALANCE=0;
        $y=0;
        $z=0;
        $DebBef =GeneralDaily::where('Account',$this->id)
            ->where('Date','<',$from)
            ->get()->sum('Debitor_Coin');


        $CredBef =GeneralDaily::where('Account',$this->id)
            ->where('Date','<',$from)
            ->get()->sum('Creditor_Coin');

        $TotalDeb =GeneralDaily::where('Account',$this->id)
            ->whereBetween('Date', [$from, $to])
            ->get()->sum('Debitor_Coin');


        $TotalCred =GeneralDaily::where('Account',$this->id)
            ->whereBetween('Date', [$from, $to])
            ->get()->sum('Creditor_Coin');

        $x=( $DebBef + $TotalDeb ) - ( $CredBef + $TotalCred ) ;



        $DBBEFORE +=$DebBef;
        $CRBEFORE +=$CredBef;
        $DBTOTAL +=$TotalDeb;
        $CRTOTAL +=$TotalCred;

        if($x > 0){

            $y=$x;
            $z=0;

        }else{
            $y=0;
            $z=$x;
        }
        $DBBALANCE +=$y;
        $CRBALANCE +=$z;



        $data=[

            'Account_Code'=>$this->Code
            ,'Account_Name'=>$this->Name
            ,'Debiator_Before'=>$DebBef
            ,'Creditor_Before'=>$CredBef
            ,'Total_Debitor'=>$TotalDeb
            ,'Total_Creditor'=>$TotalCred
            ,'Debitor_Balance'=>$y
            ,'Creditor_Balance'=>$z

        ];




        return $data ;
    }

}
