139 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			139 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Traits;
 | 
						|
 | 
						|
use App\Helper\Cache\CurrentMonthTimekeeping;
 | 
						|
use App\Models\Notes;
 | 
						|
use App\Models\User;
 | 
						|
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();
 | 
						|
        $dataNotes = Notes::getNotesByMonthAndYear((int)$month, (int)$year);
 | 
						|
 | 
						|
        $history = collect($history);
 | 
						|
        $result = [];
 | 
						|
        foreach ($admins as $key => $admin) {
 | 
						|
            $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;
 | 
						|
                });
 | 
						|
                $hasNotes = $dataNotes->filter(function ($entry) use ($i, $admin) {
 | 
						|
                    return $entry->n_user_id == $admin->id && $entry->n_day == $i;
 | 
						|
                });
 | 
						|
                // 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;
 | 
						|
                        }
 | 
						|
                    }
 | 
						|
                    $notes = [];
 | 
						|
                    if (count($hasNotes) > 0) {
 | 
						|
                        foreach ($hasNotes as $k_Note => $value_Note) {
 | 
						|
                            $notes[$k_Note] = [
 | 
						|
                                'id' => $value_Note->n_id,
 | 
						|
                                'timeType' => $value_Note->n_time_type,
 | 
						|
                                'timeTypeName' => $value_Note->time_type_name,
 | 
						|
                                'reason' => $value_Note->n_reason,
 | 
						|
                                'reasonName' => $value_Note->reason_name,
 | 
						|
                                'note' => $value_Note->n_note
 | 
						|
                            ];
 | 
						|
                        }
 | 
						|
                    }
 | 
						|
                    $user_data[] = ['values' => array_values($hasEntry->toArray()), 'notes' => array_values($notes), 'total' => $total, 'day' => $i];
 | 
						|
                } else {
 | 
						|
                    if (count($hasNotes) > 0) {
 | 
						|
                        $notes = [];
 | 
						|
                        foreach ($hasNotes as $k_Note => $value_Note) {
 | 
						|
                            $notes[$k_Note] = [
 | 
						|
                                'id' => $value_Note->n_id,
 | 
						|
                                'timeType' => $value_Note->n_time_type,
 | 
						|
                                'timeTypeName' => $value_Note->time_type_name,
 | 
						|
                                'reason' => $value_Note->n_reason,
 | 
						|
                                'reasonName' => $value_Note->reason_name,
 | 
						|
                                'note' => $value_Note->n_note
 | 
						|
                            ];
 | 
						|
                        }
 | 
						|
                        $user_data[] = ['values' => [], 'notes' => array_values($notes), 'total' => 0, 'day' => $i];
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }
 | 
						|
 | 
						|
            $result[] = ['user' => $admin->id, '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;
 | 
						|
    }
 | 
						|
 | 
						|
    public function appendUsersInformation($results)
 | 
						|
    {
 | 
						|
        $users = User::all();
 | 
						|
        $data = [];
 | 
						|
        foreach($users as $user){
 | 
						|
            foreach($results as $result){
 | 
						|
                $result = (array)$result;
 | 
						|
                if($result['user'] === $user->id){
 | 
						|
                    $data[] = ['user'=> $user, 'history' => $result['history']];
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
        return $data;
 | 
						|
    }
 | 
						|
}
 |