Class: Deal

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/deal.rb

Overview

Schema Information

Table name: deals

id              :integer          not null, primary key
buy_quantity    :integer          not null
get_percentage  :integer
deal_type_id    :integer          not null
product_type_id :integer          not null
get_amount      :integer
deleted_at      :datetime
created_at      :datetime         not null
updated_at      :datetime         not null

Class Method Summary (collapse)

Class Method Details

+ (Object) best_qualifing_deal(order)



35
36
37
38
39
40
41
42
43
# File 'app/models/deal.rb', line 35

def self.best_qualifing_deal(order)
  product_type_and_amount_hash = order.number_of_a_given_product_type.sort_by{|v| v.last.size }.reverse
  deal_time = order.completed_at || Time.zone.now # Deal.best_qualifing_deal(ooo)
  product_type_and_amount_hash.each do |h|
    deal_amount = qualifing_deal(h, deal_time)
    return deal_amount.round_at(2) if deal_amount
  end
  0.0
end

+ (Object) qualifing_deal(h, at)



44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/deal.rb', line 44

def self.qualifing_deal(h, at)
  deal = self.where(['deals.buy_quantity <= ?', h.last.size]).
  where(['deals.product_type_id IN (?)', h.first]).
  where(['deals.created_at <= ?', at]).where(['deals.deleted_at >= ? OR deals.deleted_at IS NULL', at]).
  order('deals.get_percentage DESC').first
  if deal && deal.get_amount && deal.get_amount > 0.0
    deal.get_amount.to_f / 100.0
  else
    deal ?  ((h.last.sort.reverse[0..(deal.buy_quantity - 1)].min) * deal.get_percentage).to_f / 100.0 : false
  end
end