ranjith-dev #633
@@ -284,6 +284,40 @@ class Scheduler extends Command
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$vehicleReports = AlertMailRule::where('module', 'VehicleReport')
|
||||||
|
->where('rule_name', 'VehicleReportMail')
|
||||||
|
->select('plant', 'schedule_type')
|
||||||
|
->distinct()
|
||||||
|
->get();
|
||||||
|
|
||||||
|
foreach ($vehicleReports as $rule) {
|
||||||
|
switch ($rule->schedule_type) {
|
||||||
|
case 'Live':
|
||||||
|
// Run every minute
|
||||||
|
\Artisan::call('send:vehicle-report', [
|
||||||
|
'schedule_type' => $rule->schedule_type,
|
||||||
|
'plant' => $rule->plant,
|
||||||
|
]);
|
||||||
|
break;
|
||||||
|
case 'Hourly':
|
||||||
|
if (now()->minute == 0) {
|
||||||
|
\Artisan::call('send:vehicle-report', [
|
||||||
|
'schedule_type' => $rule->schedule_type,
|
||||||
|
'plant' => $rule->plant,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'Daily':
|
||||||
|
if (now()->format('H:i') == '07:59') {
|
||||||
|
\Artisan::call('send:vehicle-report', [
|
||||||
|
'schedule_type' => $rule->schedule_type,
|
||||||
|
'plant' => $rule->plant,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
132
app/Console/Commands/SendVehicleReport.php
Normal file
132
app/Console/Commands/SendVehicleReport.php
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Mail\VehicleEntryMail;
|
||||||
|
use App\Models\AlertMailRule;
|
||||||
|
use App\Models\Plant;
|
||||||
|
use App\Models\VehicleEntry;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Mail;
|
||||||
|
|
||||||
|
class SendVehicleReport extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'send:vehicle-report {schedule_type} {plant}';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Command description';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
|
||||||
|
$scheduleType = $this->argument('schedule_type');
|
||||||
|
$plantId = (int) $this->argument('plant');
|
||||||
|
|
||||||
|
$mailRules = AlertMailRule::where('module', 'VehicleReport')
|
||||||
|
->where('rule_name', 'VehicleReportMail')
|
||||||
|
->where('schedule_type', $scheduleType)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$plants = ($plantId == 0)
|
||||||
|
? Plant::all()
|
||||||
|
: Plant::where('id', $plantId)->get();
|
||||||
|
|
||||||
|
if ($plants->isEmpty()) {
|
||||||
|
$this->error('No valid plant(s) found.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strtolower($scheduleType) == 'daily') {
|
||||||
|
$startDate = now()->subDay()->setTime(8, 0, 0);
|
||||||
|
$endDate = now()->setTime(8, 0, 0);
|
||||||
|
} else {
|
||||||
|
$startDate = now()->setTime(8, 0, 0);
|
||||||
|
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($plants as $plant) {
|
||||||
|
|
||||||
|
$vehicleEntries = VehicleEntry::select([
|
||||||
|
'vehicle_number',
|
||||||
|
'entry_time',
|
||||||
|
'exit_time',
|
||||||
|
'duration',
|
||||||
|
'type',
|
||||||
|
])
|
||||||
|
->where('plant_id', $plant->id)
|
||||||
|
->whereBetween('created_at', [$startDate, $endDate])
|
||||||
|
->get();
|
||||||
|
|
||||||
|
if ($vehicleEntries->isEmpty()) {
|
||||||
|
$this->warn("No records for {$plant->name}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tableData = [];
|
||||||
|
$no = 1;
|
||||||
|
|
||||||
|
foreach ($vehicleEntries as $vehicleEntry) {
|
||||||
|
$tableData[] = [
|
||||||
|
'no' => $no++,
|
||||||
|
'plant' => $plant->name,
|
||||||
|
'vehicleNumber' => $vehicleEntry->vehicle_number,
|
||||||
|
'entryTime' => $vehicleEntry->entry_time,
|
||||||
|
'exitTime' => $vehicleEntry->exit_time,
|
||||||
|
'duration' => $vehicleEntry->duration,
|
||||||
|
'type' => $vehicleEntry->type,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($plantId == 0) {
|
||||||
|
$rule = $mailRules->first();
|
||||||
|
} else {
|
||||||
|
$rule = $mailRules->where('plant', $plant->id)->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$rule) {
|
||||||
|
$this->warn("No mail rule for {$plant->name}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$toEmails = collect(explode(',', $rule->email))
|
||||||
|
->map(fn($e) => trim($e))
|
||||||
|
->filter()
|
||||||
|
->unique()
|
||||||
|
->values()
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
$ccEmails = collect(explode(',', $rule->cc_emails))
|
||||||
|
->map(fn($e) => trim($e))
|
||||||
|
->filter()
|
||||||
|
->unique()
|
||||||
|
->values()
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
$mail = new VehicleEntryMail(
|
||||||
|
$scheduleType,
|
||||||
|
$tableData,
|
||||||
|
'Daily Vehicle Transit Report'
|
||||||
|
);
|
||||||
|
|
||||||
|
Mail::to($toEmails)->cc($ccEmails)->send($mail);
|
||||||
|
|
||||||
|
$this->info("Mail sent for {$plant->name}");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
90
app/Mail/VehicleEntryMail.php
Normal file
90
app/Mail/VehicleEntryMail.php
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
class VehicleEntryMail extends Mailable
|
||||||
|
{
|
||||||
|
use Queueable, SerializesModels;
|
||||||
|
|
||||||
|
public $scheduleType;
|
||||||
|
|
||||||
|
public $tableData;
|
||||||
|
|
||||||
|
public $mailSubject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new message instance.
|
||||||
|
*/
|
||||||
|
public function __construct($scheduleType, $tableData, $mailSubject)
|
||||||
|
{
|
||||||
|
$this->scheduleType = $scheduleType;
|
||||||
|
$this->tableData = $tableData ?? [];
|
||||||
|
$this->mailSubject = $mailSubject ?? 'Vehicle Entry Report';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the message envelope.
|
||||||
|
*/
|
||||||
|
public function envelope(): Envelope
|
||||||
|
{
|
||||||
|
return new Envelope(
|
||||||
|
subject: 'Vehicle Entry Mail',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the message content definition.
|
||||||
|
*/
|
||||||
|
public function content(): Content
|
||||||
|
{
|
||||||
|
$greeting = '<b>Dear Sir</b>';
|
||||||
|
|
||||||
|
//$greeting1 = 'Dear C.R.I Branch Team, <br><br> Please follow and ensure the same';
|
||||||
|
|
||||||
|
if ($this->scheduleType == 'Daily') {
|
||||||
|
$reportPeriod = "";
|
||||||
|
$greeting .= $reportPeriod;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->scheduleType == 'Hourly') {
|
||||||
|
$now = now();
|
||||||
|
$fromHour = $now->copy()->subHour()->format('H:i:s');
|
||||||
|
$toHour = $now->format('H:i:s');
|
||||||
|
$reportDate = $now->format('d/m/Y');
|
||||||
|
$greeting .= "from: $reportDate, $fromHour to $toHour. <br><br>Please arrange to receipt the same immediately.";
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->scheduleType == 'Live') {
|
||||||
|
$now = now();
|
||||||
|
$fromMinute = $now->copy()->subMinute()->format('d/m/Y H:i:s');
|
||||||
|
$toMinute = $now->format('d/m/Y H:i:s');
|
||||||
|
$greeting .= "from: $fromMinute to $toMinute. <br><br>Please arrange to receipt the same immediately.";
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Content(
|
||||||
|
view: 'mail.vehicle_entry_report',
|
||||||
|
with: [
|
||||||
|
'company' => 'CRI Digital Manufacturing Solutions',
|
||||||
|
'greeting' => $greeting,
|
||||||
|
'tableData' => $this->tableData,
|
||||||
|
'wishes' => 'Thanks & Regards,<br>CRI Digital Manufacturing Solutions',
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get the attachments for the message.
|
||||||
|
*
|
||||||
|
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
|
||||||
|
*/
|
||||||
|
public function attachments(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
92
resources/views/mail/vehicle_entry_report.blade.php
Normal file
92
resources/views/mail/vehicle_entry_report.blade.php
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Vehicle Entry Report</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: 'Segoe UI', Arial, sans-serif;
|
||||||
|
background-color: #f9fafb;
|
||||||
|
color: #333;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
h2 {
|
||||||
|
color: #215c98;
|
||||||
|
text-align: center;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
table {
|
||||||
|
border-collapse: collapse;
|
||||||
|
width: 100%;
|
||||||
|
font-size: 14px;
|
||||||
|
white-space: nowrap !important;
|
||||||
|
}
|
||||||
|
th, td {
|
||||||
|
border: 1px solid #020813da;
|
||||||
|
padding: 8px 10px;
|
||||||
|
text-align: center;
|
||||||
|
white-space: nowrap !important;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
}
|
||||||
|
.td-left {
|
||||||
|
text-align: left !important;
|
||||||
|
}
|
||||||
|
th {
|
||||||
|
background-color: #215c98;
|
||||||
|
color: #fff;
|
||||||
|
white-space: nowrap !important;
|
||||||
|
}
|
||||||
|
tr:nth-child(even) {
|
||||||
|
background-color: #f3f4f6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.footer {
|
||||||
|
text-align: center;
|
||||||
|
font-size: 13px;
|
||||||
|
color: #6b7280;
|
||||||
|
margin-top: 30px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div style="text-align: center; font-weight: bold;">
|
||||||
|
{{ $company }}
|
||||||
|
</div>
|
||||||
|
<br>
|
||||||
|
<p>{!! $greeting !!}</p>
|
||||||
|
<div class="container">
|
||||||
|
@if(empty($tableData))
|
||||||
|
<p style="text-align:center;">No vehicle entry data available.</p>
|
||||||
|
@else
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>No</th>
|
||||||
|
<th>Plant Name</th>
|
||||||
|
<th>Vehicle Number</th>
|
||||||
|
<th>Entry Time</th>
|
||||||
|
<th>Exit Time</th>
|
||||||
|
<th>Duration</th>
|
||||||
|
<th>Type</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@foreach ($tableData as $row)
|
||||||
|
<tr>
|
||||||
|
<td>{{ $loop->iteration }}</td>
|
||||||
|
<td>{{ $row['plant'] }}</td>
|
||||||
|
<td>{{ $row['vehicleNumber'] }}</td>
|
||||||
|
<td>{{ $row['entryTime'] }}</td>
|
||||||
|
<td>{{ $row['exitTime'] }}</td>
|
||||||
|
<td>{{ $row['duration'] }}</td>
|
||||||
|
<td>{{ $row['type'] }}</td>
|
||||||
|
</tr>
|
||||||
|
@endforeach
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
@endif
|
||||||
|
<p>{!! $wishes !!}</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user