From 8c4d12a501d6598979822fba0d0c725e84b27be7 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Wed, 13 Jan 2021 16:23:41 -0800 Subject: [PATCH] limit item counter to max quantity available even if amount is manually filled in --- .../admin/spree/orders/variant_autocomplete.js.erb | 10 +++++++++- .../spree/admin/orders/_shipment_manifest.html.haml | 2 +- config/locales/en.yml | 2 ++ spec/features/admin/order_spec.rb | 10 ++++++---- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb b/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb index c7237e6553..0bc4ba3f92 100644 --- a/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb +++ b/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb @@ -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), diff --git a/app/views/spree/admin/orders/_shipment_manifest.html.haml b/app/views/spree/admin/orders/_shipment_manifest.html.haml index 8798fd00b6..51d1571735 100644 --- a/app/views/spree/admin/orders/_shipment_manifest.html.haml +++ b/app/views/spree/admin/orders/_shipment_manifest.html.haml @@ -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) diff --git a/config/locales/en.yml b/config/locales/en.yml index 613f2adb45..c2b9b56697 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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..." diff --git a/spec/features/admin/order_spec.rb b/spec/features/admin/order_spec.rb index e34a0e6d79..1a85278b2d 100644 --- a/spec/features/admin/order_spec.rb +++ b/spec/features/admin/order_spec.rb @@ -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