From e2b8108972591d92043efafd9d4433f0df7b793e Mon Sep 17 00:00:00 2001 From: Cillian O'Ruanaidh Date: Fri, 29 Oct 2021 17:43:27 +0100 Subject: [PATCH] If an order has out of stock line items display them and let admins remove them --- app/views/spree/admin/orders/_form.html.haml | 3 + .../_insufficient_stock_lines.html.haml | 39 ++++++++++ app/webpacker/css/admin/sections/orders.scss | 10 +++ spec/system/admin/order_spec.rb | 21 +++++ .../spree/admin/orders/edit.html.haml_spec.rb | 77 ++++++++++++++++--- 5 files changed, 139 insertions(+), 11 deletions(-) create mode 100644 app/views/spree/admin/orders/_insufficient_stock_lines.html.haml diff --git a/app/views/spree/admin/orders/_form.html.haml b/app/views/spree/admin/orders/_form.html.haml index 72dbf3d81bb..f902fe361c1 100644 --- a/app/views/spree/admin/orders/_form.html.haml +++ b/app/views/spree/admin/orders/_form.html.haml @@ -2,6 +2,9 @@ - if @line_item.try(:errors).present? = render :partial => 'spree/shared/error_messages', :locals => { :target => @line_item } + - if !@order.completed? && @order.insufficient_stock_lines.any? + = render 'spree/admin/orders/insufficient_stock_lines', insufficient_stock_lines: @order.insufficient_stock_lines + = render :partial => "spree/admin/orders/shipment", :collection => @order.shipments, :locals => { :order => order } = render :partial => "spree/admin/orders/_form/adjustments", :locals => { :adjustments => @order.line_item_adjustments, :title => t(".line_item_adjustments")} diff --git a/app/views/spree/admin/orders/_insufficient_stock_lines.html.haml b/app/views/spree/admin/orders/_insufficient_stock_lines.html.haml new file mode 100644 index 00000000000..70a4ac14ce2 --- /dev/null +++ b/app/views/spree/admin/orders/_insufficient_stock_lines.html.haml @@ -0,0 +1,39 @@ +%div.insufficient-stock-items + %fieldset.no-border-bottom + %legend + = t("spree.orders.line_item.out_of_stock") + + %table + %colgroup + %col{ :style => "width: 10%;" } + %col{ :style => "width: 30%;" } + %col{ :style => "width: 15%;" } + %col{ :style => "width: 15%;" } + %col{ :style => "width: 15%;" } + %col{ :style => "width: 15%;" } + + %thead + %th{ :colspan => "2" } + = Spree.t(:item_description) + %th + = Spree.t(:price) + %th + = Spree.t(:quantity) + %th + = Spree.t(:total) + %th.orders-actions.actions + + - insufficient_stock_lines.each do |line_item| + %tr.insufficient-stock-item + %td + = render 'spree/shared/variant_thumbnail', variant: line_item.variant + %td + = line_item.variant.product_and_full_name + %td.align-center + = line_item.single_money.to_html + %td.align-center + = line_item.quantity + %td.align-center + = line_item.money.to_html + %td.actions + = link_to_delete line_item, { url: main_app.admin_bulk_line_item_path(line_item), no_text: true } diff --git a/app/webpacker/css/admin/sections/orders.scss b/app/webpacker/css/admin/sections/orders.scss index e8f4d796922..06cc6be17ad 100644 --- a/app/webpacker/css/admin/sections/orders.scss +++ b/app/webpacker/css/admin/sections/orders.scss @@ -33,6 +33,16 @@ } } +.insufficient-stock-items { + legend { + color: $color-error; + } + + table tr:last-child th { + border-bottom: 1px solid $admin-table-border; + } +} + // Customize orduct add fieldset #add-line-item { fieldset { diff --git a/spec/system/admin/order_spec.rb b/spec/system/admin/order_spec.rb index 52bbc1fcafc..0e3aca106b6 100644 --- a/spec/system/admin/order_spec.rb +++ b/spec/system/admin/order_spec.rb @@ -448,6 +448,27 @@ def new_order_with_distribution(distributor, order_cycle) end end end + + context "when an incomplete order has some line items with insufficient stock" do + let(:incomplete_order) do + create(:order_with_line_items, user: user, distributor: distributor, + order_cycle: order_cycle) + end + + it "displays the out of stock line items and they can be deleted from the order" do + incomplete_order.line_items.first.variant.update!(on_demand: false, on_hand: 0) + + visit spree.edit_admin_order_path(incomplete_order) + + within ".insufficient-stock-items" do + expect(page).to have_content incomplete_order.products.first.name + accept_alert 'Are you sure?' do + find("a.delete-resource").click + end + expect(page).to_not have_content incomplete_order.products.first.name + end + end + end end it "creating an order with distributor and order cycle" do diff --git a/spec/views/spree/admin/orders/edit.html.haml_spec.rb b/spec/views/spree/admin/orders/edit.html.haml_spec.rb index d3817f82374..96c4f404afb 100644 --- a/spec/views/spree/admin/orders/edit.html.haml_spec.rb +++ b/spec/views/spree/admin/orders/edit.html.haml_spec.rb @@ -22,21 +22,76 @@ def current_ability end allow(view).to receive_messages spree_current_user: create(:user) + end + + context "when order is complete" do + let(:order) { create(:completed_order_with_fees) } + + before do + order.distributor = create(:distributor_enterprise) + assign(:order, order) + assign(:shops, [order.distributor]) + assign(:order_cycles, []) + end + + describe "order values" do + it "displays order shipping costs, transaction fee and order total" do + render + + expect(rendered).to have_content("Shipping Method\nUPS Ground $6.00") + expect(rendered).to have_content("Transaction fee:\n\n$10.00") + expect(rendered).to have_content("Order Total\n$36.00") + end + end + + context "when some line items are out of stock" do + let!(:out_of_stock_line_item) do + line_item = order.line_items.first + line_item.variant.update!(on_demand: false, on_hand: 0) + line_item + end + + it "doesn't display a table of out of stock line items" do + render - order = create(:completed_order_with_fees) - order.distributor = create(:distributor_enterprise) - assign(:order, order) - assign(:shops, [order.distributor]) - assign(:order_cycles, []) + expect(rendered).to_not have_content "Out of Stock" + expect(rendered).to_not have_selector ".insufficient-stock-items", + text: out_of_stock_line_item.variant.display_name + end + end end - describe "order values" do - it "displays order shipping costs, transaction fee and order total" do - render + context "when order is incomplete" do + let(:order) { create(:order_with_line_items) } + + before do + assign(:order, order) + assign(:shops, [order.distributor]) + assign(:order_cycles, []) + end + + context "when some line items are out of stock" do + let!(:out_of_stock_line_item) do + line_item = order.line_items.first + line_item.variant.update!(on_demand: false, on_hand: 0) + line_item + end + + it "displays a table of out of stock line items" do + render - expect(rendered).to have_content("Shipping Method\nUPS Ground $6.00") - expect(rendered).to have_content("Transaction fee:\n\n$10.00") - expect(rendered).to have_content("Order Total\n$36.00") + expect(rendered).to have_content "Out of Stock" + expect(rendered).to have_selector ".insufficient-stock-items", + text: out_of_stock_line_item.variant.display_name + end + end + + context "when all line items are in stock" do + it "doesn't display a table of out of stock line items" do + render + + expect(rendered).to_not have_content "Out of Stock" + end end end end