Created
June 2, 2023 11:39
-
-
Save shankhadevpadam/4b7b92c7320b061cfc3d2f1ce72e5d1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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