Skip to content

Commit 1db0767

Browse files
authored
Merge pull request solidusio#6411 from SuperGoodSoft/fix-return-item-initialization
Fix return item initialization in backend
2 parents a0bc8d8 + a570cc0 commit 1db0767

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

backend/app/controllers/spree/admin/return_authorizations_controller.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ def load_return_items
4040
associated_inventory_units = @return_authorization.return_items.map(&:inventory_unit)
4141
unassociated_inventory_units = all_inventory_units - associated_inventory_units
4242

43-
new_return_items = unassociated_inventory_units.map do |new_unit|
44-
Spree::ReturnItem.new(inventory_unit: new_unit).tap(&:set_default_amount)
43+
unassociated_inventory_units.each do |new_unit|
44+
@return_authorization.return_items.
45+
build(inventory_unit: new_unit).
46+
tap(&:set_default_amount)
4547
end
46-
@form_return_items = (@return_authorization.return_items + new_return_items).sort_by(&:inventory_unit_id)
48+
49+
@form_return_items = @return_authorization.return_items.sort_by(&:inventory_unit_id)
4750
end
4851

4952
def load_reimbursement_types

backend/spec/controllers/spree/admin/return_authorizations_controller_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@
102102
expect(assigns(:form_return_items).size).to eq 3
103103
expect(assigns(:form_return_items).select(&:new_record?).size).to eq 3
104104
end
105+
106+
context 'with an order with currency other than the store default' do
107+
let!(:order) { create(:shipped_order, line_items_count: 3, currency: 'EUR') }
108+
109+
it 'sets the currency on the return items from the order' do
110+
subject
111+
expect(
112+
assigns(:form_return_items).all? { |ri|
113+
ri.display_amount.currency.iso_code == 'EUR'
114+
}
115+
).to be true
116+
end
117+
end
105118
end
106119
end
107120

core/app/models/spree/return_item.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,16 @@ def part_of_exchange?
177177
exchange_requested? || sibling_intended_for_exchange('unexchanged')
178178
end
179179

180+
def currency
181+
return_authorization.try(:currency) || Spree::Config[:currency]
182+
end
183+
180184
private
181185

182186
def persist_acceptance_status_errors
183187
update(acceptance_status_errors: validator.errors)
184188
end
185189

186-
def currency
187-
return_authorization.try(:currency) || Spree::Config[:currency]
188-
end
189-
190190
def process_inventory_unit!
191191
inventory_unit.return!
192192

core/spec/models/spree/return_item_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,23 @@
142142
end
143143
end
144144

145+
describe "#display_amount" do
146+
let(:amount) { 21.22 }
147+
let(:order) { build(:order, currency: 'EUR') }
148+
let(:return_authorization) { build(:return_authorization, order:) }
149+
let(:return_item) { build(:return_item, return_authorization:, amount:) }
150+
151+
context 'with an amount in the non-default currency' do
152+
it "returns a Spree::Money" do
153+
expect(return_item.display_amount).to eq Spree::Money.new(amount, currency: 'EUR')
154+
end
155+
end
156+
end
157+
145158
describe "#display_total_excluding_vat" do
146159
let(:amount) { 21.22 }
147160
let(:included_tax_total) { 1.22 }
148-
let(:return_item) { build(:return_item, amount:, included_tax_total:) }
161+
let(:return_item) { create(:return_item, amount:, included_tax_total:) }
149162

150163
it "returns a Spree::Money" do
151164
expect(return_item.display_total_excluding_vat).to eq Spree::Money.new(amount - included_tax_total)

0 commit comments

Comments
 (0)