Commit 9292ee8d8b198b069ee24581bcced8bfb0207b96

Authored by Jose Pinto
Exists in master

Merge branch 'master' into 'master'

ordeno plazos de menor a mayor

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