mealsConstructorCtrl.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. app.controller('mealsConstructor', ['$window','$scope','$interval',function($window,$scope, $interval) {
  2. // $scope.list1 = [
  3. // {'title': 'Br_1', imageUrl:'./img/1.jpg', price: 10},{'title': 'Br_2', imageUrl:'./img/1.jpg', price: 10},
  4. // {'title': 'Br_3', imageUrl:'./img/1.jpg', price: 10},{'title': 'Br_4', imageUrl:'./img/1.jpg', price: 10}
  5. // ];
  6. // $scope.list2 = [
  7. // {'title': 'Lu_1', imageUrl:'./img/2.jpg', price: 10},{'title': 'Lu_2', imageUrl:'./img/2.jpg', price: 10},
  8. // {'title': 'Lu_3', imageUrl:'./img/2.jpg', price: 10},{'title': 'Lu_4', imageUrl:'./img/2.jpg', price: 10}
  9. // ];
  10. // $scope.list3 = [
  11. // {'title': 'Di_1', imageUrl:'./img/3.jpg', price: 10},{'title': 'Di_2', imageUrl:'./img/3.jpg', price: 10},
  12. // {'title': 'Di_3', imageUrl:'./img/3.jpg', price: 10},{'title': 'Di_4', imageUrl:'./img/3.jpg', price: 10}
  13. // ];
  14. // $scope.list4 = [
  15. // {'title': 'Sn_1', imageUrl:'./img/4.jpg', price: 10},{'title': 'Sn_2', imageUrl:'./img/4.jpg', price: 10},
  16. // {'title': 'Sn_3', imageUrl:'./img/4.jpg', price: 10},{'title': 'Sn_4', imageUrl:'./img/4.jpg', price: 10}
  17. // ];
  18. $scope.list1 = [];
  19. $scope.list2 = [];
  20. $scope.list3 = [];
  21. $scope.list4 = [];
  22. $scope.firebaseMeals = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('meals');
  23. $scope.firebaseGlobalMeals = firebase.database().ref().child('meals');
  24. $scope.firebaseMeals.on('value', function(snap){
  25. $scope.userMeals = snap.val();
  26. var userMealsKeys = Object.keys($scope.userMeals);
  27. for (var i = 0; i < userMealsKeys.length; i++) {
  28. if($scope.userMeals[userMealsKeys[i]].type == 'breakfast' && $scope.userMeals[userMealsKeys[i]].isSelected == true){
  29. $scope.list1.push($scope.userMeals[userMealsKeys[i]]);
  30. }
  31. if($scope.userMeals[userMealsKeys[i]].type == 'lunch' && $scope.userMeals[userMealsKeys[i]].isSelected == true){
  32. $scope.list2.push($scope.userMeals[userMealsKeys[i]]);
  33. }
  34. if($scope.userMeals[userMealsKeys[i]].type == 'dinner' && $scope.userMeals[userMealsKeys[i]].isSelected == true){
  35. $scope.list3.push($scope.userMeals[userMealsKeys[i]]);
  36. }
  37. if($scope.userMeals[userMealsKeys[i]].type == 'snacks' && $scope.userMeals[userMealsKeys[i]].isSelected == true){
  38. $scope.list4.push($scope.userMeals[userMealsKeys[i]]);
  39. }
  40. }
  41. })
  42. $scope.arrDays = [
  43. {caption: 'DAY 1', day: 'day1'},
  44. {caption: 'DAY 2', day: 'day2'},
  45. {caption: 'DAY 3', day: 'day3'},
  46. {caption: 'DAY 4', day: 'day4'},
  47. {caption: 'DAY 5', day: 'day5'},
  48. {caption: 'DAY 6', day: 'day6'},
  49. {caption: 'DAY 7', day: 'day7'},
  50. ];
  51. //$scope.todayDay = new Date().getDate() + 1;
  52. //$scope.todayMonthYear = ( new Date().getMonth() + 1 + '/' + new Date().getFullYear() );
  53. $scope.order = {
  54. day1: {
  55. breakfast: [],
  56. lunch: [],
  57. dinner: [],
  58. snacks: []
  59. },
  60. day2: {
  61. breakfast: [],
  62. lunch: [],
  63. dinner: [],
  64. snacks: []
  65. },
  66. day3: {
  67. breakfast: [],
  68. lunch: [],
  69. dinner: [],
  70. snacks: []
  71. },
  72. day4: {
  73. breakfast: [],
  74. lunch: [],
  75. dinner: [],
  76. snacks: []
  77. },
  78. day5: {
  79. breakfast: [],
  80. lunch: [],
  81. dinner: [],
  82. snacks: []
  83. },
  84. day6: {
  85. breakfast: [],
  86. lunch: [],
  87. dinner: [],
  88. snacks: []
  89. },
  90. day7: {
  91. breakfast: [],
  92. lunch: [],
  93. dinner: [],
  94. snacks: []
  95. },
  96. };
  97. $scope.sumToBacket = 0;
  98. $interval(function(){
  99. let keyDay = ['breakfast','lunch','dinner','snacks']
  100. let keyObj = Object.keys($scope.order);
  101. var sum = 0;
  102. for(let i = 0; i < keyObj.length; i++) {
  103. //console.log($scope.order[keyObj[i]]);
  104. for(let j=0; j < keyDay.length; j++) {
  105. //console.log($scope.order[keyObj[i]][keyDay[j]]);
  106. for(let l=0; l < $scope.order[keyObj[i]][keyDay[j]].length; l++) {
  107. //console.log($scope.order[keyObj[i]][keyDay[j]][l]['price']);
  108. sum += +$scope.order[keyObj[i]][keyDay[j]][l]['price'];
  109. }
  110. }
  111. }
  112. $scope.sumToBacket = sum;
  113. },500);
  114. $scope.deleteMeal = function(day, typeMeal,index) {
  115. //console.log($scope.order[day][typeMeal][index]);
  116. $scope.order[day][typeMeal].splice(index,1);
  117. }
  118. $scope.hideMe = function(day,typeMeal) {
  119. return $scope.order[day][typeMeal].length > 0
  120. };
  121. $scope.sendObject = {
  122. imageUrl: 'content/images/ready-menu/ready-menu-item/custom-menu-item.jpg',
  123. title: 'Custom menu ',
  124. weekCount: 1,
  125. order: $scope.order
  126. }
  127. $scope.firebaseShoppingCart = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('shoppingCart');
  128. $scope.firebaseShoppingCart.on('value', function(snap){
  129. $scope.shoppingCartItems = snap.val();
  130. $scope.customMenuCount = 1;
  131. angular.forEach($scope.shoppingCartItems, function(item,key){
  132. if(item.title.indexOf('Custom menu') > -1){
  133. $scope.customMenuCount++;
  134. }
  135. })
  136. })
  137. $scope.sendCustomDiet = function(){
  138. $scope.sendObject.overallPrice = $scope.sumToBacket;
  139. $scope.sendObject.title += $scope.customMenuCount;
  140. $scope.firebaseShoppingCart.push().set(angular.copy($scope.sendObject));
  141. $scope.firebaseGlobalMeals.on('value', function(snap){
  142. $scope.globalMeals = snap.val();
  143. firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('meals').set($scope.globalMeals);
  144. $window.location.assign('#!/shopping-cart');
  145. })
  146. }
  147. }]);