Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var OrderCtrl = function ($scope, $http, $storage, security, $location, tools) {
- $scope.products = [];
- $scope.variants = [];
- $scope.orderGrid = [];
- $scope.grades = [];
- $scope.totalStudents = 0;
- $scope.orderSubTotal = 0;
- $scope.orderTotal = 0;
- $scope.servicesSubTotal = 0;
- $scope.posterVariantsRequired = false;
- $scope.editMode = false;
- populateVariants();
- populatePricingTable();
- populateOrderGrid();
- function populatePricingTable() {
- $http.get('data/products.json')
- .success(function (response) {
- $scope.products = response;
- });
- }
- function populateVariants() {
- $http.get('data/variants.json')
- .success(function (response) {
- $scope.variants = response;
- });
- }
- function populateOrderGrid() {
- for (var i = 3; i <= 12; i++) {
- $scope.grades.push({name:'Grade ' + i, quantity:0, number:i});
- }
- $http.get('data/products.json')
- .success(function (response) {
- var products = response;
- var grid = [];
- products.forEach(function (item) {
- var gridRow = item;
- if (item.type == 1)
- gridRow.gradeQuantities = angular.copy($scope.grades);
- gridRow.total = 0;
- gridRow.quantity = 0;
- grid.push(gridRow);
- });
- $scope.orderGrid = grid;
- // enter edit mode if any
- edit();
- });
- }
- $scope.calculateServicesSubTotal = function (item) {
- if (item) {
- var quantity = (isNaN(parseInt(item.quantity))) ? 0 : parseInt(item.quantity);
- if (item.id == 8) {
- item.price = tools.getPriceForQuantity($scope.variants, item, quantity).price;
- }
- item.total = item.price * quantity;
- }
- var tempServicesSubTotal = 0;
- $scope.orderGrid.forEach(function (item) {
- if (item.type == 2) {
- tempServicesSubTotal += item.total;
- }
- });
- $scope.servicesSubTotal = tempServicesSubTotal;
- $scope.orderTotal = $scope.orderSubTotal + tempServicesSubTotal;
- }
- $scope.calculatePosterSubTotal = function (item) {
- var price = tools.getPriceForQuantity($scope.variants, item, item.quantity).price;
- item.total = item.quantity * price;
- $scope.calculateServicesSubTotal();
- };
- $scope.calculateTotals = function () {
- var tempTotalStudents = 0;
- var tempOrderSubTotal = 0;
- var price = 0;
- $scope.orderGrid.forEach(function (row) {
- if (row.type == 1) {
- row.gradeQuantities.forEach(function (grade) {
- var quantity = (isNaN(parseInt(grade.quantity))) ? 0 : parseInt(grade.quantity);
- tempTotalStudents += quantity;
- price = tools.getPriceForQuantity($scope.variants, row, quantity).price;
- tempOrderSubTotal += price * quantity;
- });
- }
- });
- $scope.orderSubTotal = tempOrderSubTotal;
- $scope.totalStudents = tempTotalStudents;
- $scope.orderTotal = tempOrderSubTotal + $scope.servicesSubTotal;
- };
- function edit() {
- var editMode = $storage('editMode').getItem(0);
- if (editMode && editMode.value == true) {
- $scope.editMode = true;
- }
- var orderTable = $storage('order');
- var order = orderTable.getItem(0);
- if (order) {
- $scope.totalStudents = order.totalStudents;
- $scope.orderSubTotal = order.orderSubTotal;
- $scope.servicesSubTotal = order.servicesSubTotal;
- $scope.orderGrid = order.orderGradesQuantities;
- $scope.orderTotal = order.orderSubTotal + order.servicesSubTotal;
- }
- }
- $scope.cancelEdit = function () {
- tools.cancelEditMode();
- $location.path('/confirm');
- };
- $scope.checkout = function () {
- if ($scope.frmRubricPosters.$valid) {
- var order = {
- totalStudents:$scope.totalStudents,
- orderSubTotal:$scope.orderSubTotal,
- servicesSubTotal:$scope.servicesSubTotal,
- orderGradesQuantities:$scope.orderGrid
- }
- var orderTable = $storage('order');
- //remove previous orders
- orderTable.removeItem(0);
- orderTable.setItem(order);
- if ($scope.editMode) {
- tools.cancelEditMode();
- $location.path('/confirm');
- }
- else
- $location.path('/payment');
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement