Added non planned production report command page
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 18s
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 18s
This commit is contained in:
212
app/Console/Commands/SendNonPlannedProductionReport.php
Normal file
212
app/Console/Commands/SendNonPlannedProductionReport.php
Normal file
@@ -0,0 +1,212 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Mail\NonPlanProductionMail;
|
||||
use App\Models\Line;
|
||||
use App\Models\Plant;
|
||||
use App\Models\ProductionPlan;
|
||||
use App\Models\ProductionQuantity;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class SendNonPlannedProductionReport extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'send:non-planned-production-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', 'ProductionNonPlanQuantities')
|
||||
->where('rule_name', 'NonPlanProductionMail')
|
||||
->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) {
|
||||
|
||||
$lines = Line::where('plant_id', $plant->id)
|
||||
->whereIn('type', [
|
||||
'Base FG Line',
|
||||
'FG Line',
|
||||
'Process FG Line',
|
||||
'SFG Line',
|
||||
])
|
||||
->orderByRaw("
|
||||
CASE
|
||||
WHEN LOWER(type) = 'fg line' THEN 0
|
||||
WHEN LOWER(type) = 'base fg line' THEN 1
|
||||
WHEN LOWER(type) = 'process fg line' THEN 2
|
||||
WHEN LOWER(type) = 'sfg line' THEN 3
|
||||
ELSE 4
|
||||
END
|
||||
")
|
||||
->get();
|
||||
|
||||
$month = $startDate->month;
|
||||
$year = $startDate->year;
|
||||
|
||||
foreach ($lines as $line) {
|
||||
|
||||
$dayproductionQuantity = 0;
|
||||
$nightproductionQuantity = 0;
|
||||
$nonPlannedproductionQuantity = 0;
|
||||
$totalActualProduction = 0;
|
||||
|
||||
$itemIds = ProductionPlan::where('plant_id', $plant->id)
|
||||
->where('line_id', $line->id)
|
||||
->whereMonth('created_at', $month)
|
||||
->whereYear('created_at', $year)
|
||||
->distinct()
|
||||
->pluck('item_id');
|
||||
|
||||
if (strtolower($line->type) == 'fg line') {
|
||||
|
||||
$actualTotalQuantity = \App\Models\QualityValidation::where('plant_id', $plant->id)
|
||||
->where('line_id', $line->id)
|
||||
->whereBetween('created_at', [$startDate, $endDate])
|
||||
->count();
|
||||
|
||||
$totalActualProduction = $actualTotalQuantity;
|
||||
|
||||
$nonPlannedproductionQuantity = \App\Models\QualityValidation::where('plant_id', $plant->id)
|
||||
->where('line_id', $line->id)
|
||||
->whereHas('stickerMaster', function ($query) use ($itemIds) {
|
||||
$query->whereNotIn('item_id', $itemIds);
|
||||
})
|
||||
->whereBetween('created_at', [$startDate, $endDate])
|
||||
->count();
|
||||
|
||||
$nonPlannedPercentage = $totalActualProduction > 0
|
||||
? round(($nonPlannedproductionQuantity / $totalActualProduction) * 100, 2)
|
||||
: 0;
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
$nonPlannedproductionQuantity = ProductionQuantity::where('plant_id', $plant->id)
|
||||
->where('line_id', $line->id)
|
||||
->whereBetween('created_at', [$startDate, $endDate])
|
||||
->count();
|
||||
}
|
||||
|
||||
if (strtolower($line->type) == 'fg line') {
|
||||
|
||||
$dayproductionQuantity = \App\Models\QualityValidation::where('plant_id', $plant->id)
|
||||
->where('line_id', $line->id)
|
||||
->whereHas('stickerMaster', function ($query) use ($itemIds) {
|
||||
$query->whereNotIn('item_id', $itemIds);
|
||||
})
|
||||
->whereBetween('created_at', [
|
||||
$startDate->copy()->setTime(8, 0, 0),
|
||||
$startDate->copy()->setTime(19, 30, 0),
|
||||
])
|
||||
->count();
|
||||
|
||||
$nightproductionQuantity = \App\Models\QualityValidation::where('plant_id', $plant->id)
|
||||
->where('line_id', $line->id)
|
||||
->whereHas('stickerMaster', function ($query) use ($itemIds) {
|
||||
$query->whereNotIn('item_id', $itemIds);
|
||||
})
|
||||
->whereBetween('created_at', [
|
||||
$startDate->copy()->setTime(19, 30, 0),
|
||||
$endDate->copy()->setTime(8, 0, 0),
|
||||
])
|
||||
->count();
|
||||
}
|
||||
|
||||
$tableData[] = [
|
||||
'no' => $no++,
|
||||
'plant' => $plant->name,
|
||||
'type' => $line->type,
|
||||
'line' => $line->name,
|
||||
'dayShiftActualQuantity' => floor($dayproductionQuantity),
|
||||
'nightShiftActualQuantity' => floor($nightproductionQuantity),
|
||||
'uom' => $line->uom,
|
||||
'totalActualQuantity' => $totalActualProduction,
|
||||
'nonPlannedQuantity' => $nonPlannedproductionQuantity,
|
||||
'nonPlannedPercentage' => $nonPlannedPercentage,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$mail = new NonPlanProductionMail($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'] ?? '');
|
||||
|
||||
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) : ''));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user