Initial commit for new repo
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 1m4s

This commit is contained in:
dhanabalan
2025-12-16 17:05:04 +05:30
commit 3f0d529640
862 changed files with 141157 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class LogClear extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:log-clear';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
file_put_contents(storage_path('logs/laravel.log'), '');
$this->info('Laravel log cleared!');
}
}

View File

@@ -0,0 +1,164 @@
<?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', 'InvoiceMail')
// ->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;
// }
// $SerialscheduleType = AlertMailRule::where('module', 'InvoiceValidation')
// ->where('rule_name', 'SerialInvoiceMail')
// ->value('schedule_type');
// switch ($SerialscheduleType) {
// 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;
// }
// $materialscheduleType = AlertMailRule::where('module', 'InvoiceValidation')
// ->where('rule_name', 'MaterialInvoiceMail')
// ->value('schedule_type');
// switch ($materialscheduleType) {
// 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', 'ProductionMail')
// ->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;
// }
// }
public function schedule(Schedule $schedule): void
{
// Define the rules you want to schedule
$rules = [
[
'module' => 'InvoiceValidation',
'rule_name' => 'InvoiceMail',
'argument' => 'InvoiceMail',
],
[
'module' => 'InvoiceValidation',
'rule_name' => 'SerialInvoiceMail',
'argument' => 'SerialInvoiceMail',
],
[
'module' => 'InvoiceValidation',
'rule_name' => 'MaterialInvoiceMail',
'argument' => 'MaterialInvoiceMail',
],
];
// Loop through each rule and schedule accordingly
foreach ($rules as $rule) {
$scheduleType = AlertMailRule::where('module', $rule['module'])
->where('rule_name', $rule['rule_name'])
->value('schedule_type');
switch ($scheduleType) {
case 'Live':
$schedule->command("send:invoice-report {$rule['argument']}")->everyMinute()->runInBackground();
break;
case 'Hourly':
$schedule->command("send:invoice-report {$rule['argument']}")->hourly()->runInBackground();
break;
case 'Daily':
$schedule->command("send:invoice-report {$rule['argument']}")->dailyAt('07:59')->runInBackground();
break;
default:
$schedule->command("send:invoice-report {$rule['argument']}")->hourly()->runInBackground();
break;
}
}
// Production Report Scheduling
$productionScheduleType = AlertMailRule::where('module', 'ProductionQuantities')
->where('rule_name', 'ProductionMail')
->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;
}
}
}

View File

