Advertisement
Guest User

order.js

a guest
Jul 31st, 2013
1,374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var OrderCtrl = function ($scope, $http, $storage, security, $location, tools) {
  2.     $scope.products = [];
  3.     $scope.variants = [];
  4.     $scope.orderGrid = [];
  5.     $scope.grades = [];
  6.     $scope.totalStudents = 0;
  7.     $scope.orderSubTotal = 0;
  8.     $scope.orderTotal = 0;
  9.     $scope.servicesSubTotal = 0;
  10.     $scope.posterVariantsRequired = false;
  11.     $scope.editMode = false;
  12.  
  13.     populateVariants();
  14.     populatePricingTable();
  15.     populateOrderGrid();
  16.  
  17.     function populatePricingTable() {
  18.         $http.get('data/products.json')
  19.             .success(function (response) {
  20.                 $scope.products = response;
  21.             });
  22.     }
  23.  
  24.     function populateVariants() {
  25.         $http.get('data/variants.json')
  26.             .success(function (response) {
  27.                 $scope.variants = response;
  28.             });
  29.     }
  30.  
  31.     function populateOrderGrid() {
  32.         for (var i = 3; i <= 12; i++) {
  33.             $scope.grades.push({name:'Grade ' + i, quantity:0, number:i});
  34.         }
  35.         $http.get('data/products.json')
  36.             .success(function (response) {
  37.                 var products = response;
  38.                 var grid = [];
  39.                 products.forEach(function (item) {
  40.                     var gridRow = item;
  41.                     if (item.type == 1)
  42.                         gridRow.gradeQuantities = angular.copy($scope.grades);
  43.                     gridRow.total = 0;
  44.                     gridRow.quantity = 0;
  45.                     grid.push(gridRow);
  46.                 });
  47.                 $scope.orderGrid = grid;
  48.  
  49.                 // enter edit mode if any
  50.                 edit();
  51.             });
  52.     }
  53.  
  54.     $scope.calculateServicesSubTotal = function (item) {
  55.         if (item) {
  56.             var quantity = (isNaN(parseInt(item.quantity))) ? 0 : parseInt(item.quantity);
  57.             if (item.id == 8) {
  58.                 item.price = tools.getPriceForQuantity($scope.variants, item, quantity).price;
  59.             }
  60.             item.total = item.price * quantity;
  61.         }
  62.         var tempServicesSubTotal = 0;
  63.         $scope.orderGrid.forEach(function (item) {
  64.             if (item.type == 2) {
  65.                 tempServicesSubTotal += item.total;
  66.             }
  67.         });
  68.         $scope.servicesSubTotal = tempServicesSubTotal;
  69.         $scope.orderTotal = $scope.orderSubTotal + tempServicesSubTotal;
  70.     }
  71.  
  72.     $scope.calculatePosterSubTotal = function (item) {
  73.         var price = tools.getPriceForQuantity($scope.variants, item, item.quantity).price;
  74.         item.total = item.quantity * price;
  75.         $scope.calculateServicesSubTotal();
  76.     };
  77.  
  78.     $scope.calculateTotals = function () {
  79.         var tempTotalStudents = 0;
  80.         var tempOrderSubTotal = 0;
  81.         var price = 0;
  82.         $scope.orderGrid.forEach(function (row) {
  83.             if (row.type == 1) {
  84.                 row.gradeQuantities.forEach(function (grade) {
  85.                     var quantity = (isNaN(parseInt(grade.quantity))) ? 0 : parseInt(grade.quantity);
  86.                     tempTotalStudents += quantity;
  87.                     price = tools.getPriceForQuantity($scope.variants, row, quantity).price;
  88.                     tempOrderSubTotal += price * quantity;
  89.                 });
  90.             }
  91.         });
  92.         $scope.orderSubTotal = tempOrderSubTotal;
  93.         $scope.totalStudents = tempTotalStudents;
  94.         $scope.orderTotal = tempOrderSubTotal + $scope.servicesSubTotal;
  95.     };
  96.  
  97.     function edit() {
  98.         var editMode = $storage('editMode').getItem(0);
  99.         if (editMode && editMode.value == true) {
  100.             $scope.editMode = true;
  101.         }
  102.         var orderTable = $storage('order');
  103.         var order = orderTable.getItem(0);
  104.         if (order) {
  105.             $scope.totalStudents = order.totalStudents;
  106.             $scope.orderSubTotal = order.orderSubTotal;
  107.             $scope.servicesSubTotal = order.servicesSubTotal;
  108.             $scope.orderGrid = order.orderGradesQuantities;
  109.             $scope.orderTotal = order.orderSubTotal + order.servicesSubTotal;
  110.         }
  111.     }
  112.  
  113.     $scope.cancelEdit = function () {
  114.         tools.cancelEditMode();
  115.         $location.path('/confirm');
  116.     };
  117.  
  118.     $scope.checkout = function () {
  119.         if ($scope.frmRubricPosters.$valid) {
  120.             var order = {
  121.                 totalStudents:$scope.totalStudents,
  122.                 orderSubTotal:$scope.orderSubTotal,
  123.                 servicesSubTotal:$scope.servicesSubTotal,
  124.                 orderGradesQuantities:$scope.orderGrid
  125.             }
  126.             var orderTable = $storage('order');
  127.             //remove previous orders
  128.             orderTable.removeItem(0);
  129.             orderTable.setItem(order);
  130.             if ($scope.editMode) {
  131.                 tools.cancelEditMode();
  132.                 $location.path('/confirm');
  133.             }
  134.             else
  135.                 $location.path('/payment');
  136.         }
  137.     };
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement