controllerCisterna.js 5.24 KB
angular.module('focaAbmVehiculo')
    .controller('focaAbmVehiculoCisternaController', [
        '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal',
        'focaModalService', 'focaBotoneraLateralService', '$timeout',
        function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal,
        focaModalService, focaBotoneraLateralService, $timeout) {
            $scope.editar = false;
            $scope.now = new Date();
            $scope.cisterna = {
                cisternaCarga: {
                    articulo: {}
                }
            };

            $scope.focused = 1;
            $scope.next = function(key) {
                if (key === 13) $scope.focused++;
            };

            //SETEO BOTONERA LATERAL
            $timeout(function() {
                focaBotoneraLateralService.showSalir(false);
                focaBotoneraLateralService.showPausar(false);
                focaBotoneraLateralService.showCancelar(true);
                focaBotoneraLateralService.showGuardar(true, $scope.guardar);
            });

            focaAbmVehiculoService.getCisterna($routeParams.id).then(function(res) {
                if(res.data) {
                    $scope.cisterna = res.data;
                    $scope.editar = true;
                }else {
                    $scope.editar = false;
                }
            });
            focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) {
                var codigo = ('00000' + res.data.transportista.COD).slice(-5);
                $scope.$broadcast('addCabecera', {
                    label: 'Transportista:',
                    valor: codigo + ' - ' + res.data.transportista.NOM
                });
                $scope.$broadcast('addCabecera', {
                    label: 'Unidad:',
                    valor: res.data.codigo
                });
            });
            $scope.cancelar = function() {
                $location.path('/vehiculo/' + $routeParams.idVehiculo);
            };
            $scope.guardar = function() {
                if(!$scope.cisterna.unidadMedida) {
                    focaModalService.alert('Ingrese unidad de medida');
                    return;
                }
                if(!$scope.cisterna.cisternaCarga.articulo) {
                    focaModalService.alert('Ingrese producto');
                    return;
                }
                focaAbmVehiculoService.getCisternadoPorVehiculo($routeParams.idVehiculo)
                    .then(function(res) {
                        var existe = res.data.filter(function(cisterna) {
                            return cisterna.codigo === $scope.cisterna.codigo;
                        });
                        if(existe.length > 0) {
                            return Promise.reject('Existe codigo de cisterna');
                        }else {
                            $scope.cisterna.idVehiculo = $routeParams.idVehiculo;
                            delete $scope.cisterna.unidadMedida;                
                            delete $scope.cisterna.cisternaCarga.articulo;
                            return focaAbmVehiculoService.guardarCisterna($scope.cisterna);
                        }
                    }).then(function() {
                        $location.path('/vehiculo/' + $routeParams.idVehiculo);
                    }, function(rej) {
                        focaModalService.alert(rej);
                    });
            };

            // $scope.seleccionarArticulo = function() {
            //     var modalInstance = $uibModal.open(
            //         {
            //             ariaLabelledBy: 'Busqueda de Productos',
            //             templateUrl: 'modal-busqueda-productos.html',
            //             controller: 'modalBusquedaProductosCtrl',
            //             resolve: {
            //                 parametroProducto: {
            //                     idLista: -1,
            //                     cotizacion: 1,
            //                     simbolo: '$'
            //                 }
            //             },
            //             size: 'lg'
            //         }
            //     );
            //     modalInstance.result.then(
            //         function(producto) {
            //             $scope.cisterna.cisternaCarga.idProducto = producto.id,
            //             $scope.cisterna.cisternaCarga.articulo.DetArt = producto.descripcion;
            //         }, function() {
            //             // funcion ejecutada cuando se cancela el modal
            //         }
            //     );
            // };

            $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;
                });
            };
        }
    ]);