/** * This exists to makes statuses sortable by assigning them a value * * Expired -1 * Running 0 * Upcoming 1 * * @param \October\Rain\Database\Builder $query * @return \October\Rain\Database\Builder */ public function scopeSelectStatus($query) { $grammar = $query->getQuery()->getGrammar(); $start_at = $grammar->wrap($this->table . '.start_at'); $end_at = $grammar->wrap($this->table . '.end_at'); $now = Carbon::now(); $subquery = "CASE " . "WHEN ({$end_at} IS NOT NULL AND {$end_at} < '{$now}') THEN -1 " . "WHEN ({$start_at} IS NOT NULL AND {$start_at} > '{$now}') THEN 1 " . "ELSE 0 " . "END"; return $query->selectSubquery($subquery, 'status'); }
/** * This exists to makes statuses sortable by assigning them a value * * Disabled -2 * Out of stock -1 * Normal 0 * Discounted 1 * * @param \October\Rain\Database\Builder $query * @return \October\Rain\Database\Builder */ public function scopeSelectStatus($query) { $grammar = $query->getQuery()->getGrammar(); $price = $grammar->wrap('price'); $inventory = $grammar->wrap('inventory'); $is_enabled = $grammar->wrap('bedard_shop_products.is_enabled'); $base_price = $grammar->wrap('bedard_shop_products.base_price'); $subquery = "CASE " . "WHEN {$is_enabled} = 0 THEN -2 " . "WHEN ({$inventory} IS NULL or {$inventory} = 0) THEN -1 " . "WHEN {$price} < {$base_price} THEN 1 " . "ELSE 0 " . "END"; return $query->selectSubquery($subquery, 'status'); }