UNPKG

cobuild-angular-stack

Version:

Base stack angular sass jade gulp

148 lines (133 loc) 5.48 kB
/** * Created by garusis on 8/04/16. */ (function (module) { AdminOrdersListController.$inject = ['$scope', '$http', 'Order', 'AdminAuth', 'CustomOrderCount']; module.controller('AdminOrdersListController', AdminOrdersListController); function AdminOrdersListController($scope, $http, Order, Auth, CustomOrderCount) { $scope.curPage = 0; $scope.pageSize = 20; $scope.loader = false; $scope.pageDirect = 1; $scope.data = { filterName: '', years: [], year: String((new Date()).getFullYear()), month: String(((new Date()).getMonth() + 1) < 10 ? "0"+((new Date()).getMonth() + 1) : ((new Date()).getMonth() + 1)) }; $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 }; var totalOrders = CustomOrderCount.count(); var cast = Promise.resolve(totalOrders); $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); }; } } } cast.then(function(value) { $scope.numberOfPages = Math.ceil(value.data / $scope.pageSize); $scope.getData(); $scope.getPages(); }); $scope.getData = function(isLoad) { $scope.$watchGroup([ 'data.filterName', 'sortBy.selected.field', 'sortBy.selected.order' ], function (newValue, oldValue) { if ((newValue !== '' || oldValue !== '') && !isLoad) { var sortBy = $scope.sortBy.selected; $scope.orders = Order.customFind({ filter: { include: 'coupleAccount', order: sortBy.field + ' ' + sortBy.order, where: $scope.data.filterName, limit:$scope.pageSize, skip:$scope.curPage * $scope.pageSize } }); } }); }; $scope.getYears = function() { var date = new Date(); var finish = date.getFullYear() + 1; var start = finish - 10; $scope.data.years = _.rangeRight(start, finish); } $scope.pageDirect = 1; $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.goToPage = function(isNext){ if (isNext){ $scope.curPage = $scope.curPage + 1; }else{ $scope.curPage = $scope.curPage - 1; } $scope.getData(); } $scope.exportData = function () { var sortBy = $scope.sortBy.selected; $http({ url: "http://uniko-report.co:3000/api/v2/orders/listasxlsx?access_token=" + Auth.getAuthData().accessTokenId, method: 'post', responseType: "blob", data: { filter: { include: 'coupleAccount', order: sortBy.field + ' ' + sortBy.order, where: { "created": { gte: new Date($scope.data.year, parseInt($scope.data.month) - 1, 1), lte: new Date($scope.data.year, parseInt($scope.data.month), 0) } } } } }).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(); }); }; $scope.getYears(); } })(angular.module('uniko.admin.orders'));