@@ -0,0 +1,226 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;
use App\Models\AlertMailRule;
class Scheduler extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
// protected $signature = 'app:scheduler';
protected $signature = 'custom:scheduler';
/**
* The console command description.
*
* @var string
*/
// protected $description = 'Command description';
protected $description = 'Manually trigger scheduler logic';
/**
* Execute the console command.
*/
// public function handle()
// {
// //
// }
// public function handle()
// {
// // --- Production Rules ---
// $productionRules = AlertMailRule::where('module', 'ProductionQuantities')
// ->where('rule_name', 'ProductionMail')
// ->select('plant', 'schedule_type')
// ->distinct()
// ->get();
// // foreach ($productionRules as $rule) {
// // if ($rule->schedule_type == 'Live') {
// // $this->callArtisanCommand('send:production-report', $rule);
// // }
// // }
// foreach ($productionRules as $rule) {
// $command = $schedule->command('send:production-report', [
// $rule->schedule_type,
// $rule->plant,
// ]);
// switch ($rule->schedule_type) {
// case 'Live':
// $command->everyMinute();
// break;
// case 'Hourly':
// $command->hourly();
// break;
// case 'Daily':
// $command->dailyAt('07:59');
// break;
// }
// }
// // --- Invoice Validation Rules ---
// $invoiceRules = AlertMailRule::where('module', 'InvoiceValidation')
// ->select('plant', 'schedule_type')
// ->distinct()
// ->get();
// foreach ($invoiceRules as $rule) {
// if ($rule->schedule_type == 'Live') {
// $this->callArtisanCommand('send:invoice-report', $rule);
// }
// }
// // --- Invoice Data Report Rules ---
// $invoiceDataRules = AlertMailRule::where('module', 'InvoiceDataReport')
// ->select('plant', 'schedule_type')
// ->distinct()
// ->get();
// foreach ($invoiceDataRules as $rule) {
// if ($rule->schedule_type == 'Live') {
// $this->callArtisanCommand('send:invoice-data-report', $rule);
// }
// }
// \Log::info('Custom scheduler executed at ' . now());
// $this->info('Custom scheduler executed successfully.');
// }
public function handle()
{
//$schedule = app(\Illuminate\Console\Scheduling\Schedule::class);
// --- Production Rules ---
$productionRules = AlertMailRule::where('module', 'ProductionQuantities')
->where('rule_name', 'ProductionMail')
->select('plant', 'schedule_type')
->distinct()
->get();
foreach ($productionRules as $rule) {
switch ($rule->schedule_type) {
case 'Live':
// Run every minute
\Artisan::call('send:production-report', [
'schedule_type' => $rule->schedule_type,
'plant' => $rule->plant,
]);
break;
case 'Hourly':
if (now()->minute == 0) {
\Artisan::call('send:production-report', [
'schedule_type' => $rule->schedule_type,
'plant' => $rule->plant,
]);
}
break;
case 'Daily':
if (now()->format('H:i') == '15:59') {
\Artisan::call('send:production-report', [
'schedule_type' => $rule->schedule_type,
'plant' => $rule->plant,
]);
}
break;
}
}
// --- Invoice Validation Rules ---
$invoiceRules = AlertMailRule::where('module', 'InvoiceValidation')
->select('plant', 'schedule_type')
->distinct()
->get();
foreach ($invoiceRules as $rule) {
switch ($rule->schedule_type) {
case 'Live':
// Run every minute
\Artisan::call('send:invoice-report', [
'schedule_type' => $rule->schedule_type,
'plant' => $rule->plant,
]);
break;
case 'Hourly':
if (now()->minute == 0) {
\Artisan::call('send:invoice-report', [
'schedule_type' => $rule->schedule_type,
'plant' => $rule->plant,
]);
}
break;
case 'Daily':
if (now()->format('H:i') == '15:59') {
\Artisan::call('send:invoice-report', [
'schedule_type' => $rule->schedule_type,
'plant' => $rule->plant,
]);
}
break;
}
}
// --- Invoice Data Report Rules ---
$invoiceDataRules = AlertMailRule::where('module', 'InvoiceDataReport')
->select('plant', 'schedule_type')
->distinct()
->get();
foreach ($invoiceDataRules as $rule) {
switch ($rule->schedule_type) {
case 'Live':
// Run every minute
\Artisan::call('send:invoice-data-report', [
'schedule_type' => $rule->schedule_type,
'plant' => $rule->plant,
]);
break;
case 'Hourly':
if (now()->minute == 0) {
\Artisan::call('send:invoice-data-report', [
'schedule_type' => $rule->schedule_type,
'plant' => $rule->plant,
]);
}
break;
case 'Daily':
if (now()->format('H:i') == '15:59') {
\Artisan::call('send:invoice-data-report', [
'schedule_type' => $rule->schedule_type,
'plant' => $rule->plant,
]);
}
break;
}
}
}
/**
* Helper to call Artisan commands with parameters.
*/
protected function callArtisanCommand($commandName, $rule)
{
\Artisan::call($commandName, [
'schedule_type' => $rule->schedule_type,
'plant' => $rule->plant,
]);
$this->info("Executed {$commandName} for plant: {$rule->plant}");
\Log::info("Executed {$commandName} for plant: {$rule->plant}");
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace App\Console\Commands;
use App\Models\AlertMailRule;
use App\Models\Line;
use App\Models\Plant;
use App\Models\ProductionQuantity;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
class SendDailyProductionReport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:send-daily-production-report';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
// $webhookUrl1 = env('TEAM_URL_RANSAR_I');
// $webhookUrl2 = env('TEAM_URL_RANSAR_II');
$webhookUrls = [
'Ransar Industries-I' => env('TEAM_URL_RANSAR_I'),
'Ransar Industries-II' => env('TEAM_URL_RANSAR_II'),
];
$start = Carbon::yesterday()->setHour(8)->setMinute(0)->setSecond(0);
$end = Carbon::today()->setHour(7)->setMinute(59)->setSecond(59);
foreach ($webhookUrls as $plantName => $webhookUrl)
{
$plant = Plant::where('name', $plantName)->first();
$plantNa = $plant ? $plant->name : $plantName;
$lines = Line::where('plant_id', $plant->id ?? 0)->get();
$tableData = [];
foreach ($lines as $line) {
$count = ProductionQuantity::where('line_id', $line->id)
->whereBetween('created_at', [$start, $end])
->count();
$tableData[] = [
'line_name' => $line->name ?? '',
'production_count' => $count ?? 0
];
}
$table = "| Line Name | Total Production Count |\n";
$table .= "|------------|------------------------|\n";
foreach ($tableData as $row) {
$table .= "| {$row['line_name']} | {$row['production_count']} |\n";
}
$messageText = "**Daily Production Report - {$plantNa}** \n"
. "From: {$start->format('Y-m-d H:i:s')} \n"
. "To: {$end->format('Y-m-d H:i:s')} \n\n"
. $table;
// Send message to Teams
$response = Http::post($webhookUrl, ['text' => $messageText]);
if ($response->successful()) {
$this->info("Message sent successfully for {$plantNa}");
} else {
$this->error("Failed to send message for {$plantNa}");
}
}
}
}

