controller.js 5.39 KB
angular.module('focaAbmVehiculo')
    .controller('focaAbmVehiculosController', [
        '$scope', 'focaAbmVehiculoService', '$location', 'focaModalService', '$uibModal',
        function($scope, focaAbmVehiculoService, $location, focaModalService, $uibModal) {
            $scope.buscar = function(idTransportista) {
                focaAbmVehiculoService
                    .getVehiculosPorTransportista(idTransportista)
                    .then(function(datos) {
                        $scope.vehiculos = datos.data;
                    });
            };
            $scope.editar = function(id) {
                $location.path('/vehiculo/' + id + '/' + $scope.idTransportista);
            };
            $scope.solicitarConfirmacion = function(vehiculo) {
                focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' +
                vehiculo.id + ' ' + vehiculo.tractor + ' ?').then(
                        function(data) {
                            if(data) {
                                focaAbmVehiculoService.deleteVehiculo(vehiculo.id);
                                $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1);
                            }
                        }
                    );
            };
            $scope.seleccionarTransportista = function() {
                var modalInstance = $uibModal.open(
                    {
                        ariaLabelledBy: 'Busqueda de Transportista',
                        templateUrl: 'modal-proveedor.html',
                        controller: 'focaModalProveedorCtrl',
                        size: 'lg',
                        resolve: {
                            transportista: function() {
                                return true;
                            }
                        }
                    }
                );
                modalInstance.result.then(
                    function(transprotista) {
                        $scope.idTransportista = transprotista.COD;
                        $scope.filtros = transprotista.NOM.trim();
                        $scope.buscar(transprotista.COD);
                    }, function() {

                    }
                );
            };
        }
    ])
    .controller('focaAbmVehiculoController', [
        '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal',
        'focaModalService',
        function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal,
        focaModalService) {
            $scope.nuevo = $routeParams.idVehiculo == 0 ? true : false;
            if($scope.nuevo) {
                focaAbmVehiculoService
                    .getTransportistaPorId($routeParams.idTransportista)
                    .then(function(res) {
                        $scope.vehiculo.transportista = res.data;
                    })
            }
            $scope.vehiculo = {};
            focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) {
                if(res.data) $scope.vehiculo = res.data;
            });
            focaAbmVehiculoService.getCisternadoPorVehiculo($routeParams.idVehiculo)
                .then(function(res) {
                    $scope.cisternas = res.data;
            });
            $scope.cancelar = function() {
                $location.path('/vehiculo');
            };
            $scope.editar = function(id) {
                $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/' + id);
            };
            $scope.guardar = function() {
                if(!$scope.vehiculo.transportista) {
                    focaModalService.alert('Elija Transportista');
                    return;
                }
                delete $scope.vehiculo.transportista;
                focaAbmVehiculoService.guerdarVehiculo($scope.vehiculo).then(function(res) {
                    $location.path('/vehiculo/' + res.data.id);
                });
            };
            $scope.solicitarConfirmacionCisterna = function(cisterna) {
                focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' +
                cisterna.id + ' ' + cisterna.codigo + ' ?').then(
                        function(data) {
                            if(data) {
                                focaAbmVehiculoService.deleteCisterna(cisterna.id);
                                $scope.cisternas.splice($scope.cisternas.indexOf(cisterna), 1);
                            }
                        }
                    );
            };
            $scope.seleccionarTransportista = function() {
                var modalInstance = $uibModal.open(
                    {
                        ariaLabelledBy: 'Busqueda de Transportista',
                        templateUrl: 'modal-proveedor.html',
                        controller: 'focaModalProveedorCtrl',
                        size: 'lg',
                        resolve: {
                            transportista: function() {
                                return true;
                            }
                        }
                    }
                );
                modalInstance.result.then(
                    function(transprotista) {
                        $scope.vehiculo.idTransportista = transprotista.COD;
                        $scope.vehiculo.transportista = transprotista;
                    }, function() {

                    }
                );
            };
        }
    ]);