<?php

namespace App\Http\Controllers\Backend\report;

use App\Http\Controllers\Controller;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use App\Models\Expense;
use App\Models\ExpenseReceipt;
use App\Models\ExpenseReceiptService;
use App\Helpers\CurrentUser;
use App\Helpers\DefaultBranch;
use Carbon\Carbon;
use Auth;

class ExpenseReportController extends Controller
{
    function __construct()
    {
        $this->middleware('permission:expense-daily-report-access', ['only' => ['dailyReport','dailyReportWithDate']]);
        $this->middleware('permission:expense-monthly-report-access', ['only' => ['monthlyExpenseReport','selectedMonthExpenseReport']]);
        $this->middleware('permission:expense-between-report-access', ['only' => ['betweenExpenseReport','selectedBetweenExpenseReport']]);
    }

    //To get daily invoice report...
    public function dailyReport()
    {
        //Today date, month & year...
        $selectedDate = Carbon::now()->today()->toDateString();

        //To get all the ExpenseReceipt data...
        $expenseReceiptIds = ExpenseReceipt::whereDate('created_at','=', $selectedDate)->where('status', true)
                        ->select('id')->pluck('id')->toArray();
        $unpaidExpenseReceiptData = ExpenseReceipt::orderBy('id','desc')->whereDate('created_at','=', $selectedDate)->where('status', true)->get();

        //To get expense service data...
        $totalExpenseServiceQty = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->count();
        $totalExpenseServiceAmount = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->sum('expense_amount');

        return view('admin.reports.expenseReport.dailyExpenseReportList',compact('unpaidExpenseReceiptData'
                    ,'totalExpenseServiceQty','totalExpenseServiceAmount','selectedDate'));

    }
    
    //To get selected year invoice report...
    public function dailyReportWithDate(Request $request)
    {
        $request->validate([
            'selected_from_date'=> 'required',
        ]);

        //Today date, month & year...
        $selectedDate = Carbon::parse($request->selected_from_date)->format('Y-m-d');

        //To get all the ExpenseReceipt data...
        $expenseReceiptIds = ExpenseReceipt::whereDate('created_at','=', $selectedDate)->where('status', true)
                        ->select('id')->pluck('id')->toArray();
        $unpaidExpenseReceiptData = ExpenseReceipt::orderBy('id','desc')->whereDate('created_at','=', $selectedDate)->where('status', true)->get();

        //To get expense service data...
        $totalExpenseServiceQty = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->count();
        $totalExpenseServiceAmount = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->sum('expense_amount');

        return view('admin.reports.expenseReport.dailyExpenseReportList',compact('unpaidExpenseReceiptData'
                    ,'totalExpenseServiceQty','totalExpenseServiceAmount','selectedDate'));
    }

    //To get monthly expense report...
    public function monthlyExpenseReport()
    {
        //Today date, month & year...
        $todayDate = Carbon::now()->today()->toDateString();

        //To get current & all year...
        $currentYear = Carbon::now()->year;
        $getAllYears = [];
        $endYear = 2100;
        for($startYear = 2000; $startYear <= $endYear; $startYear++){
            $getAllYears[] = $startYear;
        }

        //To get all the months & current month name..
        $currentMonth = date('F', strtotime(Carbon::now()));
        $getAllMonthNames = [];
        for ($m=1; $m<=12; $m++) {
            $getAllMonthNames[] = date('F', mktime(0,0,0,$m, 1, date('Y')));
        }

        //To get all the ExpenseReceipt data...
        $expenseReceiptIds = ExpenseReceipt::whereYear('created_at','=', $currentYear)->whereMonth('created_at','=', date('m', strtotime($currentMonth)))
                        ->where('status', true)->select('id')->pluck('id')->toArray();
        $unpaidExpenseReceiptData = ExpenseReceipt::orderBy('id','desc')->whereYear('created_at','=', $currentYear)->whereMonth('created_at','=', date('m', strtotime($currentMonth)))
                        ->where('status', true)->get();

        //To get expense service data...
        $totalExpenseServiceQty = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->count();
        $totalExpenseServiceAmount = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->sum('expense_amount');

        return view('admin.reports.expenseReport.monthlyExpenseReportList',compact('unpaidExpenseReceiptData'
                    ,'totalExpenseServiceQty','totalExpenseServiceAmount','getAllYears','currentYear','getAllMonthNames','currentMonth'));
    }

    //To get selected month expense report...
    public function selectedMonthExpenseReport(Request $request)
    {
        $request->validate([
            'month'=> 'required',
            'year'=> 'required',
        ]);

        //Today date, month & year...
        $todayDate = Carbon::now()->today()->toDateString();
        $currentMonth = $request->month;
        $currentYear = $request->year;

        //To get all year...
        $getAllYears = [];
        $endYear = 2100;
        for($startYear = 2000; $startYear <= $endYear; $startYear++){
            $getAllYears[] = $startYear;
        }

        //To get all the months & current month name..
        $getAllMonthNames = [];
        for ($m=1; $m<=12; $m++) {
            $getAllMonthNames[] = date('F', mktime(0,0,0,$m, 1, date('Y')));
        }

        //To get all the ExpenseReceipt data...
        $expenseReceiptIds = ExpenseReceipt::whereYear('created_at','=', $currentYear)->whereMonth('created_at','=', date('m', strtotime($currentMonth)))
                        ->where('status', true)->select('id')->pluck('id')->toArray();
        $unpaidExpenseReceiptData = ExpenseReceipt::orderBy('id','desc')->whereYear('created_at','=', $currentYear)->whereMonth('created_at','=', date('m', strtotime($currentMonth)))
                        ->where('status', true)->get();

        //To get expense service data...
        $totalExpenseServiceQty = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->count();
        $totalExpenseServiceAmount = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->sum('expense_amount');

        return view('admin.reports.expenseReport.monthlyExpenseReportList',compact('unpaidExpenseReceiptData'
                    ,'totalExpenseServiceQty','totalExpenseServiceAmount','getAllYears','currentYear','getAllMonthNames','currentMonth'));

    }

