Commit e728e2db08f21c95f1edd104848cbb37c2a0ddfb

Authored by Jose Pinto
Exists in master

Merge branch 'master' into 'master'

Master(efernandez)

See merge request !18
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 *
395 $scope.cobranza.cotizacion.VENDEDOR;
396 } else {
397 $scope.cobrosTabla.push(cobro);
398 }
399 }, function() { 397 }, function() {
400 398
401 } 399 }
402 ); 400 );
403 }; 401 };
404 402
405 $scope.seleccionarMoneda = function() { 403 $scope.seleccionarMoneda = function() {
406 var modalInstance = $uibModal.open( 404 var modalInstance = $uibModal.open(
407 { 405 {
408 ariaLabelledBy: 'Busqueda de Moneda', 406 ariaLabelledBy: 'Busqueda de Moneda',
409 templateUrl: 'modal-moneda.html', 407 templateUrl: 'modal-moneda.html',
410 controller: 'focaModalMonedaController', 408 controller: 'focaModalMonedaController',
411 size: 'lg' 409 size: 'lg'
412 } 410 }
413 ); 411 );
414 modalInstance.result.then( 412 modalInstance.result.then(
415 function(moneda) { 413 function(moneda) {
416 $scope.seleccionarCotizacion(moneda); 414 $scope.seleccionarCotizacion(moneda);
417 }, function() { 415 }, function() {
418 416
419 } 417 }
420 ); 418 );
421 }; 419 };
422 420
423 $scope.seleccionarCotizacion = function(moneda) { 421 $scope.seleccionarCotizacion = function(moneda) {
424 var modalInstance = $uibModal.open( 422 var modalInstance = $uibModal.open(
425 { 423 {
426 ariaLabelledBy: 'Busqueda de Cotización', 424 ariaLabelledBy: 'Busqueda de Cotización',
427 templateUrl: 'modal-cotizacion.html', 425 templateUrl: 'modal-cotizacion.html',
428 controller: 'focaModalCotizacionController', 426 controller: 'focaModalCotizacionController',
429 size: 'lg', 427 size: 'lg',
430 resolve: {idMoneda: function() {return moneda.ID;}} 428 resolve: {idMoneda: function() {return moneda.ID;}}
431 } 429 }
432 ); 430 );
433 modalInstance.result.then( 431 modalInstance.result.then(
434 function(cotizacion) { 432 function(cotizacion) {
435 $scope.cobranza.moneda = moneda; 433 $scope.cobranza.moneda = moneda;
436 $scope.cobranza.cotizacion = cotizacion; 434 $scope.cobranza.cotizacion = cotizacion;
437 addCabecera('Moneda:', moneda.DETALLE); 435 addCabecera('Moneda:', moneda.DETALLE);
438 addCabecera( 436 addCabecera(
439 'Fecha cotizacion:', 437 'Fecha cotizacion:',
440 $filter('date')(cotizacion.FECHA, 'dd/MM/yyyy') 438 $filter('date')(cotizacion.FECHA, 'dd/MM/yyyy')
441 ); 439 );
442 addCabecera('Cotizacion:', cotizacion.VENDEDOR); 440 addCabecera('Cotizacion:', cotizacion.VENDEDOR);
443 }, function() { 441 }, function() {
444 442
445 } 443 }
446 ); 444 );
447 }; 445 };
448 446
449 $scope.seleccionarCobrador = function() { 447 $scope.seleccionarCobrador = function() {
450 var modalInstance = $uibModal.open( 448 var modalInstance = $uibModal.open(
451 { 449 {
452 ariaLabelledBy: 'Busqueda de Cobradores', 450 ariaLabelledBy: 'Busqueda de Cobradores',
453 templateUrl: 'modal-cobradores.html', 451 templateUrl: 'modal-cobradores.html',
454 controller: 'focaModalCobradoresController', 452 controller: 'focaModalCobradoresController',
455 size: 'lg' 453 size: 'lg'
456 } 454 }
457 ); 455 );
458 modalInstance.result.then( 456 modalInstance.result.then(
459 function(cobrador) { 457 function(cobrador) {
460 addCabecera('Cobrador:', cobrador.nombre); 458 addCabecera('Cobrador:', cobrador.nombre);
461 $scope.cobranza.cobrador = cobrador; 459 $scope.cobranza.cobrador = cobrador;
462 }, function() { 460 }, function() {
463 461
464 } 462 }
465 ); 463 );
466 }; 464 };
467 465
468 $scope.getTotalDeuda = function() { 466 $scope.getTotalDeuda = function() {
469 var total = 0; 467 var total = 0;
470 for (var i = 0; i < $scope.facturaTabla.length; i++) { 468 for (var i = 0; i < $scope.facturaTabla.length; i++) {
471 total += $scope.facturaTabla[i].IPA; 469 total += $scope.facturaTabla[i].IPA;
472 } 470 }
473 return parseFloat(total.toFixed(2)); 471 return parseFloat(total.toFixed(2));
474 }; 472 };
475 473
476 $scope.getTotalCobrado = function() { 474 $scope.getTotalCobrado = function() {
477 var total = 0; 475 var total = 0;
478 for (var i = 0; i < $scope.cobrosTabla.length; i++) { 476 for (var i = 0; i < $scope.cobrosTabla.length; i++) {
479 total += $scope.cobrosTabla[i].importe; 477 total += $scope.cobrosTabla[i].importe;
480 } 478 }
481 return parseFloat(total.toFixed(2)); 479 return parseFloat(total.toFixed(2));
482 }; 480 };
483 481
484 $scope.getSubTotal = function() { 482 $scope.getSubTotal = function() {
485 if($scope.articuloACargar) { 483 if($scope.articuloACargar) {
486 return $scope.articuloACargar.precio * $scope.articuloACargar.cantidad; 484 return $scope.articuloACargar.precio * $scope.articuloACargar.cantidad;
487 } 485 }
488 }; 486 };
489 //Recibe aviso si el teclado está en uso 487 //Recibe aviso si el teclado está en uso
490 // $rootScope.$on('usarTeclado', function(event, data) { 488 // $rootScope.$on('usarTeclado', function(event, data) {
491 // if(data) { 489 // if(data) {
492 // $scope.mostrarTeclado = true; 490 // $scope.mostrarTeclado = true;
493 // return; 491 // return;
494 // } 492 // }
495 // $scope.mostrarTeclado = false; 493 // $scope.mostrarTeclado = false;
496 // }) 494 // })
497 $scope.selectFocus = function($event) { 495 $scope.selectFocus = function($event) {
498 //Si el teclado esta en uso no selecciona el valor 496 //Si el teclado esta en uso no selecciona el valor
499 // if($scope.mostrarTeclado) { 497 // if($scope.mostrarTeclado) {
500 // return; 498 // return;
501 // } 499 // }
502 $event.target.select(); 500 $event.target.select();
503 }; 501 };
504 502
505 $scope.salir = function() { 503 $scope.salir = function() {
506 $location.path('/'); 504 $location.path('/');
507 }; 505 };
508 506
509 $scope.parsearATexto = function(articulo) { 507 $scope.parsearATexto = function(articulo) {
510 articulo.cantidad = parseFloat(articulo.cantidad); 508 articulo.cantidad = parseFloat(articulo.cantidad);
511 articulo.precio = parseFloat(articulo.precio); 509 articulo.precio = parseFloat(articulo.precio);
512 }; 510 };
513 511
514 $scope.rellenar = function(relleno, longitud) { 512 $scope.rellenar = function(relleno, longitud) {
515 relleno = '' + relleno; 513 relleno = '' + relleno;
516 while (relleno.length < longitud) { 514 while (relleno.length < longitud) {
517 relleno = '0' + relleno; 515 relleno = '0' + relleno;
518 } 516 }
519 517
520 return relleno; 518 return relleno;
521 }; 519 };
522 520
523 $scope.quitarFactura = function(key) { 521 $scope.quitarFactura = function(key) {
524 $scope.facturaTabla.splice(key, 1); 522 $scope.facturaTabla.splice(key, 1);
525 }; 523 };
526 524
527 $scope.quitarCobro = function(key) { 525 $scope.quitarCobro = function(key) {
528 $scope.cobrosTabla.splice(key, 1); 526 $scope.cobrosTabla.splice(key, 1);
529 }; 527 };
530 528
531 function addCabecera(label, valor) { 529 function addCabecera(label, valor) {
532 var propiedad = $filter('filter')($scope.cabecera, {label: label}, true); 530 var propiedad = $filter('filter')($scope.cabecera, {label: label}, true);
533 if(propiedad.length === 1) { 531 if(propiedad.length === 1) {
534 propiedad[0].valor = valor; 532 propiedad[0].valor = valor;
535 } else { 533 } else {
536 $scope.cabecera.push({label: label, valor: valor}); 534 $scope.cabecera.push({label: label, valor: valor});
537 } 535 }
538 } 536 }
539 // TODO: descomentar cuando se use 537 // TODO: descomentar cuando se use
540 /*function removeCabecera(label) { 538 /*function removeCabecera(label) {
541 var propiedad = $filter('filter')($scope.cabecera, {label: label}, true); 539 var propiedad = $filter('filter')($scope.cabecera, {label: label}, true);
542 if(propiedad.length === 1){ 540 if(propiedad.length === 1){
543 $scope.cabecera.splice($scope.cabecera.indexOf(propiedad[0]), 1); 541 $scope.cabecera.splice($scope.cabecera.indexOf(propiedad[0]), 1);
544 } 542 }
545 }*/ 543 }*/
546 function setearMonedaPorDefecto() { 544 function setearMonedaPorDefecto() {
547 var monedaPorDefecto; 545 var monedaPorDefecto;
548 //Trabajo con la cotización más reciente, por eso uso siempre la primera '[0]' 546 //Trabajo con la cotización más reciente, por eso uso siempre la primera '[0]'
549 focaCrearCobranzaService.getCotizacionByIdMoneda(1).then(function(res) { 547 focaCrearCobranzaService.getCotizacionByIdMoneda(1).then(function(res) {
550 monedaPorDefecto = res.data[0]; 548 monedaPorDefecto = res.data[0];
551 $scope.cobranza.moneda = monedaPorDefecto; 549 $scope.cobranza.moneda = monedaPorDefecto;
552 $scope.cobranza.cotizacion = monedaPorDefecto.cotizaciones[0]; 550 $scope.cobranza.cotizacion = monedaPorDefecto.cotizaciones[0];
553 }); 551 });
554 } 552 }
555 553
556 function obtenerNumeroComprobante() { 554 function obtenerNumeroComprobante() {
557 focaCrearCobranzaService.getNumeroRecibo().then( 555 focaCrearCobranzaService.getNumeroRecibo().then(
558 function(res) { 556 function(res) {
559 $scope.puntoVenta = $scope.rellenar(res.data.sucursal, 4); 557 $scope.puntoVenta = $scope.rellenar(res.data.sucursal, 4);
560 $scope.comprobante = $scope.rellenar(res.data.numeroRecibo, 8); 558 $scope.comprobante = $scope.rellenar(res.data.numeroRecibo, 8);
561 }, 559 },
562 function(err) { 560 function(err) {
563 focaModalService.alert('La terminal no esta configurada correctamente'); 561 focaModalService.alert('La terminal no esta configurada correctamente');
564 console.info(err); 562 console.info(err);
565 } 563 }
566 ); 564 );
567 } 565 }
568 obtenerNumeroComprobante(); 566 obtenerNumeroComprobante();
569 setearMonedaPorDefecto(); 567 setearMonedaPorDefecto();