Skip to content

Commit

Permalink
If an order has out of stock line items display them and let admins r…
Browse files Browse the repository at this point in the history
…emove them
  • Loading branch information
cillian committed Dec 3, 2021
1 parent 9590b15 commit ca00f4e
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 11 deletions.
11 changes: 11 additions & 0 deletions app/assets/stylesheets/admin/sections/orders.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import 'admin/globals/variables';
@import "../../darkswarm/branding";

// Customize orders filter
[data-hook="admin_orders_index_search"] {
Expand Down Expand Up @@ -35,6 +36,16 @@
}
}

.insufficient-stock-items {
legend {
color: $color-error;
}

table tr:last-child th {
border-bottom: 1px solid $clr-turquoise-light;
}
}

// Customize orduct add fieldset
#add-line-item {
fieldset {
Expand Down
3 changes: 3 additions & 0 deletions app/views/spree/admin/orders/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
- if @line_item.try(:errors).present?
= render :partial => 'spree/shared/error_messages', :locals => { :target => @line_item }
- if [email protected]? && @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")}
Expand Down
39 changes: 39 additions & 0 deletions app/views/spree/admin/orders/_insufficient_stock_lines.html.haml
Original file line number Diff line number Diff line change
@@ -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 }
21 changes: 21 additions & 0 deletions spec/system/admin/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,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
Expand Down
77 changes: 66 additions & 11 deletions spec/views/spree/admin/orders/edit.html.haml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ca00f4e

Please sign in to comment.