    //To get between expense report...
    public function betweenExpenseReport()
    {
        //Today date, month & year...
        $todayDate = Carbon::now()->today()->toDateString();

        //Today & End date...
        $startDate = Carbon::now()->today()->toDateString();
        $endDate = Carbon::now()->today()->toDateString();

        //To get all the ExpenseReceipt data...
        $expenseReceiptIds = ExpenseReceipt::whereDate('created_at','>=',$startDate)->whereDate('created_at','<=',$endDate)
                        ->where('status', true)->select('id')->pluck('id')->toArray();
        $unpaidExpenseReceiptData = ExpenseReceipt::orderBy('id','desc')->whereDate('created_at','>=',$startDate)->whereDate('created_at','<=',$endDate)
                        ->where('status', true)->get();

        //To get expense service data...
        $totalExpenseServiceQty = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->count();
        $totalExpenseServiceAmount = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->sum('expense_amount');

        return view('admin.reports.expenseReport.betweenExpenseReportList',compact('unpaidExpenseReceiptData'
                    ,'totalExpenseServiceQty','totalExpenseServiceAmount','startDate','endDate'));
    }

    //To get selected between expense report...
    public function selectedBetweenExpenseReport(Request $request)
    {
        $request->validate([
            'start_date'=> 'required',
            'end_date'=> 'required',
        ]);

        //Today date, month & year...
        $todayDate = Carbon::now()->today()->toDateString();

        //Today & End date...
        $startDate = Carbon::parse($request->start_date)->format('Y-m-d');
        $endDate = Carbon::parse($request->end_date)->format('Y-m-d');

        //To get all the ExpenseReceipt data...
        $expenseReceiptIds = ExpenseReceipt::whereDate('created_at','>=',$startDate)->whereDate('created_at','<=',$endDate)
                        ->where('status', true)->select('id')->pluck('id')->toArray();
        $unpaidExpenseReceiptData = ExpenseReceipt::orderBy('id','desc')->whereDate('created_at','>=',$startDate)->whereDate('created_at','<=',$endDate)
                        ->where('status', true)->get();

        //To get expense service data...
        $totalExpenseServiceQty = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->count();
        $totalExpenseServiceAmount = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->sum('expense_amount');

        return view('admin.reports.expenseReport.betweenExpenseReportList',compact('unpaidExpenseReceiptData'
                    ,'totalExpenseServiceQty','totalExpenseServiceAmount','startDate','endDate'));

    }

    //To get yearly invoice report...
    public function yearlyExpenseReport()
    {
        //Today date, month & year...
        $todayDate = Carbon::now()->today()->toDateString();
        $currentMonth = date('F', strtotime(Carbon::now()));
        $currentYear = Carbon::now()->year;

        //To get current & all year...
        $currentYear = Carbon::now()->year;
        $getAllYears = [];
        $endYear = 2100;
        for($startYear = 2000; $startYear <= $endYear; $startYear++){
            $getAllYears[] = $startYear;
        }

        //To get all the ExpenseReceipt data...
        $expenseReceiptIds = ExpenseReceipt::whereYear('created_at','=', $currentYear)->where('status', true)
                        ->select('id')->pluck('id')->toArray();
        $unpaidExpenseReceiptData = ExpenseReceipt::orderBy('id','desc')->whereYear('created_at','=', $currentYear)->where('status', true)->get();

        //To get expense service data...
        $totalExpenseServiceQty = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->count();
        $totalExpenseServiceAmount = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->sum('expense_amount');

        return view('admin.reports.expenseReport.yearlyExpenseReportList',compact('unpaidExpenseReceiptData'
                    ,'totalExpenseServiceQty','totalExpenseServiceAmount','getAllYears','currentYear'));

    }
    
    //To get selected year invoice report...
    public function selectedYearExpenseReport(Request $request)
    {
        //Today date, month & year...
        $todayDate = Carbon::now()->today()->toDateString();
        $currentMonth = date('F', strtotime(Carbon::now()));
        $currentYear = $request->year;

        //To get all year...
        $getAllYears = [];
        $endYear = 2100;
        for($startYear = 2000; $startYear <= $endYear; $startYear++){
            $getAllYears[] = $startYear;
        }

        //To get all the ExpenseReceipt data...
        $expenseReceiptIds = ExpenseReceipt::whereYear('created_at','=', $currentYear)->where('status', true)
                        ->select('id')->pluck('id')->toArray();
        $unpaidExpenseReceiptData = ExpenseReceipt::orderBy('id','desc')->whereYear('created_at','=', $currentYear)->where('status', true)->get();

        //To get expense service data...
        $totalExpenseServiceQty = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->count();
        $totalExpenseServiceAmount = ExpenseReceiptService::orderBy('id','desc')
                    ->whereIn('expense_receipt_id', $expenseReceiptIds)->sum('expense_amount');

        return view('admin.report.expense.yearlyExpenseReportList',compact('unpaidExpenseReceiptData'
                    ,'totalExpenseServiceQty','totalExpenseServiceAmount','getAllYears','currentYear'));
    }
}
