Commit 8f055fb560bd3fc2db2b8bf108b28f25150515b0

Authored by Eric Fernandez
1 parent 2273314362
Exists in master and in 2 other branches develop, lab

Lógica guardado

Showing 1 changed file with 109 additions and 38 deletions   Show diff stats
src/js/controller.js
... ... @@ -4,62 +4,133 @@ angular.module('focaModalDetalleHojaRuta')
4 4 '$filter',
5 5 '$scope',
6 6 '$uibModalInstance',
7   - 'remito',
  7 + 'idRemito',
8 8 'focaModalDetalleHojaRutaService',
9   - function($filter, $scope, $uibModalInstance, remito, focaModalDetalleHojaRutaService) {
10   - $scope.remito = remito;
  9 + 'focaModalService',
  10 + 'focaSeguimientoService',
  11 + function($filter, $scope, $uibModalInstance, idRemito, focaModalDetalleHojaRutaService,
  12 + focaModalService, focaSeguimientoService)
  13 + {
  14 + //Seteo variables
  15 + $scope.remito = {};
11 16 $scope.cisternas = [];
12 17 $scope.articuloSeleccionado = {};
  18 + $scope.aDescargar = [];
  19 + $scope.cargando = true;
  20 +
  21 + //Datos
  22 + var promesaRemito = focaModalDetalleHojaRutaService
  23 + .getRemitoById(idRemito);
  24 + var promesaCisternas = focaModalDetalleHojaRutaService
  25 + .getCisternasByIdRemito(idRemito);
  26 +
  27 + Promise.all([promesaRemito, promesaCisternas]).then(function(res) {
  28 + $scope.cargando = false;
  29 + $scope.remito = res[0].data;
  30 + $scope.cisternas = res[1].data;
  31 + $scope.cambio($scope.remito.articulosRemito[0]);
  32 + $scope.$digest();
  33 + }, function() {
  34 + focaModalService.alert('El servicio no responde intente más tarde');
  35 + $uibModalInstance.dismiss();
  36 + });
13 37 $scope.cambio = function(articulo) {
  38 + $scope.aDescargar = [];
14 39 $scope.articuloSeleccionado = articulo;
15 40 };
16   - focaModalDetalleHojaRutaService.getCisternasByIdRemito($scope.remito.id)
17   - .then(function(res) {
18   - $scope.cisternas = res.data;
19   - });
20   -
21   - $scope.aceptar = function() {
22   - var articulos = articulosDescargados();
23   - var cisternaMovimientos = [];
  41 + $scope.descargar = function(key) {
24 42 var hojaRutaMovimientos = [];
25   - for(var i = 0; i < articulos.length; i++) {
26   - var cisternaMovimiento = {
27   - idRemito: $scope.remito.id,
28   - cantidad: articulos[i].aCargar,
29   - metodo: 'descarga'
30   - };
31   - var hojaRutaMovimiento = {
32   - idRemito: $scope.remito.id,
33   - reciboDescarga: articulos[i].numeroRecibo
  43 + var cisternaMovimientos = [];
  44 + var cisternaCargas = [];
  45 + var totalADescargar = 0;
  46 + for (var i = 0; i < $scope.aDescargar.length; i++) {
  47 + totalADescargar += $scope.aDescargar[i] || 0;
  48 + }
  49 + if(key === 13) {
  50 + focaModalService
  51 + .confirm('¿Desea descargar ' + totalADescargar + ' litros de ' +
  52 + $scope.articuloSeleccionado.descripcion + '?')
  53 + .then(descargar);
  54 + }
  55 + function descargar() {
  56 + for (var i = 0; i < $scope.cisternas.length; i++) {
  57 + var descarga = $scope.aDescargar[i];
  58 + if(!descarga) continue;
  59 + var cisternaCarga = $scope.cisternas[i].cisternaCarga;
  60 + cisternaCarga.cantidad -= descarga;
  61 +
  62 + //Guardar
  63 + var now = new Date();
  64 + var cisternaMovimiento = {
  65 + fecha: now.toISOString().slice(0, 19).replace('T', ' '),
  66 + cantidad: descarga,
  67 + metodo: 'descarga',
  68 + idCisternaCarga: cisternaCarga.id,
  69 + idRemito: $scope.remito.id
  70 + };
  71 + var hojaRutaMovimiento = {
  72 + reciboDescarga: $scope.numeroRecibo,
  73 + idRemito: $scope.remito.id
  74 + };
  75 + delete cisternaCarga.articulo;
  76 + cisternaCargas.push(cisternaCarga);
  77 + cisternaMovimientos.push(cisternaMovimiento);
  78 + hojaRutaMovimientos.push(hojaRutaMovimiento);
  79 + }
  80 + var save = {
  81 + cisternaCargas: cisternaCargas,
  82 + cisternaMovimientos: cisternaMovimientos,
  83 + hojaRutaMovimientos: hojaRutaMovimientos
34 84 };
35   - cisternaMovimientos.push(cisternaMovimiento);
36   - hojaRutaMovimientos.push(hojaRutaMovimiento);
  85 + focaModalDetalleHojaRutaService
  86 + .postMovimientoHojaRuta(save)
  87 + .then(guardarSeguimiento)
  88 + .catch(error);
  89 + function guardarSeguimiento(res) {
  90 + focaSeguimientoService
  91 + .guardarPosicion(
  92 + 'Entrega de producto',
  93 + res.data[0].id,
  94 + $scope.remito.observaciones);
  95 + $scope.aDescargar = [];
  96 + $scope.remito.observaciones = '';
  97 + success();
  98 + }
37 99 }
38   - var result = {
39   - cisternaMovimientos: cisternaMovimientos,
40   - hojaRutaMovimientos: hojaRutaMovimientos,
41   - idRemito: $scope.remito.id,
42   - observaciones: $scope.remito.observaciones
43   - };
44   - $uibModalInstance.close(result);
45 100 };
46 101  
47 102 $scope.cancel = function() {
48 103 $uibModalInstance.dismiss('cancel');
49 104 };
50 105  
51   - $scope.validar = function() {
52   - return !articulosDescargados().length;
  106 + $scope.distribucionDisponible = function() {
  107 + return $scope.articuloSeleccionado.cantidadDescargada ===
  108 + $scope.articuloSeleccionado.cantidad;
  109 + };
  110 +
  111 + $scope.actualizarArticulo = function() {
  112 + $scope.articuloSeleccionado.cantidadDescargada = 0;
  113 + for(var i = 0; i < $scope.aDescargar.length; i++) {
  114 + $scope.articuloSeleccionado.cantidadDescargada +=
  115 + parseFloat($scope.aDescargar[i]) || 0;
  116 + }
53 117 };
54 118  
55   - function articulosDescargados() {
56   - var articulosDescargados = $scope.remito.articulosRemito.filter(
57   - function(articulo) {
58   - if(articulo.aCargar && articulo.numeroRecibo) {
59   - return articulo;
60   - }
  119 + $scope.rechazar = function() {
  120 + focaModalService
  121 + .prompt('Aclare el motivo de rechazo')
  122 + .then(function() {
  123 + focaModalService.alert('desarrollo');
  124 + $uibModalInstance.dismiss();
61 125 });
62   - return articulosDescargados;
  126 + };
  127 +
  128 + //funciones
  129 + function error(error) {
  130 + focaModalService.alert('Hubo un error ' + error);
  131 + }
  132 + function success() {
  133 + focaModalService.alert('Operación realizada con éxito');
63 134 }
64 135 }
65 136 ]