Commit 6304aedb3fa9f1fca0789a252dfd2548651347b5
1 parent
11408cb3fe
Exists in
master
and in
1 other branch
fuera ToDo
Showing
1 changed file
with
0 additions
and
1 deletions
Show diff stats
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 | 'parametrosFactura', | 8 | 'parametrosFactura', |
9 | '$uibModal', | 9 | '$uibModal', |
10 | 'focaModalService', | 10 | 'focaModalService', |
11 | function($filter, $scope, $uibModalInstance, | 11 | function($filter, $scope, $uibModalInstance, |
12 | focaModalFacturaService, parametrosFactura, $uibModal, focaModalService | 12 | focaModalFacturaService, parametrosFactura, $uibModal, focaModalService |
13 | ) { | 13 | ) { |
14 | var fecha = new Date(); | 14 | var fecha = new Date(); |
15 | $scope.fechaHasta = new Date(); | 15 | $scope.fechaHasta = new Date(); |
16 | $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); | 16 | $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); |
17 | $scope.filters = ''; | 17 | $scope.filters = ''; |
18 | $scope.facturas = []; | 18 | $scope.facturas = []; |
19 | $scope.primerBusqueda = false; | 19 | $scope.primerBusqueda = false; |
20 | $scope.searchLoading = false; | 20 | $scope.searchLoading = false; |
21 | $scope.parametrosFactura = parametrosFactura; | 21 | $scope.parametrosFactura = parametrosFactura; |
22 | // pagination | 22 | // pagination |
23 | $scope.numPerPage = 10; | 23 | $scope.numPerPage = 10; |
24 | $scope.currentPage = 1; | 24 | $scope.currentPage = 1; |
25 | $scope.filteredFacturas = []; | 25 | $scope.filteredFacturas = []; |
26 | $scope.currentPageFacturas = []; | 26 | $scope.currentPageFacturas = []; |
27 | $scope.selectedFactura = -1; | 27 | $scope.selectedFactura = -1; |
28 | 28 | ||
29 | //METODOS | 29 | //METODOS |
30 | $scope.busquedaPress = function(key) { | 30 | $scope.busquedaPress = function(key) { |
31 | if (key === 13) { | 31 | if (key === 13) { |
32 | //TODO Validaciones con alertas | ||
33 | if(!$scope.fechaDesde) { | 32 | if(!$scope.fechaDesde) { |
34 | focaModalService.alert('INGRESE FECHA DESDE'); | 33 | focaModalService.alert('INGRESE FECHA DESDE'); |
35 | return; | 34 | return; |
36 | } | 35 | } |
37 | if(!$scope.fechaHasta) { | 36 | if(!$scope.fechaHasta) { |
38 | focaModalService.alert('INGRESE FECHA HASTA'); | 37 | focaModalService.alert('INGRESE FECHA HASTA'); |
39 | return; | 38 | return; |
40 | } | 39 | } |
41 | if($scope.fechaDesde > $scope.fechaHasta) { | 40 | if($scope.fechaDesde > $scope.fechaHasta) { |
42 | focaModalService | 41 | focaModalService |
43 | .alert('La fecha desde no puede ser mayor a la fecha hasta'); | 42 | .alert('La fecha desde no puede ser mayor a la fecha hasta'); |
44 | return; | 43 | return; |
45 | } | 44 | } |
46 | $scope.searchLoading = true; | 45 | $scope.searchLoading = true; |
47 | //TODO hacer filtro de fecha | 46 | //TODO hacer filtro de fecha |
48 | focaModalFacturaService | 47 | focaModalFacturaService |
49 | .getFacturasByIdCliente( | 48 | .getFacturasByIdCliente( |
50 | parametrosFactura.cliente.COD, | 49 | parametrosFactura.cliente.COD, |
51 | parametrosFactura.moneda, | 50 | parametrosFactura.moneda, |
52 | '2001-01-01', | 51 | '2001-01-01', |
53 | '2100-12-31' | 52 | '2100-12-31' |
54 | ) | 53 | ) |
55 | .then(llenarDatos); | 54 | .then(llenarDatos); |
56 | } | 55 | } |
57 | }; | 56 | }; |
58 | 57 | ||
59 | $scope.aceptar = function() { | 58 | $scope.aceptar = function() { |
60 | var result = $scope.facturas.filter(function(a) {return a.checked === true;}); | 59 | var result = $scope.facturas.filter(function(a) {return a.checked === true;}); |
61 | $uibModalInstance.close(result); | 60 | $uibModalInstance.close(result); |
62 | }; | 61 | }; |
63 | 62 | ||
64 | $scope.tieneAlMenosUno = function() { | 63 | $scope.tieneAlMenosUno = function() { |
65 | var result = $scope.facturas.filter(function(a) {return a.checked === true;}); | 64 | var result = $scope.facturas.filter(function(a) {return a.checked === true;}); |
66 | return !result.length; | 65 | return !result.length; |
67 | }; | 66 | }; |
68 | 67 | ||
69 | function llenarDatos(res) { | 68 | function llenarDatos(res) { |
70 | $scope.facturas = []; | 69 | $scope.facturas = []; |
71 | $scope.filteredFacturas = []; | 70 | $scope.filteredFacturas = []; |
72 | $scope.currentPageFacturas = []; | 71 | $scope.currentPageFacturas = []; |
73 | $scope.selectedFactura = -1; | 72 | $scope.selectedFactura = -1; |
74 | $scope.searchLoading = false; | 73 | $scope.searchLoading = false; |
75 | $scope.primerBusqueda = true; | 74 | $scope.primerBusqueda = true; |
76 | $scope.facturas = calcularSaldos(res.data); | 75 | $scope.facturas = calcularSaldos(res.data); |
77 | $scope.search(); | 76 | $scope.search(); |
78 | primera(); | 77 | primera(); |
79 | } | 78 | } |
80 | 79 | ||
81 | $scope.verFactura = function(factura) { | 80 | $scope.verFactura = function(factura) { |
82 | var modalInstance = $uibModal.open( | 81 | var modalInstance = $uibModal.open( |
83 | { | 82 | { |
84 | ariaLabelledBy: 'Detalle de factura', | 83 | ariaLabelledBy: 'Detalle de factura', |
85 | templateUrl: 'foca-modal-factura-detalle.html', | 84 | templateUrl: 'foca-modal-factura-detalle.html', |
86 | controller: 'focaModalFacturaDetalleController', | 85 | controller: 'focaModalFacturaDetalleController', |
87 | size: 'md', | 86 | size: 'md', |
88 | resolve: { | 87 | resolve: { |
89 | factura: factura | 88 | factura: factura |
90 | } | 89 | } |
91 | } | 90 | } |
92 | ); | 91 | ); |
93 | modalInstance.result.then(); | 92 | modalInstance.result.then(); |
94 | }; | 93 | }; |
95 | 94 | ||
96 | $scope.search = function() { | 95 | $scope.search = function() { |
97 | if($scope.facturas.length > 0) { | 96 | if($scope.facturas.length > 0) { |
98 | $scope.filteredFacturas = $filter('filter')( | 97 | $scope.filteredFacturas = $filter('filter')( |
99 | $scope.facturas, | 98 | $scope.facturas, |
100 | {$: $scope.filters} | 99 | {$: $scope.filters} |
101 | ); | 100 | ); |
102 | 101 | ||
103 | $scope.lastPage = Math.ceil( | 102 | $scope.lastPage = Math.ceil( |
104 | $scope.filteredFacturas.length / $scope.numPerPage | 103 | $scope.filteredFacturas.length / $scope.numPerPage |
105 | ); | 104 | ); |
106 | 105 | ||
107 | $scope.resetPage(); | 106 | $scope.resetPage(); |
108 | } | 107 | } |
109 | }; | 108 | }; |
110 | 109 | ||
111 | $scope.resetPage = function() { | 110 | $scope.resetPage = function() { |
112 | $scope.currentPage = 1; | 111 | $scope.currentPage = 1; |
113 | $scope.selectPage(1); | 112 | $scope.selectPage(1); |
114 | }; | 113 | }; |
115 | 114 | ||
116 | $scope.selectPage = function(page) { | 115 | $scope.selectPage = function(page) { |
117 | var start = (page - 1) * $scope.numPerPage; | 116 | var start = (page - 1) * $scope.numPerPage; |
118 | var end = start + $scope.numPerPage; | 117 | var end = start + $scope.numPerPage; |
119 | $scope.paginas = []; | 118 | $scope.paginas = []; |
120 | $scope.paginas = calcularPages(page); | 119 | $scope.paginas = calcularPages(page); |
121 | $scope.currentPageFacturas = $scope.filteredFacturas.slice(start, end); | 120 | $scope.currentPageFacturas = $scope.filteredFacturas.slice(start, end); |
122 | $scope.currentPage = page; | 121 | $scope.currentPage = page; |
123 | }; | 122 | }; |
124 | 123 | ||
125 | $scope.cancel = function() { | 124 | $scope.cancel = function() { |
126 | $uibModalInstance.dismiss('cancel'); | 125 | $uibModalInstance.dismiss('cancel'); |
127 | }; | 126 | }; |
128 | 127 | ||
129 | $scope.busquedaDown = function(key) { | 128 | $scope.busquedaDown = function(key) { |
130 | if (key === 40) { | 129 | if (key === 40) { |
131 | primera(key); | 130 | primera(key); |
132 | } | 131 | } |
133 | }; | 132 | }; |
134 | 133 | ||
135 | $scope.itemFactura = function(key) { | 134 | $scope.itemFactura = function(key) { |
136 | if (key === 38) { | 135 | if (key === 38) { |
137 | anterior(key); | 136 | anterior(key); |
138 | } | 137 | } |
139 | 138 | ||
140 | if (key === 40) { | 139 | if (key === 40) { |
141 | siguiente(key); | 140 | siguiente(key); |
142 | } | 141 | } |
143 | 142 | ||
144 | if (key === 37) { | 143 | if (key === 37) { |
145 | retrocederPagina(); | 144 | retrocederPagina(); |
146 | } | 145 | } |
147 | 146 | ||
148 | if (key === 39) { | 147 | if (key === 39) { |
149 | avanzarPagina(); | 148 | avanzarPagina(); |
150 | } | 149 | } |
151 | }; | 150 | }; |
152 | 151 | ||
153 | $scope.enviarMail = function(factura) { | 152 | $scope.enviarMail = function(factura) { |
154 | focaModalService | 153 | focaModalService |
155 | .prompt('Ingrese email para enviar comprobante', factura.MAIL_COBRANZA) | 154 | .prompt('Ingrese email para enviar comprobante', factura.MAIL_COBRANZA) |
156 | .then(function(res) { | 155 | .then(function(res) { |
157 | return focaModalFacturaService.enviarFacturaPorMail(res, factura); | 156 | return focaModalFacturaService.enviarFacturaPorMail(res, factura); |
158 | }) | 157 | }) |
159 | .then(function() { | 158 | .then(function() { |
160 | focaModalService.alert('Mensaje enviado correctamente'); | 159 | focaModalService.alert('Mensaje enviado correctamente'); |
161 | }); | 160 | }); |
162 | }; | 161 | }; |
163 | 162 | ||
164 | function calcularPages(paginaActual) { | 163 | function calcularPages(paginaActual) { |
165 | var paginas = []; | 164 | var paginas = []; |
166 | paginas.push(paginaActual); | 165 | paginas.push(paginaActual); |
167 | 166 | ||
168 | if (paginaActual - 1 > 1) { | 167 | if (paginaActual - 1 > 1) { |
169 | 168 | ||
170 | paginas.unshift(paginaActual - 1); | 169 | paginas.unshift(paginaActual - 1); |
171 | if (paginaActual - 2 > 1) { | 170 | if (paginaActual - 2 > 1) { |
172 | paginas.unshift(paginaActual - 2); | 171 | paginas.unshift(paginaActual - 2); |
173 | } | 172 | } |
174 | } | 173 | } |
175 | 174 | ||
176 | if (paginaActual + 1 < $scope.lastPage) { | 175 | if (paginaActual + 1 < $scope.lastPage) { |
177 | paginas.push(paginaActual + 1); | 176 | paginas.push(paginaActual + 1); |
178 | if (paginaActual + 2 < $scope.lastPage) { | 177 | if (paginaActual + 2 < $scope.lastPage) { |
179 | paginas.push(paginaActual + 2); | 178 | paginas.push(paginaActual + 2); |
180 | } | 179 | } |
181 | } | 180 | } |
182 | 181 | ||
183 | if (paginaActual !== 1) { | 182 | if (paginaActual !== 1) { |
184 | paginas.unshift(1); | 183 | paginas.unshift(1); |
185 | } | 184 | } |
186 | 185 | ||
187 | if (paginaActual !== $scope.lastPage) { | 186 | if (paginaActual !== $scope.lastPage) { |
188 | paginas.push($scope.lastPage); | 187 | paginas.push($scope.lastPage); |
189 | } | 188 | } |
190 | 189 | ||
191 | return paginas; | 190 | return paginas; |
192 | } | 191 | } |
193 | 192 | ||
194 | function primera() { | 193 | function primera() { |
195 | $scope.selectedFactura = 0; | 194 | $scope.selectedFactura = 0; |
196 | } | 195 | } |
197 | 196 | ||
198 | function anterior() { | 197 | function anterior() { |
199 | if ($scope.selectedFactura === 0 && $scope.currentPage > 1) { | 198 | if ($scope.selectedFactura === 0 && $scope.currentPage > 1) { |
200 | retrocederPagina(); | 199 | retrocederPagina(); |
201 | } else { | 200 | } else { |
202 | $scope.selectedFactura--; | 201 | $scope.selectedFactura--; |
203 | } | 202 | } |
204 | } | 203 | } |
205 | 204 | ||
206 | function siguiente() { | 205 | function siguiente() { |
207 | if ($scope.selectedFactura < $scope.currentPageFacturas.length - 1 ) { | 206 | if ($scope.selectedFactura < $scope.currentPageFacturas.length - 1 ) { |
208 | $scope.selectedFactura++; | 207 | $scope.selectedFactura++; |
209 | } else { | 208 | } else { |
210 | avanzarPagina(); | 209 | avanzarPagina(); |
211 | } | 210 | } |
212 | } | 211 | } |
213 | 212 | ||
214 | function retrocederPagina() { | 213 | function retrocederPagina() { |
215 | if ($scope.currentPage > 1) { | 214 | if ($scope.currentPage > 1) { |
216 | $scope.selectPage($scope.currentPage - 1); | 215 | $scope.selectPage($scope.currentPage - 1); |
217 | $scope.selectedFactura = $scope.numPerPage - 1; | 216 | $scope.selectedFactura = $scope.numPerPage - 1; |
218 | } | 217 | } |
219 | } | 218 | } |
220 | 219 | ||
221 | function avanzarPagina() { | 220 | function avanzarPagina() { |
222 | if ($scope.currentPage < $scope.lastPage) { | 221 | if ($scope.currentPage < $scope.lastPage) { |
223 | $scope.selectPage($scope.currentPage + 1); | 222 | $scope.selectPage($scope.currentPage + 1); |
224 | $scope.selectedFactura = 0; | 223 | $scope.selectedFactura = 0; |
225 | } | 224 | } |
226 | } | 225 | } |
227 | 226 | ||
228 | function calcularSaldos(facturas) { | 227 | function calcularSaldos(facturas) { |
229 | var saldo = 0; | 228 | var saldo = 0; |
230 | 229 | ||
231 | facturas.forEach(function(factura) { | 230 | facturas.forEach(function(factura) { |
232 | if (factura.TCO === 'CI' || | 231 | if (factura.TCO === 'CI' || |
233 | factura.TCO === 'FT' || | 232 | factura.TCO === 'FT' || |
234 | factura.TCO === 'ND'){ | 233 | factura.TCO === 'ND'){ |
235 | factura.IPA = factura.IPA * -1; | 234 | factura.IPA = factura.IPA * -1; |
236 | }else{ | 235 | }else{ |
237 | factura.IPA = factura.IPA; | 236 | factura.IPA = factura.IPA; |
238 | } | 237 | } |
239 | saldo += factura.IPA; | 238 | saldo += factura.IPA; |
240 | factura.saldo = saldo; | 239 | factura.saldo = saldo; |
241 | factura.saldo_show = Math.abs(saldo); | 240 | factura.saldo_show = Math.abs(saldo); |
242 | factura.IPA_SHOW = Math.abs(factura.IPA); | 241 | factura.IPA_SHOW = Math.abs(factura.IPA); |
243 | }); | 242 | }); |
244 | 243 | ||
245 | return facturas; | 244 | return facturas; |
246 | } | 245 | } |
247 | 246 | ||
248 | $scope.busquedaPress(13); | 247 | $scope.busquedaPress(13); |
249 | } | 248 | } |
250 | ] | 249 | ] |
251 | ); | 250 | ); |
252 | 251 |