<commit_before>// Retrieve the RTP that match the data entered on the form so far.
// This means: On changes to provider, agency, supplier, start date, end date,
//  or invoice date, query for matching RTPs.
//  If exactly one is found, return a select drop-down that has the one item
//  selected.
//  If more than one, return the select drop-down with options but none
//  selected.
//  Otherwise, return a select with 'Out of pocket' selected.

$(document).on('turbolinks:load', function() {
  function id_of_interest(attribute) {
    return '#invoice_' + attribute;
  }

  function value_of_element(element) {
    // console.log(element.tagName);
    if (element.tagName == 'TEXTAREA') {
      return element.value;
    } else if (element.tagName == 'INPUT') {
      if (element.type == 'radio' && element.checked) {
        if (!element.value)
          return "on";
        else
          return element.value;
      } else if (element.type == 'checkbox' && element.checked) {
        if (!element.value)
          return "on";
        else
          return element.value;
      } else {
        return element.value;
      }
    } else if (element.tagName == 'SELECT' && element.selectedIndex != -1) {
      return element.options[element.selectedIndex].value;
    }
  }

  attributes_of_interest = [
    'invoice_from',
    'service_start',
    'service_end',
    'invoice_date',
    'funded_person_id'
  ];

  // Update Out of Pocket
  out_of_pocket_field = $('#invoice_out_of_pocket');
  invoice_amount_field = $('#invoice_invoice_amount');

  function allocation_fields() {
    a = $('input').filter(function() {
      return this.id.match(/invoice_allocations_attributes_[0-9]+_amount/);
    });
    // console.log(a);
    return a;
  }

  function update_out_of_pocket() {
    allocated_spending = allocation_fields().toArray().reduce(function(a, b) {
      // console.log('update_out_of_pocket b: ' + b.value);
      return b.value === undefined? a: a + Number(b.value);
    }, 0);
    out_of_pocket =
    Math.max(0,
      Number($('#invoice_invoice_amount').val()) - allocated_spending);
      // console.log('About to set Out of Pocket to ' + out_of_pocket);
      out_of_pocket_field.val(out_of_pocket.toFixed(2));
    }

    function set_up_triggers() {
      trigger_fields = $.merge($.merge(allocation_fields(),
                                       out_of_pocket_field),
                               invoice_amount_field);
      // console.log('Setting up triggers on ' + trigger_fields);
      trigger_fields.change(function() {
        update_out_of_pocket();
      });
    }

  if (document.getElementById('invoice')) {
    $.map(attributes_of_interest, function(id, key) {
      $(id_of_interest(id)).change(function() {
        // console.log('Something changed.');
        var params = {};
        attributes_of_interest.forEach(function(x) {
          // console.log(x + ': ' + value_of_element($(id_of_interest(x))[0]));
          params[x] = value_of_element($(id_of_interest(x))[0]);
        });

        // path = window.location.pathname;
        // url = path.substr(0, path.lastIndexOf('/'));
        // url = url.substr(0, url.lastIndexOf('/'));
        // console.log(url);

        // console.log('About to look for RTPs: ' + $.param(params));
        // TODO: Check that this doesn't return other people's RTPs
        var request = $.ajax({
          url: "/invoices/rtps",
          data: params,
          method: "GET",
          dataType: "html"<commit_msg>Fix formatting.<commit_after>// Retrieve the RTP that match the data entered on the form so far.
// This means: On changes to provider, agency, supplier, start date, end date,
//  or invoice date, query for matching RTPs.
//  If exactly one is found, return a select drop-down that has the one item
//  selected.
//  If more than one, return the select drop-down with options but none
//  selected.
//  Otherwise, return a select with 'Out of pocket' selected.

$(document).on('turbolinks:load', function() {
  function id_of_interest(attribute) {
    return '#invoice_' + attribute;
  }

  function value_of_element(element) {
    // console.log(element.tagName);
    if (element.tagName == 'TEXTAREA') {
      return element.value;
    } else if (element.tagName == 'INPUT') {
      if (element.type == 'radio' && element.checked) {
        if (!element.value)
          return "on";
        else
          return element.value;
      } else if (element.type == 'checkbox' && element.checked) {
        if (!element.value)
          return "on";
        else
          return element.value;
      } else {
        return element.value;
      }
    } else if (element.tagName == 'SELECT' && element.selectedIndex != -1) {
      return element.options[element.selectedIndex].value;
    }
  }

  attributes_of_interest = [
    'invoice_from',
    'service_start',
    'service_end',
    'invoice_date',
    'funded_person_id'
  ];

  // Update Out of Pocket
  out_of_pocket_field = $('#invoice_out_of_pocket');
  invoice_amount_field = $('#invoice_invoice_amount');

  function allocation_fields() {
    a = $('input').filter(function() {
      return this.id.match(/invoice_allocations_attributes_[0-9]+_amount/);
    });
    // console.log(a);
    return a;
  }

  function update_out_of_pocket() {
    allocated_spending = allocation_fields().toArray().reduce(function(a, b) {
      // console.log('update_out_of_pocket b: ' + b.value);
      return b.value === undefined? a: a + Number(b.value);
    }, 0);
    out_of_pocket =
    Math.max(0,
      Number($('#invoice_invoice_amount').val()) - allocated_spending);
    // console.log('About to set Out of Pocket to ' + out_of_pocket);
    out_of_pocket_field.val(out_of_pocket.toFixed(2));
  }

  function set_up_triggers() {
    trigger_fields = $.merge($.merge(allocation_fields(),
                                     out_of_pocket_field),
                             invoice_amount_field);
    // console.log('Setting up triggers on ' + trigger_fields);
    trigger_fields.change(function() {
      update_out_of_pocket();
    });
  }

  if (document.getElementById('invoice')) {
    $.map(attributes_of_interest, function(id, key) {
      $(id_of_interest(id)).change(function() {
        // console.log('Something changed.');
        var params = {};
        attributes_of_interest.forEach(function(x) {
          // console.log(x + ': ' + value_of_element($(id_of_interest(x))[0]));
          params[x] = value_of_element($(id_of_interest(x))[0]);
        });

        // path = window.location.pathname;
        // url = path.substr(0, path.lastIndexOf('/'));
        // url = url.substr(0, url.lastIndexOf('/'));
        // console.log(url);

        // console.log('About to look for RTPs: ' + $.param(params));
        // TODO: Check that this doesn't return other people's RTPs
        var request = $.ajax({
          url: "/invoices/rtps",
          data: params,
          method: "GET",
          dataType: "html"
{'fraction_non_alphanumeric': 0.14381797366914711, 'fraction_numerical': 0.0028620492272467086, 'mean_word_length': 2.5676365492598263, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4991608', 'n_tokens_mistral': 2187, 'n_tokens_neox': 2127, 'n_words': 474}