<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Branch;
use App\Models\Billing;
use App\Models\BillPayment;
use App\Models\ExpenseReceipt;
use App\Models\ExpenseReceiptService;
use Carbon\Carbon;
use Validator;

class ReportController extends Controller
{
    /*
    *******************************
    Expense Report Start...
    *******************************
    */

    //To get yearly expense 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)->with(['expenseCategoryData','expenseData'])->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 response()->json([
            'message'   =>  'Successfully loaded ata.',
            'unpaidExpenseReceiptData'   =>  $unpaidExpenseReceiptData,
            'totalExpenseServiceQty'   =>  $totalExpenseServiceQty,
            'totalExpenseServiceAmount'   =>  $totalExpenseServiceAmount,
            'getAllYears'   =>  $getAllYears,
            'currentYear'   =>  $currentYear,
            'status_code'   => 201
        ], 201);
    }
    
    //To get selected year expense report...
    public function selectedYearExpenseReport(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'year'=> 'required',
        ]);

        if ($validator->fails()) {
        	return response(['errors'=>$validator->errors()->all()], 422);
        }

        //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)->with(['expenseCategoryData','expenseData'])->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 response()->json([
            'message'   =>  'Successfully loaded ata.',
            'unpaidExpenseReceiptData'   =>  $unpaidExpenseReceiptData,
            'totalExpenseServiceQty'   =>  $totalExpenseServiceQty,
            'totalExpenseServiceAmount'   =>  $totalExpenseServiceAmount,
            'getAllYears'   =>  $getAllYears,
            'currentYear'   =>  $currentYear,
            'status_code'   => 201
        ], 201);
    }

    //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)->with(['expenseCategoryData','expenseData'])->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 response()->json([
            'message'   =>  'Successfully loaded ata.',
            'unpaidExpenseReceiptData'   =>  $unpaidExpenseReceiptData,
            'totalExpenseServiceQty'   =>  $totalExpenseServiceQty,
            'totalExpenseServiceAmount'   =>  $totalExpenseServiceAmount,
            'getAllYears'   =>  $getAllYears,
            'currentYear'   =>  $currentYear,
            'getAllMonthNames'   =>  $getAllMonthNames,
            'currentMonth'   =>  $currentMonth,
            'status_code'   => 201
        ], 201);
    }

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

        if ($validator->fails()) {
        	return response(['errors'=>$validator->errors()->all()], 422);
        }

        //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)->with(['expenseCategoryData','expenseData'])->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 response()->json([
            'message'   =>  'Successfully loaded ata.',
            'unpaidExpenseReceiptData'   =>  $unpaidExpenseReceiptData,
            'totalExpenseServiceQty'   =>  $totalExpenseServiceQty,
            'totalExpenseServiceAmount'   =>  $totalExpenseServiceAmount,
            'getAllYears'   =>  $getAllYears,
            'currentYear'   =>  $currentYear,
            'getAllMonthNames'   =>  $getAllMonthNames,
            'currentMonth'   =>  $currentMonth,
            'status_code'   => 201
        ], 201);
    }

    //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)->with(['expenseCategoryData','expenseData'])->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 response()->json([
            'message'   =>  'Successfully loaded ata.',
            'unpaidExpenseReceiptData'   =>  $unpaidExpenseReceiptData,
            'totalExpenseServiceQty'   =>  $totalExpenseServiceQty,
            'totalExpenseServiceAmount'   =>  $totalExpenseServiceAmount,
            'startDate'   =>  $startDate,
            'endDate'   =>  $endDate,
            'status_code'   => 201
        ], 201);
    }

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

        if ($validator->fails()) {
        	return response(['errors'=>$validator->errors()->all()], 422);
        }

        //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)->with(['expenseCategoryData','expenseData'])->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 response()->json([
            'message'   =>  'Successfully loaded ata.',
            'unpaidExpenseReceiptData'   =>  $unpaidExpenseReceiptData,
            'totalExpenseServiceQty'   =>  $totalExpenseServiceQty,
            'totalExpenseServiceAmount'   =>  $totalExpenseServiceAmount,
            'startDate'   =>  $startDate,
            'endDate'   =>  $endDate,
            'status_code'   => 201
        ], 201);
    }
}
