<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Mail\sendingClientSummary;
use App\Mail\sendingSupplierSummary;
use App\Models\BankBuyCash;
use App\Models\BankCash;
use App\Models\BuyBill;
use App\Models\BuyBillReturn;
use App\Models\BuyCash;
use App\Models\Cash;
use App\Models\Company;
use App\Models\Employee;
use App\Models\EmployeeCash;
use App\Models\ExtraSettings;
use App\Models\Gift;
use App\Models\OuterClient;
use App\Models\Quotation;
use App\Models\SaleBill;
use App\Models\SaleBillReturn;
use App\Models\Supplier;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;

class SummaryController extends Controller
{
    public function get_clients_summary()
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $outer_clients = $company->outerClients;
        return view('client.summary.clients', compact('company', 'company_id', 'outer_clients'));
    }

    public function post_clients_summary(Request $request)
    {
        
        $company_id = $request->company_id ;
        $outer_client_id = $request->outer_client_id;
        $from_date = $request->from_date;
        $to_date = $request->to_date;
        $submit = $request->submit;
        $today = date('Y-m-d');
        $outer_client_k = OuterClient::where('company_id',$company_id)->FindOrFail($outer_client_id);
        if (empty($to_date) || empty($from_date) ) {
                // all summary results
                $gifts = $outer_client_k->gifts;
                $quotations = $outer_client_k->quotations;
                $saleBills = SaleBill::where('company_id',$company_id)->active()->where('status','done')->where('outer_client_id', $outer_client_id)->get();
                $cashs = Cash::where('company_id',$company_id)->where('outer_client_id', $outer_client_id)->where('amount', '>', 0)->get();
                $borrows = Cash::where('company_id',$company_id)->where('outer_client_id', $outer_client_id)->where('amount', '<', 0)->get();
                $bankcashs = $outer_client_k->bankcashs;
                $returns = $outer_client_k->saleBillReturns;
            } else {
                // from - to
                $gifts = Gift::where('company_id',$company_id)->where('outer_client_id', $outer_client_id)
                    ->whereBetween('created_at', [$from_date, $to_date])->get();
                $quotations = Quotation::where('company_id',$company_id)->where('outer_client_id', $outer_client_id)
                    ->whereBetween('created_at', [$from_date, $to_date])->get();
                $saleBills = SaleBill::where('company_id',$company_id)->active()->where('status','done')->where('outer_client_id', $outer_client_id)
                    ->whereBetween('created_at', [$from_date, $to_date])->get();
                $cashs = Cash::where('company_id',$company_id)->where('outer_client_id', $outer_client_id)
                    ->where('amount', '>', 0)
                    ->whereBetween('created_at', [$from_date, $to_date])->get();
                $borrows = Cash::where('company_id',$company_id)->where('outer_client_id', $outer_client_id)
                    ->where('amount', '<', 0)
                    ->whereBetween('created_at', [$from_date, $to_date])->get();
                $bankcashs = BankCash::where('company_id',$company_id)->where('outer_client_id', $outer_client_id)
                    ->whereBetween('created_at', [$from_date, $to_date])->get();
                $returns = SaleBillReturn::where('company_id',$company_id)->where('outer_client_id', $outer_client_id)
                    ->whereBetween('created_at', [$from_date, $to_date])->get();
            
            }

            

        return view('client.summary.clients_post',
            compact('returns', 'from_date', 'to_date'
                , 'outer_client_k', 'gifts', 'quotations', 'saleBills', 'cashs', 'bankcashs', 'borrows'));
    }

    public function get_suppliers_summary()
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $suppliers = $company->suppliers;
        return view('client.summary.suppliers', compact('company', 'company_id', 'suppliers'));
    }

    public function post_suppliers_summary(Request $request)
    {
        $company_id = $request->company_id ;
        $supplier_id = $request->supplier_id;
        $from_date = $request->from_date;
        $to_date = $request->to_date;
        $today = date('Y-m-d');
        $supplier_k = Supplier::FindOrFail($supplier_id);
            if (empty($from_date) || empty($to_date)) {
                // all summary results
                $buyBills =  BuyBill::active()->where('company_id',$company_id)->where('status','done')->where('supplier_id', $supplier_id)->get();
                $buyCashs = BuyCash::where('company_id',$company_id)->where('supplier_id', $supplier_id)->where('amount', '>', 0)->get();
                $buyBorrows = BuyCash::where('company_id',$company_id)->where('supplier_id', $supplier_id)->where('amount', '<', 0)->get();
                $bankbuyCashs = $supplier_k->bankbuyCashs;
                $returns = $supplier_k->buyBillReturns;
            } else {
                // from - to
                $buyBills = BuyBill::active()->where('company_id',$company_id)->where('status','done')->where('supplier_id', $supplier_id)
                    ->whereBetween('created_at', [$from_date, $to_date])->get();
                $buyCashs = BuyCash::where('company_id',$company_id)->where('supplier_id', $supplier_id)
                    ->where('amount', '>', 0)
                    ->whereBetween('created_at', [$from_date, $to_date])->get();

                $buyBorrows = BuyCash::where('company_id',$company_id)->where('supplier_id', $supplier_id)
                    ->where('amount', '<', 0)
                    ->whereBetween('created_at', [$from_date, $to_date])->get();

                $bankbuyCashs = BankBuyCash::where('company_id',$company_id)->where('supplier_id', $supplier_id)
                    ->whereBetween('created_at', [$from_date, $to_date])->get();
                $returns = BuyBillReturn::where('company_id',$company_id)->where('supplier_id', $supplier_id)
                    ->whereBetween('created_at', [$from_date, $to_date])->get();
            }
            
       
            return view('client.summary.suppliers_post',
                compact( 'supplier_k','from_date', 'to_date','buyBills','buyCashs', 'buyBorrows','bankbuyCashs', 'returns'));
        
    }
    public function send_client_summary(Request $request)
    {
        $url = $request->url;
        $id = $request->id;
        $outer_client = OuterClient::FindOrFail($id);
        $data = array(
            'body' => 'كشف حساب عميل',
            'url' => $url,
            'subject' => 'مرفق مع هذه الرسالة رابط لكشف الحساب ',
        );
        Mail::to($outer_client->client_email)->send(new sendingClientSummary($data));
        return redirect()->to('/client/clients-summary-get')
            ->with('success', 'تم ارسال كشف الحساب الى بريد العميل بنجاح');

    }
    public function send_supplier_summary(Request $request)
    {
        $url = $request->url;
        $id = $request->id;
        $supplier = Supplier::FindOrFail($id);
        $data = array(
            'body' => 'كشف حساب مورد',
            'url' => $url,
            'subject' => 'مرفق مع هذه الرسالة رابط لكشف الحساب ',
        );
        Mail::to($supplier->supplier_email)->send(new sendingSupplierSummary($data));
        return redirect()->to('/client/suppliers-summary-get')
            ->with('success', 'تم ارسال كشف الحساب الى بريد المورد بنجاح');

    }

    public function client_summary($id)
    {
        $outer_client_k = OuterClient::FindOrFail($id);
        $gifts = $outer_client_k->gifts;
        $saleBills = SaleBill::active()->where('status','done')->where('outer_client_id', $id)->get();
        $quotations = Quotation::active()->where('outer_client_id', $id)->get();
        $cashs = Cash::where('outer_client_id', $id)->where('amount', '>', 0)->get();
        $borrows = Cash::where('outer_client_id', $id)->where('amount', '<', 0)->get();
        $bankcashs = $outer_client_k->bankcashs;
        $returns = $outer_client_k->saleBillReturns;
         

        return view('client.summary.clients_post',
            compact('returns', 'outer_client_k', 'gifts', 'quotations', 'saleBills', 'cashs', 'bankcashs', 'borrows'));
    }

    public function suppliers_summary($id)
    {
        $supplier_k=Supplier::findOrFail($id);
        $buyBills = BuyBill::active()->done()->where('supplier_id', $id)->get();
        $buyCashs = BuyCash::where('supplier_id', $id)->where('amount', '>', 0)->get();
        $buyBorrows = BuyCash::where('supplier_id', $id)->where('amount', '<', 0)->get();
        $bankbuyCashs = $supplier_k->bankbuyCashs;
        $returns = $supplier_k->buyBillReturns;
         

        return view('client.summary.suppliers_post',
        compact( 'supplier_k', 'buyBills', 'buyCashs', 'bankbuyCashs', 'returns', 'buyBorrows'));

    }


    public function get_employees_summary(){

        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $employees = $company->employees;
        return view('client.summary.employees', compact('company', 'company_id', 'employees'));
    }

    public function post_employees_summary(Request $request){
            $from_date = $request->from_date;
            $to_date = $request->to_date;

    $employee = Employee::findOrfail($request->employee);
    if(empty($from_date) || empty($to_date)){
    $cashs = EmployeeCash::with(['client','safe','bank'])->where('employee_id',$request->employee)->get();
    $deserve = $employee->balance;
    if($deserve > 0){ $deserve = $deserve; $type =" لة " ;}elseif($deserve < 0){$deserve = abs($deserve); $type =" علية "; }else{$deserve = 0; $type ="";}        
      
    }else{
    $deserve = $this->getDaysCount($from_date, $to_date) * ($employee->salary /30) ;
    
    $cashs = EmployeeCash::with(['client','safe','bank'])->where('employee_id',$request->employee)->whereBetween('date', [$from_date, $to_date])->get();
    $type = "";   
    }
    return response()->json(['result'=> $cashs,'employee'=>$employee,'deserve'=>round($deserve,2),'type'=>$type]);
        
    }

    public function getDaysCount($from, $to)
    {
        $from = \Carbon\Carbon::parse($from);
        $to = \Carbon\Carbon::parse($to);
        $diff_in_days = $to->diffInDays($from);
        $work_days = $diff_in_days + 1;
        return $work_days;
    }




}
