edit delay and penalty calculations

This commit is contained in:
2026-07-22 17:38:35 +03:30
parent e1f86e6ffe
commit e9f397d759
4 changed files with 57 additions and 12 deletions
@@ -71,7 +71,9 @@ class AttendanceController extends Controller
}
if ($user && !empty($user->attendances)) {
foreach ($user->attendances as $att) {
$date = $att->entered_at != null ? Jalalian::fromCarbon(Carbon::parse($att->entered_at)) : null;
$date = $att->entered_at != null
? \App\Support\JalaliHelper::fromDateTime($att->entered_at)
: null;
$att->date = $date != null ? $date->format('Y/m/d') : null;
if ($att->duration != null && $date != null && $now->getMonth() == $date->getMonth()) {
[$h, $m, $sec] = explode(':', $att->duration);
+6 -1
View File
@@ -127,7 +127,12 @@ class LatePenaltyService
public function jalaliMonthBounds(?Carbon $date = null): array
{
$j = Jalalian::fromCarbon($date ?? Carbon::now());
$carbon = $date ?? Carbon::now();
if ($carbon->year < 1000 || $carbon->year > 3000) {
$carbon = Carbon::now();
}
$j = Jalalian::fromCarbon($carbon);
$start = (new Jalalian($j->getYear(), $j->getMonth(), 1))->toCarbon()->startOfDay();
$endDay = $j->getMonth() <= 6 ? 31 : ($j->getMonth() <= 11 ? 30 : ($j->isLeapYear() ? 30 : 29));
$end = (new Jalalian($j->getYear(), $j->getMonth(), $endDay))->toCarbon()->endOfDay();
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace App\Support;
use Carbon\Carbon;
use Morilog\Jalali\Jalalian;
use Throwable;
class JalaliHelper
{
public static function fromDateTime($value): ?Jalalian
{
if ($value === null || $value === '' || $value === '0000-00-00' || str_starts_with((string) $value, '0000-00-00')) {
return null;
}
try {
$carbon = $value instanceof Carbon ? $value : Carbon::parse($value);
if ($carbon->year < 1000 || $carbon->year > 3000) {
return null;
}
return Jalalian::fromCarbon($carbon);
} catch (Throwable $e) {
return null;
}
}
public static function format($value, string $format = 'Y/m/d', string $fallback = '-'): string
{
$jalali = self::fromDateTime($value);
return $jalali ? $jalali->format($format) : $fallback;
}
}
+13 -10
View File
@@ -146,10 +146,13 @@ if (!empty($user->tasks)) {
// بررسی وجود داده
$attendances = $user->attendances ?? collect();
// گروه‌بندی داده‌ها بر اساس نام ماه (مثلا: مهر، آبان)
$groupedData = $attendances->groupBy(function($item) {
return \Morilog\Jalali\Jalalian::fromCarbon(\Carbon\Carbon::parse($item->entered_at))->format('%B');
});
// گروه‌بندی داده‌ها بر اساس نام ماه شمسی
$groupedData = $attendances
->filter(fn ($item) => !empty($item->entered_at))
->groupBy(function ($item) {
$month = \App\Support\JalaliHelper::format($item->entered_at, '%B', '');
return $month !== '' ? $month : 'نامعتبر';
});
@endphp
<div class="container inline-flex flex-col items-center justify-center">
@@ -188,10 +191,10 @@ if (!empty($user->tasks)) {
@php
$monthLateMinutes = 0;
$monthDailyFine = 0;
$monthKey = $records->first()?->entered_at
? \Carbon\Carbon::parse($records->first()->entered_at)
: null;
if ($monthKey) {
$monthKey = null;
$firstEntered = $records->first()?->entered_at;
if ($firstEntered && \App\Support\JalaliHelper::fromDateTime($firstEntered)) {
$monthKey = \Carbon\Carbon::parse($firstEntered);
[$mStart, $mEnd] = app(\App\Services\LatePenaltyService::class)->jalaliMonthBounds($monthKey);
$monthWeeklyFine = (int) \App\Models\LatePeriodFine::query()
->where('user_identity', $user->user_name)
@@ -220,9 +223,9 @@ if (!empty($user->tasks)) {
{{-- تاریخ --}}
<td style="border-right: 1px solid #ddd; padding: 4px; background-color: {{"#e6e6e6" }};">
{{ $item->entered_at ? \Morilog\Jalali\Jalalian::fromCarbon(\Carbon\Carbon::parse($item->entered_at))->format('m/d') : '-' }}
{{ \App\Support\JalaliHelper::format($item->entered_at, 'm/d') }}
<span class="text-xs text-gray-500 block">
{{ $item->entered_at ? \Morilog\Jalali\Jalalian::fromCarbon(\Carbon\Carbon::parse($item->entered_at))->format('%A') : '' }}
{{ \App\Support\JalaliHelper::format($item->entered_at, '%A', '') }}
</span>
</td>