@extends('layouts.main') @section('content')

Brick Production Report

Filter Production Report
{{-- FILTER FIELDS ROW --}}
{{-- ✅ NEW: Work Station --}}
{{-- ACTIONS ROW (perfect alignment) --}}
Reset
{{-- Export route should exist --}} Export
@if(($production ?? collect())->isEmpty())
No brick production data found for the selected filters.
@else
Brick Production Summary ({{ $startDate->format('d M Y') }} - {{ $endDate->format('d M Y') }}) @if(request()->filled('work_station_id')) | Work Station: {{ optional(($workStations ?? collect())->firstWhere('id', (int)request('work_station_id')))->name ?? request('work_station_id') }} @else | All Work Stations @endif
@php $totalCement = 0; $totalGoodBricks = 0; $totalExpected = 0; @endphp @foreach($production as $prod) @php $cementUsed = (int) ($prod->total ?? 0); // ✅ Use eager loaded relationship (NO extra queries) $stockBricks = collect($prod->stockBricks ?? []); // If filter selected, keep only that type if (request()->filled('brick_type_id')) { $stockBricks = $stockBricks->where('brick_type_id', (int) request('brick_type_id')); } // Remove empty ones $stockBricks = $stockBricks->filter(function ($sb) { return (int)($sb->actual_output ?? 0) > 0 || (int)($sb->expected_output ?? 0) > 0 || (int)($sb->total_bricks_without_fault ?? 0) > 0; })->values(); // If none remain, show ONE row with "Not Recorded" $rowspan = max(1, $stockBricks->count()); $totalCement += $cementUsed; $sumGood = (int) $stockBricks->sum(fn($sb) => (int)($sb->total_bricks_without_fault ?? 0)); $sumExpected = (int) $stockBricks->sum(fn($sb) => (int)($sb->expected_output ?? 0)); $totalGoodBricks += $sumGood; $totalExpected += $sumExpected; @endphp @if($stockBricks->isEmpty()) @php $good = 0; $expected = 0; $variance = 0; $status = 'Missing Output'; @endphp @else @foreach($stockBricks as $i => $sb) @php $good = (int) ($sb->total_bricks_without_fault ?? 0); $expected = (int) ($sb->expected_output ?? 0); $variance = $good - $expected; $status = ($expected > 0 && $good == 0) ? 'Missing Output' : ($good < $expected ? 'Below Expectation' : 'Met Target'); $brickTypeName = $sb->brickType->name ?? 'Unknown'; @endphp {{-- Date shown once per stock --}} @if($i === 0) @endif {{-- Cement used shown once per stock --}} @if($i === 0) @endif @endforeach @endif @endforeach @php $totalVariance = $totalGoodBricks - $totalExpected; @endphp
Date Brick Type Cement Used (Bags) Good Bricks Expected Output Variance Status
{{ optional($prod->created_at)->format('d M Y') }} Not Recorded {{ number_format($cementUsed) }} {{ number_format($good) }} {{ number_format($expected) }} 0 {{ $status }}
{{ optional($prod->created_at)->format('d M Y') }} {{ $brickTypeName }} {{ number_format($cementUsed) }} {{ number_format($good) }} {{ number_format($expected) }} {{ $variance >= 0 ? '+' : '' }}{{ number_format($variance) }} {{ $status }}
TOTALS - {{ number_format($totalCement) }} {{ number_format($totalGoodBricks) }} {{ number_format($totalExpected) }} {{ $totalVariance >= 0 ? '+' : '' }}{{ number_format($totalVariance) }} {{ $totalGoodBricks >= $totalExpected ? 'Target Achieved' : 'Below Target' }}
@endif
@endsection @section('scripts') @endsection