<%= form_for(@refund, :url => { :action => :create }, :html => { :class => "form-horizontal" }) do |f| %> <%= hidden_field_tag :account_id, @account_id %> <%= hidden_field_tag :payment_id, @payment_id %> <%= hidden_field_tag :invoice_id, @invoice_id%>
<%= f.label :adjustment_type, "Adjustment Type", :class => "control-label" %>
<%= f.label :amount, "Refund amount", :class => "control-label" %>
<%= f.text_field :amount, :id => 'refund_amount', :value => @payment.paid_amount, :class => 'input-small' %>

<%= @account.currency %>

<%= label_tag :reason, "Reason", :class => "control-label" %>
<%= select_tag :reason, options_for_select(Kaui::Refund::SAMPLE_REASON_CODES) %>
<%= label_tag :comment, "Comment", :class => "control-label" %>
<%= text_area_tag :comment, "", :rows => 3, :class => 'input-xlarge' %>
<%= button_tag "Create refund", :id => "submit", :class =>"btn btn-primary" %> <%= link_to 'Back', :back, :class => 'btn' %>
<% end %> <%= javascript_tag do %> var textToDivId = function(textId) { return "div_" + textId.split("tf_adj_")[1]; }; var textToCheckboxId = function(textId) { return "cb_adj_" + textId.split("tf_adj_")[1]; }; var checkboxToTextId = function(cbId) { return "tf_adj_" + cbId.split("cb_adj_")[1]; }; var setClassForElement = function(id, newClass) { var currentClasses = $(id).attr('class').split(' '); for (var i=0; i Number($("#" + id).attr('originalValue'))) { setClassForElement($("#" + divId), "control-group error"); } else { setClassForElement($("#" + divId), "control-group"); } }; var validateRefundAmount = function() { if (Number($("#refund_amount").attr('value')) > <%= @payment.paid_amount %> || Number($("#refund_amount").attr('value')) <= 0) { setClassForElement("#div_refund_amount", "control-group error"); $("#submit").prop('disabled', true); } else { setClassForElement("#div_refund_amount", "control-group"); $("#submit").prop('disabled', false); } }; /* * Disabled unwanted invoice item at the time we submit the form */ var disableNonCheckedInvoiceItem = function() { $('input').filter(function() { return this.id.match(/cb_adj_/); }).each(function() { var id = checkboxToTextId(this.id); if (! $(this).is(':checked')) { $("#" + id).prop('disabled', true); } }); }; /* * Recompute refund amount based on adjustment type: * - For Invoice Item Adjustment, recompute price based on selection and invalidate text area to make it match exact selection * _ For Invoice adjustment or no adjustment, default to payment amount */ var recomputeRefundAmountAndValidateAmount = function() { var computedRefundAmount = <%= @payment.paid_amount %>; if ($("#refund_adjustment_type_invoiceitemadjustment").is(':checked')) { var x = 0; $('input').filter(function() { return this.id.match(/tf_adj_/) }).each(function() { var id = textToCheckboxId(this.id); if ($("#" + id).is(':checked')) { x = x + Number(this.value); } }); computedRefundAmount = x.toFixed(2); $("#refund_amount").attr('value', computedRefundAmount); $("#refund_amount").prop('readonly', true); } else { $("#refund_amount").attr('value', computedRefundAmount); $("#refund_amount").prop('readonly', false); } validateRefundAmount(); }; /* * When clicking checkbox for each item, disable amount and recompute total refund amount */ var onClickInvoiceItemAdjustment = function(event) { var id = checkboxToTextId(this.id); if ($(this).is(':checked')) { $("#" + id).prop('readonly', true); } else { $("#" + id).prop('readonly', false); $("#" + id).attr('value', $("#" + id).attr('originalValue')); } recomputeRefundAmountAndValidateAmount(); validateInvoiceItemAmount(id); }; /* * When selecting Invoice Adjustment or No Adjustment, hide invoice items and recompute refund Amount */ var onClickWithInvoiceOrNoAdjustment = function(event) { $("#invoiceItems").hide(); recomputeRefundAmountAndValidateAmount(); }; /* * When selecting Invoice Item Adjustment, show items and recompute refund amount */ var onClickWithInvoiceItemAdjustment = function(event) { $("#invoiceItems").show(); recomputeRefundAmountAndValidateAmount(); }; /* * Attach all handlers when page loads */ $(document).ready(function() { /* * For refund amount text area, attach handler to disable 'ENTER' and also prevent bad values */ $("#refund_amount").keydown(function(event) { preventNonNumericValues(event); }).bind('keypress', function(e) { if ((e.keyCode || e.which) == 13) { return false; } }).blur(function(e) { validateRefundAmount(); }); /* * If doing invoice Item adjustment, disable entries that were not selected, so the controllers does not * get them */ $("#submit").click(disableNonCheckedInvoiceItem); /* * Adjustment type handlers */ $("#refund_adjustment_type_noinvoiceadjustment") .click(onClickWithInvoiceOrNoAdjustment); $("#refund_adjustment_type_invoiceadjustment") .click(onClickWithInvoiceOrNoAdjustment); $("#refund_adjustment_type_invoiceitemadjustment") .click(onClickWithInvoiceItemAdjustment); /* * Attach handler onClickInvoiceItemAdjustment for all invoice item checkbox */ $('input').filter(function() { return this.id.match(/cb_adj_/); }).click(onClickInvoiceItemAdjustment); /* * Attach handler for all invoice item text areas so that: * - We disable posting form when pressing 'ENTER' * - Automatically select the matching checkbox on 'ENTER' and disable checkbox */ $('input').filter(function() { return this.id.match(/tf_adj_/); }).each( function() { var originalValue = this.value; $(this).attr('originalValue', originalValue); $(this).bind('keypress', function(e) { if ((e.keyCode || e.which) == 13) { var id = textToCheckboxId(this.id); $("#" + id).attr('checked', true); $(this).prop('readonly', true); recomputeRefundAmountAndValidateAmount(); validateInvoiceItemAmount(this.id); return false; } }); }); }); <% end %>