Skip to content

Instantly share code, notes, and snippets.

@andybak
Created January 11, 2014 13:10
Show Gist options
  • Save andybak/8370783 to your computer and use it in GitHub Desktop.
Save andybak/8370783 to your computer and use it in GitHub Desktop.
var stockApp = angular.module('stockApp', [
'ngResource',
]);
stockApp.value('currentBooking', {bookingId: BOOKING_ID});
stockApp.factory('TimeBlock', [function() {
}]);
stockApp.controller('BookingCtrl', ['$scope', '$http', 'currentBooking', 'Booking',
function($scope, $http, currentBooking, Booking) {
$scope.bookingId = function () {
return currentBooking.bookingId;
};
$scope.$watch('bookingId()', function () {
var booking = Booking.get({id: currentBooking.bookingId}, function() {
var start = new Date(booking.leaves_stock);
var end = new Date(booking.returns_to_stock);
start.setDate(start.getDate() - 1);
end.setDate(end.getDate() - 1);
$scope.start = start;
$scope.end = end;
$scope.booking = booking;
});
});
}
]);
stockApp.controller('TimeBlockCtrl', ['$scope',
function ($scope) {
$scope.dateRange = function(booking){
var input = [];
var start = $scope.start;
var end = $scope.end;
for (var i=start; i<=end; i.setDate(i.getDate() + 1)) {
input.push(i.getDate()+'/'+ (i.getMonth() + 1));
}
return input;
};
}
]);
stockApp.controller('StockListCtrl', ['$scope', 'StockItems', 'currentBooking',
function($scope, StockItems, currentBooking) {
$scope.stockItems = StockItems;
StockItems.currentBooking = currentBooking;
}
]);
stockApp.factory('Booking', ['$resource', function($resource) {
return $resource('/api/booking/:id', null, {
"update": {method: "PUT"}
});
}]);
stockApp.factory('StockItems', function ($http) {
return {
items: [],
currentBooking: null,
search: function (query) {
var self = this;
self.items = [];
$http.get('/api/stock_item/?search=' + query).success(function(data) {
data.results.forEach(function (stock_item) {
self.items.push(stock_item);
var id = stock_item.url.split('/');
id = id[id.length-2];
if (self.currentBooking.bookingId) {
$http.get('/api/stock_item/' + id + '/get_segments_for_booking/?booking=' + self.currentBooking.bookingId)
.success(function (data) {
stock_item.segments = data.segments;
});
}
});
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment