Created
January 21, 2014 10:13
-
-
Save andybak/8537515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bookingScreenApp = angular.module('bookingScreenApp'); | |
bookingScreenApp.controller('BookingCtrl', function($scope, currentBooking) { | |
$scope.currentBooking = currentBooking; | |
$scope.getBookingData = function () { return currentBooking.bookingData }; | |
$scope.getStart = function () { return currentBooking.start }; | |
$scope.getEnd = function () { return currentBooking.end }; | |
$scope.getActualDuration = function () { return ((currentBooking.end - currentBooking.start)/86400000) - 2 }; | |
}); | |
bookingScreenApp.controller('TimeBlockCtrl', function ($scope) { | |
$scope.dateRange = function(getStart, getEnd){ | |
var input = []; | |
var start = getStart(); | |
var end = getEnd(); | |
if (start && end) { | |
//Clone the date objects before we iterate | |
start = new Date(start.valueOf()); | |
end = new Date(end.valueOf()); | |
for (var i=start; i<=end; i.setDate(i.getDate() + 1)) { | |
input.push(i.getDate()+'/'+ (i.getMonth() + 1)); | |
} | |
return input; | |
} | |
}; | |
}); | |
bookingScreenApp.controller('StockListCtrl', function($scope, inventory, currentBooking) { | |
$scope.inventory = inventory; | |
inventory.currentBooking = currentBooking; | |
} | |
); | |
bookingScreenApp.controller('NewBookingCtrl', function($scope, NewBooking, currentBooking) { | |
$scope.newBookingData = {}; | |
$scope.createNewBooking = function() { | |
NewBooking.create($scope.newBookingData, currentBooking.setFromId); | |
}; | |
} | |
); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bookingScreenApp = angular.module('bookingScreenApp'); | |
bookingScreenApp.factory('NewBooking', function ($http) { | |
return { | |
newBookingData: null, | |
create: function(newBookingData, setBooking) { | |
return $http.post('/api/create-booking/', newBookingData) | |
.success(function(data){ | |
setBooking(data.id); | |
}) | |
.error(function(){ | |
alert("error creating new booking"); | |
}); | |
} | |
} | |
}); | |
bookingScreenApp.factory('currentBooking', function ($http) { | |
var currentBooking = { | |
bookingId: null, | |
bookingData: null, | |
start: null, | |
end: null, | |
setFromData: function(data) { | |
var _this = this; | |
var bookingData = data; | |
var start = new Date(bookingData.leaves_stock); | |
var end = new Date(bookingData.returns_to_stock); | |
start.setDate(start.getDate() - 1); | |
end.setDate(end.getDate() + 1); | |
_this.start = start; | |
_this.end = end; | |
_this.bookingData = bookingData; | |
}, | |
setFromId: function (bookingId) { | |
var _this = this; | |
if (bookingId === this.bookingId) return; | |
this.bookingId = bookingId; | |
console.log('getting'); | |
console.log(this); | |
console.log(this.bookingId); | |
$http.get('/api/booking/' + bookingId) | |
.success(function(data){ | |
currentBooking.setFromData(data); | |
}); | |
}, | |
save: function() { | |
console.log('saving'); | |
console.log(this); | |
console.log(this.bookingId); | |
var bookingDataFlat = {}; | |
for (var k in this.bookingData) { | |
var v = this.bookingData[k]; | |
if (typeof v === 'object' && v != null) { | |
bookingDataFlat[k] = v.id; | |
} else { | |
bookingDataFlat[k] = v; | |
} | |
} | |
$http.put('/api/booking/' + this.bookingId + '/', bookingDataFlat) | |
.success(function(){ | |
}) | |
.error(function(){ | |
alert("error saving"); | |
}); | |
} | |
}; | |
// Initialised from variable preloaded in page | |
currentBooking.setFromId(BOOKING_ID); | |
return currentBooking; | |
}); | |
bookingScreenApp.factory('inventory', function ($http) { | |
return { | |
items: [], | |
currentBooking: null, | |
toggleChildren: function(stockItem) { | |
if (stockItem.type=="tracked") { | |
var self = this; | |
stockItem.showChild = !stockItem.showChild; | |
if (stockItem.showChild) { | |
angular.forEach(stockItem.stockitems, function(tracked_child){ | |
$http.get( | |
'/api/stock_item/' + tracked_child.id + '/get_segments_for_period/', { | |
params: { | |
start: self.currentBooking.start.toISOString().replace('Z',''), | |
end: self.currentBooking.end.toISOString().replace('Z','') | |
} | |
} | |
) | |
.success(function (data) { | |
tracked_child.segments = data.segments; | |
}); | |
}); | |
} | |
} | |
}, | |
AddToBooking: function(stockItem) { | |
var self = this; | |
$http.put( | |
'/api/stock_item/' + stockItem.id + '/add_to_booking/' + self.currentBooking.bookingId, { | |
} | |
) | |
.success(function (data) { | |
stockItem.segments = data.segments; | |
self.currentBooking.setFromData(data.booking_data); | |
}) | |
.error(function(data){ | |
alert('Error in AddToBooking'); | |
}); | |
}, | |
search: function (query) { | |
var self = this; | |
self.items = []; | |
$http.get('/api/item_model/?search=' + query).success(function(data) { | |
data.results.forEach(function (item_model) { | |
item_model.showChild = false; | |
self.items.push(item_model); | |
$http.get( | |
'/api/item_model/' + item_model.id + '/get_segments_for_period', { | |
params: { | |
start: self.currentBooking.start.toISOString().replace('Z',''), | |
end: self.currentBooking.end.toISOString().replace('Z','') | |
} | |
} | |
) | |
.success(function (data) { | |
item_model.segments = data.segments; | |
}); | |
}); | |
}); | |
}, | |
clearSearch: function(e) { | |
this.items = []; | |
e.search_term=''; | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment