110 lines
4.6 KiB
PHP
110 lines
4.6 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) {
|
|
// echo($hasEntry);
|
|
return Carbon::parse($entry->created_at)->setTimezone(env('TIME_ZONE'))->format('Y-m-d') === $date && $entry->user_id == $admin->id;
|
|
});
|
|
// echo($hasEntry);
|
|
|
|
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]);
|
|
}
|
|
|
|
public function addWorkingTimeForMultipleUser(Request $request)
|
|
{
|
|
$user_ids = $request->users;
|
|
$year = $request->year;
|
|
$month = $request->month;
|
|
$day = $request->day;
|
|
$type = $request->type;
|
|
foreach($user_ids as $id){
|
|
$user = Admin::find($id);
|
|
$date = Carbon::create($year, $month, $day)->setTimezone(env('TIME_ZONE'));
|
|
$start = $date->copy()->setTime(7, 31, 11);
|
|
$end = $type == 'half' ? $date->copy()->setTime(11, 31, 11) : $date->copy()->setTime(17, 1, 11);
|
|
Tracking::insert([
|
|
[
|
|
'name' => $user->name,
|
|
'user_id' => $user->id,
|
|
'status' => 'check in',
|
|
'time_string' => $start ->format('Y-m-d H:i:s'),
|
|
'created_at' => $start->setTimezone('UTC')
|
|
],
|
|
[
|
|
'name' => $user->name,
|
|
'user_id' => $user->id,
|
|
'status' => 'check out',
|
|
'time_string' => $end->format('Y-m-d H:i:s'),
|
|
'created_at' => $end->setTimezone('UTC')
|
|
]
|
|
]);
|
|
}
|
|
|
|
return response()->json(['status'=> true, 'message'=> 'Add successfully']);
|
|
}
|
|
}
|