Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
118 lines
3.2 KiB
PHP
118 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Mail\PanelBoxReportMail;
|
|
use App\Models\Machine;
|
|
use App\Models\Plant;
|
|
use App\Models\ProductionCharacteristic;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class SendPanelBoxReport extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'send:panel-box-report {schedule_type} {plant} {machine_id}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Command description';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$scheduleType = $this->argument('schedule_type');
|
|
$plantId = (int) $this->argument('plant');
|
|
$machineId = (int) $this->argument('machine_id');
|
|
|
|
$mailRules = \App\Models\AlertMailRule::where('module', 'PanelBoxReport')
|
|
->where('rule_name', 'PanelBoxNotOkReportMail')
|
|
->where('schedule_type', $scheduleType)
|
|
->where('plant', $plantId)
|
|
->where('machine_id', $machineId)
|
|
->get();
|
|
|
|
$emails = $mailRules
|
|
->pluck('email')
|
|
->filter()
|
|
->flatMap(function ($email) {
|
|
return array_map('trim', explode(',', $email));
|
|
})
|
|
->unique()
|
|
->values()
|
|
->toArray();
|
|
|
|
$ccEmails = $mailRules
|
|
->pluck('cc_emails')
|
|
->filter()
|
|
->flatMap(function ($email) {
|
|
return array_map('trim', explode(',', $email));
|
|
})
|
|
->unique()
|
|
->values()
|
|
->toArray();
|
|
|
|
$plants = $plantId == 0
|
|
? Plant::all()
|
|
: Plant::where('id', $plantId)->get();
|
|
|
|
if ($plants->isEmpty()) {
|
|
$this->error('No valid plant(s) found.');
|
|
return;
|
|
}
|
|
|
|
$plant = Plant::find($plantId);
|
|
|
|
$plantName = $plant?->name ?? $plantId;
|
|
|
|
$machine = Machine::find($machineId);
|
|
|
|
$machineName = $machine?->work_center ?? $machineId;
|
|
|
|
if (strtolower($scheduleType) == 'daily') {
|
|
$startDate = now()->subDay()->setTime(8, 0, 0);
|
|
$endDate = now()->setTime(8, 0, 0);
|
|
} else {
|
|
$startDate = now()->setTime(8, 0, 0);
|
|
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
|
|
}
|
|
|
|
$records = ProductionCharacteristic::where('plant_id', $plantId)
|
|
->where('machine_id', $machineId)
|
|
->whereBetween('created_at', [$startDate, $endDate])
|
|
->where('status', 'NotOk')
|
|
->select('serial_number')
|
|
->distinct()
|
|
->get();
|
|
|
|
if ($records->isEmpty()) {
|
|
$this->info('No panel box records found.');
|
|
return;
|
|
}
|
|
|
|
// Send mail to mapped email IDs
|
|
Mail::to($emails)
|
|
->cc($ccEmails)
|
|
->send(
|
|
new PanelBoxReportMail(
|
|
$records,
|
|
$plantId,
|
|
$machineId,
|
|
$plantName,
|
|
$machineName,
|
|
)
|
|
);
|
|
|
|
$this->info('Panel box report mail sent successfully.');
|
|
}
|
|
}
|