Module: RailsTrade::TradeItem
- Extended by:
- ActiveSupport::Concern
- Included in:
- TradeItem
- Defined in:
- app/models/rails_trade/trade_item.rb
Instance Method Summary collapse
- #compute_promote ⇒ Object
- #confirm_ordered! ⇒ Object
- #confirm_paid! ⇒ Object
- #confirm_part_paid! ⇒ Object
- #confirm_refund! ⇒ Object
-
#discount_price ⇒ Object
批发价和零售价之间的差价,即批发折扣.
- #init_amount ⇒ Object
- #metering_attributes ⇒ Object
- #original_quantity ⇒ Object
- #reset_amount ⇒ Object
- #reset_amount! ⇒ Object
- #sum_amount ⇒ Object
- #sync_changed_amount ⇒ Object
- #valid_promote_buyers(buyer) ⇒ Object
Instance Method Details
#compute_promote ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'app/models/rails_trade/trade_item.rb', line 65 def compute_promote good.valid_promote_goods.map do |promote_good| value = metering_attributes.fetch(promote_good.promote.metering) promote_charge = promote_good.promote.compute_charge(value, **extra) next unless promote_charge if promote_good.promote.single? tp = self.trade_promotes.build(promote_charge_id: promote_charge.id, promote_good_id: promote_good.id) else tp = trade.trade_promotes.build(promote_charge_id: promote_charge.id, promote_good_id: promote_good.id) end tp.compute_amount end return unless trade.buyer trade.buyer.promote_buyers.each do |promote_buyer| value = metering_attributes.fetch(promote_buyer.promote.metering) promote_charge = promote_buyer.promote.compute_charge(value, **extra) next unless promote_charge if promote_good.promote.single? tp = self.trade_promotes.build(promote_charge_id: promote_charge.id, promote_buyer_id: promote_buyer.id, promote_good_id: promote_buyer.promote_good_id) else tp = trade.trade_promotes.build(promote_charge_id: promote_charge.id, promote_buyer_id: promote_buyer.id, promote_good_id: promote_buyer.promote_good_id) end tp.compute_amount end end |
#confirm_ordered! ⇒ Object
140 141 142 |
# File 'app/models/rails_trade/trade_item.rb', line 140 def confirm_ordered! self.good.order_done end |
#confirm_paid! ⇒ Object
144 145 |
# File 'app/models/rails_trade/trade_item.rb', line 144 def confirm_paid! end |
#confirm_part_paid! ⇒ Object
147 148 |
# File 'app/models/rails_trade/trade_item.rb', line 147 def confirm_part_paid! end |
#confirm_refund! ⇒ Object
150 151 |
# File 'app/models/rails_trade/trade_item.rb', line 150 def confirm_refund! end |
#discount_price ⇒ Object
批发价和零售价之间的差价,即批发折扣
53 54 55 |
# File 'app/models/rails_trade/trade_item.rb', line 53 def discount_price wholesale_price - (retail_price * number) end |
#init_amount ⇒ Object
57 58 59 60 61 62 63 |
# File 'app/models/rails_trade/trade_item.rb', line 57 def init_amount self.original_amount = single_price * number self.advance_amount = good.advance_price self.amount = original_amount trade.item_amount += amount trade.amount += amount end |
#metering_attributes ⇒ Object
136 137 138 |
# File 'app/models/rails_trade/trade_item.rb', line 136 def metering_attributes attributes.slice 'quantity', 'amount', 'number' end |
#original_quantity ⇒ Object
48 49 50 |
# File 'app/models/rails_trade/trade_item.rb', line 48 def original_quantity good.unified_quantity * self.number end |
#reset_amount ⇒ Object
118 119 120 121 122 123 124 |
# File 'app/models/rails_trade/trade_item.rb', line 118 def reset_amount self.additional_amount = trade_promotes.default_where('amount-gte': 0).sum(:amount) self.reduced_amount = trade_promotes.default_where('amount-lt': 0).sum(:amount) self.amount = original_amount + additional_amount + reduced_amount self.valid? self.changes end |
#reset_amount! ⇒ Object
126 127 128 129 |
# File 'app/models/rails_trade/trade_item.rb', line 126 def reset_amount! self.reset_amount self.update_columns changes.transform_values(&->(o){ o[1] }) end |
#sum_amount ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/rails_trade/trade_item.rb', line 92 def sum_amount self.additional_amount = trade_promotes.select(&->(o){ o.amount >= 0 }).sum(&:amount) self.reduced_amount = trade_promotes.select(&->(o){ o.amount < 0 }).sum(&:amount) # 促销价格 self.retail_price = single_price + additional_amount self.wholesale_price = original_amount + additional_amount self.amount = original_amount + additional_amount + reduced_amount # 最终价格 self end |
#sync_changed_amount ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/models/rails_trade/trade_item.rb', line 103 def sync_changed_amount self.amount = original_amount + additional_amount + reduced_amount changed_amount = amount - amount_before_last_save trade.item_amount += changed_amount trade.amount += changed_amount if trade.amount == trade.compute_saved_amount trade.save! else trade.errors.add :amount, 'not equal' logger.error "#{self.class.name}/#{trade.class.name}: #{trade.error_text}" raise ActiveRecord::RecordInvalid.new(trade) end end |
#valid_promote_buyers(buyer) ⇒ Object
131 132 133 134 |
# File 'app/models/rails_trade/trade_item.rb', line 131 def valid_promote_buyers(buyer) ids = (available_promote_ids & buyer.all_promote_ids) - buyer.promote_buyers.pluck(:promote_id) Promote.where(id: ids) end |