controllerCisterna.js
6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
angular.module('focaAbmVehiculo')
.controller('focaAbmVehiculoCisternaController', [
'$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal',
'focaModalService', 'focaBotoneraLateralService', '$timeout', '$window',
function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal,
focaModalService, focaBotoneraLateralService, $timeout, $window) {
$scope.nuevo = ($routeParams.id > 0) ? false : true;
$scope.editar = false;
$scope.now = new Date();
$scope.cisterna = {};
$scope.focused = $scope.nuevo ? 1 : 2;
$scope.next = function(key) {
if (key === 13) $scope.focused++;
};
$scope.capacidadVechiulo = 0;
$scope.transportista = '';
//SETEO BOTONERA LATERAL
$timeout(function() {
focaBotoneraLateralService.showSalir(false);
focaBotoneraLateralService.showPausar(false);
focaBotoneraLateralService.showCancelar(true);
focaBotoneraLateralService.showGuardar(true, $scope.guardar);
});
if($routeParams.idTemp != undefined) {
$scope.cisterna = focaAbmVehiculoService.getCisternasLocal()[$routeParams.idTemp];
}else {
focaAbmVehiculoService.getCisterna($routeParams.id).then(function(res) {
if(res.data) $scope.cisterna = res.data;
});
}
focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) {
var codigo = ('00000' + res.data.transportista.COD).slice(-5);
$scope.transportista = res.data.transportista.COD;
$scope.capacidadVechiulo = res.data.capacidad;
$scope.$broadcast('addCabecera', {
label: 'Transportista:',
valor: codigo + ' - ' + res.data.transportista.NOM
});
$scope.$broadcast('addCabecera', {
label: 'Unidad:',
valor: res.data.codigo
});
$scope.$broadcast('addCabecera', {
label: 'Capacidad total vehículo:',
valor: res.data.capacidad
});
});
$scope.cancelar = function() {
$location.path('/vehiculo/' + $routeParams.idVehiculo);
};
$scope.guardar = function() {
if(!$scope.cisterna.unidadMedida) {
focaModalService.alert('Ingrese unidad de medida');
return;
}
validaCodigoCapacidad().then(function() {
$scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo);
delete $scope.cisterna.vehiculo;
if($routeParams.idTemp != undefined) {
//SI SE EDITA UNA CISTERNA LOCALMENTE
focaAbmVehiculoService
.guardarCisternaLocal($scope.cisterna, $routeParams.idTemp);
}else if($scope.cisterna.id) {
//SI SE EDITA UNA CISTERNA PREVIAMENTE GUARDADA
focaAbmVehiculoService.deleteCisterna($scope.cisterna.id);
focaAbmVehiculoService.guardarCisternaLocal($scope.cisterna);
}else {
//SI SE EDITA CREA UNA NUEVA CISTERNA
focaAbmVehiculoService.guardarCisternaLocal($scope.cisterna);
}
$window.location.assign('/#!/vehiculo/' + $routeParams.idVehiculo +
'/' + $scope.transportista);
}, function(err) {
focaModalService.alert(err);
});
};
$scope.seleccionarUnidadMedida = function() {
var modalInstance = $uibModal.open(
{
ariaLabelledBy: 'Busqueda de Unidades de medida',
templateUrl: 'modal-unidad-medida.html',
controller: 'focaModalUnidadMedidaCtrl',
size: 'lg'
}
);
modalInstance.result.then(function(unidaMedida) {
$scope.cisterna.idUnidadMedida = unidaMedida.ID;
$scope.cisterna.unidadMedida = unidaMedida;
});
};
function validaCodigoCapacidad() {
return new Promise(function(resolve, reject) {
focaAbmVehiculoService
.getCisternadoPorVehiculo($routeParams.idVehiculo)
.then(function(res) {
var cisternas = focaAbmVehiculoService.getCisternasLocal().concat(res.data);
var totalCargado = 0;
cisternas.forEach(function(cisterna) {
//SI EL CODIGO YA EXISTE
if(cisterna.codigo === $scope.cisterna.codigo &&
(cisterna.id !== $scope.cisterna.id ||
cisterna.idTemp !== $scope.cisterna.idTemp)) {
reject('Código de cisterna existente');
}
if(cisterna.id !== $scope.cisterna.id) {
totalCargado += cisterna.capacidad;
}
});
//SI EL TOTAL DE CAPACIDAD DE CISTERNAS ES MAYOR QUE LA DEL VEHICULO
totalCargado = totalCargado + parseInt($scope.cisterna.capacidad);
if(totalCargado > $scope.capacidadVechiulo) {
reject('La capacidad total de las cisternas' +
' no debe ser mayor a la del vehiculo');
}else {
resolve();
}
});
});
}
}
]);