Commit 60ec8cdd63d5852935449046e244fa699dc008f1

Authored by Jose Pinto
1 parent 46923feb4c
Exists in master

espacios

Showing 1 changed file with 2 additions and 2 deletions   Show diff stats
src/js/controller.js
1 angular.module('focaModalPrecioCondicion') 1 angular.module('focaModalPrecioCondicion')
2 .controller('focaModalPrecioCondicionController', 2 .controller('focaModalPrecioCondicionController',
3 [ 3 [
4 '$timeout', 4 '$timeout',
5 '$filter', 5 '$filter',
6 '$scope', 6 '$scope',
7 '$uibModalInstance', 7 '$uibModalInstance',
8 'focaModalService', 8 'focaModalService',
9 'focaModalPrecioCondicionService', 9 'focaModalPrecioCondicionService',
10 function( 10 function(
11 $timeout, $filter, $scope, $uibModalInstance, 11 $timeout, $filter, $scope, $uibModalInstance,
12 focaModalService, focaModalPrecioCondicionService 12 focaModalService, focaModalPrecioCondicionService
13 ) { 13 ) {
14 14
15 focaModalPrecioCondicionService.getPreciosCondicionesPlazosPagos().then( 15 focaModalPrecioCondicionService.getPreciosCondicionesPlazosPagos().then(
16 function(res) { 16 function(res) {
17 for(var i = 0; i < res.data.length; i++) { 17 for(var i = 0; i < res.data.length; i++) {
18 var plazosTemp = ''; 18 var plazosTemp = '';
19 for(var j = 0; j < res.data[i].plazoPago.length; j++) { 19 for(var j = 0; j < res.data[i].plazoPago.length; j++) {
20 if(j + 1 === res.data[i].plazoPago.length){ 20 if(j + 1 === res.data[i].plazoPago.length) {
21 plazosTemp += res.data[i].plazoPago[j].dias; 21 plazosTemp += res.data[i].plazoPago[j].dias;
22 }else{ 22 }else {
23 plazosTemp += res.data[i].plazoPago[j].dias + ', '; 23 plazosTemp += res.data[i].plazoPago[j].dias + ', ';
24 } 24 }
25 } 25 }
26 res.data[i].plazos = plazosTemp.trim(); 26 res.data[i].plazos = plazosTemp.trim();
27 } 27 }
28 $scope.precioCondicion = res.data; 28 $scope.precioCondicion = res.data;
29 $scope.search(); 29 $scope.search();
30 } 30 }
31 ); 31 );
32 $scope.filters = ''; 32 $scope.filters = '';
33 $scope.ingreso = false; 33 $scope.ingreso = false;
34 $scope.plazosNuevos = []; 34 $scope.plazosNuevos = [];
35 $scope.plazoACargar = 35 $scope.plazoACargar =
36 { 36 {
37 item: 1 37 item: 1
38 }; 38 };
39 // pagination 39 // pagination
40 $scope.numPerPage = 10; 40 $scope.numPerPage = 10;
41 $scope.currentPage = 1; 41 $scope.currentPage = 1;
42 $scope.filteredPrecioCondicion = []; 42 $scope.filteredPrecioCondicion = [];
43 $scope.currentPagePrecioCondicion = []; 43 $scope.currentPagePrecioCondicion = [];
44 $scope.selectedPrecioCondicion = -1; 44 $scope.selectedPrecioCondicion = -1;
45 45
46 //METODOS 46 //METODOS
47 47
48 $scope.agregarPlazo = function(key) { 48 $scope.agregarPlazo = function(key) {
49 if(key === 13) { 49 if(key === 13) {
50 if(!$scope.plazoACargar.dias) { 50 if(!$scope.plazoACargar.dias) {
51 focaModalService.alert('Ingrese cantidad de días'); 51 focaModalService.alert('Ingrese cantidad de días');
52 return; 52 return;
53 } 53 }
54 var tieneEseDia = $scope.plazosNuevos.filter(function(a) { 54 var tieneEseDia = $scope.plazosNuevos.filter(function(a) {
55 return a.dias === $scope.plazoACargar.dias; 55 return a.dias === $scope.plazoACargar.dias;
56 }); 56 });
57 if(tieneEseDia.length > 0) { 57 if(tieneEseDia.length > 0) {
58 focaModalService.alert('Ya ha ingresado un plazo con esos días'); 58 focaModalService.alert('Ya ha ingresado un plazo con esos días');
59 return; 59 return;
60 } 60 }
61 $scope.plazosNuevos.push($scope.plazoACargar); 61 $scope.plazosNuevos.push($scope.plazoACargar);
62 $scope.plazoACargar = 62 $scope.plazoACargar =
63 { 63 {
64 item: $scope.plazosNuevos.length + 1 64 item: $scope.plazosNuevos.length + 1
65 }; 65 };
66 } 66 }
67 }; 67 };
68 68
69 $scope.volver = function() { 69 $scope.volver = function() {
70 $scope.ingreso = false; 70 $scope.ingreso = false;
71 $scope.plazosNuevos = []; 71 $scope.plazosNuevos = [];
72 $scope.plazoACargar = 72 $scope.plazoACargar =
73 { 73 {
74 item: $scope.plazosNuevos.length + 1 74 item: $scope.plazosNuevos.length + 1
75 }; 75 };
76 }; 76 };
77 77
78 $scope.quitarPlazo = function(key) { 78 $scope.quitarPlazo = function(key) {
79 $scope.plazosNuevos.splice(key, 1); 79 $scope.plazosNuevos.splice(key, 1);
80 $scope.plazoACargar = 80 $scope.plazoACargar =
81 { 81 {
82 item: $scope.plazosNuevos.length + 1 82 item: $scope.plazosNuevos.length + 1
83 }; 83 };
84 }; 84 };
85 85
86 $scope.search = function(pressed) { 86 $scope.search = function(pressed) {
87 $scope.filteredPrecioCondicion = $filter('filter')( 87 $scope.filteredPrecioCondicion = $filter('filter')(
88 $scope.precioCondicion, 88 $scope.precioCondicion,
89 {$: $scope.filters} 89 {$: $scope.filters}
90 ); 90 );
91 91
92 if(pressed) { 92 if(pressed) {
93 if($scope.filteredPrecioCondicion.length === 0) { 93 if($scope.filteredPrecioCondicion.length === 0) {
94 $timeout(function() { 94 $timeout(function() {
95 angular.element('#search')[0].focus(); 95 angular.element('#search')[0].focus();
96 $scope.filters = ''; 96 $scope.filters = '';
97 }); 97 });
98 }else { 98 }else {
99 primera(); 99 primera();
100 } 100 }
101 } 101 }
102 102
103 $scope.lastPage = Math.ceil( 103 $scope.lastPage = Math.ceil(
104 $scope.filteredPrecioCondicion.length / $scope.numPerPage 104 $scope.filteredPrecioCondicion.length / $scope.numPerPage
105 ); 105 );
106 106
107 $scope.resetPage(); 107 $scope.resetPage();
108 }; 108 };
109 109
110 $scope.resetPage = function() { 110 $scope.resetPage = function() {
111 $scope.currentPage = 1; 111 $scope.currentPage = 1;
112 $scope.selectPage(1); 112 $scope.selectPage(1);
113 }; 113 };
114 114
115 $scope.selectPage = function(page) { 115 $scope.selectPage = function(page) {
116 var start = (page - 1) * $scope.numPerPage; 116 var start = (page - 1) * $scope.numPerPage;
117 var end = start + $scope.numPerPage; 117 var end = start + $scope.numPerPage;
118 $scope.paginas = []; 118 $scope.paginas = [];
119 $scope.paginas = calcularPages(page); 119 $scope.paginas = calcularPages(page);
120 $scope.currentPagePrecioCondicion = 120 $scope.currentPagePrecioCondicion =
121 $scope.filteredPrecioCondicion.slice(start, end); 121 $scope.filteredPrecioCondicion.slice(start, end);
122 $scope.currentPage = page; 122 $scope.currentPage = page;
123 }; 123 };
124 124
125 $scope.select = function(precioCondicion) { 125 $scope.select = function(precioCondicion) {
126 $uibModalInstance.close(precioCondicion); 126 $uibModalInstance.close(precioCondicion);
127 }; 127 };
128 128
129 $scope.cancel = function() { 129 $scope.cancel = function() {
130 $uibModalInstance.dismiss('cancel'); 130 $uibModalInstance.dismiss('cancel');
131 }; 131 };
132 132
133 $scope.busquedaDown = function(key) { 133 $scope.busquedaDown = function(key) {
134 if (key === 40) { 134 if (key === 40) {
135 primera(key); 135 primera(key);
136 } 136 }
137 }; 137 };
138 138
139 $scope.busquedaPress = function(key) { 139 $scope.busquedaPress = function(key) {
140 if (key === 13) { 140 if (key === 13) {
141 $scope.search(true); 141 $scope.search(true);
142 } 142 }
143 }; 143 };
144 144
145 $scope.itemProducto = function(key) { 145 $scope.itemProducto = function(key) {
146 if (key === 38) { 146 if (key === 38) {
147 anterior(key); 147 anterior(key);
148 } 148 }
149 149
150 if (key === 40) { 150 if (key === 40) {
151 siguiente(key); 151 siguiente(key);
152 } 152 }
153 153
154 if (key === 37) { 154 if (key === 37) {
155 retrocederPagina(); 155 retrocederPagina();
156 } 156 }
157 157
158 if (key === 39) { 158 if (key === 39) {
159 avanzarPagina(); 159 avanzarPagina();
160 } 160 }
161 }; 161 };
162 162
163 function calcularPages(paginaActual) { 163 function calcularPages(paginaActual) {
164 var paginas = []; 164 var paginas = [];
165 paginas.push(paginaActual); 165 paginas.push(paginaActual);
166 166
167 if (paginaActual - 1 > 1) { 167 if (paginaActual - 1 > 1) {
168 168
169 paginas.unshift(paginaActual - 1); 169 paginas.unshift(paginaActual - 1);
170 if (paginaActual - 2 > 1) { 170 if (paginaActual - 2 > 1) {
171 paginas.unshift(paginaActual - 2); 171 paginas.unshift(paginaActual - 2);
172 } 172 }
173 } 173 }
174 174
175 if (paginaActual + 1 < $scope.lastPage) { 175 if (paginaActual + 1 < $scope.lastPage) {
176 paginas.push(paginaActual + 1); 176 paginas.push(paginaActual + 1);
177 if (paginaActual + 2 < $scope.lastPage) { 177 if (paginaActual + 2 < $scope.lastPage) {
178 paginas.push(paginaActual + 2); 178 paginas.push(paginaActual + 2);
179 } 179 }
180 } 180 }
181 181
182 if (paginaActual !== 1) { 182 if (paginaActual !== 1) {
183 paginas.unshift(1); 183 paginas.unshift(1);
184 } 184 }
185 185
186 if (paginaActual !== $scope.lastPage) { 186 if (paginaActual !== $scope.lastPage) {
187 paginas.push($scope.lastPage); 187 paginas.push($scope.lastPage);
188 } 188 }
189 189
190 return paginas; 190 return paginas;
191 } 191 }
192 192
193 function primera() { 193 function primera() {
194 $scope.selectedPrecioCondicion = 0; 194 $scope.selectedPrecioCondicion = 0;
195 } 195 }
196 196
197 function anterior() { 197 function anterior() {
198 if ($scope.selectedPrecioCondicion === 0 && $scope.currentPage > 1) { 198 if ($scope.selectedPrecioCondicion === 0 && $scope.currentPage > 1) {
199 retrocederPagina(); 199 retrocederPagina();
200 } else { 200 } else {
201 $scope.selectedPrecioCondicion--; 201 $scope.selectedPrecioCondicion--;
202 } 202 }
203 } 203 }
204 204
205 function siguiente() { 205 function siguiente() {
206 if ($scope.selectedPrecioCondicion < 206 if ($scope.selectedPrecioCondicion <
207 $scope.currentPagePrecioCondicion.length ) { 207 $scope.currentPagePrecioCondicion.length ) {
208 $scope.selectedPrecioCondicion++; 208 $scope.selectedPrecioCondicion++;
209 } else { 209 } else {
210 avanzarPagina(); 210 avanzarPagina();
211 } 211 }
212 } 212 }
213 213
214 function retrocederPagina() { 214 function retrocederPagina() {
215 if ($scope.currentPage > 1) { 215 if ($scope.currentPage > 1) {
216 $scope.selectPage($scope.currentPage - 1); 216 $scope.selectPage($scope.currentPage - 1);
217 $scope.selectedPrecioCondicion = $scope.numPerPage - 1; 217 $scope.selectedPrecioCondicion = $scope.numPerPage - 1;
218 } 218 }
219 } 219 }
220 220
221 function avanzarPagina() { 221 function avanzarPagina() {
222 if ($scope.currentPage < $scope.lastPage) { 222 if ($scope.currentPage < $scope.lastPage) {
223 $scope.selectPage($scope.currentPage + 1); 223 $scope.selectPage($scope.currentPage + 1);
224 $scope.selectedPrecioCondicion = 0; 224 $scope.selectedPrecioCondicion = 0;
225 } 225 }
226 } 226 }
227 } 227 }
228 ] 228 ]
229 ); 229 );
230 230