dev #130
|
|
@ -388,9 +388,10 @@ class TicketController extends Controller
|
||||||
// Danh sách ngày nghỉ theo tháng
|
// Danh sách ngày nghỉ theo tháng
|
||||||
$requestMonths = $this->groupLeaveRequestsByMonth($dataListPeriod);
|
$requestMonths = $this->groupLeaveRequestsByMonth($dataListPeriod);
|
||||||
$monthsInfo = [];
|
$monthsInfo = [];
|
||||||
|
$hasInsufficientDays = false;
|
||||||
|
$errorMessage = '';
|
||||||
|
|
||||||
foreach ($requestMonths as $monthKey => $monthData) {
|
foreach ($requestMonths as $monthKey => $monthData) {
|
||||||
|
|
||||||
// Tính tổng số ngày nghỉ có phép trong tháng
|
// Tính tổng số ngày nghỉ có phép trong tháng
|
||||||
$usedDaysInMonth = $this->getUsedLeaveDaysInMonth($user, $monthData['year'], $monthData['month'], 'ONLEAVE');
|
$usedDaysInMonth = $this->getUsedLeaveDaysInMonth($user, $monthData['year'], $monthData['month'], 'ONLEAVE');
|
||||||
|
|
||||||
|
|
@ -403,7 +404,6 @@ class TicketController extends Controller
|
||||||
// Tính tổng số ngày nghỉ trong tháng
|
// Tính tổng số ngày nghỉ trong tháng
|
||||||
$totalDaysInMonth = $usedDaysInMonth + $usedDaysInMonthWithoutPay + $monthData['days_requested'];
|
$totalDaysInMonth = $usedDaysInMonth + $usedDaysInMonthWithoutPay + $monthData['days_requested'];
|
||||||
|
|
||||||
|
|
||||||
// Tính tổng phép có trong tháng
|
// Tính tổng phép có trong tháng
|
||||||
$totalLeaveDaysInMonth = $this->getTotalLeaveDaysInMonth($user, $monthData['year'], $monthData['month']);
|
$totalLeaveDaysInMonth = $this->getTotalLeaveDaysInMonth($user, $monthData['year'], $monthData['month']);
|
||||||
|
|
||||||
|
|
@ -412,7 +412,6 @@ class TicketController extends Controller
|
||||||
|
|
||||||
//Ngày phép còn lại trong tháng
|
//Ngày phép còn lại trong tháng
|
||||||
$remainingDaysInMonth = $totalLeaveDaysInMonth - $totalLeaveDaysInMonthToMonth;
|
$remainingDaysInMonth = $totalLeaveDaysInMonth - $totalLeaveDaysInMonthToMonth;
|
||||||
// dd($remainingDaysInMonth, $totalLeaveDaysInMonth, $totalLeaveDaysInMonthToMonth);
|
|
||||||
|
|
||||||
$month_data = [
|
$month_data = [
|
||||||
'year' => $monthData['year'],
|
'year' => $monthData['year'],
|
||||||
|
|
@ -423,51 +422,44 @@ class TicketController extends Controller
|
||||||
'days_used' => $usedDaysInMonth, //tổng số ngày nghỉ có phép ở tháng hiện tại
|
'days_used' => $usedDaysInMonth, //tổng số ngày nghỉ có phép ở tháng hiện tại
|
||||||
'days_used_without_pay' => $usedDaysInMonthWithoutPay, //tổng số ngày nghỉ không phép ở tháng hiện tại
|
'days_used_without_pay' => $usedDaysInMonthWithoutPay, //tổng số ngày nghỉ không phép ở tháng hiện tại
|
||||||
'days_requested' => $monthData['days_requested'], //số ngày yêu cầu nghỉ của tháng
|
'days_requested' => $monthData['days_requested'], //số ngày yêu cầu nghỉ của tháng
|
||||||
|
'status' => 'ok', // mặc định là ok
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Xử lý các trường hợp thiếu ngày phép
|
||||||
if ($remainingDaysInMonth <= 0) { //hết phép
|
if ($remainingDaysInMonth <= 0) { //hết phép
|
||||||
return [
|
$hasInsufficientDays = true;
|
||||||
'success' => false,
|
$month_data['status'] = 'no_days_left';
|
||||||
'message' => "Hiện tại bạn đã hết phép nghỉ trong tháng {$monthData['month']}/{$monthData['year']}\nBạn có chấp nhận nộp: " . $monthData['days_requested'] . " ngày không phép không?",
|
$monthMessage = "Hiện tại bạn đã hết phép nghỉ trong tháng {$monthData['month']}/{$monthData['year']}\nBạn sẽ nộp: " . $monthData['days_requested'] . " ngày không phép.";
|
||||||
'warning_type' => 'exceed_monthly_limit',
|
$errorMessage .= $errorMessage ? "\n\n" . $monthMessage : $monthMessage;
|
||||||
'month_data' => $month_data
|
} else if ($remainingDaysInMonth < $monthData['days_requested']) { // không đủ ngày phép
|
||||||
];
|
$hasInsufficientDays = true;
|
||||||
} else {
|
$month_data['status'] = 'insufficient_days';
|
||||||
// Còn phép
|
$daysNotEnough = $monthData['days_requested'] - $remainingDaysInMonth;
|
||||||
if ($remainingDaysInMonth >= $monthData['days_requested']) {
|
$monthMessage = "Tháng {$monthData['month']}/{$monthData['year']}: Số ngày phép còn lại: {$remainingDaysInMonth}, Số ngày yêu cầu: {$monthData['days_requested']}.\nBạn sẽ sử dụng {$remainingDaysInMonth} ngày phép và {$daysNotEnough} ngày không phép.";
|
||||||
return [
|
$errorMessage .= $errorMessage ? "\n\n" . $monthMessage : $monthMessage;
|
||||||
'success' => true,
|
|
||||||
'message' => "Đủ ngày phép",
|
|
||||||
'warning_type' => 'exceed_monthly_limit',
|
|
||||||
'month_data' => $month_data
|
|
||||||
];
|
|
||||||
} else {
|
|
||||||
return $this->exceedMonthlyLimitResponse($remainingDaysInMonth, $monthData, $usedDaysInMonth, $usedDaysInMonthWithoutPay, $month_data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Thêm thông tin tháng vào mảng kết quả
|
||||||
|
$monthsInfo[] = $month_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trả về kết quả tổng hợp
|
||||||
|
if ($hasInsufficientDays) {
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'message' => $errorMessage . "\n\nBạn có chấp nhận không?",
|
||||||
|
'warning_type' => 'exceed_monthly_limit',
|
||||||
|
'months_info' => $monthsInfo
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'success' => false,
|
'success' => true,
|
||||||
'message' => "Không tìm được mảng danh sách ngày nghỉ",
|
'message' => "Đủ ngày phép cho tất cả tháng yêu cầu.",
|
||||||
'warning_type' => 'exceed_monthly_limit',
|
|
||||||
'months_info' => $monthsInfo
|
'months_info' => $monthsInfo
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
//Trả về thông báo vượt quá giới hạn ngày nghỉ có phép trong tháng
|
|
||||||
private function exceedMonthlyLimitResponse(float $remainingDaysInMonth, array $monthData, float $usedDaysInMonth, float $usedDaysInMonthWithoutPay, array $month_data): array
|
|
||||||
{
|
|
||||||
$daysNotEnough = $monthData['days_requested'] - $remainingDaysInMonth;
|
|
||||||
$message = "Số ngày phép còn lại: {$remainingDaysInMonth}, Số ngày yêu cầu: {$monthData['days_requested']}.\n\nBạn có chấp nhận nộp: {$remainingDaysInMonth} ngày phép và {$daysNotEnough} ngày không phép không?";
|
|
||||||
return [
|
|
||||||
'success' => false,
|
|
||||||
'message' => "Bạn không đủ ngày phép.\n\nTrong tháng {$monthData['month']}/{$monthData['year']}:\n\nBạn đã nghỉ: {$usedDaysInMonth} ngày phép, {$usedDaysInMonthWithoutPay} ngày không phép. \n\n{$message}",
|
|
||||||
'warning_type' => 'exceed_monthly_limit',
|
|
||||||
'month_data' => $month_data
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
//Tính tổng số ngày nghỉ có phép đến tháng hiện tại
|
//Tính tổng số ngày nghỉ có phép đến tháng hiện tại
|
||||||
private function getTotalLeaveDaysInMonthToMonth($user, int $year, int $month): float
|
private function getTotalLeaveDaysInMonthToMonth($user, int $year, int $month): float
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue