UNPKG

cobuild-angular-stack

Version:

Base stack angular sass jade gulp

326 lines (291 loc) 12.1 kB
(function (module) { 'use strict'; module.controller('AdminCoupleAccountListController', CoupleAccountListController); CoupleAccountListController.$inject = ['$scope', '$rootScope', '$filter', '$state', 'CoupleAccount', 'toastr', 'AdminAuth', '$http','CustomCouplesCount']; function CoupleAccountListController($scope, $rootScope, $filter, $state, CoupleAccount, toastr, Auth, $http, CustomCouplesCount) { /* ************ init APP */ $scope.curPage = 0; $scope.pageSize = 20; $scope.loader = false; $scope.coupleAccounts = []; $scope.pageDirect = 1; $scope.filter = { name: '' }; $scope.sortBy = { fields: [ {lable: 'Fecha de creación', name: 'createdAt'}, {label: 'Fecha de Boda', name: 'weddingData.date'}, {label: 'Nombre', name: 'weddingData.nameP1'} ], order: [ {label: 'Descendente', name: 'DESC'}, {label: 'Ascendente', name: 'ASC'} ] }; $scope.sortBy.selected = { field: $scope.sortBy.fields[0].name, order: $scope.sortBy.order[0].name }; $scope.advancedSearch = { isActive: false, date: { startDate: null, endDate: null }, dateCreated: { startDate: null, endDate: null }, dateRecruiting: { startDate: null, endDate: null } } /* ************ functions APP */ $scope.applyCalendar = function() { var optionsDaterangepicker = { format : "DD-MM-YYYY", applyClass: 'btn-green', applyLabel: "Aceptar", cancelLabel: 'Cancelar', daysOfWeek: ["Do", "Lu", "Ma", "Mi", "Ju", "Vi", "Sa"], monthNames: ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"], } $scope.optionsDate = { applyClass: 'btn-inverse', locale: optionsDaterangepicker, eventHandlers: { 'cancel.daterangepicker': function (ev, picker) { $scope.advancedSearch.date = { startDate: null, endDate: null } } } }; $scope.optionsDateRecruiting = { applyClass: 'btn-inverse', locale: optionsDaterangepicker, eventHandlers: { 'cancel.daterangepicker': function (ev, picker) { $scope.advancedSearch.dateRecruiting = { startDate: null, endDate: null } } } }; $scope.optionsDateCreated = { applyClass: 'btn-inverse', locale: optionsDaterangepicker, eventHandlers: { 'cancel.daterangepicker': function (ev, picker) { $scope.advancedSearch.dateCreated = { startDate: null, endDate: null } } } }; } $scope.getPages = function() { var total = $scope.numberOfPages; var page = $scope.curPage == 0 ? 1 : $scope.curPage; $scope.pageMax = !$scope.pageMax ? 1 : $scope.pageMax; $scope.pageMin = !$scope.pageMin ? 1 : $scope.pageMin; if((page < $scope.pageMin || page > $scope.pageMax) || $scope.pageMax == 1) { $scope.pageMax = $scope.pageMax >= page ? (page + 2) : $scope.pageMax; $scope.pageMax = $scope.pageMax > total ? total : (page == (total - 1) ? (total - 1) : $scope.pageMax); $scope.arrayPages = []; if($scope.pageMax > 1 && total > 1) { $scope.pageMin = $scope.pageMax - 2; for(var pageMin = $scope.pageMax - 2;pageMin <= $scope.pageMax; pageMin ++ ) { $scope.arrayPages.push(pageMin); }; } } } $scope.onClear = function() { $state.reload(); } $scope.onSearch = function() { console.log("data =====>"); $scope.onDates(); var sortBy = $scope.sortBy.selected; if($scope.advancedSearch.type_pay > 0) { sortBy.field = 'payInformationData.payDate'; } console.log("llega =======>"); $scope.loader = true; CoupleAccount.filterAdvanced({ filter: { where: $scope.advancedSearch, limit:$scope.pageSize, skip:$scope.curPage * $scope.pageSize, order: sortBy.field + ' ' + sortBy.order } }).$promise.then($scope.response); } $scope.refresh = function () { console.log("pasa"); var val = $scope.filter.name; $scope.loader = true; var where = { nameFilter: val, }; var sortBy = $scope.sortBy.selected; console.log("=================>"); CoupleAccount.filter({ filter: { where: where, iclude: ['code'], limit:$scope.pageSize, skip:$scope.curPage * $scope.pageSize, order: sortBy.field + ' ' + sortBy.order } }).$promise.then($scope.response); }; $scope.response = function(couples) { $scope.loader = false; $scope.coupleAccounts = couples; } $scope.getPayment = function(coupleAccount) { var price = 0; if(coupleAccount.payInformationData.isPaid) { if(!coupleAccount.payInformationData.payPerPercentage && coupleAccount.payInformationData.paymentMethod != 'Manual') { _.each(coupleAccount.payInformationData.paymentData.products, function(product) { price += parseFloat(product.unit_price); }); } else if(coupleAccount.payInformationData.paymentMethod == 'Manual') { price = coupleAccount.payInformationData.fixedValue; } } return $filter('currency')(price, '$', 2); } $scope.selectFieldOrder = function (field) { $scope.sortBy.selected.field = field; }; $scope.selectOrder = function (order) { $scope.sortBy.selected.order = order; }; $scope.onDates = function() { if($scope.advancedSearch.date.startDate && $scope.advancedSearch.date.endDate) { $scope.advancedSearch.startDate = $scope.advancedSearch.date.startDate.format("YYYY-MM-DD"); $scope.advancedSearch.endDate = $scope.advancedSearch.date.endDate.add(1, 'day').format("YYYY-MM-DD"); } else { $scope.advancedSearch.date = { startDate: null, endDate: null } delete $scope.advancedSearch.startDate; delete $scope.advancedSearch.endDate; } if($scope.advancedSearch.dateCreated.startDate && $scope.advancedSearch.dateCreated.endDate) { $scope.advancedSearch.startDateCreated = $scope.advancedSearch.dateCreated.startDate.format("YYYY-MM-DD"); $scope.advancedSearch.endDateCreated = $scope.advancedSearch.dateCreated.endDate.add(1, 'day').format("YYYY-MM-DD"); } else { $scope.advancedSearch.dateCreated = { startDate: null, endDate: null } delete $scope.advancedSearch.startDateCreated; delete $scope.advancedSearch.endDateCreated; } if($scope.advancedSearch.dateRecruiting.startDate && $scope.advancedSearch.dateRecruiting.endDate) { $scope.advancedSearch.startDateRecruiting = $scope.advancedSearch.dateRecruiting.startDate.format("YYYY-MM-DD"); $scope.advancedSearch.endDateRecruiting = $scope.advancedSearch.dateRecruiting.endDate.add(1, 'day').format("YYYY-MM-DD"); } else { $scope.advancedSearch.dateRecruiting = { startDate: null, endDate: null } delete $scope.advancedSearch.startDateRecruiting; delete $scope.advancedSearch.endDateRecruiting; } } $scope.exportData = function () { $scope.onDates(); var sortBy = $scope.sortBy.selected; $http({ url: "http://uniko-report.co:3000/api/v2/coupleAccounts/listasxlsx?access_token=" + Auth.getAuthData().accessTokenId, method: 'post', responseType: "blob", data: { "filter": { "where": $scope.advancedSearch, "order": sortBy.field + ' ' + sortBy.order } }, }).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 = 'coupleslist.xlsx'; a.click(); window.URL.revokeObjectURL(url); $(a).remove(); }); }; $scope.getData = function(){ console.log("get data"); $scope .$watchGroup([ 'filterName' ], $scope.refresh); $scope.getPages(); }; $scope.getCount = function() { console.log("llega"); var totalOrders = CustomCouplesCount.count(); var cast = Promise.resolve(totalOrders); cast.then(function(value) { $scope.numberOfPages = Math.ceil(value.data.count / $scope.pageSize); $scope.getData(); }); }; $scope.goToPagedirect = function(page){ console.log('$scope.pageDirect = ',$scope.pageDirect); if (!page){ $scope.pageDirect = 1; } else { $scope.pageDirect = page; $scope.curPage = page; $scope.getData(); } } $scope.deleteCouple = function(id) { var first = parseInt(Math.random() * (12 - 1) + 1); var second = parseInt(Math.random() * (12 - 1) + 1); var result = prompt("¿Desea eliminar a la pareja (resultado de "+first+" * "+second+")?"); if(result == (first * second)) { CoupleAccount.deleteById({id:id}, function(err, data) { $scope.onSearch(); }); } } $scope.goToPage = function(isNext){ if (isNext){ $scope.curPage = $scope.curPage + 1; }else{ $scope.curPage = $scope.curPage - 1; } $scope.getData(); } $scope.create = function () { $state.go('admin.coupleAccounts.new'); }; /* ************ start APP */ $scope.applyCalendar(); $scope.getCount(); } })(angular.module('uniko.admin.coupleAccounts'));