mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-28 21:07:16 +00:00
216 lines
5.8 KiB
Plaintext
216 lines
5.8 KiB
Plaintext
function AdminCreateOrderCycleCtrl($scope, OrderCycle, Enterprise) {
|
|
$scope.enterprises = Enterprise.index();
|
|
|
|
$scope.order_cycle = OrderCycle.order_cycle;
|
|
|
|
$scope.exchangeSelectedVariants = function(exchange) {
|
|
return OrderCycle.exchangeSelectedVariants(exchange);
|
|
};
|
|
|
|
$scope.enterpriseTotalVariants = function(enterprise) {
|
|
return OrderCycle.enterpriseTotalVariants(enterprise);
|
|
};
|
|
|
|
$scope.toggleProducts = function($event, exchange) {
|
|
$event.preventDefault();
|
|
OrderCycle.toggleProducts(exchange);
|
|
};
|
|
|
|
$scope.addSupplier = function($event) {
|
|
$event.preventDefault();
|
|
OrderCycle.addSupplier($scope.new_supplier_id);
|
|
};
|
|
|
|
$scope.submit = function() {
|
|
OrderCycle.create();
|
|
};
|
|
}
|
|
|
|
|
|
function AdminEditOrderCycleCtrl($scope, $location, OrderCycle, Enterprise) {
|
|
$scope.enterprises = Enterprise.index();
|
|
|
|
var order_cycle_id = $location.absUrl().match(/\/admin\/order_cycles\/(\d+)/)[1];
|
|
$scope.order_cycle = OrderCycle.load(order_cycle_id);
|
|
|
|
$scope.exchangeSelectedVariants = function(exchange) {
|
|
return OrderCycle.exchangeSelectedVariants(exchange);
|
|
};
|
|
|
|
$scope.enterpriseTotalVariants = function(enterprise) {
|
|
return OrderCycle.enterpriseTotalVariants(enterprise);
|
|
};
|
|
|
|
$scope.toggleProducts = function($event, exchange) {
|
|
$event.preventDefault();
|
|
OrderCycle.toggleProducts(exchange);
|
|
};
|
|
|
|
$scope.addSupplier = function($event) {
|
|
$event.preventDefault();
|
|
OrderCycle.addSupplier($scope.new_supplier_id);
|
|
};
|
|
|
|
$scope.submit = function() {
|
|
OrderCycle.update();
|
|
};
|
|
}
|
|
|
|
|
|
angular.module('order_cycle', ['ngResource']).
|
|
config(function($httpProvider) {
|
|
$httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
|
|
}).
|
|
factory('OrderCycle', function($resource, $window) {
|
|
var OrderCycle = $resource('/admin/order_cycles/:order_cycle_id.json', {},
|
|
{'index': { method: 'GET', isArray: true},
|
|
'create': { method: 'POST'},
|
|
'update': { method: 'PUT'}});
|
|
|
|
|
|
return {
|
|
order_cycle: {incoming_exchanges: [],
|
|
outgoing_exchanges: []},
|
|
|
|
exchangeSelectedVariants: function(exchange) {
|
|
var numActiveVariants = 0;
|
|
|
|
angular.forEach(exchange.variants, function(active, id) {
|
|
if(active) {
|
|
numActiveVariants++;
|
|
}
|
|
});
|
|
|
|
return numActiveVariants;
|
|
},
|
|
|
|
enterpriseTotalVariants: 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
|
|
},
|
|
|
|
addSupplier: function(new_supplier_id) {
|
|
this.order_cycle.incoming_exchanges.push({enterprise_id: new_supplier_id, active: true, variants: {}});
|
|
},
|
|
|
|
load: function(order_cycle_id) {
|
|
var service = this;
|
|
|
|
OrderCycle.get({order_cycle_id: order_cycle_id}, function(oc) {
|
|
$.extend(service.order_cycle, oc);
|
|
service.order_cycle.incoming_exchanges = [];
|
|
service.order_cycle.outgoing_exchanges = [];
|
|
for(i in service.order_cycle.exchanges) {
|
|
var exchange = service.order_cycle.exchanges[i];
|
|
if(exchange.sender_id == service.order_cycle.coordinator_id) {
|
|
$.extend(exchange, {enterprise_id: exchange.receiver_id, active: true});
|
|
delete(exchange.sender_id);
|
|
service.order_cycle.outgoing_exchanges.push(exchange);
|
|
|
|
} else if(exchange.receiver_id == service.order_cycle.coordinator_id) {
|
|
$.extend(exchange, {enterprise_id: exchange.sender_id, active: true});
|
|
delete(exchange.receiver_id);
|
|
service.order_cycle.incoming_exchanges.push(exchange)
|
|
|
|
} else {
|
|
console.log('Exchange between two enterprises, neither of which is coordinator!');
|
|
}
|
|
}
|
|
|
|
delete(service.order_cycle.exchanges);
|
|
});
|
|
|
|
return this.order_cycle;
|
|
},
|
|
|
|
create: function() {
|
|
this.removeInactiveExchanges();
|
|
|
|
var oc = new OrderCycle({order_cycle: this.order_cycle});
|
|
oc.$create(function(data) {
|
|
if(data['success']) {
|
|
$window.location = '/admin/order_cycles';
|
|
} else {
|
|
console.log('fail');
|
|
}
|
|
});
|
|
},
|
|
|
|
update: function() {
|
|
this.removeInactiveExchanges();
|
|
|
|
var oc = new OrderCycle({order_cycle: this.order_cycle});
|
|
oc.$update({order_cycle_id: this.order_cycle.id}, function(data) {
|
|
if(data['success']) {
|
|
$window.location = '/admin/order_cycles';
|
|
} else {
|
|
console.log('fail');
|
|
}
|
|
});
|
|
},
|
|
|
|
removeInactiveExchanges: function() {
|
|
angular.forEach([this.order_cycle.incoming_exchanges,
|
|
this.order_cycle.outgoing_exchanges],
|
|
function(exchanges) {
|
|
for(var i=0; i < exchanges.length; i++) {
|
|
if(!exchanges[i].active) {
|
|
exchanges.splice(i, 1);
|
|
i--;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
};
|
|
}).
|
|
factory('Enterprise', function($resource) {
|
|
var Enterprise = $resource('/admin/enterprises/:enterprise_id.json', {},
|
|
{'index': { method: 'GET', isArray: true}});
|
|
|
|
return {
|
|
Enterprise: Enterprise,
|
|
enterprises: {},
|
|
|
|
index: function() {
|
|
var service = this;
|
|
|
|
Enterprise.index(function(data) {
|
|
for(i in data) {
|
|
service.enterprises[data[i]['id']] = data[i];
|
|
}
|
|
});
|
|
|
|
return this.enterprises;
|
|
}
|
|
};
|
|
}).
|
|
directive('datetimepicker', ['$parse', function($parse) {
|
|
return function(scope, element, attrs) {
|
|
// using $parse instead of scope[attrs.datetimepicker] for cases
|
|
// where attrs.datetimepicker is 'foo.bar.lol'
|
|
$(element).datetimepicker({
|
|
dateFormat: 'yy-mm-dd',
|
|
timeFormat: 'HH:mm:ss',
|
|
showOn: "button",
|
|
buttonImage: "<%= asset_path 'datepicker/cal.gif' %>",
|
|
buttonImageOnly: true,
|
|
stepMinute: 15,
|
|
onSelect: function(dateText, inst) {
|
|
scope.$apply(function() {
|
|
parsed = $parse(attrs.datetimepicker);
|
|
parsed.assign(scope, dateText);
|
|
});
|
|
}
|
|
});
|
|
};
|
|
}]);
|