From 567196fe0e11508dd096c58dcd26c5c15a6fcb81 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 7 Jun 2019 12:06:05 +0100 Subject: [PATCH] Fix line item verification of stock on the browser side by adding logic to handle completed orders with some reserved stock The shopping/orders_spec is now validating this edge case by using all stock available in one of the line items --- .../darkswarm/directives/on_hand.js.coffee | 8 +++++--- app/views/spree/orders/_line_item.html.haml | 3 ++- spec/features/consumer/shopping/orders_spec.rb | 11 +++++++---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/darkswarm/directives/on_hand.js.coffee b/app/assets/javascripts/darkswarm/directives/on_hand.js.coffee index 9a849029b4..0173196138 100644 --- a/app/assets/javascripts/darkswarm/directives/on_hand.js.coffee +++ b/app/assets/javascripts/darkswarm/directives/on_hand.js.coffee @@ -14,9 +14,11 @@ Darkswarm.directive "ofnOnHand", -> ngModel.$parsers.push (viewValue) -> on_hand = parseInt(attr.ofnOnHand) - if parseInt(viewValue) > on_hand - alert t("js.insufficient_stock", {on_hand: on_hand}) - viewValue = on_hand + finalized_quantity = parseInt(attr.finalizedquantity) + available_quantity = on_hand + finalized_quantity + if parseInt(viewValue) > available_quantity + alert t("js.insufficient_stock", {on_hand: available_quantity}) + viewValue = available_quantity ngModel.$setViewValue viewValue ngModel.$render() diff --git a/app/views/spree/orders/_line_item.html.haml b/app/views/spree/orders/_line_item.html.haml index 14ef285565..cfd4d4a500 100644 --- a/app/views/spree/orders/_line_item.html.haml +++ b/app/views/spree/orders/_line_item.html.haml @@ -22,7 +22,8 @@ %td.text-right.cart-item-price{"data-hook" => "cart_item_price"} = line_item.single_display_amount_with_adjustments.to_html %td.text-center.cart-item-quantity{"data-hook" => "cart_item_quantity"} - = item_form.number_field :quantity, :min => 0, "ofn-on-hand" => "#{variant.on_demand && 9999 || variant.on_hand}", "ng-model" => "line_item_#{line_item.id}", :class => "line_item_quantity", :size => 5 + - finalized_quantity = @order.completed? ? line_item.quantity : 0 + = item_form.number_field :quantity, :min => 0, "ofn-on-hand" => "#{variant.on_demand && 9999 || variant.on_hand}", "finalizedquantity" => finalized_quantity, "ng-model" => "line_item_#{line_item.id}", :class => "line_item_quantity", :size => 5 %td.cart-item-total.text-right{"data-hook" => "cart_item_total"} = line_item.display_amount_with_adjustments.to_html unless line_item.quantity.nil? diff --git a/spec/features/consumer/shopping/orders_spec.rb b/spec/features/consumer/shopping/orders_spec.rb index 50ad7d7563..b9caaa7e75 100644 --- a/spec/features/consumer/shopping/orders_spec.rb +++ b/spec/features/consumer/shopping/orders_spec.rb @@ -146,7 +146,10 @@ feature "Order Management", js: true do within "tr.variant-#{item1.variant.id}" do expect(page).to have_content item1.product.name expect(page).to have_field 'order_line_items_attributes_0_quantity' - fill_in 'order_line_items_attributes_0_quantity', with: 2 + # The original item quantity is 1, there are 4 more items available in stock + # By changing quantity to 5 we validate the case where the original stock in the order + # must be taken into account to fullfil the order (no insufficient stock error) + fill_in 'order_line_items_attributes_0_quantity', with: 5 end expect(page).to have_button I18n.t(:save_changes) @@ -158,15 +161,15 @@ feature "Order Management", js: true do click_button I18n.t(:save_changes) - expect(find(".order-total.grand-total")).to have_content "85.00" - expect(item1.reload.quantity).to eq 2 + expect(find(".order-total.grand-total")).to have_content "115.00" + expect(item1.reload.quantity).to eq 5 # Deleting an item within "tr.variant-#{item2.variant.id}" do click_link "delete_line_item_#{item2.id}" end - expect(find(".order-total.grand-total")).to have_content "75.00" + expect(find(".order-total.grand-total")).to have_content "105.00" expect(Spree::LineItem.find_by_id(item2.id)).to be nil # Cancelling the order