78 lines
2.2 KiB
PHP
78 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\IamPrincipal;
|
|
use App\Models\RedeemRestaurant;
|
|
use Illuminate\Contracts\View\View;
|
|
use Maatwebsite\Excel\Concerns\FromView;
|
|
|
|
class ReportExport implements FromView
|
|
{
|
|
protected $reportType;
|
|
protected $states;
|
|
protected $startDate;
|
|
protected $endDate;
|
|
|
|
public function __construct($reportType, $states, $startDate, $endDate)
|
|
{
|
|
$this->reportType = $reportType;
|
|
$this->states = $states;
|
|
$this->startDate = $startDate;
|
|
$this->endDate = $endDate;
|
|
}
|
|
|
|
public function view(): View
|
|
{
|
|
if ($this->reportType === 'Total Users') {
|
|
$query = IamPrincipal::query();
|
|
|
|
if (!empty($this->states)) {
|
|
$query->whereIn('state_xid', $this->states);
|
|
}
|
|
|
|
if ($this->startDate) {
|
|
$query->whereDate('created_at', '>=', $this->startDate);
|
|
}
|
|
|
|
if ($this->endDate) {
|
|
$query->whereDate('created_at', '<=', $this->endDate);
|
|
}
|
|
|
|
$data = $query->get();
|
|
return view('exports.report', [
|
|
'data' => $data,
|
|
'reportType' => $this->reportType
|
|
]);
|
|
} elseif ($this->reportType === 'Redemptions') {
|
|
$query = RedeemRestaurant::with(['restaurant', 'customer'])->where('is_redeem', 0);
|
|
|
|
if (!empty($this->states)) {
|
|
$query->whereHas('customer', function ($q) {
|
|
$q->whereIn('state_xid', $this->states);
|
|
});
|
|
}
|
|
|
|
if ($this->startDate) {
|
|
$query->whereDate('redeem_date', '>=', $this->startDate);
|
|
}
|
|
|
|
if ($this->endDate) {
|
|
$query->whereDate('redeem_date', '<=', $this->endDate);
|
|
}
|
|
|
|
$data = $query->get();
|
|
return view('exports.report', [
|
|
'data' => $data,
|
|
'reportType' => $this->reportType
|
|
]);
|
|
}
|
|
|
|
// Default empty data if no matching report type
|
|
return view('exports.report', [
|
|
'data' => collect(),
|
|
'reportType' => $this->reportType
|
|
]);
|
|
}
|
|
}
|