<%# Receive payment, and Edit payment (edit.html.erb) for a saved one %> <% editing = @form.persisted? %> <% title = editing ? l(:label_edit_payment) : l(:button_receive_payment) %> <% html_title title %> <% customer = Customer.find_by(id: @form.customer_id) if @form.customer_id.present? %>

<%= link_to l(:label_customers), customers_path %> » <% if customer %><%= link_to customer.name, customer_path(customer) %> »<% end %> <% if editing %> <%= link_to l(:label_payments), qbo_payments_path(customer_id: @form.customer_id) %> » <% end %> <%= title %>

<%= error_messages_for @form %> <% if @conflict %>

<%= l(:text_payment_reload) %> <%= link_to l(:button_reload_payment), edit_qbo_payment_path(@form.id) %>

<% end %> <% if @form.locked %>

<%= @form.locked %>

<% end %> <%= form_tag (editing ? qbo_payment_path(@form.id) : qbo_payments_path), method: (editing ? :patch : :post), id: 'qbo-payment-form', data: { open_invoices_url: open_invoices_qbo_payments_path, none_text: l(:label_no_open_invoices) } do %> <%= hidden_field_tag 'payment[sync_token]', @form.sync_token, id: nil if editing %>

<%# A saved payment keeps its customer %> <% if editing %> <%= text_field_tag 'payment[customer_name]', @form.customer_name, id: 'qbo_payment_customer_name', size: 40, readonly: true, tabindex: -1 %> <% else %> <%= text_field_tag 'payment[customer_name]', @form.customer_name, id: 'qbo_payment_customer_name', class: 'customer-name', autocomplete: 'off', size: 40, placeholder: l(:label_choose_a_customer), data: { autocomplete_url: '/customers/autocomplete' } %> <% end %> <%= hidden_field_tag 'payment[customer_id]', @form.customer_id, id: 'customer_id' %>

<%= date_field_tag 'payment[txn_date]', @form.txn_date, id: 'qbo_payment_txn_date' %>

<%= select_tag 'payment[payment_method_id]', options_for_select(@payment_methods, @form.payment_method_id), include_blank: true, id: 'qbo_payment_method' %> <% if @payment_methods.empty? %> <%= l(:text_no_payment_methods) %> <% end %>

<%= text_field_tag 'payment[reference]', @form.reference, id: 'qbo_payment_reference', size: 20, maxlength: 21 %> <%= l(:text_payment_reference) %>

<%= text_field_tag 'payment[amount]', @form.amount, id: 'qbo_payment_amount', size: 12, inputmode: 'decimal', autocomplete: 'off' %>

<%= hidden_field_tag 'payment[deposit]', '0', id: nil %> <%= check_box_tag 'payment[deposit]', '1', @form.deposit?, id: 'qbo_payment_deposit' %> <%= l(:text_payment_deposit) %>

<%= text_area_tag 'payment[memo]', @form.memo, id: 'qbo_payment_memo', rows: 3, cols: 60, maxlength: 4000 %>

<%# How the amount will be applied: the customer's open invoices, oldest first (worked out again when saving) %>
<%= l(:label_payment_applies_to) %>

<%= l(:text_payment_pick_invoices) %>

<% @open_invoices.each do |invoice| %> <% end %> <% if @open_invoices.empty? %> <% end %>
<%= l(:label_invoice) %> <%= l(:label_date) %> <%= l(:label_due_date) %> <%= l(:label_estimate_total) %> <%= l(:label_open_balance) %> <%= l(:label_payment_applied) %>
<%= check_box_tag 'payment[invoice_ids][]', invoice[:id], @form.invoice_ids.include?(invoice[:id]), id: nil, class: 'qbo-payment-pick' %> #<%= invoice[:doc_number] %> <%= invoice[:date] %> <%= invoice[:due_date] %> <%= number_to_currency(invoice[:total]) %> <%= number_to_currency(invoice[:balance]) %>
<%= l(:label_no_open_invoices) %>
<%= l(:label_payment_unapplied) %>

<%= submit_tag l(:button_save_payment) unless @form.locked %> <%= link_to l(:button_cancel), editing ? qbo_payment_path(@form.id) : (customer ? customer_path(customer) : home_path) %>

<% end %> <%= javascript_tag do %> $(function () { var $form = $('#qbo-payment-form'); if (!$form.length) return; var money = new Intl.NumberFormat(<%= raw (current_language.to_s.presence || 'en').to_json %>, { style: 'currency', currency: 'USD' }); var $rows = $('#qbo-payment-invoice-rows'); function amount() { var n = Number(String($('#qbo_payment_amount').val() || '').replace(/[$,\s]/g, '')); return isFinite(n) && n > 0 ? n : 0; } function picked() { return $rows.find('.qbo-payment-pick:checked'); } // Oldest first, like QuickBooks: on the ticked invoices if any are ticked, otherwise on all of them. // A deposit applies to nothing. function recalc() { var deposit = $('#qbo_payment_deposit').is(':checked'); $form.toggleClass('qbo-deposit', deposit); var left = deposit ? 0 : Math.round(amount() * 100); var onlyPicked = picked().length > 0; $rows.children('tr[data-balance]').each(function () { var balance = Math.round(parseFloat($(this).data('balance')) * 100); var counts = !onlyPicked || $(this).find('.qbo-payment-pick').is(':checked'); var take = counts ? Math.min(left, balance) : 0; left -= take; $(this).find('.qbo-payment-applied').text(take > 0 ? money.format(take / 100) : ''); }); var unapplied = deposit ? amount() : left / 100; $('#qbo-payment-unapplied').text(money.format(unapplied)); } function loadInvoices(customerId) { $rows.empty(); if (!customerId) { recalc(); return; } $.getJSON($form.data('openInvoicesUrl'), { customer_id: customerId }).done(function (invoices) { (invoices || []).forEach(function (invoice) { $('').attr('data-balance', invoice.balance).append( $('').append($('').val(invoice.id)), $('').text('#' + invoice.doc_number), $('').text(invoice.date), $('').text(invoice.due_date), $('').text(money.format(parseFloat(invoice.total || 0))), $('').text(money.format(parseFloat(invoice.balance))), $('') ).appendTo($rows); }); if (!$rows.children().length) { $('').append($('').text($form.data('noneText'))).appendTo($rows); } recalc(); }); } $('#customer_id').on('change', function () { loadInvoices(this.value); }); $('#qbo_payment_customer_name').on('input', function () { if (!this.value.trim()) $('#customer_id').val(''); }); // Ticking invoices fills in the amount: the total of their open balances function fillAmount() { var cents = 0; picked().each(function () { cents += Math.round(parseFloat($(this).closest('tr').data('balance')) * 100); }); if (picked().length) { $('#qbo_payment_amount').val((cents / 100).toFixed(2)); $('#qbo_payment_deposit').prop('checked', false); } var $all = $('#qbo-payment-all'); var boxes = $rows.find('.qbo-payment-pick'); $all.prop('checked', boxes.length > 0 && picked().length === boxes.length); recalc(); } $rows.on('change', '.qbo-payment-pick', fillAmount); $('#qbo-payment-all').on('change', function () { $rows.find('.qbo-payment-pick').prop('checked', this.checked); if (!this.checked) $('#qbo_payment_amount').val(''); fillAmount(); }); $('#qbo_payment_amount').on('input', recalc); $('#qbo_payment_deposit').on('change', recalc); $form.on('keydown', 'input[type=text]', function (e) { if (e.key === 'Enter' && $(this).hasClass('customer-name')) e.preventDefault(); }); recalc(); }); <% end %>