diff --git a/Controllers/ExcelController.cs b/Controllers/ExcelController.cs new file mode 100644 index 0000000..2629fb7 --- /dev/null +++ b/Controllers/ExcelController.cs @@ -0,0 +1,182 @@ +using Microsoft.AspNetCore.Mvc; +using ImpressãoFaturamento.Models; +using Microsoft.Office.Interop.Excel; +using System.Data.OleDb; + +namespace ImpressãoFaturamento.Controllers + +{ + [ApiController] + [Route("api/[controller]")] + public class ExcelController : ControllerBase + { + [HttpPost] + public IActionResult Post([FromBody] FaturamentoModel data) + { + if (data == null) + { + return BadRequest("Invalid data."); + } + + var filePath = WriteDataToExcel(data); + + if (!string.IsNullOrEmpty(filePath)) + { + return Ok("File created and printed successfully."); + } + + return StatusCode(500, "An error occurred while processing the file."); + } + + private string WriteDataToExcel(FaturamentoModel data) + { + string templatePath = Path.Combine(Directory.GetCurrentDirectory(), "template.xlsx"); + string pdfOutputPath = Path.Combine(Directory.GetCurrentDirectory(), data.DadosCadastrais.NomeUnidade + ".pdf"); + + Application excelApp = new Application(); + Workbooks workbooks = excelApp.Workbooks; + Workbook workbook = workbooks.Open(templatePath); + Worksheet worksheet = (Worksheet)workbook.Worksheets[1]; + + //dados cadastrais + worksheet.Cells[3, "C"].Value = data.DadosCadastrais.NomeCliente; + worksheet.Cells[4, "C"].Value = data.DadosCadastrais.NomeUnidade; + worksheet.Cells[5, "C"].Value = data.DadosCadastrais.MesConsumo; + worksheet.Cells[6, "C"].Value = data.DadosCadastrais.BandeiraCativo; + worksheet.Cells[3, "G"].Value = data.DadosCadastrais.ICMS; + worksheet.Cells[4, "G"].Value = data.DadosCadastrais.PIS_COFINS; + worksheet.Cells[5, "G"].Value = data.DadosCadastrais.FatorTributo; + worksheet.Cells[6, "G"].Value = data.DadosCadastrais.DescontoTUSD; + worksheet.Cells[5, "L"].Value = data.DadosCadastrais.PercentualUnidade; + worksheet.Cells[4, "R"].Value = data.DadosCadastrais.CustoTotalCCEE; + + //dados de faturamento - Cativo + int linha = 11; + foreach (var item in data.Faturamento.Cativo) + { + worksheet.Cells[linha, "B"].Value = item.Nome; + worksheet.Cells[linha, "C"] = item.Quantidade; + worksheet.Cells[linha, "E"] = item.Tarifa; + worksheet.Cells[linha, "G"] = item.Subtotal; + worksheet.Cells[linha, "H"] = item.TipoImposto; + worksheet.Cells[linha, "I"].Value = item.ValorFinal; + + linha++; + } + + //dados de faturamento - Livre + linha = 33; + foreach (var item in data.Faturamento.Livre) + { + worksheet.Cells[linha, "B"].Value = item.Nome; + worksheet.Cells[linha, "C"] = item.Quantidade; + worksheet.Cells[linha, "E"] = item.Tarifa; + worksheet.Cells[linha, "G"] = item.Subtotal; + worksheet.Cells[linha, "H"] = item.TipoImposto; + worksheet.Cells[linha, "I"].Value = item.ValorFinal; + + linha++; + } + + faturamento(workbook, worksheet); + + workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, pdfOutputPath); + workbook.Close(false); + excelApp.Quit(); + + System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); + System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks); + System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); + + return pdfOutputPath; + } + + public void faturamento(Workbook workbook, Worksheet worksheet) + { + float idUnidade = worksheet.Cells[70, "A"].Value; + float mesRef = worksheet.Cells[70, "D"].Value; + float tipoFaturamento = worksheet.Cells[70, "I"].Value; + float cod_fatura = idUnidade + mesRef + tipoFaturamento; + string empresa = worksheet.Cells[70, "B"].Value; + string unidade = worksheet.Cells[70, "C"].Value; + decimal custoCativo = worksheet.Cells[70, "E"].Value; + decimal custoLivre = worksheet.Cells[70, "F"].Value; + decimal economia = worksheet.Cells[70, "G"].Value; + decimal remuneracao = worksheet.Cells[70, "H"].Value; + decimal remuneracaoFixo = worksheet.Cells[70, "J"].Value; + decimal remuneracaoPiso = worksheet.Cells[70, "K"].Value; + decimal remuneracaoTeto = worksheet.Cells[70, "L"].Value; + decimal remuneracaoPercentual = worksheet.Cells[70, "M"].Value; + + int numeroUnidades = Convert.ToInt32(unidade); + + var pdfOutputPath = Path.Combine(Directory.GetCurrentDirectory(), idUnidade + ".pdf"); + + if (remuneracaoFixo != 0 || numeroUnidades == 1) + { + //imprimir remuneracao fixa + workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, pdfOutputPath); + + } + + if (remuneracaoFixo == 0 || numeroUnidades != 1) + { + //imprimir remuneracao piso/teto/percentual + worksheet.Cells[61, "F"].Value = remuneracaoPiso; + //TODO: imprimir + worksheet.Cells[61, "F"].Value = remuneracaoTeto; + //TODO: imprimir + worksheet.Cells[61, "F"].Value = remuneracaoPercentual; + //TODO: imprimir + + workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, pdfOutputPath); + } + + //Conexão com o banco de dados Access + string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=CaminhoParaSeuBancoDeDados.accdb;Jet OLEDB:Database Password=SuaSenha;"; + using (OleDbConnection connection = new OleDbConnection(connectionString)) + { + connection.Open(); + + // Atualizar o registro se ele já existir + string updateQuery = "UPDATE TabelaResumoFaturamento SET ValorTotal = @ValorTotal WHERE IdUnidade = @IdUnidade"; + using (OleDbCommand updateCommand = new OleDbCommand(updateQuery, connection)) + { + updateCommand.Parameters.AddWithValue("@ValorTotal", remuneracao); + updateCommand.Parameters.AddWithValue("@IdUnidade", idUnidade); + int rowsUpdated = updateCommand.ExecuteNonQuery(); + + // Se nenhum registro foi atualizado, inserir um novo registro + if (rowsUpdated == 0) + { + string insertQuery = "INSERT INTO TabelaResumoFaturamento (IdUnidade, MesRef, TipoFaturamento, ValorTotal) VALUES (@IdUnidade, @MesRef, @TipoFaturamento, @ValorTotal)"; + using (OleDbCommand insertCommand = new OleDbCommand(insertQuery, connection)) + { + insertCommand.Parameters.AddWithValue("@IdUnidade", idUnidade); + insertCommand.Parameters.AddWithValue("@MesRef", mesRef); + insertCommand.Parameters.AddWithValue("@TipoFaturamento", tipoFaturamento); + insertCommand.Parameters.AddWithValue("@ValorTotal", remuneracao); + insertCommand.ExecuteNonQuery(); + } + } + } + + // Verificar se é a última unidade faturada + if (unidadesFaturadas == numeroUnidades) + { + //TODO: comparar faturamento global e atualizar faturamento final + //TODO: manter somente o faturamento correto + // Atualizar o resumo de faturamento + string updateQuery = "UPDATE TabelaResumoFaturamento SET ValorTotal = @ValorTotal WHERE IdUnidade = @IdUnidade"; + using (OleDbCommand updateCommand = new OleDbCommand(updateQuery, connection)) + { + updateCommand.Parameters.AddWithValue("@ValorTotal", remuneracao); + updateCommand.Parameters.AddWithValue("@IdUnidade", idUnidade); + updateCommand.ExecuteNonQuery(); + } + } + } + } + } +} + diff --git a/IBIS COPACABANA POSTO 2.pdf b/IBIS COPACABANA POSTO 2.pdf new file mode 100644 index 0000000..002ae12 --- /dev/null +++ b/IBIS COPACABANA POSTO 2.pdf @@ -0,0 +1,107 @@ + RELATÓRIO MENSAL DE ECONOMIA + +Consumidor: HOTÉIS UP ASSET - VAREJISTA ICMS: 24,00% +Unidade: IBIS COPACABANA POSTO 2 PIS/COFINS: 6,70% +Mês de Consumo: 2405 Fator Tributo: 29,09% +Bandeira Cativo: Verde: R$/MWh 0 Desc. TUSD: 50,00% + + CUSTO SIMULADO CATIVO + + Descrição Grandezas Tarifas Subtotal Impostos Valor Final + +Consumo Ponta 6,222 MWh 533,91 R$/MWh R$ 3.321,89 29,09% R$ 4.684,79 + 349,00 R$/MWh 29,09% +Consumo F. Ponta 64,553 MWh R$/MWh R$ 22.528,99 29,09% R$ 31.772,14 + 0,00 R$/MWh 29,09% +Bandeira Tarifária 70,775 MWh 2.892,41 R$/MWh R$ - 29,09% R$ - + R$/kW 29,09% +Encargos Ponta 6,222 MWh 254,76 R$/kW R$ 17.996,05 6,70% R$ 25.379,44 + 0,00 R$/kW 29,09% +Encargos F. Ponta 64,553 MWh 0,00 R$/kW R$ 16.445,52 29,09% R$ 23.192,75 + R$/kVArh 29,09% +Demanda Ponta 0,000 kW 24,70 R$/kVAr R$ - 29,09% R$ - + 49,40 +Demanda Isenta Ponta 0,000 kW 364,41 R$ - R$ - + 24,70 +Demanda F. Ponta 155,520 kW R$ 3.841,34 R$ 5.417,36 + +Ultrapassagem Demanda F. Ponta 155,520 kW R$ 7.682,69 R$ 10.834,73 + +Energia Reativa 0,023 kVArh R$ 8,53 R$ 12,03 + +Demanda Reativa 0,000 kVAr R$ - R$ - + +Iluminação Pública (COSIP) R$ 995,92 + +Reembolso FIC/DIC R$ - + +Multas e Juros R$ - + +Créditos R$ - + +Liminar ICMS R$ - + +Outros R$ - + +Fatura Cativo Grandezas x Tarifas = Subtotal ÷ Impostos = R$ 102.289,16 + + CUSTO LIVRE + + Descrição Grandezas Tarifas Subtotal Impostos Valor Final + +Fatura Energia ACL* 70,775 MWh 211,28 R$/MWh R$ 14.953,34 DEVEC R$ 14.953,34 + MWh 1.573,59 R$/MWh 29,09% +Encargos Ponta 6,222 MWh R$/MWh R$ 9.790,56 29,09% R$ 13.807,42 + kW 254,76 R$/kW 29,09% +Encargos F. Ponta 64,553 kW 0,00 R$/kW R$ 16.445,52 6,70% R$ 23.192,75 + kW 0,00 R$/kW 29,09% +Demanda Ponta 0,000 kW R$/kW R$ - 29,09% R$ - + kVArh 12,35 R$/kVArh 29,09% +Demanda Isenta Ponta 0,000 kVAr 49,40 R$/kVAr R$ - 29,09% R$ - + 364,41 +Demanda F. Ponta 155,520 MWh 24,70 R$ 1.920,67 R$ 2.708,68 + +Ultrapassagem Demanda F. Ponta 155,520 R$ 7.682,69 R$ 10.834,73 + +Energia Reativa 0,023 R$ 8,53 R$ 12,03 + +Demanda Reativa 0,000 R$ - R$ - + +Iluminação Pública (COSIP) R$ 995,92 + +Reembolso FIC/DIC R$ - + +Multas e Juros R$ - + +Créditos R$ - + +Créditos Consumidor Livre R$ - + +Encargos de Conexão R$ - + +DEVEC 70,775 211,28 R$/MWh R$ 14.953,30 24,00% R$ 4.722,09 + +Ajuste Desconto TUSD TUSD R$ - + ENERGIA +Subvenção Tarifária TOTAL R$ 727,17 + +Liminar ICMS R$ - + +Conta Covid + Esc. Hidrica R$ 1.459,25 + +Outros R$ 450,78 + +Total R$ 58.910,82 + +* Preço Ponderado de Compra (Longo Prazo + Curto Prazo - se houver) R$ 14.953,34 + + R$ 73.864,16 + + Custo Cativo: R$ 102.289,16 + 73.864,16 + Custo Livre: R$ + 28.425,00 28% + Economia: R$ + 1.705,50 + Remuneração Smart: R$ + diff --git a/ImpressãoFaturamento.csproj b/ImpressãoFaturamento.csproj new file mode 100644 index 0000000..54a0700 --- /dev/null +++ b/ImpressãoFaturamento.csproj @@ -0,0 +1,27 @@ + + + + net7.0 + enable + enable + + + + + tlbimp + 9 + 1 + 00020813-0000-0000-c000-000000000046 + 0 + false + true + + + + + + + + + + diff --git a/ImpressãoFaturamento.sln b/ImpressãoFaturamento.sln new file mode 100644 index 0000000..83fa724 --- /dev/null +++ b/ImpressãoFaturamento.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33829.357 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImpressãoFaturamento", "ImpressãoFaturamento.csproj", "{9E5A08E3-018B-4124-AD11-9625D5AD4E80}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9E5A08E3-018B-4124-AD11-9625D5AD4E80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9E5A08E3-018B-4124-AD11-9625D5AD4E80}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9E5A08E3-018B-4124-AD11-9625D5AD4E80}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9E5A08E3-018B-4124-AD11-9625D5AD4E80}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F3467393-9A41-4963-84ED-8BA5669EC89A} + EndGlobalSection +EndGlobal diff --git a/Models/DataModel.cs b/Models/DataModel.cs new file mode 100644 index 0000000..44677fa --- /dev/null +++ b/Models/DataModel.cs @@ -0,0 +1,76 @@ +using System.Text.Json.Serialization; + +namespace ImpressãoFaturamento.Models +{ + public class DadosCadastrais + { + [JsonPropertyName("nome_cliente")] + public string NomeCliente { get; set; } + + [JsonPropertyName("nome_unidade")] + public string NomeUnidade { get; set; } + + [JsonPropertyName("mes_consumo")] + public string MesConsumo { get; set; } + + [JsonPropertyName("bandeira_cativo")] + public string BandeiraCativo { get; set; } + + [JsonPropertyName("icms")] + public string ICMS { get; set; } + + [JsonPropertyName("pis_cofins")] + public string PIS_COFINS { get; set; } + + [JsonPropertyName("fator_tributo")] + public string FatorTributo { get; set; } + + [JsonPropertyName("desconto_tusd")] + public string DescontoTUSD { get; set; } + + [JsonPropertyName("percentual_unidade")] + public decimal PercentualUnidade { get; set; } + + [JsonPropertyName("custo_total_CCEE")] + public decimal CustoTotalCCEE { get; set; } + } + + public class ItemFaturamento + { + [JsonPropertyName("nome")] + public string Nome { get; set; } + + [JsonPropertyName("quantidade")] + public decimal? Quantidade { get; set; } + + [JsonPropertyName("tarifa")] + public decimal? Tarifa { get; set; } + + [JsonPropertyName("subtotal")] + public decimal? Subtotal { get; set; } + + [JsonPropertyName("tipo_imposto")] + public string? TipoImposto { get; set; } + + [JsonPropertyName("valor_final")] + public decimal ValorFinal { get; set; } + } + + public class Faturamento + { + [JsonPropertyName("cativo")] + public List Cativo { get; set; } + + [JsonPropertyName("livre")] + public List Livre { get; set; } + } + + public class FaturamentoModel + { + [JsonPropertyName("dados_cadastrais")] + public DadosCadastrais DadosCadastrais { get; set; } + + [JsonPropertyName("faturamento")] + public Faturamento Faturamento { get; set; } + } +} diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..48863a6 --- /dev/null +++ b/Program.cs @@ -0,0 +1,25 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..caa24cb --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:47908", + "sslPort": 44332 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5147", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7099;http://localhost:5147", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/appsettings.Development.json b/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/template.xlsx b/template.xlsx new file mode 100644 index 0000000..c90d7dd Binary files /dev/null and b/template.xlsx differ