<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\BankBuyCash;
use App\Models\BankCash;
use App\Models\BuyBill;
use App\Models\BuyBillReturn;
use App\Models\BuyCash;
use App\Models\Capital;
use App\Models\Cash;
use App\Models\Company;
use App\Models\EmployeeCash;
use App\Models\Expense;
use App\Models\ExtraSettings;
use App\Models\Gift;
use App\Models\Quotation;
use App\Models\SaleBill;
use App\Models\SaleBillReturn;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class AccountingController extends Controller
{
    public function journal_get()
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);

        return view('client.daily.daily', compact('company'));
    }

    public function journal_post(Request $request)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $date = $request->date ; 

        $jornal = array(); 

        $cashs = Cash::where('company_id', $company_id)->where('date',$date)->get();
        $buysCash = BuyCash::where('company_id', $company_id)->where('date',$date)->get();
        $bankcashs = BankCash::where('company_id', $company_id)->where('date',$date)->get();
        $bankBuyCashs = BankBuyCash::where('company_id', $company_id)->where('date',$date)->get();
        $expenses = Expense::where('company_id', $company_id)->where('created_at',$date)->get();
        $capitals = Capital::where('company_id', $company_id)->where('created_at',$date)->get();
        $saleBills = SaleBill::where('company_id', $company_id)->where('rest','!=',0)->where('date',$date)->get();
        $buyBills = BuyBill::where('company_id', $company_id)->where('rest','!=',0)->where('date',$date)->get();
        $employeeCashs = EmployeeCash::where('company_id', $company_id)->where('created_at',$date)->get();



        foreach($buyBills as $buyBill){
           
            if($buyBill->rest>0){
            $explanation = "تسجيل مديونية في حساب المورد ".$buyBill->supplier->supplier_name;
            }else{
            $explanation = "تسجيل رصيد في حساب المورد ".$buyBill->supplier->supplier_name;
            }
            $jornal[] = [
                'date'=>$buyBill->date,
                'explanation'=>$explanation,
                'debit'=>$buyBill->rest,
                'credit'=>$buyBill->rest,

            ] ;
        }

        foreach($saleBills as $saleBill){
           
            if($saleBill->rest>0){
            $explanation = "تسجيل مديونية في حساب العميل ".$saleBill->outerClient->client_name;
            }else{
            $explanation = "تسجيل رصيد في حساب العميل ".$saleBill->outerClient->client_name;
            }
            $jornal[] = [
                'date'=>$saleBill->date,
                'explanation'=>$explanation,
                'debit'=>$saleBill->rest,
                'credit'=>$saleBill->rest,

            ] ;
        }

        foreach($buysCash as $buyCash){
            if($buyCash->amount >0){
                $explanation = " من ".$buyCash->safe->safe_name." إلي المورد " . $buyCash->supplier->supplier_name;
            }else{
                $explanation = " من المورد ".$buyCash->supplier->supplier_name." إلي ". $buyCash->safe->safe_name;
            }
            $jornal[] = [
                'date'=>$buyCash->date,
                'explanation'=>$explanation,
                'debit'=>$buyCash->amount,
                'credit'=>$buyCash->amount,

            ] ;
        }
        foreach($cashs as $cash){
            if(empty($cash->outerClient)){
                $client_name = "النقدي" ;

            }else{
                $client_name = $cash->outerClient->client_name ;
            }
            if($cash->amount > 0){
                $explanation = " من العميل ".$client_name." إلي ". $cash->safe->safe_name;

            }else{
                $explanation = " من ".$cash->safe->safe_name." إلي العميل " . $client_name;

            }
            $jornal[] = [
                'date'=>$cash->date,
                'explanation'=>$explanation,
                'debit'=>$cash->amount,
                'credit'=>$cash->amount,

            ] ;
        }

        foreach($bankcashs as $bankcash){
            if(empty($bankcash->outer_client_id	)){
                $client_name = "النقدي" ;

            }else{
                $client_name = $bankcash->outerClient->client_name ;
            }
            if($bankcash->amount > 0){
                $explanation = " من العميل ".$client_name." إلي ". $bankcash->bank->bank_name;

            }else{
                $explanation = " من ".$bankcash->bank->bank_name." إلي العميل " . $client_name;

            }
            $jornal[] = [
                'date'=>$bankcash->date,
                'explanation'=>$explanation,
                'debit'=>$bankcash->amount,
                'credit'=>$bankcash->amount,

            ] ;
        }
        foreach($bankBuyCashs as $bankBuyCash){
            if($bankBuyCash->amount >0){
                $explanation = " من ".$bankcash->bank->bank_name." إلي المورد " . $bankBuyCash->supplier->supplier_name;
            }else{
                $explanation = " من المورد ".$bankBuyCash->supplier->supplier_name." إلي ". $bankcash->bank->bank_name;
            }
            $jornal[] = [
                'date'=>$bankBuyCash->date,
                'explanation'=>$explanation,
                'debit'=>$bankBuyCash->amount,
                'credit'=>$bankBuyCash->amount,

            ] ;
        }
        foreach($expenses as $expense){
            
                $explanation = " من ".$expense->safe->safe_name." إلي " . $expense->expense_details;

            $jornal[] = [
                'date'=>$expense->created_at,
                'explanation'=>$explanation,
                'debit'=>$expense->amount,
                'credit'=>$expense->amount,

            ] ;
        }
        foreach($capitals as $capital){

            $explanation = " رأس مال إلي ". $capital->safe->safe_name;

            $jornal[] = [
                'date'=>$capital->created_at,
                'explanation'=>$explanation,
                'debit'=>$capital->amount,
                'credit'=>$capital->amount,

            ] ;
        }
        foreach($employeeCashs as $employeeCash){

            $explanation = " دفعة إلي الموظف ". $employeeCash->employee->name . ' من '.$employeeCash->safe->safe_name ;

            $jornal[] = [
                'date'=>$employeeCash->created_at,
                'explanation'=>$explanation,
                'debit'=>$employeeCash->amount,
                'credit'=>$employeeCash->amount,

            ] ;
        }

        return response()->json(['jornal'=>$jornal,'staust'=>true]);

    }

}
