Added process order report mail #1411

Merged
jothi merged 1 commits from ranjith-dev into master 2026-11-13 06:09:17 +00:00
3 changed files with 391 additions and 0 deletions
Showing only changes of commit 96774140cd - Show all commits

View File

@@ -0,0 +1,219 @@
<?php
namespace App\Console\Commands;
use App\Mail\ProcessOrderMail;
use App\Models\AlertMailRule;
use App\Models\CharacteristicValue;
use App\Models\Plant;
use App\Models\ProcessOrder;
use DB;
use Illuminate\Console\Command;
class SendProcessOrderReport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send:process-order-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', 'ProcessOrder')
->where('rule_name', 'ProcessOrderMail')
->where('schedule_type', $scheduleType)
->where('plant', $plantId)
->get();
$plants = ($plantId == 0)
? Plant::all()
: Plant::where('id', $plantId)->get();
$plantCodes = $plants->pluck('name', 'id');
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);
}
$query = ProcessOrder::whereBetween('created_at', [$startDate, $endDate]);
if ($plantId != 0) {
$query->where('plant_id', $plantId);
}
$tableData = [];
$no = 1;
foreach ($plants as $plant) {
$receivedSummary = ProcessOrder::query()
->where('plant_id', $plant->id)
->whereBetween('created_at', [$startDate, $endDate])
->groupBy('process_order')
->select('process_order', DB::raw('SUM(received_quantity) as total_received'))
->pluck('total_received', 'process_order');
// $latestRecords = ProcessOrder::query()
// ->where('plant_id', $plant->id)
// ->whereBetween('created_at', [$startDate, $endDate])
// ->orderBy('process_order')
// ->orderByDesc('created_at')
// ->distinct('process_order')
// ->selectRaw('MAX(coil_number) as max_coil_number')
// ->with(['line', 'item'])
// ->get();
$latestRecords = ProcessOrder::query()
->where('plant_id', $plant->id)
->whereBetween('created_at', [$startDate, $endDate])
->whereIn('id', function ($query) use ($plant, $startDate, $endDate) {
$query->selectRaw('MAX(id)')
->from('process_orders')
->where('plant_id', $plant->id)
->whereBetween('created_at', [$startDate, $endDate])
->groupBy('process_order');
})
->with(['line', 'item'])
->orderBy('process_order')
->get();
foreach ($latestRecords as $order) {
$tableData[] = [
'no' => $no++,
'plant' => $plant->name,
'line' => $order->line?->name ?? '-',
'machineName' => $order->machine_name,
'item' => $order->item?->code ?? '-',
'processOrder' => $order->process_order,
'coilNo' => $order->coil_number,
'orderQuan' => $order->order_quantity,
'recQuan' => $receivedSummary[$order->process_order] ?? 0,
'scrapQuan' => $order->scrap_quantity,
'sfgNo' => $order->sfg_number,
];
}
}
$noNotOk = 1;
$NotOkData = [];
foreach ($plants as $plant) {
$processOrderTotals = DB::table('process_orders')
->where('plant_id', $plant->id)
->where('coil_number', '!=', 0)
->selectRaw('
process_order,
COUNT(*) AS total_count
')
->groupBy('process_order')
->get()
->keyBy('process_order');
$statusCounts = DB::table('characteristic_values')
->where('plant_id', $plant->id)
->whereBetween('created_at', [$startDate, $endDate])
->selectRaw("
process_order,
SUM(CASE WHEN status = 'NotOk' THEN 1 ELSE 0 END) AS notok_count,
SUM(CASE WHEN status = 'Ok' THEN 1 ELSE 0 END) AS ok_count
")
->groupBy('process_order')
->get()
->keyBy('process_order');
$latestRecords = CharacteristicValue::query()
->where('plant_id', $plant->id)
->whereBetween('created_at', [$startDate, $endDate])
->orderBy('process_order')
->orderByDesc('created_at')
->distinct('process_order')
->with(['line', 'item'])
->get();
foreach ($latestRecords as $order) {
$processOrder = $order->process_order;
$total = $processOrderTotals[$processOrder]->total_count ?? 0;
$notOk = $statusCounts[$processOrder]->notok_count ?? 0;
$ok = $statusCounts[$processOrder]->ok_count ?? 0;
$notOkPerc = $total > 0
? round(($notOk / $total) * 100, 2)
: 0;
$okPerc = $total > 0
? round(($ok / $total) * 100, 2)
: 0;
$NotOkData[] = [
'no' => $noNotOk++,
'plant' => $plant->name,
'line' => $order->line?->name ?? '-',
'machineName' => $order->machine_name,
'item' => $order->item?->code ?? '-',
'processOrder' => $order->process_order,
'NotOkPerc' => $notOkPerc . '%',
'OkPerc' => $okPerc . '%',
];
}
}
$mail = new ProcessOrderMail($scheduleType, $tableData, $NotOkData);
$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) : ''));
}
}
}

