From 30e7fe9a3f0a4697e147dcba3f7be85377b37437 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 3 Nov 2026 11:50:29 +0530 Subject: [PATCH] Added day plan target import page --- app/Imports/DayPlanTargetImport.php | 188 ++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 app/Imports/DayPlanTargetImport.php diff --git a/app/Imports/DayPlanTargetImport.php b/app/Imports/DayPlanTargetImport.php new file mode 100644 index 00000000..85900033 --- /dev/null +++ b/app/Imports/DayPlanTargetImport.php @@ -0,0 +1,188 @@ +plantId = $plantId; + $this->lineId = $lineId; + $this->year = $year; + $this->month = $month; + $this->week = strtolower($week); + } + + + public function collection(Collection $rows) + { + $weekMapping = [ + 'week_1' => 1, + 'week_2' => 8, + 'week_3' => 15, + 'week_4' => 22, + 'week_5' => 29, + ]; + + if (!isset($weekMapping[$this->week])) { + throw new Exception('Invalid week selected.'); + } + + $startDay = $weekMapping[$this->week]; + + $daysInMonth = cal_days_in_month( + CAL_GREGORIAN, + (int) $this->month, + (int) $this->year + ); + + $maxDays = min(7, $daysInMonth - $startDay + 1); + + if ($maxDays <= 0) { + throw new Exception('Selected week is outside the selected month.'); + } + + DB::transaction(function () use ($rows, $startDay, $maxDays) { + + // Get calendar leave dates + $calendar = ProductionCalendar::where('plant_id', $this->plantId) + ->whereMonth('created_at', $this->month) + ->whereYear('created_at', $this->year) + ->first(); + + $leaveDates = $calendar->leave_dates ?? []; + + if (is_string($leaveDates)) { + $leaveDates = explode(',', $leaveDates); + } + + $leaveDates = collect($leaveDates) + ->map(fn ($date) => trim((string) $date)) + ->filter() + ->map(fn ($date) => Carbon::parse($date)->format('Y-m-d')) + ->toArray(); + + foreach ($rows as $index => $row) { + + $excelValues = array_values($row->toArray()); + + // First two columns + $plantCode = trim((string) ($excelValues[0] ?? '')); + $itemCode = trim((string) ($excelValues[1] ?? '')); + + if ($plantCode === '' || $itemCode === '') { + throw new Exception( + 'Plant Code or Item Code missing in Excel row ' . ($index + 2) + ); + } + + $plant = Plant::where('code', $plantCode)->first(); + + if (!$plant) { + throw new Exception("Plant Code {$plantCode} not found."); + } + + $item = Item::where('code', $itemCode) + ->where('plant_id', $plant->id) + ->first(); + + if (!$item) { + throw new Exception( + "Item Code {$itemCode} not found for Plant Code {$plantCode}." + ); + } + + $plan = ProductionPlan::where('plant_id', $plant->id) + ->where('item_id', $item->id) + ->whereMonth('created_at', $this->month) + ->whereYear('created_at', $this->year) + ->first(); + + if (!$plan) { + throw new Exception( + "Production plan not found for Item Code: {$itemCode}" + ); + } + + // Remaining columns contain daily quantities + $dayValues = array_slice($excelValues, 2); + + $updateData = []; + + for ($i = 0; $i < $maxDays; $i++) { + + $databaseDay = $startDay + $i; + + $date = Carbon::create( + $this->year, + $this->month, + $databaseDay + )->format('Y-m-d'); + + // Leave date: always zero + if (in_array($date, $leaveDates)) { + $updateData["day_{$databaseDay}"] = 0; + continue; + } + + $quantity = $dayValues[$i] ?? null; + + // Blank quantity: do not update + if ($quantity === null || trim((string) $quantity) === '') { + continue; + } + + if (!is_numeric($quantity) || $quantity < 0) { + throw new Exception( + "Invalid quantity for Plant {$plantCode}, Item {$itemCode}, Day {$databaseDay}." + ); + } + + $updateData["day_{$databaseDay}"] = $quantity; + } + + if (!empty($updateData)) { + $plan->update($updateData); + + // Refresh the latest database values + $plan->refresh(); + + // Calculate total of day_1 to day_31 + $totalPlanQuantity = 0; + + for ($day = 1; $day <= 31; $day++) { + + $totalPlanQuantity += (float) ( + $plan->{"day_{$day}"} ?? 0 + ); + } + + // Update monthly plan quantity + $plan->update([ + 'plan_quantity' => $totalPlanQuantity, + ]); + } + } + }); + } + +} -- 2.49.1