%# 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? %>
<%= 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 %>
<%= 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) { $('