77 lines
2.2 KiB
PHP
77 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\Maintenance\Services;
|
|
|
|
use App\Business;
|
|
use App\User;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\Maintenance\Models\MaintenanceNotification;
|
|
use Modules\Maintenance\Models\PreventiveSchedule;
|
|
use Modules\Maintenance\Models\SparePart;
|
|
|
|
class MaintenanceNotificationService
|
|
{
|
|
public function notify(
|
|
Business $business,
|
|
string $type,
|
|
string $title,
|
|
string $message,
|
|
?User $user = null,
|
|
?array $data = null
|
|
): ?MaintenanceNotification {
|
|
if (! Schema::hasTable('maintenance_notifications')) {
|
|
return null;
|
|
}
|
|
|
|
return MaintenanceNotification::create([
|
|
'business_id' => $business->id,
|
|
'user_id' => $user?->id,
|
|
'type' => $type,
|
|
'title' => $title,
|
|
'message' => $message,
|
|
'data' => $data,
|
|
'channels' => ['in_app'],
|
|
'scheduled_at' => now(),
|
|
'sent_at' => now(),
|
|
]);
|
|
}
|
|
|
|
public function scanAndNotify(Business $business): array
|
|
{
|
|
$sent = [];
|
|
|
|
if (Schema::hasTable('maintenance_preventive_schedules')) {
|
|
$due = PreventiveSchedule::where('business_id', $business->id)
|
|
->where('is_active', true)
|
|
->where('next_due_at', '<=', now()->addDays(3))
|
|
->count();
|
|
|
|
if ($due > 0) {
|
|
$sent[] = $this->notify(
|
|
$business,
|
|
'pm_due',
|
|
'PM سررسید',
|
|
"{$due} برنامه PM در ۳ روز آینده سررسید دارد."
|
|
);
|
|
}
|
|
}
|
|
|
|
if (Schema::hasTable('maintenance_spare_parts')) {
|
|
$low = SparePart::where('business_id', $business->id)
|
|
->whereColumn('quantity', '<=', 'min_stock')
|
|
->count();
|
|
|
|
if ($low > 0) {
|
|
$sent[] = $this->notify(
|
|
$business,
|
|
'low_stock',
|
|
'موجودی کم',
|
|
"{$low} قطعه یدکی زیر حداقل موجودی است."
|
|
);
|
|
}
|
|
}
|
|
|
|
return array_filter($sent);
|
|
}
|
|
}
|