Class: Spree::ExchangeLineItem
- Inherits:
-
Object
- Object
- Spree::ExchangeLineItem
- Extended by:
- DisplayMoney
- Includes:
- PostSale::TaxedLine
- Defined in:
- app/models/spree/exchange_line_item.rb
Overview
One swap on a Exchange: a quantity of one variant going back, the same quantity of another coming out.
Both halves are taxed: the units coming back are credited the tax the
customer paid on them (original_*), and the replacement is taxed as a new
sale (new_*), each for the announced quantity.
Instance Method Summary collapse
-
#credited_quantity ⇒ Integer
The units this swap covers: none once canceled, what arrived once the warehouse has counted, what was announced before.
-
#credited_worth ⇒ BigDecimal
What the units coming back are worth, tax included.
- #fold_tax_lines! ⇒ void
- #new_pre_tax_amount ⇒ Object
- #new_tax_total ⇒ Object
-
#new_variant_price ⇒ Object
What the replacement costs, tax on top included.
- #original_pre_tax_amount ⇒ Object
-
#original_price ⇒ Object
What the customer paid for the units coming back, after discounts and with the tax charged on top — not the list price, which would over-credit a discounted line.
- #original_tax_total ⇒ Object
- #price_difference ⇒ Object
-
#replacement_amount_for(units) ⇒ BigDecimal
The replacement keeps the deal the customer had on what it replaces: its price carries the original line's discount in the same proportion, so swapping a size of a discounted item costs nothing.
-
#settled_charge ⇒ BigDecimal
What their replacements cost, from the rows as they stand.
-
#settled_credit ⇒ BigDecimal
What the units that arrived are credited, from the rows as they stand.
-
#settled_tax ⇒ BigDecimal
The tax inside #settled_credit less the tax inside #settled_charge.
- #tax_category_id ⇒ Object
-
#taxable_basis ⇒ Object
What the provider taxes the replacement on (see Spree::TaxProvider::Base#estimate_replacement).
Methods included from DisplayMoney
Methods included from PostSale::TaxedLine
#credited_tax_total, #settles_tax?
Instance Method Details
#credited_quantity ⇒ Integer
The units this swap covers: none once canceled, what arrived once the warehouse has counted, what was announced before.
79 80 81 82 83 |
# File 'app/models/spree/exchange_line_item.rb', line 79 def credited_quantity return 0 if exchange.canceled? exchange.received? || exchange.fulfilled? ? received_quantity.to_i : quantity.to_i end |
#credited_worth ⇒ BigDecimal
Returns what the units coming back are worth, tax included.
86 87 88 89 90 91 92 |
# File 'app/models/spree/exchange_line_item.rb', line 86 def credited_worth units = credited_quantity return 0.to_d if quantity.to_i.zero? original_amount_for(units) + Spree::Money::Rounding.to_currency((original_additional_tax_total / quantity) * units, currency) end |
#fold_tax_lines! ⇒ void
This method returns an undefined value.
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/models/spree/exchange_line_item.rb', line 126 def fold_tax_lines! rows = tax_lines.reload.to_a original_included, original_additional = tax_totals_from(rows.select(&:credit?)) new_included, new_additional = tax_totals_from(rows.reject(&:credit?)) update_columns( original_included_tax_total: original_included, original_additional_tax_total: original_additional, new_included_tax_total: new_included, new_additional_tax_total: new_additional ) end |
#new_pre_tax_amount ⇒ Object
51 52 53 |
# File 'app/models/spree/exchange_line_item.rb', line 51 def new_pre_tax_amount replacement_amount_for(quantity) - new_included_tax_total end |
#new_tax_total ⇒ Object
55 56 57 |
# File 'app/models/spree/exchange_line_item.rb', line 55 def new_tax_total new_included_tax_total + new_additional_tax_total end |
#new_variant_price ⇒ Object
What the replacement costs, tax on top included.
47 48 49 |
# File 'app/models/spree/exchange_line_item.rb', line 47 def new_variant_price replacement_amount_for(quantity) + new_additional_tax_total end |
#original_pre_tax_amount ⇒ Object
38 39 40 |
# File 'app/models/spree/exchange_line_item.rb', line 38 def original_pre_tax_amount original_amount_for(quantity) - original_included_tax_total end |
#original_price ⇒ Object
What the customer paid for the units coming back, after discounts and with the tax charged on top — not the list price, which would over-credit a discounted line.
34 35 36 |
# File 'app/models/spree/exchange_line_item.rb', line 34 def original_price original_amount_for(quantity) + original_additional_tax_total end |
#original_tax_total ⇒ Object
42 43 44 |
# File 'app/models/spree/exchange_line_item.rb', line 42 def original_tax_total original_included_tax_total + original_additional_tax_total end |
#price_difference ⇒ Object
59 60 61 |
# File 'app/models/spree/exchange_line_item.rb', line 59 def price_difference new_variant_price - original_price end |
#replacement_amount_for(units) ⇒ BigDecimal
The replacement keeps the deal the customer had on what it replaces: its price carries the original line's discount in the same proportion, so swapping a size of a discounted item costs nothing.
69 70 71 72 73 |
# File 'app/models/spree/exchange_line_item.rb', line 69 def replacement_amount_for(units) price = new_variant&.price_in(currency)&.amount.to_d Spree::Money::Rounding.to_currency(price * units.to_i * discount_ratio, currency) end |
#settled_charge ⇒ BigDecimal
What their replacements cost, from the rows as they stand.
114 115 116 |
# File 'app/models/spree/exchange_line_item.rb', line 114 def settled_charge replacement_amount_for(received_quantity) + tax_lines.charges.additional.sum(:amount) end |
#settled_credit ⇒ BigDecimal
What the units that arrived are credited, from the rows as they stand.
107 108 109 |
# File 'app/models/spree/exchange_line_item.rb', line 107 def settled_credit original_amount_for(received_quantity) + tax_lines.credits.additional.sum(:amount) end |
#settled_tax ⇒ BigDecimal
The tax inside #settled_credit less the tax inside #settled_charge.
121 122 123 |
# File 'app/models/spree/exchange_line_item.rb', line 121 def settled_tax credited_tax_total - tax_lines.charges.sum(:amount) end |
#tax_category_id ⇒ Object
100 101 102 |
# File 'app/models/spree/exchange_line_item.rb', line 100 def tax_category_id new_variant&.tax_category_id end |
#taxable_basis ⇒ Object
What the provider taxes the replacement on (see Spree::TaxProvider::Base#estimate_replacement).
96 97 98 |
# File 'app/models/spree/exchange_line_item.rb', line 96 def taxable_basis replacement_amount_for(credited_quantity) end |