diff --git a/app/assets/javascripts/admin/bulk_order_management.js.coffee b/app/assets/javascripts/admin/bulk_order_management.js.coffee index 7e61aee56e..15f8eee81d 100644 --- a/app/assets/javascripts/admin/bulk_order_management.js.coffee +++ b/app/assets/javascripts/admin/bulk_order_management.js.coffee @@ -24,8 +24,9 @@ orderManagementModule.controller "AdminOrderMgmtCtrl", [ $http.defaults.headers.common["X-Spree-Token"] = spree_api_key dataFetcher("/api/enterprises/managed?template=bulk_index&q[is_primary_producer_eq]=true").then (data) -> $scope.suppliers = data - # Need to have suppliers before we get products so we can match suppliers to product.supplier - $scope.fetchOrders() + dataFetcher("/api/enterprises/managed?template=bulk_index&q[is_distributor_eq]=true").then (data) -> + $scope.distributors = data + $scope.fetchOrders() else if authorise_api_reponse.hasOwnProperty("error") $scope.api_error_msg = authorise_api_reponse("error") else @@ -38,6 +39,7 @@ orderManagementModule.controller "AdminOrderMgmtCtrl", [ $scope.resetOrders = (data) -> $scope.orders = data $scope.resetLineItems() + $scope.matchDistributor order for order in $scope.orders $scope.resetLineItems = -> $scope.lineItems = $scope.orders.reduce (lineItems,order) -> @@ -48,15 +50,21 @@ orderManagementModule.controller "AdminOrderMgmtCtrl", [ , [] $scope.matchSupplier = (line_item) -> - for i of $scope.suppliers - supplier = $scope.suppliers[i] + for i, supplier of $scope.suppliers if angular.equals(supplier, line_item.supplier) line_item.supplier = supplier break + + $scope.matchDistributor = (order) -> + for i, distributor of $scope.distributors + if angular.equals(distributor, order.distributor) + order.distributor = distributor + break ] orderManagementModule.filter "selectFilter", -> - return (lineItems,selectedSupplier) -> + return (lineItems,selectedSupplier,selectedDistributor) -> filtered = [] - filtered.push line_item for line_item in lineItems when selectedSupplier == undefined || line_item.supplier == selectedSupplier + filtered.push line_item for line_item in lineItems when (selectedSupplier == undefined || line_item.supplier == selectedSupplier) && + (selectedDistributor == undefined || line_item.order.distributor == selectedDistributor) filtered \ No newline at end of file diff --git a/app/views/spree/admin/orders/bulk_management.html.haml b/app/views/spree/admin/orders/bulk_management.html.haml index 678c5c7a2e..ea40e2951a 100644 --- a/app/views/spree/admin/orders/bulk_management.html.haml +++ b/app/views/spree/admin/orders/bulk_management.html.haml @@ -6,8 +6,12 @@ %div{ 'ng-app' => 'ofn.bulk_order_management', 'ng-controller' => 'AdminOrderMgmtCtrl', 'ng-init' => "initialise('#{@spree_api_key}');" } %div{ 'ng-show' => '!spree_api_key_ok' } {{ api_error_msg }} - %div.selectors{ :class => "four columns alpha" } - %select.select2{ :class => "three columns alpha", :name => 'supplier_filter', 'ng-model' => 'supplierFilter', 'ng-options' => 'supplier.name for supplier in suppliers'} + .filter_selects{ :class => "four columns alpha" } + .supplier_filter{ :class => "four columns alpha" } + %select.select2{ :class => "three columns alpha", :name => 'supplier_filter', 'ng-model' => 'supplierFilter', 'ng-options' => 's.name for s in suppliers'} + .distributor_filter{ :class => "four columns alpha" } + %select.select2{ :class => "three columns alpha", :name => 'distributor_filter', 'ng-model' => 'distributorFilter', 'ng-options' => 'd.name for d in distributors'} + %table.index#listing_orders.bulk %thead @@ -19,7 +23,7 @@ %th.variant Product (Unit): Var %th.quantity Quantity %th.max Max - %tr.line_item{ 'ng-repeat' => 'line_item in lineItems | selectFilter:supplierFilter', 'ng-class-even' => "'even'", 'ng-class-odd' => "'odd'" } + %tr.line_item{ 'ng-repeat' => 'line_item in lineItems | selectFilter:supplierFilter:distributorFilter', 'ng-class-even' => "'even'", 'ng-class-odd' => "'odd'" } %td.id {{ line_item.id }} %td.email {{ line_item.order.email }} %td.date {{ line_item.order.completed_at }} diff --git a/spec/features/admin/bulk_order_management_spec.rb b/spec/features/admin/bulk_order_management_spec.rb index 1e9cadf3d9..755e908f4a 100644 --- a/spec/features/admin/bulk_order_management_spec.rb +++ b/spec/features/admin/bulk_order_management_spec.rb @@ -90,8 +90,8 @@ feature %q{ let!(:d2) { FactoryGirl.create(:distributor_enterprise) } let!(:p1) { FactoryGirl.create(:product, supplier: s1 ) } let!(:p2) { FactoryGirl.create(:product, supplier: s2 ) } - let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) } - let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) } + let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: d1 ) } + let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: d2 ) } let!(:li1) { FactoryGirl.create(:line_item, order: o1, product: p1 ) } let!(:li2) { FactoryGirl.create(:line_item, order: o2, product: p2 ) } @@ -107,6 +107,15 @@ feature %q{ page.should have_selector "td.id", text: li1.id.to_s, visible: true page.should_not have_selector "td.id", text: li2.id.to_s, visible: true end + + it "displays a select box for distributors, which filters line items by the selected distributor" do + page.should have_select "distributor_filter", with_options: [d1.name,d2.name] + page.should have_selector "td.id", text: li1.id.to_s, visible: true + page.should have_selector "td.id", text: li2.id.to_s, visible: true + select d1.name, from: "distributor_filter" + page.should have_selector "td.id", text: li1.id.to_s, visible: true + page.should_not have_selector "td.id", text: li2.id.to_s, visible: true + end end end end diff --git a/spec/javascripts/unit/bulk_order_management_spec.js.coffee b/spec/javascripts/unit/bulk_order_management_spec.js.coffee index 27cc3d870a..0347902a1d 100644 --- a/spec/javascripts/unit/bulk_order_management_spec.js.coffee +++ b/spec/javascripts/unit/bulk_order_management_spec.js.coffee @@ -12,13 +12,15 @@ describe "AdminOrderMgmtCtrl", -> ) describe "loading data upon initialisation", -> - it "gets a list of suppliers and then calls fetchOrders", -> + it "gets a list of suppliers and a list of distributors and then calls fetchOrders", -> httpBackend.expectGET("/api/users/authorise_api?token=api_key").respond success: "Use of API Authorised" httpBackend.expectGET("/api/enterprises/managed?template=bulk_index&q[is_primary_producer_eq]=true").respond "list of suppliers" + httpBackend.expectGET("/api/enterprises/managed?template=bulk_index&q[is_distributor_eq]=true").respond "list of distributors" spyOn(scope, "fetchOrders").andReturn "nothing" scope.initialise "api_key" httpBackend.flush() expect(scope.suppliers).toEqual "list of suppliers" + expect(scope.distributors).toEqual "list of distributors" expect(scope.fetchOrders.calls.length).toEqual 1 expect(scope.spree_api_key_ok).toEqual true @@ -37,15 +39,19 @@ describe "AdminOrderMgmtCtrl", -> describe "resetting orders", -> beforeEach -> + spyOn(scope, "matchDistributor").andReturn "nothing" spyOn(scope, "resetLineItems").andReturn "nothing" - scope.resetOrders "list of orders" + scope.resetOrders [ "order1", "order2", "order3" ] it "sets the value of $scope.orders to the data received", -> - expect(scope.orders).toEqual "list of orders" + expect(scope.orders).toEqual [ "order1", "order2", "order3" ] it "makes a call to $scope.resetLineItems", -> expect(scope.resetLineItems).toHaveBeenCalled() + it "calls matchDistributor for each line item", -> + expect(scope.matchDistributor.calls.length).toEqual 3 + describe "resetting line items", -> order1 = order2 = order3 = null @@ -73,26 +79,52 @@ describe "AdminOrderMgmtCtrl", -> describe "matching supplier", -> it "changes the supplier of the line_item to the one which matches it from the suppliers list", -> - s1_s = + supplier1_list = id: 1 name: "S1" - s2_s = + supplier2_list = id: 2 name: "S2" - s1_l = + supplier1_line_item = id: 1 name: "S1" - expect(s1_s is s1_l).not.toEqual true + expect(supplier1_list is supplier1_line_item).not.toEqual true scope.suppliers = [ - s1_s - s2_s + supplier1_list + supplier2_list ] line_item = id: 10 - supplier: s1_l + supplier: supplier1_line_item scope.matchSupplier line_item - expect(line_item.supplier is s1_s).toEqual true \ No newline at end of file + expect(line_item.supplier is supplier1_list).toEqual true + + describe "matching distributor", -> + it "changes the distributor of the order to the one which matches it from the distributors list", -> + distributor1_list = + id: 1 + name: "D1" + + distributor2_list = + id: 2 + name: "D2" + + distributor1_order = + id: 1 + name: "D1" + + expect(distributor1_list is distributor1_order).not.toEqual true + scope.distributors = [ + distributor1_list + distributor2_list + ] + order = + id: 10 + distributor: distributor1_order + + scope.matchDistributor order + expect(order.distributor is distributor1_list).toEqual true \ No newline at end of file