angular.module('focaModalDescarga') .controller('focaModalDescargaCtrl', [ '$scope', 'focaModalDescargasService', '$filter', 'focaModalService', '$timeout', '$uibModalInstance', '$uibModal', 'remito', 'focaModalDetalleHojaRutaService', 'focaSeguimientoService', 'crearRemitoService', function ($scope, focaModalDescargasService, $filter, focaModalService, $timeout, $uibModalInstance, $uibModal, remito, focaModalDetalleHojaRutaService, focaSeguimientoService, crearRemitoService) { //#region variables console.log(remito); $scope.remito = {}; $scope.remito = remito; $scope.nroRecibo = 0; $scope.cisternas = remito.cisternas; $scope.nroRemito; $scope.articulos = []; //#endregion function init () { crearRemitoService.getNumeroRemito().then( function (res) { $scope.nroRemito = rellenar(res.data.sucursal, 4) + '-' + rellenar(res.data.numeroRemito, 8); }, function (err) { focaModalService.alert('La terminal no esta configurada correctamente'); console.info(err); } ); }; init(); $scope.crearRemito = function () { if (!$scope.remito.cliente) { focaModalService.alert("No has seleccionado cliente"); return; } if (!validarDescarga()) { focaModalService.alert("Cantidad de descarga ingresada erronea"); return; } if (!$scope.nroRecibo) { focaModalService.alert("No ingresaste nro de recibo"); return; } //#region OBJETO REMITO var remito = { id: 0, fechaRemito: new Date().toISOString().slice(0, 19).replace('T', ' '), idCliente: $scope.remito.cliente.cod, nombreCliente: $scope.remito.cliente.nom, cuitCliente: $scope.remito.cliente.cuit, total: $scope.getTotal() * $scope.remito.cotizacion.VENDEDOR, numeroNotaPedido: $scope.remito.numeroNotaPedido, idVendedor: $scope.remito.cliente.ven, idProveedor: $scope.remito.proveedor.COD, idDomicilio: 0, idCotizacion: $scope.remito.cotizacion.ID, idListaPrecio: $scope.remito.cliente.mod, flete: $scope.remito.flete, fob: $scope.remito.fob, bomba: $scope.remito.bomba, kilometros: $scope.remito.kilometros, domicilioStamp: $scope.remito.cliente.DOM, observaciones: $scope.remito.observaciones, numeroRemito: parseInt($scope.comprobante), sucursal: parseInt($scope.puntoVenta), responsabilidadIvaCliente: $scope.remito.cliente.iva, descuento: 0,//TODO, importeNeto: getImporte('netoUnitario'), importeExento: getImporte('exentoUnitario'), importeIva: getImporte('ivaUnitario'), importeIvaServicios: 0,//TODO importeImpuestoInterno: getImporte('impuestoInternoUnitario'), importeImpuestoInterno1: getImporte('impuestoInterno1Unitario'), importeImpuestoInterno2: getImporte('impuestoInterno2Unitario'), percepcion: 0,//TODO percepcionIva: 0,//TODO redondeo: 0,//TODO anulado: false, planilla: $filter('date')($scope.now, 'ddMMyyyy'), lugar: parseInt($scope.puntoVenta), cuentaMadre: 0,//TODO cuentaContable: 0,//TODO asiento: 0,//TODO e_hd: '',//TODO c_hd: '', numeroLiquidoProducto: 0,//TODO estado: 0, destinoVenta: 0,//TODO operacionTipo: 0, //TODO idHojaRuta: $scope.remito.idHojaRuta } //#endregionc focaModalDescargasService.crearRemito({ remito: remito, notaPedido: { id: 0 } }) .then(function (res) { console.log(res); remitoId = res.data.id descargar(remitoId); focaModalDescargasService. addArticulos($scope.remito.articulosRemito, $scope.articulos, remitoId, 1); focaModalService.alert("Descarga realizada") .then(function () { $uibModalInstance.dismiss(); }) }) .catch(function (e) { console.log(e); }) }; $scope.getTotal = function () { var total = 0; $scope.cisternas.forEach(function (cisterna) { total += cisterna.cisternaCarga.articulo.PreVen * cisterna.cisternaCarga.descargar; cisterna.cisternaCarga.articulo.cantidad = cisterna.cisternaCarga.descargar; $scope.articulos.push(cisterna.cisternaCarga.articulo); }); return parseFloat(total.toFixed(2)); }; function descargar(idRemito) { for (var i = 0; i < $scope.cisternas.length; i++) { var descarga = $scope.cisternas[i].cisternaCarga.descargar; var hojaRutaMovimientos = []; var cisternaMovimientos = []; var cisternaCargas = []; var cisternaCarga = $scope.cisternas[i].cisternaCarga; if (!descarga) continue; // if (descarga > cisternaCarga.cantidad) { // focaModalService.alert('La cantidad a descargar no debe ser ' + // 'mayor a la cantidad de la cisterna'); // $scope.cargando = false; // return; // } cisternaCarga.cantidad -= descarga; //Guardar var now = new Date(); var cisternaMovimiento = { fecha: now.toISOString().slice(0, 19).replace('T', ' '), cantidad: descarga, metodo: 'descarga', idCisternaCarga: cisternaCarga.id, idRemito: idRemito }; var hojaRutaMovimiento = { reciboDescarga: $scope.nroRecibo, idRemito: idRemito }; cisternaCarga = angular.copy($scope.cisternas[i].cisternaCarga); delete cisternaCarga.articulo; delete cisternaCarga.descargar; cisternaCargas.push(cisternaCarga); cisternaMovimientos.push(cisternaMovimiento); hojaRutaMovimientos.push(hojaRutaMovimiento); } var save = { cisternaCargas: cisternaCargas, cisternaMovimientos: cisternaMovimientos, hojaRutaMovimientos: hojaRutaMovimientos, articulo: $scope.articuloSeleccionado }; focaModalDetalleHojaRutaService .postMovimientoHojaRuta(save) .then(guardarSeguimiento) .catch(function () { }); function guardarSeguimiento(res) { focaSeguimientoService .guardarPosicion( 'Entrega de producto', res.data[0].id, ''); $scope.aDescargar = []; }; }; function getImporte(propiedad) { var importe = 0; $scope.remito.articulosRemito.forEach(function (articulo) { if (articulo[propiedad]) { importe += articulo[propiedad] * articulo.cantidad; } return; }); return importe; }; function validarDescarga() { hasDisponible = $scope.cisternas.find(function (cisterna) { return parseInt(cisterna.cisternaCarga.descargar) >= 0 && parseInt(cisterna.cisternaCarga.descargar) <= cisterna.cisternaCarga.cantidad }); return hasDisponible; }; $scope.seleccionarCliente = function () { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Cliente', templateUrl: 'foca-busqueda-cliente-modal.html', controller: 'focaBusquedaClienteModalController', resolve: { vendedor: function () { return null; }, cobrador: function () { return null; } }, size: 'lg' } ); modalInstance.result.then(function (cliente) { $scope.remito.cliente = cliente; }, function () { //funcion ejecutada al cancelar modal }); }; $scope.cancelar = function () { $uibModalInstance.dismiss(); } function rellenar(relleno, longitud) { relleno = '' + relleno; while (relleno.length < longitud) { relleno = '0' + relleno; } return relleno; } } ]);