Module: RailsTrade::TradePromote

Extended by:
ActiveSupport::Concern
Included in:
TradePromote
Defined in:
app/models/rails_trade/trade_promote.rb

Instance Method Summary collapse

Instance Method Details

#check_promote_buyerObject



87
88
89
90
# File 'app/models/rails_trade/trade_promote.rb', line 87

def check_promote_buyer
  return unless promote_buyer
  self.promote_buyer.update state: 'used'
end

#compute_amountObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/rails_trade/trade_promote.rb', line 41

def compute_amount
  if single?
    value = trade_item.metering_attributes.fetch(promote.metering, 0)
    added_amount = trade_item.trade_promotes.select { |cp| cp.promote.sequence < self.promote.sequence }.sum(&->(o){ o.send(promote.metering) })
    
    self.based_amount = value + added_amount
  else
    value = trade.metering_attributes.fetch(promote.metering, 0)
    added_amount = trade.trade_promotes.select { |cp| cp.promote.sequence < self.promote.sequence }.sum(&->(o){ o.send(promote.metering) })
    
    self.based_amount = value + added_amount
  end

  self.computed_amount = self.promote_charge.final_price(based_amount)
  self.amount = computed_amount if amount.zero?
  self
end

#sync_changed_amountObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/rails_trade/trade_promote.rb', line 59

def sync_changed_amount
  if amount >= 0 && amount_before_last_save >= 0
    _additional_amount = amount - amount_before_last_save
    _reduced_amount = 0
  elsif amount >= 0 && amount_before_last_save < 0
    _additional_amount = amount
    _reduced_amount = -amount_before_last_save
  elsif amount < 0 && amount_before_last_save >= 0
    _additional_amount = -amount_before_last_save
    _reduced_amount = amount
  else  # amount < 0 && amount_before_last_save < 0
    _additional_amount = 0
    _reduced_amount = amount - amount_before_last_save
  end
  
  if overall?
    trade.overall_additional_amount += _additional_amount
    trade.overall_reduced_amount += _reduced_amount
    trade.amount += (_additional_amount + _reduced_amount)
    trade.save!
  elsif single?
    trade_item.additional_amount += _additional_amount
    trade_item.reduced_amount += _reduced_amount
    trade_item.amount += (_additional_amount + _reduced_amount)
    trade_item.save!
  end
end