@extends('layouts.finance')
@section('title', 'Net Profit Detail')
@section('subtitle', 'Filtered P&L after voucher cost, redeemed point deductions, and expenses')
@section('content')
@php
$displayCurrency = 'PKR';
$fmt = fn($usd) => $metrics->moneyIn($usd, 'USD', $displayCurrency);
$filters = $filters ?? ['mode' => 'month', 'month' => now()->format('Y-m'), 'from' => now()->startOfMonth()->toDateString(), 'to' => now()->endOfMonth()->toDateString()];
$rangeLabel = $filters['mode'] === 'date' ? $filters['from'].' to '.$filters['to'] : \Illuminate\Support\Carbon::createFromFormat('Y-m', $filters['month'])->format('F Y');
$itemFilterParams = [
'mode' => $filters['mode'],
'month' => $filters['month'],
'from' => $filters['from'],
'to' => $filters['to'],
];
$profit = $kpis['profit'];
$revenue = $kpis['revenue'];
$expenses = $kpis['expenses'];
$margin = $kpis['margin'];
$isPositive = $profit >= 0;
$profitDetails = $profitDetails ?? ['summary' => [], 'products' => collect(), 'recent_orders' => collect()];
$profitSummary = $profitDetails['summary'] ?? [];
$productProfitRows = collect($profitDetails['products'] ?? []);
$recentProfitOrders = collect($profitDetails['recent_orders'] ?? []);
$pointDetails = $profitDetails['point_details'] ?? ['summary' => [], 'by_item_user' => collect(), 'recent' => collect(), 'source' => null];
$pointSummary = $pointDetails['summary'] ?? [];
$recentPointRows = collect($pointDetails['recent'] ?? []);
$pointValue = fn($origin, $kind) => (float) ($pointSummary[$origin][$kind] ?? 0);
$sourceCurrency = $profitSummary['currency'] ?? 'PKR';
$voucherCogsUsd = $metrics->usd((float)($profitSummary['cogs'] ?? 0), $sourceCurrency);
$issuedPointsUsd = $metrics->usd((float)($profitSummary['issued_points'] ?? 0), $sourceCurrency);
$pointsUsd = $metrics->usd((float)($profitSummary['points'] ?? 0), $sourceCurrency);
$issuedPointsBreakdown = collect($profitSummary['issued_points_breakdown'] ?? [])
->map(fn ($amount) => $metrics->usd((float) $amount, $sourceCurrency));
$pointsBreakdown = collect($profitSummary['points_breakdown'] ?? [])
->map(fn ($amount) => $metrics->usd((float) $amount, $sourceCurrency));
$quarterlyIssuedPointsUsd = (float) ($issuedPointsBreakdown['quarterly'] ?? 0);
$yearlyIssuedPointsUsd = (float) ($issuedPointsBreakdown['yearly'] ?? 0);
$quarterlyPointsUsd = (float) ($pointsBreakdown['quarterly'] ?? 0);
$yearlyPointsUsd = (float) ($pointsBreakdown['yearly'] ?? 0);
$grossProfitBeforePoints = $revenue - $voucherCogsUsd;
$profitAfterPoints = $grossProfitBeforePoints - $pointsUsd;
$expCatLabels = $expByCategory->keys()->toArray();
$expCatValues = $expByCategory->values()->toArray();
@endphp
{{-- Back link --}}
Filters
{{ $rangeLabel }}
Profit by Item
{{ $rangeLabel }}
| Item |
Sold Voucher |
Buying Price |
Sale Price |
Conversion Rate |
Buying / Voucher |
Sale After Points / Voucher |
Issued Points |
Redeemed Points |
Profit |
@forelse($productProfitRows as $row)
@php
$rowSoldQty = (float) ($row['sold_qty'] ?? 0);
$rowAvgBuying = (float) ($row['buying_price_per_voucher'] ?? ($rowSoldQty > 0 ? ((float) ($row['cogs'] ?? 0) / $rowSoldQty) : 0));
$rowSaleAfterPoints = (float) ($row['sale_after_points_per_voucher'] ?? ($rowSoldQty > 0 ? (((float) ($row['revenue'] ?? 0) - (float) ($row['points'] ?? 0)) / $rowSoldQty) : 0));
$rowRevenue = $metrics->usd($row['revenue'], 'PKR');
$rowCogs = $metrics->usd($row['cogs'], 'PKR');
$rowConversionRate = (float) ($row['conversion_rate'] ?? 0);
$rowAvgBuyingUsd = $metrics->usd($rowAvgBuying, 'PKR');
$rowSaleAfterPointsUsd = $metrics->usd($rowSaleAfterPoints, 'PKR');
$rowIssuedPoints = $metrics->usd((float) ($row['issued_points'] ?? 0), 'PKR');
$rowUsedPoints = $metrics->usd((float) ($row['points'] ?? 0), 'PKR');
$rowProfit = $metrics->usd($row['profit'], 'PKR');
$rowPositive = $row['profit'] >= 0;
@endphp
| {{ $row['title'] }} |
{{ number_format($row['sold_qty']) }} |
{{ $fmt($rowCogs) }} |
{{ $fmt($rowRevenue) }} |
{{ $rowConversionRate > 0 ? number_format($rowConversionRate, 4) : '—' }} |
{{ $fmt($rowAvgBuyingUsd) }} |
{{ $fmt($rowSaleAfterPointsUsd) }} |
{{ $fmt($rowIssuedPoints) }} |
{{ $fmt($rowUsedPoints) }} |
{{ $fmt($rowProfit) }} |
@empty
| No paid voucher profit detail found for this range. |
@endforelse
Net Profit
{{ $fmt($profit) }}
{{ number_format($margin, 1) }}% margin
Revenue
{{ $fmt($revenue) }}
{{ $rangeLabel }}
Expenses
{{ $fmt($expenses) }}
All running costs
COGS
{{ $fmt($voucherCogsUsd) }}
Inventory consumed
Redeemed Points
{{ $fmt($pointsUsd) }}
Deducted from profit
{{-- Detailed formula --}}
Profit Formula
Source: {{ $profitSummary['source'] ?? 'examism_v3' }}
Paid order revenue
{{ $fmt($revenue) }}
Sold voucher cost
-{{ $fmt($voucherCogsUsd) }}
Gross profit
{{ $fmt($grossProfitBeforePoints) }}
Redeemed points
-{{ $fmt($pointsUsd) }}
Operating expenses
-{{ $fmt($expenses) }}
Net profit
{{ $fmt($profit) }}
Month Detail
Products sold{{ number_format($productProfitRows->sum('sold_qty')) }}
Paid orders counted{{ number_format($productProfitRows->sum('orders_count')) }}
Gross profit before points{{ $fmt($grossProfitBeforePoints) }}
Profit after redeemed points{{ $fmt($profitAfterPoints) }}
Final margin{{ number_format($margin, 2) }}%
{{-- Point deduction detail --}}
Quarterly Redeemed Points
-{{ $fmt($quarterlyPointsUsd) }}
Deducted from gross profit
Yearly Redeemed Points
-{{ $fmt($yearlyPointsUsd) }}
Deducted from gross profit
Profit After Redeemed Points
{{ $fmt($profitAfterPoints) }}
Before operating expenses
{{-- Issued, redeemed, and available point detail --}}
Quarterly & Yearly Point Ledger
Issued, redeemed, and available points by origin
{{ $pointDetails['source'] ?? 'No point ledger' }}
Quarterly Issued
{{ number_format($pointValue('quarterly', 'issued'), 2) }}
Quarterly Redeemed
{{ number_format($pointValue('quarterly', 'used'), 2) }}
Quarterly Available
{{ number_format($pointValue('quarterly', 'available'), 2) }}
Yearly Issued
{{ number_format($pointValue('yearly', 'issued'), 2) }}
Yearly Redeemed
{{ number_format($pointValue('yearly', 'used'), 2) }}
Yearly Available
{{ number_format($pointValue('yearly', 'available'), 2) }}
Recent Point Activity
Latest {{ number_format($recentPointRows->count()) }}
| Type |
Product |
User |
Order |
Date |
Issued |
Redeemed |
Available |
@forelse($recentPointRows as $row)
| {{ $row['origin'] }} |
{{ $row['product_name'] }} |
{{ $row['agent_name'] }}
{{ $row['agent_business_name'] ?: ($row['agent_email'] ?: 'No user detail') }}
|
{{ $row['order_id'] ? '#'.$row['order_id'] : '—' }} |
{{ $row['activity_date'] ?: '—' }} |
{{ number_format((float)$row['issued'], 2) }} |
{{ number_format((float)$row['used'], 2) }} |
{{ number_format((float)$row['available'], 2) }} |
@empty
| No recent point activity found for this range. |
@endforelse
{{-- Charts --}}
Revenue vs. Expenses — 6 Months
USD
Expense Categories
This month
@if(count($expCatLabels) > 0)
@else
No expenses found for this range.
@endif
{{-- Profit trend line --}}
Net Profit Trend — 6 Months
USD
{{-- P&L Table --}}
Profit & Loss
{{ $rangeLabel }}
| Line Item |
USD Value |
PKR Equivalent |
| Revenue |
{{ $metrics->money($revenue) }} |
{{ $fmt($revenue) }} |
| — COGS |
-{{ $metrics->money($voucherCogsUsd) }} |
-{{ $fmt($voucherCogsUsd) }} |
| Gross Profit Before Points |
{{ $metrics->money($grossProfitBeforePoints) }} |
{{ $fmt($grossProfitBeforePoints) }} |
| — Redeemed Points |
-{{ $metrics->money($pointsUsd) }} |
-{{ $fmt($pointsUsd) }} |
| Profit After Redeemed Points |
{{ $metrics->money($profitAfterPoints) }} |
{{ $fmt($profitAfterPoints) }} |
| — Operating Expenses |
-{{ $metrics->money($expenses) }} |
-{{ $fmt($expenses) }} |
| Net Profit |
{{ $metrics->money($profit) }} |
{{ $fmt($profit) }} |
{{-- Product profit detail --}}
Profit by Product
{{ $rangeLabel }}
| Product |
Qty |
Orders |
Revenue |
COGS |
Avg. Buying |
Avg. Sale |
Quarterly Issued |
Yearly Issued |
Redeemed Total |
Net Profit |
Margin |
@forelse($productProfitRows as $row)
@php
$rowSoldQty = (float) ($row['sold_qty'] ?? 0);
$rowAvgBuying = (float) ($row['buying_price_per_voucher'] ?? ($rowSoldQty > 0 ? ((float) ($row['cogs'] ?? 0) / $rowSoldQty) : 0));
$rowSaleAfterPoints = (float) ($row['sale_after_points_per_voucher'] ?? ($rowSoldQty > 0 ? (((float) ($row['revenue'] ?? 0) - (float) ($row['points'] ?? 0)) / $rowSoldQty) : 0));
$rowRevenue = $metrics->usd($row['revenue'], 'PKR');
$rowCogs = $metrics->usd($row['cogs'], 'PKR');
$rowAvgBuyingUsd = $metrics->usd($rowAvgBuying, 'PKR');
$rowSaleAfterPointsUsd = $metrics->usd($rowSaleAfterPoints, 'PKR');
$rowPoints = $metrics->usd($row['points'], 'PKR');
$rowPointBreakdown = collect($row['points_breakdown'] ?? [])->map(fn ($amount) => $metrics->usd((float) $amount, 'PKR'));
$rowIssuedPointBreakdown = collect($row['issued_points_breakdown'] ?? [])->map(fn ($amount) => $metrics->usd((float) $amount, 'PKR'));
$rowProfit = $metrics->usd($row['profit'], 'PKR');
$rowPositive = $row['profit'] >= 0;
@endphp
| {{ $row['title'] }} |
{{ number_format($row['sold_qty']) }} |
{{ number_format($row['orders_count']) }} |
{{ $fmt($rowRevenue) }} |
-{{ $fmt($rowCogs) }} |
{{ $fmt($rowAvgBuyingUsd) }} |
{{ $fmt($rowSaleAfterPointsUsd) }} |
{{ $fmt((float) ($rowIssuedPointBreakdown['quarterly'] ?? 0)) }} |
{{ $fmt((float) ($rowIssuedPointBreakdown['yearly'] ?? 0)) }} |
-{{ $fmt($rowPoints) }} |
{{ $fmt($rowProfit) }} |
{{ number_format($row['margin'], 1) }}% |
@empty
| No paid voucher profit detail found for this range. |
@endforelse
{{-- Recent paid order detail --}}
Recent Paid Orders
Latest 20
| Order |
Date |
Qty |
Revenue |
COGS |
Quarterly Issued |
Yearly Issued |
Redeemed Total |
Profit |
@forelse($recentProfitOrders as $order)
@php
$orderRevenue = $metrics->usd($order['revenue'], 'PKR');
$orderCogs = $metrics->usd($order['cogs'], 'PKR');
$orderPoints = $metrics->usd($order['points'], 'PKR');
$orderPointBreakdown = collect($order['points_breakdown'] ?? [])->map(fn ($amount) => $metrics->usd((float) $amount, 'PKR'));
$orderIssuedPointBreakdown = collect($order['issued_points_breakdown'] ?? [])->map(fn ($amount) => $metrics->usd((float) $amount, 'PKR'));
$orderProfit = $metrics->usd($order['profit'], 'PKR');
$orderPositive = $order['profit'] >= 0;
@endphp
| #{{ $order['id'] }} |
{{ $order['date'] ?: '—' }} |
{{ number_format($order['qty']) }} |
{{ $fmt($orderRevenue) }} |
-{{ $fmt($orderCogs) }} |
{{ $fmt((float) ($orderIssuedPointBreakdown['quarterly'] ?? 0)) }} |
{{ $fmt((float) ($orderIssuedPointBreakdown['yearly'] ?? 0)) }} |
-{{ $fmt($orderPoints) }} |
{{ $fmt($orderProfit) }} |
@empty
| No recent paid orders found for this range. |
@endforelse
{{-- Expense category detail --}}
@if($expByCategory->count() > 0)
Expense Breakdown by Category
| Category | USD | PKR | % of Expenses |
@foreach($expByCategory as $cat => $usdVal)
| {{ $cat }} |
{{ $metrics->money($usdVal) }} |
{{ $fmt($usdVal) }} |
{{ $expenses > 0 ? number_format(($usdVal / $expenses) * 100, 1) : '0.0' }}% |
@endforeach
@endif
@push('scripts')
@endpush
@endsection