angular.module('focaCrearNotaPedido') .controller('notaPedidoCtrl', ['$scope', '$uibModal', '$location', 'crearNotaPedidoService', 'focaModalService', function($scope, $uibModal, $location, crearNotaPedidoService, focaModalService) { $scope.show = false; $scope.edit = false; $scope.dateOptions = { maxDate: new Date(), minDate: new Date(2010, 0, 1) }; $scope.notaPedido = { vendedor: {}, cliente: {} }; $scope.articulosTabla = []; var idLista; var notaPedidoTemp = crearNotaPedidoService.getNotaPedido(); $scope.domiciliosCliente = crearNotaPedidoService.getDomicilios(1); crearNotaPedidoService.getPrecioCondicion().then( function(res) { $scope.precioCondiciones = res.data; } ); if (notaPedidoTemp !== undefined) { notaPedidoTemp.fechaCarga = new Date(notaPedidoTemp.fechaCarga); $scope.notaPedido = notaPedidoTemp; $scope.notaPedido.flete = ($scope.notaPedido.flete).toString(); $scope.notaPedido.bomba = ($scope.notaPedido.bomba).toString(); idLista = $scope.notaPedido.precioCondicion; crearNotaPedidoService .getArticulosByIdNotaPedido($scope.notaPedido.id).then( function(res) { $scope.articulosTabla = res.data; } ); crearNotaPedidoService.getDomiciliosByIdNotaPedido($scope.notaPedido.id).then( function(res) { $scope.notaPedido.domicilio = res.data; } ); } else { $scope.notaPedido.fechaCarga = new Date(); $scope.notaPedido.domicilio = [{ id: 0 }]; $scope.notaPedido.bomba = '1'; $scope.notaPedido.flete = '0'; idLista = undefined; } $scope.addNewDom = function() { $scope.notaPedido.domicilio.push({ 'id': 0 }); }; $scope.removeNewChoice = function(choice) { if ($scope.notaPedido.domicilio.length > 1) { $scope.notaPedido.domicilio.splice($scope.notaPedido.domicilio.findIndex( function(c) { return c.$$hashKey === choice.$$hashKey; } ), 1); } }; $scope.crearNotaPedido = function() { var notaPedido = { id: 0, precioCondicion: $scope.notaPedido.precioCondicion, fechaCarga: $scope.notaPedido.fechaCarga, vendedor: $scope.notaPedido.vendedor, cliente: $scope.notaPedido.cliente, producto: $scope.notaPedido.producto, bomba: $scope.notaPedido.bomba, petrolera: $scope.notaPedido.petrolera, domicilio: $scope.notaPedido.domicilio, kilometros: $scope.notaPedido.kilometros, jurisdiccionIIBB: $scope.notaPedido.jurisdiccionIIBB, costoFinanciacion: $scope.notaPedido.costoFinanciacion, flete: $scope.notaPedido.flete, costoUnitarioKmFlete: $scope.notaPedido.costoUnitarioKmFlete, total: $scope.articulosTabla[0].subTotal }; crearNotaPedidoService.crearNotaPedido(notaPedido).then( function() { focaModalService('Nota pedido creada'); $location.path('/venta-nota-pedido'); } ); var articulosNotaPedido = $scope.articulosTabla; for(var i = 0; i< articulosNotaPedido.length;i++) { crearNotaPedidoService .crearArticulosParaNotaPedido(articulosNotaPedido[i]).then( function() { return; } ); } }; $scope.siguienteTab = function() { $scope.active = 1; }; $scope.seleccionarArticulo = function() { if (idLista === undefined) { focaModalService.alert( 'primero seleccione una lista de precio y condicion'); return; } var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Productos', templateUrl: 'modal-busqueda-productos.html', controller: 'modalBusquedaProductosCtrl', resolve: { idLista: function() { return idLista; } }, size: 'lg' } ); modalInstance.result.then( function(producto) { var newArt = { id: 0, codigo: producto.codigo, sector: producto.sector, descripcion: producto.descripcion, item: $scope.articulosTabla.length + 1, nombre: producto.descripcion, precio: producto.precio.toFixed(2), costoUnitario: producto.costo, cantidad: 1 }; $scope.articuloACargar = newArt; }, function() { // funcion ejecutada cuando se cancela el modal } ); }; $scope.seleccionarVendedor = function() { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Vendedores', templateUrl: 'modal-vendedores.html', controller: 'modalVendedoresCtrl', size: 'lg' } ); modalInstance.result.then( function(vendedor) { $scope.notaPedido.vendedor.nombre = vendedor.NomVen; }, function() { } ); }; $scope.seleccionarPetrolera = function() { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Petrolera', templateUrl: 'modal-petroleras.html', controller: 'modalPetrolerasCtrl', size: 'lg' } ); modalInstance.result.then( function(petrolera) { $scope.notaPedido.petrolera = petrolera.NOM; }, function() { } ); }; $scope.seleccionarCliente = function() { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Cliente', templateUrl: 'foca-busqueda-cliente-modal.html', controller: 'focaBusquedaClienteModalController', size: 'lg' } ); modalInstance.result.then( function(cliente) { $scope.notaPedido.cliente.nombre = cliente.nom; }, function() { } ); }; $scope.mostrarFichaCliente = function() { $uibModal.open( { ariaLabelledBy: 'Datos del Cliente', templateUrl: 'foca-crear-nota-pedido-ficha-cliente.html', controller: 'focaCrearNotaPedidoFichaClienteController', size: 'lg' } ); }; $scope.obtenerDomicilios = function(id) { crearNotaPedidoService.getDomicilios(id).then( function(res) { $scope.notaPedido.domicilio = res.data; } ); }; $scope.getTotal = function() { var total = 0; var array = $scope.articulosTabla; for (var i = 0; i < array.length; i++) { total += array[i].precio * array[i].cantidad; } return total.toFixed(2); }; $scope.getSubTotal = function() { if($scope.articuloACargar) { return $scope.articuloACargar.precio * $scope.articuloACargar.cantidad; } }; $scope.cargarArticulos = function() { idLista = $scope.notaPedido.precioCondicion; $scope.articulosTabla = []; }; $scope.abrirModalListaPrecio = function() { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Precio Condición', templateUrl: 'modal-precio-condicion.html', controller: 'focaModalPrecioCondicionController', size: 'lg' } ); modalInstance.result.then( function(precioCondicion) { $scope.notaPedido.precioCondicion = precioCondicion.nombre; idLista = precioCondicion.idListaPrecio; $scope.articulosTabla = []; }, function() { } ); }; $scope.abrirModalFlete = function() { if($scope.notaPedido.flete === '1') { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Flete', templateUrl: 'modal-flete.html', controller: 'focaModalFleteController', size: 'lg' } ); modalInstance.result.then( function(flete) { $scope.choferes = ''; $scope.vehiculos = ''; $scope.notaPedido.fleteNombre = flete.nombre; $scope.notaPedido.costoUnitarioKmFlete = flete.costoKilometro; $scope.choferes = flete.chofer; $scope.vehiculos = flete.vehiculo; }, function() { } ); } }; $scope.agregarATabla = function(key) { if(key === 13) { $scope.articulosTabla.unshift($scope.articuloACargar); $scope.articuloACargar = undefined; } }; $scope.quitarArticulo = function(key) { $scope.articulosTabla.splice(key, 1); }; $scope.editarArticulo = function(key) { if(key === 13) { $scope.edit = false; } }; $scope.cambioEdit = function() { $scope.edit = !$scope.edit ? true : false; }; $scope.limpiarFlete = function() { $scope.notaPedido.fleteNombre = ''; $scope.notaPedido.chofer = ''; $scope.notaPedido.vehiculo = ''; $scope.notaPedido.kilometros = ''; $scope.notaPedido.costoUnitarioKmFlete = ''; }; $scope.crearPedidoDemo = function() { focaModalService.alert('Pedido Creado'); $scope.notaPedido.precioCondicion = ''; $scope.articulosTabla = []; $scope.notaPedido.fleteNombre = ''; $scope.notaPedido.chofer = ''; $scope.notaPedido.vehiculo = ''; $scope.notaPedido.kilometros = ''; $scope.notaPedido.costoUnitarioKmFlete = ''; $scope.notaPedido.vendedor.nombre = ''; $scope.notaPedido.cliente.nombre = ''; $scope.domicilio.dom = ''; $scope.notaPedido.flete = 0; }; } ] ) .controller('notaPedidoListaCtrl', [ '$scope', 'crearNotaPedidoService', '$location', function($scope, crearNotaPedidoService, $location) { crearNotaPedidoService.obtenerNotaPedido().then(function(datos) { $scope.notaPedidos = datos.data; }); $scope.editar = function(notaPedido) { crearNotaPedidoService.setNotaPedido(notaPedido); $location.path('/venta-nota-pedido/abm/'); }; $scope.crearPedido = function() { crearNotaPedidoService.clearNotaPedido(); $location.path('/venta-nota-pedido/abm/'); }; } ]) .controller('focaCrearNotaPedidoFichaClienteController', [ '$scope', 'crearNotaPedidoService', '$location', function($scope, crearNotaPedidoService, $location) { crearNotaPedidoService.obtenerNotaPedido().then(function(datos) { $scope.notaPedidos = datos.data; }); $scope.editar = function(notaPedido) { crearNotaPedidoService.setNotaPedido(notaPedido); $location.path('/venta-nota-pedido/abm/'); }; $scope.crearPedido = function() { crearNotaPedidoService.clearNotaPedido(); $location.path('/venta-nota-pedido/abm/'); }; } ]);