<%# Refund of completed work on a paid invoice: a QuickBooks Refund Receipt (InvoiceRefundWriter) %> <% html_title "#{l(:label_refund)} #{l(:label_refund_invoice_number, number: @invoice.doc_number)}" %> <% writer = RedmineQboBilling::RefundWriter %> <% money = ->(amount) { writer.money(amount) } %> <% plan = @plan %> <% number = @invoice.doc_number.to_s %>

<%= link_to l(:label_customers), customers_path %> » <%= link_to @customer.name, customer_path(@customer) %> » <%= link_to l(:label_refund_invoice_number, number: number), edit_qbo_invoice_path(@invoice.id) %> » <%= l(:label_refund) %>

<%= error_messages_for @form %>

<%= l(:text_refund_final_sale) %>

<% if @refunds.any? %>
<%= l(:text_refund_invoice_earlier) %> <%= safe_join(@refunds.map { |r| link_to("#{format_date(r.txn_date)} · #{money.(r.refund_amount)}", qbo_refund_path(r)) }, ', ') %>
<% end %> <%= form_tag create_invoice_qbo_refunds_path, method: :post, id: 'qbo-refund-form' do %> <%= hidden_field_tag 'refund[customer_id]', @customer.id, id: nil %> <%= hidden_field_tag 'refund[invoice_id]', @invoice.id, id: nil %>
<%= l(:label_refund_invoice_lines, number: number, date: format_date(@invoice.txn_date)) %> <% @lines.each do |line| %> <% choice = @form.line(line.key) %> <% field = "refund[lines][#{line.key}]" %> <% dom = "qbo_refund_line_#{line.key.parameterize(separator: '_')}" %> <% if line.refundable? %> <% else %> <% end %> <% end %>
<%= l(:label_refund_line) %> <%= l(:label_refund_line_billed) %> <%= l(:label_refund_line_refunded) %> <%= l(:label_refund_line_quantity) %> <%= l(:label_refund_line_amount) %>
<% if line.refundable? %> <%= hidden_field_tag "#{field}[refund]", '0', id: nil %> <%= check_box_tag "#{field}[refund]", '1', choice&.refund?, id: dom, class: 'qbo-refund-pick' %> <% end %> <%= l(line.parts ? :label_refund_parts : :label_refund_labor) %> <% if line.taxable %><%= l(:label_refund_taxable) %><% end %> <% if line.quantity && line.rate %><%= writer.plain(line.quantity) %> × <%= money.(line.rate) %>
<% end %> <%= money.(line.amount) %>
<%= line.refunded_amount.positive? ? money.(line.refunded_amount) : '' %> <% if line.quantity && line.rate %> <%= text_field_tag "#{field}[quantity]", choice&.quantity, size: 5, inputmode: 'decimal', autocomplete: 'off', class: 'qbo-refund-quantity', 'aria-label': l(:label_refund_line_quantity) %> / <%= writer.plain(line.remaining_quantity) %> <% end %> $<%= text_field_tag "#{field}[amount]", choice&.amount, size: 9, inputmode: 'decimal', autocomplete: 'off', class: 'qbo-refund-line-amount', 'aria-label': l(:label_refund_line_amount), data: { max: line.remaining_amount.to_s('F') } %> <%= l(:text_refund_line_done) %>

<%= l(:text_refund_invoice_lines) %>

<%= hidden_field_tag 'refund[charge_fee]', '0', id: nil %> <%= check_box_tag 'refund[charge_fee]', '1', @form.charge_fee?, id: 'qbo_refund_charge_fee' %> <%= l(:label_refund_fee_rate, rate: writer.plain(plan.fee_rate)) %> <%= l(:text_refund_charge_fee) %>

<%= l(:text_refund_method) %>

<%= text_field_tag 'refund[check_number]', @form.check_number, id: 'qbo_refund_check_number', size: 12, maxlength: 21 %>

<%= date_field_tag 'refund[txn_date]', @form.txn_date, id: 'qbo_refund_date' %>

<%= text_area_tag 'refund[reason]', @form.reason, id: 'qbo_refund_reason', rows: 3, cols: 60, maxlength: 2000 %> <%= l(:text_refund_invoice_reason) %>

<% [[:note_issues, :label_refund_note_issues], [:email_customer, :label_refund_email_customer]].each do |flag, label| %> <% end %> <% if @customer.email.blank? %><%= l(:text_refund_no_email) %><% end %>

<%= l(:label_refund_summary) %> >
<%= l(:label_refund_lines_amount) %><%= money.(plan.lines_amount) %>
<%= l(:label_refund_discount) %>-<%= money.(plan.discount) %>
<%= l(:label_refund_tax_estimate) %><%= money.(plan.tax) %>
<%= l(:label_refund_fee_on_parts) %> (<%= money.(plan.parts_amount) %>) -<%= money.(plan.fee) %>
<%= l(:label_refund_amount) %><%= money.(plan.refund) %>

<%= l(:text_refund_invoice_summary) %>

<%= submit_tag l(:button_refund), data: { confirm: l(:text_refund_invoice_confirm) }, disabled: @lines.none?(&:refundable?) %>

<% end %> <%= javascript_tag do %> $(function() { var table = $('#qbo-refund-lines table'); var money = function(n) { return '$' + n.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ','); }; var num = function(v) { return parseFloat(String(v || '').replace(/[$,\s]/g, '')) || 0; }; var round = function(n) { return Math.round(n * 100) / 100; }; function update() { var lines = 0, parts = 0, taxable = 0; $('tr.qbo-refund-line').each(function() { var row = $(this); if (!row.find('.qbo-refund-pick').is(':checked')) return; var amount = num(row.find('.qbo-refund-line-amount').val()); lines += amount; if (row.data('parts') == 1) parts += amount; if (row.data('taxable') == 1) taxable += amount; }); var discountRate = num(table.data('discount-rate')), taxRate = num(table.data('tax-rate')); var discount = round(lines * discountRate); var tax = round(taxable * (1 - discountRate) * taxRate); var fee = $('#qbo_refund_charge_fee').is(':checked') ? Math.round(parts * num(table.data('fee-rate'))) / 100 : 0; $('#qbo-refund-lines-amount').text(money(lines)); $('#qbo-refund-discount').text('-' + money(discount)); $('#qbo-refund-tax').text(money(tax)); $('#qbo-refund-parts').text(money(parts)); $('#qbo-refund-fee').text('-' + money(fee)); $('#qbo-refund-amount').text(money(Math.max(lines - discount + tax - fee, 0))); } // A quantity refunds that many at the line's rate (the amount can still be changed after) $('.qbo-refund-quantity').on('input', function() { var row = $(this).closest('tr'), rate = num(row.data('rate')); if (rate) row.find('.qbo-refund-line-amount').val(round(num($(this).val()) * rate).toFixed(2)); row.find('.qbo-refund-pick').prop('checked', num($(this).val()) > 0); update(); }); $('.qbo-refund-line-amount, #qbo_refund_charge_fee, .qbo-refund-pick').on('input change', update); function method() { $('#qbo-refund-check-number').toggle($('#qbo_refund_method_check').is(':checked')); } $('input[name="refund[method]"]').on('change', method); update(); method(); }); <% end %>