Added trigger approval mail pages #261
204
app/Console/Commands/TriggerPendingApprovalMails.php
Normal file
204
app/Console/Commands/TriggerPendingApprovalMails.php
Normal file
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Mail\CharacteristicApprovalMail;
|
||||
use App\Models\CharacteristicApproverMaster;
|
||||
use App\Models\RequestCharacteristic;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class TriggerPendingApprovalMails extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
// protected $signature = 'app:trigger-pending-approval-mails';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
// protected $description = 'Command description';
|
||||
|
||||
// /**
|
||||
// * Execute the console command.
|
||||
// */
|
||||
// public function handle()
|
||||
// {
|
||||
// //
|
||||
// }
|
||||
|
||||
protected $signature = 'approval:trigger-mails';
|
||||
protected $description = 'Trigger approval mail manually';
|
||||
|
||||
public $pdfPath;
|
||||
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$this->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');
|
||||
}
|
||||
}
|
||||
115
app/Mail/CharacteristicApprovalMail.php
Normal file
115
app/Mail/CharacteristicApprovalMail.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Illuminate\Mail\Mailables\Attachment;
|
||||
use Storage;
|
||||
|
||||
class CharacteristicApprovalMail extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public $request;
|
||||
public $approverName;
|
||||
public $level;
|
||||
|
||||
public $pdfPath;
|
||||
public $tableData;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*/
|
||||
public function __construct($request, $approverName, $level, $pdfPath = null, $characteristics = [])
|
||||
{
|
||||
$this->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,<br>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<int, \Illuminate\Mail\Mailables\Attachment>
|
||||
*/
|
||||
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)),
|
||||
];
|
||||
}
|
||||
}
|
||||
88
resources/views/mail/characteristic-approval.blade.php
Normal file
88
resources/views/mail/characteristic-approval.blade.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
{{-- <title>Invoice Data Report</title> --}}
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div style="text-align: center; font-weight: bold;">
|
||||
{{ $company }}
|
||||
</div>
|
||||
<br>
|
||||
<p>{!! $greeting !!}</p>
|
||||
|
||||
<table border="1" width="50%" cellpadding="6" cellspacing="0">
|
||||
|
||||
<tr>
|
||||
<th width="30%">Title</th>
|
||||
<th width="40%">Details</th>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Requested DateTime</td>
|
||||
<td>{{ $request->created_at->format('d-m-Y H:i:s') }}</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Plant</td>
|
||||
<td>{{ $request->plant->name ?? $request->plant_id }}</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Item Code</td>
|
||||
<td>{{ $request->item->code ?? $request->code }}</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Job Number</td>
|
||||
<td>{{ $request->aufnr }}</td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
||||
<br>
|
||||
|
||||
<table border="1" width="50%" cellpadding="6" cellspacing="0">
|
||||
<tr>
|
||||
<th>Characteristics Name</th>
|
||||
<th>SAP Value</th>
|
||||
<th>Update Value</th>
|
||||
<th>WorkFlow ID</th>
|
||||
</tr>
|
||||
|
||||
@forelse ($tableData as $char)
|
||||
<tr>
|
||||
{{-- <td>{{ $char['characteristic_name'] ?? '-' }}</td> --}}
|
||||
<td>{{ strtoupper($char['characteristic_name'] ?? '-') }}</td>
|
||||
<td>{{ $char['current_value'] ?? '-' }}</td>
|
||||
<td>{{ $char['update_value'] ?? '-' }}</td>
|
||||
<td>{{ $char['work_flow_id'] ?? '-' }}</td>
|
||||
</tr>
|
||||
@empty
|
||||
<tr>
|
||||
<td colspan="4">No Characteristics Found</td>
|
||||
</tr>
|
||||
@endforelse
|
||||
</table>
|
||||
<br>
|
||||
<table border="1" width="50%" cellpadding="6" cellspacing="0">
|
||||
<tr>
|
||||
<th>Approver Name</th>
|
||||
<th>Approve Status</th>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>{{ $approverName }}</td>
|
||||
<td style="text-align: center;">
|
||||
<a href="{{ $approveUrl }}">Approve</a> |
|
||||
<a href="{{ $holdUrl }}">Hold</a> |
|
||||
<a href="{{ $rejectUrl }}">Reject</a>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>{!! $wishes !!}</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user