module Returnly class RefundPresenter def self.present_estimate(calculator) total = Money.new 0 sub_total = Money.new 0 calculator.line_items_return_items.values.flatten.each do |return_item| total += Money.from_amount return_item.total sub_total += Money.from_amount return_item.pre_tax_amount end { product_amount: sub_total.to_f, tax_amount: (total - sub_total).to_f, discount_amount: calculator.order.promo_total.to_f, total_refund_amount: (total - calculator.order.promo_total).to_f, refundable_shipping_amount: calculator.order.shipment_total.to_f, refundable_shipping_tax_amount: calculator.shipping_tax.to_f, max_refundable_amount: calculator.order.total.to_f, refunds_by_payment_method: refunds_by_payment(calculator.order) } end def self.present_line_items(calculator) calculator.line_items_return_items.values.map do |return_items| line_item = return_items.first.inventory_unit.line_item total_amount = 0.0 sub_total = 0.0 return_items.each do |return_item| total_amount += return_item.total sub_total += return_item.pre_tax_amount end { order_line_item_id: line_item.id, quantity: return_items.size, total_amount: total_amount.to_f, tax_amount: (total_amount - sub_total).to_f } end end def self.present_refund(refunder) customer_return = refunder.customer_return { refund_id: customer_return.number, line_items: customer_return.return_items.group_by { |ri| ri.inventory_unit.line_item.id }.map do |_, return_items| first_return_item = return_items.first { refund_line_item_id: first_return_item.id, order_line_item_id: first_return_item.inventory_unit.line_item.id, refunded_amount_per_item: first_return_item.amount.to_f, units: return_items.size } end, transactions: [ id: refunder.reimbursement.id, amount: refunder.reimbursement.total ] } end private_class_method def self.refunds_by_payment(order) order.payments.each_with_object([]) do |payment, payments| payments << { code: payment.source_type.constantize.model_name.human, amount: (order.item_total + order.adjustment_total).to_f, original_transaction_id: '' } end end end end