75 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
<?php
 | 
						|
 | 
						|
namespace Modules\Admin\app\Http\Controllers;
 | 
						|
 | 
						|
use App\Http\Controllers\Controller;
 | 
						|
use App\Traits\HasFilterRequest;
 | 
						|
use App\Traits\HasOrderByRequest;
 | 
						|
use App\Traits\HasSearchRequest;
 | 
						|
use Carbon\Carbon;
 | 
						|
use Illuminate\Http\Request;
 | 
						|
use Illuminate\Support\Facades\DB;
 | 
						|
use Modules\Admin\app\Models\Admin;
 | 
						|
use Modules\Admin\app\Models\Tracking;
 | 
						|
 | 
						|
use function PHPSTORM_META\type;
 | 
						|
 | 
						|
class TimekeepingController extends Controller
 | 
						|
{
 | 
						|
    use HasOrderByRequest;
 | 
						|
    use HasFilterRequest;
 | 
						|
    use HasSearchRequest;
 | 
						|
 | 
						|
    public function get(Request $request)
 | 
						|
    {
 | 
						|
        // dd($request->date);
 | 
						|
        $now = Carbon::create($request->year, $request->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;
 | 
						|
        // return response()->json(['status'=> true, 'data'=>$request->all()]);
 | 
						|
        // Lấy ngày đầu tháng
 | 
						|
        $startOfMonth = $now->startOfMonth()->toDateString();
 | 
						|
        // Lấy ngày cuối tháng
 | 
						|
        $endOfMonth = $now->endOfMonth()->toDateString();
 | 
						|
        $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 $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) {
 | 
						|
                    return Carbon::parse($entry->created_at)->setTimezone(env('TIME_ZONE'))->format('Y-m-d') === $date && $entry->user_id == $admin->id;
 | 
						|
                });
 | 
						|
                
 | 
						|
                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 response()->json(['status'=> true, 'data'=>$result]);
 | 
						|
    }
 | 
						|
}
 |