Skip to content

Commit

Permalink
Create a valid reimbursement in sample
Browse files Browse the repository at this point in the history
The current code is not creating a valid reimbursement since it's
created when the associated order is still not in the riht state
(paid and shipped).

The amount of the reimbursement was also wrong, since the return
item created had an exchange variant associated, making the total
of the calculated reimboursement 0.

This was mainly used to allow seeing the reimbursement email in the
preview (with solidusio#1146), and it was working fine for that purpose but it
is creating an inconsistent order in the backend running samples.

Retruns are already complex and messed up, we need to avoid showing
wrong examples of code and order states to users.
  • Loading branch information
kennyadsl committed Feb 1, 2020
1 parent a363a74 commit d56cdb5
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions sample/db/samples/reimbursements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,43 @@

Spree::Sample.load_sample("orders")

order = Spree::Order.last
inventory_unit = order.inventory_units.first
order = Spree::Order.last
inventory_unit = order.inventory_units.take!
stock_location = inventory_unit.find_stock_item.stock_location
return_reason = Spree::ReturnReason.active.take!
preferred_reimbursement_type = Spree::ReimbursementType.where(name: 'Original').take!

return_item = Spree::ReturnItem.create(inventory_unit: inventory_unit)
# Mark the order paid and shipped
order.payments.pending.each(&:complete)
order.shipments.each do |shipment|
shipment.suppress_mailer = false
shipment.ship!
end

return_item.exchange_variant = return_item.eligible_exchange_variants.last
return_item.build_exchange_inventory_unit
return_item.accept!
# Create a return authorization
return_item = Spree::ReturnItem.new(
inventory_unit: inventory_unit,
preferred_reimbursement_type: preferred_reimbursement_type
)

customer_return = Spree::CustomerReturn.create(
stock_location: stock_location,
return_items: [return_item]
order.return_authorizations.create!(
reason: return_reason,
return_items: [return_item],
stock_location: stock_location
)

# Create a customer return and mark it as received
customer_return = Spree::CustomerReturn.create!(
return_items: [return_item],
stock_location: stock_location
)
return_item.reload
return_item.skip_customer_return_processing = true
return_item.receive!
customer_return.process_return!

# Accept the customer return and reimburse it
return_item.accept!
order.reimbursements.create(
customer_return: customer_return,
return_items: [return_item]
Expand Down

0 comments on commit d56cdb5

Please sign in to comment.