/** * Rooms controller * NOTE: Set $scope.scenarioId before! */ app.controller('RoomsCtrl', function($scope, $location, $http, $filter) { var allAttendees = []; var availableAttendees = { 'default': [], }; $scope.arrAddRoom = {}; $scope.init = function(){ var req = { method: 'GET', url: '/scenario/getWith/' + $scope.scenarioId, } $http(req).then(function(response) { if (response.status && response.data.status) { $scope.rooms = response.data.data[0].hotel.rooms; angular.forEach($scope.rooms, function(room){ angular.forEach(room.beds, function(bed){ angular.forEach(bed.attendees, function(attendee, key){ if(attendee.scenario_attendee_id != 0){ bed.attendees[key] = parseInt(attendee.scenario_attendee_id); }else{ bed.attendees[key] = 0; } }); }); }); $scope.attendees = response.data.data[0].attendees; angular.forEach($scope.attendees, function(attendee){ attendee.scenario_attendee_id = parseInt(attendee.scenario_attendee_id); attendee.name = attendee.passport_name_first + ' ' + attendee.passport_name_last; attendee.type = attendee.attendee_type_id; }); allAttendees = angular.copy($scope.attendees); availableAttendees['default'] = angular.copy($scope.attendees); $scope.calculateAvailableAttendees(); if (response.data.data[0] && response.data.data[0].from && response.data.data[0].lastChangeDate) { $scope.fromDate = response.data.data[0].from; $scope.today = response.data.data[0].today; $scope.lastChangeDate = response.data.data[0].lastChangeDate; } } }); var req = { method: 'GET', url: '/attendee_type/get', } $http(req).then(function(response) { if (response.status && response.data.status) { $scope.attendee_types = response.data.data; } }); } $scope.init(); /*------------[ROOMS]-------------*/ $scope.addRoom = function(arrAddRoom){ if($scope.scenario && $scope.scenario.hotel.id && arrAddRoom && !isNaN(arrAddRoom.singles) && !isNaN(arrAddRoom.doubles) && (arrAddRoom.singles > 0 || arrAddRoom.doubles > 0)){ var req = { method: 'POST', url: '/scenario_hotel_room/insert', data: { scenario_hotel_id: $scope.scenario.hotel.id, name: arrAddRoom.name, surcharge: arrAddRoom.surcharge, amount_of_singles: arrAddRoom.singles, amount_of_doubles: arrAddRoom.doubles, } } $http(req).then(function(response) { if (response.status && response.data.status) { swal("Succes!", "De kamer is toegevoegd", "success"); $scope.init(); } }); } } $scope.onSelectedRoomTypeChange = function(selectedRoomType) { if (!selectedRoomType) { $scope.arrAddRoom = { singles: 0, doubles: 0, } return; } var surcharge = 0.0; // set different key when sales priceKey = 'price_surcharge_sales'; // check required data if (selectedRoomType[priceKey] !== undefined && selectedRoomType[priceKey] != 0) { if (selectedRoomType['price_surcharge_per'] !== undefined && selectedRoomType['price_surcharge_per'] === 'PERSON') { surcharge = (parseInt(selectedRoomType['amount_of_singles']) + (parseInt(selectedRoomType['amount_of_doubles']) * 2)) * parseFloat(selectedRoomType[priceKey]); } else if (selectedRoomType['price_surcharge_per'] !== undefined && selectedRoomType['price_surcharge_per'] === 'ROOM') { surcharge = parseFloat(selectedRoomType[priceKey]); } } $scope.arrAddRoom = { name: selectedRoomType.name, singles: parseInt(selectedRoomType.amount_of_singles), doubles: parseInt(selectedRoomType.amount_of_doubles), surcharge: parseFloat(surcharge * $scope.numOfNights), } } $scope.removeRoom = function(room){ swal({ title: "Kamer verwijderen", text: "Weet je zeker dat je de kamer wilt verwijderen?", icon: "warning", buttons: { cancel: "Nee", confirm: "Ja" }, dangerMode: true, }).then((willDelete) => { if (willDelete) { if(room && room.id){ var req = { method: 'GET', url: '/scenario_hotel_room/delete/' + room.id, } $http(req).then(function(response) { if (response.status && response.data.status) { swal("Succes!", "De kamer is verwijderd", "success"); $scope.init(); } }); } } }); } $scope.$on('getAvailableAttendees', function(e) { $scope.$emit('pingBack', $scope.getAvailableAttendeeList()); }); $scope.$on('init', function(e) { $scope.$emit('pingBack', $scope.init()); }); $scope.getAvailableAttendeeList = function() { return availableAttendees['default']; } //array from numberOf $scope.getNumber = function(num) { return new Array(parseInt(num)); } $scope.getLabel = function(attendee){ if (attendee !== undefined) { angular.forEach($scope.attendee_types, function(type){ if(attendee.type == type.id){ attendee.type = type.name; } }); if (attendee.type){ return attendee.name + " (" + attendee.type + ")"; }else{ return attendee.name; } } } $scope.getAvailableAttendees = function(attendeeId, bedId){ if(!angular.isNumber(attendeeId)){ return availableAttendees['default']; }else{ searchAttendee = $filter('filter')(allAttendees, {'scenario_attendee_id': parseInt(attendeeId)}, true); var newAvailableAttendeeList = angular.copy(availableAttendees['default']); newAvailableAttendeeList.push(searchAttendee[0]); if (availableAttendees[bedId] === undefined || availableAttendees[bedId].length != newAvailableAttendeeList.length) { availableAttendees[bedId] = newAvailableAttendeeList; } return availableAttendees[bedId]; } } $scope.calculateAvailableAttendees = function(newAttendee, oldAttendee, currentBed) { if (!isNaN(oldAttendee)) { searchAttendee = $filter('filter')(allAttendees, {'scenario_attendee_id': parseInt(oldAttendee)}, true); if (searchAttendee && searchAttendee[0]) { availableAttendees['default'].push(searchAttendee[0]); availableAttendees = { 'default': availableAttendees['default'], } } } if(currentBed === undefined){ angular.forEach($scope.rooms, function(room){ angular.forEach(room.beds, function(bed){ angular.forEach(bed.attendees, function(attendee){ if(angular.isNumber(attendee)){ searchAttendee = $filter('filter')(availableAttendees['default'], {'scenario_attendee_id': attendee}, true); if(searchAttendee && searchAttendee[0]){ availableAttendees['default'].splice(availableAttendees['default'].indexOf(searchAttendee[0]), 1); } } }); }); }); }else{ angular.forEach(currentBed.attendees, function(attendee){ if(angular.isNumber(attendee)){ searchAttendee = $filter('filter')(availableAttendees['default'], {'scenario_attendee_id': attendee}, true); if(searchAttendee && searchAttendee[0]){ availableAttendees['default'].splice(availableAttendees['default'].indexOf(searchAttendee[0]), 1); } } }); } } $scope.saveRoomParticipants = function(){ if ($scope.today && $scope.lastChangeDate && ($scope.today >= $scope.lastChangeDate)) { swal({ title: "Kamerlijst wijzigen", text: "Weet je zeker dat je de kamerlijst wilt wijzigen? \n Er zullen nu kosten voor worden gerekend.", icon: "warning", buttons: { cancel: "Nee", confirm: "Ja" }, dangerMode: true, }).then((willChange) => { if (willChange) { $scope.saveRoomBedParticipants(); } }); } else { $scope.saveRoomBedParticipants(); } } $scope.saveRoomBedParticipants = function() { var sendData = {'data': $scope.rooms}; var req = { method: 'POST', url: '/scenario_hotel_room_bed_attendee/insertOrUpdate', data: sendData, } $http(req).then(function(response) { if (response.status && response.data.status) { swal('Succes','De deelnemers zijn succesvol aan de kamers toegevoegd','success'); } else { swal('Foutmelding','De deelnemers konden niet aan de kamers worden toegevoegd, probeer het opnieuw!','error'); } }); } });