<?php

namespace App\Http\Controllers\Finance;

use App\Http\Controllers\Controller;
use App\Http\Controllers\RandomNumbersController;
use App\Models\AccBillsManager;
use App\Models\AccVendor;
use App\Models\BillMasterVendor;
use App\Models\CompanyChartsOfAccount;
use App\Models\CompanyFinancialMainAccount;
use App\Models\Requisition;
use Illuminate\Http\Request;

class AccVendorsController extends Controller
{
    public function index(){
        $lists =  AccVendor::orderBy('name', 'asc')->get();

        return view('finance.vendors.acc-vendors', compact('lists'));
    }

    /**Display Pop Up window to store a New Vendor**/
    public function showNewVendor(){
        /**Get a List of all the Expense Main Accounts**/
        $expenseMainAccount = CompanyFinancialMainAccount::where('category', 'expense')->get();
        $purchasesMainAccount = CompanyFinancialMainAccount::where('category', 'purchases')->get();

        return view('finance.vendors.add-company-vendor', compact('expenseMainAccount', 'purchasesMainAccount'));
    }

    public function storeNewVendor(Request $request){
        $branch = $request['branch'];
        $name = $request['name'];
        $accNo = $request['acc-no'];
        $contactPerson = $request['contact-person'];
        $phoneNumber = $request['phone'];
        $emailAddress = $request['email-address'];
        $kra = $request['kra'];
        $expenseLedger = $request['ledger-id'];

        $res = 'empty name';
        if(!empty($name)){
            $res = 'empty contact person';
            if(!empty($contactPerson)){
                $res = 'empty phone';
                if(!empty($phoneNumber)){
                    $res = 'empty ledger';
                    if(!empty($expenseLedger)){
                        /**At this stage we can now proceed to create a new vendor**/
                        $exists = AccVendor::where('name', $name)->first();
                        $res = 'vendor exist';
                        if(!$exists){
                            /**Generate a Stamp**/
                            $stamp = (new RandomNumbersController)->getRandomMix(10);
                            $res = 'stamp exist';
                            if(!AccVendor::where('stamp', $stamp)->first()){
                                /**Prepare to store the Vendor into the System Now**/

                                $createVendor = AccVendor::create(
                                    [
                                        'branch' => $branch,
                                        'name' => $name,
                                        'acc_no' => $accNo,
                                        'contact_person' => $contactPerson,
                                        'ledger_id' => $expenseLedger,
                                        'phone' => $phoneNumber,
                                        'email' => $emailAddress,
                                        'kra' => $kra,
                                        'stamp' => $stamp,
                                        'created_by' => auth()->user()->id,
                                    ]
                                );
                                $res = 'error';
                                if($createVendor){
                                    $res = 'success';
                                }
                            }
                        }
                    }
                }
            }
        }
        return $res;
    }

    /**Create a Function to Display Vendor Information**/
    public function showVendor($id){
        $vendor = AccVendor::where('stamp', $id)->first();
        $vendors = AccVendor::orderBy('name', 'asc')->get();
        $pendingRequisitions = Requisition::where('dr_id',$vendor->id)->where('req_type', 'bill-payment')->where('second_approved', 0)->get();

        if($vendor){
            $recentTransactions = AccBillsManager::where('vendor_id', $vendor->id)->orderBy('created_at', 'desc')->get();
            return view('finance.vendors.acc-vendor', compact('vendor', 'vendors', 'recentTransactions', 'pendingRequisitions'));
        }
        return view('errors.404');
    }
}
