info('Approval mail job started'); // .. 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(); $stoppedRecords = $stoppedRecords->filter(function ($item) { $approver = CharacteristicApproverMaster::find($item->characteristic_approver_master_id); return $approver && $approver->approver_type == 'Stop'; }); if ($stoppedRecords->isEmpty()) { $this->info('No laser stop pending approvals'); return; } $grouped = $stoppedRecords->groupBy(function ($item) { $this->wfId = $item->work_flow_id; return $item->plant_id . '|' . $item->machine_id . '|' . $item->aufnr . '|' . $item->work_flow_id; }); $pendingApprovers = RequestCharacteristic::where('work_flow_id', $this->wfId)->latest()->first(); $approverNameFromMaster = null; if ($pendingApprovers && $pendingApprovers->characteristic_approver_master_id) { $approverNameFromMaster = CharacteristicApproverMaster::find($pendingApprovers->characteristic_approver_master_id); } $rows = []; 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 ) ); RequestCharacteristic::whereIn('id', $groupRecords->pluck('id')) ->update($updateData); $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; } }