Class: Returnly::RefundPresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/returnly/refund_presenter.rb

Class Method Summary collapse

Class Method Details

.present_estimate(calculator) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/returnly/refund_presenter.rb', line 4

def self.present_estimate(calculator)
  sub_total = Money.new 0
  tax = Money.new 0

  return_items = calculator.line_items_return_items
  return_items[:items].values.flatten.each do |return_item|
    sub_total += Money.from_amount return_item.total_excluding_vat
    tax += Money.from_amount calculator.line_item_tax(return_item.inventory_unit.line_item)
  end

  discount = Money.from_amount(calculator.discount)
  if return_items[:is_returned_items]
    sub_total += discount
  end
  total_refund_amount = sub_total + tax - (discount * -1) # discount is negative, so we multiply it by -1 to make it positive for subtraction
  shipping_tax        = calculator.shipping_tax
  {
    product_amount:                 sub_total.to_f,
    tax_amount:                     tax.to_f,
    discount_amount:                discount.to_f,
    total_refund_amount:            total_refund_amount.to_f,
    refundable_shipping_amount:     calculator.order.shipment_total.to_f,
    refundable_shipping_tax_amount: shipping_tax.to_f,
    max_refundable_amount:          (calculator.order.total - calculator.order.refunds.reload.sum(&:amount)).to_f,
    refunds_by_payment_method:      refunds_by_payment(calculator.order, total_refund_amount.to_d + calculator.order.shipment_total + shipping_tax)
  }
end

.present_refund(refunder) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/returnly/refund_presenter.rb', line 32

def self.present_refund(refunder)
  customer_return = refunder.customer_return

  {
    refund_id:    refunder.order.reload.refunds.last.try(:id).try(:to_s),
    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.to_s,
        order_line_item_id:  first_return_item.inventory_unit.line_item.id.to_s,
        units:               return_items.size,
        restock:             first_return_item.resellable
      }
    end,
    transactions: refunder.reimbursement.refunds.map do |refund|
      {
        id: refund.transaction_id,
        amount: refund.amount.to_f,
        code: refund.payment.source_type.constantize.model_name.human
      }
    end
  }
end

.present_refund_with_zero_amount(refunder) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/returnly/refund_presenter.rb', line 56

def self.present_refund_with_zero_amount(refunder)
  {
    refund_id: '',
    line_items: refunder.line_items.map do |line_item|
      {
        refund_line_item_id: '',
        order_line_item_id: line_item[:order_line_item_id].to_s,
        units: line_item[:units].to_i,
        restock: !!(line_item[:restock].to_s =~ /true/)
      }
    end,
    transactions: []
  }
end