Added mail logic for immediate laser stopped alert
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 17s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 14s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 22s
Laravel Pint / pint (pull_request) Successful in 3m2s
Laravel Larastan / larastan (pull_request) Failing after 7m7s
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 17s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 14s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 22s
Laravel Pint / pint (pull_request) Successful in 3m2s
Laravel Larastan / larastan (pull_request) Failing after 7m7s
This commit is contained in:
113
app/Console/Commands/SendLaserStopReport.php
Normal file
113
app/Console/Commands/SendLaserStopReport.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Mail\LaserStopReportMail;
|
||||
use App\Models\ClassCharacteristic;
|
||||
use App\Models\Machine;
|
||||
use App\Models\Plant;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class SendLaserStopReport extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'send:laser-stop-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', 'LaserStopAlert')
|
||||
->where('rule_name', 'LaserStopAlertMail')
|
||||
->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;
|
||||
|
||||
$records = ClassCharacteristic::where('is_stopped', 2)
|
||||
->where('plant_id', $plantId)
|
||||
->where('machine_id', $machineId)
|
||||
->get();
|
||||
|
||||
if ($records->isEmpty()) {
|
||||
$this->info('No laser stop records found.');
|
||||
return;
|
||||
}
|
||||
|
||||
$recordIds = $records->pluck('id');
|
||||
|
||||
// Send mail to mapped email IDs
|
||||
Mail::to($emails)
|
||||
->cc($ccEmails)
|
||||
->send(
|
||||
new LaserStopReportMail(
|
||||
$records,
|
||||
$plantId,
|
||||
$machineId,
|
||||
$plantName,
|
||||
$machineName,
|
||||
)
|
||||
);
|
||||
|
||||
ClassCharacteristic::whereIn('id', $recordIds)
|
||||
->update([
|
||||
'is_stopped' => 1,
|
||||
]);
|
||||
|
||||
$this->info('Laser stop report mail sent successfully.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user