Module: Rawbotz::Models::StockProductFactory

Includes:
RawgentoDB, RawgentoModels
Defined in:
lib/rawbotz/models/stock_product_factory.rb

Class Method Summary collapse

Class Method Details

.create(suppliers) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rawbotz/models/stock_product_factory.rb', line 34

def self.create suppliers
  product_ids = LocalProduct.unscoped.where(supplier_id: suppliers.map(&:id), active: true).pluck(:product_id)

  sales_30  = Query.num_sales_since(Date.today - 30,  product_ids)
  sales_60  = Query.num_sales_since(Date.today - 60,  product_ids)
  sales_90  = Query.num_sales_since(Date.today - 90,  product_ids)
  sales_365 = Query.num_sales_since(Date.today - 365, product_ids)

  beginning_of_time = StockItem.order(created_at: :asc).first.created_at.to_date

  sales       = Query.num_sales_since(beginning_of_time, product_ids)
  first_sales = Query.first_sales(product_ids)
  # Default to today instead of nil date
  first_sales.default = Date.today
  stock = {}

  Query.stock.each {|s| stock[s.product_id] = s.qty}
  product_ids.map do |product_id|
    StockProduct.new(product: LocalProduct.unscoped.find_by(product_id: product_id),
                     current_stock: stock[product_id],
                     sales_last_30: sales_30[product_id]&.qty,
                     sales_last_60: sales_60[product_id]&.qty,
                     sales_last_90: sales_90[product_id]&.qty,
                     sales_last_365: sales_365[product_id]&.qty,
                     num_days_first_sale: Date.today - first_sales[product_id].to_date
                    )
  end
end

.create_single(product) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rawbotz/models/stock_product_factory.rb', line 7

def self.create_single product
  product_id = product.product_id
  sales_30  = Query.num_sales_since(Date.today - 30,  product_id)
  sales_60  = Query.num_sales_since(Date.today - 60,  product_id)
  sales_90  = Query.num_sales_since(Date.today - 90,  product_id)
  sales_365 = Query.num_sales_since(Date.today - 365, product_id)

  beginning_of_time = StockItem.order(created_at: :asc).pluck(:created_at).first.to_date

  sales       = Query.num_sales_since(beginning_of_time, product_id)
  first_sales = Query.first_sales(product_id)
  # Default to today instead of nil date
  first_sales.default = Date.today

  # stock product has trouble when empty values ...
  stock = Query.stock.find {|s| s.product_id = product_id}.qty
  StockProduct.new(product: LocalProduct.unscoped.find_by(product_id: product_id),
                   current_stock: stock[product_id],
                   sales_last_30: sales_30[product_id]&.qty ,
                   sales_last_60: sales_60[product_id]&.qty ,
                   sales_last_90: sales_90[product_id]&.qty ,
                   sales_last_365: sales_365[product_id]&.qty ,
                   num_days_first_sale: Date.today - first_sales[product_id].to_date
                  )

end