Commit 63d05e3575be6df93bed058466cc4588dae73c72

Authored by Eric Fernandez
1 parent c0938b7d0f
Exists in master

refactor seleccionar efectivo

Showing 1 changed file with 8 additions and 9 deletions   Show diff stats
src/js/controller.js
1 angular.module('focaCrearCobranza') .controller('cobranzaController', 1 angular.module('focaCrearCobranza') .controller('cobranzaController',
2 [ 2 [
3 '$scope', 3 '$scope',
4 '$uibModal', 4 '$uibModal',
5 '$location', 5 '$location',
6 '$filter', 6 '$filter',
7 'focaCrearCobranzaService', 7 'focaCrearCobranzaService',
8 'focaModalService', 8 'focaModalService',
9 '$cookies', 9 '$cookies',
10 'focaSeguimientoService', 10 'focaSeguimientoService',
11 function($scope, $uibModal, $location, $filter, focaCrearCobranzaService, focaModalService, 11 function($scope, $uibModal, $location, $filter, focaCrearCobranzaService, focaModalService,
12 $cookies, focaSeguimientoService) 12 $cookies, focaSeguimientoService)
13 { 13 {
14 $scope.botonera = [ 14 $scope.botonera = [
15 {texto: 'Cliente', accion: function() {$scope.seleccionarCliente();}}, 15 {texto: 'Cliente', accion: function() {$scope.seleccionarCliente();}},
16 {texto: 'Cobrador', accion: function() {$scope.seleccionarCobrador();}}, 16 {texto: 'Cobrador', accion: function() {$scope.seleccionarCobrador();}},
17 {texto: 'Comprobantes', accion: function() {$scope.swichDeuda();}}, 17 {texto: 'Comprobantes', accion: function() {$scope.swichDeuda();}},
18 {texto: 'Cobros', accion: function() {$scope.swichCobro();}}, 18 {texto: 'Cobros', accion: function() {$scope.swichCobro();}},
19 {texto: 'Moneda', accion: function() {$scope.seleccionarMoneda();}}, 19 {texto: 'Moneda', accion: function() {$scope.seleccionarMoneda();}},
20 {texto: '', accion: function() {}}, 20 {texto: '', accion: function() {}},
21 {texto: '', accion: function() {}}, 21 {texto: '', accion: function() {}},
22 {texto: '', accion: function() {}} 22 {texto: '', accion: function() {}}
23 ]; 23 ];
24 $scope.datepickerAbierto = false; 24 $scope.datepickerAbierto = false;
25 $scope.cobroDeuda = true; 25 $scope.cobroDeuda = true;
26 $scope.show = false; 26 $scope.show = false;
27 $scope.cargando = true; 27 $scope.cargando = true;
28 $scope.dateOptions = { 28 $scope.dateOptions = {
29 maxDate: new Date(), 29 maxDate: new Date(),
30 minDate: new Date(2010, 0, 1) 30 minDate: new Date(2010, 0, 1)
31 }; 31 };
32 32
33 $scope.cabecera = []; 33 $scope.cabecera = [];
34 $scope.showCabecera = true; 34 $scope.showCabecera = true;
35 35
36 $scope.cobranza = {}; 36 $scope.cobranza = {};
37 37
38 $scope.fecha = new Date(); 38 $scope.fecha = new Date();
39 $scope.puntoVenta = '0000'; 39 $scope.puntoVenta = '0000';
40 $scope.comprobante = '00000000'; 40 $scope.comprobante = '00000000';
41 $scope.facturaTabla = []; 41 $scope.facturaTabla = [];
42 $scope.cobrosTabla = []; 42 $scope.cobrosTabla = [];
43 43
44 focaCrearCobranzaService.getUsuario($cookies.get('idUsuario')).then(function(data) { 44 focaCrearCobranzaService.getUsuario($cookies.get('idUsuario')).then(function(data) {
45 $scope.usuario = data.data; 45 $scope.usuario = data.data;
46 }); 46 });
47 47
48 $scope.crearCobranza = function() { 48 $scope.crearCobranza = function() {
49 if(!$scope.cobranza.cliente) { 49 if(!$scope.cobranza.cliente) {
50 focaModalService.alert('Ingrese Cliente'); 50 focaModalService.alert('Ingrese Cliente');
51 return; 51 return;
52 } 52 }
53 if(!$scope.cobranza.cobrador) { 53 if(!$scope.cobranza.cobrador) {
54 focaModalService.alert('Ingrese Cobrador'); 54 focaModalService.alert('Ingrese Cobrador');
55 return; 55 return;
56 } 56 }
57 if($scope.facturaTabla.length < 1) { 57 if($scope.facturaTabla.length < 1) {
58 focaModalService.alert('Ingrese al menos una factura'); 58 focaModalService.alert('Ingrese al menos una factura');
59 return; 59 return;
60 } 60 }
61 if($scope.getTotalCobrado() + $scope.getTotalDeuda() !== 0) { 61 if($scope.getTotalCobrado() + $scope.getTotalDeuda() !== 0) {
62 focaModalService.alert('La diferencia debe ser ' + 62 focaModalService.alert('La diferencia debe ser ' +
63 $scope.cobranza.moneda.SIMBOLO + '0,00'); 63 $scope.cobranza.moneda.SIMBOLO + '0,00');
64 return; 64 return;
65 } 65 }
66 var cobranza = {}; 66 var cobranza = {};
67 var cheques = []; 67 var cheques = [];
68 var cuerpos = []; 68 var cuerpos = [];
69 //TODO: habilitar edición 69 //TODO: habilitar edición
70 $scope.editando = false; 70 $scope.editando = false;
71 $scope.saveLoading = true; 71 $scope.saveLoading = true;
72 for (var i = 0; i < $scope.facturaTabla.length; i++) { 72 for (var i = 0; i < $scope.facturaTabla.length; i++) {
73 var cuerpoFactura = { 73 var cuerpoFactura = {
74 CYV: 'V', 74 CYV: 'V',
75 TIP: 'C', 75 TIP: 'C',
76 TCO: 'RC', 76 TCO: 'RC',
77 PVE: $scope.puntoVenta, 77 PVE: $scope.puntoVenta,
78 NCO: $scope.comprobante, 78 NCO: $scope.comprobante,
79 LOP: 'L', 79 LOP: 'L',
80 TIL: $scope.facturaTabla[i].TCO, 80 TIL: $scope.facturaTabla[i].TCO,
81 COM: $scope.facturaTabla[i].numeroFactura + '-' + 81 COM: $scope.facturaTabla[i].numeroFactura + '-' +
82 $scope.rellenar($scope.facturaTabla[i].NCU,2), 82 $scope.rellenar($scope.facturaTabla[i].NCU,2),
83 FEC: $scope.fecha.toISOString().slice(0, 19).replace('T', ' '), 83 FEC: $scope.fecha.toISOString().slice(0, 19).replace('T', ' '),
84 IMP: Math.abs($scope.facturaTabla[i].IPA), 84 IMP: Math.abs($scope.facturaTabla[i].IPA),
85 RES: 0,//caja de tesorería 85 RES: 0,//caja de tesorería
86 SUBM: 0 86 SUBM: 0
87 }; 87 };
88 cuerpos.push(cuerpoFactura); 88 cuerpos.push(cuerpoFactura);
89 } 89 }
90 90
91 for (var j = 0; j < $scope.cobrosTabla.length; j++) { 91 for (var j = 0; j < $scope.cobrosTabla.length; j++) {
92 92
93 var efectivo = $scope.cobrosTabla[j].tipo === 'Efectivo' ? true : false; 93 var efectivo = $scope.cobrosTabla[j].tipo === 'Efectivo' ? true : false;
94 94
95 var cuerpoCobros = { 95 var cuerpoCobros = {
96 CYV: 'V', 96 CYV: 'V',
97 TIP: 'C', 97 TIP: 'C',
98 TCO: 'RC', 98 TCO: 'RC',
99 PVE: $scope.puntoVenta, 99 PVE: $scope.puntoVenta,
100 NCO: $scope.comprobante, 100 NCO: $scope.comprobante,
101 LOP: 'P', 101 LOP: 'P',
102 TIL: 'EF', 102 TIL: 'EF',
103 COM: efectivo ? 'ef(COBRO EN EFECTIVO)' : 'ch(' + 103 COM: efectivo ? 'ef(COBRO EN EFECTIVO)' : 'ch(' +
104 $scope.cobrosTabla[j].numero + ')' + $scope.cobrosTabla[j].banco.desbco, 104 $scope.cobrosTabla[j].numero + ')' + $scope.cobrosTabla[j].banco.desbco,
105 FEC: efectivo ? 105 FEC: efectivo ?
106 $scope.cobrosTabla[j].fecha 106 $scope.cobrosTabla[j].fecha
107 .toISOString().slice(0, 19).replace('T', ' ') : 107 .toISOString().slice(0, 19).replace('T', ' ') :
108 $scope.cobrosTabla[j].fechaPresentacion 108 $scope.cobrosTabla[j].fechaPresentacion
109 .toISOString().slice(0, 19).replace('T', ' '), 109 .toISOString().slice(0, 19).replace('T', ' '),
110 IMP: Math.abs($scope.cobrosTabla[j].importe), 110 IMP: Math.abs($scope.cobrosTabla[j].importe),
111 RES: 0,//caja de tesorería 111 RES: 0,//caja de tesorería
112 SUBM: 0 112 SUBM: 0
113 }; 113 };
114 cuerpos.push(cuerpoCobros); 114 cuerpos.push(cuerpoCobros);
115 115
116 if(!efectivo) { 116 if(!efectivo) {
117 var cheque = { 117 var cheque = {
118 BCO: $scope.cobrosTabla[j].banco.ID, 118 BCO: $scope.cobrosTabla[j].banco.ID,
119 NUM: $scope.comprobante, 119 NUM: $scope.comprobante,
120 FEP: $scope.cobrosTabla[j].fechaPresentacion 120 FEP: $scope.cobrosTabla[j].fechaPresentacion
121 .toISOString().slice(0, 19).replace('T', ' '), 121 .toISOString().slice(0, 19).replace('T', ' '),
122 FEE: $scope.cobrosTabla[j].fechaEmision 122 FEE: $scope.cobrosTabla[j].fechaEmision
123 .toISOString().slice(0, 19).replace('T', ' '), 123 .toISOString().slice(0, 19).replace('T', ' '),
124 LUG: $scope.cobrosTabla[j].localidad.NOMBRE, 124 LUG: $scope.cobrosTabla[j].localidad.NOMBRE,
125 IMP: $scope.cobrosTabla[j].importe, 125 IMP: $scope.cobrosTabla[j].importe,
126 LIB: $scope.cobrosTabla[j].librador, 126 LIB: $scope.cobrosTabla[j].librador,
127 EST: 'C',//'D' depositado, 'E' entregado, 'C' en cartera 127 EST: 'C',//'D' depositado, 'E' entregado, 'C' en cartera
128 PCI: $scope.cobrosTabla[j].provincia.ID, 128 PCI: $scope.cobrosTabla[j].provincia.ID,
129 LPLA: 0, 129 LPLA: 0,
130 PLA: 0, 130 PLA: 0,
131 VEN: $scope.usuario.CodVen,//Id vendedor 131 VEN: $scope.usuario.CodVen,//Id vendedor
132 CCLIE: $scope.cobranza.cliente.COD,//Id cliente 132 CCLIE: $scope.cobranza.cliente.COD,//Id cliente
133 REN: 0, 133 REN: 0,
134 PVEC: $scope.puntoVenta, 134 PVEC: $scope.puntoVenta,
135 NCOC: $scope.comprobante, 135 NCOC: $scope.comprobante,
136 OBSE: $scope.cobrosTabla[j].observaciones, 136 OBSE: $scope.cobrosTabla[j].observaciones,
137 LUV: 0, 137 LUV: 0,
138 ORI: 've', 138 ORI: 've',
139 FER: '', 139 FER: '',
140 BIMP: 0, 140 BIMP: 0,
141 COMP: 'C ' +'RC ' + $scope.puntoVenta + '-' + $scope.comprobante, 141 COMP: 'C ' +'RC ' + $scope.puntoVenta + '-' + $scope.comprobante,
142 VAL_E: '',//Cuando egresa por ingresos y egresos en el numero de egreso 142 VAL_E: '',//Cuando egresa por ingresos y egresos en el numero de egreso
143 VAL_I: '',//Cuando Ingresa por ingresos y egresos en el numero ingreso 143 VAL_I: '',//Cuando Ingresa por ingresos y egresos en el numero ingreso
144 REC_CAJ: 'D', 144 REC_CAJ: 'D',
145 TIPO_C: 0,//?? 145 TIPO_C: 0,//??
146 SALDO_CAJ: 'S', 146 SALDO_CAJ: 'S',
147 FECHA_INGRESO: $scope.fecha 147 FECHA_INGRESO: $scope.fecha
148 .toISOString().slice(0, 19).replace('T', ' '), 148 .toISOString().slice(0, 19).replace('T', ' '),
149 Vendedor_valor: 0, 149 Vendedor_valor: 0,
150 FAMILIA: 0, 150 FAMILIA: 0,
151 CUIT_LIB: '', 151 CUIT_LIB: '',
152 COD_LUG: $scope.cobrosTabla[j].localidad.ID,//código lugar 152 COD_LUG: $scope.cobrosTabla[j].localidad.ID,//código lugar
153 SEN: '', 153 SEN: '',
154 NRC: 0, 154 NRC: 0,
155 COD_LARGO: '', 155 COD_LARGO: '',
156 VN: 0, 156 VN: 0,
157 ID_LECTOR: 0, 157 ID_LECTOR: 0,
158 NATHB: '' 158 NATHB: ''
159 }; 159 };
160 cheques.push(cheque); 160 cheques.push(cheque);
161 } 161 }
162 } 162 }
163 163
164 cobranza = { 164 cobranza = {
165 recibo: { 165 recibo: {
166 CYV: 'V', 166 CYV: 'V',
167 TIP: 'C', 167 TIP: 'C',
168 TCO: 'RC', 168 TCO: 'RC',
169 PVE: $scope.puntoVenta, //Sucursar, punto de venta 169 PVE: $scope.puntoVenta, //Sucursar, punto de venta
170 NCO: $scope.comprobante, //Numero de comprobante 170 NCO: $scope.comprobante, //Numero de comprobante
171 FEC: $scope.fecha.toISOString().slice(0, 19).replace('T', ' '), 171 FEC: $scope.fecha.toISOString().slice(0, 19).replace('T', ' '),
172 CLI: $scope.cobranza.cliente.COD, 172 CLI: $scope.cobranza.cliente.COD,
173 ATO: 0, //número de asiento 173 ATO: 0, //número de asiento
174 CFE: $scope.cobranza.cobrador.nombre, 174 CFE: $scope.cobranza.cobrador.nombre,
175 PLA: '',//Numero de planilla, sin uso 175 PLA: '',//Numero de planilla, sin uso
176 ID_MONEDA: $scope.cobranza.moneda.ID, 176 ID_MONEDA: $scope.cobranza.moneda.ID,
177 COTIZACION: $scope.cobranza.cotizacion.VENDEDOR, 177 COTIZACION: $scope.cobranza.cotizacion.VENDEDOR,
178 idCobrador: $scope.cobranza.cobrador.id 178 idCobrador: $scope.cobranza.cobrador.id
179 }, 179 },
180 cuerpo: cuerpos, 180 cuerpo: cuerpos,
181 cheques: cheques, 181 cheques: cheques,
182 acobypag: { 182 acobypag: {
183 CYV: 'V', 183 CYV: 'V',
184 COD: $scope.cobranza.cliente.COD, 184 COD: $scope.cobranza.cliente.COD,
185 FEP: $scope.fecha.toISOString().slice(0, 19).replace('T', ' '), 185 FEP: $scope.fecha.toISOString().slice(0, 19).replace('T', ' '),
186 TIP: 'C', 186 TIP: 'C',
187 TCO: 'RC', 187 TCO: 'RC',
188 SUC: $scope.puntoVenta, 188 SUC: $scope.puntoVenta,
189 NCO: $scope.comprobante, 189 NCO: $scope.comprobante,
190 IPA: $scope.getTotalCobrado(), 190 IPA: $scope.getTotalCobrado(),
191 SAL: '',//?? 191 SAL: '',//??
192 TCA: 1, 192 TCA: 1,
193 ZONA: 1, 193 ZONA: 1,
194 FPA: 2,//Forma de pago 194 FPA: 2,//Forma de pago
195 REC: 0, 195 REC: 0,
196 REP: 0, 196 REP: 0,
197 FER: null, 197 FER: null,
198 REM: 0, 198 REM: 0,
199 FRE: null,//?? 199 FRE: null,//??
200 PRO: 'N', 200 PRO: 'N',
201 FEV: $scope.fecha.toISOString().slice(0, 19).replace('T', ' ') 201 FEV: $scope.fecha.toISOString().slice(0, 19).replace('T', ' ')
202 }, 202 },
203 datosCobrador: { 203 datosCobrador: {
204 COD: $scope.cobranza.cobrador.id, 204 COD: $scope.cobranza.cobrador.id,
205 PVE: $scope.puntoVenta, 205 PVE: $scope.puntoVenta,
206 NUM: $scope.comprobante, 206 NUM: $scope.comprobante,
207 EST: 'C', 207 EST: 'C',
208 OBS: 'RC: ' + $scope.comprobante + '-' + $scope.fecha.toLocaleDateString(), 208 OBS: 'RC: ' + $scope.comprobante + '-' + $scope.fecha.toLocaleDateString(),
209 DAT1: 'C', 209 DAT1: 'C',
210 CLI: $scope.cobranza.cliente.COD 210 CLI: $scope.cobranza.cliente.COD
211 } 211 }
212 }; 212 };
213 focaCrearCobranzaService.guardarCobranza(cobranza).then( 213 focaCrearCobranzaService.guardarCobranza(cobranza).then(
214 function(result) { 214 function(result) {
215 $scope.saveLoading = false; 215 $scope.saveLoading = false;
216 focaModalService.alert('Cobranza guardada con éxito'); 216 focaModalService.alert('Cobranza guardada con éxito');
217 217
218 focaSeguimientoService.guardarPosicion( 218 focaSeguimientoService.guardarPosicion(
219 result.data.numero, 219 result.data.numero,
220 'Cobranza', 220 'Cobranza',
221 'Nº: ' + $filter('comprobante')([ 221 'Nº: ' + $filter('comprobante')([
222 result.data.sucursal, 222 result.data.sucursal,
223 result.data.numero 223 result.data.numero
224 ]) + '<br/>' + 224 ]) + '<br/>' +
225 'Vendedor: ' + $scope.cobranza.cobrador.nombre + '<br/>' + 225 'Vendedor: ' + $scope.cobranza.cobrador.nombre + '<br/>' +
226 'Total: ' + $filter('currency')($scope.getTotalCobrado()), 226 'Total: ' + $filter('currency')($scope.getTotalCobrado()),
227 result.data.sucursal 227 result.data.sucursal
228 ); 228 );
229 229
230 $scope.cobranza = { 230 $scope.cobranza = {
231 fecha: new Date() 231 fecha: new Date()
232 }; 232 };
233 setearMonedaPorDefecto(); 233 setearMonedaPorDefecto();
234 obtenerNumeroComprobante(); 234 obtenerNumeroComprobante();
235 235
236 $scope.cabecera = []; 236 $scope.cabecera = [];
237 $scope.fecha = new Date(); 237 $scope.fecha = new Date();
238 $scope.facturaTabla = []; 238 $scope.facturaTabla = [];
239 $scope.cobrosTabla = []; 239 $scope.cobrosTabla = [];
240 }, function(error) { 240 }, function(error) {
241 focaModalService.alert('Hubo un problema al cargar la cobranza'); 241 focaModalService.alert('Hubo un problema al cargar la cobranza');
242 $scope.saveLoading = false; 242 $scope.saveLoading = false;
243 console.info(error); 243 console.info(error);
244 } 244 }
245 ); 245 );
246 }; 246 };
247 247
248 $scope.swichCobro = function() { 248 $scope.swichCobro = function() {
249 $scope.cobroDeuda = false; 249 $scope.cobroDeuda = false;
250 }; 250 };
251 251
252 $scope.swichDeuda = function() { 252 $scope.swichDeuda = function() {
253 $scope.cobroDeuda = true; 253 $scope.cobroDeuda = true;
254 }; 254 };
255 255
256 $scope.seleccionarCobranza = function() { 256 $scope.seleccionarCobranza = function() {
257 257
258 var modalInstance = $uibModal.open( 258 var modalInstance = $uibModal.open(
259 { 259 {
260 ariaLabelledBy: 'Busqueda de Cobranzas', 260 ariaLabelledBy: 'Busqueda de Cobranzas',
261 templateUrl: 'foca-modal-cobranza.html', 261 templateUrl: 'foca-modal-cobranza.html',
262 controller: 'focaModalCobranzaController', 262 controller: 'focaModalCobranzaController',
263 size: 'lg' 263 size: 'lg'
264 } 264 }
265 ); 265 );
266 modalInstance.result.then(function(cobranza) { 266 modalInstance.result.then(function(cobranza) {
267 $scope.editando = true; 267 $scope.editando = true;
268 $scope.cabecera = []; 268 $scope.cabecera = [];
269 $scope.facturaTabla = []; 269 $scope.facturaTabla = [];
270 $scope.cobrosTabla = []; 270 $scope.cobrosTabla = [];
271 271
272 $scope.fecha = new Date(cobranza.fecha); 272 $scope.fecha = new Date(cobranza.fecha);
273 273
274 addCabecera('Cliente:', cobranza.cliente.NOM); 274 addCabecera('Cliente:', cobranza.cliente.NOM);
275 addCabecera('Cobrador:', cobranza.cobrador); 275 addCabecera('Cobrador:', cobranza.cobrador);
276 276
277 $scope.facturaTabla = cobranza.facturas; 277 $scope.facturaTabla = cobranza.facturas;
278 $scope.cobrosTabla = cobranza.cobros; 278 $scope.cobrosTabla = cobranza.cobros;
279 279
280 }); 280 });
281 }; 281 };
282 282
283 $scope.seleccionarCliente = function() { 283 $scope.seleccionarCliente = function() {
284 284
285 var modalInstance = $uibModal.open( 285 var modalInstance = $uibModal.open(
286 { 286 {
287 ariaLabelledBy: 'Busqueda de Cliente', 287 ariaLabelledBy: 'Busqueda de Cliente',
288 templateUrl: 'foca-busqueda-cliente-modal.html', 288 templateUrl: 'foca-busqueda-cliente-modal.html',
289 controller: 'focaBusquedaClienteModalController', 289 controller: 'focaBusquedaClienteModalController',
290 size: 'lg' 290 size: 'lg'
291 } 291 }
292 ); 292 );
293 modalInstance.result.then( 293 modalInstance.result.then(
294 function(cliente) { 294 function(cliente) {
295 addCabecera('Cliente:', cliente.nom); 295 addCabecera('Cliente:', cliente.nom);
296 $scope.cobranza.cliente = { 296 $scope.cobranza.cliente = {
297 COD: cliente.cod, 297 COD: cliente.cod,
298 CUIT: cliente.cuit, 298 CUIT: cliente.cuit,
299 NOM: cliente.nom 299 NOM: cliente.nom
300 }; 300 };
301 }, function() { 301 }, function() {
302 302
303 } 303 }
304 ); 304 );
305 }; 305 };
306 306
307 $scope.seleccionarFactura = function() { 307 $scope.seleccionarFactura = function() {
308 if(!$scope.cobranza.cliente) { 308 if(!$scope.cobranza.cliente) {
309 focaModalService.alert('Seleccione primero un cliente'); 309 focaModalService.alert('Seleccione primero un cliente');
310 return; 310 return;
311 } 311 }
312 var modalInstance = $uibModal.open( 312 var modalInstance = $uibModal.open(
313 { 313 {
314 ariaLabelledBy: 'Busqueda de Facturas', 314 ariaLabelledBy: 'Busqueda de Facturas',
315 templateUrl: 'foca-modal-factura.html', 315 templateUrl: 'foca-modal-factura.html',
316 controller: 'focaModalFacturaController', 316 controller: 'focaModalFacturaController',
317 size: 'lg', 317 size: 'lg',
318 resolve: { 318 resolve: {
319 parametrosFactura: function() { 319 parametrosFactura: function() {
320 return { 320 return {
321 cliente: $scope.cobranza.cliente.COD, 321 cliente: $scope.cobranza.cliente.COD,
322 simbolo: $scope.cobranza.moneda.SIMBOLO, 322 simbolo: $scope.cobranza.moneda.SIMBOLO,
323 cotizacion: $scope.cobranza.cotizacion.VENDEDOR, 323 cotizacion: $scope.cobranza.cotizacion.VENDEDOR,
324 moneda: $scope.cobranza.moneda.ID 324 moneda: $scope.cobranza.moneda.ID
325 }; 325 };
326 } 326 }
327 } 327 }
328 } 328 }
329 ); 329 );
330 modalInstance.result.then( 330 modalInstance.result.then(
331 function(facturas) { 331 function(facturas) {
332 $scope.facturaTabla = $scope.facturaTabla.concat(facturas); 332 $scope.facturaTabla = $scope.facturaTabla.concat(facturas);
333 }, function() { 333 }, function() {
334 334
335 } 335 }
336 ); 336 );
337 }; 337 };
338 338
339 $scope.seleccionarCheque = function() { 339 $scope.seleccionarCheque = function() {
340 var modalInstance = $uibModal.open( 340 var modalInstance = $uibModal.open(
341 { 341 {
342 ariaLabelledBy: 'Carga de cheques', 342 ariaLabelledBy: 'Carga de cheques',
343 templateUrl: 'modal-cheque.html', 343 templateUrl: 'modal-cheque.html',
344 controller: 'focaModalChequeController', 344 controller: 'focaModalChequeController',
345 size: 'lg' 345 size: 'lg'
346 } 346 }
347 ); 347 );
348 modalInstance.result.then( 348 modalInstance.result.then(
349 function(cheque) { 349 function(cheque) {
350 var cobro = { 350 var cobro = {
351 tipo: 'Ch' + '(' + cheque.numero + ')' + ' ' + cheque.banco.desbco, 351 tipo: 'Ch' + '(' + cheque.numero + ')' + ' ' + cheque.banco.desbco,
352 numero: cheque.numero, 352 numero: cheque.numero,
353 banco: cheque.banco, 353 banco: cheque.banco,
354 fecha: cheque.fechaEmision.toLocaleDateString() + '-' + 354 fecha: cheque.fechaEmision.toLocaleDateString() + '-' +
355 cheque.fechaPresentacion.toLocaleDateString(), 355 cheque.fechaPresentacion.toLocaleDateString(),
356 fechaPresentacion: cheque.fechaPresentacion, 356 fechaPresentacion: cheque.fechaPresentacion,
357 fechaEmision: cheque.fechaEmision, 357 fechaEmision: cheque.fechaEmision,
358 importe: cheque.importe * $scope.cobranza.cotizacion.VENDEDOR, 358 importe: cheque.importe * $scope.cobranza.cotizacion.VENDEDOR,
359 localidad: cheque.localidad, 359 localidad: cheque.localidad,
360 librador: cheque.librador, 360 librador: cheque.librador,
361 provincia: cheque.provincia, 361 provincia: cheque.provincia,
362 observaciones: cheque.observaciones 362 observaciones: cheque.observaciones
363 }; 363 };
364 $scope.cobrosTabla.push(cobro); 364 $scope.cobrosTabla.push(cobro);
365 }, function() { 365 }, function() {
366 366
367 } 367 }
368 ); 368 );
369 }; 369 };
370 370
371 $scope.seleccionarEfectivo = function() { 371 $scope.seleccionarEfectivo = function() {
372 var modalInstance = $uibModal.open( 372 var modalInstance = $uibModal.open(
373 { 373 {
374 ariaLabelledBy: 'Carga de cheques', 374 ariaLabelledBy: 'Carga de cheques',
375 templateUrl: 'modal-efectivo.html', 375 templateUrl: 'modal-efectivo.html',
376 controller: 'focaModalEfectivoController', 376 controller: 'focaModalEfectivoController',
377 size: 'sm', 377 size: 'sm',
378 resolve: { 378 resolve: {
379 sugerido: function() {return $scope.getTotalDeuda();} 379 sugerido: function() {
380 var sugerido = $scope.getTotalDeuda() + $scope.getTotalCobrado();
381 return sugerido < 0 ? sugerido : null ;
382 }
380 } 383 }
381 } 384 }
382 ); 385 );
383 modalInstance.result.then( 386 modalInstance.result.then(
384 function(efectivo) { 387 function(efectivo) {
385 var cobro = { 388 var cobro = {
386 tipo: 'Efectivo', 389 tipo: 'Efectivo',
387 fecha: new Date(), 390 fecha: new Date(),
388 importe: efectivo * $scope.cobranza.cotizacion.VENDEDOR 391 importe: efectivo * $scope.cobranza.cotizacion.VENDEDOR
389 }; 392 };
390 var existeCobroEfectivo = $scope.cobrosTabla.filter(function(a) { 393 $scope.cobrosTabla = $scope.cobrosTabla.filter(function(a) {
391 return a.tipo === 'Efectivo'; 394 return a.tipo !== 'Efectivo';
392 }); 395 });
393 if(existeCobroEfectivo.length > 0) { 396 $scope.cobrosTabla.push(cobro);
394 existeCobroEfectivo[0].importe += efectivo * 397
395 $scope.cobranza.cotizacion.VENDEDOR;
396 } else {
397 $scope.cobrosTabla.push(cobro);
398 }
399 }, function() { 398 }, function() {
400 399
401 } 400 }
402 ); 401 );
403 }; 402 };
404 403
405 $scope.seleccionarMoneda = function() { 404 $scope.seleccionarMoneda = function() {
406 var modalInstance = $uibModal.open( 405 var modalInstance = $uibModal.open(
407 { 406 {
408 ariaLabelledBy: 'Busqueda de Moneda', 407 ariaLabelledBy: 'Busqueda de Moneda',
409 templateUrl: 'modal-moneda.html', 408 templateUrl: 'modal-moneda.html',
410 controller: 'focaModalMonedaController', 409 controller: 'focaModalMonedaController',
411 size: 'lg' 410 size: 'lg'
412 } 411 }
413 ); 412 );
414 modalInstance.result.then( 413 modalInstance.result.then(
415 function(moneda) { 414 function(moneda) {
416 $scope.seleccionarCotizacion(moneda); 415 $scope.seleccionarCotizacion(moneda);
417 }, function() { 416 }, function() {
418 417
419 } 418 }
420 ); 419 );
421 }; 420 };
422 421
423 $scope.seleccionarCotizacion = function(moneda) { 422 $scope.seleccionarCotizacion = function(moneda) {
424 var modalInstance = $uibModal.open( 423 var modalInstance = $uibModal.open(
425 { 424 {
426 ariaLabelledBy: 'Busqueda de Cotización', 425 ariaLabelledBy: 'Busqueda de Cotización',
427 templateUrl: 'modal-cotizacion.html', 426 templateUrl: 'modal-cotizacion.html',
428 controller: 'focaModalCotizacionController', 427 controller: 'focaModalCotizacionController',
429 size: 'lg', 428 size: 'lg',
430 resolve: {idMoneda: function() {return moneda.ID;}} 429 resolve: {idMoneda: function() {return moneda.ID;}}
431 } 430 }
432 ); 431 );
433 modalInstance.result.then( 432 modalInstance.result.then(
434 function(cotizacion) { 433 function(cotizacion) {
435 $scope.cobranza.moneda = moneda; 434 $scope.cobranza.moneda = moneda;
436 $scope.cobranza.cotizacion = cotizacion; 435 $scope.cobranza.cotizacion = cotizacion;
437 addCabecera('Moneda:', moneda.DETALLE); 436 addCabecera('Moneda:', moneda.DETALLE);
438 addCabecera( 437 addCabecera(
439 'Fecha cotizacion:', 438 'Fecha cotizacion:',
440 $filter('date')(cotizacion.FECHA, 'dd/MM/yyyy') 439 $filter('date')(cotizacion.FECHA, 'dd/MM/yyyy')
441 ); 440 );
442 addCabecera('Cotizacion:', cotizacion.VENDEDOR); 441 addCabecera('Cotizacion:', cotizacion.VENDEDOR);
443 }, function() { 442 }, function() {
444 443
445 } 444 }
446 ); 445 );
447 }; 446 };
448 447
449 $scope.seleccionarCobrador = function() { 448 $scope.seleccionarCobrador = function() {
450 var modalInstance = $uibModal.open( 449 var modalInstance = $uibModal.open(
451 { 450 {
452 ariaLabelledBy: 'Busqueda de Cobradores', 451 ariaLabelledBy: 'Busqueda de Cobradores',
453 templateUrl: 'modal-cobradores.html', 452 templateUrl: 'modal-cobradores.html',
454 controller: 'focaModalCobradoresController', 453 controller: 'focaModalCobradoresController',
455 size: 'lg' 454 size: 'lg'
456 } 455 }
457 ); 456 );
458 modalInstance.result.then( 457 modalInstance.result.then(
459 function(cobrador) { 458 function(cobrador) {
460 addCabecera('Cobrador:', cobrador.nombre); 459 addCabecera('Cobrador:', cobrador.nombre);
461 $scope.cobranza.cobrador = cobrador; 460 $scope.cobranza.cobrador = cobrador;
462 }, function() { 461 }, function() {
463 462
464 } 463 }
465 ); 464 );
466 }; 465 };
467 466
468 $scope.getTotalDeuda = function() { 467 $scope.getTotalDeuda = function() {
469 var total = 0; 468 var total = 0;
470 for (var i = 0; i < $scope.facturaTabla.length; i++) { 469 for (var i = 0; i < $scope.facturaTabla.length; i++) {
471 total += $scope.facturaTabla[i].IPA; 470 total += $scope.facturaTabla[i].IPA;
472 } 471 }
473 return parseFloat(total.toFixed(2)); 472 return parseFloat(total.toFixed(2));
474 }; 473 };
475 474
476 $scope.getTotalCobrado = function() { 475 $scope.getTotalCobrado = function() {
477 var total = 0; 476 var total = 0;
478 for (var i = 0; i < $scope.cobrosTabla.length; i++) { 477 for (var i = 0; i < $scope.cobrosTabla.length; i++) {
479 total += $scope.cobrosTabla[i].importe; 478 total += $scope.cobrosTabla[i].importe;
480 } 479 }
481 return parseFloat(total.toFixed(2)); 480 return parseFloat(total.toFixed(2));
482 }; 481 };
483 482
484 $scope.getSubTotal = function() { 483 $scope.getSubTotal = function() {
485 if($scope.articuloACargar) { 484 if($scope.articuloACargar) {
486 return $scope.articuloACargar.precio * $scope.articuloACargar.cantidad; 485 return $scope.articuloACargar.precio * $scope.articuloACargar.cantidad;
487 } 486 }
488 }; 487 };
489 //Recibe aviso si el teclado está en uso 488 //Recibe aviso si el teclado está en uso
490 // $rootScope.$on('usarTeclado', function(event, data) { 489 // $rootScope.$on('usarTeclado', function(event, data) {
491 // if(data) { 490 // if(data) {
492 // $scope.mostrarTeclado = true; 491 // $scope.mostrarTeclado = true;
493 // return; 492 // return;
494 // } 493 // }
495 // $scope.mostrarTeclado = false; 494 // $scope.mostrarTeclado = false;
496 // }) 495 // })
497 $scope.selectFocus = function($event) { 496 $scope.selectFocus = function($event) {
498 //Si el teclado esta en uso no selecciona el valor 497 //Si el teclado esta en uso no selecciona el valor
499 // if($scope.mostrarTeclado) { 498 // if($scope.mostrarTeclado) {
500 // return; 499 // return;
501 // } 500 // }
502 $event.target.select(); 501 $event.target.select();
503 }; 502 };
504 503
505 $scope.salir = function() { 504 $scope.salir = function() {
506 $location.path('/'); 505 $location.path('/');
507 }; 506 };
508 507
509 $scope.parsearATexto = function(articulo) { 508 $scope.parsearATexto = function(articulo) {
510 articulo.cantidad = parseFloat(articulo.cantidad); 509 articulo.cantidad = parseFloat(articulo.cantidad);
511 articulo.precio = parseFloat(articulo.precio); 510 articulo.precio = parseFloat(articulo.precio);
512 }; 511 };
513 512
514 $scope.rellenar = function(relleno, longitud) { 513 $scope.rellenar = function(relleno, longitud) {
515 relleno = '' + relleno; 514 relleno = '' + relleno;
516 while (relleno.length < longitud) { 515 while (relleno.length < longitud) {
517 relleno = '0' + relleno; 516 relleno = '0' + relleno;
518 } 517 }
519 518
520 return relleno; 519 return relleno;
521 }; 520 };
522 521
523 $scope.quitarFactura = function(key) { 522 $scope.quitarFactura = function(key) {
524 $scope.facturaTabla.splice(key, 1); 523 $scope.facturaTabla.splice(key, 1);
525 }; 524 };
526 525
527 $scope.quitarCobro = function(key) { 526 $scope.quitarCobro = function(key) {
528 $scope.cobrosTabla.splice(key, 1); 527 $scope.cobrosTabla.splice(key, 1);
529 }; 528 };
530 529
531 function addCabecera(label, valor) { 530 function addCabecera(label, valor) {
532 var propiedad = $filter('filter')($scope.cabecera, {label: label}, true); 531 var propiedad = $filter('filter')($scope.cabecera, {label: label}, true);
533 if(propiedad.length === 1) { 532 if(propiedad.length === 1) {
534 propiedad[0].valor = valor; 533 propiedad[0].valor = valor;
535 } else { 534 } else {
536 $scope.cabecera.push({label: label, valor: valor}); 535 $scope.cabecera.push({label: label, valor: valor});
537 } 536 }
538 } 537 }
539 // TODO: descomentar cuando se use 538 // TODO: descomentar cuando se use
540 /*function removeCabecera(label) { 539 /*function removeCabecera(label) {
541 var propiedad = $filter('filter')($scope.cabecera, {label: label}, true); 540 var propiedad = $filter('filter')($scope.cabecera, {label: label}, true);
542 if(propiedad.length === 1){ 541 if(propiedad.length === 1){
543 $scope.cabecera.splice($scope.cabecera.indexOf(propiedad[0]), 1); 542 $scope.cabecera.splice($scope.cabecera.indexOf(propiedad[0]), 1);
544 } 543 }
545 }*/ 544 }*/
546 function setearMonedaPorDefecto() { 545 function setearMonedaPorDefecto() {
547 var monedaPorDefecto; 546 var monedaPorDefecto;
548 //Trabajo con la cotización más reciente, por eso uso siempre la primera '[0]' 547 //Trabajo con la cotización más reciente, por eso uso siempre la primera '[0]'
549 focaCrearCobranzaService.getCotizacionByIdMoneda(1).then(function(res) { 548 focaCrearCobranzaService.getCotizacionByIdMoneda(1).then(function(res) {
550 monedaPorDefecto = res.data[0]; 549 monedaPorDefecto = res.data[0];
551 $scope.cobranza.moneda = monedaPorDefecto; 550 $scope.cobranza.moneda = monedaPorDefecto;
552 $scope.cobranza.cotizacion = monedaPorDefecto.cotizaciones[0]; 551 $scope.cobranza.cotizacion = monedaPorDefecto.cotizaciones[0];
553 }); 552 });
554 } 553 }
555 554
556 function obtenerNumeroComprobante() { 555 function obtenerNumeroComprobante() {
557 focaCrearCobranzaService.getNumeroRecibo().then( 556 focaCrearCobranzaService.getNumeroRecibo().then(
558 function(res) { 557 function(res) {
559 $scope.puntoVenta = $scope.rellenar(res.data.sucursal, 4); 558 $scope.puntoVenta = $scope.rellenar(res.data.sucursal, 4);
560 $scope.comprobante = $scope.rellenar(res.data.numeroRecibo, 8); 559 $scope.comprobante = $scope.rellenar(res.data.numeroRecibo, 8);
561 }, 560 },
562 function(err) { 561 function(err) {
563 focaModalService.alert('La terminal no esta configurada correctamente'); 562 focaModalService.alert('La terminal no esta configurada correctamente');
564 console.info(err); 563 console.info(err);
565 } 564 }
566 ); 565 );
567 } 566 }
568 obtenerNumeroComprobante(); 567 obtenerNumeroComprobante();
569 setearMonedaPorDefecto(); 568 setearMonedaPorDefecto();