Commit ab368d8a25ff2496a3e8a0bd53a7435e94fc47c9

Authored by Eric Fernandez
Exists in master and in 1 other branch develop

Merge branch 'master' into 'develop'

Master(efernandez)

See merge request !7
src/js/controller.js
1 angular.module('focaCrearFactura').controller('facturaController', [ 1 angular.module('focaCrearFactura').controller('facturaController', [
2 '$scope', '$uibModal', '$location', '$filter', 'crearFacturaService', '$timeout', 2 '$scope', '$uibModal', '$location', '$filter', 'crearFacturaService', '$timeout',
3 'focaModalService', 'crearRemitoService', '$rootScope', 'focaBotoneraLateralService', 3 'focaModalService', 'crearRemitoService', '$rootScope', 'focaBotoneraLateralService',
4 '$localStorage', 'APP', 4 '$localStorage', 'APP',
5 function ( 5 function (
6 $scope, $uibModal, $location, $filter, crearFacturaService, $timeout, focaModalService, 6 $scope, $uibModal, $location, $filter, crearFacturaService, $timeout, focaModalService,
7 crearRemitoService, $rootScope, focaBotoneraLateralService, $localStorage, APP) { 7 crearRemitoService, $rootScope, focaBotoneraLateralService, $localStorage, APP) {
8 8
9 config(); 9 config();
10 10
11 function config() { 11 function config() {
12 $scope.tmpCantidad = Number; 12 $scope.tmpCantidad = Number;
13 $scope.tmpPrecio = Number; 13 $scope.tmpPrecio = Number;
14 $scope.botonera = crearFacturaService.getBotonera(); 14 $scope.botonera = crearFacturaService.getBotonera();
15 $scope.isNumber = angular.isNumber; 15 $scope.isNumber = angular.isNumber;
16 $scope.datepickerAbierto = false; 16 $scope.datepickerAbierto = false;
17 $scope.show = false; 17 $scope.show = false;
18 $scope.cargando = true; 18 $scope.cargando = true;
19 $scope.now = new Date(); 19 $scope.now = new Date();
20 $scope.puntoVenta = rellenar(0, 4); 20 $scope.puntoVenta = rellenar(0, 4);
21 $scope.comprobante = rellenar(0, 8); 21 $scope.comprobante = rellenar(0, 8);
22 $scope.dateOptions = { 22 $scope.dateOptions = {
23 maxDate: new Date(), 23 maxDate: new Date(),
24 minDate: new Date(2010, 0, 1) 24 minDate: new Date(2010, 0, 1)
25 }; 25 };
26 $scope.cabeceras = []; 26 $scope.cabeceras = [];
27 crearFacturaService.getParametros().then(function (res) { 27 crearFacturaService.getParametros().then(function (res) {
28 28
29 var parametros = JSON.parse(res.data[0].jsonText); 29 var parametros = JSON.parse(res.data[0].jsonText);
30 30
31 if ($localStorage.factura) { 31 if ($localStorage.factura) {
32 32
33 $timeout(function () { getLSFactura(); }); 33 $timeout(function () { getLSFactura(); });
34 } else { 34 } else {
35 35
36 for (var property in parametros) { 36 for (var property in parametros) {
37 $scope.factura[property] = parametros[property]; 37 $scope.factura[property] = parametros[property];
38 $scope.inicial[property] = parametros[property]; 38 $scope.inicial[property] = parametros[property];
39 } 39 }
40 40
41 setearFactura($scope.factura); 41 setearFactura($scope.factura);
42 } 42 }
43 43
44 }); 44 });
45 45
46 //SETEO BOTONERA LATERAL 46 //SETEO BOTONERA LATERAL
47 $timeout(function () { 47 $timeout(function () {
48 focaBotoneraLateralService.showSalir(false); 48 focaBotoneraLateralService.showSalir(false);
49 focaBotoneraLateralService.showPausar(true); 49 focaBotoneraLateralService.showPausar(true);
50 focaBotoneraLateralService.showGuardar(true, $scope.seleccionarFormaDePago); 50 focaBotoneraLateralService.showGuardar(true, $scope.seleccionarFormaDePago);
51 focaBotoneraLateralService.addCustomButton('Salir', salir); 51 focaBotoneraLateralService.addCustomButton('Salir', salir);
52 }); 52 });
53 53
54 init(); 54 init();
55 55
56 } 56 }
57 57
58 function init() { 58 function init() {
59 59
60 $scope.$broadcast('cleanCabecera'); 60 $scope.$broadcast('cleanCabecera');
61 61
62 $scope.factura = { 62 $scope.factura = {
63 id: 0, 63 id: 0,
64 estado: 0, 64 estado: 0,
65 vendedor: {}, 65 vendedor: {},
66 cliente: {}, 66 cliente: {},
67 proveedor: {}, 67 proveedor: {},
68 domicilio: { dom: '' }, 68 domicilio: { dom: '' },
69 moneda: {}, 69 moneda: {},
70 cotizacion: $scope.cotizacionPorDefecto || {}, 70 cotizacion: $scope.cotizacionPorDefecto || {},
71 articulosFactura: [], 71 articulosFactura: [],
72 despachos: [] 72 despachos: []
73 }; 73 };
74 74
75 $scope.factura.articulosFactura = []; 75 $scope.factura.articulosFactura = [];
76 $scope.idLista = undefined; 76 $scope.idLista = undefined;
77 77
78 crearRemitoService.getNumeroRemito().then( 78 crearRemitoService.getNumeroRemito().then(
79 function (res) { 79 function (res) {
80 $scope.puntoVenta = rellenar(res.data.sucursal, 4); 80 $scope.puntoVenta = rellenar(res.data.sucursal, 4);
81 $scope.comprobante = rellenar(res.data.numeroRemito, 8); 81 $scope.comprobante = rellenar(res.data.numeroRemito, 8);
82 }, 82 },
83 function (err) { 83 function (err) {
84 focaModalService.alert('La terminal no esta configurada correctamente'); 84 focaModalService.alert('La terminal no esta configurada correctamente');
85 console.info(err); 85 console.info(err);
86 } 86 }
87 ); 87 );
88 88
89 $scope.inicial = angular.copy($scope.factura); 89 $scope.inicial = angular.copy($scope.factura);
90 } 90 }
91 91
92 $scope.$watch('factura', function (newValue) { 92 $scope.$watch('factura', function (newValue) {
93 focaBotoneraLateralService.setPausarData({ 93 focaBotoneraLateralService.setPausarData({
94 label: 'factura', 94 label: 'factura',
95 val: newValue 95 val: newValue
96 }); 96 });
97 }, true); 97 }, true);
98 98
99 $scope.seleccionarFormaDePago = function () { 99 $scope.seleccionarFormaDePago = function () {
100 if (!validarGuardar()) return; 100 if (!validarGuardar()) return;
101 var modalInstance = $uibModal.open( 101 var modalInstance = $uibModal.open(
102 { 102 {
103 templateUrl: 'modal-forma-pago.html', 103 templateUrl: 'modal-forma-pago.html',
104 controller: 'focaModalFormaPagoController', 104 controller: 'focaModalFormaPagoController',
105 resolve: { 105 resolve: {
106 filters: function () { return null; }, 106 parametros: function () {
107 return {
108 importe: $scope.getTotal()
109 };
110 },
107 }, 111 },
108 size: 'lg', 112 size: 'lg',
109 } 113 }
110 ); 114 );
111 modalInstance.result 115 modalInstance.result
112 .then(function (data) { 116 .then(function (data) {
113 console.log(data); 117
114 $scope.crearFactura(); 118 $scope.crearFactura(data);
115 }) 119 })
116 .catch(function (e) { console.error(e); }); 120 .catch(function (e) { console.error(e); });
117 }; 121 };
118 122
119 $scope.crearFactura = function () { 123 $scope.crearFactura = function (formaPago) {
120 124
121 var save = { 125 var save = {
122 factura: { 126 factura: {
123 127
124 BONIF: 0, 128 BONIF: 0,
125 CLI: $scope.factura.cliente.cod, 129 CLI: $scope.factura.cliente.cod,
126 CUI: $scope.factura.cliente.cuit, 130 CUI: $scope.factura.cliente.cuit,
127 CTA: $scope.factura.cliente.cod, 131 CTA: $scope.factura.cliente.cod,
128 DC1: '', 132 DC1: '',
129 DC2: '', 133 DC2: '',
130 DE1: '', 134 DE1: '',
131 DOM: $scope.factura.cliente.DOM, 135 DOM: $scope.factura.cliente.DOM,
132 FACAUT: 0, 136 FACAUT: 0,
133 DTO: 0, 137 DTO: 0,
134 FEC: $filter('date')($scope.now, 'yyyy-MM-dd HH:mm:ss'), 138 FEC: $filter('date')($scope.now, 'yyyy-MM-dd HH:mm:ss'),
135 FEC_ANT: '19000101', 139 FEC_ANT: '19000101',
136 FPA: 0, // 0 <- Múltiple formas de pago, 2 cta corriente 140 FPA: 0, // 0 <- Múltiple formas de pago, 2 cta corriente
137 IDEXCEPCION: 0, 141 IDEXCEPCION: 0,
138 IDLP: $scope.factura.cliente.mod.trim() || 0, 142 IDLP: $scope.factura.cliente.mod.trim() || 0,
139 IDPERSONERIA: 0, 143 IDPERSONERIA: 0,
140 IMI: 0, // TODO 144 IMI: 0, // TODO
141 IMI2: 0, // TODO 145 IMI2: 0, // TODO
142 IMI3: 0, // TODO 146 IMI3: 0, // TODO
143 IMP_LEY: 0, 147 IMP_LEY: 0,
144 IRI: 0, // TODO 148 IRI: 0, // TODO
145 IRS: 0, 149 IRS: 0,
146 LEG: '', 150 LEG: '',
147 LUG: $scope.factura.vendedor.LugVen, 151 LUG: $scope.factura.vendedor.LugVen,
148 MK_M: 0, 152 MK_M: 0,
149 NEE: 0, // TODO 153 NEE: 0, // TODO
150 NET: 0, // TODO 154 NET: 0, // TODO
151 NFI: '', 155 NFI: '',
152 NNP: 0, 156 NNP: 0,
153 NOM: $scope.factura.cliente.nom, 157 NOM: $scope.factura.cliente.nom,
154 OPE: $scope.factura.vendedor.CodVen, 158 OPE: $scope.factura.vendedor.CodVen,
155 PAG: $scope.getTotal(), 159 PAG: $scope.getTotal(),
156 PER: 0, 160 PER: 0,
157 PER_IVA: 0, 161 PER_IVA: 0,
158 PLA: $scope.factura.vendedor.NplVen, 162 PLA: $scope.factura.vendedor.NplVen,
159 PRO: '', 163 PRO: '',
160 REC_ANT: 0, 164 REC_ANT: 0,
161 SUC: parseInt($scope.puntoVenta), 165 SUC: parseInt($scope.puntoVenta),
162 TCA: 1, 166 TCA: 1,
163 TCO: 'FT', 167 TCO: 'FT',
164 TFI: '', 168 TFI: '',
165 TIP: $scope.factura.cliente.tipoFactura, 169 TIP: $scope.factura.cliente.tipoFactura,
166 TIV: 0, // TODO 170 TIV: 0, // TODO
167 TOT: $scope.getTotal(), 171 TOT: $scope.getTotal(),
168 TUR: 0, // TODO 172 TUR: 0, // TODO
169 VEN: $scope.factura.vendedor.CodVen, 173 VEN: $scope.factura.vendedor.CodVen,
170 VTO_CLI: '', 174 VTO_CLI: '',
171 ZON: 1, // TODO 175 ZON: 1, // TODO
172 OBSERVACIONES: $scope.factura.observaciones 176 OBSERVACIONES: $scope.factura.observaciones
173 }, 177 },
174 cuerpo: $scope.articulosFiltro(), 178 cuerpo: $scope.articulosFiltro(),
175 despachos: $scope.factura.despachos 179 despachos: $scope.factura.despachos,
180 formaPago: formaPago
176 }; 181 };
177 182
178 crearFacturaService.guardarFactura(save).then(function (res) { 183 crearFacturaService.guardarFactura(save).then(function (res) {
179 184
180 focaBotoneraLateralService.endGuardar(true); 185 focaBotoneraLateralService.endGuardar(true);
181 186
182 focaModalService.alert('Comprobante guardado con éxito'); 187 focaModalService.alert('Comprobante guardado con éxito');
183 188
184 config(); 189 config();
185 190
186 }).catch(function (err) { 191 }).catch(function (err) {
187 focaModalService.alert('Hubo un error al guardar la factura'); 192 focaModalService.alert('Hubo un error al guardar la factura');
188 console.log(err); 193 console.log(err);
189 }); 194 });
190 195
191 }; 196 };
192 197
193 $scope.seleccionarCliente = function () { 198 $scope.seleccionarCliente = function () {
194 199
195 var modalInstance = $uibModal.open( 200 var modalInstance = $uibModal.open(
196 { 201 {
197 ariaLabelledBy: 'Busqueda de Cliente', 202 ariaLabelledBy: 'Busqueda de Cliente',
198 templateUrl: 'foca-busqueda-cliente-modal.html', 203 templateUrl: 'foca-busqueda-cliente-modal.html',
199 controller: 'focaBusquedaClienteModalController', 204 controller: 'focaBusquedaClienteModalController',
200 resolve: { 205 resolve: {
201 vendedor: function () { return null; }, 206 vendedor: function () { return null; },
202 cobrador: function () { return null; } 207 cobrador: function () { return null; }
203 }, 208 },
204 size: 'lg' 209 size: 'lg'
205 } 210 }
206 ); 211 );
207 modalInstance.result.then( 212 modalInstance.result.then(
208 function (cliente) { 213 function (cliente) {
209 var modalInstance = $uibModal.open( 214 var modalInstance = $uibModal.open(
210 { 215 {
211 templateUrl: 'modal-estado-cuenta.html', 216 templateUrl: 'modal-estado-cuenta.html',
212 controller: 'focaModalEstadoCuentaController', 217 controller: 'focaModalEstadoCuentaController',
213 size: 'lg', 218 size: 'lg',
214 resolve: { 219 resolve: {
215 parametros: function () { return { idCliente: cliente.cod }; }, 220 parametros: function () { return { idCliente: cliente.cod }; },
216 } 221 }
217 } 222 }
218 ); 223 );
219 modalInstance.result 224 modalInstance.result
220 .then(function (data) { 225 .then(function (data) {
221 226
222 $scope.factura.cliente = cliente; 227 $scope.factura.cliente = cliente;
223 228
224 $scope.$broadcast('addCabecera', { 229 $scope.$broadcast('addCabecera', {
225 label: 'Cliente:', 230 label: 'Cliente:',
226 valor: $filter('rellenarDigitos')(cliente.cod, 3) + 231 valor: $filter('rellenarDigitos')(cliente.cod, 3) +
227 ' - ' + cliente.nom 232 ' - ' + cliente.nom
228 }); 233 });
229 234
230 $filter('filter')($scope.botonera, 235 $filter('filter')($scope.botonera,
231 { label: 'Cliente' })[0].checked = true; 236 { label: 'Cliente' })[0].checked = true;
232 237
233 $scope.$broadcast('addCabecera', { 238 $scope.$broadcast('addCabecera', {
234 label: 'Tipo de pago: ', 239 label: 'Tipo de pago: ',
235 valor: data 240 valor: data
236 }); 241 });
237 }) 242 })
238 .catch(function () { 243 .catch(function () {
239 244
240 $scope.seleccionarCliente(); 245 $scope.seleccionarCliente();
241 }); 246 });
242 247
243 }, function () { 248 }, function () {
244 } 249 }
245 ); 250 );
246 251
247 }; 252 };
248 253
249 $scope.seleccionarVendedor = function () { 254 $scope.seleccionarVendedor = function () {
250 var parametrosModal = { 255 var parametrosModal = {
251 titulo: 'Búsqueda vendedores', 256 titulo: 'Búsqueda vendedores',
252 query: '/vendedor-playa', 257 query: '/vendedor-playa',
253 columnas: [ 258 columnas: [
254 { 259 {
255 propiedad: 'CodVen', 260 propiedad: 'CodVen',
256 nombre: 'Código', 261 nombre: 'Código',
257 filtro: { 262 filtro: {
258 nombre: 'rellenarDigitos', 263 nombre: 'rellenarDigitos',
259 parametro: 3 264 parametro: 3
260 } 265 }
261 }, 266 },
262 { 267 {
263 propiedad: 'NomVen', 268 propiedad: 'NomVen',
264 nombre: 'Nombre' 269 nombre: 'Nombre'
265 } 270 }
266 ], 271 ],
267 size: 'md' 272 size: 'md'
268 }; 273 };
269 focaModalService.modal(parametrosModal).then( 274 focaModalService.modal(parametrosModal).then(
270 function (vendedor) { 275 function (vendedor) {
271 276
272 indicarPassword(vendedor) 277 indicarPassword(vendedor)
273 .then(function () { 278 .then(function () {
274 validarPlanillaVendedor(vendedor) 279 validarPlanillaVendedor(vendedor)
275 .then(function () { 280 .then(function () {
276 281
277 $filter('filter')($scope.botonera, { 282 $filter('filter')($scope.botonera, {
278 label: 'Vendedor' 283 label: 'Vendedor'
279 })[0].checked = true; 284 })[0].checked = true;
280 285
281 $scope.$broadcast('addCabecera', { 286 $scope.$broadcast('addCabecera', {
282 label: 'Vendedor:', 287 label: 'Vendedor:',
283 valor: $filter('rellenarDigitos')(vendedor.CodVen, 3) + 288 valor: $filter('rellenarDigitos')(vendedor.CodVen, 3) +
284 ' - ' + 289 ' - ' +
285 vendedor.NomVen 290 vendedor.NomVen
286 }); 291 });
287 292
288 $scope.factura.vendedor = vendedor; 293 $scope.factura.vendedor = vendedor;
289 294
290 getProductosByPlanilla(vendedor.NplVen); 295 getProductosByPlanilla(vendedor.NplVen);
291 }) 296 })
292 .catch($scope.seleccionarVendedor); 297 .catch($scope.seleccionarVendedor);
293 }) 298 })
294 .catch(function (err) { 299 .catch(function (err) {
295 console.log(err) 300 console.log(err)
296 }); 301 });
297 302
298 }, function () { } 303 }, function () { }
299 ); 304 );
300 }; 305 };
301 306
302 $scope.seleccionarMoneda = function () { 307 $scope.seleccionarMoneda = function () {
303 308
304 var parametrosModal = { 309 var parametrosModal = {
305 titulo: 'Búsqueda de monedas', 310 titulo: 'Búsqueda de monedas',
306 query: '/moneda', 311 query: '/moneda',
307 columnas: [ 312 columnas: [
308 { 313 {
309 propiedad: 'DETALLE', 314 propiedad: 'DETALLE',
310 nombre: 'Nombre' 315 nombre: 'Nombre'
311 }, 316 },
312 { 317 {
313 propiedad: 'SIMBOLO', 318 propiedad: 'SIMBOLO',
314 nombre: 'Símbolo' 319 nombre: 'Símbolo'
315 } 320 }
316 ], 321 ],
317 size: 'md' 322 size: 'md'
318 }; 323 };
319 focaModalService.modal(parametrosModal).then( 324 focaModalService.modal(parametrosModal).then(
320 function (moneda) { 325 function (moneda) {
321 326
322 if (moneda.ID !== 1) { 327 if (moneda.ID !== 1) {
323 $scope.abrirModalCotizacion(moneda); 328 $scope.abrirModalCotizacion(moneda);
324 return; 329 return;
325 } 330 }
326 331
327 crearRemitoService.getCotizacionByIdMoneda(1) 332 crearRemitoService.getCotizacionByIdMoneda(1)
328 .then(function (res) { 333 .then(function (res) {
329 334
330 cotizacionPArgentino = res.data[0].cotizaciones[0]; 335 cotizacionPArgentino = res.data[0].cotizaciones[0];
331 cotizacionPArgentino.moneda = moneda; 336 cotizacionPArgentino.moneda = moneda;
332 337
333 actualizarCabeceraMoneda(cotizacionPArgentino); 338 actualizarCabeceraMoneda(cotizacionPArgentino);
334 339
335 $scope.remito.cotizacion = cotizacionPArgentino; 340 $scope.remito.cotizacion = cotizacionPArgentino;
336 }); 341 });
337 }, function () { 342 }, function () {
338 343
339 } 344 }
340 ); 345 );
341 }; 346 };
342 347
343 $scope.abrirModalCotizacion = function (moneda) { 348 $scope.abrirModalCotizacion = function (moneda) {
344 var modalInstance = $uibModal.open( 349 var modalInstance = $uibModal.open(
345 { 350 {
346 ariaLabelledBy: 'Busqueda de Cotización', 351 ariaLabelledBy: 'Busqueda de Cotización',
347 templateUrl: 'modal-cotizacion.html', 352 templateUrl: 'modal-cotizacion.html',
348 controller: 'focaModalCotizacionController', 353 controller: 'focaModalCotizacionController',
349 size: 'lg', 354 size: 'lg',
350 resolve: { idMoneda: function () { return moneda.ID; } } 355 resolve: { idMoneda: function () { return moneda.ID; } }
351 } 356 }
352 ); 357 );
353 modalInstance.result.then( 358 modalInstance.result.then(
354 function (cotizacion) { 359 function (cotizacion) {
355 360
356 cotizacion.moneda = moneda; 361 cotizacion.moneda = moneda;
357 actualizarCabeceraMoneda(cotizacion); 362 actualizarCabeceraMoneda(cotizacion);
358 $scope.factura.cotizacion = cotizacion; 363 $scope.factura.cotizacion = cotizacion;
359 364
360 }, function () { 365 }, function () {
361 366
362 } 367 }
363 ); 368 );
364 }; 369 };
365 370
366 $scope.seleccionarObservaciones = function () { 371 $scope.seleccionarObservaciones = function () {
367 var observacion = { 372 var observacion = {
368 titulo: 'Ingrese Observaciones', 373 titulo: 'Ingrese Observaciones',
369 value: $scope.factura.observaciones, 374 value: $scope.factura.observaciones,
370 maxlength: 155, 375 maxlength: 155,
371 textarea: true 376 textarea: true
372 }; 377 };
373 378
374 focaModalService 379 focaModalService
375 .prompt(observacion) 380 .prompt(observacion)
376 .then(function (observaciones) { 381 .then(function (observaciones) {
377 $scope.factura.observaciones = observaciones; 382 $scope.factura.observaciones = observaciones;
378 }); 383 });
379 }; 384 };
380 385
381 $scope.articulosFiltro = function () { 386 $scope.articulosFiltro = function () {
382 return $scope.factura.articulosFactura.filter(function (articulo) { 387 return $scope.factura.articulosFactura.filter(function (articulo) {
383 return !articulo.desactivado; 388 return !articulo.desactivado;
384 }); 389 });
385 }; 390 };
386 391
387 $scope.getTotal = function () { 392 $scope.getTotal = function () {
388 393
389 var total = 0; 394 var total = 0;
390 395
391 $scope.articulosFiltro().forEach(function (articulo) { 396 $scope.articulosFiltro().forEach(function (articulo) {
392 total += articulo.PUN * articulo.CAN; 397 total += articulo.PUN * articulo.CAN;
393 }); 398 });
394 399
395 return parseFloat(total.toFixed(2)); 400 return parseFloat(total.toFixed(2));
396 }; 401 };
397 402
398 $scope.quitarDespacho = function (articulo, index) { 403 $scope.quitarDespacho = function (articulo, index) {
399 404
400 crearFacturaService 405 crearFacturaService
401 .setearDespachoDesocupado({ 406 .setearDespachoDesocupado({
402 surtidor: $scope.factura.despachos[index].SUR, 407 surtidor: $scope.factura.despachos[index].SUR,
403 producto: $scope.factura.despachos[index].PRO, 408 producto: $scope.factura.despachos[index].PRO,
404 carga: $scope.factura.despachos[index].CAR, 409 carga: $scope.factura.despachos[index].CAR,
405 planilla: $scope.factura.despachos[index].PLA 410 planilla: $scope.factura.despachos[index].PLA
406 }) 411 })
407 .then(function () { 412 .then(function () {
408 articulo.desactivado = true; 413 articulo.desactivado = true;
409 $scope.factura.despachos.splice(index, 1); 414 $scope.factura.despachos.splice(index, 1);
410 }) 415 })
411 .catch(function () { 416 .catch(function () {
412 focaModalService.alert('Hubo un error al desasociar este despacho'); 417 focaModalService.alert('Hubo un error al desasociar este despacho');
413 }); 418 });
414 }; 419 };
415 420
416 function getProductosByPlanilla(numeroPlanilla) { 421 function getProductosByPlanilla(numeroPlanilla) {
417 422
418 crearFacturaService.getProductosByPlanilla(numeroPlanilla).then(function (res) { 423 crearFacturaService.getProductosByPlanilla(numeroPlanilla).then(function (res) {
419 424
420 res.data.forEach(function (producto) { 425 res.data.forEach(function (producto) {
421 426
422 $scope.botonera.push({ 427 $scope.botonera.push({
423 label: producto.DetArt, 428 label: producto.DetArt,
424 image: 'productos.png' 429 image: 'productos.png'
425 }); 430 });
426 431
427 432
428 crearFuncionesProductos(producto); 433 crearFuncionesProductos(producto);
429 434
430 }); 435 });
431 }); 436 });
432 } 437 }
433 438
434 function crearFuncionesProductos(producto) { 439 function crearFuncionesProductos(producto) {
435 440
436 $scope[nombreFuncion(producto.DetArt)] = function () { 441 $scope[nombreFuncion(producto.DetArt)] = function () {
437 442
438 if (angular.equals($scope.factura.cliente, {})) { 443 if (angular.equals($scope.factura.cliente, {})) {
439 focaModalService.alert('Seleccione cliente'); 444 focaModalService.alert('Seleccione cliente');
440 return; 445 return;
441 } 446 }
442 447
443 var modalInstance = $uibModal.open( 448 var modalInstance = $uibModal.open(
444 { 449 {
445 templateUrl: 'modal-combustibles.html', 450 templateUrl: 'modal-combustibles.html',
446 controller: 'focaModalCombustiblesController', 451 controller: 'focaModalCombustiblesController',
447 resolve: { 452 resolve: {
448 parametros: function () { 453 parametros: function () {
449 return { 454 return {
450 despachos: producto.despachos, 455 despachos: producto.despachos,
451 nombreProducto: producto.DetArt 456 nombreProducto: producto.DetArt
452 } 457 }
453 } 458 }
454 }, 459 },
455 size: 'md' 460 size: 'md'
456 } 461 }
457 ); 462 );
458 463
459 modalInstance.result.then(function (despacho) { 464 modalInstance.result.then(function (despacho) {
460 465
461 var articulo = { 466 var articulo = {
462 TIP: $scope.factura.cliente.tipoFactura, 467 TIP: $scope.factura.cliente.tipoFactura,
463 TCO: 'FT', 468 TCO: 'FT',
464 SUC: parseInt($scope.puntoVenta), 469 SUC: parseInt($scope.puntoVenta),
465 ORD: $scope.articulosFiltro().length + 1, 470 ORD: $scope.articulosFiltro().length + 1,
466 SEC: despacho.SEC, 471 SEC: despacho.SEC,
467 ART: despacho.PRO, 472 ART: despacho.PRO,
468 RUB: producto.CodRub, 473 RUB: producto.CodRub,
469 DES: producto.DetArt, 474 DES: producto.DetArt,
470 CAN: despacho.LTS, 475 CAN: despacho.LTS,
471 PUN: producto.PreVen, // TODO 476 PUN: producto.PreVen, // TODO
472 IVA: producto.IMPIVA, // TODO 477 IVA: producto.IMPIVA, // TODO
473 NET: 0, // TODO 478 NET: 0, // TODO
474 NEX: 0, // TODO 479 NEX: 0, // TODO
475 IMI: producto.ImpInt, // TODO 480 IMI: producto.ImpInt, // TODO
476 IMI2: producto.ImpInt2, // TODO 481 IMI2: producto.ImpInt2, // TODO
477 IMI3: producto.ImpInt3, // TODO 482 IMI3: producto.ImpInt3, // TODO
478 PUT: producto.PreNet, // TODO 483 PUT: producto.PreNet, // TODO
479 SUR: despacho.SUR, 484 SUR: despacho.SUR,
480 PLA: despacho.PLA, 485 PLA: despacho.PLA,
481 LUG: despacho.LUG, 486 LUG: despacho.LUG,
482 LEG: $scope.factura.vendedor.CodVen, 487 LEG: $scope.factura.vendedor.CodVen,
483 TUR: $scope.factura.vendedor.TurVen, 488 TUR: $scope.factura.vendedor.TurVen,
484 ORDEN_PRECOMPRA: '', 489 ORDEN_PRECOMPRA: '',
485 ESC: producto.tipoFactura == 'L' ? 1 : 0, 490 ESC: producto.tipoFactura == 'L' ? 1 : 0,
486 CMF: 0, 491 CMF: 0,
487 PTA: 0, 492 PTA: 0,
488 IVS: 0, 493 IVS: 0,
489 TIVA: 0, 494 TIVA: 0,
490 CON: 0, 495 CON: 0,
491 SINO: '', 496 SINO: '',
492 ORD_TRA: 0, 497 ORD_TRA: 0,
493 IMP_DESP: 0, 498 IMP_DESP: 0,
494 PCD: 0, 499 PCD: 0,
495 RTO: '' 500 RTO: ''
496 }; 501 };
497 502
498 crearFacturaService.setearDespachoOcupado({ 503 crearFacturaService.setearDespachoOcupado({
499 surtidor: despacho.SUR, 504 surtidor: despacho.SUR,
500 producto: despacho.PRO, 505 producto: despacho.PRO,
501 carga: despacho.CAR 506 carga: despacho.CAR
502 }) 507 })
503 .then(function () { 508 .then(function () {
504 $scope.factura.articulosFactura.push(articulo); 509 $scope.factura.articulosFactura.push(articulo);
505 $scope.factura.despachos.push(despacho); 510 $scope.factura.despachos.push(despacho);
506 }) 511 })
507 .catch(function (err) { 512 .catch(function (err) {
508 513
509 focaModalService.alert('El despacho esta en uso'); 514 focaModalService.alert('El despacho esta en uso');
510 }); 515 });
511 516
512 }) 517 })
513 .catch(function (err) { 518 .catch(function (err) {
514 console.log(err); 519 console.log(err);
515 }); 520 });
516 521
517 }; 522 };
518 } 523 }
519 524
520 function nombreFuncion(string) { 525 function nombreFuncion(string) {
521 var texto = 'seleccionar'; 526 var texto = 'seleccionar';
522 var arr = string.split(' '); 527 var arr = string.split(' ');
523 arr.forEach(function (palabra) { 528 arr.forEach(function (palabra) {
524 palabra = palabra.charAt(0).toUpperCase() + palabra.slice(1); 529 palabra = palabra.charAt(0).toUpperCase() + palabra.slice(1);
525 texto += palabra; 530 texto += palabra;
526 }); 531 });
527 return texto; 532 return texto;
528 } 533 }
529 534
530 function indicarPassword(vendedor) { 535 function indicarPassword(vendedor) {
531 536
532 return new Promise(function (resolve, reject) { 537 return new Promise(function (resolve, reject) {
533 538
534 focaModalService 539 focaModalService
535 .prompt({ 540 .prompt({
536 titulo: 'Indique Contraseña', 541 titulo: 'Indique Contraseña',
537 value: '' 542 value: ''
538 }) 543 })
539 .then(function (contraseña) { 544 .then(function (contraseña) {
540 545
541 if (contraseña != vendedor.ClaVen.trim()) { 546 if (contraseña != vendedor.ClaVen.trim()) {
542 547
543 focaModalService.alert('Clave incorrecta').then(function () { 548 focaModalService.alert('Clave incorrecta').then(function () {
544 indicarPassword(vendedor); 549 indicarPassword(vendedor);
545 }); 550 });
546 } else { 551 } else {
547 resolve(); 552 resolve();
548 } 553 }
549 }) 554 })
550 .catch(reject); 555 .catch(reject);
551 }); 556 });
552 } 557 }
553 558
554 function validarPlanillaVendedor(vendedor) { 559 function validarPlanillaVendedor(vendedor) {
555 560
556 return new Promise(function (resolve, reject) { 561 return new Promise(function (resolve, reject) {
557 562
558 crearFacturaService 563 crearFacturaService
559 .validarPlanillaVendedor(vendedor.CodVen.trim()) 564 .validarPlanillaVendedor(vendedor.CodVen.trim())
560 .then(function (res) { 565 .then(function (res) {
561 566
562 if (!res.data.length) { 567 if (!res.data.length) {
563 568
564 focaModalService 569 focaModalService
565 .alert('No se encontró planilla abierta para el vendedor') 570 .alert('No se encontró planilla abierta para el vendedor')
566 .then(reject); 571 .then(reject);
567 572
568 } else { 573 } else {
569 resolve(); 574 resolve();
570 } 575 }
571 576
572 }) 577 })
573 .catch(reject); 578 .catch(reject);
574 }); 579 });
575 } 580 }
576 581
577 function rellenar(relleno, longitud) { 582 function rellenar(relleno, longitud) {
578 relleno = '' + relleno; 583 relleno = '' + relleno;
579 while (relleno.length < longitud) { 584 while (relleno.length < longitud) {
580 relleno = '0' + relleno; 585 relleno = '0' + relleno;
581 } 586 }
582 return relleno; 587 return relleno;
583 } 588 }
584 589
585 function salir() { 590 function salir() {
586 var confirmacion = false; 591 var confirmacion = false;
587 592
588 if (!angular.equals($scope.factura, $scope.inicial)) { 593 if (!angular.equals($scope.factura, $scope.inicial)) {
589 confirmacion = true; 594 confirmacion = true;
590 } 595 }
591 596
592 if (confirmacion) { 597 if (confirmacion) {
593 focaModalService.confirm( 598 focaModalService.confirm(
594 '¿Está seguro de que desea salir? Se perderán todos los datos cargados.' 599 '¿Está seguro de que desea salir? Se perderán todos los datos cargados.'
595 ).then(function (data) { 600 ).then(function (data) {
596 if (data) { 601 if (data) {
597 $location.path('/'); 602 $location.path('/');
598 } 603 }
599 }); 604 });
600 } else { 605 } else {
601 $location.path('/'); 606 $location.path('/');
602 } 607 }
603 } 608 }
604 609
605 function setearFactura(factura) { 610 function setearFactura(factura) {
606 611
607 $scope.$broadcast('cleanCabecera'); 612 $scope.$broadcast('cleanCabecera');
608 613
609 $scope.cabeceras = []; 614 $scope.cabeceras = [];
610 615
611 if (factura.cotizacion && factura.cotizacion.moneda.CODIGO_AFIP !== 'PES') { 616 if (factura.cotizacion && factura.cotizacion.moneda.CODIGO_AFIP !== 'PES') {
612 $scope.cabeceras.push({ 617 $scope.cabeceras.push({
613 label: 'Moneda:', 618 label: 'Moneda:',
614 valor: factura.cotizacion.moneda.DETALLE 619 valor: factura.cotizacion.moneda.DETALLE
615 }); 620 });
616 $scope.cabeceras.push({ 621 $scope.cabeceras.push({
617 label: 'Fecha cotizacion:', 622 label: 'Fecha cotizacion:',
618 valor: $filter('date')(factura.cotizacion.FECHA, 623 valor: $filter('date')(factura.cotizacion.FECHA,
619 'dd/MM/yyyy') 624 'dd/MM/yyyy')
620 }); 625 });
621 $scope.cabeceras.push({ 626 $scope.cabeceras.push({
622 label: 'Cotizacion:', 627 label: 'Cotizacion:',
623 valor: $filter('number')(factura.cotizacion.VENDEDOR, 628 valor: $filter('number')(factura.cotizacion.VENDEDOR,
624 '2') 629 '2')
625 }); 630 });
626 } 631 }
627 632
628 if (factura.cotizacion && factura.cotizacion.moneda) { 633 if (factura.cotizacion && factura.cotizacion.moneda) {
629 $filter('filter')($scope.botonera, { label: 'Moneda' })[0].checked = true; 634 $filter('filter')($scope.botonera, { label: 'Moneda' })[0].checked = true;
630 } 635 }
631 636
632 if (factura.cliente && factura.cliente.cod) { 637 if (factura.cliente && factura.cliente.cod) {
633 $scope.cabeceras.push({ 638 $scope.cabeceras.push({
634 label: 'Cliente:', 639 label: 'Cliente:',
635 valor: $filter('rellenarDigitos')(factura.cliente.cod, 3) + ' - ' + 640 valor: $filter('rellenarDigitos')(factura.cliente.cod, 3) + ' - ' +
636 factura.cliente.nom 641 factura.cliente.nom
637 }); 642 });
638 643
639 $filter('filter')($scope.botonera, { label: 'Cliente' })[0].checked = true; 644 $filter('filter')($scope.botonera, { label: 'Cliente' })[0].checked = true;
640 } 645 }
641 646
642 $scope.puntoVenta = rellenar(factura.sucursal, 4); 647 $scope.puntoVenta = rellenar(factura.sucursal, 4);
643 $scope.comprobante = rellenar(factura.numerofactura, 8); 648 $scope.comprobante = rellenar(factura.numerofactura, 8);
644 $scope.factura = factura; 649 $scope.factura = factura;
645 650
646 addArrayCabecera($scope.cabeceras); 651 addArrayCabecera($scope.cabeceras);
647 } 652 }
648 653
649 function getLSFactura() { 654 function getLSFactura() {
650 var factura = JSON.parse($localStorage.factura || null); 655 var factura = JSON.parse($localStorage.factura || null);
651 if (factura) { 656 if (factura) {
652 setearFactura(factura); 657 setearFactura(factura);
653 delete $localStorage.factura; 658 delete $localStorage.factura;
654 } 659 }
655 } 660 }
656 661
657 function addArrayCabecera(array) { 662 function addArrayCabecera(array) {
658 for (var i = 0; i < array.length; i++) { 663 for (var i = 0; i < array.length; i++) {
659 $scope.$broadcast('addCabecera', { 664 $scope.$broadcast('addCabecera', {
660 label: array[i].label, 665 label: array[i].label,
661 valor: array[i].valor 666 valor: array[i].valor
662 }); 667 });
663 } 668 }
664 } 669 }
665 670
666 function actualizarCabeceraMoneda(cotizacion) { 671 function actualizarCabeceraMoneda(cotizacion) {
667 672
668 $scope.factura.articulosFactura.forEach(function (art) { 673 $scope.factura.articulosFactura.forEach(function (art) {
669 art.PUN = (art.PUN * $scope.factura.cotizacion.VENDEDOR).toFixed(4); 674 art.PUN = (art.PUN * $scope.factura.cotizacion.VENDEDOR).toFixed(4);
670 art.PUN = (art.PUN / cotizacion.VENDEDOR).toFixed(4); 675 art.PUN = (art.PUN / cotizacion.VENDEDOR).toFixed(4);
671 }); 676 });
672 677
673 if (cotizacion.moneda.DETALLE === 'PESOS ARGENTINOS') { 678 if (cotizacion.moneda.DETALLE === 'PESOS ARGENTINOS') {
674 $scope.$broadcast('removeCabecera', 'Moneda:'); 679 $scope.$broadcast('removeCabecera', 'Moneda:');
675 $scope.$broadcast('removeCabecera', 'Fecha cotizacion:'); 680 $scope.$broadcast('removeCabecera', 'Fecha cotizacion:');
676 $scope.$broadcast('removeCabecera', 'Cotizacion:'); 681 $scope.$broadcast('removeCabecera', 'Cotizacion:');
677 } else { 682 } else {
678 $scope.$broadcast('addCabecera', { 683 $scope.$broadcast('addCabecera', {
679 label: 'Moneda:', 684 label: 'Moneda:',
680 valor: cotizacion.moneda.DETALLE 685 valor: cotizacion.moneda.DETALLE
681 }); 686 });
682 $scope.$broadcast('addCabecera', { 687 $scope.$broadcast('addCabecera', {
683 label: 'Fecha cotizacion:', 688 label: 'Fecha cotizacion:',
684 valor: $filter('date')(cotizacion.FECHA, 'dd/MM/yyyy') 689 valor: $filter('date')(cotizacion.FECHA, 'dd/MM/yyyy')
685 }); 690 });
686 $scope.$broadcast('addCabecera', { 691 $scope.$broadcast('addCabecera', {
687 label: 'Cotizacion:', 692 label: 'Cotizacion:',
688 valor: $filter('number')(cotizacion.VENDEDOR, '2') 693 valor: $filter('number')(cotizacion.VENDEDOR, '2')
689 }); 694 });
690 } 695 }
691 } 696 }
692 697
693 function validarGuardar() { 698 function validarGuardar() {
694 699
695 if (angular.equals({}, $scope.factura.vendedor)) { 700 if (angular.equals({}, $scope.factura.vendedor)) {
696 701
697 focaModalService.alert('Seleccione Vendedor'); 702 focaModalService.alert('Seleccione Vendedor');
698 return false; 703 return false;
699 } else if (angular.equals({}, $scope.factura.cliente)) { 704 } else if (angular.equals({}, $scope.factura.cliente)) {
700 705
701 focaModalService.alert('Seleccione Cliente') 706 focaModalService.alert('Seleccione Cliente')
702 return false; 707 return false;
703 } else if (!$scope.articulosFiltro().length) { 708 } else if (!$scope.articulosFiltro().length) {
704 709
705 focaModalService.alert('Seleccione al menos un Articulo'); 710 focaModalService.alert('Seleccione al menos un Articulo');
706 return false; 711 return false;
707 } 712 }
708 713
709 return true; 714 return true;
710 } 715 }
711 } 716 }
712 ]); 717 ]);
713 718