<?php

namespace App\Http\Controllers\Modules;

use App\Http\Controllers\Controller;
use App\Models\BillMasterCompanyOldInventory;
use App\Models\CompanyInventory;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class InventoryOldDashboardController extends Controller
{
    public function index(Request $request){
        $currency = [];
        $year = Carbon::now()->format('Y');
        if($request->has('search-year')){
            $year = $request['search-year'];
        }
        /**We now get a report of sales**/
        $jan = BillMasterCompanyOldInventory::whereBetween('created_at', [$year.'-01-01 00:00:00', $year.'-01-31 23:59:59'])->get();
        $feb = BillMasterCompanyOldInventory::whereBetween('created_at', [$year.'-02-01 00:00:00', $year.'-02-28 23:59:59'])->get();
        $mar = BillMasterCompanyOldInventory::whereBetween('created_at', [$year.'-03-01 00:00:00', $year.'-03-31 23:59:59'])->get();
        $apr = BillMasterCompanyOldInventory::whereBetween('created_at', [$year.'-04-01 00:00:00', $year.'-04-30 23:59:59'])->get();
        $may = BillMasterCompanyOldInventory::whereBetween('created_at', [$year.'-05-01 00:00:00', $year.'-05-31 23:59:59'])->get();
        $jun = BillMasterCompanyOldInventory::whereBetween('created_at', [$year.'-06-01 00:00:00', $year.'-06-30 23:59:59'])->get();
        $jul = BillMasterCompanyOldInventory::whereBetween('created_at', [$year.'-07-01 00:00:00', $year.'-07-31 23:59:59'])->get();
        $aug = BillMasterCompanyOldInventory::whereBetween('created_at', [$year.'-08-01 00:00:00', $year.'-08-31 23:59:59'])->get();
        $sep = BillMasterCompanyOldInventory::whereBetween('created_at', [$year.'-09-01 00:00:00', $year.'-09-30 23:59:59'])->get();
        $oct = BillMasterCompanyOldInventory::whereBetween('created_at', [$year.'-10-01 00:00:00', $year.'-10-31 23:59:59'])->get();
        $nov = BillMasterCompanyOldInventory::whereBetween('created_at', [$year.'-11-01 00:00:00', $year.'-11-30 23:59:59'])->get();
        $dec = BillMasterCompanyOldInventory::whereBetween('created_at', [$year.'-12-01 00:00:00', $year.'-12-31 23:59:59'])->get();

        $janData = [
            'purchases' => $jan ? $jan->where('type', '!=', 'sell')->sum('total_price_buy') : 0,
            'sales' => $jan ? $jan->where('type', 'sell')->sum('total_price_sell') : 0,
            'profit' => $jan ? $jan->where('type', 'sell')->sum('profit') : 0,
        ];
        $febData = [
            'purchases' => $feb ? $feb->where('type', '!=', 'sell')->sum('total_price_buy') : 0,
            'sales' => $feb ? $feb->where('type', 'sell')->sum('total_price_sell') : 0,
            'profit' => $feb ? $feb->where('type', 'sell')->sum('profit') : 0,
        ];
        $marData = [
            'purchases' => $mar ? $mar->where('type', '!=', 'sell')->sum('total_price_buy') : 0,
            'sales' => $mar ? $mar->where('type', 'sell')->sum('total_price_sell') : 0,
            'profit' => $mar ? $mar->where('type', 'sell')->sum('profit') : 0,
        ];
        $aprData = [
            'purchases' => $apr ? $apr->where('type', '!=', 'sell')->sum('total_price_buy') : 0,
            'sales' => $apr ? $apr->where('type', 'sell')->sum('total_price_sell') : 0,
            'profit' => $apr ? $apr->where('type', 'sell')->sum('profit') : 0,
        ];
        $mayData = [
            'purchases' => $may ? $may->where('type', '!=', 'sell')->sum('total_price_buy') : 0,
            'sales' => $may ? $may->where('type', 'sell')->sum('total_price_sell') : 0,
            'profit' => $may ? $may->where('type', 'sell')->sum('profit') : 0,
        ];
        $junData = [
            'purchases' => $jun ? $jun->where('type', '!=', 'sell')->sum('total_price_buy') : 0,
            'sales' => $jun ? $jun->where('type', 'sell')->sum('total_price_sell') : 0,
            'profit' => $jun ? $jun->where('type', 'sell')->sum('profit') : 0,
        ];
        $julData = [
            'purchases' => $jul ? $jul->where('type', '!=', 'sell')->sum('total_price_buy') : 0,
            'sales' => $jul ? $jul->where('type', 'sell')->sum('total_price_sell') : 0,
            'profit' => $jul ? $jul->where('type', 'sell')->sum('profit') : 0,
        ];
        $augData = [
            'purchases' => $aug ? $aug->where('type', '!=', 'sell')->sum('total_price_buy') : 0,
            'sales' => $aug ? $aug->where('type', 'sell')->sum('total_price_sell') : 0,
            'profit' => $aug ? $aug->where('type', 'sell')->sum('profit') : 0,
        ];
        $sepData = [
            'purchases' => $sep ? $sep->where('type', '!=', 'sell')->sum('total_price_buy') : 0,
            'sales' => $sep ? $sep->where('type', 'sell')->sum('total_price_sell') : 0,
            'profit' => $sep ? $sep->where('type', 'sell')->sum('profit') : 0,
        ];
        $octData = [
            'purchases' => $oct ? $oct->where('type', '!=', 'sell')->sum('total_price_buy') : 0,
            'sales' => $oct ? $oct->where('type', 'sell')->sum('total_price_sell') : 0,
            'profit' => $oct ? $oct->where('type', 'sell')->sum('profit') : 0,
        ];
        $novData = [
            'purchases' => $nov ? $nov->where('type', '!=', 'sell')->sum('total_price_buy') : 0,
            'sales' => $nov ? $nov->where('type', 'sell')->sum('total_price_sell') : 0,
            'profit' => $nov ? $nov->where('type', 'sell')->sum('profit') : 0,
        ];
        $decData = [
            'purchases' => $dec ? $dec->where('type', '!=', 'sell')->sum('total_price_buy') : 0,
            'sales' => $dec ? $dec->where('type', 'sell')->sum('total_price_sell') : 0,
            'profit' => $dec ? $dec->where('type', 'sell')->sum('profit') : 0,
        ];


        $search = ['type' => 'production'];
        return view('modules.stock-dash-old', compact('year','currency', 'search', 'janData', 'febData', 'marData', 'aprData', 'mayData', 'junData', 'julData', 'augData', 'sepData', 'octData', 'novData', 'decData'));
    }

    /**Create  Function that will be used to Take Stock in the Company and Display available items as Stock in the Company**/
    public function manageInventory()
    {
        $list = CompanyInventory::orderBy('name', 'asc')->get();

        return view('inventory-old.manage-inventory-old', compact('list'));
    }

    /**Create a Function that will be used to Display a Page that we can use to Run Daily Sales with Ease**/
    public function showSalesReport(Request $request)
    {
        /**By Default, we pull for today's Report**/
        $list = BillMasterCompanyOldInventory::where('type', 'sell')->whereBetween('created_at', [date('Y-m-d')." 00:00:00",date('Y-m-d')." 23:59:59"])->get();
        if($request->has('search')){
            $list = BillMasterCompanyOldInventory::where('type', 'sell')->whereBetween('created_at', [$request['from']." 00:00:00",$request['to']." 23:59:59"])->get();
        }
        $search = [
            'from' => $request['from'],
            'to' => $request['to'],
        ];
        return view('inventory-old.daily-sales-old', compact('list', 'search'));
    }

    /**Create a Function that will be used to Display a Page that we can use to Run Inventory Loaded with Ease**/
    public function showInventoryReport(Request $request)
    {
        /**By Default, we pull for today's Report**/
        $list = BillMasterCompanyOldInventory::where('type', 'inventory')->whereBetween('created_at', [date('Y-m-d')." 00:00:00",date('Y-m-d')." 23:59:59"])->get();
        if($request->has('search')){
            $list = BillMasterCompanyOldInventory::where('type', 'inventory')->whereBetween('created_at', [$request['from']." 00:00:00",$request['to']." 23:59:59"])->get();
        }
        $search = [
            'from' => $request['from'],
            'to' => $request['to'],
        ];
        return view('inventory-old.loaded-inventory-analysis-old', compact('list', 'search'));
    }

    /**Create a function that will be used to Display the Inventory status or Analysis for the Company**/
    public function getCompanyInventoryAnalysis()
    {
        if(auth()->user()->admin == 1){
            $list = CompanyInventory::orderBy('name', 'asc')->get();

            return view('inventory-old.inventory-analysis-old', compact('list'));
        }
        return view('errors.404');
    }

    /**Create a function that will be used to do comparative analysis on stocks between old and new stock**/
    public function comparativeInventoryAnalysis(Request $request)
    {

        if(auth()->user()->admin == 1){
            $list = [];
            $search = [
                'as-at' => date('Y-m-d'),
            ];
            if($request->has('search')){
                /**We only pool a report for items when the as at date is specified**/
                $list = CompanyInventory::orderBy('name', 'asc')->get();
                $search = [
                    'as-at' => $request['as-at'],
                ];
            }
            return view('inventory-old.comparative-inventory-analysis', compact('list', 'search'));
        }
        return view('errors.404');
    }


}
