diff --git a/app/Filament/Pages/ProductionTarget.php b/app/Filament/Pages/ProductionTarget.php index 881730a9..aa2253db 100644 --- a/app/Filament/Pages/ProductionTarget.php +++ b/app/Filament/Pages/ProductionTarget.php @@ -159,7 +159,7 @@ class ProductionTarget extends Page $month = (int) $get('month'); $category = $get('category'); - $this->dispatch('loadData',$year, $month, $plantId, $lineId, $category); + }), ]) @@ -217,7 +217,21 @@ class ProductionTarget extends Page $lineId = $this->filters['line_id'] ?? null; $year = $this->filters['year'] ?? null; $month = (int) $this->filters['month'] ?? null; - $this->dispatch('loadData',$year, $month, $plantId, $lineId, ''); + $category = $this->filters['category'] ?? null; + if($category == '' || $category == null){ + // $this->dispatch('loadData',$year, $month, $plantId, $lineId, ''); + $this->dispatch( + 'loadData', + (int) $year, + (int) $month, + (int) $plantId, + (int) $lineId + ); + } + else{ + $this->dispatch('loadDataCategory',$year, $month, $plantId, $lineId, $category); + } + } public static function canAccess(): bool diff --git a/app/Livewire/ProductionTargetPlan.php b/app/Livewire/ProductionTargetPlan.php index 44e1be74..295d48e3 100644 --- a/app/Livewire/ProductionTargetPlan.php +++ b/app/Livewire/ProductionTargetPlan.php @@ -11,6 +11,7 @@ use Livewire\Component; use Carbon\Carbon; use DB; use Maatwebsite\Excel\Facades\Excel; +use Livewire\Attributes\On; class ProductionTargetPlan extends Component { @@ -34,6 +35,7 @@ class ProductionTargetPlan extends Component protected $listeners = [ 'loadData' => 'loadProductionData', 'loadData1' => 'exportProductionData', + 'loadDataCategory' => 'loadProductionCategoryData' ]; public function getMonthDates($month, $year) @@ -454,6 +456,7 @@ class ProductionTargetPlan extends Component + #[On('loadData')] public function loadProductionData($year, $month, $plantId, $lineId) { @@ -536,8 +539,6 @@ class ProductionTargetPlan extends Component ) ->map(fn($group) => $group->keyBy('prod_date')); - Log::info('Quality: ' . ((microtime(true) - $start) * 1000) . ' ms'); - $records = []; foreach ($plans as $plan) { @@ -658,6 +659,165 @@ class ProductionTargetPlan extends Component //$this->monthlyRecords = $plans->sum('plan_quantity'); } + + public function loadProductionCategoryData($year, $month, $plantId, $lineId, $category) + { + + $this->records = []; + $this->monthlyRecords = 0; + $this->dates = []; + $this->leaveDates = []; + + if (!$plantId || !$lineId || !$month || !$year || !$category) { + // $this->records = []; + // $this->dates = []; + // $this->leaveDates = []; + return; + } + + $dates = $this->getMonthDates($month, $year); + $this->dates = $dates; + + $plans = ProductionPlan::query() + ->join('items', 'items.id', '=', 'production_plans.item_id') + ->join('lines', 'lines.id', '=', 'production_plans.line_id') + ->join('plants', 'plants.id', '=', 'production_plans.plant_id') + ->where('production_plans.plant_id', $plantId) + ->where('production_plans.line_id', $lineId) + ->where('items.category', $category) + ->whereMonth('production_plans.created_at', $month) + ->whereYear('production_plans.created_at', $year) + ->select( + 'production_plans.item_id', + 'production_plans.plant_id', + 'production_plans.line_id', + 'production_plans.plan_quantity', + 'production_plans.working_days', + 'production_plans.leave_dates', + 'items.code as item_code', + 'items.category as category', + 'items.description as item_description', + 'items.hourly_quantity as hourly_quantity', + 'lines.name as line_name', + // 'lines.line_capacity as line_capacity', + 'plants.name as plant_name' + ) + ->get(); + + $leaveDates = []; + + if ($plans->isNotEmpty() && $plans[0]->leave_dates) { + $leaveDates = array_map('trim', explode(',', $plans[0]->leave_dates)); + } + + $this->leaveDates = $leaveDates; + + $producedData = QualityValidation::selectRaw(" + quality_validations.plant_id, + quality_validations.line_id, + sticker_masters.item_id, + DATE(quality_validations.created_at) as prod_date, + COUNT(*) as total_qty + ") + ->join( + 'sticker_masters', + 'sticker_masters.id', + '=', + 'quality_validations.sticker_master_id' + ) + ->join('items', 'items.id', '=', 'sticker_masters.item_id') + ->where('items.category', $category) + ->where('quality_validations.plant_id', $plantId) + ->where('quality_validations.line_id', $lineId) + ->whereMonth('quality_validations.created_at', $month) + ->whereYear('quality_validations.created_at', $year) + ->groupBy( + 'quality_validations.plant_id', + 'quality_validations.line_id', + 'sticker_masters.item_id', + DB::raw('DATE(quality_validations.created_at)') + ) + ->get() + ->groupBy(fn($row) => + $row->plant_id . '_' . $row->line_id . '_' . $row->item_id + ) + ->map(fn($group) => $group->keyBy('prod_date')); + + $records = []; + + foreach ($plans as $plan) { + + $row = $plan->toArray(); + + $workingDays = (int) ($row['working_days'] ?? 0); + + $normalDailyTarget = $workingDays > 0 + ? round((float) $row['plan_quantity'] / $workingDays, 2) + : 0; + + $pendingQty = 0; + + $lineCapacity = (float) ($row['line_capacity'] ?? 0); + $dailyHourlyQuantity = (float) ($row['hourly_quantity'] ?? 0); + + $row['category'] = $row['category'] ?? '-'; + $row['daily_hourly_quantity'] = []; + $row['daily_target_dynamic'] = []; + $row['produced_quantity'] = []; + + $key = $row['plant_id'] . '_' . $row['line_id'] . '_' . $row['item_id']; + + foreach ($dates as $date) { + if (in_array($date, $leaveDates)) { + + $row['daily_hourly_quantity'][$date] = '-'; + $row['daily_target_dynamic'][$date] = '-'; + $row['produced_quantity'][$date] = '-'; + + continue; + } + + $todayTarget = round( + $normalDailyTarget + $pendingQty, + 2 + ); + + $producedQty = $producedData[$key][$date]->total_qty ?? 0; + + $row['daily_target_dynamic'][$date] = $todayTarget; + $row['produced_quantity'][$date] = $producedQty; + $row['daily_hourly_quantity'][$date] = $dailyHourlyQuantity * 22.5; + + $pendingQty = max( + 0, + $todayTarget - $producedQty + ); + } + + $records[] = $row; + } + + $this->records = $records; + + $this->monthlyRecords = ProductionPlan::where('plant_id', $plantId) + ->where('line_id', $lineId) + ->whereYear('created_at', $year) + ->whereMonth('created_at', $month) + ->sum('plan_quantity'); + + $this->monthlyActualQuantity = QualityValidation::where('plant_id', $plantId) + ->where('line_id', $lineId) + ->whereYear('created_at', $year) + ->whereMonth('created_at', $month) + ->count(); + + $this->efficiency = $this->monthlyRecords > 0 + ? ($this->monthlyActualQuantity / $this->monthlyRecords) * 100 + : 0; + + //$this->monthlyRecords = $plans->sum('plan_quantity'); + } + public function exportProductionData() { return Excel::download(