Metodo mover também realiza a separação de arquivos individuais no caso de faturas agrupadas.
Itens classificados como "other" são inseridos após inserir a TUSD devido a relação entre as tabelas.
This commit is contained in:
parent
a7b176ae12
commit
ba61bbae95
@ -9,6 +9,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="itext7" Version="7.2.5" />
|
||||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
||||
@ -6,6 +6,8 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using iText.Kernel.Pdf;
|
||||
using iText.Kernel.Utils;
|
||||
|
||||
public class Fatura
|
||||
{
|
||||
@ -17,6 +19,7 @@
|
||||
private string? pastaMiddle;
|
||||
private string? uc;
|
||||
private int mes;
|
||||
private int? pagina;
|
||||
|
||||
public Fatura(string id, string fatura_path, HttpClient httpClient)
|
||||
{
|
||||
@ -31,6 +34,11 @@
|
||||
this.Arquivo = new FileInfo(fatura_path);
|
||||
this.id = id;
|
||||
|
||||
if (this.faturaParsed.GetProperty("external").GetString() is not null)
|
||||
{
|
||||
this.pagina = JsonDocument.Parse(this.faturaParsed.GetProperty("external").GetString() !).RootElement.GetProperty("origin")[0].GetProperty("page").GetInt32();
|
||||
}
|
||||
|
||||
if (this.Agrupada)
|
||||
{
|
||||
this.Agrupada_children = this.faturaParsed.GetProperty("children").EnumerateArray();
|
||||
@ -156,7 +164,7 @@
|
||||
|
||||
// Loop entre os dados faturados na fatura
|
||||
int j = 0;
|
||||
StringBuilder insertOthers = new StringBuilder();
|
||||
List<string> insertOthers = new List<string>();
|
||||
foreach (Item item in parsedResult.items)
|
||||
{
|
||||
switch (item.type, item.period)
|
||||
@ -174,11 +182,13 @@
|
||||
// Demanda Ponta
|
||||
case ("demand", "peak"):
|
||||
dadosTusd.Dem_Cont_P = item.contract == 0 ? dadosTusd.Dem_Cont_P : item.contract;
|
||||
|
||||
break;
|
||||
|
||||
// Demanda Fora de Ponta
|
||||
case ("demand", "off-peak"):
|
||||
dadosTusd.Dem_Cont_FP = item.contract == 0 ? dadosTusd.Dem_Cont_FP : item.contract;
|
||||
|
||||
break;
|
||||
|
||||
// Ilum. publica
|
||||
@ -211,13 +221,9 @@
|
||||
string deleteOthers = $"DELETE FROM Dados_TUSD_aux WHERE Cod_TUSD = {dadosTusd.Cod_TUSD}";
|
||||
cmd.CommandText = deleteOthers;
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
insertOthers.Append("INSERT INTO Dados_TUSD_aux (Cod_TUSD,Id,Nome,Valor,Cod_lanc) VALUES (" + dadosTusd.Cod_TUSD + "," + j + ",'" + item.name + "'," + item.charge.ToString(new CultureInfo("en-US")) + "," + 0 + ")");
|
||||
}
|
||||
else
|
||||
{
|
||||
insertOthers.Append(",(" + dadosTusd.Cod_TUSD + "," + j + ",'" + item.name + "'," + item.charge.ToString(new CultureInfo("en-US")) + "," + 0 + ")");
|
||||
}
|
||||
insertOthers.Add("INSERT INTO Dados_TUSD_aux (Cod_TUSD,Id,Nome,Valor,Cod_lanc) VALUES (" + dadosTusd.Cod_TUSD + "," + j + ",'" + item.name + "'," + item.charge.ToString(new CultureInfo("en-US")) + "," + 0 + ")");
|
||||
|
||||
break;
|
||||
}
|
||||
@ -289,8 +295,14 @@
|
||||
cmd.ExecuteNonQuery();
|
||||
|
||||
// Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas
|
||||
cmd.CommandText = insertOthers.ToString();
|
||||
cmd.ExecuteNonQuery();
|
||||
foreach (string insert in insertOthers)
|
||||
{
|
||||
if (insert.Length != 0)
|
||||
{
|
||||
cmd.CommandText = insert;
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -339,8 +351,14 @@
|
||||
cmd.ExecuteNonQuery();
|
||||
|
||||
// Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas
|
||||
cmd.CommandText = insertOthers.ToString();
|
||||
cmd.ExecuteNonQuery();
|
||||
foreach (string insert in insertOthers)
|
||||
{
|
||||
if (insert.Length != 0)
|
||||
{
|
||||
cmd.CommandText = insert;
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.Status = "FATURA INCLUIDA NO BD";
|
||||
@ -349,24 +367,82 @@
|
||||
}
|
||||
}
|
||||
|
||||
public void Mover()
|
||||
public void Mover(bool separar)
|
||||
{
|
||||
switch (this.Status)
|
||||
string destino = string.Empty;
|
||||
|
||||
switch (this.Status, separar)
|
||||
{
|
||||
case "UNIDADE CONSUMIDORA NÃO LOCALIZADA NO BD":
|
||||
this.Arquivo!.MoveTo(this.Arquivo.Directory?.Parent?.FullName + $@"\5 - {this.Status}\ID: {this.id!} - Mês: {this.mes} - UC: {this.uc} - {this.Arquivo.Name}");
|
||||
case ("UNIDADE CONSUMIDORA NÃO LOCALIZADA NO BD", _):
|
||||
|
||||
destino = this.Arquivo?.Directory?.Parent?.FullName + $@"\5 - {this.Status}\ID {this.id!} - Mês {this.mes} - UC {this.uc}.pdf";
|
||||
|
||||
if (separar)
|
||||
{
|
||||
new PDFSplitter(this.pagina, this.Arquivo!.ToString(), destino);
|
||||
this.Arquivo = new FileInfo(destino);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Arquivo!.MoveTo(destino);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "FATURA DUPLICADA NO BD":
|
||||
this.Arquivo!.MoveTo(this.Arquivo.Directory?.Parent?.FullName + $@"\4 - {this.Status}\ID: {this.id!} - Mês: {this.mes} - Empresa: {this.empresa} - Unidade: {this.unidade}");
|
||||
case ("FATURA DUPLICADA NO BD", _):
|
||||
|
||||
destino = this.Arquivo?.Directory?.Parent?.FullName + $@"\4 - {this.Status}\ID {this.id!} - Mês {this.mes} - Empresa {this.empresa} - Unidade {this.unidade}.pdf";
|
||||
|
||||
if (separar)
|
||||
{
|
||||
new PDFSplitter(this.pagina, this.Arquivo!.ToString(), destino);
|
||||
this.Arquivo = new FileInfo(destino);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Arquivo!.MoveTo(destino);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "FATURA INCLUIDA NO BD":
|
||||
this.Arquivo!.MoveTo(this.Arquivo.Directory?.Parent?.FullName + $@"\6 - {this.Status}\ID: {this.id!} - Mês: {this.uc} - Empresa: {this.empresa} - Unidade: {this.unidade}");
|
||||
case ("FATURA INCLUIDA NO BD", _):
|
||||
|
||||
destino = this.Arquivo?.Directory?.Parent?.FullName + $@"\6 - {this.Status}\ID {this.id!} - Mês {this.mes} - Empresa {this.empresa} - Unidade {this.unidade}.pdf";
|
||||
|
||||
if (separar)
|
||||
{
|
||||
new PDFSplitter(this.pagina, this.Arquivo!.ToString(), destino);
|
||||
this.Arquivo = new FileInfo(destino);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Arquivo!.MoveTo(destino);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "INVALID":
|
||||
this.Arquivo!.MoveTo(this.Arquivo.Directory?.Parent?.FullName + $@"\7 - ERRO\ID: {this.id!} - {this.Arquivo.Name}");
|
||||
case ("INVALID", _):
|
||||
|
||||
destino = this.Arquivo?.Directory?.Parent?.FullName + $@"\7 - ERRO\ID {this.id!} - {this.Arquivo?.Name}";
|
||||
|
||||
if (separar)
|
||||
{
|
||||
new PDFSplitter(this.pagina, this.Arquivo!.ToString(), destino);
|
||||
this.Arquivo = new FileInfo(destino);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Arquivo!.MoveTo(destino);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case (_, true):
|
||||
|
||||
destino = this.Arquivo?.Directory?.Parent?.FullName + $@"\3 - PROCESSANDO\ID {this.id!} - {this.Arquivo?.Name}";
|
||||
|
||||
new PDFSplitter(this.pagina, this.Arquivo!.ToString(), destino);
|
||||
this.Arquivo = new FileInfo(destino);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -390,4 +466,33 @@
|
||||
return JsonDocument.Parse(response.Content.ReadAsStringAsync().Result).RootElement.GetProperty("access_token").GetString() !;
|
||||
}
|
||||
}
|
||||
|
||||
public class PDFSplitter
|
||||
{
|
||||
public PDFSplitter(int? pagina, string origem, string destino)
|
||||
{
|
||||
FileStream document = new FileStream(origem, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
PdfDocument pdfDocument = new PdfDocument(new PdfReader(document));
|
||||
var split = new CustomPdfSplitter(pdfDocument, pageRange => new PdfWriter(destino));
|
||||
PdfDocument result = split.ExtractPageRange(new PageRange(pagina.ToString()));
|
||||
document.Close();
|
||||
result.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public class CustomPdfSplitter : PdfSplitter
|
||||
{
|
||||
private Func<PageRange, PdfWriter> nextWriter;
|
||||
|
||||
public CustomPdfSplitter(PdfDocument pdfDocument, Func<PageRange, PdfWriter> nextWriter)
|
||||
: base(pdfDocument)
|
||||
{
|
||||
this.nextWriter = nextWriter;
|
||||
}
|
||||
|
||||
protected override PdfWriter GetNextPdfWriter(PageRange documentPageRange)
|
||||
{
|
||||
return this.nextWriter.Invoke(documentPageRange);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -7,3 +7,5 @@ using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented", Justification = "Revisado.")]
|
||||
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1633:File should have header", Justification = "Revisado.")]
|
||||
[assembly: SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Revisado", Scope = "type", Target = "~T:Download_Faturas.PDFSplitter")]
|
||||
[assembly: SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Revisado", Scope = "type", Target = "~T:Download_Faturas.CustomPdfSplitter")]
|
||||
|
||||
@ -17,11 +17,11 @@
|
||||
public const String LogFaturas2 = "import2.txt";
|
||||
#endif
|
||||
public const string CaminhoDB = "X:/Middle/Informativo Setorial/Modelo Word/BD1_dados cadastrais e faturas.accdb";
|
||||
private static HttpClient httpClient = new HttpClient();
|
||||
private static HttpClient httpClient = new ();
|
||||
private static OleDbConnection conn = new (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + CaminhoDB + ";Jet OLEDB:Database Password=gds21");
|
||||
private static OleDbCommand cmd = new ();
|
||||
private static StreamReader sr = new StreamReader(LogFaturas);
|
||||
private static StreamWriter sw = new StreamWriter(LogFaturas2);
|
||||
private static StreamReader sr = new (LogFaturas);
|
||||
private static StreamWriter sw = new (LogFaturas2);
|
||||
private static string? fatura;
|
||||
|
||||
public static void Main()
|
||||
@ -38,7 +38,7 @@
|
||||
string fatura_arquivo = fatura.Split(",")[2];
|
||||
|
||||
// Verifica se a fatura foi processada e atualiza os valores para banco de dados
|
||||
if (fatura_status == "DELAYED" | fatura_status == "ACTIONABLE" | fatura_status == string.Empty | fatura_status == "UNIDADE CONSUMIDORA NÃO LOCALIZADA NO BD")
|
||||
if (fatura_status == "DELAYED" | fatura_status == "MULTACTIONABLE" | fatura_status == "ACTIONABLE" | fatura_status == string.Empty | fatura_status == "UNIDADE CONSUMIDORA NÃO LOCALIZADA NO BD")
|
||||
{
|
||||
// Verifica se a fatura foi processada e atualiza os valores para o banco de dados
|
||||
Fatura fatura = new Fatura(fatura_ID, fatura_arquivo, httpClient);
|
||||
@ -46,32 +46,34 @@
|
||||
if (fatura.Status == "SUCCESS" & !fatura.Agrupada)
|
||||
{
|
||||
fatura.Processar(cmd);
|
||||
fatura.Mover();
|
||||
sw.WriteLine(fatura_ID + "," + fatura.Status + "," + fatura_arquivo);
|
||||
fatura.Mover(separar: false);
|
||||
sw.WriteLine(fatura_ID + "," + fatura.Status + "," + fatura.Arquivo);
|
||||
}
|
||||
else if (fatura.Status == "SUCCESS" & fatura.Agrupada)
|
||||
{
|
||||
foreach (JsonElement individual_ID in fatura.Agrupada_children)
|
||||
{
|
||||
Fatura faturaIndividual = new Fatura(individual_ID.ToString(), fatura_arquivo, httpClient);
|
||||
Fatura faturaIndividual = new (individual_ID.ToString(), fatura_arquivo, httpClient);
|
||||
|
||||
if (faturaIndividual.Status == "SUCCESS")
|
||||
{
|
||||
faturaIndividual.Processar(cmd);
|
||||
fatura.Mover();
|
||||
sw.WriteLine(individual_ID.ToString() + "," + faturaIndividual.Status + "," + fatura_arquivo);
|
||||
faturaIndividual.Mover(separar: true);
|
||||
sw.WriteLine(individual_ID.ToString() + "," + faturaIndividual.Status + "," + faturaIndividual.Arquivo);
|
||||
}
|
||||
else
|
||||
{
|
||||
sw.WriteLine(individual_ID.ToString() + "," + faturaIndividual.Status + "," + fatura_arquivo);
|
||||
fatura.Mover();
|
||||
faturaIndividual.Mover(separar: true);
|
||||
sw.WriteLine(individual_ID.ToString() + "," + faturaIndividual.Status + "," + faturaIndividual.Arquivo);
|
||||
}
|
||||
}
|
||||
|
||||
fatura.Arquivo!.Delete();
|
||||
}
|
||||
else if (fatura.Status == "INVALID")
|
||||
{
|
||||
fatura.Mover();
|
||||
sw.WriteLine(fatura_ID + "," + fatura.Status + "," + fatura_arquivo);
|
||||
fatura.Mover(separar: false);
|
||||
sw.WriteLine(fatura_ID + "," + fatura.Status + "," + fatura.Arquivo);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@ -47,7 +47,7 @@ public class Program
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var iD = JsonDocument.Parse(response.Content.ReadAsStringAsync().Result).RootElement.GetProperty("requestId");
|
||||
fatura.MoveTo(fatura.Directory?.Parent?.FullName + $@"\3 - PROCESSANDO\{iD} - " + fatura.Name);
|
||||
fatura.MoveTo(fatura.Directory?.Parent?.FullName + $@"\3 - PROCESSANDO\ID {iD} - " + fatura.Name);
|
||||
sw.Write(iD);
|
||||
sw.Write(",");
|
||||
sw.Write(",");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user