From d100d12ebefe4b65c4fd330b1064b6b6ba9a27e6 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 20 Sep 2013 11:11:59 +1000 Subject: [PATCH] When a variant is removed from supply to an order cycle, remove it from distribution also --- .../admin/order_cycle.js.erb.coffee | 18 +++++++++++++++ ..._exchange_supplied_products_form.html.haml | 4 ++-- .../unit/order_cycle_spec.js.coffee | 22 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/admin/order_cycle.js.erb.coffee b/app/assets/javascripts/admin/order_cycle.js.erb.coffee index a1ec15b6ef..2135afceeb 100644 --- a/app/assets/javascripts/admin/order_cycle.js.erb.coffee +++ b/app/assets/javascripts/admin/order_cycle.js.erb.coffee @@ -58,6 +58,9 @@ angular.module('order_cycle', ['ngResource']) $event.preventDefault() OrderCycle.removeExchangeFee(exchange, index) + $scope.removeDistributionOfVariant = (variant_id) -> + OrderCycle.removeDistributionOfVariant(variant_id) + $scope.submit = -> OrderCycle.create() ]) @@ -122,6 +125,9 @@ angular.module('order_cycle', ['ngResource']) $event.preventDefault() OrderCycle.removeExchangeFee(exchange, index) + $scope.removeDistributionOfVariant = (variant_id) -> + OrderCycle.removeDistributionOfVariant(variant_id) + $scope.submit = -> OrderCycle.update() ]) @@ -198,6 +204,10 @@ angular.module('order_cycle', ['ngResource']) distributors = (exchange.enterprise_id for exchange in this.order_cycle.outgoing_exchanges) jQuery.unique(suppliers.concat(distributors)).sort() + removeDistributionOfVariant: (variant_id) -> + for exchange in this.order_cycle.outgoing_exchanges + exchange.variants[variant_id] = false + load: (order_cycle_id) -> service = this @@ -332,4 +342,12 @@ angular.module('order_cycle', ['ngResource']) (scope, element, attrs) -> element.bind 'change', -> scope.$apply(attrs.ofwOnChange) + ) + + .directive('ofwSyncDistributions', -> + (scope, element, attrs) -> + element.bind 'change', -> + if !$(this).is(':checked') + scope.$apply -> + scope.removeDistributionOfVariant(attrs.ofwSyncDistributions) ) \ No newline at end of file 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 d4c113889d..fa4a1c555b 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 @@ -2,9 +2,9 @@ %td{:colspan => 3} .exchange-product{'ng-repeat' => 'product in enterprises[exchange.enterprise_id].supplied_products'} .exchange-product-details - = check_box_tag 'order_cycle_incoming_exchange_{{ $parent.$index }}_variants_{{ product.master_id }}', 1, 1, 'ng-hide' => 'product.variants', 'ng-model' => 'exchange.variants[product.master_id]', 'id' => 'order_cycle_incoming_exchange_{{ $parent.$index }}_variants_{{ product.master_id }}' + = check_box_tag 'order_cycle_incoming_exchange_{{ $parent.$index }}_variants_{{ product.master_id }}', 1, 1, 'ng-hide' => 'product.variants', 'ng-model' => 'exchange.variants[product.master_id]', 'ofw-sync-distributions' => '{{ product.master_id }}', 'id' => 'order_cycle_incoming_exchange_{{ $parent.$index }}_variants_{{ product.master_id }}' %img{'ng-src' => '{{ product.image_url }}'} {{ product.name }} .exchange-product-variant{'ng-repeat' => 'variant in product.variants'} - = check_box_tag 'order_cycle_incoming_exchange_{{ $parent.$parent.$index }}_variants_{{ variant.id }}', 1, 1, 'ng-model' => 'exchange.variants[variant.id]', 'id' => 'order_cycle_incoming_exchange_{{ $parent.$parent.$index }}_variants_{{ variant.id }}' + = check_box_tag 'order_cycle_incoming_exchange_{{ $parent.$parent.$index }}_variants_{{ variant.id }}', 1, 1, 'ng-model' => 'exchange.variants[variant.id]', 'ofw-sync-distributions' => '{{ variant.id }}', 'id' => 'order_cycle_incoming_exchange_{{ $parent.$parent.$index }}_variants_{{ variant.id }}' {{ variant.label }} diff --git a/spec/javascripts/unit/order_cycle_spec.js.coffee b/spec/javascripts/unit/order_cycle_spec.js.coffee index e466396dde..f12d3f972e 100644 --- a/spec/javascripts/unit/order_cycle_spec.js.coffee +++ b/spec/javascripts/unit/order_cycle_spec.js.coffee @@ -25,6 +25,7 @@ describe 'OrderCycle controllers', -> removeCoordinatorFee: jasmine.createSpy('removeCoordinatorFee') addExchangeFee: jasmine.createSpy('addExchangeFee') removeExchangeFee: jasmine.createSpy('removeExchangeFee') + removeDistributionOfVariant: jasmine.createSpy('removeDistributionOfVariant') create: jasmine.createSpy('create') Enterprise = index: jasmine.createSpy('index').andReturn('enterprises list') @@ -121,6 +122,10 @@ describe 'OrderCycle controllers', -> expect(event.preventDefault).toHaveBeenCalled() expect(OrderCycle.removeExchangeFee).toHaveBeenCalledWith('exchange', 0) + it 'Removes distribution of a variant', -> + scope.removeDistributionOfVariant('variant') + expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('variant') + it 'Submits the order cycle via OrderCycle create', -> scope.submit() expect(OrderCycle.create).toHaveBeenCalled() @@ -154,6 +159,7 @@ describe 'OrderCycle controllers', -> removeCoordinatorFee: jasmine.createSpy('removeCoordinatorFee') addExchangeFee: jasmine.createSpy('addExchangeFee') removeExchangeFee: jasmine.createSpy('removeExchangeFee') + removeDistributionOfVariant: jasmine.createSpy('removeDistributionOfVariant') update: jasmine.createSpy('update') Enterprise = index: jasmine.createSpy('index').andReturn('enterprises list') @@ -249,6 +255,10 @@ describe 'OrderCycle controllers', -> expect(event.preventDefault).toHaveBeenCalled() expect(OrderCycle.removeExchangeFee).toHaveBeenCalledWith('exchange', 0) + it 'Removes distribution of a variant', -> + scope.removeDistributionOfVariant('variant') + expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('variant') + it 'Submits the order cycle via OrderCycle update', -> scope.submit() expect(OrderCycle.update).toHaveBeenCalled() @@ -514,6 +524,18 @@ describe 'OrderCycle services', -> expect(OrderCycle.variantSuppliedToOrderCycle({id: 999})).toBeFalsy() + describe 'remove all distribution of a variant', -> + it 'removes the variant from every outgoing exchange', -> + OrderCycle.order_cycle.outgoing_exchanges = [ + {variants: {123: true, 234: true}} + {variants: {123: true, 333: true}} + ] + OrderCycle.removeDistributionOfVariant('123') + expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual [ + {variants: {123: false, 234: true}} + {variants: {123: false, 333: true}} + ] + describe 'loading an order cycle', -> beforeEach -> OrderCycle.load('123')