<commit_before>// Shipments AJAX API
$(document).ready(function () {
  'use strict';

  // handle variant selection, show stock level.
  $('#add_variant_id').change(function(){
    var variant_id = $(this).val();

    var variant = _.find(window.variants, function(variant){
      return variant.id == variant_id
    })
    $('#stock_details').html(variantStockTemplate({variant: variant}));
    $('#stock_details').show();

    $('button.add_variant').click(addVariantFromStockLocation);
  });

  //handle edit click
  $('a.edit-item').click(toggleItemEdit);

  //handle cancel click
  $('a.cancel-item').click(toggleItemEdit);

  //handle split click
  $('a.split-item').click(startItemSplit);

  //handle save click
  $('a.save-item').click(function(){
    var save = $(this);
    var shipment_number = save.data('shipment-number');
    var variant_id = save.data('variant-id');

    var quantity = parseInt(save.parents('tr').find('input.line_item_quantity').val());
    var subscribe = save.parents('tr').find("#item_auto_delivery_true:checked").val();
      //var delivery_number = 1000; //$("input.delivery_number[data-variant-id='" + variant_id + "']").val();
    var frequency = save.parents('tr').find("select#item_frequency").val();

    toggleItemEdit();
    adjustShipmentItems(shipment_number, variant_id, quantity, subscribe, frequency);
    return false;
  });

  //handle delete click
  $('a.delete-item').click(function(event){
    if (confirm(Spree.translations.are_you_sure_delete)) {
      var del = $(this);
      var shipment_number = del.data('shipment-number');
      var variant_id = del.data('variant-id');
      var shipment = _.findWhere(shipments, {number: shipment_number + ''});
      var url = Spree.routes.shipments_api + "/" + shipment_number + '/remove';

      toggleItemEdit();

      $.ajax({
        type: "PUT",
        url: Spree.url(url),
        data: {
          variant_id: variant_id,
          token: Spree.api_key
        }
      }).done(function( msg ) {
        window.location.reload();
      }).fail(function(msg) {
        alert(msg.responseJSON.message)
      });
    }
    return false;
  });

  // handle ship click
  $('[data-hook=admin_shipment_form] a.ship').on('click', function () {
    var link = $(this);
    var shipment_number = link.data('shipment-number');
    var url = Spree.url(Spree.routes.shipments_api + '/' + shipment_number + '/ship.json');
    $.ajax({
      type: 'PUT',
      url: url,
      data: {
        token: Sp<PASSWORD>.api_key
      }
    }).done(function () {
      window.location.reload();
    }).error(function (msg) {
      console.log(msg);
    });
  });

  // handle shipping method edit click
  $('a.edit-method').click(toggleMethodEdit);
  $('a.cancel-method').click(toggleMethodEdit);

  // handle shipping method save
  $('[data-hook=admin_shipment_form] a.save-method').on('click', function (event) {
    event.preventDefault();

    var link = $(this);
    var shipment_number = link.data('shipment-number');
    var selected_shipping_rate_id = link.parents('tbody').find("select#selected_shipping_rate_id[data-shipment-number='" + shipment_number + "']").val();
    var unlock = link.parents('tbody').find("input[name='open_adjustment'][data-shipment-number='" + shipment_number + "']:checked").val();
    var url = Spree.url(Spree.routes.shipments_api + '/' + shipment_number + '.json');

    $.ajax({
      type: 'PUT',
      url: url,
      data: {
        shipment: {
          selected_shipping_rate_id: selected_shipping_rate_id,
          unlock: unlock
        },
        token: Spree.api_key
      }
    }).done(function () {
      window.location.reload();
    }).error(function (msg) {
      console.log(msg);
    });
  });

  var toggleTrackingEdit = function(event) {
    event.preventDefault();

    var link = $(this);
    link.parents('tbody').find('tr.edit-tracking').toggle();
    link.parents('tbody').find('tr.show-tracking').toggle();
  }

  // handle tracking edit click
  $('a.edit-tracking').click(toggleTrackingEdit);
  $('a.cancel-tracking').click(toggleTrackingEdit);

  // handle tracking save
  $('[data-hook=admin_shipment_form] a.save-tracking').on('click', function (event) {
    event.preventDefault();

    var link = $(this);
    var shipment_number = link.data('shipment-number');
    var tracking = link.parents('tbody').find('input#tracking').val();
    var url = Spree.url(Spree.routes.shipments_api + '/' + shipment_number + '.json');

    $.ajax({
      type: 'PUT',
      url: url,
      data: {
        shipment: {
          tracking: tracking
        },
        token: Spree.api_key
      }
    }).done(function (data) {
      link.parents('tbody').find('tr.edit-tracking').toggle();

      var show = link.parents('tbody').find('tr.show-tracking');
      show.toggle();
      show.find('.tracking-value').html($("<strong>").html(Spree.translations.tracking + ": ")).append(data.tracking);
    });
  });
});

adjustShipmentItems = function(shipment_number, variant_id, quantity, auto_delivery, frequency){
    var shipment = _.findWhere(shipments, {number: shipment_number + ''});
    var inventory_units = _.where(shipment.inventory_units, {variant_id: variant_id});

    var url = Spree.routes.shipments_api + "/" + shipment_number;

    var previous_quantity = inventory_units.reduce(function(accumulator, current_unit, _index, _array) {
      return accumulator + current_unit.quantity;
    }, 0);

    if(previous_quantity<quantity){
      url += "/add"
      new_quantity = (quantity - previous_quantity);
    }else if(previous_quantity>quantity){
      url += "/remove"
      new_quantity = (previous_quantity - quantity);
    }
    url += '.json';

    if(new_quantity!=0){
      $.ajax({
        type: "PUT",
        url: Spree.url(url),
        data: {
          variant_id: variant_id,
          quantity: new_quantity,
          options: {
            auto_delivery: auto_delivery,
            frequency: frequency
          },
          token: Spree.api_key
        }
      }).done(function( msg ) {
        window.location.reload();
      }).fail(function(msg) {
        alert(msg.responseJSON.message)
      });
    }
}<commit_msg>fix update AutoDelivery option for SKU with no quantity changes<commit_after>// Shipments AJAX API
$(document).ready(function () {
  'use strict';

  // handle variant selection, show stock level.
  $('#add_variant_id').change(function(){
    var variant_id = $(this).val();

    var variant = _.find(window.variants, function(variant){
      return variant.id == variant_id
    })
    $('#stock_details').html(variantStockTemplate({variant: variant}));
    $('#stock_details').show();

    $('button.add_variant').click(addVariantFromStockLocation);
  });

  //handle edit click
  $('a.edit-item').click(toggleItemEdit);

  //handle cancel click
  $('a.cancel-item').click(toggleItemEdit);

  //handle split click
  $('a.split-item').click(startItemSplit);

  //handle save click
  $('a.save-item').click(function(){
    var save = $(this);
    var shipment_number = save.data('shipment-number');
    var variant_id = save.data('variant-id');

    var quantity = parseInt(save.parents('tr').find('input.line_item_quantity').val());
    var subscribe = save.parents('tr').find("#item_auto_delivery_true:checked").val();
      //var delivery_number = 1000; //$("input.delivery_number[data-variant-id='" + variant_id + "']").val();
    var frequency = save.parents('tr').find("select#item_frequency").val();

    toggleItemEdit();
    adjustShipmentItems(shipment_number, variant_id, quantity, subscribe, frequency);
    return false;
  });

  //handle delete click
  $('a.delete-item').click(function(event){
    if (confirm(Spree.translations.are_you_sure_delete)) {
      var del = $(this);
      var shipment_number = del.data('shipment-number');
      var variant_id = del.data('variant-id');
      var shipment = _.findWhere(shipments, {number: shipment_number + ''});
      var url = Spree.routes.shipments_api + "/" + shipment_number + '/remove';

      toggleItemEdit();

      $.ajax({
        type: "PUT",
        url: Spree.url(url),
        data: {
          variant_id: variant_id,
          token: Spree.api_key
        }
      }).done(function( msg ) {
        window.location.reload();
      }).fail(function(msg) {
        alert(msg.responseJSON.message)
      });
    }
    return false;
  });

  // handle ship click
  $('[data-hook=admin_shipment_form] a.ship').on('click', function () {
    var link = $(this);
    var shipment_number = link.data('shipment-number');
    var url = Spree.url(Spree.routes.shipments_api + '/' + shipment_number + '/ship.json');
    $.ajax({
      type: 'PUT',
      url: url,
      data: {
        token: Spree.api_key
      }
    }).done(function () {
      window.location.reload();
    }).error(function (msg) {
      console.log(msg);
    });
  });

  // handle shipping method edit click
  $('a.edit-method').click(toggleMethodEdit);
  $('a.cancel-method').click(toggleMethodEdit);

  // handle shipping method save
  $('[data-hook=admin_shipment_form] a.save-method').on('click', function (event) {
    event.preventDefault();

    var link = $(this);
    var shipment_number = link.data('shipment-number');
    var selected_shipping_rate_id = link.parents('tbody').find("select#selected_shipping_rate_id[data-shipment-number='" + shipment_number + "']").val();
    var unlock = link.parents('tbody').find("input[name='open_adjustment'][data-shipment-number='" + shipment_number + "']:checked").val();
    var url = Spree.url(Spree.routes.shipments_api + '/' + shipment_number + '.json');

    $.ajax({
      type: 'PUT',
      url: url,
      data: {
        shipment: {
          selected_shipping_rate_id: selected_shipping_rate_id,
          unlock: unlock
        },
        token: Spree.api_key
      }
    }).done(function () {
      window.location.reload();
    }).error(function (msg) {
      console.log(msg);
    });
  });

  var toggleTrackingEdit = function(event) {
    event.preventDefault();

    var link = $(this);
    link.parents('tbody').find('tr.edit-tracking').toggle();
    link.parents('tbody').find('tr.show-tracking').toggle();
  }

  // handle tracking edit click
  $('a.edit-tracking').click(toggleTrackingEdit);
  $('a.cancel-tracking').click(toggleTrackingEdit);

  // handle tracking save
  $('[data-hook=admin_shipment_form] a.save-tracking').on('click', function (event) {
    event.preventDefault();

    var link = $(this);
    var shipment_number = link.data('shipment-number');
    var tracking = link.parents('tbody').find('input#tracking').val();
    var url = Spree.url(Spree.routes.shipments_api + '/' + shipment_number + '.json');

    $.ajax({
      type: 'PUT',
      url: url,
      data: {
        shipment: {
          tracking: tracking
        },
        token: Spree.api_key
      }
    }).done(function (data) {
      link.parents('tbody').find('tr.edit-tracking').toggle();

      var show = link.parents('tbody').find('tr.show-tracking');
      show.toggle();
      show.find('.tracking-value').html($("<strong>").html(Spree.translations.tracking + ": ")).append(data.tracking);
    });
  });
});

adjustShipmentItems = function(shipment_number, variant_id, quantity, auto_delivery, frequency){
    var shipment = _.findWhere(shipments, {number: shipment_number + ''});
    var inventory_units = _.where(shipment.inventory_units, {variant_id: variant_id});

    var url = Spree.routes.shipments_api + "/" + shipment_number;

    var previous_quantity = inventory_units.reduce(function(accumulator, current_unit, _index, _array) {
      return accumulator + current_unit.quantity;
    }, 0);

    if(previous_quantity<quantity){
      url += "/add"
      new_quantity = (quantity - previous_quantity);
    }else if(previous_quantity>quantity){
      url += "/remove"
      new_quantity = (previous_quantity - quantity);
    }else{
        url += "/add"
        new_quantity =0;
    }
    url += '.json';

      $.ajax({
        type: "PUT",
        url: Spree.url(url),
        data: {
          variant_id: variant_id,
          quantity: new_quantity,
          options: {
            auto_delivery: auto_delivery,
            frequency: frequency
          },
          token: Spree.api_key
        }
      }).done(function( msg ) {
        window.location.reload();
      }).fail(function(msg) {
        alert(msg.responseJSON.message)
      });
}
{'fraction_non_alphanumeric': 0.1695037738879075, 'fraction_numerical': 0.0009635458487233017, 'mean_word_length': 3.0216338391992252, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '408307', 'n_tokens_mistral': 3942, 'n_tokens_neox': 3808, 'n_words': 671}