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>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="itext7" Version="7.2.5" />
|
||||||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
|
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
|||||||
@ -6,6 +6,8 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using iText.Kernel.Pdf;
|
||||||
|
using iText.Kernel.Utils;
|
||||||
|
|
||||||
public class Fatura
|
public class Fatura
|
||||||
{
|
{
|
||||||
@ -17,6 +19,7 @@
|
|||||||
private string? pastaMiddle;
|
private string? pastaMiddle;
|
||||||
private string? uc;
|
private string? uc;
|
||||||
private int mes;
|
private int mes;
|
||||||
|
private int? pagina;
|
||||||
|
|
||||||
public Fatura(string id, string fatura_path, HttpClient httpClient)
|
public Fatura(string id, string fatura_path, HttpClient httpClient)
|
||||||
{
|
{
|
||||||
@ -31,6 +34,11 @@
|
|||||||
this.Arquivo = new FileInfo(fatura_path);
|
this.Arquivo = new FileInfo(fatura_path);
|
||||||
this.id = id;
|
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)
|
if (this.Agrupada)
|
||||||
{
|
{
|
||||||
this.Agrupada_children = this.faturaParsed.GetProperty("children").EnumerateArray();
|
this.Agrupada_children = this.faturaParsed.GetProperty("children").EnumerateArray();
|
||||||
@ -156,7 +164,7 @@
|
|||||||
|
|
||||||
// Loop entre os dados faturados na fatura
|
// Loop entre os dados faturados na fatura
|
||||||
int j = 0;
|
int j = 0;
|
||||||
StringBuilder insertOthers = new StringBuilder();
|
List<string> insertOthers = new List<string>();
|
||||||
foreach (Item item in parsedResult.items)
|
foreach (Item item in parsedResult.items)
|
||||||
{
|
{
|
||||||
switch (item.type, item.period)
|
switch (item.type, item.period)
|
||||||
@ -174,11 +182,13 @@
|
|||||||
// Demanda Ponta
|
// Demanda Ponta
|
||||||
case ("demand", "peak"):
|
case ("demand", "peak"):
|
||||||
dadosTusd.Dem_Cont_P = item.contract == 0 ? dadosTusd.Dem_Cont_P : item.contract;
|
dadosTusd.Dem_Cont_P = item.contract == 0 ? dadosTusd.Dem_Cont_P : item.contract;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Demanda Fora de Ponta
|
// Demanda Fora de Ponta
|
||||||
case ("demand", "off-peak"):
|
case ("demand", "off-peak"):
|
||||||
dadosTusd.Dem_Cont_FP = item.contract == 0 ? dadosTusd.Dem_Cont_FP : item.contract;
|
dadosTusd.Dem_Cont_FP = item.contract == 0 ? dadosTusd.Dem_Cont_FP : item.contract;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Ilum. publica
|
// Ilum. publica
|
||||||
@ -211,13 +221,9 @@
|
|||||||
string deleteOthers = $"DELETE FROM Dados_TUSD_aux WHERE Cod_TUSD = {dadosTusd.Cod_TUSD}";
|
string deleteOthers = $"DELETE FROM Dados_TUSD_aux WHERE Cod_TUSD = {dadosTusd.Cod_TUSD}";
|
||||||
cmd.CommandText = deleteOthers;
|
cmd.CommandText = deleteOthers;
|
||||||
cmd.ExecuteNonQuery();
|
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 + ")");
|
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 + ")");
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
insertOthers.Append(",(" + dadosTusd.Cod_TUSD + "," + j + ",'" + item.name + "'," + item.charge.ToString(new CultureInfo("en-US")) + "," + 0 + ")");
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -289,9 +295,15 @@
|
|||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
|
|
||||||
// Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas
|
// Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas
|
||||||
cmd.CommandText = insertOthers.ToString();
|
foreach (string insert in insertOthers)
|
||||||
|
{
|
||||||
|
if (insert.Length != 0)
|
||||||
|
{
|
||||||
|
cmd.CommandText = insert;
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Cria o comando para lançar da fatura
|
// Cria o comando para lançar da fatura
|
||||||
@ -339,9 +351,15 @@
|
|||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
|
|
||||||
// Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas
|
// Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas
|
||||||
cmd.CommandText = insertOthers.ToString();
|
foreach (string insert in insertOthers)
|
||||||
|
{
|
||||||
|
if (insert.Length != 0)
|
||||||
|
{
|
||||||
|
cmd.CommandText = insert;
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.Status = "FATURA INCLUIDA NO BD";
|
this.Status = "FATURA INCLUIDA NO BD";
|
||||||
this.uc = uc;
|
this.uc = uc;
|
||||||
@ -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":
|
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}");
|
|
||||||
|
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;
|
break;
|
||||||
|
|
||||||
case "FATURA DUPLICADA NO BD":
|
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}");
|
|
||||||
|
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;
|
break;
|
||||||
|
|
||||||
case "FATURA INCLUIDA NO BD":
|
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}");
|
|
||||||
|
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;
|
break;
|
||||||
|
|
||||||
case "INVALID":
|
case ("INVALID", _):
|
||||||
this.Arquivo!.MoveTo(this.Arquivo.Directory?.Parent?.FullName + $@"\7 - ERRO\ID: {this.id!} - {this.Arquivo.Name}");
|
|
||||||
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -390,4 +466,33 @@
|
|||||||
return JsonDocument.Parse(response.Content.ReadAsStringAsync().Result).RootElement.GetProperty("access_token").GetString() !;
|
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", "SA1600:ElementsMustBeDocumented", Justification = "Revisado.")]
|
||||||
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1633:File should have header", 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";
|
public const String LogFaturas2 = "import2.txt";
|
||||||
#endif
|
#endif
|
||||||
public const string CaminhoDB = "X:/Middle/Informativo Setorial/Modelo Word/BD1_dados cadastrais e faturas.accdb";
|
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 OleDbConnection conn = new (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + CaminhoDB + ";Jet OLEDB:Database Password=gds21");
|
||||||
private static OleDbCommand cmd = new ();
|
private static OleDbCommand cmd = new ();
|
||||||
private static StreamReader sr = new StreamReader(LogFaturas);
|
private static StreamReader sr = new (LogFaturas);
|
||||||
private static StreamWriter sw = new StreamWriter(LogFaturas2);
|
private static StreamWriter sw = new (LogFaturas2);
|
||||||
private static string? fatura;
|
private static string? fatura;
|
||||||
|
|
||||||
public static void Main()
|
public static void Main()
|
||||||
@ -38,7 +38,7 @@
|
|||||||
string fatura_arquivo = fatura.Split(",")[2];
|
string fatura_arquivo = fatura.Split(",")[2];
|
||||||
|
|
||||||
// Verifica se a fatura foi processada e atualiza os valores para banco de dados
|
// 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
|
// Verifica se a fatura foi processada e atualiza os valores para o banco de dados
|
||||||
Fatura fatura = new Fatura(fatura_ID, fatura_arquivo, httpClient);
|
Fatura fatura = new Fatura(fatura_ID, fatura_arquivo, httpClient);
|
||||||
@ -46,32 +46,34 @@
|
|||||||
if (fatura.Status == "SUCCESS" & !fatura.Agrupada)
|
if (fatura.Status == "SUCCESS" & !fatura.Agrupada)
|
||||||
{
|
{
|
||||||
fatura.Processar(cmd);
|
fatura.Processar(cmd);
|
||||||
fatura.Mover();
|
fatura.Mover(separar: false);
|
||||||
sw.WriteLine(fatura_ID + "," + fatura.Status + "," + fatura_arquivo);
|
sw.WriteLine(fatura_ID + "," + fatura.Status + "," + fatura.Arquivo);
|
||||||
}
|
}
|
||||||
else if (fatura.Status == "SUCCESS" & fatura.Agrupada)
|
else if (fatura.Status == "SUCCESS" & fatura.Agrupada)
|
||||||
{
|
{
|
||||||
foreach (JsonElement individual_ID in fatura.Agrupada_children)
|
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")
|
if (faturaIndividual.Status == "SUCCESS")
|
||||||
{
|
{
|
||||||
faturaIndividual.Processar(cmd);
|
faturaIndividual.Processar(cmd);
|
||||||
fatura.Mover();
|
faturaIndividual.Mover(separar: true);
|
||||||
sw.WriteLine(individual_ID.ToString() + "," + faturaIndividual.Status + "," + fatura_arquivo);
|
sw.WriteLine(individual_ID.ToString() + "," + faturaIndividual.Status + "," + faturaIndividual.Arquivo);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
sw.WriteLine(individual_ID.ToString() + "," + faturaIndividual.Status + "," + fatura_arquivo);
|
faturaIndividual.Mover(separar: true);
|
||||||
fatura.Mover();
|
sw.WriteLine(individual_ID.ToString() + "," + faturaIndividual.Status + "," + faturaIndividual.Arquivo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fatura.Arquivo!.Delete();
|
||||||
}
|
}
|
||||||
else if (fatura.Status == "INVALID")
|
else if (fatura.Status == "INVALID")
|
||||||
{
|
{
|
||||||
fatura.Mover();
|
fatura.Mover(separar: false);
|
||||||
sw.WriteLine(fatura_ID + "," + fatura.Status + "," + fatura_arquivo);
|
sw.WriteLine(fatura_ID + "," + fatura.Status + "," + fatura.Arquivo);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@ -47,7 +47,7 @@ public class Program
|
|||||||
if (response.IsSuccessStatusCode)
|
if (response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
var iD = JsonDocument.Parse(response.Content.ReadAsStringAsync().Result).RootElement.GetProperty("requestId");
|
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(iD);
|
||||||
sw.Write(",");
|
sw.Write(",");
|
||||||
sw.Write(",");
|
sw.Write(",");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user