Add SendInvoiceDataReport command and InvoiceDataMail class with HTML view for invoice data reporting
This commit is contained in:
201
app/Console/Commands/SendInvoiceDataReport.php
Normal file
201
app/Console/Commands/SendInvoiceDataReport.php
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Mail\InvoiceDataMail;
|
||||||
|
use App\Models\Line;
|
||||||
|
use App\Models\Plant;
|
||||||
|
use App\Models\ProductionPlan;
|
||||||
|
use App\Models\ProductionQuantity;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Facades\Mail;
|
||||||
|
|
||||||
|
class SendInvoiceDataReport extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'send:invoice-data-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'); // cast to int for safety
|
||||||
|
|
||||||
|
|
||||||
|
// Fetch mail rules based on schedule type
|
||||||
|
$mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation')
|
||||||
|
->where('rule_name', 'InvoiceDataMail')
|
||||||
|
->where('schedule_type', $scheduleType)
|
||||||
|
->where('plant', $plantId)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$emails = $mailRules->pluck('email')->unique()->toArray();
|
||||||
|
|
||||||
|
//$ccEmails = $mailRules->pluck('cc_email')->filter()->unique()->toArray();
|
||||||
|
|
||||||
|
//$this->info('Found emails: ' . implode(', ', $emails));
|
||||||
|
|
||||||
|
$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) {
|
||||||
|
|
||||||
|
// $distributions = ['Direct Sale', 'Branch Sale', 'Internal Transfer'];
|
||||||
|
|
||||||
|
// foreach ($distributions as $selectedDistribution) {
|
||||||
|
|
||||||
|
// // Get invoice records
|
||||||
|
// $invoices = \App\Models\InvoiceDataValidation::where('plant_id', $plant->id)
|
||||||
|
// ->where('distribution_channel_desc', $selectedDistribution)
|
||||||
|
// ->whereBetween('document_date', [$startDate, $endDate])
|
||||||
|
// ->select('customer_code', 'document_number', 'document_date', 'customer_trade_name', 'customer_location')
|
||||||
|
// ->distinct('document_number')
|
||||||
|
// ->get();
|
||||||
|
|
||||||
|
// if ($invoices->isEmpty()) {
|
||||||
|
// $tableData[] = [
|
||||||
|
// 'no' => $no++,
|
||||||
|
// 'plant' => $plant->name,
|
||||||
|
// 'distribution_type' => $selectedDistribution,
|
||||||
|
// 'customer_code' => '-',
|
||||||
|
// 'document_number' => '-',
|
||||||
|
// 'document_date' => '-',
|
||||||
|
// 'customer_trade_name' => '-',
|
||||||
|
// 'customer_location' => '-',
|
||||||
|
// 'status' => 'No Invoices',
|
||||||
|
// ];
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Fetch which invoices actually went out
|
||||||
|
// $wentOutInvoices = \App\Models\InvoiceOutValidation::where('plant_id', $plant->id)
|
||||||
|
// ->whereIn('qr_code', $invoices->pluck('document_number')->toArray())
|
||||||
|
// ->whereBetween('scanned_at', [$startDate, $endDate])
|
||||||
|
// ->distinct('qr_code')
|
||||||
|
// ->pluck('qr_code')
|
||||||
|
// ->toArray();
|
||||||
|
|
||||||
|
// foreach ($invoices as $inv) {
|
||||||
|
// $status = in_array($inv->document_number, $wentOutInvoices)
|
||||||
|
// ? 'Went Out'
|
||||||
|
// : 'Pending';
|
||||||
|
|
||||||
|
// $tableData[] = [
|
||||||
|
// 'no' => $no++,
|
||||||
|
// 'plant' => $plant->name,
|
||||||
|
// 'distribution_type' => $selectedDistribution,
|
||||||
|
// 'customer_code' => $inv->customer_code,
|
||||||
|
// 'document_number' => $inv->document_number,
|
||||||
|
// 'document_date' => $inv->document_date,
|
||||||
|
// 'customer_trade_name' => $inv->customer_trade_name,
|
||||||
|
// 'customer_location' => $inv->customer_location,
|
||||||
|
// 'status' => $status,
|
||||||
|
// ];
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
$tableData = [];
|
||||||
|
$no = 1;
|
||||||
|
|
||||||
|
foreach ($plants as $plant) {
|
||||||
|
|
||||||
|
$distributions = ['Direct Sale', 'Branch Sale', 'Internal Transfer'];
|
||||||
|
|
||||||
|
foreach ($distributions as $selectedDistribution) {
|
||||||
|
|
||||||
|
$invoices = \App\Models\InvoiceDataValidation::where('plant_id', $plant->id)
|
||||||
|
->where('distribution_channel_desc', $selectedDistribution)
|
||||||
|
->whereBetween('document_date', [$startDate, $endDate])
|
||||||
|
->select('customer_code', 'document_number', 'document_date', 'customer_trade_name', 'customer_location')
|
||||||
|
->distinct('document_number')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
if ($invoices->isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$wentOutInvoices = \App\Models\InvoiceOutValidation::where('plant_id', $plant->id)
|
||||||
|
->whereIn('qr_code', $invoices->pluck('document_number')->toArray())
|
||||||
|
->whereBetween('scanned_at', [$startDate, $endDate])
|
||||||
|
->distinct('qr_code')
|
||||||
|
->pluck('qr_code')
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
$pendingInvoices = $invoices->filter(function ($inv) use ($wentOutInvoices) {
|
||||||
|
return !in_array($inv->document_number, $wentOutInvoices);
|
||||||
|
});
|
||||||
|
|
||||||
|
if ($pendingInvoices->isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($pendingInvoices as $inv) {
|
||||||
|
$tableData[] = [
|
||||||
|
'no' => $no++,
|
||||||
|
'plant' => $plant->name,
|
||||||
|
'distribution_type' => $selectedDistribution,
|
||||||
|
'customer_code' => $inv->customer_code,
|
||||||
|
'document_number' => $inv->document_number,
|
||||||
|
'document_date' => $inv->document_date,
|
||||||
|
'customer_trade_name' => $inv->customer_trade_name,
|
||||||
|
'customer_location' => $inv->customer_location,
|
||||||
|
'status' => 'Pending',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$mail = new InvoiceDataMail($scheduleType, $tableData);
|
||||||
|
$contentVars = $mail->content()->with;
|
||||||
|
|
||||||
|
$this->info($contentVars['greeting'] ?? 'Invoice Data Report');
|
||||||
|
$this->table(
|
||||||
|
['No', 'Plant', 'Distribution Type', 'Customer Code', 'Document Number', 'Document Date', 'Trade Name', 'Location', 'Status'],
|
||||||
|
$tableData
|
||||||
|
);
|
||||||
|
$this->info($contentVars['wishes'] ?? '');
|
||||||
|
|
||||||
|
// Send Mail //->cc($ccEmails)
|
||||||
|
if (!empty($emails)) {
|
||||||
|
foreach ($emails as $email){
|
||||||
|
\Mail::to($email)->send(new InvoiceDataMail($scheduleType, $tableData));
|
||||||
|
}
|
||||||
|
$this->info("Invoice data report sent to " . count($emails) . " recipient(s).");
|
||||||
|
} else {
|
||||||
|
$this->warn('No recipients found for InvoiceDataMail.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
89
app/Mail/InvoiceDataMail.php
Normal file
89
app/Mail/InvoiceDataMail.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 InvoiceDataMail 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: 'Invoice Data Mail',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the message content definition.
|
||||||
|
*/
|
||||||
|
public function content(): Content
|
||||||
|
{
|
||||||
|
$greeting = "Dear Sir/Madam,<br><br>Kindly find the attached invoice pending report status,";
|
||||||
|
|
||||||
|
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.invoice_data_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 [];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
111
resources/views/mail/invoice_data_report.blade.php
Normal file
111
resources/views/mail/invoice_data_report.blade.php
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Invoice Data 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;
|
||||||
|
}
|
||||||
|
th {
|
||||||
|
background-color: #215c98;
|
||||||
|
color: #fff;
|
||||||
|
white-space: nowrap !important;
|
||||||
|
}
|
||||||
|
tr:nth-child(even) {
|
||||||
|
background-color: #f3f4f6;
|
||||||
|
}
|
||||||
|
.status-wentout {
|
||||||
|
color: #10b981;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.status-pending {
|
||||||
|
color: #ef4444;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.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 invoice data available for this schedule.</p>
|
||||||
|
@else
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>No</th>
|
||||||
|
<th>Plant</th>
|
||||||
|
<th>Distribution Type</th>
|
||||||
|
<th>Customer Code</th>
|
||||||
|
<th>Document Number</th>
|
||||||
|
<th>Document Date</th>
|
||||||
|
<th>Customer TradeName</th>
|
||||||
|
<th>Customer Location</th>
|
||||||
|
<th>Status</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@foreach ($tableData as $row)
|
||||||
|
<tr>
|
||||||
|
<td>{{ $row['no'] }}</td>
|
||||||
|
<td>{{ $row['plant'] }}</td>
|
||||||
|
<td>{{ $row['distribution_type'] }}</td>
|
||||||
|
<td>{{ $row['customer_code'] }}</td>
|
||||||
|
<td>{{ $row['document_number'] }}</td>
|
||||||
|
|
||||||
|
{{-- ✅ Safe Carbon parse --}}
|
||||||
|
<td>
|
||||||
|
@if(!empty($row['document_date']) && $row['document_date'] != '-')
|
||||||
|
{{ \Carbon\Carbon::parse($row['document_date'])->format('d-M-Y') }}
|
||||||
|
@else
|
||||||
|
-
|
||||||
|
@endif
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<td>{{ $row['customer_trade_name'] }}</td>
|
||||||
|
<td>{{ $row['customer_location'] }}</td>
|
||||||
|
<td class="{{ $row['status'] == 'Went Out' ? 'status-wentout' : ($row['status'] == 'Pending' ? 'status-pending' : '') }}">
|
||||||
|
{{ $row['status'] }}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
@endforeach
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
@endif
|
||||||
|
<p>{!! $wishes !!}</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -12,11 +12,15 @@ Artisan::command('inspire', function () {
|
|||||||
// Schedule::command('send:invoice-report');
|
// Schedule::command('send:invoice-report');
|
||||||
// Schedule::command('send:production-report');
|
// Schedule::command('send:production-report');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app()->booted(function () {
|
app()->booted(function () {
|
||||||
$schedule = app(Schedule::class);
|
$schedule = app(Schedule::class);
|
||||||
|
|
||||||
// Production report scheduling
|
//$schedule->command('report:send-daily-production')->dailyAt('07:59');
|
||||||
|
|
||||||
|
//Production report scheduling
|
||||||
$productionRules = AlertMailRule::where('module', 'ProductionQuantities')
|
$productionRules = AlertMailRule::where('module', 'ProductionQuantities')
|
||||||
->where('rule_name', 'ProductionMail')
|
->where('rule_name', 'ProductionMail')
|
||||||
->select('plant', 'schedule_type')
|
->select('plant', 'schedule_type')
|
||||||
@@ -67,6 +71,31 @@ Artisan::command('inspire', function () {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Invoice Data report scheduling
|
||||||
|
$invoiceDataRules = AlertMailRule::where('module', 'InvoiceValidation')
|
||||||
|
->select('plant', 'schedule_type')
|
||||||
|
->distinct()
|
||||||
|
->get();
|
||||||
|
|
||||||
|
foreach ($invoiceDataRules as $rule) {
|
||||||
|
$type = $rule->schedule_type;
|
||||||
|
$plantId = $rule->plant;
|
||||||
|
|
||||||
|
$command = $schedule->command('send:invoice-data-report', [$type, $plantId]);
|
||||||
|
|
||||||
|
switch ($type) {
|
||||||
|
case 'Live':
|
||||||
|
$command->everyMinute();
|
||||||
|
break;
|
||||||
|
case 'Hourly':
|
||||||
|
$command->hourly();
|
||||||
|
break;
|
||||||
|
case 'Daily':
|
||||||
|
$command->dailyAt('07:59');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user