<?php

namespace App\Helper\Cache;

use Modules\Admin\app\Models\MonthlyTimekeeping;
use Illuminate\Support\Facades\Cache;
use App\Helper\Constant\CacheConstant;
use Carbon\Carbon;

class CurrentMonthTimekeeping
{
    public static $globals = 'current-month-timekeeping-data';
    public static $key = 'current-month-timekeeping-data';
    public static function getCacheCurrentMonthTimekeeping()
    {
        $currentDate = Carbon::now();
        $currentMonth = $currentDate->month;
        $currentYear = $currentDate->year;
        $monthData = Cache::get(self::$key, null);
        if (isset($GLOBALS[self::$globals]) && $monthData != null)
            return $GLOBALS[self::$globals];
        if ($monthData == null) {
            $monthData = MonthlyTimekeeping::where('month', '=', $currentMonth)->where('year', '=', $currentYear)->first();
            if ($monthData) {
                Cache::put(self::$key, json_encode($monthData), (60 * CacheConstant::$expired));
                $monthData = json_encode($monthData);
            } else {
                $monthData = null;
            }
        }
        
        if ($monthData == null) {
            $GLOBALS[self::$globals] = null;
        } else {
            $GLOBALS[self::$globals] = json_decode($monthData);
        }
        return $GLOBALS[self::$globals];
    }

    public static function cleanCacheCurrentMonthTimekeeping()
    {
        Cache::forget(self::$key);
    }
}
