From 594bec1b375f4b4230507387bbef1d959aee6c49 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 20 Oct 2016 08:12:27 +1100 Subject: [PATCH] StandingLineItems can be removed when creating a StandingOrder --- .../standing_order_controller.js.coffee | 5 +++ .../services/standing_order.js.coffee | 4 ++ .../admin/standing_orders/_products.html.haml | 2 +- spec/features/admin/standing_orders_spec.rb | 38 +++++++++++++------ 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/admin/standing_orders/controllers/standing_order_controller.js.coffee b/app/assets/javascripts/admin/standing_orders/controllers/standing_order_controller.js.coffee index 29bee6a6e0..203a8bf59d 100644 --- a/app/assets/javascripts/admin/standing_orders/controllers/standing_order_controller.js.coffee +++ b/app/assets/javascripts/admin/standing_orders/controllers/standing_order_controller.js.coffee @@ -21,8 +21,13 @@ angular.module("admin.standingOrders").controller "StandingOrderController", ($s $scope.stepTitleFor = (step) -> t("admin.standing_orders.steps.#{step}") $scope.addStandingLineItem = -> + $scope.standing_order_form.$setDirty() StandingOrder.buildItem($scope.newItem) + $scope.removeStandingLineItem = (item) -> + $scope.standing_order_form.$setDirty() + StandingOrder.removeItem(item) + $scope.estimatedSubtotal = -> $scope.standingOrder.standing_line_items.reduce (subtotal, item) -> subtotal += item.price_estimate * item.quantity diff --git a/app/assets/javascripts/admin/standing_orders/services/standing_order.js.coffee b/app/assets/javascripts/admin/standing_orders/services/standing_order.js.coffee index 381f73cc2d..7958abaaee 100644 --- a/app/assets/javascripts/admin/standing_orders/services/standing_order.js.coffee +++ b/app/assets/javascripts/admin/standing_orders/services/standing_order.js.coffee @@ -16,6 +16,10 @@ angular.module("admin.standingOrders").factory "StandingOrder", ($injector, $htt , (response) => InfoDialog.open 'error', response.data.errors[0] + removeItem: (item) -> + index = @standingOrder.standing_line_items.indexOf(item) + @standingOrder.standing_line_items.splice(index,1) + create: -> StatusMessage.display 'progress', 'Saving...' delete @errors[k] for k, v of @errors diff --git a/app/views/admin/standing_orders/_products.html.haml b/app/views/admin/standing_orders/_products.html.haml index 7be89b2acd..6e73523e81 100644 --- a/app/views/admin/standing_orders/_products.html.haml +++ b/app/views/admin/standing_orders/_products.html.haml @@ -21,7 +21,7 @@ %input.qty{ name: 'quantity', type: 'number', min: 0, ng: { model: 'item.quantity' } } %td.total.align-center {{ (item.price_estimate * item.quantity) | currency }} %td.actions - %a.delete-resource.icon_link.with-tip.icon-trash.no-text{ data: { confirm: "Are you sure?" }, :href => "javascript:void(0)" } + %a.delete-item.icon-trash.no-text{ ng: { click: 'removeStandingLineItem(item)'}, data: { confirm: "Are you sure?" }, :href => "javascript:void(0)" } %tbody#subtotal.no-border-top{"data-hook" => "admin_order_form_subtotal"} %tr#subtotal-row %td{:colspan => "3"} diff --git a/spec/features/admin/standing_orders_spec.rb b/spec/features/admin/standing_orders_spec.rb index 1149210809..50db23124f 100644 --- a/spec/features/admin/standing_orders_spec.rb +++ b/spec/features/admin/standing_orders_spec.rb @@ -62,11 +62,13 @@ feature 'Standing Orders' do context 'creating a new standing order' do let!(:customer) { create(:customer, enterprise: shop) } - let!(:product) { create(:product, supplier: shop) } - let!(:variant) { create(:variant, product: product, unit_value: '100', price: 12.00, option_values: []) } + let!(:product1) { create(:product, supplier: shop) } + let!(:product2) { create(:product, supplier: shop) } + let!(:variant1) { create(:variant, product: product1, unit_value: '100', price: 12.00, option_values: []) } + let!(:variant2) { create(:variant, product: product2, unit_value: '1000', price: 6.00, option_values: []) } let!(:enterprise_fee) { create(:enterprise_fee, amount: 1.75) } let!(:order_cycle) { create(:simple_order_cycle, coordinator: shop, orders_open_at: 2.days.from_now, orders_close_at: 7.days.from_now) } - let!(:outgoing_exchange) { order_cycle.exchanges.create(sender: shop, receiver: shop, variants: [variant], enterprise_fees: [enterprise_fee]) } + let!(:outgoing_exchange) { order_cycle.exchanges.create(sender: shop, receiver: shop, variants: [variant1, variant2], enterprise_fees: [enterprise_fee]) } let!(:schedule) { create(:schedule, order_cycles: [order_cycle]) } let!(:payment_method) { create(:payment_method, distributors: [shop]) } let!(:shipping_method) { create(:shipping_method, distributors: [shop]) } @@ -108,11 +110,11 @@ feature 'Standing Orders' do expect(page).to have_content 'Please add at least one product' # Adding a product and getting a price estimate - targetted_select2_search product.name, from: '#add_variant_id', dropdown_css: '.select2-drop' + targetted_select2_search product1.name, from: '#add_variant_id', dropdown_css: '.select2-drop' fill_in 'add_quantity', with: 2 click_link 'Add' within 'table#standing-line-items tr.item', match: :first do - expect(page).to have_selector 'td.description', text: "#{product.name} - #{variant.full_name}" + expect(page).to have_selector 'td.description', text: "#{product1.name} - #{variant1.full_name}" expect(page).to have_selector 'td.price', text: "$13.75" expect(page).to have_input 'quantity', with: "2" expect(page).to have_selector 'td.total', text: "$27.50" @@ -120,6 +122,20 @@ feature 'Standing Orders' do click_button('Next') + # Deleting the existing product and adding a new product + within 'table#standing-line-items tr.item', match: :first do + accept_alert "Are you sure?" do find("a.delete-item").click end + end + targetted_select2_search product2.name, from: '#add_variant_id', dropdown_css: '.select2-drop' + fill_in 'add_quantity', with: 3 + click_link 'Add' + within 'table#standing-line-items tr.item', match: :first do + expect(page).to have_selector 'td.description', text: "#{product2.name} - #{variant2.full_name}" + expect(page).to have_selector 'td.price', text: "$7.75" + expect(page).to have_input 'quantity', with: "3" + expect(page).to have_selector 'td.total', text: "$23.25" + end + expect{ click_button('Create Standing Order') expect(page).to have_content 'Saved' @@ -127,10 +143,10 @@ feature 'Standing Orders' do # Prices are shown within 'table#standing-line-items tr.item', match: :first do - expect(page).to have_selector 'td.description', text: "#{product.name} - #{variant.full_name}" - expect(page).to have_selector 'td.price', text: "$13.75" - expect(page).to have_input 'quantity', with: "2" - expect(page).to have_selector 'td.total', text: "$27.50" + expect(page).to have_selector 'td.description', text: "#{product2.name} - #{variant2.full_name}" + expect(page).to have_selector 'td.price', text: "$7.75" + expect(page).to have_input 'quantity', with: "3" + expect(page).to have_selector 'td.total', text: "$23.25" end # Basic properties of standing order are set @@ -145,8 +161,8 @@ feature 'Standing Orders' do # Standing Line Items are created expect(standing_order.standing_line_items.count).to eq 1 standing_line_item = standing_order.standing_line_items.first - expect(standing_line_item.variant).to eq variant - expect(standing_line_item.quantity).to eq 2 + expect(standing_line_item.variant).to eq variant2 + expect(standing_line_item.quantity).to eq 3 end context 'editing an existing standing order' do