92 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			92 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Traits;
 | 
						|
 | 
						|
use App\Helper\Cache\CurrentMonthTimekeeping;
 | 
						|
use Carbon\Carbon;
 | 
						|
use Illuminate\Http\JsonResponse;
 | 
						|
use Illuminate\Support\Facades\DB;
 | 
						|
use Modules\Admin\app\Models\Admin;
 | 
						|
use Modules\Admin\app\Models\MonthlyTimekeeping;
 | 
						|
 | 
						|
trait AnalyzeData
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * Return a JSON response.
 | 
						|
     *
 | 
						|
     * @param  mixed  $data
 | 
						|
     * @param  int  $status
 | 
						|
     * @param  array  $headers
 | 
						|
     * @param  int  $options
 | 
						|
     * @return array $result
 | 
						|
     */
 | 
						|
    public function analyzeCurrentMonthTimeKeepingData($month, $year)
 | 
						|
    {
 | 
						|
        // dd((int)$month, (int)$year);
 | 
						|
        $now = Carbon::create((int)$year, (int)$month);
 | 
						|
        // $now = Carbon::create(2024, 5, 30); // Nếu muốn khởi tạo với một ngày cụ thể
 | 
						|
        $daysInMonth = $now->daysInMonth;
 | 
						|
        // Lấy ngày đầu tháng
 | 
						|
        $startOfMonth = $now->startOfMonth()->toDateString();
 | 
						|
        // Lấy ngày cuối tháng
 | 
						|
        $endOfMonth = $now->endOfMonth()->endOfDay()->toDateTimeString();
 | 
						|
        $admins = Admin::all();
 | 
						|
        $history = DB::table('tracking')->select('*')->whereBetween('tracking.created_at', [$startOfMonth, $endOfMonth])->orderBy('tracking.created_at', 'asc')->get();
 | 
						|
        $history = collect($history);
 | 
						|
        $result = [];
 | 
						|
        foreach ($admins as $key => $admin) {
 | 
						|
            if ($key == 0) {
 | 
						|
                $user_data = [];
 | 
						|
                for ($i = 1; $i <= $daysInMonth; $i++) {
 | 
						|
                    // Tạo ngày cụ thể trong tháng
 | 
						|
                    $date = Carbon::create($now->year, $now->month, $i)->setTimezone(env('TIME_ZONE'))->format('Y-m-d');
 | 
						|
                    // Kiểm tra xem có mục nào trong $history có created_at trùng với $date
 | 
						|
                    $hasEntry = $history->filter(function ($entry) use ($date, $admin) {
 | 
						|
                        // echo($hasEntry);
 | 
						|
                        return Carbon::parse($entry->created_at)->setTimezone(env('TIME_ZONE'))->format('Y-m-d') === $date && $entry->user_id == $admin->id;
 | 
						|
                    });
 | 
						|
                    // echo($hasEntry);
 | 
						|
                    // dd($date,$admin,$history,$daysInMonth);
 | 
						|
                    if (count($hasEntry) > 0) {
 | 
						|
                        $values = array_values($hasEntry->toArray());
 | 
						|
                        $last_checkin = null;
 | 
						|
                        $total = 0;
 | 
						|
                        foreach ($values as $value) {
 | 
						|
                            $createdAt = Carbon::parse($value->created_at)->setTimezone(env('TIME_ZONE'));
 | 
						|
                            if ($value->status == 'check out' && $last_checkin != null) {
 | 
						|
                                $lastCheckInTime = Carbon::parse($last_checkin)->setTimezone(env('TIME_ZONE'));
 | 
						|
                                // Tính thời gian làm việc bằng hiệu của thời gian check out và check in
 | 
						|
                                $workingTime = $createdAt->diffInSeconds($lastCheckInTime);
 | 
						|
                                $total += $workingTime;
 | 
						|
                            }
 | 
						|
 | 
						|
                            if ($value->status == 'check in') {
 | 
						|
                                $last_checkin = $createdAt;
 | 
						|
                            }
 | 
						|
                        }
 | 
						|
                        $user_data[] = ['values' => array_values($hasEntry->toArray()), 'total' => $total, 'day' => $i];
 | 
						|
                    }
 | 
						|
                }
 | 
						|
 | 
						|
                $result[] = ['user' => $admin, 'history' => $user_data];
 | 
						|
            }
 | 
						|
        }
 | 
						|
        return $result;
 | 
						|
    }
 | 
						|
 | 
						|
    public function createOrUpdateRecordForCurrentMonth($month, $year)
 | 
						|
    {
 | 
						|
        $data = MonthlyTimekeeping::where('month', '=', $month)->where('year', '=', $year)->first();
 | 
						|
        if ($data) {
 | 
						|
            $result = $this->analyzeCurrentMonthTimeKeepingData($month, $year);
 | 
						|
            $data->update(['data' => json_encode($result)]);
 | 
						|
        } else {
 | 
						|
            $result = $this->analyzeCurrentMonthTimeKeepingData($month, $year);
 | 
						|
            MonthlyTimekeeping::create(['month' => $month, 'year' => $year, 'working_days' => Carbon::create((int)$year, (int)$month)->daysInMonth, 'data' => json_encode($result)]);
 | 
						|
        }
 | 
						|
 | 
						|
        CurrentMonthTimekeeping::cleanCacheCurrentMonthTimekeeping();
 | 
						|
        return;
 | 
						|
    }
 | 
						|
}
 |