angular.module('focaModalInforme') .controller('focaModalInformeLitrosKmUnidadController', [ '$filter', '$scope', '$uibModalInstance', 'focaModalInformeLitrosKmUnidadService', 'i18nService', 'focaModalService', '$uibModal', function($filter, $scope, $uibModalInstance, focaModalInformeLitrosKmUnidadService, i18nService, focaModalService, $uibModal ) { var meses = ['ene', 'feb', 'mar', 'abr', 'may', 'jun', 'jul', 'ago', 'sep', 'oct', 'nov', 'dic']; var fecha = new Date(); $scope.generando = false; $scope.fechaHasta = new Date(); $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); $scope.buscar = true; $scope.informe = {}; i18nService.setCurrentLang('es'); $scope.gridOptions = { enableGridMenu: true, exporterMenuCsv: false, exporterPdfPageSize: 'A4', exporterPdfFooter: function (currentPage, pageCount) { return { columns: [ {text: $filter('date')(new Date(), 'dd/MM/yyyy'), margin: [40, 0]}, {text: currentPage + ' de ' + pageCount, margin: [28, 0], alignment: 'right'} ] }; }, columnDefs: [ { field: 'fecha', enableSorting: false, cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) { if (grid.getCellValue(row,col).indexOf('Sector:') !== -1) return 'yellow w-100'; if (grid.getCellValue(row,col).indexOf('Producto:') !== -1) return 'red w-100'; } }, { field: 'litrosRepartidos', enableSorting: false, cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) { if (!grid.getCellValue(row,col)) return 'd-none'; } }, { field: 'kmRecorridos', enableSorting: false, cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) { if (!grid.getCellValue(row,col)) return 'd-none'; } }, { field: 'lts/Km', enableSorting: false, cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) { if (!grid.getCellValue(row,col)) return 'd-none'; } } ] }; $scope.generarInforme = function() { if(!$scope.unidad){ focaModalService.alert('Primero seleccione una unidad'); return; } $scope.generando = true; focaModalInformeLitrosKmUnidadService .getDescargas( $scope.unidad.id, $scope.fechaDesde.toISOString().split('.')[0], $scope.fechaHasta.toISOString().split('.')[0] ) .then(function(res) { var result = []; res.data.forEach(function(sector) { result.push({ fecha: 'Sector: ' + $filter('rellenarDigitos')(sector[0].articulo.CodSec, 2, '0') + ' - ' + sector[0].articulo.sector.NOMBRE.trim() }); sector.forEach(function(cisternaCarga) { result.push({ fecha: 'Producto: ' + cisternaCarga.articulo.CodArt + ' - ' + cisternaCarga.articulo.DetArt }); cisternaCarga.cisternaMovimientos.forEach(function(cisternaMovimiento) { var lts = cisternaMovimiento.lts; var kms = cisternaMovimiento.kms; result.push({ fecha: meses[cisternaMovimiento.month] + ' - ' + cisternaMovimiento.year, litrosRepartidos: $filter('number')(lts, 2), kmRecorridos: $filter('number')(kms, 2), 'lts/Km': (kms) ? $filter('number')(lts / kms, 2) : lts }); }); }); }); $scope.gridOptions.data = result; $scope.generando = false; $scope.buscar = false; }); }; $scope.seleccionarTransportista = function() { var parametrosModal = { titulo: 'Búsqueda de Transportista', query: '/transportista', columnas: [ { propiedad: 'COD', nombre: 'Código', filtro: { nombre: 'rellenarDigitos', parametro: 5 } }, { propiedad: 'NOM', nombre: 'Nombre' }, { propiedad: 'CUIT', nombre: 'CUIT' } ], size: 'md' }; return focaModalService.modal(parametrosModal) }; $scope.seleccionarUnidad = function(key) { if (key === 13) { $scope.seleccionarTransportista().then( function(transportista) { $scope.transportista = transportista; var parametrosModal = { titulo: 'Búsqueda de vehículos', searchText: $scope.unidad.codigo, query: '/vehiculo/transportista/' + transportista.COD, columnas: [ { propiedad: 'codigo', nombre: 'Código' }, { propiedad: 'tractor', nombre: 'Tractor' }, { propiedad: 'semi', nombre: 'Semi' }, { propiedad: 'tractor', nombre: 'Tractor' } ], size: 'md' }; focaModalService.modal(parametrosModal).then( function(unidad) { $scope.unidad = unidad; }, function() { $scope.seleccionarUnidad(); }); }); } }; $scope.volver = function() { $scope.buscar = true; }; $scope.cancel = function() { $uibModalInstance.dismiss('Cancelar'); }; } ] );