| | | 1 | | using System.Text; |
| | | 2 | | using FAU.DataAccess.Repositories; |
| | | 3 | | using FAU.Entidades; |
| | | 4 | | |
| | | 5 | | namespace FAU.Logica.Services; |
| | | 6 | | |
| | | 7 | | public class ArchivoOrganismoService : IArchivoOrganismoService |
| | | 8 | | { |
| | | 9 | | private readonly IArchivoOrganismoRepository _repo; |
| | | 10 | | |
| | 7 | 11 | | public ArchivoOrganismoService(IArchivoOrganismoRepository repo) |
| | | 12 | | { |
| | 7 | 13 | | _repo = repo; |
| | 7 | 14 | | } |
| | | 15 | | |
| | | 16 | | public async Task<BrouPreviewDto> ObtenerPreviewBrouAsync(long periodoId) |
| | | 17 | | { |
| | 4 | 18 | | var version = await _repo.ObtenerVersionMaximaAsync(periodoId); |
| | 4 | 19 | | var datos = await _repo.ObtenerDatosBrouAsync(periodoId, version); |
| | | 20 | | |
| | 4 | 21 | | var filas = new List<BrouFilaDto>(); |
| | 4 | 22 | | var excepciones = new List<BrouExcepcionDto>(); |
| | | 23 | | |
| | 24 | 24 | | foreach (var d in datos) |
| | | 25 | | { |
| | 8 | 26 | | if (!string.IsNullOrWhiteSpace(d.NumeroCuenta)) |
| | | 27 | | { |
| | 6 | 28 | | filas.Add(new BrouFilaDto |
| | 6 | 29 | | { |
| | 6 | 30 | | Cedula = d.Cedula, |
| | 6 | 31 | | NumeroCuenta = d.NumeroCuenta!, |
| | 6 | 32 | | Liquido = d.Liquido, |
| | 6 | 33 | | Regimen = d.Regimen |
| | 6 | 34 | | }); |
| | | 35 | | } |
| | | 36 | | else |
| | | 37 | | { |
| | 2 | 38 | | excepciones.Add(new BrouExcepcionDto |
| | 2 | 39 | | { |
| | 2 | 40 | | Cedula = d.Cedula, |
| | 2 | 41 | | Nombre = d.NombreCompleto, |
| | 2 | 42 | | Liquido = d.Liquido, |
| | 2 | 43 | | Motivo = "Sin cuenta bancaria registrada" |
| | 2 | 44 | | }); |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | 4 | 48 | | return new BrouPreviewDto |
| | 4 | 49 | | { |
| | 4 | 50 | | Filas = filas, |
| | 4 | 51 | | Excepciones = excepciones, |
| | 6 | 52 | | TotalLiquido = filas.Sum(f => f.Liquido), |
| | 4 | 53 | | CantidadRegistros = filas.Count |
| | 4 | 54 | | }; |
| | 4 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async Task<byte[]> GenerarCsvBrouAsync(long periodoId) |
| | | 58 | | { |
| | 1 | 59 | | var preview = await ObtenerPreviewBrouAsync(periodoId); |
| | | 60 | | |
| | 1 | 61 | | var sb = new StringBuilder(); |
| | 6 | 62 | | foreach (var fila in preview.Filas) |
| | | 63 | | { |
| | 2 | 64 | | sb.AppendLine( |
| | 2 | 65 | | $"{fila.Cedula};{fila.NumeroCuenta};{fila.Liquido.ToString("F2", System.Globalization.CultureInfo.Invari |
| | | 66 | | } |
| | | 67 | | |
| | 1 | 68 | | return Encoding.UTF8.GetBytes(sb.ToString()); |
| | 1 | 69 | | } |
| | | 70 | | |
| | | 71 | | public async Task<SiifResumenDto> ObtenerResumenSiifAsync(long periodoId) |
| | | 72 | | { |
| | 3 | 73 | | var version = await _repo.ObtenerVersionMaximaAsync(periodoId); |
| | 3 | 74 | | var items = await _repo.ObtenerItemsSiifAsync(periodoId, version); |
| | | 75 | | |
| | 3 | 76 | | var filas = new List<SiifFilaProgramaDto>(); |
| | 3 | 77 | | var totalesGenerales = new SiifSubtotalesDto(); |
| | 3 | 78 | | var totalesGeneralesSin083 = new SiifSubtotalesDto(); |
| | | 79 | | |
| | 3 | 80 | | var grupos = items |
| | 7 | 81 | | .Where(i => i.ClasificacionSiif != null) |
| | 9 | 82 | | .GroupBy(i => (i.ProgramaId, i.ProgramaNombre, i.RegimenId, i.RegimenNombre)); |
| | | 83 | | |
| | 14 | 84 | | foreach (var grupo in grupos) |
| | | 85 | | { |
| | 4 | 86 | | var conRubro083 = new SiifSubtotalesDto(); |
| | 4 | 87 | | var sinRubro083 = new SiifSubtotalesDto(); |
| | | 88 | | |
| | 20 | 89 | | foreach (var item in grupo) |
| | | 90 | | { |
| | 6 | 91 | | AplicarClasificacion(conRubro083, item.ClasificacionSiif!, item.Importe); |
| | 6 | 92 | | if (!item.Rubro083) |
| | 5 | 93 | | AplicarClasificacion(sinRubro083, item.ClasificacionSiif!, item.Importe); |
| | | 94 | | } |
| | | 95 | | |
| | 4 | 96 | | totalesGenerales.Acumular(conRubro083); |
| | 4 | 97 | | totalesGeneralesSin083.Acumular(sinRubro083); |
| | | 98 | | |
| | 4 | 99 | | filas.Add(new SiifFilaProgramaDto |
| | 4 | 100 | | { |
| | 4 | 101 | | Programa = grupo.Key.ProgramaNombre, |
| | 4 | 102 | | ProgramaId = grupo.Key.ProgramaId ?? 0, |
| | 4 | 103 | | Regimen = grupo.Key.RegimenNombre, |
| | 4 | 104 | | RegimenId = grupo.Key.RegimenId ?? 0, |
| | 4 | 105 | | ConRubro083 = conRubro083, |
| | 4 | 106 | | SinRubro083 = sinRubro083 |
| | 4 | 107 | | }); |
| | | 108 | | } |
| | | 109 | | |
| | 3 | 110 | | return new SiifResumenDto |
| | 3 | 111 | | { |
| | 3 | 112 | | Filas = filas, |
| | 3 | 113 | | TotalesGenerales = totalesGenerales, |
| | 3 | 114 | | TotalesGeneralesSin083 = totalesGeneralesSin083 |
| | 3 | 115 | | }; |
| | 3 | 116 | | } |
| | | 117 | | |
| | | 118 | | private static void AplicarClasificacion(SiifSubtotalesDto dto, string clasificacion, decimal importe) |
| | | 119 | | { |
| | | 120 | | switch (clasificacion) |
| | | 121 | | { |
| | 18 | 122 | | case "HabNorGrab": dto.HabNorGrab += importe; break; |
| | 0 | 123 | | case "HabsAnGrab": dto.HabsAnGrab += importe; break; |
| | 0 | 124 | | case "HabsNoGrab": dto.HabsNoGrab += importe; break; |
| | 0 | 125 | | case "BenSocGrab": dto.BenSocGrab += importe; break; |
| | 0 | 126 | | case "BenSocNoGr": dto.BenSocNoGr += importe; break; |
| | 4 | 127 | | case "DtoLegGrab": dto.DtoLegGrab += importe; break; |
| | 0 | 128 | | case "DtoLegNoGr": dto.DtoLegNoGr += importe; break; |
| | 0 | 129 | | case "DtoPerGrab": dto.DtoPerGrab += importe; break; |
| | | 130 | | } |
| | 0 | 131 | | } |
| | | 132 | | } |