@extends('layouts.admin') @push('styles') @endpush @section('content')
Refresh

Monthly Expense & Purchase Report

@php // Create a combined array of all unique year-month keys $keys = []; foreach ($saleAmounts as $row) { $keys[$row->year . '-' . $row->month] = true; } foreach ($dir_exp as $row) { $keys[$row->year . '-' . $row->month] = true; } foreach ($inDir_exp as $row) { $keys[$row->year . '-' . $row->month] = true; } foreach ($purchase as $row) { $keys[$row->year . '-' . $row->month] = true; } @endphp @foreach(array_keys($keys) as $key) @php [$y, $m] = explode('-', $key); $sale = $saleAmounts->firstWhere(fn($r) => $r->year == $y && $r->month == $m); $dir = $dir_exp->firstWhere(fn($r) => $r->year == $y && $r->month == $m); $indir = $inDir_exp->firstWhere(fn($r) => $r->year == $y && $r->month == $m); $pur = $purchase->firstWhere(fn($r) => $r->year == $y && $r->month == $m); $saleTotal = $sale->total ?? 0; $purchaseTotal = $pur->total ?? 0; $expenseTotal = ($dir->amount ?? 0) + ($indir->amount ?? 0); $purchasePercent = $saleTotal > 0 ? ($purchaseTotal / $saleTotal) * 100 : 0; $expensePercent = $saleTotal > 0 ? ($expenseTotal / $saleTotal) * 100 : 0; @endphp @endforeach
@php $totalSale = $saleAmounts->sum('total'); $totalPurchase = $purchase->sum('total'); $totalExpense = $dir_exp->sum('amount') + $inDir_exp->sum('amount'); $totalPurchasePercent = $totalSale > 0 ? ($totalPurchase / $totalSale) * 100 : 0; $totalExpensePercent = $totalSale > 0 ? ($totalExpense / $totalSale) * 100 : 0; @endphp
Year Month Sale Amount Purchase Total Purchase (%) Expense Expense (%)
{{ $y }} {{ DateTime::createFromFormat('!m', $m)->format('F') }} {{ number_format($sale->total ?? 0, 2) }} {{ number_format($pur->total ?? 0, 2) }} {{ number_format($purchasePercent, 2) }}% {{ number_format(($dir->amount ?? 0) + ($indir->amount ?? 0), 2) }} {{ number_format($expensePercent, 2) }}%
Total {{ number_format($saleAmounts->sum('total'), 2) }} {{ number_format($purchase->sum('total'), 2) }} {{ number_format($totalPurchasePercent, 2) }}% {{ number_format($dir_exp->sum('amount') + $inDir_exp->sum('amount'), 2) }} {{ number_format($totalExpensePercent, 2) }}%
@endsection @push('scripts') @endpush