Commit c05675f6ce6bf5d487905cae04753e1088934152

Authored by Eric Fernandez
1 parent da62c0db26
Exists in master

agrego todo

Showing 1 changed file with 1 additions and 0 deletions   Show diff stats
src/js/controller.js
1 angular.module('focaModalNotaPedido') 1 angular.module('focaModalNotaPedido')
2 .controller('focaModalNotaPedidoController', 2 .controller('focaModalNotaPedidoController',
3 [ 3 [
4 '$filter', 4 '$filter',
5 '$scope', 5 '$scope',
6 '$uibModalInstance', 6 '$uibModalInstance',
7 'focaModalNotaPedidoService', 7 'focaModalNotaPedidoService',
8 'usadoPor', 8 'usadoPor',
9 function($filter, $scope, $uibModalInstance, 9 function($filter, $scope, $uibModalInstance,
10 focaModalNotaPedidoService, usadoPor 10 focaModalNotaPedidoService, usadoPor
11 ) { 11 ) {
12 var fecha = new Date(); 12 var fecha = new Date();
13 $scope.fechaHasta = new Date(); 13 $scope.fechaHasta = new Date();
14 $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); 14 $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1));
15 $scope.filters = ''; 15 $scope.filters = '';
16 $scope.notasPedido = []; 16 $scope.notasPedido = [];
17 $scope.primerBusqueda = false; 17 $scope.primerBusqueda = false;
18 $scope.searchLoading = false; 18 $scope.searchLoading = false;
19 // pagination 19 // pagination
20 $scope.numPerPage = 10; 20 $scope.numPerPage = 10;
21 $scope.currentPage = 1; 21 $scope.currentPage = 1;
22 $scope.filteredNotasPedido = []; 22 $scope.filteredNotasPedido = [];
23 $scope.currentPageNotasPedido = []; 23 $scope.currentPageNotasPedido = [];
24 $scope.selectedNotaPedido = -1; 24 $scope.selectedNotaPedido = -1;
25 25
26 //METODOS 26 //METODOS
27 $scope.busquedaPress = function(key) { 27 $scope.busquedaPress = function(key) {
28 if (key === 13) { 28 if (key === 13) {
29 //TODO Validaciones con alertas 29 //TODO Validaciones con alertas
30 if(!$scope.fechaDesde) { 30 if(!$scope.fechaDesde) {
31 alert('INGRESE FECHA DESDE'); 31 alert('INGRESE FECHA DESDE');
32 return; 32 return;
33 } 33 }
34 if(!$scope.fechaHasta) { 34 if(!$scope.fechaHasta) {
35 alert('INGRESE FECHA HASTA'); 35 alert('INGRESE FECHA HASTA');
36 return; 36 return;
37 } 37 }
38 if($scope.fechaDesde > $scope.fechaHasta) { 38 if($scope.fechaDesde > $scope.fechaHasta) {
39 alert('La fecha desde no puede ser mayor a la fecha hasta'); 39 alert('La fecha desde no puede ser mayor a la fecha hasta');
40 return; 40 return;
41 } 41 }
42 $scope.searchLoading = true; 42 $scope.searchLoading = true;
43 //TODO hacer filtro de fecha
43 focaModalNotaPedidoService 44 focaModalNotaPedidoService
44 .getNotasPedido(usadoPor, $scope.fechaDesde.toISOString().split('.')[0], 45 .getNotasPedido(usadoPor, $scope.fechaDesde.toISOString().split('.')[0],
45 $scope.fechaHasta.toISOString().split('.')[0]) 46 $scope.fechaHasta.toISOString().split('.')[0])
46 .then(llenarDatos); 47 .then(llenarDatos);
47 } 48 }
48 }; 49 };
49 function llenarDatos(res) { 50 function llenarDatos(res) {
50 $scope.notasPedido = []; 51 $scope.notasPedido = [];
51 $scope.filteredNotasPedido = []; 52 $scope.filteredNotasPedido = [];
52 $scope.currentPageNotasPedido = []; 53 $scope.currentPageNotasPedido = [];
53 $scope.selectedNotaPedido = -1; 54 $scope.selectedNotaPedido = -1;
54 $scope.searchLoading = false; 55 $scope.searchLoading = false;
55 $scope.primerBusqueda = true; 56 $scope.primerBusqueda = true;
56 $scope.notasPedido = res.data; 57 $scope.notasPedido = res.data;
57 $scope.search(); 58 $scope.search();
58 primera(); 59 primera();
59 } 60 }
60 $scope.search = function() { 61 $scope.search = function() {
61 if($scope.notasPedido.length > 0) { 62 if($scope.notasPedido.length > 0) {
62 $scope.filteredNotasPedido = $filter('filter')( 63 $scope.filteredNotasPedido = $filter('filter')(
63 $scope.notasPedido, 64 $scope.notasPedido,
64 {$: $scope.filters} 65 {$: $scope.filters}
65 ); 66 );
66 67
67 $scope.lastPage = Math.ceil( 68 $scope.lastPage = Math.ceil(
68 $scope.filteredNotasPedido.length / $scope.numPerPage 69 $scope.filteredNotasPedido.length / $scope.numPerPage
69 ); 70 );
70 71
71 $scope.resetPage(); 72 $scope.resetPage();
72 } 73 }
73 }; 74 };
74 75
75 $scope.resetPage = function() { 76 $scope.resetPage = function() {
76 $scope.currentPage = 1; 77 $scope.currentPage = 1;
77 $scope.selectPage(1); 78 $scope.selectPage(1);
78 }; 79 };
79 80
80 $scope.selectPage = function(page) { 81 $scope.selectPage = function(page) {
81 var start = (page - 1) * $scope.numPerPage; 82 var start = (page - 1) * $scope.numPerPage;
82 var end = start + $scope.numPerPage; 83 var end = start + $scope.numPerPage;
83 $scope.paginas = []; 84 $scope.paginas = [];
84 $scope.paginas = calcularPages(page); 85 $scope.paginas = calcularPages(page);
85 $scope.currentPageNotasPedido = $scope.filteredNotasPedido.slice(start, end); 86 $scope.currentPageNotasPedido = $scope.filteredNotasPedido.slice(start, end);
86 $scope.currentPage = page; 87 $scope.currentPage = page;
87 }; 88 };
88 89
89 $scope.select = function(notaPedido) { 90 $scope.select = function(notaPedido) {
90 $uibModalInstance.close(notaPedido); 91 $uibModalInstance.close(notaPedido);
91 }; 92 };
92 93
93 $scope.cancel = function() { 94 $scope.cancel = function() {
94 $uibModalInstance.dismiss('cancel'); 95 $uibModalInstance.dismiss('cancel');
95 }; 96 };
96 97
97 $scope.busquedaDown = function(key) { 98 $scope.busquedaDown = function(key) {
98 if (key === 40) { 99 if (key === 40) {
99 primera(key); 100 primera(key);
100 } 101 }
101 }; 102 };
102 103
103 $scope.itemNotaPedido = function(key) { 104 $scope.itemNotaPedido = function(key) {
104 if (key === 38) { 105 if (key === 38) {
105 anterior(key); 106 anterior(key);
106 } 107 }
107 108
108 if (key === 40) { 109 if (key === 40) {
109 siguiente(key); 110 siguiente(key);
110 } 111 }
111 112
112 if (key === 37) { 113 if (key === 37) {
113 retrocederPagina(); 114 retrocederPagina();
114 } 115 }
115 116
116 if (key === 39) { 117 if (key === 39) {
117 avanzarPagina(); 118 avanzarPagina();
118 } 119 }
119 }; 120 };
120 121
121 function calcularPages(paginaActual) { 122 function calcularPages(paginaActual) {
122 var paginas = []; 123 var paginas = [];
123 paginas.push(paginaActual); 124 paginas.push(paginaActual);
124 125
125 if (paginaActual - 1 > 1) { 126 if (paginaActual - 1 > 1) {
126 127
127 paginas.unshift(paginaActual - 1); 128 paginas.unshift(paginaActual - 1);
128 if (paginaActual - 2 > 1) { 129 if (paginaActual - 2 > 1) {
129 paginas.unshift(paginaActual - 2); 130 paginas.unshift(paginaActual - 2);
130 } 131 }
131 } 132 }
132 133
133 if (paginaActual + 1 < $scope.lastPage) { 134 if (paginaActual + 1 < $scope.lastPage) {
134 paginas.push(paginaActual + 1); 135 paginas.push(paginaActual + 1);
135 if (paginaActual + 2 < $scope.lastPage) { 136 if (paginaActual + 2 < $scope.lastPage) {
136 paginas.push(paginaActual + 2); 137 paginas.push(paginaActual + 2);
137 } 138 }
138 } 139 }
139 140
140 if (paginaActual !== 1) { 141 if (paginaActual !== 1) {
141 paginas.unshift(1); 142 paginas.unshift(1);
142 } 143 }
143 144
144 if (paginaActual !== $scope.lastPage) { 145 if (paginaActual !== $scope.lastPage) {
145 paginas.push($scope.lastPage); 146 paginas.push($scope.lastPage);
146 } 147 }
147 148
148 return paginas; 149 return paginas;
149 } 150 }
150 151
151 function primera() { 152 function primera() {
152 $scope.selectedNotaPedido = 0; 153 $scope.selectedNotaPedido = 0;
153 } 154 }
154 155
155 function anterior() { 156 function anterior() {
156 if ($scope.selectedNotaPedido === 0 && $scope.currentPage > 1) { 157 if ($scope.selectedNotaPedido === 0 && $scope.currentPage > 1) {
157 retrocederPagina(); 158 retrocederPagina();
158 } else { 159 } else {
159 $scope.selectedNotaPedido--; 160 $scope.selectedNotaPedido--;
160 } 161 }
161 } 162 }
162 163
163 function siguiente() { 164 function siguiente() {
164 if ($scope.selectedNotaPedido < $scope.currentPageNotasPedido.length - 1 ) { 165 if ($scope.selectedNotaPedido < $scope.currentPageNotasPedido.length - 1 ) {
165 $scope.selectedNotaPedido++; 166 $scope.selectedNotaPedido++;
166 } else { 167 } else {
167 avanzarPagina(); 168 avanzarPagina();
168 } 169 }
169 } 170 }
170 171
171 function retrocederPagina() { 172 function retrocederPagina() {
172 if ($scope.currentPage > 1) { 173 if ($scope.currentPage > 1) {
173 $scope.selectPage($scope.currentPage - 1); 174 $scope.selectPage($scope.currentPage - 1);
174 $scope.selectedNotaPedido = $scope.numPerPage - 1; 175 $scope.selectedNotaPedido = $scope.numPerPage - 1;
175 } 176 }
176 } 177 }
177 178
178 function avanzarPagina() { 179 function avanzarPagina() {
179 if ($scope.currentPage < $scope.lastPage) { 180 if ($scope.currentPage < $scope.lastPage) {
180 $scope.selectPage($scope.currentPage + 1); 181 $scope.selectPage($scope.currentPage + 1);
181 $scope.selectedNotaPedido = 0; 182 $scope.selectedNotaPedido = 0;
182 } 183 }
183 } 184 }
184 } 185 }
185 ] 186 ]
186 ); 187 );
187 188