ranjith-dev #1159
@@ -544,6 +544,42 @@ class Scheduler extends Command
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//..Laser Idle Report
|
||||
|
||||
$laserIdleRules = AlertMailRule::where('module', 'LaserIdleHourReport')
|
||||
->where('rule_name', 'LaserIdleMail')
|
||||
->select('plant', 'schedule_type')
|
||||
->distinct()
|
||||
->get();
|
||||
|
||||
foreach ($laserIdleRules as $rule) {
|
||||
switch ($rule->schedule_type) {
|
||||
case 'Live':
|
||||
// Run every minute
|
||||
\Artisan::call('send:laser-idle-hour-report', [
|
||||
'schedule_type' => $rule->schedule_type,
|
||||
'plant' => $rule->plant,
|
||||
]);
|
||||
break;
|
||||
case 'Hourly':
|
||||
if (now()->minute == 0) {
|
||||
\Artisan::call('send:laser-idle-hour-report', [
|
||||
'schedule_type' => $rule->schedule_type,
|
||||
'plant' => $rule->plant,
|
||||
]);
|
||||
}
|
||||
break;
|
||||
case 'Daily':
|
||||
if (now()->format('H:i') == '07:59') {
|
||||
\Artisan::call('send:laser-idle-hour-report', [
|
||||
'schedule_type' => $rule->schedule_type,
|
||||
'plant' => $rule->plant,
|
||||
]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
196
app/Console/Commands/SendLaserIdleHourReport.php
Normal file
196
app/Console/Commands/SendLaserIdleHourReport.php
Normal file
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Mail\LaserIdleMail;
|
||||
use App\Models\ClassCharacteristic;
|
||||
use App\Models\Plant;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class SendLaserIdleHourReport extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'send:laser-idle-hour-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 = \App\Models\AlertMailRule::where('module', 'LaserIdleHourReport')
|
||||
->where('rule_name', 'LaserIdleMail')
|
||||
->where('schedule_type', $scheduleType)
|
||||
->where('plant', $plantId)
|
||||
->get();
|
||||
|
||||
$emails = $mailRules->pluck('email')->unique()->toArray();
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
$tableData = [];
|
||||
$no = 1;
|
||||
|
||||
foreach ($plants as $plant) {
|
||||
|
||||
$machineRecords = ClassCharacteristic::where('plant_id', $plant->id)
|
||||
->whereBetween('created_at', [$startDate, $endDate])
|
||||
->get();
|
||||
|
||||
$machineNames = collect()
|
||||
->merge(
|
||||
$machineRecords
|
||||
->pluck('motor_machine_name')
|
||||
->filter()
|
||||
)
|
||||
->merge(
|
||||
$machineRecords
|
||||
->pluck('pump_machine_name')
|
||||
->filter()
|
||||
)
|
||||
->merge(
|
||||
$machineRecords
|
||||
->pluck('name_plate_machine_name')
|
||||
->filter()
|
||||
)
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
foreach ($machineNames as $machineName) {
|
||||
|
||||
$motorCount = $machineRecords
|
||||
->where('motor_machine_name', $machineName)
|
||||
->sum(function ($record) {
|
||||
return (float) ($record->motor_marked_physical_count ?? 0);
|
||||
});
|
||||
|
||||
$pumpCount = $machineRecords
|
||||
->where('pump_machine_name', $machineName)
|
||||
->sum(function ($record) {
|
||||
return (float) ($record->pump_marked_physical_count ?? 0);
|
||||
});
|
||||
|
||||
$namePlateCount = $machineRecords
|
||||
->where('name_plate_machine_name', $machineName)
|
||||
->where('name_plate_marked_status', 'Marked')
|
||||
->count();
|
||||
|
||||
$manualCount = $machineRecords
|
||||
->filter(function ($record) use ($machineName) {
|
||||
return $record->motor_machine_name == $machineName
|
||||
|| $record->pump_machine_name == $machineName
|
||||
|| $record->name_plate_machine_name == $machineName;
|
||||
})
|
||||
->sum(function ($record) {
|
||||
return (float) ($record->man_marked_status ?? 0);
|
||||
});
|
||||
|
||||
$totalExpectedMinutes =
|
||||
$machineRecords
|
||||
->where('motor_machine_name', $machineName)
|
||||
->sum(fn ($record) => (float) ($record->motor_expected_time ?? 0))
|
||||
|
||||
+ $machineRecords
|
||||
->where('pump_machine_name', $machineName)
|
||||
->sum(fn ($record) => (float) ($record->pump_expected_time ?? 0))
|
||||
|
||||
+ $machineRecords
|
||||
->where('name_plate_machine_name', $machineName)
|
||||
->sum(fn ($record) => (float) ($record->name_plate_expected_time ?? 0))
|
||||
|
||||
+ $machineRecords
|
||||
->where('motor_machine_name', $machineName)
|
||||
->sum(fn ($record) => (float) ($record->man_mark_expected_time ?? 0));
|
||||
|
||||
$totalLaserOnHoursValue = $totalExpectedMinutes / 60;
|
||||
|
||||
$totalHours = intdiv((int) $totalExpectedMinutes, 60);
|
||||
$totalMinutes = (int) $totalExpectedMinutes % 60;
|
||||
|
||||
$totalLaserOnHours = "{$totalHours} hr {$totalMinutes} min";
|
||||
|
||||
$totalLaserOffHoursValue = 22.5 - $totalLaserOnHoursValue;
|
||||
|
||||
$offHours = floor($totalLaserOffHoursValue);
|
||||
$offMinutes = round(($totalLaserOffHoursValue - $offHours) * 60);
|
||||
|
||||
$totalLaserOffHours = "{$offHours} hr {$offMinutes} min";
|
||||
|
||||
$tableData[] = [
|
||||
'no' => $no++,
|
||||
'plant' => $plant->name,
|
||||
'machine' => $machineName,
|
||||
|
||||
'motorCount' => $motorCount,
|
||||
'pumpCount' => $pumpCount,
|
||||
'namePlateCount' => $namePlateCount,
|
||||
|
||||
'manualCount' => $manualCount,
|
||||
'totalLaserOnHours' => $totalLaserOnHours,
|
||||
'totalLaserOffHours' => $totalLaserOffHours,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$mail = new LaserIdleMail($scheduleType, $tableData);
|
||||
|
||||
// $contentVars = $mail->content()->with;
|
||||
|
||||
foreach ($mailRules as $rule)
|
||||
{
|
||||
$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();
|
||||
|
||||
if (empty($toEmails)) {
|
||||
$this->warn("Skipping rule ID {$rule->id} — no valid To emails found.");
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
\Mail::to($toEmails)->cc($ccEmails)->send($mail);
|
||||
|
||||
$this->info("Mail sent for rule ID {$rule->id} → To: ".implode(', ', $toEmails).($ccEmails ? ' | CC: '.implode(', ', $ccEmails) : ''));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
89
app/Mail/LaserIdleMail.php
Normal file
89
app/Mail/LaserIdleMail.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?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 DateTime;
|
||||
|
||||
class LaserIdleMail extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public $tableData;
|
||||
|
||||
public $scheduleType;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*/
|
||||
public function __construct($scheduleType, $tableData = [])
|
||||
{
|
||||
$this->scheduleType = $scheduleType;
|
||||
$this->tableData = $tableData ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message envelope.
|
||||
*/
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: 'Laser Idle Report',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message content definition.
|
||||
*/
|
||||
public function content(): Content
|
||||
{
|
||||
$greeting = "Dear Sir/Madam,<br><br>Kindly find the attached Laser Idle Report status details ,";
|
||||
|
||||
if ($this->scheduleType == 'Daily') {
|
||||
$fromDate = (new DateTime('yesterday 08:00'))->format('d/m/Y H:i').':000';
|
||||
$toDate = (new DateTime('today 07:59'))->format('d/m/Y H:i').':999';
|
||||
$reportPeriod = "The following report presents results from: $fromDate to $toDate.";
|
||||
$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 .= "The following report presents results from: $reportDate, $fromHour to $toHour.";
|
||||
}
|
||||
|
||||
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 .= "The following report presents results from: $fromMinute to $toMinute.";
|
||||
}
|
||||
|
||||
return new Content(
|
||||
view: 'mail.laser_idle_hour_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 [];
|
||||
}
|
||||
}
|
||||
88
resources/views/mail/laser_idle_hour_report.blade.php
Normal file
88
resources/views/mail/laser_idle_hour_report.blade.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Invoice Table</title>
|
||||
<style>
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
width: 60%;
|
||||
}
|
||||
th, td {
|
||||
border: 1px solid #444;
|
||||
padding: 8px;
|
||||
text-align: left;
|
||||
}
|
||||
th {
|
||||
background: #f2f2f2;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div style="text-align: center; font-weight: bold;">
|
||||
{{ $company }}
|
||||
</div>
|
||||
<br>
|
||||
<p>{!! $greeting !!}</p>
|
||||
|
||||
<table style="width: 100%; border-collapse: collapse; table-layout: auto;">
|
||||
|
||||
<thead>
|
||||
<tr>
|
||||
<th rowspan="2" style="border:1px solid #444; padding:8px; background:#f2f2f2; text-align:center;">No</th>
|
||||
<th rowspan="2" style="border:1px solid #444; padding:8px; background:#f2f2f2; text-align:center;">Plant</th>
|
||||
<th rowspan="2" style="border:1px solid #444; padding:8px; background:#f2f2f2; text-align:center;">Machine Name</th>
|
||||
|
||||
<th colspan="3" style="border:1px solid #444; padding:8px; background:#f2f2f2; text-align:center;">
|
||||
Auto Laser Mark Count
|
||||
</th>
|
||||
|
||||
<th rowspan="2" style="border:1px solid #444; padding:8px; background:#f2f2f2; text-align:center;">Manual Mark Count</th>
|
||||
<th rowspan="2" style="border:1px solid #444; padding:8px; background:#f2f2f2; text-align:center;">Total Laser On Hours</th>
|
||||
<th rowspan="2" style="border:1px solid #444; padding:8px; background:#f2f2f2; text-align:center;">Total Laser Off Hours</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th style="border:1px solid #444; padding:8px; background:#f2f2f2; text-align:center;">Motor</th>
|
||||
<th style="border:1px solid #444; padding:8px; background:#f2f2f2; text-align:center;">Pump</th>
|
||||
<th style="border:1px solid #444; padding:8px; background:#f2f2f2; text-align:center;">NamePlate</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
@foreach($tableData as $row)
|
||||
|
||||
<tr>
|
||||
|
||||
<td style="border: 1px solid #444; padding: 8px; text-align: center;">{{ $row['no'] }}</td>
|
||||
<td style="border: 1px solid #444; padding: 8px; text-align: center; white-space: nowrap;">{{ $row['plant'] }}</td>
|
||||
<td style="border: 1px solid #444; padding: 8px; text-align: center; white-space: nowrap;">{{ $row['machine'] }}</td>
|
||||
<td style="text-align:center; border:1px solid #444; padding:8px;">
|
||||
{{ $row['motorCount'] }}
|
||||
</td>
|
||||
|
||||
<td style="text-align:center; border:1px solid #444; padding:8px;">
|
||||
{{ $row['pumpCount'] }}
|
||||
</td>
|
||||
<td style="text-align:center; border:1px solid #444; padding:8px;">
|
||||
{{ $row['namePlateCount'] }}
|
||||
</td>
|
||||
|
||||
<td style="text-align:center; border:1px solid #444; padding:8px;">
|
||||
{{ $row['manualCount'] }}
|
||||
</td>
|
||||
<td style="text-align:center; border:1px solid #444; padding:8px;">
|
||||
{{ $row['totalLaserOnHours'] }}
|
||||
</td>
|
||||
<td style="text-align:center; border:1px solid #444; padding:8px;">
|
||||
{{ $row['totalLaserOffHours'] }}
|
||||
</td>
|
||||
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
|
||||
</table>
|
||||
|
||||
<p>{!! $wishes !!}</p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user