From 207c92b569978364a5c3c302727b7ce3fd50d192 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 13 Oct 2026 14:25:30 +0530 Subject: [PATCH] Added non planned production report command page --- .../SendNonPlannedProductionReport.php | 212 ++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 app/Console/Commands/SendNonPlannedProductionReport.php diff --git a/app/Console/Commands/SendNonPlannedProductionReport.php b/app/Console/Commands/SendNonPlannedProductionReport.php new file mode 100644 index 00000000..6eb7e872 --- /dev/null +++ b/app/Console/Commands/SendNonPlannedProductionReport.php @@ -0,0 +1,212 @@ +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) : '')); + } + } +}