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
This commit is contained in:
luisramos0
2019-06-07 12:06:05 +01:00
parent 7bd32d4967
commit 567196fe0e
3 changed files with 14 additions and 8 deletions

View File

@@ -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()

View File

@@ -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?

View File

@@ -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