Sha256: 8fb68b96794ad48ae4911ab76432d54822ad1b3a3f9ad2aa35ae01ebf029f660

Contents?: true

Size: 1.8 KB

Versions: 6

Compression:

Stored size: 1.8 KB

Contents

class Kaui::RefundsController < Kaui::EngineController

  def new
    cached_options_for_klient = options_for_klient

    fetch_invoice = promise { Kaui::Invoice.find_by_id(params.require(:invoice_id), 'NONE', cached_options_for_klient) }
    fetch_payment = promise { Kaui::InvoicePayment::find_by_id(params.require(:payment_id), false, false, cached_options_for_klient) }
    fetch_bundles = promise { @account.bundles(cached_options_for_klient) }

    @invoice = wait(fetch_invoice)
    @payment = wait(fetch_payment)
    @bundles = wait(fetch_bundles)

    @refund = KillBillClient::Model::InvoiceItem.new(:invoice_id => @invoice.invoice_id)
  end

  def create
    invoice = Kaui::Invoice.find_by_id(params.require(:invoice_id), 'NONE', options_for_klient)

    if params[:adjustment_type] == 'invoiceItemAdjustment'
      items = []
      (params.to_unsafe_h[:adjustments] || []).each do |ii|
        original_item = find_original_item(invoice.items, ii[0])

        item = KillBillClient::Model::InvoiceItem.new
        item.invoice_item_id = ii[0]
        # If we tried to do a partial item adjustment, we pass the value, if not we don't send any value and let the system
        # decide what is the maximum amount we can have on that item
        item.amount = (ii[1].to_f == original_item.amount) ? nil : ii[1]

        items << item
      end
    end

    KillBillClient::Model::InvoicePayment.refund(params.require(:payment_id), params[:amount], items, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
    redirect_to kaui_engine.account_invoice_path(invoice.account_id, invoice.invoice_id), :notice => 'Refund created'
  end

  private

  def find_original_item(items, item_id)
    items.each do |ii|
      if ii.invoice_item_id == item_id
        return ii
      end
    end
    nil
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kaui-2.1.0 app/controllers/kaui/refunds_controller.rb
kaui-2.0.4 app/controllers/kaui/refunds_controller.rb
kaui-2.0.3 app/controllers/kaui/refunds_controller.rb
kaui-2.0.2 app/controllers/kaui/refunds_controller.rb
kaui-2.0.1 app/controllers/kaui/refunds_controller.rb
kaui-2.0.0 app/controllers/kaui/refunds_controller.rb