Only show products for distribution that have been supplied to the order cycle

This commit is contained in:
Rohan Mitchell
2013-01-17 10:49:59 +11:00
parent 8f05c69696
commit 3991199c30
3 changed files with 83 additions and 1 deletions

View File

@@ -12,6 +12,9 @@ app.controller 'AdminCreateOrderCycleCtrl', ($scope, OrderCycle, Enterprise) ->
$scope.enterpriseTotalVariants = (enterprise) ->
Enterprise.totalVariants(enterprise)
$scope.productSuppliedToOrderCycle = (product) ->
OrderCycle.productSuppliedToOrderCycle(product)
$scope.toggleProducts = ($event, exchange) ->
$event.preventDefault()
OrderCycle.toggleProducts(exchange)
@@ -41,6 +44,9 @@ app.controller 'AdminEditOrderCycleCtrl', ($scope, $location, OrderCycle, Enterp
$scope.enterpriseTotalVariants = (enterprise) ->
Enterprise.totalVariants(enterprise)
$scope.productSuppliedToOrderCycle = (product) ->
OrderCycle.productSuppliedToOrderCycle(product)
$scope.toggleProducts = ($event, exchange) ->
$event.preventDefault()
OrderCycle.toggleProducts(exchange)
@@ -85,6 +91,27 @@ app.factory 'OrderCycle', ($resource, $window) ->
addDistributor: (new_distributor_id) ->
this.order_cycle.outgoing_exchanges.push({enterprise_id: new_distributor_id, active: true, variants: {}})
productSuppliedToOrderCycle: (product) ->
product_variant_ids = (variant.id for variant in product.variants)
variant_ids = [product.master_id].concat(product_variant_ids)
incomingExchangesVariants = this.incomingExchangesVariants()
# TODO: This would be much nicer functional (set intersection)
found = false
for variant_id in variant_ids
if incomingExchangesVariants.indexOf(variant_id) != -1
found = true
break
found
incomingExchangesVariants: ->
variant_ids = []
for exchange in this.order_cycle.incoming_exchanges
variant_ids.push(parseInt(id)) for id, active of exchange.variants when active
variant_ids
load: (order_cycle_id) ->
service = this

View File

@@ -1,5 +1,5 @@
%td{:colspan => 3}
.exchange-product{'ng-repeat' => 'product in supplied_products'}
.exchange-product{'ng-repeat' => 'product in supplied_products | filter:productSuppliedToOrderCycle'}
.exchange-product-details
= check_box_tag 'order_cycle_exchange_{{ $parent.$index }}_variants_{{ product.master_id }}', 1, 1, 'ng-hide' => 'product.variants', 'ng-model' => 'exchange.variants[product.master_id]', 'id' => 'order_cycle_exchange_{{ $parent.$index }}_variants_{{ product.master_id }}'
%img{'ng-src' => '{{ product.image_url }}'}

View File

@@ -14,6 +14,7 @@ describe 'OrderCycle controllers', ->
OrderCycle =
order_cycle: 'my order cycle'
exchangeSelectedVariants: jasmine.createSpy('exchangeSelectedVariants').andReturn('variants selected')
productSuppliedToOrderCycle: jasmine.createSpy('productSuppliedToOrderCycle').andReturn('product supplied')
toggleProducts: jasmine.createSpy('toggleProducts')
addSupplier: jasmine.createSpy('addSupplier')
addDistributor: jasmine.createSpy('addDistributor')
@@ -44,6 +45,10 @@ describe 'OrderCycle controllers', ->
expect(scope.enterpriseTotalVariants('enterprise')).toEqual('variants total')
expect(Enterprise.totalVariants).toHaveBeenCalledWith('enterprise')
it 'Delegates productSuppliedToOrderCycle to OrderCycle', ->
expect(scope.productSuppliedToOrderCycle('product')).toEqual('product supplied')
expect(OrderCycle.productSuppliedToOrderCycle).toHaveBeenCalledWith('product')
it 'Delegates toggleProducts to OrderCycle', ->
scope.toggleProducts(event, 'exchange')
expect(event.preventDefault).toHaveBeenCalled()
@@ -83,6 +88,7 @@ describe 'OrderCycle controllers', ->
OrderCycle =
load: jasmine.createSpy('load')
exchangeSelectedVariants: jasmine.createSpy('exchangeSelectedVariants').andReturn('variants selected')
productSuppliedToOrderCycle: jasmine.createSpy('productSuppliedToOrderCycle').andReturn('product supplied')
toggleProducts: jasmine.createSpy('toggleProducts')
addSupplier: jasmine.createSpy('addSupplier')
addDistributor: jasmine.createSpy('addDistributor')
@@ -112,6 +118,10 @@ describe 'OrderCycle controllers', ->
expect(scope.enterpriseTotalVariants('enterprise')).toEqual('variants total')
expect(Enterprise.totalVariants).toHaveBeenCalledWith('enterprise')
it 'Delegates productSuppliedToOrderCycle to OrderCycle', ->
expect(scope.productSuppliedToOrderCycle('product')).toEqual('product supplied')
expect(OrderCycle.productSuppliedToOrderCycle).toHaveBeenCalledWith('product')
it 'Delegates toggleProducts to OrderCycle', ->
scope.toggleProducts(event, 'exchange')
expect(event.preventDefault).toHaveBeenCalled()
@@ -245,6 +255,51 @@ describe 'OrderCycle services', ->
{enterprise_id: '123', active: true, variants: {}}
]
describe 'fetching all variants supplied on incoming exchanges', ->
it 'collects variants from incoming exchanges', ->
OrderCycle.order_cycle.incoming_exchanges = [
{variants: {1: true, 2: false}}
{variants: {3: false, 4: true}}
{variants: {5: true, 6: false}}
]
expect(OrderCycle.incomingExchangesVariants()).toEqual [1, 4, 5]
describe 'checking whether a product is supplied to the order cycle', ->
product_master_present = product_variant_present = product_master_absent = product_variant_absent = null
beforeEach ->
product_master_present =
name: "Linseed (500g)"
master_id: 1
variants: []
product_variant_present =
name: "Linseed (500g)"
master_id: 2
variants: [{id: 3}, {id: 4}]
product_master_absent =
name: "Linseed (500g)"
master_id: 5
variants: []
product_variant_absent =
name: "Linseed (500g)"
master_id: 6
variants: [{id: 7}, {id: 8}]
spyOn(OrderCycle, 'incomingExchangesVariants').andReturn([1, 3])
it 'returns true for products whose master is supplied', ->
expect(OrderCycle.productSuppliedToOrderCycle(product_master_present)).toBeTruthy()
it 'returns true for products for whom a variant is supplied', ->
expect(OrderCycle.productSuppliedToOrderCycle(product_variant_present)).toBeTruthy()
it 'returns false for products whose master is not supplied', ->
expect(OrderCycle.productSuppliedToOrderCycle(product_master_absent)).toBeFalsy()
it 'returns false for products whose variants are not supplied', ->
expect(OrderCycle.productSuppliedToOrderCycle(product_variant_absent)).toBeFalsy()
describe 'loading an order cycle', ->
beforeEach ->
OrderCycle.load('123')