@if(($production ?? collect())->isEmpty())
No brick production data found for the selected filters.
@else
| Date |
Brick Type |
Cement Used (Bags) |
Good Bricks |
Expected Output |
Variance |
Status |
@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
| {{ optional($prod->created_at)->format('d M Y') }} |
Not Recorded |
{{ number_format($cementUsed) }} |
{{ number_format($good) }} |
{{ number_format($expected) }} |
0 |
{{ $status }}
|
@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)
|
{{ optional($prod->created_at)->format('d M Y') }}
|
@endif
{{ $brickTypeName }} |
{{-- Cement used shown once per stock --}}
@if($i === 0)
{{ number_format($cementUsed) }}
|
@endif
{{ number_format($good) }} |
{{ number_format($expected) }} |
{{ $variance >= 0 ? '+' : '' }}{{ number_format($variance) }}
|
{{ $status }}
|
@endforeach
@endif
@endforeach
@php
$totalVariance = $totalGoodBricks - $totalExpected;
@endphp
| 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