Added invoicemail ad productionmail report views pages and commands pages
This commit is contained in:
55
app/Console/Commands/ScheduleList.php
Normal file
55
app/Console/Commands/ScheduleList.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\AlertMailRule;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
|
||||
class ScheduleList
|
||||
{
|
||||
public function schedule(Schedule $schedule): void
|
||||
{
|
||||
// Invoice Report Scheduling
|
||||
$scheduleType = AlertMailRule::where('module', 'InvoiceValidation')
|
||||
->where('rule_name', 'InvoiceMailAlert')
|
||||
->value('schedule_type');
|
||||
|
||||
switch ($scheduleType) {
|
||||
case 'Live':
|
||||
$schedule->command('send:invoice-report')->everyMinute()->runInBackground();
|
||||
break;
|
||||
case 'Hourly':
|
||||
$schedule->command('send:invoice-report')->hourly()->runInBackground();
|
||||
break;
|
||||
// case 'Daily':
|
||||
// $schedule->command('send:invoice-report')->daily()->runInBackground(); //->dailyAt('07:59')
|
||||
// break;
|
||||
case 'Daily':
|
||||
$schedule->command('send:invoice-report')->dailyAt('07:59')->runInBackground(); //->dailyAt('07:59')
|
||||
break;
|
||||
default:
|
||||
$schedule->command('send:invoice-report')->hourly()->runInBackground();
|
||||
break;
|
||||
}
|
||||
|
||||
// Production Report Scheduling
|
||||
$productionScheduleType = AlertMailRule::where('module', 'ProductionQuantities')
|
||||
->where('rule_name', 'ProductionMailAlert')
|
||||
->value('schedule_type');
|
||||
|
||||
switch ($productionScheduleType) {
|
||||
case 'Live':
|
||||
$schedule->command('send:production-report')->everyMinute()->runInBackground();
|
||||
break;
|
||||
case 'Hourly':
|
||||
$schedule->command('send:production-report')->hourly()->runInBackground();
|
||||
break;
|
||||
case 'Daily':
|
||||
$schedule->command('send:production-report')->dailyAt('07:59')->runInBackground();
|
||||
break;
|
||||
default:
|
||||
$schedule->command('send:production-report')->hourly()->runInBackground();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
223
app/Console/Commands/SendInvoiceReport.php
Normal file
223
app/Console/Commands/SendInvoiceReport.php
Normal file
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\InvoiceValidation;
|
||||
use App\Models\Plant;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use App\Mail\test;
|
||||
|
||||
class SendInvoiceReport extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
// protected $signature = 'app:send-invoice-report';
|
||||
protected $signature = 'send:invoice-report';
|
||||
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Command description';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
// public function handle()
|
||||
// {
|
||||
// //
|
||||
// }
|
||||
|
||||
// public function handle()
|
||||
// {
|
||||
|
||||
// $mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation')
|
||||
// ->where('rule_name', 'InvoiceMailAlert')
|
||||
// ->get();
|
||||
|
||||
// $emails = $mailRules->pluck('email')->unique()->toArray();
|
||||
// $plants = InvoiceValidation::select('plant_id')->distinct()->pluck('plant_id');
|
||||
// $tableData = [];
|
||||
// $no = 1;
|
||||
|
||||
// foreach ($plants as $plantId) {
|
||||
// $plant = Plant::find($plantId);
|
||||
// $plantName = $plant ? $plant->name : $plantId;
|
||||
|
||||
// $totalInvoice = InvoiceValidation::where('plant_id', $plantId)
|
||||
// ->distinct('invoice_number')
|
||||
// ->count('invoice_number');
|
||||
|
||||
// $startDate = now()->setTime(8, 0, 0);
|
||||
// $endDate = now()->copy()->addDay()->setTime(8, 0, 0);
|
||||
|
||||
// $scannedInvoice = InvoiceValidation::select('invoice_number')
|
||||
// ->where('plant_id', $plantId)
|
||||
// ->whereNull('quantity')
|
||||
// ->whereBetween('updated_at', [$startDate, $endDate])
|
||||
// ->groupBy('invoice_number')
|
||||
// ->havingRaw("COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)")
|
||||
// ->count();
|
||||
|
||||
// $tableData[] = [
|
||||
// 'no' => $no++,
|
||||
// 'plant' => $plantName,
|
||||
// 'totalInvoice' => $totalInvoice,
|
||||
// 'scannedInvoice' => $scannedInvoice,
|
||||
// ];
|
||||
// }
|
||||
|
||||
// // Mail::to('jothikumar.padmanaban@cripumps.com')->send(
|
||||
// // new test($tableData)
|
||||
// // );
|
||||
|
||||
// if (!empty($emails))
|
||||
// {
|
||||
// // Mail::to($emails)->send(new test($tableData));
|
||||
// // $this->info('Invoice report email sent successfully to: ' . implode(', ', $emails));
|
||||
// foreach ($emails as $email) {
|
||||
// Mail::to($email)->send(new test($tableData));
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// else
|
||||
// {
|
||||
// $this->info('No recipients found for InvoiceMailAlert.');
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
|
||||
public function handle()
|
||||
{
|
||||
// Group emails by rule name
|
||||
$mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation')->get()->groupBy('rule_name');
|
||||
|
||||
$startDate = now()->setTime(8, 0, 0);
|
||||
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
|
||||
$plants = InvoiceValidation::select('plant_id')->distinct()->pluck('plant_id');
|
||||
|
||||
// Build all tables once
|
||||
$serialTableData = [];
|
||||
$materialTableData = [];
|
||||
$bundleTableData = [];
|
||||
$noSerial = 1;
|
||||
$noMaterial = 1;
|
||||
$noBundle = 1;
|
||||
|
||||
foreach ($plants as $plantId) {
|
||||
$plant = Plant::find($plantId);
|
||||
$plantName = $plant ? $plant->name : $plantId;
|
||||
|
||||
//..Serial Invoice
|
||||
|
||||
$totalSerialCount = InvoiceValidation::where('plant_id', $plantId)
|
||||
->whereNull('quantity')
|
||||
->whereBetween('created_at', [$startDate, $endDate])
|
||||
->distinct('invoice_number')
|
||||
->count('invoice_number');
|
||||
|
||||
$scannedSerialCount = InvoiceValidation::select('invoice_number')
|
||||
->where('plant_id', $plantId)
|
||||
->whereNull('quantity')
|
||||
->whereBetween('updated_at', [$startDate, $endDate])
|
||||
->groupBy('invoice_number')
|
||||
->havingRaw(
|
||||
"COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"
|
||||
)
|
||||
->count();
|
||||
|
||||
$serialTableData[] = [
|
||||
'no' => $noSerial++,
|
||||
'plant' => $plantName,
|
||||
'totalInvoice' => $totalSerialCount,
|
||||
'scannedInvoice' => $scannedSerialCount,
|
||||
];
|
||||
|
||||
//..Individual Invoice
|
||||
|
||||
$TotalMatCount = InvoiceValidation::where('plant_id', $plantId)
|
||||
->where('quantity', 1)
|
||||
->whereBetween('created_at', [$startDate, $endDate])
|
||||
->distinct('invoice_number')
|
||||
->count('invoice_number');
|
||||
|
||||
$scannedMatCount = InvoiceValidation::select('invoice_number')
|
||||
->where('plant_id', $plantId)
|
||||
->where('quantity', 1)
|
||||
->whereBetween('updated_at', [$startDate, $endDate])
|
||||
->groupBy('invoice_number')
|
||||
->havingRaw(
|
||||
"COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"
|
||||
)
|
||||
->count();
|
||||
|
||||
$materialTableData[] = [
|
||||
'no' => $noMaterial++,
|
||||
'plant' => $plantName,
|
||||
'totalInvoice' => $TotalMatCount,
|
||||
'scannedInvoice' => $scannedMatCount,
|
||||
];
|
||||
|
||||
//..BUndle Invoice
|
||||
|
||||
$totalBundleCount = InvoiceValidation::where('plant_id', $plantId)
|
||||
->where('quantity', '>', 1)
|
||||
->whereBetween('created_at', [$startDate, $endDate])
|
||||
->distinct('invoice_number')
|
||||
->count('invoice_number');
|
||||
|
||||
$scannedBundleCount = InvoiceValidation::select('invoice_number')
|
||||
->where('plant_id', $plantId)
|
||||
->where('quantity', '>', 1)
|
||||
->whereBetween('updated_at', [$startDate, $endDate])
|
||||
->groupBy('invoice_number')
|
||||
->havingRaw(
|
||||
"COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"
|
||||
)
|
||||
->count();
|
||||
|
||||
$bundleTableData[] = [
|
||||
'no' => $noBundle++,
|
||||
'plant' => $plantName,
|
||||
'totalInvoice' => $totalBundleCount,
|
||||
'scannedInvoice' => $scannedBundleCount,
|
||||
];
|
||||
}
|
||||
|
||||
// Send to SerialInvoiceMail recipients
|
||||
if ($mailRules->has('SerialInvoiceMail')) {
|
||||
$emails = $mailRules['SerialInvoiceMail']->pluck('email')->unique()->toArray();
|
||||
foreach ($emails as $email) {
|
||||
Mail::to($email)->send(new test($serialTableData, [], []));
|
||||
}
|
||||
}
|
||||
|
||||
// Send to MaterialInvoiceMail recipients (material + bundle table)
|
||||
if ($mailRules->has('MaterialInvoiceMail')) {
|
||||
$emails = $mailRules['MaterialInvoiceMail']->pluck('email')->unique()->toArray();
|
||||
foreach ($emails as $email) {
|
||||
Mail::to($email)->send(new test([], $materialTableData, $bundleTableData));
|
||||
}
|
||||
}
|
||||
|
||||
// Send to InvoiceMail recipients (all three tables)
|
||||
if ($mailRules->has('InvoiceMail')) {
|
||||
$emails = $mailRules['InvoiceMail']->pluck('email')->unique()->toArray();
|
||||
foreach ($emails as $email) {
|
||||
Mail::to($email)->send(new test($serialTableData, $materialTableData, $bundleTableData));
|
||||
}
|
||||
}
|
||||
|
||||
//$this->info(json_encode($materialTableData));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
133
app/Console/Commands/SendProductionReport.php
Normal file
133
app/Console/Commands/SendProductionReport.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\InvoiceValidation;
|
||||
use App\Models\Plant;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use App\Mail\ProductionMail;
|
||||
use App\Models\Line;
|
||||
use App\Models\ProductionQuantity;
|
||||
|
||||
class SendProductionReport extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'send:production-report';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Command description';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities')
|
||||
->where('rule_name', 'ProductionMail')
|
||||
->get();
|
||||
|
||||
$emails = $mailRules->pluck('email')->unique()->toArray();
|
||||
|
||||
$lines = Line::all();
|
||||
$fgLineIds = [];
|
||||
$nonFgLineIds = [];
|
||||
$lineNames = [];
|
||||
|
||||
foreach ($lines as $line) {
|
||||
$lineNames[$line->id] = $line->name;
|
||||
if ($line->type == 'FG Line') {
|
||||
$fgLineIds[] = $line->id;
|
||||
} else {
|
||||
$nonFgLineIds[] = $line->id;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Set date range
|
||||
$startDate = now()->setTime(8, 0, 0);
|
||||
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
|
||||
|
||||
// 3. Get all plants
|
||||
$plants = ProductionQuantity::select('plant_id')->distinct()->pluck('plant_id');
|
||||
|
||||
// 4. Get production quantities for FG and non-FG lines in one go
|
||||
// $fgProduction = ProductionQuantity::select('line_id', \DB::raw('COUNT(*) as total_quantity'))
|
||||
// ->whereBetween('created_at', [$startDate, $endDate])
|
||||
// ->whereIn('line_id', $fgLineIds)
|
||||
// ->groupBy('line_id')
|
||||
// ->pluck('total_quantity', 'line_id')
|
||||
// ->toArray();
|
||||
|
||||
$nonFgProduction = ProductionQuantity::select('line_id', \DB::raw('COUNT(*) as total_quantity'))
|
||||
->whereBetween('created_at', [$startDate, $endDate])
|
||||
->whereIn('line_id', $nonFgLineIds)
|
||||
->groupBy('line_id')
|
||||
->pluck('total_quantity', 'line_id')
|
||||
->toArray();
|
||||
|
||||
$tableData = [];
|
||||
$no = 1;
|
||||
|
||||
foreach ($plants as $plantId)
|
||||
{
|
||||
$plant = Plant::find($plantId);
|
||||
$plantName = $plant ? $plant->name : $plantId;
|
||||
|
||||
//Get all unique line_ids for this plant
|
||||
$lineIds = ProductionQuantity::where('plant_id', $plantId)
|
||||
->select('line_id')
|
||||
->distinct()
|
||||
->pluck('line_id');
|
||||
|
||||
foreach ($lineIds as $lineId) {
|
||||
$lineName = $lineNames[$lineId] ?? $lineId;
|
||||
|
||||
$targetQuantity = \App\Models\ProductionPlan::where('plant_id', $plantId)
|
||||
->where('line_id', $lineId)
|
||||
->whereBetween('created_at', [$startDate, $endDate])
|
||||
->sum('plan_quantity');
|
||||
|
||||
// Use the correct production quantity array based on line type
|
||||
if (in_array($lineId, $nonFgLineIds))
|
||||
{
|
||||
$productionQuantity = $nonFgProduction[$lineId] ?? 0;
|
||||
}
|
||||
// else
|
||||
// {
|
||||
// $productionQuantity = $nonFgProduction[$lineId] ?? 0;
|
||||
// }
|
||||
$tableData[] = [
|
||||
'no' => $no++,
|
||||
'plant' => $plantName,
|
||||
'line' => $lineName,
|
||||
'targetQuantity' => $targetQuantity,
|
||||
'productionQuantity' => $productionQuantity,
|
||||
];
|
||||
}
|
||||
}
|
||||
//$this->info(json_encode($tableData));
|
||||
|
||||
if (!empty($emails))
|
||||
{
|
||||
// Mail::to($emails)->send(new test($tableData));
|
||||
// $this->info('Invoice report email sent successfully to: ' . implode(', ', $emails));
|
||||
foreach ($emails as $email)
|
||||
{
|
||||
Mail::to($email)->send(new ProductionMail($tableData));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->info('No recipients found for InvoiceMailAlert.');
|
||||
}
|
||||
//$this->info(implode(', ', $emails));
|
||||
}
|
||||
}
|
||||
61
app/Mail/ProductionMail.php
Normal file
61
app/Mail/ProductionMail.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?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 ProductionMail extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public $tableData;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*/
|
||||
public function __construct($tableData = [])
|
||||
{
|
||||
$this->tableData = $tableData ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message envelope.
|
||||
*/
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: 'Production Mail',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message content definition.
|
||||
*/
|
||||
public function content(): Content
|
||||
{
|
||||
return new Content(
|
||||
view: 'mail.production_report',
|
||||
with: [
|
||||
'company' => "CRI Digital Manufacturing Solutions",
|
||||
'greeting' => "Dear Sir/Madam,<br><br>Kindly find the attached production report status details for the 'Target Quantity' and 'Production Quantity' count,",
|
||||
'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 [];
|
||||
}
|
||||
}
|
||||
77
app/Mail/test.php
Normal file
77
app/Mail/test.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class test extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public $serialTableData;
|
||||
public $materialTableData;
|
||||
public $bundleTableData;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*/
|
||||
// public function __construct($tableData = [])
|
||||
// {
|
||||
// $this->tableData = $tableData ?? [];
|
||||
// }
|
||||
public function __construct($serialTableData, $materialTableData, $bundleTableData)
|
||||
{
|
||||
$this->serialTableData = $serialTableData;
|
||||
$this->materialTableData = $materialTableData;
|
||||
$this->bundleTableData = $bundleTableData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message envelope.
|
||||
*/
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: 'Invoice Table Report'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message content definition.
|
||||
*/
|
||||
public function content(): Content
|
||||
{
|
||||
return new Content(
|
||||
view: 'mail.test_mail',
|
||||
// with: [
|
||||
// 'name' => "CRI Digital Manufacturing Solutions<br><br>Dear Sir/Madam,<br><br> Kindly find the attached invoice status details for the 'Total Number Of Invoices' and 'Scanned Number Of Invoices' count,",
|
||||
// 'tableData' => $this->tableData,
|
||||
// 'wishes' => "Thanks & Regards<br><br>CRI Digital Manufacturing Solutions"
|
||||
// ],
|
||||
with: [
|
||||
'company' => "CRI Digital Manufacturing Solutions",
|
||||
'greeting' => "Dear Sir/Madam,<br><br>Kindly find the attached invoice status details for the 'Total Number of Invoices' and 'Scanned Number of Invoices' count,",
|
||||
//'tableData' => $this->tableData,
|
||||
'serialTableData' => $this->serialTableData,
|
||||
'materialTableData' => $this->materialTableData,
|
||||
'bundleTableData' => $this->bundleTableData,
|
||||
'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 [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user