@extends('admin.admin_dashboard')
@section('admin')
@php
/*
//old logic
use Carbon\Carbon;
use App\Models\User;
// my zone
$my_zone = auth()->user()->ce_zone;
// 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) use ($my_zone) {
$query->whereNull('referred_by')->whereNotNull('zone')->where('zone', $my_zone);
})
->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->ce_zone; //
})
->map(function ($groupedPayments, $referredById) use ($monthKey) {
return [
'month' => Carbon::createFromFormat('Y-m', $monthKey)->format('F Y'),
'ce_zone' => $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;
// my zone
$my_zone = auth()->user()->ce_zone;
// 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) use ($my_zone) {
$query->whereNull('referred_by')->whereNotNull('zone')->where('zone', $my_zone);
})
->get();
// Conversion rates to Espees (per 1 unit of currency)
$conversionRates = [
'ESPEES' => 1,
'ESPEES-VOUCHER' => 1,
'USD' => 1 / 1.25, // 0.8 Espees
'GBP' => 1 / 1, // 1.0 Espee
'CAD' => 1 / 1.7, // 0.588 Espees
'EUR' => 1 / 1.15, // 0.869 Espees
'ZAR' => 1 / 23, // 0.043 Espees
'NGN' => 1 / 2050, // 0.000488 Espees
]; // <-- Added conversion rates block
// 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) use ($conversionRates) {
// <-- added $conversionRates here
return $monthlyPayments
->groupBy(function ($payment) {
return $payment->user->ce_zone;
})
->map(function ($groupedPayments, $referredById) use ($monthKey, $conversionRates) {
// <-- added $conversionRates here
// ✅ Convert all total_amounts to Espees before summing
$totalInEspees = $groupedPayments->sum(function ($payment) use ($conversionRates) {
$currency = strtoupper($payment->currency ?? 'ESPEES');
$rate = $conversionRates[$currency] ?? 1; // default to 1 if not found
return $payment->total_amount * $rate;
});
return [
'month' => Carbon::createFromFormat('Y-m', $monthKey)->format('F Y'),
'ce_zone' => $referredById,
'total' => $totalInEspees, // <-- total now in Espees
'commission' => $totalInEspees * 0.3, // <-- commission also recalculated in Espees
];
})
->values(); // reset keys
});
@endphp
{{ $my_zone }} - Monthly Subscription Commissions
@foreach ($monthlyGrouped as $monthKey => $referrals)
| S/N |
Zone |
Total Subscription Amount |
Commission (30%) |
@php $i = 1; @endphp
@foreach ($referrals as $summary)
| {{ $i++ }} |
{{ $summary['ce_zone'] }} |
{{ number_format($summary['total'], 2) }} Espees |
{{ number_format($summary['commission'], 2) }} Espees |
@endforeach
@endforeach
@endsection