ultimatepos/Modules/ManagementTools/Entities/MtUserReportSetting.php

99 lines
2.6 KiB
PHP

<?php
namespace Modules\ManagementTools\Entities;
use App\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class MtUserReportSetting extends Model
{
public const MODE_QUARTER = 'quarter';
public const MODE_HOURLY = 'hourly';
public const MODE_DAILY = 'daily';
public const MODE_MONTHLY = 'monthly';
public const MODE_GPS = 'gps_live';
public const MODE_CUSTOM = 'custom';
protected $table = 'mt_user_report_settings';
protected $guarded = ['id'];
protected $casts = [
'interval_minutes' => 'integer',
];
public function user()
{
return $this->belongsTo(User::class);
}
public static function modes(): array
{
return [
self::MODE_QUARTER => __('managementtools::lang.report_mode_quarter'),
self::MODE_HOURLY => __('managementtools::lang.report_mode_hourly'),
self::MODE_DAILY => __('managementtools::lang.report_mode_daily'),
self::MODE_MONTHLY => __('managementtools::lang.report_mode_monthly'),
self::MODE_GPS => __('managementtools::lang.report_mode_gps_live'),
self::MODE_CUSTOM => __('managementtools::lang.report_mode_custom'),
];
}
public static function resolveForUser(int $business_id, int $user_id): self
{
return static::firstOrCreate(
['business_id' => $business_id, 'user_id' => $user_id],
['report_mode' => self::MODE_QUARTER, 'interval_minutes' => 15]
);
}
public function modeLabel(): string
{
if ($this->report_mode === self::MODE_CUSTOM) {
return __('managementtools::lang.report_mode_custom_every', ['minutes' => $this->interval_minutes]);
}
return self::modes()[$this->report_mode] ?? '—';
}
public function slotIntervalMinutes(): int
{
return match ($this->report_mode) {
self::MODE_HOURLY => 60,
self::MODE_GPS => 1,
self::MODE_CUSTOM => max(1, min(1440, (int) $this->interval_minutes)),
default => 15,
};
}
public function isSingleSlotDay(): bool
{
return $this->report_mode === self::MODE_DAILY;
}
public function isMonthlyMode(): bool
{
return $this->report_mode === self::MODE_MONTHLY;
}
public function isGpsMode(): bool
{
return $this->report_mode === self::MODE_GPS;
}
public function allowsReportingOnDay(string $day): bool
{
if ($this->isMonthlyMode()) {
return (int) Carbon::parse($day)->format('d') === 1;
}
return true;
}
}