Class: Report::ObsoleteItemsReport::Command

Inherits:
BaseCommand
  • Object
show all
Defined in:
app/services/report/obsolete_items_report/command.rb

Overview

Command pattern to encapsulate parameters necessary to run a presence report

Instance Method Summary collapse

Instance Method Details

#execute(age_in_years_since_sold: 3, sku_pattern: nil, qty_in_stock_lt: 2) ⇒ Object



4
5
6
7
8
9
# File 'app/services/report/obsolete_items_report/command.rb', line 4

def execute(age_in_years_since_sold: 3, sku_pattern: nil, qty_in_stock_lt: 2)
  qty_in_stock_sq = "(select sum(si.qty_on_hand) from store_items si where si.item_id = items.id and si.location NOT IN ('SCRAP')) < #{qty_in_stock_lt}".sql_safe
  relation = Item.active.where(Item[:last_sale_date].lteq(age_in_years_since_sold.year.ago)).where(qty_in_stock_sq)
  relation = relation.where(Item[:sku].matches(sku_pattern)) if sku_pattern.present?
  relation
end