diff --git a/app/Console/Commands/SendProductionReport.php b/app/Console/Commands/SendProductionReport.php index ca6689b..c7acbd3 100644 --- a/app/Console/Commands/SendProductionReport.php +++ b/app/Console/Commands/SendProductionReport.php @@ -3,6 +3,7 @@ namespace App\Console\Commands; use App\Mail\ProductionMail; +use App\Models\Item; use App\Models\Line; use App\Models\Plant; use App\Models\ProductionPlan; @@ -75,59 +76,211 @@ class SendProductionReport extends Command // . + // foreach ($plants as $plant) { + // $lines = Line::where('plant_id', $plant->id)->get(); + + // foreach ($lines as $line) { + + // $month = $startDate->month; + // $year = $startDate->year; + + // $workingDays = ProductionPlan::where('plant_id', $plantId) + // ->whereMonth('created_at', $month) + // ->whereYear('created_at', $year) + // ->value('working_days') ?? 0; + + // $totalTargetQuantity = 0; + + // $monthlyPlan = ProductionPlan::where('plant_id', $plantId) + // ->where('line_id', $line->id) + // ->whereMonth('created_at', $month) + // ->whereYear('created_at', $year) + // ->sum('plan_quantity'); // / $workingDays + + // $dailyTarget = $workingDays > 0 + // ? $monthlyPlan / $workingDays + // : 0; + + // $dailyTarget = round($dailyTarget, 2); + + // // $totalTargetQuantity = round($totalTargetQuantity, 2); + + // $monthStart = $startDate->copy()->startOfMonth(); + // $completedDays = $monthStart->diffInDays($startDate); + + // $expectedTillYesterday = $dailyTarget * $completedDays; + + // 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(); + // } + + // $previousRemaining = max(0, $expectedTillYesterday - $productionQuantity); + + // $totalTargetQuantity = round($dailyTarget + $previousRemaining, 2); + + // $tableData[] = [ + // 'no' => $no++, + // 'plant' => $plant->name, + // 'line' => $line->name, + // 'type' => $line->type, + // 'targetQuantity' => $totalTargetQuantity, + // 'productionQuantity' => $productionQuantity, + // ]; + // } + // } + + + //..New Logic + foreach ($plants as $plant) { + $lines = Line::where('plant_id', $plant->id)->get(); foreach ($lines as $line) { - $targetQuantity = ProductionPlan::where('plant_id', $plant->id) + + $month = $startDate->month; + $year = $startDate->year; + + $workingDays = ProductionPlan::where('plant_id', $plant->id) ->where('line_id', $line->id) - ->whereBetween('created_at', [$PlanstartDate, $planendDate]) + ->whereMonth('created_at', $month) + ->whereYear('created_at', $year) + ->value('working_days') ?? 0; + + $monthlyPlan = ProductionPlan::where('plant_id', $plant->id) + ->where('line_id', $line->id) + ->whereMonth('created_at', $month) + ->whereYear('created_at', $year) ->sum('plan_quantity'); - /** @phpstan-ignore property.notFound */ + $dailyTarget = $workingDays > 0 ? $monthlyPlan / $workingDays : 0; + + $dailyTarget = round($dailyTarget, 2); + + $leaveDatesString = ProductionPlan::where('plant_id', $plant->id) + ->where('line_id', $line->id) + ->whereMonth('created_at', $month) + ->whereYear('created_at', $year) + ->value('leave_dates'); + + $leaveDates = $leaveDatesString ? explode(',', $leaveDatesString) : []; + + $monthStart = $startDate->copy()->startOfMonth(); + $yesterday = $startDate->copy()->subDay(); + + $completedDays = 0; + $currentDate = $monthStart->copy(); + + while ($currentDate->lte($yesterday)) { + + if (!in_array($currentDate->format('Y-m-d'), $leaveDates)) { + $completedDays++; + } + + $currentDate->addDay(); + } + + $expectedTillYesterday = $dailyTarget * $completedDays; + if (strtolower($line->type) == 'fg line') { + + $producedTillYesterday = \App\Models\QualityValidation::where('plant_id', $plant->id) + ->where('line_id', $line->id) + ->whereMonth('created_at', $month) + ->whereYear('created_at', $year) + ->where('created_at', '<', $startDate) + ->count(); + + } else { + + $producedTillYesterday = ProductionQuantity::where('plant_id', $plant->id) + ->where('line_id', $line->id) + ->whereMonth('created_at', $month) + ->whereYear('created_at', $year) + ->where('created_at', '<', $startDate) + ->count(); + } + + $previousRemaining = max(0, $expectedTillYesterday - $producedTillYesterday); + + $totalTargetQuantity = round($dailyTarget + $previousRemaining, 2); + + $itemIds = ProductionPlan::where('plant_id', $plant->id) + ->where('line_id', $line->id) + ->whereMonth('created_at', $month) + ->whereYear('created_at', $year) + ->distinct() + ->pluck('item_id'); + + $totalHourlyQuantity = Item::whereIn('id', $itemIds) + ->sum('hourly_quantity'); + + $capacityQuan = $totalHourlyQuantity * 22.5; + + 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(); } + if ($capacityQuan > 0) { + $efficiency = ($productionQuantity / $capacityQuan) * 100; + } else { + $efficiency = 0; + } + + $efficiency = round($efficiency, 2); + $tableData[] = [ 'no' => $no++, 'plant' => $plant->name, - 'line' => $line->name, 'type' => $line->type, - 'targetQuantity' => $targetQuantity, + 'line' => $line->name, + 'capacityQuantity' => floor($capacityQuan), + 'targetQuantity' => floor($totalTargetQuantity), 'productionQuantity' => $productionQuantity, + 'efficiency' => $efficiency . '%', ]; } } - // $this->table(['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'], $fgTableData); + // $service = new \App\Services\ProductionTargetService(); - // $this->table(['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'], $tableData); + // $year = $startDate->year; + // $month = $startDate->month; - // if (!empty($emails)) { - // foreach ($emails as $email) { - // Mail::to($email)->send(new ProductionMail($tableData)); - // } - // } else { - // $this->info('No recipients found for ProductionMailAlert.'); - // } + // [$records, $dates] = $service->generate( + // $plantId, + // $year, + // $month, + // ); - // $this->info("Production report sent to " . count($emails) . " recipient(s)."); - // Preview in console $mail = new ProductionMail($scheduleType, $tableData); + + // $mail = new ProductionMail($scheduleType,$tableData,$records,$dates); + $contentVars = $mail->content()->with; $this->info($contentVars['greeting'] ?? 'Production Report'); $this->table( - ['No', 'Plant', 'Line', 'Type', 'Target Quantity', 'Production Quantity'], + ['No', 'Plant', 'Line', 'Type', 'Capacity Quantity', 'Target Quantity', 'Production Quantity'], $tableData ); $this->info($contentVars['wishes'] ?? ''); diff --git a/resources/views/mail/production_report.blade.php b/resources/views/mail/production_report.blade.php index c7ea15c..9267994 100644 --- a/resources/views/mail/production_report.blade.php +++ b/resources/views/mail/production_report.blade.php @@ -61,9 +61,11 @@ No Plant Line - Target Quantity Line Type + Capacity Quantity + Target Quantity Production Quantity + OverAll Efficiency(%) @@ -72,9 +74,11 @@ {{ $row['no'] }} {{ $row['plant'] }} {{ $row['line'] }} - {{ $row['targetQuantity'] }} {{ $row['type'] }} + {{ $row['capacityQuantity'] }} + {{ $row['targetQuantity'] }} {{ $row['productionQuantity'] }} + {{ $row['efficiency'] }} @endforeach