UNPKG

cobuild-angular-stack

Version:

Base stack angular sass jade gulp

538 lines (473 loc) 21 kB
(function (module) { 'use strict'; module .controller('AdminCoupleAccountShowController', CoupleAccountShowController) .controller('AdminModalGiftController', ModalGiftController); CoupleAccountShowController.$inject = ['$rootScope', '$scope', '$timeout', '$state', 'CoupleAccount', '$http', '$stateParams', '$uibModal', 'toastr', 'Upload', 'AdminAuth', 'CashOutInformation', '$translate', 'uiGmapGoogleMapApi', 'CustomCouple','lodash']; ModalGiftController.$inject = ['$uibModalInstance', 'msg']; function CoupleAccountShowController($rootScope, $scope, $timeout, $state, CoupleAccount, $http, $stateParams, $uibModal, toastr, Upload, Auth, CashOutInformation, $translate, GoogleMapApi, CustomCouple, lodash) { $scope.coupleAccount = CoupleAccount .findOne({ filter: { where: { url: $stateParams.url, tempAccount: {neq: true} }, include: ['cashOutInformation', 'orders','code'] } }); var originalCoupleAccount; $scope.weddingDate = null; $scope.saveDisabled = true; $scope.searchActive = false; $scope.dateOptions = { dateDisabled: false, formatYear: 'yy', minDate: new Date(), startingDay: 1 }; $scope.hstep = 1; $scope.mstep = 15; $scope.tabActive = 0; $scope.$on('$stateChangeSuccess', function (event, toState) { $scope.tabActive = toState.data.tab; }); $scope.options = { hstep: [1, 2, 3], mstep: [1, 5, 10, 15, 25, 30] }; $scope.sortBy = { fields: [ {label: 'Fecha', name: 'created'} ], order: [ {label: 'Ascendente', name: 'ASC'}, {label: 'Descendente', name: 'DESC'} ] }; $scope.sortBy.selected = { field: $scope.sortBy.fields[0].name, order: $scope.sortBy.order[1].name }; $scope.orders = []; $scope.paymentStates = [ { label: 'pending', id: 'pending' }, { label: 'plan-premium-heading', id: 'fixed_pay' }, { label: 'percent', id: 'percent' }, { label: 'Pago premium', id: 'complete_pay' } ]; var paymentStatesById = lodash.keyBy($scope.paymentStates, 'id'); $scope.paymentInfo = { status: null }; $scope.$watch('copied', function (newVal) { if (newVal) { $translate('copy-url').then(function (translation) { toastr.success(translation); }); $scope.copied = false; } }); var watch; $scope.getPayment = function() { var pay = 0; $scope.coupleAccount.payInformationData = !$scope.coupleAccount.payInformationData ? ({ paymentData: {} }) : $scope.coupleAccount.payInformationData; var products = $scope.coupleAccount.payInformationData.paymentData ? $scope.coupleAccount.payInformationData.paymentData.products : []; if(products && products.length > 0) { _.each(products, function(product) { pay += parseFloat(product.unit_price) }) } $scope.pay = pay; } $scope.orderGift = function() { CoupleAccount.setGiftListToOrder({ id: $scope.coupleAccount.id }).$promise.then(function(data) { toastr.success("Balance regalos","Success"); $state.transitionTo($state.current, {url:$scope.coupleAccount.url}, { reload: true, inherit: false, notify: true }); }, function(err) { toastr.error('No se pudo hacer el balance de regalos con las orenes notificar a desarrollo'); }) } $scope.getInformation = function() { $scope.coupleAccount = CoupleAccount .findOne({ filter: { where: { url: $stateParams.url, tempAccount: {neq: true} }, include: ['cashOutInformation', 'orders','code'] } }); $scope.coupleAccount.$promise .then(function () { if ($scope.coupleAccount.weddingData.date) { $scope.coupleAccount.weddingData.date = new Date($scope.coupleAccount.weddingData.date); } $scope.getPayment(); $scope.orders = $scope.coupleAccount.orders; originalCoupleAccount = lodash.cloneDeep($scope.coupleAccount); $scope.url = 'http://front.uniko.co/' + originalCoupleAccount.url; if (!$scope.coupleAccount.payInformationData.isPaid) { $scope.coupleAccount.payInformationData.fixedValue = $rootScope.dataUtils.payPrice.fixed; $scope.coupleAccount.payInformationData.CompleteValue = $rootScope.dataUtils.payPrice.fixedComplete; $scope.coupleAccount.payInformationData.percentageValue = $rootScope.dataUtils.payPrice.percent; $scope.paymentInfo.status = paymentStatesById['pending']; } else { $scope.coupleAccount.payInformationData.CompleteValue = $rootScope.dataUtils.payPrice.fixedComplete; $scope.paymentInfo.status = paymentStatesById[$scope.coupleAccount.payInformationData.payPerPercentage ? 'percent' : ($scope.coupleAccount.payInformationData.isPremium ? 'complete_pay' : 'fixed_pay' )] } $scope.$watch('paymentInfo.status', function (newVal, oldValue) { if (newVal !== oldValue) { $scope.coupleAccount.payInformationData.fixedValue = $rootScope.dataUtils.payPrice.fixed; $scope.coupleAccount.payInformationData.isPaid = newVal.id !== 'pending'; $scope.coupleAccount.payInformationData.payPerPercentage = newVal.id === 'percent' && newVal.id == 'complete_pay'; $scope.coupleAccount.payInformationData.isPremium = newVal.id !== 'pending' && newVal.id == 'complete_pay'; } }); $scope.$watch(function () { return originalCoupleAccount; }, function (oldVal) { $scope.giftTotal = 0; $scope.cashoutTotal = 0; lodash.forEach($scope.coupleAccount.giftList, function (gift) { $scope.giftTotal += parseFloat(gift.gift.price); }); lodash.forEach($scope.coupleAccount.cashOutInformation, function (cashout) { if (cashout.isPaid) { $scope.cashoutTotal += parseFloat(cashout.approvedAmount); } }); if ($scope.coupleAccount.weddingData.gpsaddress) { $scope.map.markers = [$scope.coupleAccount.weddingData.gpsaddress]; $scope.map.zoom = 16; $scope.map.center = { latitude: $scope.coupleAccount.weddingData.gpsaddress.latitude, longitude: $scope.coupleAccount.weddingData.gpsaddress.longitude }; } }); watch = $scope.$watch(function () { return JSON.stringify($scope.coupleAccount); }, function (oldVal, newVal) { if (oldVal !== newVal) { $scope.saveDisabled = false; } }); }); } $scope.changeStatusAccount = function () { $scope.update(); }; $scope.changeTable = function () { if (!$scope.coupleAccount.isDisabled) { $scope.update(); } else { $scope.coupleAccount.$prototype$closeTable(function () { originalCoupleAccount = lodash.cloneDeep($scope.coupleAccount); $translate('success-save-data').then(function (translation) { toastr.success(translation); }); $state.transitionTo($state.current, {url:$scope.coupleAccount.url}, { reload: true, inherit: false, notify: true }); }, function () { }); } }; $scope.updateCashout = function (cashout) { CashOutInformation .update({where: {id: cashout.id}}, cashout) .$promise .then(function () { $translate('retirement-success').then(function (translation) { toastr.success(translation); }); $state.transitionTo($state.current, {url:$scope.coupleAccount.url}, { reload: true, inherit: false, notify: true }); }) }; $scope.update = function () { $scope.saveDisabled = true; watch(); CoupleAccount.updateMainAttributes($scope.coupleAccount, function () { originalCoupleAccount = lodash.cloneDeep($scope.coupleAccount); $translate('success-save-data').then(function (translation) { toastr.success(translation); }); $state.transitionTo($state.current, {url:$scope.coupleAccount.url}, { reload: true, inherit: false, notify: true }); }, function (err) { }); }; $scope.reset = function () { $scope.coupleAccount.$resolved = false; $scope.saveDisabled = true; watch(); $timeout(function () { $scope.coupleAccount = lodash.cloneDeep(originalCoupleAccount); originalCoupleAccount.imagesList = lodash.clone(originalCoupleAccount.imagesList); $scope.coupleAccount.$resolved = true; watch = $scope.$watch(function () { return JSON.stringify($scope.coupleAccount); }, function (oldVal, newVal) { if (oldVal !== newVal) { $scope.saveDisabled = false; } }); }, 0); }; $scope.delete = function () { $scope.coupleAccount.$remove(function () { $scope.coupleAccount = null; $state.go('admin.coupleAccounts'); $translate('success-delete-article').then(function (translation) { toastr.success(translation); }); }, function () { $translate('error-delete-article').then(function (translation) { toastr.success(translation); }); }); }; $scope.exportData = function () { //ng-href= $http({ url: "http://uniko.co:3000/api/v2/coupleaccounts/" + $scope.coupleAccount.id + "/exportasxlsx?access_token=" + Auth.getAuthData().accessTokenId, method: 'get', responseType: "blob" }).then(function (data) { var a = document.createElement("a"); document.body.appendChild(a); a.style = "display: none"; var blob = data.data, url = window.URL.createObjectURL(blob); a.href = url; a.download = 'datauser.xlsx'; a.click(); window.URL.revokeObjectURL(url); $(a).remove(); }); }; $scope.exportRSVP = function () { $http({ url: 'http://uniko.co:3000/api/v2/coupleaccounts/' + $scope.coupleAccount.id + '/rsvpAsxlsx?access_token=' + Auth.getAuthData().accessTokenId, method: 'get', responseType: "blob" }).then(function (data) { var a = document.createElement("a"); document.body.appendChild(a); a.style = "display: none"; var blob = data.data, url = window.URL.createObjectURL(blob); a.href = url; a.download = 'rsvp.xlsx'; a.click(); window.URL.revokeObjectURL(url); $(a).remove(); }); }; $scope.filterOrders = {val: ''}; $scope.$watch('filterOrders.val', function (newValue, oldValue) { if (newValue !== '' || oldValue !== '') { $scope.orders = lodash.filter($scope.coupleAccount.orders, function (order) { var match = false; try { var valRegexp = new RegExp(lodash.escapeRegExp(newValue), 'i'); match = valRegexp.test(order.paymentData.payer.fullname) || valRegexp.test(order.paymentData.payer.email) || valRegexp.test(order.paymentData.message.givers.text) || valRegexp.test(order.paymentData.message.emails.text); } catch (err) { } match = match || order.id === newValue; return match; }); } }); $scope.exportDataOrders = function () { $http({ url: "http://uniko.co:3000/api/v2/orders/listasxlsx?access_token=" + Auth.getAuthData().accessTokenId, method: 'post', responseType: "blob", data: { filter: { include: 'coupleAccount', where: $scope.filterName } } }).then(function (data) { var a = document.createElement("a"); document.body.appendChild(a); a.style = "display: none"; var blob = data.data, url = window.URL.createObjectURL(blob); a.href = url; a.download = 'orderlist.xlsx'; a.click(); window.URL.revokeObjectURL(url); $(a).remove(); }); }; GoogleMapApi.then(function (maps) { maps.visualRefresh = true; $scope.defaultBounds = new google.maps.LatLngBounds( new google.maps.LatLng(40.82148, -73.66450), new google.maps.LatLng(40.66541, -74.31715)); $scope.map.bounds = { northeast: { latitude: $scope.defaultBounds.getNorthEast().lat(), longitude: $scope.defaultBounds.getNorthEast().lng() }, southwest: { latitude: $scope.defaultBounds.getSouthWest().lat(), longitude: -$scope.defaultBounds.getSouthWest().lng() } }; $scope.searchbox.options.bounds = new google.maps.LatLngBounds($scope.defaultBounds.getNorthEast(), $scope.defaultBounds.getSouthWest()); }); $scope.window = { show: false, options: { pixelOffset: {width: 0, height: -40} }, templateurl: 'window.tpl.html', templateparameter: {}, closeClick: function () { $scope.window.show = false; } }; $scope.map = { control: {}, center: { latitude: 19.4284700, longitude: -99.1276600 }, zoom: 7, dragging: false, bounds: {}, markers: [], idkey: 'place_id', events: { idle: function (map) { }, dragend: function (map) { //update the search box bounds after dragging the map var bounds = map.getBounds(); var ne = bounds.getNorthEast(); var sw = bounds.getSouthWest(); $scope.searchbox.options.bounds = new google.maps.LatLngBounds(sw, ne); //$scope.searchbox.options.visible = true; } } }; $scope.searchbox = { template: 'searchbox.tpl.html', options: { autocomplete: true, types: ['(regions)'], country: 'mx' }, events: { places_changed: function (autocomplete) { var place = autocomplete.getPlaces()[0]; if (place.address_components) { var bounds = new google.maps.LatLngBounds(); var marker = { idKey: place.place_id, place_id: place.place_id, name: place.address_components[0].long_name, latitude: place.geometry.location.lat(), longitude: place.geometry.location.lng(), templateurl: 'window.tpl.html', templateparameter: place, events: { click: function (marker) { $scope.window.templateparameter = marker.model.templateparameter; $scope.window.show = true; } } }; bounds.extend(place.geometry.location); $scope.map.center = { latitude: place.geometry.location.lat(), longitude: place.geometry.location.lng() }; $scope.map.zoom = 16; $scope.map.markers = [marker]; $scope.coupleAccount.weddingData.gpsaddress = marker; } else { console.log("do something else with the search string: " + place.name); } } } }; $scope.openModalGift = function () { var modalInstance = $uibModal.open({ animation: true, ariaLabelledBy: 'modal-title', ariaDescribedBy: 'modal-body', templateUrl: 'modalGiveGift.html', controller: 'AdminModalGiftController', controllerAs: '$AdminModal', size: 'sm', resolve: { msg: function () { return ($scope.coupleAccount.weddingData.nameP1 || 'No determinado aun') + 'y' + ($scope.coupleAccount.weddingData.nameP2 || 'No determinado aun'); } } }); modalInstance.result.then(function (value) { CustomCouple.giveAdminGift($scope.coupleAccount.id, value) .then(function (response) { if (response.data && response.data.isPaid == true) { $scope.getInformation(); toastr.success('Se registro el regalo correctamente'); } }) .catch(function (err) { toastr.error('No se pudo realizar el registro del regalo'); }); }); }; $scope.getInformation(); } function ModalGiftController($uibModalInstance, msg) { var $ctrl = this; $ctrl.value; $ctrl.msg = msg; $ctrl.ok = function () { $uibModalInstance.close($ctrl.value); }; $ctrl.cancel = function () { $uibModalInstance.dismiss('cancel'); }; } })(angular.module('uniko.admin.coupleAccounts'));