diff --git a/app/assets/javascripts/admin/order_cycle.js.erb.coffee b/app/assets/javascripts/admin/order_cycle.js.erb.coffee index 9589b790b9..807f0184b7 100644 --- a/app/assets/javascripts/admin/order_cycle.js.erb.coffee +++ b/app/assets/javascripts/admin/order_cycle.js.erb.coffee @@ -9,9 +9,15 @@ angular.module('order_cycle', ['ngResource']) $scope.loaded = -> Enterprise.loaded && EnterpriseFee.loaded + $scope.suppliedVariants = (enterprise_id) -> + Enterprise.suppliedVariants(enterprise_id) + $scope.exchangeSelectedVariants = (exchange) -> OrderCycle.exchangeSelectedVariants(exchange) + $scope.setExchangeVariants = (exchange, variants, selected) -> + OrderCycle.setExchangeVariants(exchange, variants, selected) + $scope.enterpriseTotalVariants = (enterprise) -> Enterprise.totalVariants(enterprise) @@ -83,9 +89,15 @@ angular.module('order_cycle', ['ngResource']) $scope.loaded = -> Enterprise.loaded && EnterpriseFee.loaded && OrderCycle.loaded + $scope.suppliedVariants = (enterprise_id) -> + Enterprise.suppliedVariants(enterprise_id) + $scope.exchangeSelectedVariants = (exchange) -> OrderCycle.exchangeSelectedVariants(exchange) + $scope.setExchangeVariants = (exchange, variants, selected) -> + OrderCycle.setExchangeVariants(exchange, variants, selected) + $scope.enterpriseTotalVariants = (enterprise) -> Enterprise.totalVariants(enterprise) @@ -175,6 +187,9 @@ angular.module('order_cycle', ['ngResource']) toggleProducts: (exchange) -> exchange.showProducts = !exchange.showProducts + setExchangeVariants: (exchange, variants, selected) -> + exchange.variants[variant] = selected for variant in variants + addSupplier: (new_supplier_id) -> this.order_cycle.incoming_exchanges.push({enterprise_id: new_supplier_id, incoming: true, active: true, variants: {}, enterprise_fees: []}) @@ -322,6 +337,16 @@ angular.module('order_cycle', ['ngResource']) this.enterprises + suppliedVariants: (enterprise_id) -> + vs = (this.variantsOf(product) for product in this.enterprises[enterprise_id].supplied_products) + [].concat vs... + + variantsOf: (product) -> + if product.variants.length > 0 + variant.id for variant in product.variants + else + [product.master_id] + totalVariants: (enterprise) -> numVariants = 0 diff --git a/app/assets/stylesheets/admin/openfoodnetwork.css.scss b/app/assets/stylesheets/admin/openfoodnetwork.css.scss index 14feebae0d..7991945ba7 100644 --- a/app/assets/stylesheets/admin/openfoodnetwork.css.scss +++ b/app/assets/stylesheets/admin/openfoodnetwork.css.scss @@ -52,6 +52,11 @@ form.order_cycle { border-bottom: 2px solid #C3D9FF; } + .exchange-select-all-variants { + clear: both; + margin: 5px; + } + .exchange-product { float: left; overflow: auto; diff --git a/app/views/admin/order_cycles/_exchange_supplied_products_form.html.haml b/app/views/admin/order_cycles/_exchange_supplied_products_form.html.haml index 7a20954eb8..6ad13a88a6 100644 --- a/app/views/admin/order_cycles/_exchange_supplied_products_form.html.haml +++ b/app/views/admin/order_cycles/_exchange_supplied_products_form.html.haml @@ -1,5 +1,10 @@ / TODO: Unify this with exchange_distributed_products_form %td{:colspan => 3} + .exchange-select-all-variants + %label + = check_box_tag 'order_cycle_incoming_exchange_{{ $parent.$index }}_select_all_variants', 1, 1, 'ng-model' => 'exchange.select_all_variants', 'ng-click' => 'setExchangeVariants(exchange, suppliedVariants(exchange.enterprise_id), exchange.select_all_variants)', 'id' => 'order_cycle_incoming_exchange_{{ $parent.$index }}_select_all_variants' + Select all + .exchange-product{'ng-repeat' => 'product in enterprises[exchange.enterprise_id].supplied_products'} .exchange-product-details diff --git a/spec/javascripts/unit/order_cycle_spec.js.coffee b/spec/javascripts/unit/order_cycle_spec.js.coffee index 0466a5b861..5441d5c0b0 100644 --- a/spec/javascripts/unit/order_cycle_spec.js.coffee +++ b/spec/javascripts/unit/order_cycle_spec.js.coffee @@ -19,6 +19,7 @@ describe 'OrderCycle controllers', -> variantSuppliedToOrderCycle: jasmine.createSpy('variantSuppliedToOrderCycle').andReturn('variant supplied') exchangeDirection: jasmine.createSpy('exchangeDirection').andReturn('exchange direction') toggleProducts: jasmine.createSpy('toggleProducts') + setExchangeVariants: jasmine.createSpy('setExchangeVariants') addSupplier: jasmine.createSpy('addSupplier') addDistributor: jasmine.createSpy('addDistributor') removeExchange: jasmine.createSpy('removeExchange') @@ -31,6 +32,7 @@ describe 'OrderCycle controllers', -> Enterprise = index: jasmine.createSpy('index').andReturn('enterprises list') supplied_products: 'supplied products' + suppliedVariants: jasmine.createSpy('suppliedVariants').andReturn('supplied variants') totalVariants: jasmine.createSpy('totalVariants').andReturn('variants total') EnterpriseFee = index: jasmine.createSpy('index').andReturn('enterprise fees list') @@ -63,10 +65,18 @@ describe 'OrderCycle controllers', -> EnterpriseFee.loaded = false expect(scope.loaded()).toBe(false) + it "delegates suppliedVariants to Enterprise", -> + expect(scope.suppliedVariants('enterprise_id')).toEqual('supplied variants') + expect(Enterprise.suppliedVariants).toHaveBeenCalledWith('enterprise_id') + it 'Delegates exchangeSelectedVariants to OrderCycle', -> expect(scope.exchangeSelectedVariants('exchange')).toEqual('variants selected') expect(OrderCycle.exchangeSelectedVariants).toHaveBeenCalledWith('exchange') + it "delegates setExchangeVariants to OrderCycle", -> + scope.setExchangeVariants('exchange', 'variants', 'selected') + expect(OrderCycle.setExchangeVariants).toHaveBeenCalledWith('exchange', 'variants', 'selected') + it 'Delegates enterpriseTotalVariants to Enterprise', -> expect(scope.enterpriseTotalVariants('enterprise')).toEqual('variants total') expect(Enterprise.totalVariants).toHaveBeenCalledWith('enterprise') @@ -169,6 +179,7 @@ describe 'OrderCycle controllers', -> variantSuppliedToOrderCycle: jasmine.createSpy('variantSuppliedToOrderCycle').andReturn('variant supplied') exchangeDirection: jasmine.createSpy('exchangeDirection').andReturn('exchange direction') toggleProducts: jasmine.createSpy('toggleProducts') + setExchangeVariants: jasmine.createSpy('setExchangeVariants') addSupplier: jasmine.createSpy('addSupplier') addDistributor: jasmine.createSpy('addDistributor') removeExchange: jasmine.createSpy('removeExchange') @@ -181,6 +192,7 @@ describe 'OrderCycle controllers', -> Enterprise = index: jasmine.createSpy('index').andReturn('enterprises list') supplied_products: 'supplied products' + suppliedVariants: jasmine.createSpy('suppliedVariants').andReturn('supplied variants') totalVariants: jasmine.createSpy('totalVariants').andReturn('variants total') EnterpriseFee = index: jasmine.createSpy('index').andReturn('enterprise fees list') @@ -213,10 +225,18 @@ describe 'OrderCycle controllers', -> OrderCycle.loaded = false expect(scope.loaded()).toBe(false) + it "delegates suppliedVariants to Enterprise", -> + expect(scope.suppliedVariants('enterprise_id')).toEqual('supplied variants') + expect(Enterprise.suppliedVariants).toHaveBeenCalledWith('enterprise_id') + it 'Delegates exchangeSelectedVariants to OrderCycle', -> expect(scope.exchangeSelectedVariants('exchange')).toEqual('variants selected') expect(OrderCycle.exchangeSelectedVariants).toHaveBeenCalledWith('exchange') + it "delegates setExchangeVariants to OrderCycle", -> + scope.setExchangeVariants('exchange', 'variants', 'selected') + expect(OrderCycle.setExchangeVariants).toHaveBeenCalledWith('exchange', 'variants', 'selected') + it 'Delegates totalVariants to Enterprise', -> expect(scope.enterpriseTotalVariants('enterprise')).toEqual('variants total') expect(Enterprise.totalVariants).toHaveBeenCalledWith('enterprise') @@ -332,6 +352,25 @@ describe 'OrderCycle services', -> $httpBackend.flush() expect(Enterprise.supplied_products).toEqual [1, 2, 3, 4, 5, 6] + it "finds supplied variants for an enterprise", -> + spyOn(Enterprise, 'variantsOf').andReturn(10) + Enterprise.index() + $httpBackend.flush() + expect(Enterprise.suppliedVariants(1)).toEqual [10, 10] + + describe "finding the variants of a product", -> + it "returns the master for products without variants", -> + p = + master_id: 1 + variants: [] + expect(Enterprise.variantsOf(p)).toEqual [1] + + it "returns the variant ids for products with variants", -> + p = + master_id: 1 + variants: [{id: 2}, {id: 3}] + expect(Enterprise.variantsOf(p)).toEqual [2, 3] + it 'counts total variants supplied by an enterprise', -> enterprise = supplied_products: [ @@ -450,6 +489,12 @@ describe 'OrderCycle services', -> OrderCycle.toggleProducts(exchange) expect(exchange.showProducts).toEqual(true) + describe "setting exchange variants", -> + it "sets all variants to the provided value", -> + exchange = {variants: {2: false}} + OrderCycle.setExchangeVariants(exchange, [1, 2, 3], true) + expect(exchange.variants).toEqual {1: true, 2: true, 3: true} + describe 'adding suppliers', -> exchange = null