controllerCisterna.js 5.54 KB
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.idx !== -1) {
                $scope.cisterna = [$routeParams.idx]
                focaAbmVehiculoService
                    .getCisternas($routeParams.idVehiculo)
                    .then(function(res) {
                        $scope.cisterna = res[$routeParams.idx];
                    });
            }

            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;

                        focaAbmVehiculoService
                            .guardarCisterna($scope.cisterna, $routeParams.idx);
                            
                        $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
                        .getCisternas($routeParams.idVehiculo)
                        .then(function(res) {
                            var cisternas = res;
                            var totalCargado = 0;
                            cisternas.forEach(function(cisterna, idx) {
                                //SI EL CODIGO YA EXISTE
                                if(cisterna.codigo === $scope.cisterna.codigo &&
                                    idx != $routeParams.idx &&
                                    !cisterna.desactivado) {
                                    reject('Código de cisterna existente');
                                }
                                if(idx != $routeParams.idx &&
                                    !cisterna.desactivado) {
                                    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();
                            }
                        });
                });
            }
        }
    ]);