@extends('layouts.finance') @section('title', ($selectedProduct['title'] ?? 'Item').' Profit') @section('subtitle', 'Filtered sold voucher profit after redeemed quarterly and yearly points') @section('content') @php $displayCurrency = 'PKR'; $fmt = fn($usd) => $metrics->moneyIn($usd, 'USD', $displayCurrency); $profitDetails = $profitDetails ?? ['summary' => [], 'products' => collect(), 'recent_orders' => collect(), 'point_details' => []]; $summary = $profitDetails['summary'] ?? []; $productRows = collect($profitDetails['products'] ?? []); $row = $productRows->first() ?? [ 'item_id' => $selectedProduct['item_id'] ?? $filters['product_id'], 'title' => $selectedProduct['title'] ?? 'Item #'.$filters['product_id'], 'sold_qty' => 0, 'orders_count' => 0, 'revenue' => 0, 'cogs' => 0, 'issued_points' => 0, 'issued_points_breakdown' => ['quarterly' => 0, 'yearly' => 0], 'points' => 0, 'points_breakdown' => ['quarterly' => 0, 'yearly' => 0], 'profit' => 0, 'margin' => 0, ]; $recentOrders = collect($profitDetails['recent_orders'] ?? []); $pointDetails = $profitDetails['point_details'] ?? ['summary' => [], 'by_item_user' => collect(), 'recent' => collect(), 'source' => null]; $pointRows = collect($pointDetails['by_item_user'] ?? []); $sourceCurrency = $summary['currency'] ?? 'PKR'; $revenueUsd = $metrics->usd((float) ($row['revenue'] ?? 0), 'PKR'); $cogsUsd = $metrics->usd((float) ($row['cogs'] ?? 0), 'PKR'); $issuedUsd = $metrics->usd((float) ($row['issued_points'] ?? 0), 'PKR'); $usedUsd = $metrics->usd((float) ($row['points'] ?? 0), 'PKR'); $profitUsd = $metrics->usd((float) ($row['profit'] ?? 0), 'PKR'); $grossUsd = $revenueUsd - $cogsUsd; $soldQty = (float) ($row['sold_qty'] ?? 0); $avgBuying = (float) ($row['buying_price_per_voucher'] ?? ($soldQty > 0 ? ((float) ($row['cogs'] ?? 0) / $soldQty) : 0)); $saleAfterPoints = (float) ($row['sale_after_points_per_voucher'] ?? ($soldQty > 0 ? (((float) ($row['revenue'] ?? 0) - (float) ($row['points'] ?? 0)) / $soldQty) : 0)); $avgBuyingUsd = $metrics->usd($avgBuying, 'PKR'); $saleAfterPointsUsd = $metrics->usd($saleAfterPoints, 'PKR'); $issuedBreakdown = collect($row['issued_points_breakdown'] ?? []); $usedBreakdown = collect($row['points_breakdown'] ?? []); $isPositive = ((float) ($row['profit'] ?? 0)) >= 0; @endphp
| Item | Sold Voucher | Buying Price | Sale Price | Buying / Voucher | Sale After Points / Voucher | Q Issued | Y Issued | Issued Total | Q Redeemed | Y Redeemed | Redeemed Total | Profit |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| {{ $row['title'] ?? $selectedProduct['title'] }} | {{ number_format((float) ($row['sold_qty'] ?? 0)) }} | {{ $fmt($cogsUsd) }} | {{ $fmt($revenueUsd) }} | {{ $fmt($avgBuyingUsd) }} | {{ $fmt($saleAfterPointsUsd) }} | {{ $fmt($metrics->usd((float) ($issuedBreakdown['quarterly'] ?? 0), 'PKR')) }} | {{ $fmt($metrics->usd((float) ($issuedBreakdown['yearly'] ?? 0), 'PKR')) }} | {{ $fmt($issuedUsd) }} | {{ $fmt($metrics->usd((float) ($usedBreakdown['quarterly'] ?? 0), 'PKR')) }} | {{ $fmt($metrics->usd((float) ($usedBreakdown['yearly'] ?? 0), 'PKR')) }} | {{ $fmt($usedUsd) }} | {{ $fmt($profitUsd) }} |
| User | Business | Q Issued | Q Redeemed | Y Issued | Y Redeemed | Total Issued | Total Used |
|---|---|---|---|---|---|---|---|
|
{{ $pointRow['agent_name'] }}
{{ $pointRow['agent_email'] ?: 'No email' }}
|
{{ $pointRow['agent_business_name'] ?: '-' }} | {{ number_format((float) ($quarterly['issued'] ?? 0), 2) }} | {{ number_format((float) ($quarterly['used'] ?? 0), 2) }} | {{ number_format((float) ($yearly['issued'] ?? 0), 2) }} | {{ number_format((float) ($yearly['used'] ?? 0), 2) }} | {{ number_format((float) ($total['issued'] ?? 0), 2) }} | {{ number_format((float) ($total['used'] ?? 0), 2) }} |
| No quarterly or yearly redeemed point detail found for this item and date range. | |||||||
| Order | Date | Sold Voucher | Sale Price | Buying Price | Sale After Points / Voucher | Buying / Voucher | Redeemed Points | Profit |
|---|---|---|---|---|---|---|---|---|
| #{{ $order['id'] }} | {{ $order['date'] ?: '-' }} | {{ number_format((float) ($order['qty'] ?? 0)) }} | {{ $fmt($metrics->usd((float) ($order['revenue'] ?? 0), 'PKR')) }} | {{ $fmt($metrics->usd((float) ($order['cogs'] ?? 0), 'PKR')) }} | {{ $fmt($metrics->usd($orderSaleAfterPoints, 'PKR')) }} | {{ $fmt($metrics->usd($orderAvgBuying, 'PKR')) }} | {{ $fmt($metrics->usd((float) ($order['points'] ?? 0), 'PKR')) }} | {{ $fmt($orderProfit) }} |
| No sold voucher orders found for this item and date range. | ||||||||