Added laser stop approval mail command page
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 17s
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 17s
This commit is contained in:
@@ -2,12 +2,18 @@
|
|||||||
|
|
||||||
namespace App\Console\Commands;
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Mail\LaserStopMail;
|
||||||
use App\Mail\LaserStopReportMail;
|
use App\Mail\LaserStopReportMail;
|
||||||
|
use App\Models\CharacteristicApproverMaster;
|
||||||
use App\Models\ClassCharacteristic;
|
use App\Models\ClassCharacteristic;
|
||||||
use App\Models\Machine;
|
use App\Models\Machine;
|
||||||
use App\Models\Plant;
|
use App\Models\Plant;
|
||||||
|
use App\Models\RequestCharacteristic;
|
||||||
|
use App\Models\TempStopCharacteristic;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Support\Facades\Mail;
|
use Illuminate\Support\Facades\Mail;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
class SendLaserStopReport extends Command
|
class SendLaserStopReport extends Command
|
||||||
{
|
{
|
||||||
@@ -16,7 +22,7 @@ class SendLaserStopReport extends Command
|
|||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $signature = 'send:laser-stop-report {schedule_type} {plant} {machine_id}';
|
protected $signature = 'approval:laser-stop';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The console command description.
|
* The console command description.
|
||||||
@@ -25,89 +31,194 @@ class SendLaserStopReport extends Command
|
|||||||
*/
|
*/
|
||||||
protected $description = 'Command description';
|
protected $description = 'Command description';
|
||||||
|
|
||||||
|
public $subjectLine;
|
||||||
|
|
||||||
|
// public $tempCharacteristics = [];
|
||||||
|
|
||||||
|
public $wfId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the console command.
|
* Execute the console command.
|
||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
$scheduleType = $this->argument('schedule_type');
|
$this->info('Approval mail job started');
|
||||||
$plantId = (int) $this->argument('plant');
|
|
||||||
$machineId = (int) $this->argument('machine_id');
|
|
||||||
|
|
||||||
$mailRules = \App\Models\AlertMailRule::where('module', 'LaserStopAlert')
|
// .. Laser Stop Mail trigger logic
|
||||||
->where('rule_name', 'LaserStopAlertMail')
|
|
||||||
->where('schedule_type', $scheduleType)
|
$stoppedRecords = RequestCharacteristic::where(function ($q) {
|
||||||
->where('plant', $plantId)
|
$q->whereNull('approver_status1')
|
||||||
->where('machine_id', $machineId)
|
->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();
|
->get();
|
||||||
|
|
||||||
$emails = $mailRules
|
$stoppedRecords = $stoppedRecords->filter(function ($item) {
|
||||||
->pluck('email')
|
$approver = CharacteristicApproverMaster::find($item->characteristic_approver_master_id);
|
||||||
->filter()
|
return $approver && $approver->approver_type == 'Stop';
|
||||||
->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
|
if ($stoppedRecords->isEmpty()) {
|
||||||
? Plant::all()
|
$this->info('No laser stop pending approvals');
|
||||||
: Plant::where('id', $plantId)->get();
|
|
||||||
|
|
||||||
if ($plants->isEmpty()) {
|
|
||||||
$this->error('No valid plant(s) found.');
|
|
||||||
return;
|
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);
|
$approverNameFromMaster = null;
|
||||||
|
if ($pendingApprovers && $pendingApprovers->characteristic_approver_master_id) {
|
||||||
$machineName = $machine?->work_center ?? $machineId;
|
$approverNameFromMaster = CharacteristicApproverMaster::find($pendingApprovers->characteristic_approver_master_id);
|
||||||
|
|
||||||
$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');
|
$rows = [];
|
||||||
|
|
||||||
// Send mail to mapped email IDs
|
foreach ($grouped as $groupRecords) {
|
||||||
Mail::to($emails)
|
|
||||||
->cc($ccEmails)
|
$first = $groupRecords->first();
|
||||||
->send(
|
|
||||||
new LaserStopReportMail(
|
$approver = CharacteristicApproverMaster::where('plant_id', $first->plant_id)
|
||||||
$records,
|
->where('machine_id', $first->machine_id)
|
||||||
$plantId,
|
->where('id', $first->characteristic_approver_master_id)
|
||||||
$machineId,
|
->first();
|
||||||
$plantName,
|
|
||||||
$machineName,
|
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)
|
RequestCharacteristic::whereIn('id', $groupRecords->pluck('id'))
|
||||||
->update([
|
->update($updateData);
|
||||||
'is_stopped' => 1,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user