Added laser stop approval mail command page
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 17s

This commit is contained in:
dhanabalan
2026-09-19 09:21:58 +05:30
parent 02c7aa2cd8
commit 6679974252

View File

@@ -2,12 +2,18 @@
namespace App\Console\Commands;
use App\Mail\LaserStopMail;
use App\Mail\LaserStopReportMail;
use App\Models\CharacteristicApproverMaster;
use App\Models\ClassCharacteristic;
use App\Models\Machine;
use App\Models\Plant;
use App\Models\RequestCharacteristic;
use App\Models\TempStopCharacteristic;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Carbon;
class SendLaserStopReport extends Command
{
@@ -16,7 +22,7 @@ class SendLaserStopReport extends Command
*
* @var string
*/
protected $signature = 'send:laser-stop-report {schedule_type} {plant} {machine_id}';
protected $signature = 'approval:laser-stop';
/**
* The console command description.
@@ -25,89 +31,194 @@ class SendLaserStopReport extends Command
*/
protected $description = 'Command description';
public $subjectLine;
// public $tempCharacteristics = [];
public $wfId;
/**
* Execute the console command.
*/
public function handle()
{
$scheduleType = $this->argument('schedule_type');
$plantId = (int) $this->argument('plant');
$machineId = (int) $this->argument('machine_id');
$this->info('Approval mail job started');
$mailRules = \App\Models\AlertMailRule::where('module', 'LaserStopAlert')
->where('rule_name', 'LaserStopAlertMail')
->where('schedule_type', $scheduleType)
->where('plant', $plantId)
->where('machine_id', $machineId)
// .. Laser Stop Mail trigger logic
$stoppedRecords = RequestCharacteristic::where(function ($q) {
$q->whereNull('approver_status1')
->orWhere('approver_status1', 'Hold');
})
->where(function ($q) {
$q->whereNull('approver_status2')
->orWhere('approver_status2', 'Hold');
})
->where(function ($q) {
$q->whereNull('approver_status3')
->orWhere('approver_status3', 'Hold');
})
->get();
$emails = $mailRules
->pluck('email')
->filter()
->flatMap(function ($email) {
return array_map('trim', explode(',', $email));
})
->unique()
->values()
->toArray();
$stoppedRecords = $stoppedRecords->filter(function ($item) {
$approver = CharacteristicApproverMaster::find($item->characteristic_approver_master_id);
return $approver && $approver->approver_type == 'Stop';
});
$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.');
if ($stoppedRecords->isEmpty()) {
$this->info('No laser stop pending approvals');
return;
}
$plant = Plant::find($plantId);
$grouped = $stoppedRecords->groupBy(function ($item) {
$this->wfId = $item->work_flow_id;
return $item->plant_id . '|' . $item->machine_id . '|' . $item->aufnr . '|' . $item->work_flow_id;
});
$plantName = $plant?->name ?? $plantId;
$pendingApprovers = RequestCharacteristic::where('work_flow_id', $this->wfId)->latest()->first();
$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;
$approverNameFromMaster = null;
if ($pendingApprovers && $pendingApprovers->characteristic_approver_master_id) {
$approverNameFromMaster = CharacteristicApproverMaster::find($pendingApprovers->characteristic_approver_master_id);
}
$recordIds = $records->pluck('id');
$rows = [];
// Send mail to mapped email IDs
Mail::to($emails)
->cc($ccEmails)
->send(
new LaserStopReportMail(
$records,
$plantId,
$machineId,
$plantName,
$machineName,
foreach ($grouped as $groupRecords) {
$first = $groupRecords->first();
$approver = CharacteristicApproverMaster::where('plant_id', $first->plant_id)
->where('machine_id', $first->machine_id)
->where('id', $first->characteristic_approver_master_id)
->first();
if (!$approver) {
continue;
}
$columns = Schema::getColumnListing('temp_stop_characteristics');
$exclude = ['id', 'plant_id', 'machine_id', 'item_id', 'aufnr', 'class', 'arbid', 'gamng', 'lmnga', 'zz1_cn_bill_ord', 'created_at', 'updated_at', 'deleted_at', 'has_work_flow_id', 'created_by', 'updated_by' ];
$filteredColumns = array_diff($columns, $exclude);
$row1 = TempStopCharacteristic::where('plant_id', $first->plant_id)
->where('machine_id', $first->machine_id)
->where('aufnr', $first->aufnr)
->where('model_type', $first->model_type)
->where('gernr', $first->gernr)
->latest()
->first();
$stopDetails = [
'samlight_logged_name' => $row1?->samlight_logged_name,
'zmm_heading' => $row1?->zmm_heading,
'machine_name' => $row1?->machine_name,
'stopped_at' => $row1?->stopped_at,
'stopped_by' => $row1?->stopped_by,
];
$data = [];
if ($row1) {
foreach ($filteredColumns as $column) {
$value = $row1->getAttribute($column);
if ($value != null && $value != '') {
$data[$column] = $value;
}
}
}
$characteristics = $data;
$level = null;
$mail = null;
$name = null;
$updateData = [];
$now = Carbon::now();
// --- FIRST MAIL ---
if (is_null($first->mail_status)){
$level = 1;
$mail = $approver->mail1;
$name = $approver->name1;
$updateData['mail_status'] = 'Sent';
if ($approver->duration1 > 0)
{
$duration = number_format((float)$approver->duration1, 2, '.', '');
[$hours, $minutes] = explode('.', $duration);
$totalMinutes = ((int)$hours * 60) + (int)$minutes;
$updateData['trigger_at'] = $now
->copy()
->addMinutes($totalMinutes)
->startOfMinute();
}
else
{
$updateData['trigger_at'] = null;
}
}
if (!$level || !$mail) {
continue;
}
$subjectLine = 'Laser Stop Approval Mail';
$emails = array_map('trim', explode(',', $mail));
Mail::to($emails)->send(
new LaserStopMail(
$first,
$name,
$level,
$pendingApprovers,
$approverNameFromMaster,
$subjectLine,
$characteristics,
$stopDetails
)
);
ClassCharacteristic::whereIn('id', $recordIds)
->update([
'is_stopped' => 1,
]);
RequestCharacteristic::whereIn('id', $groupRecords->pluck('id'))
->update($updateData);
$this->info('Laser stop report mail sent successfully.');
$rows[] = [
$first->id,
$first->plant_id,
$first->machine_id,
"Level $level",
$mail,
'SENT'
];
}
$this->table(
['ID', 'Plant', 'Machine', 'Level', 'Mail', 'Status'],
$rows
);
$this->info('Approval mail job completed');
}
public function convertToMinutes($duration)
{
if (!$duration) return 0;
$parts = explode('.', (string)$duration);
$hours = (int)($parts[0] ?? 0);
$minutes = (int)($parts[1] ?? 0);
return ($hours * 60) + $minutes;
}
}