<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\Bank;
use App\Models\BankBuyCash;
use App\Models\BankCash;
use App\Models\BuyCash;
use App\Models\Company;
use App\Models\OuterClient;
use App\Models\PreSettings;
use App\Models\Supplier;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class CashBankController extends Controller
{
    public function add_cashbank_clients($id)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $old_cash = BankCash::max('cash_number');
        $pre_cash = ++$old_cash;
        $outer_clients = OuterClient::where('company_id', $company_id)->findOrFail($id);
        $banks = $company->banks;
        $cashs = BankCash::where('company_id', $company_id)->where('outer_client_id', $id)->get();

        return view('client.finances.cashbank.clients', compact('cashs','outer_clients', 'banks', 'company_id', 'pre_cash', 'company'));
    }

    public function edit_cashbank_clients($id)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $cash = BankCash::FindOrFail($id);

        $outer_clients = OuterClient::where('company_id', $company_id)->get();
        $banks = $company->banks;

        return view('client.finances.paymentsbank.edit_clients', compact('outer_clients',
            'banks', 'company_id', 'cash', 'company'));
    }



    public function store_cashbank_clients(Request $request)
    {
        $this->validate($request, [
            'cash_number' => 'required',
            'outer_client_id' => 'required',
            'amount' => 'required',
            'date' => 'required',
            'time' => 'required',
            'bank_id' => 'required',
        ]);
        $data = $request->all();
        $company_id = $data['company_id'];
        $data['client_id'] = Auth::user()->id;
        $outer_client_id = $data['outer_client_id'];
        $amount = $data['amount'];
        $outer_client = OuterClient::FindOrFail($outer_client_id);
        $balance_before = $outer_client->balance;
        $balance_after = $balance_before - $amount;
        $data['balance_before'] = $balance_before;
        $data['balance_after'] = $balance_after;
        $bank_id = $data['bank_id'];
        $bank = Bank::FindOrFail($bank_id);
        $old_balance = $bank->bank_balance;
        $new_balance = $old_balance + $amount;
        $cash = BankCash::create($data);
        $outer_client->update([
            'balance' => $balance_after,
        ]);
        $bank->update([
            'bank_balance' => $new_balance,
        ]);
       
        return response()->json(['message' => 'تم تسجيل العملية بنجاح','status'=>true]);
    }

    public function update_cashbank_clients(Request $request, $id)
    {
        $this->validate($request, [
            'cash_number' => 'required',
            'outer_client_id' => 'required',
            'amount' => 'required',
            'date' => 'required',
            'time' => 'required',
            'bank_id' => 'required',
        ]);
        $data = $request->all();

        // Return the old values before the operation
        $old_cash = BankCash::FindOrFail($id);
        $old_bank_id = $old_cash->bank_id;
        $old_bank = Bank::FindOrFail($old_bank_id);
        $old_amount = $old_cash->amount;
        $old_bank_balance = $old_bank->bank_balance;
        $new_bank_balance = $old_bank_balance - $old_amount;
        $old_outer_client_id = $old_cash->outer_client_id;
        $old_outer_client = OuterClient::FindOrFail($old_outer_client_id);
        $old_outer_client_balance = $old_outer_client->balance;
        $new_outer_client_balance = $old_outer_client_balance + $old_amount;
        $old_bank->update([
            'bank_balance' => $new_bank_balance,
        ]);
        $old_outer_client->update([
            'balance' => $new_outer_client_balance,
        ]);
        // Assign the new values of the operation
        $company_id = $data['company_id'];
        $data['client_id'] = Auth::user()->id;
        $outer_client_id = $data['outer_client_id'];
        $amount = $data['amount'];
        $outer_client = OuterClient::FindOrFail($outer_client_id);
        $balance_before = $outer_client->balance;
        $balance_after = $balance_before - $amount;
        $data['balance_before'] = $balance_before;
        $data['balance_after'] = $balance_after;
        $bank_id = $data['bank_id'];
        $bank = Bank::FindOrFail($bank_id);
        $old_balance = $bank->bank_balance;
        $new_balance = $old_balance + $amount;
        $outer_client->update([
            'balance' => $balance_after,
        ]);
        $bank->update([
            'bank_balance' => $new_balance,
        ]);
        $cash = BankCash::FindOrFail($id);
        $cash->update($data);

        return redirect()->route('client.cashbank.clients')
            ->with('success', 'تم تعديل البيانات بنجاح');
    }

    public function destroy_cashbank_clients(Request $request)
    {
        $data = $request->all();
        $id = $request->id;
        // Return the old values before the operation
        $old_cash = BankCash::FindOrFail($id);
        if(!$old_cash->bill_id){
        $old_bank_id = $old_cash->bank_id;
        $old_bank = Bank::FindOrFail($old_bank_id);
        $old_amount = $old_cash->amount;
        $old_bank_balance = $old_bank->bank_balance;
        $new_bank_balance = $old_bank_balance - $old_amount;
        $old_outer_client_id = $old_cash->outer_client_id;
        $old_outer_client = OuterClient::FindOrFail($old_outer_client_id);
        $old_outer_client_balance = $old_outer_client->balance;
        $new_outer_client_balance = $old_outer_client_balance + $old_amount;
        $old_bank->update([
            'bank_balance' => $new_bank_balance,
        ]);
        $old_outer_client->update([
            'balance' => $new_outer_client_balance,
        ]);
        $cash = BankCash::FindOrFail($id);
        $cash->delete();

        return response()->json(['message' => 'تم حذف البيانات بنجاح','status' => true]);


        }else{
            return response()->json([
                'status' => false,
                'message' => 'فشل في حذف عملية الدفع',
                'sol'=>"يمكنك فك الدفع من خلال الفاتورة فقط"
            ]);

        }
    }

    public function add_cashbank_suppliers($id)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);

        $old_cash = BankBuyCash::max('cash_number');
        $pre_buy_cash = ++$old_cash;

        $suppliers = Supplier::where('company_id', $company_id)->findOrFail($id);
        $banks = $company->banks;
        $buy_cashs = BankBuyCash::where('company_id', $company_id)->get();

        return view('client.finances.cashbank.suppliers', compact('buy_cashs','suppliers', 'banks', 'company_id', 'pre_buy_cash', 'company'));
    }

    public function edit_cashbank_suppliers($id)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $buy_cash = BankBuyCash::FindOrFail($id);

        $suppliers = Supplier::where('company_id', $company_id)->get();
        $banks = Bank::where('company_id', $company_id)->get();

        return view('client.finances.paymentsbank.edit_suppliers', compact('suppliers',
            'banks', 'company_id', 'buy_cash', 'company'));
    }



    public function store_cashbank_suppliers(Request $request)
    {
        $this->validate($request, [
            'cash_number' => 'required',
            'supplier_id' => 'required',
            'amount' => 'required',
            'date' => 'required',
            'time' => 'required',
            'bank_id' => 'required',
        ]);
        $data = $request->all();
        $company_id = $data['company_id'];
        $data['client_id'] = Auth::user()->id;
        $supplier_id = $data['supplier_id'];
        $amount = $data['amount'];
        $supplier = Supplier::FindOrFail($supplier_id);
        $balance_before = $supplier->balance;
        $balance_after = $balance_before - $amount;
        $data['balance_before'] = $balance_before;
        $data['balance_after'] = $balance_after;
        $cash = BankBuyCash::create($data);
        $supplier->update([
            'balance' => $balance_after,
        ]);
        $bank_id = $data['bank_id'];
        $bank = Bank::FindOrFail($bank_id);
        $old_balance = $bank->bank_balance;
        $new_balance = $old_balance - $amount;
        $bank->update([
            'bank_balance' => $new_balance,
        ]);
        return response()->json(['message' => 'تم اضافة العملية بنجاح','status'=>true]);
    }

    public function update_cashbank_suppliers(Request $request, $id)
    {
        $this->validate($request, [
            'cash_number' => 'required',
            'supplier_id' => 'required',
            'amount' => 'required',
            'date' => 'required',
            'time' => 'required',
            'bank_id' => 'required',
        ]);
        $data = $request->all();

        // Return the old values before the operation
        $old_cash = BankBuyCash::FindOrFail($id);
        $old_bank_id = $old_cash->bank_id;
        $old_bank = Bank::FindOrFail($old_bank_id);
        $old_amount = $old_cash->amount;
        $old_bank_balance = $old_bank->bank_balance;
        $new_bank_balance = $old_bank_balance + $old_amount;
        $old_supplier_id = $old_cash->supplier_id;
        $old_supplier = Supplier::FindOrFail($old_supplier_id);
        $old_supplier_balance = $old_supplier->balance;
        $new_supplier_balance = $old_supplier_balance + $old_amount;
        $old_bank->update([
            'bank_balance' => $new_bank_balance,
        ]);
        $old_supplier->update([
            'balance' => $new_supplier_balance,
        ]);
        // Assign the new values of the operation
        $company_id = $data['company_id'];
        $data['client_id'] = Auth::user()->id;
        $supplier_id = $data['supplier_id'];
        $amount = $data['amount'];
        $supplier = Supplier::FindOrFail($supplier_id);
        $balance_before = $supplier->balance;
        $balance_after = $balance_before - $amount;
        $data['balance_before'] = $balance_before;
        $data['balance_after'] = $balance_after;
        $bank_id = $data['bank_id'];
        $bank = Bank::FindOrFail($bank_id);
        $old_balance = $bank->bank_balance;
        $new_balance = $old_balance - $amount;
        $supplier->update([
            'balance' => $balance_after,
        ]);
        $bank->update([
            'bank_balance' => $new_balance,
        ]);
        $cash = BankBuyCash::FindOrFail($id);
        $cash->update($data);

        return redirect()->route('client.cashbank.suppliers')
            ->with('success', 'تم تعديل البيانات بنجاح');
    }

    public function destroy_cashbank_suppliers(Request $request)
    {
        $data = $request->all();
        $id = $request->id;

        // Return the old values before the operation
        $old_cash = BankBuyCash::FindOrFail($id);
        if(!$old_cash->bill_id){
        $old_bank_id = $old_cash->bank_id;
        $old_bank = Bank::FindOrFail($old_bank_id);
        $old_amount = $old_cash->amount;
        $old_bank_balance = $old_bank->bank_balance;
        $new_bank_balance = $old_bank_balance + $old_amount;
        $old_supplier_id = $old_cash->supplier_id;
        $old_supplier = Supplier::FindOrFail($old_supplier_id);
        $old_supplier_balance = $old_supplier->balance;
        $new_supplier_balance = $old_supplier_balance + $old_amount;
        $old_bank->update([
            'bank_balance' => $new_bank_balance,
        ]);
        $old_supplier->update([
            'balance' => $new_supplier_balance,
        ]);
        $old_cash->delete();

        return response()->json(['message' => 'تم حذف البيانات بنجاح','status' => true]);


        }else{
            return response()->json([
                'status' => false,
                'message' => 'فشل في حذف عملية الدفع',
                'sol'=>"يمكنك فك الدفع من خلال الفاتورة فقط"
            ]);

        }
        
    }

    public function give_advance_bank_clients($id){
        
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $outer_clients = OuterClient::where('company_id', $company_id)->findOrFail($id);
        $banks = $company->banks;
        $cashs = BankCash::where('company_id', $company_id)->where('outer_client_id',$id)->where('amount', '<', 0)->get();


        return view('client.finances.borrowbank.clients', compact('cashs','outer_clients', 'banks','company_id', 'company'));
  
    }

    public function store2_bank_clients(Request $request){

        if(empty($request->cash_number)){
            $request['cash_number'] = $this->getCashNumber($request->company_id);
        }

        $this->validate($request, [
            'cash_number' => 'required',
            'outer_client_id' => 'required',
            'amount' => 'required',
            'date' => 'required',
            'time' => 'required',
            'bank_id' => 'required',

        ]);
        $data = $request->all();
        $company_id = $data['company_id'];
        $data['client_id'] = Auth::user()->id;
        $outer_client_id = $data['outer_client_id'];
        $amount = $data['amount'];
        $outer_client = OuterClient::FindOrFail($outer_client_id);
        $balance_before = $outer_client->balance;
        $balance_after = $balance_before + $amount;
        $data['balance_before'] = $balance_before;
        $data['balance_after'] = $balance_after;
        $bank_id = $data['bank_id'];
        $bank = Bank::FindOrFail($bank_id);
        $old_balance = $bank->bank_balance;
        $new_balance = $old_balance - $amount;
        $data['amount'] = -$amount;
        $cash = BankCash::create($data);
        $outer_client->update([
            'balance' => $balance_after,
        ]);
        $bank->update([
            'bank_balance' => $new_balance,
        ]);
        return response()->json(['status' => true, 'message' => 'تم تسجيل العملية بنجاح ']);
    }

    public function getCashNumber($company_id){
        $check = BankCash::all();
        if ($check->isEmpty()) {
            $pre_settings = PreSettings::where('company_id', $company_id)->first();
            $pre_cash = $pre_settings->pre_cash;
        } else {
            $old_cash = BankCash::where('company_id', $company_id)->max('cash_number');
            $pre_cash = ++$old_cash;
        }

        return  $pre_cash ;
    }

    public function destroy_bank_borrow_clients(Request $request){

        $data = $request->all();
        $id = $request->id;
        // Return the old values before the operation
        $old_cash = BankCash::FindOrFail($id);
        $old_bank_id = $old_cash->bank_id;
        $bank = Bank::FindOrFail($old_bank_id);
        $old_amount = $old_cash->amount;
        $old_bank_balance = $bank->bank_balance;
        $new_bank_balance = $old_bank_balance - $old_amount;
        $old_outer_client_id = $old_cash->outer_client_id;
        $old_outer_client = OuterClient::FindOrFail($old_outer_client_id);
        $old_outer_client_balance = $old_outer_client->balance;
        $new_outer_client_balance = $old_outer_client_balance + $old_amount;
        $bank->update([
            'bank_balance' => $new_bank_balance,
        ]);
        $old_outer_client->update([
            'balance' => $new_outer_client_balance,
        ]);
        $cash = BankCash::FindOrFail($id);
        $cash->delete();

        return response()->json([
            'status' => true,
            'message' => 'تم حذف العملية بنجاح',
        ]);
    }

    public function take_bank_borrow_form_supplier($id)
    {
        
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $check = BankBuyCash::all();
        if ($check->isEmpty()) {
            $pre_settings = PreSettings::where('company_id', $company_id)->first();
            $pre_buy_cash = $pre_settings->pre_buy_cash;
        } else {
            $old_cash = BankBuyCash::where('company_id', $company_id)->max('cash_number');
            $pre_buy_cash = ++$old_cash;
        }
        $suppliers = Supplier::where('company_id', $company_id)->findOrFail($id);
        $banks = Bank::where('company_id', $company_id)->get();
        $buy_cashs = BankBuyCash::where('supplier_id',$id)->where('company_id', $company_id)->where('amount','<',0)->get();

        return view('client.finances.borrowbank.suppliers', compact('buy_cashs','suppliers', 'banks', 'company_id', 'pre_buy_cash', 'company'));
    }

    public function store_bank_borrow_form_supplier(Request $request)
    {
        $this->validate($request, [
            'cash_number' => 'required',
            'supplier_id' => 'required',
            'amount' => 'required',
            'date' => 'required',
            'time' => 'required',
            'bank_id' => 'required',
        ]);
        $data = $request->all();
        $company_id = $data['company_id'];
        $data['client_id'] = Auth::user()->id;
        $supplier_id = $data['supplier_id'];
        $amount = $data['amount'];
        $supplier = Supplier::FindOrFail($supplier_id);
        $balance_before = $supplier->balance;
        $balance_after = $balance_before + $amount;
        $data['balance_before'] = $balance_before;
        $data['balance_after'] = $balance_after;
        $data['amount'] = -$amount;
        $cash = BankBuyCash::create($data);
        $supplier->update([
            'balance' => $balance_after,
        ]);
        $bank_id = $data['bank_id'];
        $bank = Bank::FindOrFail($bank_id);
        $old_balance = $bank->balance;
        $new_balance = $old_balance + $amount;
        $bank->update([
            'bank_balance' => $new_balance,
        ]);
        return response()->json([
            'status' => true,
            'message' => 'تم تسجيل العملية بنجاح',
        ]);
    }

    public function destroy_bank_borrow_form_supplier(Request $request)
    {
        $data = $request->all();
        $id = $request->id;

        // Return the old values before the operation
        $old_cash = BankBuyCash::FindOrFail($id);
        $old_bank_id = $old_cash->bank_id;
        $old_bank = Bank::FindOrFail($old_bank_id);
        $old_amount = $old_cash->amount;
        $old_bank_balance = $old_bank->bank_balance;
        $new_bank_balance = $old_bank_balance + $old_amount;
        $old_supplier_id = $old_cash->supplier_id;
        $old_supplier = Supplier::FindOrFail($old_supplier_id);
        $old_supplier_balance = $old_supplier->balance;
        $new_supplier_balance = $old_supplier_balance + $old_amount;
        $old_bank->update([
            'bank_balance' => $new_bank_balance,
        ]);
        $old_supplier->update([
            'balance' => $new_supplier_balance,
        ]);
        $borrow = BankBuyCash::FindOrFail($id);
        $borrow->delete();

        return response()->json(['message' => 'تم حذف البيانات بنجاح','status' => true]);

    }

}
