ultimatepos/Modules/Maintenance/Console/ProcessPreventiveMaintenance.php

65 lines
2.1 KiB
PHP

<?php
namespace Modules\Maintenance\Console;
use App\Business;
use App\Utils\ModuleUtil;
use Illuminate\Console\Command;
use Modules\Maintenance\Services\MaintenanceNotificationService;
use Modules\Maintenance\Services\PreventiveScheduleService;
class ProcessPreventiveMaintenance extends Command
{
protected $signature = 'pos:maintenanceProcessPreventive';
protected $description = 'Send preventive maintenance reminders and auto-create work orders when due.';
public function __construct(
protected PreventiveScheduleService $scheduleService,
protected MaintenanceNotificationService $notificationService
) {
parent::__construct();
}
public function handle(): int
{
if (! \Module::has('Maintenance') || ! \Module::isEnabled('Maintenance')) {
$this->info('Maintenance module is not enabled.');
return self::SUCCESS;
}
$moduleUtil = new ModuleUtil();
if (! $moduleUtil->isModuleInstalled('Maintenance')) {
$this->info('Maintenance module is not installed.');
return self::SUCCESS;
}
$totals = ['reminders' => 0, 'work_orders' => 0];
Business::query()->select('id')->orderBy('id')->chunk(50, function ($rows) use ($moduleUtil, &$totals) {
foreach ($rows as $row) {
if ($moduleUtil->isSuperadminInstalled()
&& ! $moduleUtil->hasThePermissionInSubscription($row->id, 'maintenance_module')) {
continue;
}
$business = Business::find($row->id);
if (! $business) {
continue;
}
$result = $this->scheduleService->processBusinessSchedules($business);
$totals['reminders'] += $result['reminders'];
$totals['work_orders'] += $result['work_orders'];
$this->notificationService->scanAndNotify($business);
}
});
$this->info("Reminders sent: {$totals['reminders']}, work orders created: {$totals['work_orders']}.");
return self::SUCCESS;
}
}