Files
portal-api/app/Repositories/Discount/DiscountRepository.php
T
2025-07-24 16:18:27 +07:00

73 lines
2.4 KiB
PHP

<?php
namespace App\Repositories\Discount;
use App\Models\Discount;
use App\Repositories\BaseRepository;
class DiscountRepository extends BaseRepository implements DiscountRepositoryInterface
{
public function getModel()
{
return Discount::class;
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$model = $this->model;
if (count($relationships) > 0) {
$model = $model->with($relationships);
}
$model = $model->select($select);
if (isset($data['start']) && ! empty($data['start'])) {
$model = $model->whereDate("start", ">=", $data['start']);
}
if (isset($data['end'])) {
$model = $model->where(function ($query) use ($data) {
if (isset($data['end']['condition'])) {
switch (strtoupper($data['end']['condition'])) {
case "IN":
$query->whereIn($data['end']['key'], $data['end']['value']);
break;
case "NotIn":
$query->whereNotIn($data['end']['key'], $data['end']['value']);
break;
default:
$query->where($data['end']['key'], $data['end']['condition'], $data['end']['value']);
break;
}
} else {
$query->where($data['end']['key'], $data['end']['value']);
}
});
}
if (isset($data['type']) && ! empty($data['type'])) {
$model = $model->where("type", $data['phone']);
}
if (isset($data['status']) && ! empty($data['status']) && $data['status'] != 'full') {
$model = $model->where("status", match ($data['status']) {
'active' => 1,
'de_active' => 0,
'closed' => 2,
});
}
if (count($withCount) > 0) {
$model = $model->withCount($withCount);
}
if (isset($data['page'])) {
return $model->paginate(15);
}
return $model->get();
}
public function findBy(string $key, string $value)
{
if (empty($value) || empty($key)) {
return false;
}
return $this->model->where($key, $value)->first();
}
}