Commit 21f95418c3b9e4c5acbfa2dfc9559f572214c579

Authored by Eric Fernandez
Exists in master

Merge branch 'master' into 'master'

Master(efernandez)

See merge request !2
src/js/controller.js
1 angular.module('focaModalFactura') 1 angular.module('focaModalFactura')
2 .controller('focaModalFacturaController', 2 .controller('focaModalFacturaController',
3 [ 3 [
4 '$filter', 4 '$filter',
5 '$scope', 5 '$scope',
6 '$uibModalInstance', 6 '$uibModalInstance',
7 'focaModalFacturaService', 7 'focaModalFacturaService',
8 'cliente', 8 'cliente',
9 function($filter, $scope, $uibModalInstance, 9 function($filter, $scope, $uibModalInstance,
10 focaModalFacturaService, cliente 10 focaModalFacturaService, cliente
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.facturas = []; 16 $scope.facturas = [];
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.filteredFacturas = []; 22 $scope.filteredFacturas = [];
23 $scope.currentPageFacturas = []; 23 $scope.currentPageFacturas = [];
24 $scope.selectedFactura = -1; 24 $scope.selectedFactura = -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 //TODO hacer filtro de fecha
44 focaModalFacturaService 44 focaModalFacturaService
45 .getFacturasByIdCliente(cliente, $scope.fechaDesde 45 .getFacturasByIdCliente(cliente, $scope.fechaDesde
46 .toISOString().split('.')[0], 46 .toISOString().split('.')[0],
47 $scope.fechaHasta.toISOString().split('.')[0]) 47 $scope.fechaHasta.toISOString().split('.')[0])
48 .then(llenarDatos); 48 .then(llenarDatos);
49 } 49 }
50 }; 50 };
51 51
52 $scope.aceptar = function() { 52 $scope.aceptar = function() {
53 var result = $scope.facturas.filter(function(a) {return a.checked === true;}); 53 var result = $scope.facturas.filter(function(a) {return a.checked === true;});
54 $uibModalInstance.close(result); 54 $uibModalInstance.close(result);
55 }; 55 };
56 56
57 $scope.tieneAlMenosUno = function() { 57 $scope.tieneAlMenosUno = function() {
58 var result = $scope.facturas.filter(function(a) {return a.checked === true;}); 58 var result = $scope.facturas.filter(function(a) {return a.checked === true;});
59 return result.length === 0 ? true : false; 59 return !result.length;
60 }; 60 };
61 61
62 function llenarDatos(res) { 62 function llenarDatos(res) {
63 $scope.facturas = []; 63 $scope.facturas = [];
64 $scope.filteredFacturas = []; 64 $scope.filteredFacturas = [];
65 $scope.currentPageFacturas = []; 65 $scope.currentPageFacturas = [];
66 $scope.selectedFactura = -1; 66 $scope.selectedFactura = -1;
67 $scope.searchLoading = false; 67 $scope.searchLoading = false;
68 $scope.primerBusqueda = true; 68 $scope.primerBusqueda = true;
69 $scope.facturas = res.data; 69 $scope.facturas = res.data;
70 $scope.search(); 70 $scope.search();
71 primera(); 71 primera();
72 } 72 }
73 $scope.search = function() { 73 $scope.search = function() {
74 if($scope.facturas.length > 0) { 74 if($scope.facturas.length > 0) {
75 $scope.filteredFacturas = $filter('filter')( 75 $scope.filteredFacturas = $filter('filter')(
76 $scope.facturas, 76 $scope.facturas,
77 {$: $scope.filters} 77 {$: $scope.filters}
78 ); 78 );
79 79
80 $scope.lastPage = Math.ceil( 80 $scope.lastPage = Math.ceil(
81 $scope.filteredFacturas.length / $scope.numPerPage 81 $scope.filteredFacturas.length / $scope.numPerPage
82 ); 82 );
83 83
84 $scope.resetPage(); 84 $scope.resetPage();
85 } 85 }
86 }; 86 };
87 87
88 $scope.resetPage = function() { 88 $scope.resetPage = function() {
89 $scope.currentPage = 1; 89 $scope.currentPage = 1;
90 $scope.selectPage(1); 90 $scope.selectPage(1);
91 }; 91 };
92 92
93 $scope.selectPage = function(page) { 93 $scope.selectPage = function(page) {
94 var start = (page - 1) * $scope.numPerPage; 94 var start = (page - 1) * $scope.numPerPage;
95 var end = start + $scope.numPerPage; 95 var end = start + $scope.numPerPage;
96 $scope.paginas = []; 96 $scope.paginas = [];
97 $scope.paginas = calcularPages(page); 97 $scope.paginas = calcularPages(page);
98 $scope.currentPageFacturas = $scope.filteredFacturas.slice(start, end); 98 $scope.currentPageFacturas = $scope.filteredFacturas.slice(start, end);
99 $scope.currentPage = page; 99 $scope.currentPage = page;
100 }; 100 };
101 101
102 $scope.cancel = function() { 102 $scope.cancel = function() {
103 $uibModalInstance.dismiss('cancel'); 103 $uibModalInstance.dismiss('cancel');
104 }; 104 };
105 105
106 $scope.busquedaDown = function(key) { 106 $scope.busquedaDown = function(key) {
107 if (key === 40) { 107 if (key === 40) {
108 primera(key); 108 primera(key);
109 } 109 }
110 }; 110 };
111 111
112 $scope.itemFactura = function(key) { 112 $scope.itemFactura = function(key) {
113 if (key === 38) { 113 if (key === 38) {
114 anterior(key); 114 anterior(key);
115 } 115 }
116 116
117 if (key === 40) { 117 if (key === 40) {
118 siguiente(key); 118 siguiente(key);
119 } 119 }
120 120
121 if (key === 37) { 121 if (key === 37) {
122 retrocederPagina(); 122 retrocederPagina();
123 } 123 }
124 124
125 if (key === 39) { 125 if (key === 39) {
126 avanzarPagina(); 126 avanzarPagina();
127 } 127 }
128 }; 128 };
129 129
130 function calcularPages(paginaActual) { 130 function calcularPages(paginaActual) {
131 var paginas = []; 131 var paginas = [];
132 paginas.push(paginaActual); 132 paginas.push(paginaActual);
133 133
134 if (paginaActual - 1 > 1) { 134 if (paginaActual - 1 > 1) {
135 135
136 paginas.unshift(paginaActual - 1); 136 paginas.unshift(paginaActual - 1);
137 if (paginaActual - 2 > 1) { 137 if (paginaActual - 2 > 1) {
138 paginas.unshift(paginaActual - 2); 138 paginas.unshift(paginaActual - 2);
139 } 139 }
140 } 140 }
141 141
142 if (paginaActual + 1 < $scope.lastPage) { 142 if (paginaActual + 1 < $scope.lastPage) {
143 paginas.push(paginaActual + 1); 143 paginas.push(paginaActual + 1);
144 if (paginaActual + 2 < $scope.lastPage) { 144 if (paginaActual + 2 < $scope.lastPage) {
145 paginas.push(paginaActual + 2); 145 paginas.push(paginaActual + 2);
146 } 146 }
147 } 147 }
148 148
149 if (paginaActual !== 1) { 149 if (paginaActual !== 1) {
150 paginas.unshift(1); 150 paginas.unshift(1);
151 } 151 }
152 152
153 if (paginaActual !== $scope.lastPage) { 153 if (paginaActual !== $scope.lastPage) {
154 paginas.push($scope.lastPage); 154 paginas.push($scope.lastPage);
155 } 155 }
156 156
157 return paginas; 157 return paginas;
158 } 158 }
159 159
160 function primera() { 160 function primera() {
161 $scope.selectedFactura = 0; 161 $scope.selectedFactura = 0;
162 } 162 }
163 163
164 function anterior() { 164 function anterior() {
165 if ($scope.selectedFactura === 0 && $scope.currentPage > 1) { 165 if ($scope.selectedFactura === 0 && $scope.currentPage > 1) {
166 retrocederPagina(); 166 retrocederPagina();
167 } else { 167 } else {
168 $scope.selectedFactura--; 168 $scope.selectedFactura--;
169 } 169 }
170 } 170 }
171 171
172 function siguiente() { 172 function siguiente() {
173 if ($scope.selectedFactura < $scope.currentPageFacturas.length - 1 ) { 173 if ($scope.selectedFactura < $scope.currentPageFacturas.length - 1 ) {
174 $scope.selectedFactura++; 174 $scope.selectedFactura++;
175 } else { 175 } else {
176 avanzarPagina(); 176 avanzarPagina();
177 } 177 }
178 } 178 }
179 179
180 function retrocederPagina() { 180 function retrocederPagina() {
181 if ($scope.currentPage > 1) { 181 if ($scope.currentPage > 1) {
182 $scope.selectPage($scope.currentPage - 1); 182 $scope.selectPage($scope.currentPage - 1);
183 $scope.selectedFactura = $scope.numPerPage - 1; 183 $scope.selectedFactura = $scope.numPerPage - 1;
184 } 184 }
185 } 185 }
186 186
187 function avanzarPagina() { 187 function avanzarPagina() {
188 if ($scope.currentPage < $scope.lastPage) { 188 if ($scope.currentPage < $scope.lastPage) {
189 $scope.selectPage($scope.currentPage + 1); 189 $scope.selectPage($scope.currentPage + 1);
190 $scope.selectedFactura = 0; 190 $scope.selectedFactura = 0;
191 } 191 }
192 } 192 }
193 } 193 }
194 ] 194 ]
195 ); 195 );
196 196