<?php

namespace App\Http\Controllers\Client;

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


class CashController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:اعطاء دفعة إلي مورد', ['only' => ['add_cash_suppliers', 'store_cash_suppliers', 'edit_cash_suppliers', 'update_cash_suppliers']]);
        $this->middleware('permission:حذف دفعة إلي مورد', ['only' => ['destroy_cash_suppliers']]);
       
        $this->middleware('permission:استلام دفعة من عميل', ['only' => ['add_cash_clients', 'store_cash_clients', 'edit_cash_clients', 'update_cash_clients', 'destroy_cash_clients']]);
        $this->middleware('permission:حذف دفعة من عميل', ['only' => ['destroy_cash_clients']]);

        $this->middleware('permission:إعطاء سلفة إلي عميل', ['only' => ['give_cash_clients', 'store2_cash_clients', 'edit_borrow_clients', 'update_cash_borrow_clients', 'destroy_borrow_clients']]);
        $this->middleware('permission:حذف سلفة إلي عميل', ['only' => ['destroy_borrow_clients']]);

        $this->middleware('permission:أخد سلفة من مورد', ['only' => ['take_cash_borrow_form_supplier', 'store_cash_borrow_form_supplier', 'edit_cash_borrow_form_supplier', 'update_cash_borrow_form_supplier']]);
        $this->middleware('permission:حذف سلفة من مورد', ['only' => ['destroy_cash_borrow_form_supplier']]);
        
    }

    public function add_cash_clients($id)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $outer_clients = OuterClient::where('company_id',$company_id)->FindOrFail($id);
        $safes = $company->safes;
        $banks = $company->banks;
        $cashs = Cash::where('company_id', $company_id)->where('amount', '>', 0)->where('outer_client_id',$id)->get();
        return view('client.finances.cash.clients', compact('cashs','outer_clients', 'banks', 'safes', 'company_id', 'company'));
    }

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


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

    public function edit_cash_clients($outer_client,$opreation)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $cash = Cash::where('outer_client_id',$outer_client)->where('bill_id',null)->where('amount','>',0)->FindOrFail($opreation);
        $outer_clients = OuterClient::where('company_id', $company_id)->findOrFail($outer_client);
        $safes = Safe::where('company_id', $company_id)->get();

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

    public function edit_borrow_clients($outer_client,$opreation)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        
        $cash = Cash::where('outer_client_id',$outer_client)->where('bill_id',null)->where('amount','<',0)->FindOrFail($opreation);

        $outer_clients = OuterClient::where('company_id', $company_id)->findOrFail($outer_client);
        $safes = Safe::where('company_id', $company_id)->get();

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


    public function store_cash_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',
            'safe_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;
        $safe_id = $data['safe_id'];
        $safe = Safe::FindOrFail($safe_id);
        $old_balance = $safe->balance;
        $new_balance = $old_balance + $amount;
        $cash = Cash::create($data);
        $outer_client->update([
            'balance' => $balance_after,
        ]);
        $safe->update([
            'balance' => $new_balance,
        ]);
        return response()->json(['status' => 'success', 'message' => 'تم حفظ العملية بنجاح ']);
    }

    public function store2_cash_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',
            'safe_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;
        $safe_id = $data['safe_id'];
        $safe = Safe::FindOrFail($safe_id);
        $old_balance = $safe->balance;
        $new_balance = $old_balance - $amount;
        $data['amount'] = -$amount;
        $cash = Cash::create($data);
        $outer_client->update([
            'balance' => $balance_after,
        ]);
        $safe->update([
            'balance' => $new_balance,
        ]);
        return response()->json(['status' => true, 'message' => 'تم تسجيل العملية بنجاح ']);
        }

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

        // Return the old values before the operation
        $old_cash = Cash::FindOrFail($request->id);
        $old_safe_id = $old_cash->safe_id;
        $old_safe = Safe::FindOrFail($old_safe_id);
        $old_amount = $old_cash->amount;
        $old_safe_balance = $old_safe->balance;
        $new_safe_balance = $old_safe_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_safe->update([
            'balance' => $new_safe_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;
        $safe_id = $data['safe_id'];
        $safe = Safe::FindOrFail($safe_id);
        $old_balance = $safe->balance;
        $new_balance = $old_balance + $amount;
        $outer_client->update([
            'balance' => $balance_after,
        ]);
        $safe->update([
            'balance' => $new_balance,
        ]);
        $cash = Cash::FindOrFail($request->id);
        $cash->update($data);

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

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

        // Return the old values before the operation
        $old_cash = Cash::FindOrFail($request->id);
        $old_safe_id = $old_cash->safe_id;
        $old_safe = Safe::FindOrFail($old_safe_id);
        $old_amount = abs($old_cash->amount);
        $old_safe_balance = $old_safe->balance;
        $new_safe_balance = $old_safe_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_safe->update([
            'balance' => $new_safe_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;
        $safe_id = $data['safe_id'];
        $safe = Safe::FindOrFail($safe_id);
        $old_balance = $safe->balance;
        $new_balance = $old_balance - $amount;
        $outer_client->update([
            'balance' => $balance_after,
        ]);
        $data['amount'] = -$amount;
        $safe->update([
            'balance' => $new_balance,
        ]);
        $cash = Cash::FindOrFail($request->id);
        $cash->update($data);

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

    public function destroy_cash_clients(Request $request)
    {
        $data = $request->all();
        $id = $request->id;
        // Return the old values before the operation
        $old_cash = Cash::FindOrFail($id);
        if(!$old_cash->bill_id){
        $old_safe_id = $old_cash->safe_id;
        $old_safe = Safe::FindOrFail($old_safe_id);
        $old_amount = $old_cash->amount;
        $old_safe_balance = $old_safe->balance;
        $new_safe_balance = $old_safe_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_safe->update([
            'balance' => $new_safe_balance,
        ]);
        $old_outer_client->update([
            'balance' => $new_outer_client_balance,
        ]);
        $cash = Cash::FindOrFail($id);
        $cash->delete();

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

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

        }

    }

    public function destroy_borrow_clients(Request $request)
    {
        $data = $request->all();
        $id = $request->id;
        // Return the old values before the operation
        $old_cash = Cash::FindOrFail($id);
        $old_safe_id = $old_cash->safe_id;
        $old_safe = Safe::FindOrFail($old_safe_id);
        $old_amount = $old_cash->amount;
        $old_safe_balance = $old_safe->balance;
        $new_safe_balance = $old_safe_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_safe->update([
            'balance' => $new_safe_balance,
        ]);
        $old_outer_client->update([
            'balance' => $new_outer_client_balance,
        ]);
        $cash = Cash::FindOrFail($id);
        $cash->delete();

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

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

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

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

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

    public function edit_cash_suppliers($supplier,$opreation)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $buy_cash = BuyCash::where('supplier_id',$supplier)->where('bill_id',null)->where('amount','>',0)->FindOrFail($opreation);

        $suppliers = Supplier::where('company_id', $company_id)->findOrFail($supplier);
        $safes = Safe::where('company_id', $company_id)->get();

        return view('client.finances.payments.edit_suppliers', compact('suppliers',
            'safes', 'company_id', 'buy_cash', 'company'));
    }
    public function edit_borrow_suppliers($supplier,$opreation)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $buy_cash = BuyCash::where('supplier_id',$supplier)->where('bill_id',null)->where('amount','<',0)->FindOrFail($opreation);

        $suppliers = Supplier::where('company_id', $company_id)->findOrFail($supplier);
        $safes = Safe::where('company_id', $company_id)->get();

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





    public function store_cash_suppliers(Request $request)
    {
        $this->validate($request, [
            'cash_number' => 'required',
            'supplier_id' => 'required',
            'amount' => 'required',
            'date' => 'required',
            'time' => 'required',
            'safe_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 = BuyCash::create($data);
        $supplier->update([
            'balance' => $balance_after,
        ]);
        $safe_id = $data['safe_id'];
        $safe = Safe::FindOrFail($safe_id);
        $old_balance = $safe->balance;
        $new_balance = $old_balance - $amount;
        $safe->update([
            'balance' => $new_balance,
        ]);
        
        return response()->json([
            'status' => true,
            'message' => 'تم اضافة العملية بنجاح',
        ]);
    }

    public function store_cash_borrow_form_supplier(Request $request)
    {
        $this->validate($request, [
            'cash_number' => 'required',
            'supplier_id' => 'required',
            'amount' => 'required',
            'date' => 'required',
            'time' => 'required',
            'safe_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 = BuyCash::create($data);
        $supplier->update([
            'balance' => $balance_after,
        ]);
        $safe_id = $data['safe_id'];
        $safe = Safe::FindOrFail($safe_id);
        $old_balance = $safe->balance;
        $new_balance = $old_balance + $amount;
        $safe->update([
            'balance' => $new_balance,
        ]);
        return response()->json([
            'status' => true,
            'message' => 'تم تسجيل العملية بنجاح',
        ]);
    }

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

        // Return the old values before the operation
        $old_cash = BuyCash::FindOrFail($request->id);
        $old_safe_id = $old_cash->safe_id;
        $old_safe = Safe::FindOrFail($old_safe_id);
        $old_amount = $old_cash->amount;
        $old_safe_balance = $old_safe->balance;
        $new_safe_balance = $old_safe_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_safe->update([
            'balance' => $new_safe_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;
        $safe_id = $data['safe_id'];
        $safe = Safe::FindOrFail($safe_id);
        $old_balance = $safe->balance;
        $new_balance = $old_balance - $amount;
        $supplier->update([
            'balance' => $balance_after,
        ]);
        $safe->update([
            'balance' => $new_balance,
        ]);
        $cash = BuyCash::FindOrFail($request->id);
        $cash->update($data);

        return   response()->json([
            'status' => true,
            'message' => 'تم تعديل العملية بنجاح',
        ]);
    }
    public function update_borrow_suppliers(Request $request)
    {
        $this->validate($request, [
            'cash_number' => 'required',
            'supplier_id' => 'required',
            'amount' => 'required',
            'date' => 'required',
            'time' => 'required',
            'safe_id' => 'required',
        ]);
        $data = $request->all();

        // Return the old values before the operation
        $old_cash = BuyCash::FindOrFail($request->id);
        $old_safe_id = $old_cash->safe_id;
        $old_safe = Safe::FindOrFail($old_safe_id);
        $old_amount = abs($old_cash->amount);
        $old_safe_balance = $old_safe->balance;
        $new_safe_balance = $old_safe_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_safe->update([
            'balance' => $new_safe_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;
        $safe_id = $data['safe_id'];
        $safe = Safe::FindOrFail($safe_id);
        $old_balance = $safe->balance;
        $new_balance = $old_balance + $amount;
        $supplier->update([
            'balance' => $balance_after,
        ]);
        $safe->update([
            'balance' => $new_balance,
        ]);
        $data['amount'] = -$amount;
        $cash = BuyCash::FindOrFail($request->id);
        $cash->update($data);

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

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

        // Return the old values before the operation
        $old_cash = BuyCash::FindOrFail($id);
        if(!$old_cash->bill_id){
        $old_safe_id = $old_cash->safe_id;
        $old_safe = Safe::FindOrFail($old_safe_id);
        $old_amount = $old_cash->amount;
        $old_safe_balance = $old_safe->balance;
        $new_safe_balance = $old_safe_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_safe->update([
            'balance' => $new_safe_balance,
        ]);
        $old_supplier->update([
            'balance' => $new_supplier_balance,
        ]);
        $cash = BuyCash::FindOrFail($id);
        $cash->delete();
        return response()->json(['message' => 'تم حذف العملية بنجاح','status' => true]);
        
        }else{
            return response()->json([
                'status' => false,
                'message' => 'فشل في حذف عملية الدفع',
                'sol'=>"يمكنك فك الدفع من خلال الفاتورة فقط"
            ]);

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

        // Return the old values before the operation
        $old_cash = BuyCash::FindOrFail($id);
        $old_safe_id = $old_cash->safe_id;
        $old_safe = Safe::FindOrFail($old_safe_id);
        $old_amount = $old_cash->amount;
        $old_safe_balance = $old_safe->balance;
        $new_safe_balance = $old_safe_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_safe->update([
            'balance' => $new_safe_balance,
        ]);
        $old_supplier->update([
            'balance' => $new_supplier_balance,
        ]);
        $cash = BuyCash::FindOrFail($id);
        $cash->delete();

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

    }

    public function get_client_debit_by_id(Request $request){
     $outer_client =  OuterClient::findOrFail($request->client_id) ; 
    return response()->json([
        'balance'=> $outer_client->balance
    ]);
    

    }


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

        return  $pre_cash ;
    }
}
