269 lines
11 KiB
PHP
269 lines
11 KiB
PHP
<?php
|
|
|
|
namespace Modules\Admin\app\Http\Controllers;
|
|
|
|
use App\Helper\Cache\CurrentMonthTimekeeping;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Notes;
|
|
use App\Traits\AnalyzeData;
|
|
use App\Traits\HasFilterRequest;
|
|
use App\Traits\HasOrderByRequest;
|
|
use App\Traits\HasSearchRequest;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use App\Mail\TicketMail;
|
|
use Modules\Admin\app\Models\Admin;
|
|
use Modules\Admin\app\Models\MonthlyTimekeeping;
|
|
use Modules\Admin\app\Models\Tracking;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use App\Exports\TimekeepingExport;
|
|
use Modules\Admin\app\Models\Ticket;
|
|
|
|
class TimekeepingController extends Controller
|
|
{
|
|
use HasOrderByRequest;
|
|
use HasFilterRequest;
|
|
use HasSearchRequest;
|
|
use AnalyzeData;
|
|
|
|
public function get(Request $request)
|
|
{
|
|
$currentDate = Carbon::now();
|
|
$currentMonth = $currentDate->month;
|
|
$currentYear = $currentDate->year;
|
|
$data = MonthlyTimekeeping::where('month', '=', $request->month)->where('year', '=', $request->year)->first();
|
|
if ($currentMonth == (int)$request->month && $currentYear == (int)$request->year) {
|
|
$cacheData = CurrentMonthTimekeeping::getCacheCurrentMonthTimekeeping();
|
|
if ($cacheData) {
|
|
$cacheData->data = json_decode($cacheData->data, true);
|
|
return response()->json(['status' => true, 'data' => $this->appendUsersInformation($cacheData->data), 'working_days' => $cacheData->working_days, 'message' => 'Get from cache']);
|
|
} else {
|
|
$result = $this->analyzeCurrentMonthTimeKeepingData($currentMonth, $currentYear);
|
|
if ($data) {
|
|
$data->update(['data' => json_encode($result)]);
|
|
return response()->json(['status' => true, 'data' => $this->appendUsersInformation($result), 'message' => 'Get from analyzeCurrentMonthTimeKeepingData + update record']);
|
|
}
|
|
MonthlyTimekeeping::create(['month' => $currentMonth, 'year' => $currentYear, 'working_days' => $currentDate->daysInMonth, 'data' => json_encode($result)]);
|
|
return response()->json(['status' => true, 'data' => $this->appendUsersInformation($result), 'message' => 'Get from analyzeCurrentMonthTimeKeepingData + create record']);
|
|
}
|
|
} else {
|
|
if ($data) {
|
|
$data['data'] = json_decode($data['data']);
|
|
return response()->json(['status' => true, 'data' => $this->appendUsersInformation($data['data']), 'working_days' => $data['working_days'], 'message' => 'Get from DB']);
|
|
} else {
|
|
$result = $this->analyzeCurrentMonthTimeKeepingData($request->month, $request->year);
|
|
MonthlyTimekeeping::create(['month' => $request->month, 'year' => $request->year, 'working_days' => Carbon::create((int)$request->year, (int)$request->month)->daysInMonth, 'data' => json_encode($result)]);
|
|
return response()->json(['status' => true, 'data' => $this->appendUsersInformation($result), 'message' => 'Get from analyzeCurrentMonthTimeKeepingData']);
|
|
}
|
|
}
|
|
return response()->json(['status' => false, 'mewssage' => 'Get data failed!']);
|
|
}
|
|
|
|
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')
|
|
]
|
|
]);
|
|
}
|
|
$this->createOrUpdateRecordForCurrentMonth($month, $year);
|
|
return response()->json(['status' => true, 'message' => 'Add successfully']);
|
|
}
|
|
|
|
public function saveWorkingDays(Request $request)
|
|
{
|
|
$data = MonthlyTimekeeping::where('month', '=', $request->month)->where('year', '=', $request->year)->first();
|
|
if ($data) {
|
|
$data->update(['working_days' => $request->working_days]);
|
|
$this->createOrUpdateRecordForCurrentMonth($request->month, $request->year);
|
|
return response()->json(['status' => true, 'message' => 'Update successful']);
|
|
}
|
|
|
|
return response()->json(['status' => false, 'message' => 'Update failed']);
|
|
}
|
|
|
|
public function addNoteForUser(Request $request)
|
|
{
|
|
$user_id = $request->users["id"] ?? "";
|
|
$month = $request->month;
|
|
$year = $request->year;
|
|
$day = $request->day;
|
|
$time_type = $request->type;
|
|
$reason = $request->reason;
|
|
$note = $request->note;
|
|
|
|
if ($user_id == "") {
|
|
return response()->json(['status' => false, 'message' => 'User not found!']);
|
|
}
|
|
|
|
$existingNote = Notes::where('n_user_id', $user_id)
|
|
->where('n_day', $day)
|
|
->where('n_month', $month)
|
|
->where('n_year', $year)
|
|
->where('n_reason', $reason)
|
|
->first();
|
|
|
|
if ($existingNote) {
|
|
// Cập nhật bản ghi nếu đã tồn tại
|
|
$existingNote->update([
|
|
'n_day' => $day,
|
|
'n_month' => $month,
|
|
'n_year' => $year,
|
|
'n_time_type' => $time_type,
|
|
'n_reason' => $reason,
|
|
'n_note' => $note
|
|
]);
|
|
} else {
|
|
// Chèn bản ghi mới nếu không tồn tại
|
|
Notes::create([
|
|
'n_user_id' => $user_id,
|
|
'n_day' => $day,
|
|
'n_month' => $month,
|
|
'n_year' => $year,
|
|
'n_time_type' => $time_type,
|
|
'n_reason' => $reason,
|
|
'n_note' => $note
|
|
]);
|
|
}
|
|
|
|
$this->createOrUpdateRecordForCurrentMonth($month, $year);
|
|
|
|
return response()->json(['status' => true, 'message' => 'Add successfully']);
|
|
}
|
|
|
|
public function updateCacheMonth(Request $request)
|
|
{
|
|
$month = $request->month;
|
|
$year = $request->year;
|
|
|
|
$this->createOrUpdateRecordForCurrentMonth($month, $year);
|
|
|
|
return response()->json(['status' => true, 'message' => 'Update successfully']);
|
|
}
|
|
|
|
public function deleteNote(Request $request)
|
|
{
|
|
$rules = [
|
|
'id' => 'required'
|
|
];
|
|
|
|
// Validate the request
|
|
$request->validate($rules);
|
|
$id = $request->input('id');
|
|
$month = $request->month;
|
|
$year = $request->year;
|
|
|
|
$note = Notes::find($id);
|
|
if (!$note) {
|
|
return response()->json(['message' => 'Note not found', 'status' => false]);
|
|
}
|
|
|
|
if ($note->ticket_id != null) {
|
|
$ticket = Ticket::find($note->ticket_id);
|
|
if (!$ticket) {
|
|
return response()->json(['message' => 'Ticket not found, can not delete note', 'status' => false]);
|
|
}
|
|
|
|
$admin = auth('admins')->user();
|
|
|
|
// Handle send mail
|
|
$dataMasterStartPeriod = CategoryController::getListMasterByCodeAndType("TIME_TYPE", $ticket->start_period);
|
|
$dataMasterEndPeriod = CategoryController::getListMasterByCodeAndType("TIME_TYPE", $ticket->end_period);
|
|
$dataMasterType = CategoryController::getListMasterByCodeAndType("REASON", $ticket->type);
|
|
$formattedStartDate = Carbon::createFromFormat('Y-m-d', $ticket->start_date)->format('d/m/Y');
|
|
$formattedEndDate = Carbon::createFromFormat('Y-m-d', $ticket->end_date)->format('d/m/Y');
|
|
|
|
$user = Admin::find($ticket->user_id);
|
|
|
|
$data = array(
|
|
"email_template" => "email.notification_tickets_user",
|
|
"user_name" => $user->name,
|
|
"email" => $user->email,
|
|
"name" => $admin->name, //name admin duyệt
|
|
"date" => $dataMasterStartPeriod->c_name . " (" . $formattedStartDate . ") - " . $dataMasterEndPeriod->c_name . " (" . $formattedEndDate . ")",
|
|
"type" => $dataMasterType->c_name,
|
|
"note" => $ticket->reason,
|
|
"admin_note" => $ticket->admin_note,
|
|
"link" => "/tickets", //link đến page admin
|
|
"status" => "refused",
|
|
"subject" => "[Ticket response] Ticket From " . $admin->name
|
|
);
|
|
Mail::to($user->email)->send(new TicketMail($data));
|
|
|
|
// Update
|
|
$ticket->updated_by = $admin->name;
|
|
$ticket->status = "REFUSED";
|
|
$ticket->save();
|
|
Notes::where('ticket_id', $ticket->id)->delete();
|
|
|
|
// Clear Timekeeping cache
|
|
$this->createOrUpdateRecordForCurrentMonth(Carbon::parse($ticket->start_date)->month, Carbon::parse($ticket->start_date)->year);
|
|
$this->createOrUpdateRecordForCurrentMonth(Carbon::parse($ticket->end_date)->month, Carbon::parse($ticket->end_date)->year);
|
|
|
|
return response()->json(['message' => 'Delete success', 'status' => true]);
|
|
}
|
|
|
|
$note->delete();
|
|
$this->createOrUpdateRecordForCurrentMonth($month, $year);
|
|
|
|
return response()->json(['message' => 'Delete success', 'status' => true]);
|
|
}
|
|
|
|
public function export(Request $request)
|
|
{
|
|
// Validate request
|
|
$request->validate([
|
|
'month' => 'required|numeric|between:1,12',
|
|
'year' => 'required|numeric|min:2000',
|
|
'working_days' => 'required|numeric|between:1,31'
|
|
]);
|
|
|
|
// Reuse get() function to fetch data
|
|
$response = $this->get($request);
|
|
$responseData = json_decode($response->getContent(), true);
|
|
|
|
if (!$responseData['status']) {
|
|
return response()->json(['status' => false, 'message' => 'No data found']);
|
|
}
|
|
|
|
// Lọc chỉ lấy user có permission bao gồm staff
|
|
$staffData = array_filter($responseData['data'], function ($user) {
|
|
return isset($user['user']['permission']) && strpos($user['user']['permission'], 'staff') !== false;
|
|
});
|
|
|
|
$currentDate = date('d_His');
|
|
return Excel::download(
|
|
new TimekeepingExport(
|
|
array_values($staffData), // Convert to indexed array after filtering
|
|
$request->month,
|
|
$request->year,
|
|
$request->working_days
|
|
),
|
|
"Timekeeping_{$request->month}_{$request->year}_{$currentDate}.xlsx"
|
|
);
|
|
}
|
|
}
|