@extends('admin.admin_dashboard')
@section('admin')
@php
/*
//old logic
use Carbon\Carbon;
use App\Models\User;
// Get all relevant payments with user
$subscriptions = App\Models\Payment::with('user')
->where('purpose', 'subscription')
->whereNull('coupon')
->where('created_at', '>', Carbon::parse('2025-06-30')->endOfDay())
->whereHas('user', function ($query) {
$query->whereNotNull('referred_by');
})
->get();
// Group by month then by referred_by (referrer user ID)
$monthlyGrouped = $subscriptions
->groupBy(function ($payment) {
return Carbon::parse($payment->created_at)->format('Y-m'); // group by year-month
})
->map(function ($monthlyPayments, $monthKey) {
return $monthlyPayments
->groupBy(function ($payment) {
return $payment->user->referred_by; // influencer's user ID
})
->map(function ($groupedPayments, $referredById) use ($monthKey) {
return [
'month' => Carbon::createFromFormat('Y-m', $monthKey)->format('F Y'),
'referred_by' => $referredById,
'total' => $groupedPayments->sum('total_amount'),
'commission' => $groupedPayments->sum('total_amount') * 0.3,
];
})
->values(); // reset keys
});
*/
@endphp
@php
use Carbon\Carbon;
use App\Models\User;
// Get all relevant payments with user
$subscriptions = App\Models\Payment::with('user')
->where('purpose', 'subscription')
->whereNull('coupon')
->where('created_at', '>', Carbon::parse('2025-06-30')->endOfDay())
->whereHas('user', function ($query) {
$query->whereNotNull('referred_by');
})
->get();
// Conversion rates to Espees (per 1 unit of currency) ✅ added
$conversionRates = [
'ESPEES' => 1,
'ESPEES-VOUCHER' => 1,
'USD' => 1 / 1.25,
'GBP' => 1 / 1,
'CAD' => 1 / 1.7,
'EUR' => 1 / 1.15,
'ZAR' => 1 / 23,
'NGN' => 1 / 2050,
];
// Group by month then by referred_by (referrer user ID)
$monthlyGrouped = $subscriptions
->groupBy(function ($payment) {
return Carbon::parse($payment->created_at)->format('Y-m');
})
->map(function ($monthlyPayments, $monthKey) use ($conversionRates) {
// ✅ added use($conversionRates)
return $monthlyPayments
->groupBy(function ($payment) {
return $payment->user->referred_by;
})
->map(function ($groupedPayments, $referredById) use ($monthKey, $conversionRates) {
// ✅ added $conversionRates
// ✅ recalculate total_amount in Espees before summing
$totalInEspees = $groupedPayments->sum(function ($payment) use ($conversionRates) {
$currency = strtoupper($payment->currency ?? 'ESPEES');
$rate = $conversionRates[$currency] ?? 1; // default 1 if missing
return $payment->total_amount * $rate; // convert each amount to Espees
});
return [
'month' => Carbon::createFromFormat('Y-m', $monthKey)->format('F Y'),
'referred_by' => $referredById,
'total' => $totalInEspees, // ✅ now in Espees
'commission' => $totalInEspees * 0.3, // ✅ commission also based on Espees total
];
})
->values();
});
@endphp
Monthly Subscription Commissions
@foreach ($monthlyGrouped as $monthKey => $referrals)
| S/N |
referred_by |
Total Subscription Amount |
Commission (30%) |
@php $i = 1; @endphp
@foreach ($referrals as $summary)
| {{ $i++ }} |
{{ $summary['referred_by'] }} |
{{ number_format($summary['total'], 2) }} Espees |
{{ number_format($summary['commission'], 2) }} Espees |
@endforeach
@endforeach
@endsection