Commit 611712f62d8527807943c32898327c5164ce8819

Authored by Eric Fernandez
Exists in master

Merge branch 'master' into 'master'

Master(efernandez)

See merge request !5
src/js/controller.js
1 angular.module('focaModalDetalleHojaRuta') 1 angular.module('focaModalDetalleHojaRuta')
2 .controller('focaModalDetalleHojaRutaController', 2 .controller('focaModalDetalleHojaRutaController',
3 [ 3 [
4 '$filter', 4 '$filter',
5 '$scope', 5 '$scope',
6 '$uibModalInstance', 6 '$uibModalInstance',
7 'idRemito', 7 'idRemito',
8 'focaModalDetalleHojaRutaService', 8 'focaModalDetalleHojaRutaService',
9 'focaModalService', 9 'focaModalService',
10 'focaSeguimientoService', 10 'focaSeguimientoService',
11 function($filter, $scope, $uibModalInstance, idRemito, focaModalDetalleHojaRutaService, 11 function($filter, $scope, $uibModalInstance, idRemito, focaModalDetalleHojaRutaService,
12 focaModalService, focaSeguimientoService) 12 focaModalService, focaSeguimientoService)
13 { 13 {
14 //Seteo variables 14 //Seteo variables
15 $scope.remito = {}; 15 $scope.remito = {};
16 $scope.cisternas = []; 16 $scope.cisternas = [];
17 $scope.articuloSeleccionado = {}; 17 $scope.articuloSeleccionado = {};
18 $scope.aDescargar = []; 18 $scope.aDescargar = [];
19 $scope.cargando = true; 19 $scope.cargando = true;
20 20
21 //Datos 21 //Datos
22 var promesaRemito = focaModalDetalleHojaRutaService 22 var promesaRemito = focaModalDetalleHojaRutaService
23 .getRemitoById(idRemito); 23 .getRemitoById(idRemito);
24 var promesaCisternas = focaModalDetalleHojaRutaService 24 var promesaCisternas = focaModalDetalleHojaRutaService
25 .getCisternasByIdRemito(idRemito); 25 .getCisternasByIdRemito(idRemito);
26 26
27 Promise.all([promesaRemito, promesaCisternas]).then(function(res) { 27 Promise.all([promesaRemito, promesaCisternas]).then(function(res) {
28 $scope.cargando = false; 28 $scope.cargando = false;
29 $scope.remito = res[0].data; 29 $scope.remito = res[0].data;
30 $scope.cisternas = res[1].data; 30 $scope.cisternas = res[1].data;
31 var articuloAChequear = $scope.remito.articulosRemito.filter( 31 var articuloAChequear = $scope.remito.articulosRemito.filter(
32 function(articulo) { 32 function(articulo) {
33 return !articulo.descargado; 33 return !articulo.descargado;
34 }); 34 });
35 if(!articuloAChequear.length || $scope.remito.rechazado) { 35 if(!articuloAChequear.length || $scope.remito.rechazado) {
36 $scope.readonly = true; 36 $scope.readonly = true;
37 $scope.cambio($scope.remito.articulosRemito[0]); 37 $scope.cambio($scope.remito.articulosRemito[0]);
38 }else { 38 }else {
39 $scope.cambio(articuloAChequear[0]); 39 $scope.cambio(articuloAChequear[0]);
40 } 40 }
41 $scope.$digest(); 41 $scope.$digest();
42 }, function() { 42 }, function() {
43 focaModalService.alert('El servicio no responde intente más tarde'); 43 focaModalService.alert('El servicio no responde intente más tarde');
44 $uibModalInstance.dismiss(); 44 $uibModalInstance.dismiss();
45 }); 45 });
46 $scope.cambio = function(articulo) { 46 $scope.cambio = function(articulo) {
47 if(!$scope.articuloSeleccionado.descargado) { 47 if(!$scope.articuloSeleccionado.descargado) {
48 $scope.articuloSeleccionado.cantidadDescargada = 0; 48 $scope.articuloSeleccionado.cantidadDescargada = 0;
49 } 49 }
50 $scope.aDescargar = []; 50 $scope.aDescargar = [];
51 $scope.articuloSeleccionado = articulo; 51 $scope.articuloSeleccionado = articulo;
52 }; 52 };
53 $scope.descargar = function(key) { 53 $scope.descargar = function(key) {
54 if(key === 13) { 54 if(key === 13) {
55 $scope.cargando = true; 55 $scope.cargando = true;
56 var hojaRutaMovimientos = []; 56 var hojaRutaMovimientos = [];
57 var cisternaMovimientos = []; 57 var cisternaMovimientos = [];
58 var cisternaCargas = []; 58 var cisternaCargas = [];
59 var totalADescargar = 0; 59 var totalADescargar = 0;
60 for(var i = 0; i < $scope.aDescargar.length; i++) { 60 for(var i = 0; i < $scope.aDescargar.length; i++) {
61 totalADescargar += $scope.aDescargar[i] || 0; 61 totalADescargar += $scope.aDescargar[i] || 0;
62 } 62 }
63 focaModalService 63 focaModalService
64 .confirm('¿Desea descargar ' + totalADescargar + ' litros de ' + 64 .confirm('¿Desea descargar ' + totalADescargar + ' litros de ' +
65 $scope.articuloSeleccionado.descripcion + '?') 65 $scope.articuloSeleccionado.descripcion + '?')
66 .then(descargar); 66 .then(descargar, function() {
67 $scope.cargando = false;
68 });
67 } 69 }
68 function descargar() { 70 function descargar() {
69 for(var i = 0; i < $scope.cisternas.length; i++) { 71 for(var i = 0; i < $scope.cisternas.length; i++) {
70 var descarga = $scope.aDescargar[i]; 72 var descarga = $scope.aDescargar[i];
71 var cisternaCarga = $scope.cisternas[i].cisternaCarga; 73 var cisternaCarga = $scope.cisternas[i].cisternaCarga;
72 if(!descarga) continue; 74 if(!descarga) continue;
73 if(descarga > cisternaCarga.cantidad) { 75 if(descarga > cisternaCarga.cantidad) {
74 focaModalService.alert('La cantidad a descargar no debe ser ' + 76 focaModalService.alert('La cantidad a descargar no debe ser ' +
75 'mayor a la cantidad de la cisterna'); 77 'mayor a la cantidad de la cisterna');
76 $scope.cargando = false; 78 $scope.cargando = false;
77 return; 79 return;
78 } 80 }
79 cisternaCarga.cantidad -= descarga; 81 cisternaCarga.cantidad -= descarga;
80 82
81 //Guardar 83 //Guardar
82 var now = new Date(); 84 var now = new Date();
83 var cisternaMovimiento = { 85 var cisternaMovimiento = {
84 fecha: now.toISOString().slice(0, 19).replace('T', ' '), 86 fecha: now.toISOString().slice(0, 19).replace('T', ' '),
85 cantidad: descarga, 87 cantidad: descarga,
86 metodo: 'descarga', 88 metodo: 'descarga',
87 idCisternaCarga: cisternaCarga.id, 89 idCisternaCarga: cisternaCarga.id,
88 idRemito: $scope.remito.id 90 idRemito: $scope.remito.id
89 }; 91 };
90 var hojaRutaMovimiento = { 92 var hojaRutaMovimiento = {
91 reciboDescarga: $scope.numeroRecibo, 93 reciboDescarga: $scope.numeroRecibo,
92 idRemito: $scope.remito.id 94 idRemito: $scope.remito.id
93 }; 95 };
94 delete cisternaCarga.articulo; 96 delete cisternaCarga.articulo;
95 cisternaCargas.push(cisternaCarga); 97 cisternaCargas.push(cisternaCarga);
96 cisternaMovimientos.push(cisternaMovimiento); 98 cisternaMovimientos.push(cisternaMovimiento);
97 hojaRutaMovimientos.push(hojaRutaMovimiento); 99 hojaRutaMovimientos.push(hojaRutaMovimiento);
98 } 100 }
99 var save = { 101 var save = {
100 cisternaCargas: cisternaCargas, 102 cisternaCargas: cisternaCargas,
101 cisternaMovimientos: cisternaMovimientos, 103 cisternaMovimientos: cisternaMovimientos,
102 hojaRutaMovimientos: hojaRutaMovimientos, 104 hojaRutaMovimientos: hojaRutaMovimientos,
103 articulo: $scope.articuloSeleccionado 105 articulo: $scope.articuloSeleccionado
104 }; 106 };
105 focaModalDetalleHojaRutaService 107 focaModalDetalleHojaRutaService
106 .postMovimientoHojaRuta(save) 108 .postMovimientoHojaRuta(save)
107 .then(guardarSeguimiento) 109 .then(guardarSeguimiento)
108 .catch(error); 110 .catch(error);
109 function guardarSeguimiento(res) { 111 function guardarSeguimiento(res) {
110 focaSeguimientoService 112 focaSeguimientoService
111 .guardarPosicion( 113 .guardarPosicion(
112 'Entrega de producto', 114 'Entrega de producto',
113 res.data[0].id, 115 res.data[0].id,
114 $scope.remito.observaciones); 116 $scope.remito.observaciones);
115 $scope.aDescargar = []; 117 $scope.aDescargar = [];
116 $scope.remito.observaciones = ''; 118 $scope.remito.observaciones = '';
117 $scope.articuloSeleccionado.descargado = true; 119 $scope.articuloSeleccionado.descargado = true;
118 var siguienteArticulo = $scope.remito.articulosRemito.filter( 120 var siguienteArticulo = $scope.remito.articulosRemito.filter(
119 function(articulo) { 121 function(articulo) {
120 return articulo.id != $scope.articuloSeleccionado.id; 122 return articulo.id != $scope.articuloSeleccionado.id;
121 } 123 }
122 ); 124 );
123 if(siguienteArticulo.length) { 125 if(siguienteArticulo.length) {
124 $scope.cambio(siguienteArticulo[0]); 126 $scope.cambio(siguienteArticulo[0]);
125 } 127 }
126 success(); 128 success();
127 } 129 }
128 } 130 }
129 }; 131 };
130 132
131 $scope.cancel = function() { 133 $scope.cancel = function() {
132 $uibModalInstance.dismiss('cancel'); 134 $uibModalInstance.dismiss('cancel');
133 }; 135 };
134 136
135 $scope.distribucionDisponible = function() { 137 $scope.distribucionDisponible = function() {
136 return $scope.articuloSeleccionado.cantidadDescargada === 138 return $scope.articuloSeleccionado.cantidadDescargada ===
137 $scope.articuloSeleccionado.cantidad; 139 $scope.articuloSeleccionado.cantidad;
138 }; 140 };
139 141
140 $scope.actualizarArticulo = function() { 142 $scope.actualizarArticulo = function() {
141 $scope.articuloSeleccionado.cantidadDescargada = 0; 143 $scope.articuloSeleccionado.cantidadDescargada = 0;
142 for(var i = 0; i < $scope.aDescargar.length; i++) { 144 for(var i = 0; i < $scope.aDescargar.length; i++) {
143 $scope.articuloSeleccionado.cantidadDescargada += 145 $scope.articuloSeleccionado.cantidadDescargada +=
144 parseFloat($scope.aDescargar[i]) || 0; 146 parseFloat($scope.aDescargar[i]) || 0;
145 } 147 }
146 }; 148 };
147 149
148 $scope.rechazar = function() { 150 $scope.rechazar = function() {
149 focaModalService 151 focaModalService
150 .prompt('Aclare el motivo de rechazo') 152 .prompt('Aclare el motivo de rechazo')
151 .then(function(motivo) { 153 .then(function(motivo) {
152 $scope.cargando = true; 154 $scope.cargando = true;
153 var remitoRechazado = $.extend(true, {}, $scope.remito); 155 var remitoRechazado = $.extend(true, {}, $scope.remito);
154 delete remitoRechazado.articulosRemito; 156 delete remitoRechazado.articulosRemito;
155 delete remitoRechazado.notaPedido; 157 delete remitoRechazado.notaPedido;
156 remitoRechazado.rechazado = true; 158 remitoRechazado.rechazado = true;
157 remitoRechazado.motivoRechazo = motivo; 159 remitoRechazado.motivoRechazo = motivo;
158 remitoRechazado.fechaRemito = 160 remitoRechazado.fechaRemito =
159 remitoRechazado.fechaRemito.slice(0, 19).replace('T', ' '); 161 remitoRechazado.fechaRemito.slice(0, 19).replace('T', ' ');
160 focaModalDetalleHojaRutaService 162 focaModalDetalleHojaRutaService
161 .rechazarRemito(remitoRechazado) 163 .rechazarRemito(remitoRechazado)
162 .then(success) 164 .then(success)
163 .catch(error) 165 .catch(error)
164 $scope.readonly = true; 166 $scope.readonly = true;
165 }); 167 });
166 }; 168 };
167 169
168 //funciones 170 //funciones
169 function error(error) { 171 function error(error) {
170 focaModalService.alert('Hubo un error ' + error); 172 focaModalService.alert('Hubo un error ' + error);
171 } 173 }
172 function success() { 174 function success() {
173 focaModalService.alert('Operación realizada con éxito'); 175 focaModalService.alert('Operación realizada con éxito');
174 $scope.cargando = false; 176 $scope.cargando = false;
175 } 177 }
176 } 178 }
177 ] 179 ]
178 ); 180 );
179 181