View File

@@ -0,0 +1,239 @@
<?php
namespace App\Console\Commands;
use App\Mail\InvoiceDataMail;
use App\Models\AlertMailRule;
use App\Models\InvoiceDataValidation;
use App\Models\InvoiceOutValidation;
use App\Models\Plant;
use Illuminate\Console\Command;
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()
{
// ini_set('max_execution_time', 0); // disable limit
// set_time_limit(0);
$scheduleType = $this->argument('schedule_type');
$plantId = (int) $this->argument('plant');
$mailRules = AlertMailRule::where('module', 'InvoiceDataReport')
->where('rule_name', 'InvoiceDataMail')
->where('schedule_type', $scheduleType)
->where('plant', $plantId)
->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(10, 0, 0);//8:00
// $endDate = now()->setTime(10, 0, 0);//8
// }
if (strtolower($scheduleType) == 'daily') {
$firstRecord = InvoiceDataValidation::orderBy('document_date', 'asc')->first();
$lastRecord = InvoiceDataValidation::orderBy('document_date', 'desc')->first();
if ($firstRecord && $lastRecord) {
$startDate = \Carbon\Carbon::parse($firstRecord->document_date)->startOfDay();
// $endDate = \Carbon\Carbon::parse($lastRecord->document_date)->endOfDay();
$endDate = \Carbon\Carbon::parse($lastRecord->document_date)
->addDay()
->setTime(10, 0, 0);
} else {
$startDate = now()->startOfDay();
$endDate = now()->endOfDay();
}
} else {
$startDate = now()->setTime(8, 0, 0);
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
}
// ..
foreach ($plants as $plant) {
$tableData = [];
// $distributions = ['Direct Sale', 'Branch Sale', 'Internal Transfer', 'WOS', ''];
$distributions = InvoiceDataValidation::whereNotNull('distribution_channel_desc')
->distinct()
->pluck('distribution_channel_desc')
->filter(fn ($val) => trim($val) != '')
->values()
->toArray();
$distributions[] = '';
foreach ($distributions as $selectedDistribution) {
// where('plant_id', $plant->id)
$invoices = InvoiceDataValidation::where('plant_id', $plant->id)
->where('distribution_channel_desc', $selectedDistribution)
->whereBetween('document_date', [$startDate, $endDate])
->orderBy('document_date', 'asc')
->select('customer_code', 'document_number', 'document_date', 'customer_trade_name', 'customer_location', 'location')
->get()
->unique('document_number')
->values();
if ($invoices->isEmpty()) {
continue;
}
// Filter invoices directly — exclude ones with '-' in document_number
$invoices = $invoices->filter(function ($inv) {
return ! empty($inv->document_number) && ! str_contains($inv->document_number, '-');
});
if (trim($selectedDistribution) == '' || $selectedDistribution == null) {
$invoices = $invoices->filter(function ($inv) {
return str_starts_with($inv->document_number, '7');
});
}
if ($invoices->isEmpty()) {
continue;
}
$invoiceNumbers = $invoices
->pluck('document_number')
->map(fn ($n) => preg_replace('/\s+/', '', strtoupper((string) $n)))
->toArray();
// where('plant_id', $plant->id)
$wentOutInvoices = InvoiceOutValidation::whereIn('qr_code', $invoiceNumbers)
// ->whereBetween('scanned_at', [$startDate, $endDate])
->distinct('qr_code')
->pluck('qr_code')
->map(fn ($n) => preg_replace('/\s+/', '', strtoupper((string) $n)))
->toArray();
// if (!empty($wentOutInvoices)) {
// $deletedValidations = InvoiceDataValidation::whereIn('document_number', $wentOutInvoices)
// ->delete();
// $deletedOuts = InvoiceOutValidation::whereIn('qr_code', $wentOutInvoices)
// ->delete();
// $this->info("Deleted {$deletedValidations} from invoice_data_validations and {$deletedOuts} from invoice_out_validations for plant {$plant->name} ({$selectedDistribution}).");
// }
// $pendingInvoices = $invoices->filter(function ($inv) use ($wentOutInvoices) {
// return !in_array($inv->document_number, $wentOutInvoices);
// });
// $pendingInvoices = $invoices->filter(function ($inv) use ($wentOutInvoices) {
// return !in_array(strtoupper(trim($inv->document_number)), $wentOutInvoices);
// });
$pendingInvoices = $invoices->filter(function ($inv) use ($wentOutInvoices) {
$doc = preg_replace('/\s+/', '', strtoupper((string) $inv->document_number));
return ! in_array($doc, $wentOutInvoices, true);
});
if ($pendingInvoices->isEmpty()) {
continue;
}
foreach ($pendingInvoices as $inv) {
$yesterday = now()->subDay()->toDateString();
$today = now()->toDateString();
$documentDate = \Carbon\Carbon::parse($inv->document_date);
if (in_array($documentDate->toDateString(), [$today, $yesterday])) {
$statusColor = 'status-pending-yellow';
} else {
$statusColor = 'status-pending-red';
}
$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,
'location' => $inv->location,
'no_of_days_pending' => abs((int) now()->diffInDays($documentDate)),
'status' => 'Pending',
'status_class' => $statusColor,
];
}
}
$tableData = collect($tableData)
->sortBy('document_date')
->values()
->map(function ($item, $index) {
$item['no'] = $index + 1;
return $item;
})
->toArray();
$mailSubject = "Despatch Pending Sale Invoice & STO Invoice as on Date ({$plant->name})";
$mail = new InvoiceDataMail($scheduleType, $tableData, $mailSubject);
$contentVars = $mail->content()->with;
$this->info($contentVars['greeting'] ?? 'Invoice Data Report');
$this->table(
['No', 'Plant', 'Customer Code', 'Document Number', 'Document Date', 'Trade Name', 'Location', 'Pending Days', 'Status'],// 'Distribution Type'
$tableData
);
$this->info($contentVars['wishes'] ?? '');
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,217 @@
<?php
namespace App\Console\Commands;
use App\Mail\test;
use App\Models\InvoiceValidation;
use App\Models\Plant;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
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{schedule_type} {plant}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
$schedule = $this->argument('schedule_type');
// $scheduleType = $this->argument('scheduleType');
$plantIdArg = (int) $this->argument('plant'); // can be 0 for all plants
$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);
$serialTableData = [];
$materialTableData = [];
$bundleTableData = [];
// Get plant IDs: either one plant or all
$plantIds = $plantIdArg == 0
? InvoiceValidation::select('plant_id')->distinct()->pluck('plant_id')->toArray()
: [$plantIdArg];
$no = 1;
if (strtolower($schedule) == '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 ($plantIds 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();
$serialInvoiceQuan = InvoiceValidation::where('plant_id', $plantId)
->where('quantity', null)
->whereBetween('created_at', [$startDate, $endDate])
->count();
$scannedInvoiceQuan = InvoiceValidation::where('plant_id', $plantId)
->where('scanned_status', 'Scanned')
->where(function ($query) {
$query->whereNull('quantity')
->orWhere('quantity', 0);
})
->whereBetween('updated_at', [$startDate, $endDate])
->count();
$serialTableData[] = [
'no' => $no,
'plant' => $plantName,
'totalInvoice' => $totalSerialCount,
'scannedInvoice' => $scannedSerialCount,
'totalInvoiceQuan' => $serialInvoiceQuan,
'scannedInvoiceQuan' => $scannedInvoiceQuan,
];
// Material 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();
$totalMatInvoiceQuan = InvoiceValidation::where('plant_id', $plantId)
->where('quantity', 1)
->whereBetween('created_at', [$startDate, $endDate])
->count();
$scannedMatInvoiceQuan = InvoiceValidation::where('plant_id', $plantId)
->where('quantity', 1)
->whereNotNull('serial_number')
->where('serial_number', '!=', '')
->whereBetween('updated_at', [$startDate, $endDate])
->count();
$materialTableData[] = [
'no' => $no,
'plant' => $plantName,
'totalInvoice' => $totalMatCount,
'scannedInvoice' => $scannedMatCount,
'totalInvoiceQuan' => $totalMatInvoiceQuan,
'scannedInvoiceQuan' => $scannedMatInvoiceQuan,
];
// 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();
$totalBundleInvoiceQuan = InvoiceValidation::where('plant_id', $plantId)
->where('quantity', '>', 1)
->whereBetween('created_at', [$startDate, $endDate])
->count();
$scannedBundleInvoiceQuan = InvoiceValidation::where('plant_id', $plantId)
->where('quantity', '>', 1)
->whereNotNull('serial_number')
->where('serial_number', '!=', '')
->whereBetween('updated_at', [$startDate, $endDate])
->count();
$bundleTableData[] = [
'no' => $no,
'plant' => $plantName,
'totalInvoice' => $totalBundleCount,
'scannedInvoice' => $scannedBundleCount,
'totalInvoiceQuan' => $totalBundleInvoiceQuan,
'scannedInvoiceQuan' => $scannedBundleInvoiceQuan,
];
$no++;
}
$mail = new test($serialTableData, $materialTableData, $bundleTableData, $schedule);
$contentVars = $mail->content()->with;
$this->info($contentVars['greeting'] ?? 'Invoice Report');
// 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, [], [], $schedule));
}
}
// 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, $schedule));
}
}
// 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, $schedule));
}
}
// Show preview in console
$this->info('--- Serial Invoice Table ---');
$this->table(['#', 'Plant', 'Total Invoice', 'Scanned Invoice', 'TotalInvoice Quantity', 'ScannedInvoice Quantity'], $serialTableData);
$this->info('--- Material Invoice Table ---');
$this->table(['#', 'Plant', 'Total Invoice', 'Scanned Invoice', 'TotalInvoice Quantity', 'ScannedInvoice Quantity'], $materialTableData);
$this->info('--- Bundle Invoice Table ---');
$this->table(['#', 'Plant', 'Total Invoice', 'Scanned Invoice', 'TotalInvoice Quantity', 'ScannedInvoice Quantity'], $bundleTableData);
$this->info($contentVars['wishes'] ?? '');
}
}

