10, 'search' => '', 'orderBy' => 'id', 'order' => 'desc', 'with_deleted' => false, ]; return array_merge($defaultArgs, $filterData); } private function getZonePricingRuleQuery(): Builder { return $this->getQuery() ->select( "{$this->table}.id", "{$this->table}.uuid", "{$this->table}.restaurant_id", "{$this->table}.zone_id", "{$this->table}.name", "{$this->table}.priority", "{$this->table}.is_active", "{$this->table}.base_fare", "{$this->table}.minimum_fare", "{$this->table}.per_km_charge", "{$this->table}.free_distance", "{$this->table}.max_distance", "{$this->table}.surge_enabled", "{$this->table}.surge_multiplier", "{$this->table}.conditions", "{$this->table}.valid_from", "{$this->table}.valid_until", "{$this->table}.valid_days", "{$this->table}.created_at", "{$this->table}.deleted_at" ); } protected function filterSearchQuery(Builder|EloquentBuilder $query, string $searchedText): Builder { $searchable = "%$searchedText%"; return $query->where("{$this->table}.name", 'LIKE', $searchable) ->orWhere("{$this->table}.status", 'LIKE', $searchable); } public function getAll(array $filterData = []): Paginator { $filter = $this->getFilterData($filterData); $query = $this->getZonePricingRuleQuery(); if (! $filter['with_deleted']) { $query->whereNull("{$this->table}.deleted_at"); } if (! empty($filter['search'])) { $query = $this->filterSearchQuery($query, $filter['search']); } return $query ->orderBy($filter['orderBy'], $filter['order']) ->paginate($filter['perPage']); } public function getCount(array $filterData = []): int { $filter = $this->getFilterData($filterData); $query = $this->getQuery(); if (! $filter['with_deleted']) { $query->whereNull("{$this->table}.deleted_at"); } return $query->count(); } /** * @throws Exception */ public function getByColumn(string $columnName, $columnValue, array $selects = ['*']): ?object { $item = $this->getZonePricingRuleQuery() ->where($columnName, $columnValue) ->first($selects); if (empty($item)) { throw new Exception( $this->getExceptionMessage(static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE), Response::HTTP_NOT_FOUND ); } return $item; } /** * @throws Exception */ public function create(array $data): object { $data = $this->prepareForDB($data); $id = $this->getQuery()->insertGetId($data); return ZonePricingRule::find($id); } /** * @throws Exception */ public function update(int $id, array $data): object { $item = ZonePricingRule::findOrFail($id); $data = $this->prepareForDB($data, $item); parent::update($id, $data); return $this->getById($id); } /** * @throws Exception */ public function prepareForDB(array $data, ?object $item = null): array { $data = parent::prepareForDB($data, $item); if (empty($item)) { // Generate UUID for new records $data['uuid'] = (string) Str::uuid(); $data['created_at'] = now(); $data['restaurant_id'] = $this->getCurrentRestaurantId(); } else { $data['updated_at'] = now(); } return $data; } protected function getExceptionMessages(): array { return [ static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE => 'ZonePricingRule does not exist.', static::MESSAGE_ITEM_COULD_NOT_BE_DELETED => 'ZonePricingRule could not be deleted.', ]; } }