mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Count selected variants on an exchange
This commit is contained in:
@@ -3,6 +3,14 @@ function AdminCreateOrderCycleCtrl($scope, OrderCycle, Enterprise) {
|
||||
|
||||
$scope.order_cycle = OrderCycle.order_cycle;
|
||||
|
||||
$scope.exchangeVariantsSelected = function(exchange) {
|
||||
return OrderCycle.exchangeVariantsSelected(exchange);
|
||||
};
|
||||
|
||||
$scope.enterpriseVariantsTotal = function(enterprise) {
|
||||
return OrderCycle.enterpriseVariantsTotal(enterprise);
|
||||
};
|
||||
|
||||
$scope.toggleProducts = function($event, exchange) {
|
||||
$event.preventDefault();
|
||||
OrderCycle.toggleProducts(exchange);
|
||||
@@ -25,6 +33,14 @@ function AdminEditOrderCycleCtrl($scope, $location, OrderCycle, Enterprise) {
|
||||
var order_cycle_id = $location.absUrl().match(/\/admin\/order_cycles\/(\d+)/)[1];
|
||||
$scope.order_cycle = OrderCycle.load(order_cycle_id);
|
||||
|
||||
$scope.exchangeVariantsSelected = function(exchange) {
|
||||
return OrderCycle.exchangeVariantsSelected(exchange);
|
||||
};
|
||||
|
||||
$scope.enterpriseVariantsTotal = function(enterprise) {
|
||||
return OrderCycle.enterpriseVariantsTotal(enterprise);
|
||||
};
|
||||
|
||||
$scope.toggleProducts = function($event, exchange) {
|
||||
$event.preventDefault();
|
||||
OrderCycle.toggleProducts(exchange);
|
||||
@@ -56,6 +72,28 @@ angular.module('order_cycle', ['ngResource']).
|
||||
order_cycle: {incoming_exchanges: [],
|
||||
outgoing_exchanges: []},
|
||||
|
||||
exchangeVariantsSelected: function(exchange) {
|
||||
var numActiveVariants = 0;
|
||||
|
||||
angular.forEach(exchange.variants, function(active, id) {
|
||||
if(active) {
|
||||
numActiveVariants++;
|
||||
}
|
||||
});
|
||||
|
||||
return numActiveVariants;
|
||||
},
|
||||
|
||||
enterpriseVariantsTotal: function(enterprise) {
|
||||
var numVariants = 0;
|
||||
|
||||
angular.forEach(enterprise.supplied_products, function(product) {
|
||||
numVariants += product.variants.length == 0 ? 1 : product.variants.length;
|
||||
});
|
||||
|
||||
return numVariants;
|
||||
},
|
||||
|
||||
toggleProducts: function(exchange) {
|
||||
exchange.showProducts = !exchange.showProducts
|
||||
},
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
.exchange-product{'ng-repeat' => 'product in enterprises[exchange.enterprise_id].supplied_products'}
|
||||
.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 }}'}
|
||||
{{ product.name }}
|
||||
.exchange-product-variant{'ng-repeat' => 'variant in product.variants'}
|
||||
= check_box_tag 'order_cycle_exchange_{{ $parent.$parent.$index }}_variants_{{ variant.id }}', 1, 1, 'ng-model' => 'exchange.variants[variant.id]', 'id' => 'order_cycle_exchange_{{ $parent.$parent.$index }}_variants_{{ variant.id }}'
|
||||
{{ variant.label }}
|
||||
@@ -22,18 +22,12 @@
|
||||
%tr.supplier
|
||||
%td.active= check_box_tag 'order_cycle_exchange_{{ $index }}_active', 1, 1, 'ng-model' => 'exchange.active', 'id' => 'order_cycle_exchange_{{ $index }}_active'
|
||||
%td.supplier_name {{ enterprises[exchange.enterprise_id].name }}
|
||||
%td.products= f.submit 'Products', 'ng-click' => 'toggleProducts($event, exchange)'
|
||||
%td.products
|
||||
= f.submit 'Products', 'ng-click' => 'toggleProducts($event, exchange)'
|
||||
{{ exchangeVariantsSelected(exchange) }} / {{ enterpriseVariantsTotal(enterprises[exchange.enterprise_id]) }} selected
|
||||
%tr.products{'ng-show' => 'exchange.showProducts'}
|
||||
%td{:colspan => 3}
|
||||
.exchange-product{'ng-repeat' => 'product in enterprises[exchange.enterprise_id].supplied_products'}
|
||||
|
||||
.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 }}'}
|
||||
{{ product.name }}
|
||||
.exchange-product-variant{'ng-repeat' => 'variant in product.variants'}
|
||||
= check_box_tag 'order_cycle_exchange_{{ $parent.$parent.$index }}_variants_{{ variant.id }}', 1, 1, 'ng-model' => 'exchange.variants[variant.id]', 'id' => 'order_cycle_exchange_{{ $parent.$parent.$index }}_variants_{{ variant.id }}'
|
||||
{{ variant.label }}
|
||||
= render 'exchange_product_form'
|
||||
|
||||
= select_tag :new_supplier_id, options_from_collection_for_select(Enterprise.is_primary_producer, :id, :name), {'ng-model' => 'new_supplier_id'}
|
||||
= f.submit 'Add supplier', 'ng-click' => 'addSupplier($event)'
|
||||
|
||||
@@ -13,6 +13,8 @@ describe 'OrderCycle controllers', ->
|
||||
preventDefault: jasmine.createSpy('preventDefault')
|
||||
OrderCycle =
|
||||
order_cycle: 'my order cycle'
|
||||
exchangeVariantsSelected: jasmine.createSpy('exchangeVariantsSelected').andReturn('variants selected')
|
||||
enterpriseVariantsTotal: jasmine.createSpy('enterpriseVariantsTotal').andReturn('variants total')
|
||||
toggleProducts: jasmine.createSpy('toggleProducts')
|
||||
addSupplier: jasmine.createSpy('addSupplier')
|
||||
create: jasmine.createSpy('create')
|
||||
@@ -27,6 +29,12 @@ describe 'OrderCycle controllers', ->
|
||||
it 'Loads order cycles', ->
|
||||
expect(scope.order_cycle).toEqual('my order cycle')
|
||||
|
||||
it 'Delegates exchangeVariantsSelected and enterpriseVariantsTotal to OrderCycle', ->
|
||||
expect(scope.exchangeVariantsSelected('exchange')).toEqual('variants selected')
|
||||
expect(OrderCycle.exchangeVariantsSelected).toHaveBeenCalledWith('exchange')
|
||||
expect(scope.enterpriseVariantsTotal('enterprise')).toEqual('variants total')
|
||||
expect(OrderCycle.enterpriseVariantsTotal).toHaveBeenCalledWith('enterprise')
|
||||
|
||||
it 'Delegates toggleProducts to OrderCycle', ->
|
||||
scope.toggleProducts(event, 'exchange')
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
@@ -59,6 +67,8 @@ describe 'OrderCycle controllers', ->
|
||||
'example.com/admin/order_cycles/27/edit'
|
||||
OrderCycle =
|
||||
load: jasmine.createSpy('load')
|
||||
exchangeVariantsSelected: jasmine.createSpy('exchangeVariantsSelected').andReturn('variants selected')
|
||||
enterpriseVariantsTotal: jasmine.createSpy('enterpriseVariantsTotal').andReturn('variants total')
|
||||
toggleProducts: jasmine.createSpy('toggleProducts')
|
||||
addSupplier: jasmine.createSpy('addSupplier')
|
||||
update: jasmine.createSpy('update')
|
||||
@@ -73,6 +83,12 @@ describe 'OrderCycle controllers', ->
|
||||
it 'Loads order cycles', ->
|
||||
expect(OrderCycle.load).toHaveBeenCalledWith('27')
|
||||
|
||||
it 'Delegates exchangeVariantsSelected and enterpriseVariantsTotal to OrderCycle', ->
|
||||
expect(scope.exchangeVariantsSelected('exchange')).toEqual('variants selected')
|
||||
expect(OrderCycle.exchangeVariantsSelected).toHaveBeenCalledWith('exchange')
|
||||
expect(scope.enterpriseVariantsTotal('enterprise')).toEqual('variants total')
|
||||
expect(OrderCycle.enterpriseVariantsTotal).toHaveBeenCalledWith('enterprise')
|
||||
|
||||
it 'Delegates toggleProducts to OrderCycle', ->
|
||||
scope.toggleProducts(event, 'exchange')
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
@@ -143,6 +159,21 @@ describe 'OrderCycle services', ->
|
||||
incoming_exchanges: []
|
||||
outgoing_exchanges: []
|
||||
|
||||
describe 'counting variants', ->
|
||||
it 'counts selected variants in an exchange', ->
|
||||
result = OrderCycle.exchangeVariantsSelected({variants: {1: true, 2: false, 3: true}})
|
||||
expect(result).toEqual(2)
|
||||
|
||||
it 'counts total variants supplied by an enterprise', ->
|
||||
enterprise =
|
||||
supplied_products: [
|
||||
{variants: []},
|
||||
{variants: []},
|
||||
{variants: [{}, {}, {}]}
|
||||
]
|
||||
|
||||
expect(OrderCycle.enterpriseVariantsTotal(enterprise)).toEqual(5)
|
||||
|
||||
describe 'toggling products', ->
|
||||
exchange = null
|
||||
|
||||
@@ -169,7 +200,7 @@ describe 'OrderCycle services', ->
|
||||
it 'adds the supplier to incoming exchanges', ->
|
||||
OrderCycle.addSupplier('123')
|
||||
expect(OrderCycle.order_cycle.incoming_exchanges).toEqual [
|
||||
{enterprise_id: '123', active: true}
|
||||
{enterprise_id: '123', active: true, variants: {}}
|
||||
]
|
||||
|
||||
describe 'loading an order cycle', ->
|
||||
|
||||
Reference in New Issue
Block a user