Commit fda5e31ffb6242705dc8ceb0d868c85a2c248347

Authored by Marcelo Puebla
Exists in master and in 1 other branch develop

Merge branch 'develop' of git.focasoftware.com:npm/foca-crear-factura

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