Files
cheerstothe_season_2.0/app/Exports/ReportExport.php
2024-07-03 19:30:35 +05:30

89 lines
2.5 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;
protected $restaurants;
public function __construct($reportType, $states, $startDate, $endDate, $restaurants = [])
{
$this->reportType = $reportType;
$this->states = $states;
$this->startDate = $startDate;
$this->endDate = $endDate;
$this->restaurants = $restaurants;
}
public function view(): View
{
$data = collect();
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();
} 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('created_at', '>=', $this->startDate);
}
if ($this->endDate) {
$query->whereDate('created_at', '<=', $this->endDate);
}
$data = $query->get();
} elseif ($this->reportType === 'Redemptions for Specific Restaurants') {
$query = RedeemRestaurant::with('restaurant', 'customer');
if (!empty($this->restaurants)) {
$query->whereIn('manage_restaurants_xid', $this->restaurants);
}
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
]);
}
}