Class: Spree::Return
- Inherits:
-
Object
- Object
- Spree::Return
- Includes:
- ActedBy, HasCustomFields, HasExternalReferences, HasStatus, Metadata, NumberIdentifier, PostSale::Taxation, SingleStoreResource
- Defined in:
- app/models/spree/return.rb
Overview
A customer sends items back and gets their money back.
One record owns the whole operation — permission, physical receipt and money back (docs/plans/6.0-returns-exchanges-claims.md), replacing the ReturnAuthorization → CustomerReturn → Reimbursement chain removed in 6.0.
The model holds associations, validations and pure reads only. Every
transition is a workflow — Spree::Returns::Receive.call(...), never
return.receive! — so receiving can take the quantities the warehouse
actually counted, and refunding can call a payment gateway outside the
transaction that writes the status.
Instance Method Summary collapse
-
#active_shipping_label ⇒ Spree::ShippingLabel?
The label that currently binds the inbound parcel — bought or uploaded, not refunded.
-
#counted? ⇒ Boolean
Whether the warehouse has reported on this return.
- #display_refund_tax_total ⇒ Object
- #display_refund_total ⇒ Object
- #display_refunded_total ⇒ Object
-
#provider ⇒ Spree::FulfillmentProvider::Base
The provider that ships this return's label — the one that handled the outbound parcel the returned items travelled in, since that is the carrier account the merchant connected.
-
#refund_tax_total ⇒ Object
The tax inside #refund_total.
-
#refund_total ⇒ Object
What the customer is owed for the items being returned, tax included.
- #refundable_total ⇒ Object
-
#refunded_line_amounts ⇒ Hash{Integer => BigDecimal}
What a refund from this return paid for, line by line.
-
#refunded_line_taxes ⇒ Hash{Integer => BigDecimal}
The tax inside #refunded_line_amounts, for the lines whose tax is recorded.
-
#refunded_total ⇒ Object
What has actually been refunded so far — a return can be refunded in more than one step (part to store credit, part to the card), and store credit is its own ledger rather than a Spree::Refund row.
-
#returned_units ⇒ Array<Spree::FulfillmentItem>
The units as the parcel coming back holds them: a partial return sends back what the customer is returning, not everything that shipped.
-
#ship_from_address ⇒ Spree::Address?
The address the inbound parcel leaves from: where the order shipped to.
- #taxed_lines ⇒ Object
-
#to_package ⇒ Spree::Stock::Package
The returned goods as a package, for providers that need a weight and dimensions to rate a return label.
Methods included from PostSale::Taxation
#calculate_tax!, #clear_tax!, #credited_tax_total, #settle_tax!
Methods included from Metadata
#private_metadata, #private_metadata=
Methods included from HasExternalReferences
#assign_external_references, #external_id_for, #set_external_id
Methods included from ActedBy
#acted_by_prefixed_id, #acted_by_type, assume_admin_actor_type, columns_for, models, registered_model_names
Instance Method Details
#active_shipping_label ⇒ Spree::ShippingLabel?
The label that currently binds the inbound parcel — bought or uploaded, not refunded. The label workflows refuse a second active one.
63 64 65 |
# File 'app/models/spree/return.rb', line 63 def active_shipping_label shipping_labels.active.last end |
#counted? ⇒ Boolean
Whether the warehouse has reported on this return.
120 121 122 |
# File 'app/models/spree/return.rb', line 120 def counted? received? || refunded? end |
#display_refund_tax_total ⇒ Object
189 190 191 |
# File 'app/models/spree/return.rb', line 189 def display_refund_tax_total Spree::Money.new(refund_tax_total, currency: currency) end |
#display_refund_total ⇒ Object
181 182 183 |
# File 'app/models/spree/return.rb', line 181 def display_refund_total Spree::Money.new(refund_total, currency: currency) end |
#display_refunded_total ⇒ Object
185 186 187 |
# File 'app/models/spree/return.rb', line 185 def display_refunded_total Spree::Money.new(refunded_total, currency: currency) end |
#provider ⇒ Spree::FulfillmentProvider::Base
The provider that ships this return's label — the one that handled the outbound parcel the returned items travelled in, since that is the carrier account the merchant connected. Manual when nothing shipped through a provider.
73 74 75 |
# File 'app/models/spree/return.rb', line 73 def provider return_line_items.first&.fulfillment_item&.fulfillment&.provider || Spree::FulfillmentProvider::Manual.new end |
#refund_tax_total ⇒ Object
The tax inside #refund_total.
130 131 132 |
# File 'app/models/spree/return.rb', line 130 def refund_tax_total return_line_items.sum(&:refund_tax_amount) end |
#refund_total ⇒ Object
What the customer is owed for the items being returned, tax included.
125 126 127 |
# File 'app/models/spree/return.rb', line 125 def refund_total return_line_items.sum(&:refund_amount) end |
#refundable_total ⇒ Object
177 178 179 |
# File 'app/models/spree/return.rb', line 177 def refundable_total refund_total - refunded_total end |
#refunded_line_amounts ⇒ Hash{Integer => BigDecimal}
What a refund from this return paid for, line by line. Only ever what the warehouse counted: the seller's clawback is built from this, and a line nobody counted earned nothing back.
139 140 141 142 143 144 145 146 147 148 |
# File 'app/models/spree/return.rb', line 139 def refunded_line_amounts return {} unless counted? return_line_items.each_with_object(Hash.new(0)) do |line, amounts| amount = line.refund_amount next if amount.zero? amounts[line.line_item_id] += amount end end |
#refunded_line_taxes ⇒ Hash{Integer => BigDecimal}
The tax inside #refunded_line_amounts, for the lines whose tax is recorded.
154 155 156 157 158 159 160 161 162 |
# File 'app/models/spree/return.rb', line 154 def refunded_line_taxes return {} unless counted? return_line_items.each_with_object(Hash.new(0)) do |line, taxes| next if line.refund_amount.zero? || !line.settles_tax? taxes[line.line_item_id] += line.refund_tax_amount end end |
#refunded_total ⇒ Object
What has actually been refunded so far — a return can be refunded in more than one step (part to store credit, part to the card), and store credit is its own ledger rather than a Spree::Refund row. A failed refund gave nothing back; one still requested holds its amount.
168 169 170 171 172 173 174 175 |
# File 'app/models/spree/return.rb', line 168 def refunded_total refunds_given = if read_association_in_memory?(:refunds) refunds.reject(&:failed?).sum(0.to_d, &:amount) else refunds.not_failed.sum(:amount) end refunds_given + ledger_total(:store_credits) end |
#returned_units ⇒ Array<Spree::FulfillmentItem>
The units as the parcel coming back holds them: a partial return sends back what the customer is returning, not everything that shipped. The copies are never saved — the shipment's own record of what left is not ours to rewrite, and it is what a second return is measured against.
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/spree/return.rb', line 103 def returned_units return_line_items.includes(:fulfillment_item).filter_map do |return_line_item| unit = return_line_item.fulfillment_item next if unit.nil? quantity = return_line_item.quantity.to_i next unit if quantity <= 0 || quantity == unit.quantity.to_i # `dup` rather than a new record: the packer reads variant, line item # and status off the unit, and a copy carries all of it. unit.dup.tap { |returned| returned.quantity = quantity } end end |
#ship_from_address ⇒ Spree::Address?
The address the inbound parcel leaves from: where the order shipped to.
80 81 82 |
# File 'app/models/spree/return.rb', line 80 def ship_from_address order.ship_address end |
#taxed_lines ⇒ Object
193 194 195 |
# File 'app/models/spree/return.rb', line 193 def taxed_lines return_line_items end |
#to_package ⇒ Spree::Stock::Package
The returned goods as a package, for providers that need a weight and dimensions to rate a return label. Built at the return's stock location, which is where the parcel is going.
89 90 91 92 93 94 95 |
# File 'app/models/spree/return.rb', line 89 def to_package package = Spree::Stock::Package.new(stock_location) package.owner = order units = returned_units units.group_by(&:status).each { |status, items| package.add_multiple(items, status.to_sym) } package end |