View File

@@ -0,0 +1,80 @@
<?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 ProcessOrderMail extends Mailable
{
use Queueable, SerializesModels;
public $tableData;
public $NotOkData;
public $scheduleType;
/**
* Create a new message instance.
*/
public function __construct($scheduleType,$tableData = [], $NotOkData)
{
$this->scheduleType = $scheduleType;
$this->tableData = $tableData ?? [];
$this->NotOkData = $NotOkData ?? [];
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Process Order Mail',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
$greeting = "Dear Sir/Madam,<br><br>Kindly find the attached process order report status details for the 'Order Quantity' and 'Received Quantity' count,";
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;
}
return new Content(
view: 'mail.process_order_report',
with: [
'company' => "CRI Digital Manufacturing Solutions",
'greeting' => $greeting,
'tableData' => $this->tableData,
'NotOkData' => $this->NotOkData,
'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 [];
}
}

View File

@@ -0,0 +1,92 @@
<!DOCTYPE html>
<html>
<head>
<title>Process Order Report 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="table-layout: auto; width: 100%; border-collapse: collapse;">
<thead>
<tr>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">No</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2; white-space: nowrap;">Plant</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2; white-space: nowrap;">Line</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">Machine name</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">Item Code</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">Process Order</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">Order Quantity</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">Total Quantity</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">Scrap Quantity</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">SFG Number</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['line'] }}</td>
<td style="border: 1px solid #444; padding: 8px; text-align: center;">{{ $row['machineName'] }}</td>
<td style="border: 1px solid #444; padding: 8px; text-align: center;">{{ $row['item'] }}</td>
<td style="border: 1px solid #444; padding: 8px; text-align: center;">{{ $row['processOrder'] }}</td>
<td style="border: 1px solid #444; padding: 8px; text-align: center;">{{ $row['orderQuan'] }}</td>
<td style="border: 1px solid #444; padding: 8px; text-align: center;">{{ $row['recQuan'] }}</td>
<td style="border: 1px solid #444; padding: 8px; text-align: center;">{{ $row['scrapQuan'] }}</td>
<td style="border: 1px solid #444; padding: 8px; text-align: center;">{{ $row['sfgNo'] }}</td>
</tr>
@endforeach
</tbody>
</table>
<br>
<p><b><u>Process Order Not Ok Report</u></b><p>
<table style="table-layout: auto; width: 100%; border-collapse: collapse;">
<thead>
<tr>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">No</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2; white-space: nowrap;">Plant</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2; white-space: nowrap;">Line</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">Machine name</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">Item Code</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">Process Order</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">Not Ok Status (%)</th>
<th style="text-align: center; border: 1px solid #444; padding: 8px; background: #f2f2f2;">Ok Status (%)</th>
</tr>
</thead>
<tbody>
@foreach($NotOkData 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['line'] }}</td>
<td style="border: 1px solid #444; padding: 8px; text-align: center;">{{ $row['machineName'] }}</td>
<td style="border: 1px solid #444; padding: 8px; text-align: center;">{{ $row['item'] }}</td>
<td style="border: 1px solid #444; padding: 8px; text-align: center;">{{ $row['processOrder'] }}</td>
<td style="border: 1px solid #444; padding: 8px; text-align: center;">{{ $row['NotOkPerc'] }}</td>
<td style="border: 1px solid #444; padding: 8px; text-align: center;">{{ $row['OkPerc'] }}</td>
</tr>
@endforeach
</tbody>
</table>
<p>{!! $wishes !!}</p>
</body>
</html>