Class: Rawbotz::Models::StockProduct

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/rawbotz/models/stock_product.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_stockObject

Returns the value of attribute current_stock.



11
12
13
# File 'lib/rawbotz/models/stock_product.rb', line 11

def current_stock
  @current_stock
end

#mem_corrected_salesObject

Returns the value of attribute mem_corrected_sales.



11
12
13
# File 'lib/rawbotz/models/stock_product.rb', line 11

def mem_corrected_sales
  @mem_corrected_sales
end

#num_days_first_saleObject

Returns the value of attribute num_days_first_sale.



11
12
13
# File 'lib/rawbotz/models/stock_product.rb', line 11

def num_days_first_sale
  @num_days_first_sale
end

#productObject

Returns the value of attribute product.



11
12
13
# File 'lib/rawbotz/models/stock_product.rb', line 11

def product
  @product
end

#sales_last_30Object

Returns the value of attribute sales_last_30.



11
12
13
# File 'lib/rawbotz/models/stock_product.rb', line 11

def sales_last_30
  @sales_last_30
end

#sales_last_365Object

Returns the value of attribute sales_last_365.



11
12
13
# File 'lib/rawbotz/models/stock_product.rb', line 11

def sales_last_365
  @sales_last_365
end

#sales_last_60Object

Returns the value of attribute sales_last_60.



11
12
13
# File 'lib/rawbotz/models/stock_product.rb', line 11

def sales_last_60
  @sales_last_60
end

#sales_last_90Object

Returns the value of attribute sales_last_90.



11
12
13
# File 'lib/rawbotz/models/stock_product.rb', line 11

def sales_last_90
  @sales_last_90
end

Instance Method Details

#corrected_sales(num_days, per_days: num_days) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rawbotz/models/stock_product.rb', line 38

def corrected_sales num_days, per_days: num_days
  factor = num_days.to_f / per_days
  sales = nil
  if num_days == 30
    sales = corrected_sales_last_30
  elsif num_days == 60
    sales = corrected_sales_last_60
  elsif num_days == 90
    sales = corrected_sales_last_90
  elsif num_days == 365
    sales = corrected_sales_last_365
  else
    raise UnsupportedNumberOfDaysError
  end
  return nil if sales.nil?
  sales / factor
end

#corrected_sales_last_30Object

We should also extrapolate (out of-) stock days!



58
59
60
61
# File 'lib/rawbotz/models/stock_product.rb', line 58

def corrected_sales_last_30
  @mem_corrected_sales ||= {}
  @mem_corrected_sales[30] ||= calculate_corrected_sales(30)
end

#corrected_sales_last_365Object



73
74
75
76
# File 'lib/rawbotz/models/stock_product.rb', line 73

def corrected_sales_last_365
  @mem_corrected_sales ||= {}
  @mem_corrected_sales[365] ||= calculate_corrected_sales(365)
end

#corrected_sales_last_60Object



63
64
65
66
# File 'lib/rawbotz/models/stock_product.rb', line 63

def corrected_sales_last_60
  @mem_corrected_sales ||= {}
  @mem_corrected_sales[60] ||= calculate_corrected_sales(60)
end

#corrected_sales_last_90Object



68
69
70
71
# File 'lib/rawbotz/models/stock_product.rb', line 68

def corrected_sales_last_90
  @mem_corrected_sales ||= {}
  @mem_corrected_sales[90] ||= calculate_corrected_sales(90)
end

#days_since_first_stock_dateObject



78
79
80
81
82
83
# File 'lib/rawbotz/models/stock_product.rb', line 78

def days_since_first_stock_date
  #@mem_days_since_first_stock_date ||=
  return 0 if !@product.first_stock_record.present?
  first_stock_date = @product.first_stock_record.created_at.to_date
  Date.today - first_stock_date
end

#expected_stock_lifetimeObject



17
18
19
20
21
22
# File 'lib/rawbotz/models/stock_product.rb', line 17

def expected_stock_lifetime
  if @current_stock.to_i == 0 || sales_per_day.nil? || sales_per_day == 0.0
    return 0.0
  end
  @current_stock / sales_per_day
end

#out_of_stock_days(num_days) ⇒ Object



85
86
87
# File 'lib/rawbotz/models/stock_product.rb', line 85

def out_of_stock_days num_days
  @product.out_of_stock_days_since(Date.today - num_days)
end

#real_sales(num_days) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rawbotz/models/stock_product.rb', line 24

def real_sales num_days
  if num_days == 30
    @sales_last_30
  elsif num_days == 60
    @sales_last_60
  elsif num_days == 90
    @sales_last_90
  elsif num_days == 365
    @sales_last_365
  else
    raise UnsupportedNumberOfDaysError
  end
end

#sales_per_dayObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rawbotz/models/stock_product.rb', line 89

def sales_per_day
  case sales_per_day_base
  when 365
    @sales_last_365 / days_in_stock(365).to_f
  when 90
    @sales_last_90 / days_in_stock(90).to_f
  when 60
    @sales_last_60 / days_in_stock(60).to_f
  when 30
    return nil if @sales_last_30.nil?
    return nil if days_in_stock(30).to_i == 0
    @sales_last_30 / days_in_stock(30).to_f
  else
    raise UnsupportedNumberOfDaysError
  end
end

#sales_per_day_baseObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rawbotz/models/stock_product.rb', line 106

def sales_per_day_base
  product_days_first_stock = @product.days_since_first_stock
  #first saleproduct_days_first_stock = @product.days_since_first_stock
  if @sales_last_365.present? && product_days_first_stock >= 365 && num_days_first_sale >= 365 && days_in_stock(365) != 0
    return 365
  elsif @sales_last_90.present? && product_days_first_stock >= 90 && num_days_first_sale >= 90 && days_in_stock(90) != 0
    return 90
  elsif @sales_last_60.present? && product_days_first_stock >= 60 && num_days_first_sale >= 60 && days_in_stock(60) != 0
    return 60
  #elsif @sales_last_30.present?
  else
    # today - last sale ?
    return 30
  end
end