View File

@@ -0,0 +1,145 @@
<?php
namespace App\Console\Commands;
use App\Mail\ProductionMail;
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 SendProductionReport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
// protected $signature = 'send:production-report';
protected $signature = 'send:production-report {schedule_type} {plant}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
// ini_set('max_execution_time', 0); // disable limit
// set_time_limit(0);
$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', 'ProductionQuantities')
->where('rule_name', 'ProductionMail')
->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;
}
// $startDate = now()->setTime(8, 0, 0);
// $endDate = now()->copy()->addDay()->setTime(8, 0, 0);
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);
}
$PlanstartDate = now()->setTime(8, 0, 0);
$planendDate = now()->copy()->addDay()->setTime(7, 59, 0);
$tableData = [];
$no = 1;
// .
foreach ($plants as $plant) {
$lines = Line::where('plant_id', $plant->id)->get();
foreach ($lines as $line) {
$targetQuantity = ProductionPlan::where('plant_id', $plant->id)
->where('line_id', $line->id)
->whereBetween('created_at', [$PlanstartDate, $planendDate])
->sum('plan_quantity');
/** @phpstan-ignore property.notFound */
if (strtolower($line->type) == 'fg line') {
$productionQuantity = \App\Models\QualityValidation::where('plant_id', $plant->id)
->where('line_id', $line->id)
->whereBetween('created_at', [$startDate, $endDate])
->count();
} else {
$productionQuantity = ProductionQuantity::where('plant_id', $plant->id)
->where('line_id', $line->id)
->whereBetween('created_at', [$startDate, $endDate])
->count();
}
$tableData[] = [
'no' => $no++,
'plant' => $plant->name,
'line' => $line->name,
'type' => $line->type,
'targetQuantity' => $targetQuantity,
'productionQuantity' => $productionQuantity,
];
}
}
// $this->table(['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'], $fgTableData);
// $this->table(['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'], $tableData);
// if (!empty($emails)) {
// foreach ($emails as $email) {
// Mail::to($email)->send(new ProductionMail($tableData));
// }
// } else {
// $this->info('No recipients found for ProductionMailAlert.');
// }
// $this->info("Production report sent to " . count($emails) . " recipient(s).");
// Preview in console
$mail = new ProductionMail($scheduleType, $tableData);
$contentVars = $mail->content()->with;
$this->info($contentVars['greeting'] ?? 'Production Report');
$this->table(
['No', 'Plant', 'Line', 'Type', 'Target Quantity', 'Production Quantity'],
$tableData
);
$this->info($contentVars['wishes'] ?? '');
// Send mails
if (! empty($emails)) {
foreach ($emails as $email) {
Mail::to($email)->send(new ProductionMail($scheduleType, $tableData));
}
$this->info('Production report sent to '.count($emails).' recipient(s).');
} else {
$this->warn('No recipients found for ProductionMailAlert.');
}
}
}