From 97ce3df03825b57689bb19595fb5e1db0db6d6f6 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 28 Jan 2026 15:26:03 +0530 Subject: [PATCH] Added trigger approval mail pages --- .../Commands/TriggerPendingApprovalMails.php | 204 ++++++++++++++++++ app/Mail/CharacteristicApprovalMail.php | 115 ++++++++++ .../mail/characteristic-approval.blade.php | 88 ++++++++ 3 files changed, 407 insertions(+) create mode 100644 app/Console/Commands/TriggerPendingApprovalMails.php create mode 100644 app/Mail/CharacteristicApprovalMail.php create mode 100644 resources/views/mail/characteristic-approval.blade.php diff --git a/app/Console/Commands/TriggerPendingApprovalMails.php b/app/Console/Commands/TriggerPendingApprovalMails.php new file mode 100644 index 0000000..627135e --- /dev/null +++ b/app/Console/Commands/TriggerPendingApprovalMails.php @@ -0,0 +1,204 @@ +info('Approval mail job started'); + + $records = RequestCharacteristic::whereNull('approver_status1') + ->whereNull('approver_status2') + ->whereNull('approver_status3') + ->get(); + + if ($records->isEmpty()) { + $this->info('No pending approvals'); + return; + } + + $grouped = $records->groupBy(function ($item) { + return $item->plant_id . '|' . $item->machine_id . '|' . $item->aufnr . '|' . $item->work_flow_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; + } + + $characteristics = $groupRecords->map(fn ($r) => [ + 'work_flow_id' => $r->work_flow_id, + 'characteristic_name' => $r->characteristic_name, + 'current_value' => $r->current_value, + 'update_value' => $r->update_value, + ])->toArray(); + + $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'; + // $updateData['trigger_at'] = $approver->duration1 > 0 + // ? $now->copy()->addMinutes($approver->duration1 * 10) + // : null; + + 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; + } + } + + // --- SECOND MAIL --- + elseif ( + $first->mail_status == 'Sent' && + is_null($first->approver_status1) && + $first->trigger_at && + \Carbon\Carbon::parse($first->trigger_at)->lte($now) + // $first->trigger_at <= $now + ) { + $level = 2; + $mail = $approver->mail2; + $name = $approver->name2; + // $updateData['trigger_at'] = $approver->duration2 > 0 + // ? $now->copy()->addMinutes($approver->duration2 * 10) + // : null; + + // $updateData['mail_status'] = 'Sent-Mail2'; + + if ($approver->duration2 > 0) { + + $duration = number_format((float)$approver->duration2, 2, '.', ''); + [$hours, $minutes] = explode('.', $duration); + + $totalMinutes = ((int)$hours * 60) + (int)$minutes; + + // IMPORTANT: use NOW, not old trigger + $updateData['trigger_at'] = $now->copy()->addMinutes($totalMinutes); + } else { + $updateData['trigger_at'] = null; + } + + $updateData['mail_status'] = 'Sent-Mail2'; + } + + // --- THIRD MAIL --- + elseif ( + $first->mail_status == 'Sent-Mail2' && + is_null($first->approver_status1) && + is_null($first->approver_status2) && + $first->trigger_at && + // $first->trigger_at <= $now + \Carbon\Carbon::parse($first->trigger_at)->lte($now) + ) { + $level = 3; + $mail = $approver->mail3; + $name = $approver->name3; + + $updateData['trigger_at'] = null; + $updateData['mail_status'] = 'Sent-Mail3'; + } + + if (!$level || !$mail) { + continue; + } + + $pdfPath = 'uploads/LaserDocs/' . $first->work_flow_id . '.pdf'; + + Mail::to($mail)->send( + new CharacteristicApprovalMail( + $first, + $name, + $level, + $pdfPath, + $characteristics + ) + ); + + 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'); + } +} diff --git a/app/Mail/CharacteristicApprovalMail.php b/app/Mail/CharacteristicApprovalMail.php new file mode 100644 index 0000000..9d7127b --- /dev/null +++ b/app/Mail/CharacteristicApprovalMail.php @@ -0,0 +1,115 @@ +request = $request; + $this->approverName = $approverName; + $this->level = $level; + $this->pdfPath = $pdfPath; + $this->tableData = $characteristics; + } + + /** + * Get the message envelope. + */ + public function envelope(): Envelope + { + return new Envelope( + subject: 'Characteristic Approval Mail', + ); + } + + /** + * Get the message content definition. + */ + public function content(): Content + { + return new Content( + view: 'mail.characteristic-approval', + with: [ + 'company' => 'CRI Digital Manufacturing Solutions', + 'greeting' => 'Dear ' . $this->approverName . ',', + 'request' => $this->request, + 'tableData' => $this->tableData, + 'approveUrl' => $this->approveUrl(), + 'holdUrl' => $this->holdUrl(), + 'rejectUrl' => $this->rejectUrl(), + 'wishes' => 'Thanks & Regards,
CRI Digital Manufacturing Solutions', + ] + ); + } + + protected function approveUrl() + { + return URL::signedRoute('characteristic.approve', [ + 'id' => $this->request->id, + 'level' => $this->level + ]); + } + + protected function holdUrl() + { + return URL::signedRoute('characteristic.hold', [ + 'id' => $this->request->id, + 'level' => $this->level + ]); + } + + + protected function rejectUrl() + { + return URL::signedRoute('characteristic.reject', [ + 'id' => $this->request->id, + 'level' => $this->level + ]); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + if (! $this->pdfPath) { + return []; + } + + if (! Storage::disk('local')->exists($this->pdfPath)) { + return []; + } + + return [ + Attachment::fromStorageDisk( + 'local', + $this->pdfPath + )->as(basename($this->pdfPath)), + ]; + } +} diff --git a/resources/views/mail/characteristic-approval.blade.php b/resources/views/mail/characteristic-approval.blade.php new file mode 100644 index 0000000..cb1cb09 --- /dev/null +++ b/resources/views/mail/characteristic-approval.blade.php @@ -0,0 +1,88 @@ + + + + + {{-- Invoice Data Report --}} + + + +
+ {{ $company }} +
+
+

{!! $greeting !!}

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TitleDetails
Requested DateTime{{ $request->created_at->format('d-m-Y H:i:s') }}
Plant{{ $request->plant->name ?? $request->plant_id }}
Item Code{{ $request->item->code ?? $request->code }}
Job Number{{ $request->aufnr }}
+ +
+ + + + + + + + + + @forelse ($tableData as $char) + + {{-- --}} + + + + + + @empty + + + + @endforelse +
Characteristics NameSAP ValueUpdate ValueWorkFlow ID
{{ $char['characteristic_name'] ?? '-' }}{{ strtoupper($char['characteristic_name'] ?? '-') }}{{ $char['current_value'] ?? '-' }}{{ $char['update_value'] ?? '-' }}{{ $char['work_flow_id'] ?? '-' }}
No Characteristics Found
+
+ + + + + + + + + + +
Approver NameApprove Status
{{ $approverName }} + Approve | + Hold | + Reject +
+ +

{!! $wishes !!}

+ + +