Skip to content

Instantly share code, notes, and snippets.

@shankhadevpadam
Created June 2, 2023 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shankhadevpadam/4b7b92c7320b061cfc3d2f1ce72e5d1a to your computer and use it in GitHub Desktop.
Save shankhadevpadam/4b7b92c7320b061cfc3d2f1ce72e5d1a to your computer and use it in GitHub Desktop.
<?php
namespace App\Presenters;
use App\Filter\DateFilter;
use App\Models\UserPackageAffiliate;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
class AffiliateReportPresenter
{
public Builder $builder;
public function __construct(
public int $userId,
) {
$this->builder = UserPackageAffiliate::whereHas('packageAffiliate', function ($query) use ($userId) {
$query->where('user_id', $userId);
});
}
public function generate(): array
{
return [
'total' => $this->total(),
'total_affiliate_current_month' => $this->builder->whereMonth('created_at', now()->month)->count(),
'total_earning_current_month' => $this->builder->whereMonth('created_at', now()->month)->sum('reward_amount'),
//'chart_data' => $data['chart_data'],
];
}
public function total()
{
return match(request('filter')) {
'last-7-days' => $this->builder->whereAffiliateBetween(DateFilter::lastWeek())->sum('reward_amount'),
'last-month' => $this->builder->whereAffiliateBetween(DateFilter::lastMonth())->sum('reward_amount'),
'this-year' => $this->builder->whereYear('created_at', now()->year)->sum('reward_amount'),
default => transform(true, function () {
[$startDate, $endDate] = explode(',', request('filter'));
return $this->builder->whereAffiliateBetween(new DateFilter(Carbon::parse($startDate), Carbon::parse($endDate)))->sum('reward_amount');
}),
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment