Merge pull request #6658 from andrewpbrett/item-counter-fix

Further fix #5989 (Item counter accepts values higher than the available stock)
This commit is contained in:
Andy Brett
2021-01-21 10:56:18 -08:00
committed by GitHub
4 changed files with 18 additions and 6 deletions

View File

@@ -40,7 +40,13 @@ $(document).ready(function() {
var variant_id = save.data('variant-id');
var quantity = parseInt(save.parents('tr').find('input.line_item_quantity').val());
var maxQuantity = parseInt(save.parents('tr').find('input.line_item_quantity').attr("max"));
if (quantity > maxQuantity) {
quantity = maxQuantity;
save.parents('tr').find('input.line_item_quantity').val(maxQuantity);
alert('<%= I18n.t("js.admin.orders.quantity_adjusted") %>');
}
toggleItemEdit();
adjustItems(shipment_number, variant_id, quantity);
@@ -77,7 +83,9 @@ adjustItems = function(shipment_number, variant_id, quantity){
}
url += '.json';
if(new_quantity!=0){
if (new_quantity == 0) {
alert('<%= I18n.t("js.admin.orders.quantity_unchanged") %>');
} else {
$.ajax({
type: "PUT",
url: Spree.url(url),

View File

@@ -14,7 +14,7 @@
= "#{count} x #{t(state.humanize.downcase, scope: [:spree, :shipment_states], default: [:missing, "none"])}"
- unless shipment.shipped?
%td.item-qty-edit.hidden
= number_field_tag :quantity, item.quantity, :min => 0, :class => "line_item_quantity", :size => 5, :max => item.variant.on_hand
= number_field_tag :quantity, item.quantity, :min => 0, :class => "line_item_quantity", :size => 5, :max => item.variant.on_hand + item.quantity
%td.item-total.align-center
= line_item_shipment_price(line_item, item.quantity)

View File

@@ -2717,6 +2717,8 @@ See the %{link} to find out more about %{sitename}'s features and to start using
processing: "processing"
void: "void"
invalid: "invalid"
quantity_adjusted: "Insufficient stock available. Line item updated to maximum available quantity."
quantity_unchanged: "Quantity unchanged from previous amount."
resend_user_email_confirmation:
resend: "Resend"
sending: "Resend..."

View File

@@ -127,21 +127,23 @@ feature '
login_as_admin_and_visit spree.edit_admin_order_path(order)
quantity = order.line_items.first.quantity
max_quantity = 0
total = order.display_total
within("tr.stock-item", text: order.products.first.name) do
find("a.edit-item").click
expect(page).to have_input(:quantity)
fill_in(:quantity, with: order.line_items.first.product.on_hand + 1)
max_quantity = find("input[name='quantity']")["max"].to_i
fill_in(:quantity, with: max_quantity + 1)
find("a.save-item").click
end
accept_js_alert
expect(page).to_not have_content "Loading..."
within("tr.stock-item", text: order.products.first.name) do
expect(page).to have_text("#{quantity} x")
expect(page).to have_text("#{max_quantity} x")
end
expect(order.reload.display_total).to eq(total)
expect(order.reload.line_items.first.quantity).to eq(quantity)
expect(order.reload.line_items.first.quantity).to eq(max_quantity)
end
scenario "can't change distributor or order cycle once order has been finalized" do