From 24ba0f63147ac4b3b5798e464f7bfc2fb7f7a62b Mon Sep 17 00:00:00 2001 From: femsci Date: Sat, 18 Nov 2023 05:59:32 +0100 Subject: [PATCH] absolutely horrendous --- src/Nyanlabs.Umogen.Core/Models/Person.cs | 2 +- .../Models/UmoAnalModel.cs | 21 +++++++++ .../Models/UmoDocumentResult.cs | 25 ++++++---- src/Nyanlabs.Umogen.Core/Pdf/PdfRenderer.cs | 6 --- src/Nyanlabs.Umogen.Core/Umogen.cs | 2 +- .../GenerationTests.cs | 13 ++--- test/Nyanlabs.Umogen.WebTests/PdfTest.cs | 47 +++++++++++++++++++ 7 files changed, 92 insertions(+), 24 deletions(-) create mode 100644 src/Nyanlabs.Umogen.Core/Models/UmoAnalModel.cs delete mode 100644 src/Nyanlabs.Umogen.Core/Pdf/PdfRenderer.cs create mode 100644 test/Nyanlabs.Umogen.WebTests/PdfTest.cs diff --git a/src/Nyanlabs.Umogen.Core/Models/Person.cs b/src/Nyanlabs.Umogen.Core/Models/Person.cs index 4804b1f..b833009 100644 --- a/src/Nyanlabs.Umogen.Core/Models/Person.cs +++ b/src/Nyanlabs.Umogen.Core/Models/Person.cs @@ -25,6 +25,6 @@ public record Person(string Name, string Surname, string? Id = null, string? Pes public string SerializeToDocument() { - return $"{Name} {Surname}{(Pesel != null ? $" PESEL {Pesel}" : "")}{(IDCode != null ? $" legitymującą się dowodem {IDCode}" : "")}{(DoB != null ? $" ur. {DoB.Value:dd MMM yyyy}" : "")}"; + return $"{Name} {Surname}{(Pesel != null ? $" PESEL {Pesel}" : "")}{(IDCode != null ? $" legitymując{(Pesel == null ? 'e' : (((int.Parse(Pesel[^2].ToString()) & 0x1) == 0x1) ? 'y' : 'a'))} się dowodem {IDCode}" : "")}"; } } diff --git a/src/Nyanlabs.Umogen.Core/Models/UmoAnalModel.cs b/src/Nyanlabs.Umogen.Core/Models/UmoAnalModel.cs new file mode 100644 index 0000000..49ce09c --- /dev/null +++ b/src/Nyanlabs.Umogen.Core/Models/UmoAnalModel.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace Nyanlabs.Umogen.Core.Models; + +public class UmoAnalModel +{ + [JsonPropertyName("c_type")] + public string ContractType { get; set; } + [JsonPropertyName("j_pos")] + public string JobPosition { get; set; } + [JsonPropertyName("k_lit")] + public string ValueLiteral { get; set; } + [JsonPropertyName("conds")] + public ICollection Conditions { get; set; } + + public UmoDoctype Doctype => UmoDoctypeBindings.GetFromName(ContractType).Value; +} diff --git a/src/Nyanlabs.Umogen.Core/Models/UmoDocumentResult.cs b/src/Nyanlabs.Umogen.Core/Models/UmoDocumentResult.cs index 0cab7e1..0def101 100644 --- a/src/Nyanlabs.Umogen.Core/Models/UmoDocumentResult.cs +++ b/src/Nyanlabs.Umogen.Core/Models/UmoDocumentResult.cs @@ -3,13 +3,14 @@ using System.Diagnostics; namespace Nyanlabs.Umogen.Core.Models; -public class UmoDocumentResult(UmoDoctype type, Person employee, IEntity employer, ValidTime? validity, string? content) +public class UmoDocumentResult(UmoDoctype? type, Person employee, IEntity employer, ValidTime? validity, decimal? payment, UmoAnalModel anal) { - public UmoDoctype DocType { get; } = type; + public UmoDoctype DocType { get; set; } = type ?? anal.Doctype; public Person Employee { get; } = employee; public IEntity Employer { get; } = employer; public ValidTime? Validity { get; } = validity; - public string? Content { get; } = content; + public decimal? Payment { get; } = payment; + public UmoAnalModel Anal { get; } = anal; const string ISSUE_DATE = "$DATETIME_ISSUE", ENACT_DATE = "$DATETIME", @@ -17,24 +18,29 @@ public class UmoDocumentResult(UmoDoctype type, Person employee, IEntity employe EMPLOYEE = "$EMPLOYEE", VALID_TIME = "$VALIDITY_TIME", WORK_TYPE = "$WORK_TYPE", - DYNAMIC_DATA = "$WORK_DYNAMIC_DATA", + DYNAMIC_DATA = "$$WORK_DYNAMIC_DATA$$", + PAYMENT = "$PAYMENT", + PAYMENT_LIT = "$PAYMENT_LIT", START_DATE = "$WORK_START_DATE"; - public async Task ProcessPdf() + public async Task ProcessPdf(string? template = null) { - string xml = await File.ReadAllTextAsync(DocType.TemplateDoc()); + string xml = await File.ReadAllTextAsync(template ?? DocType.TemplateDoc()); xml = xml.Replace(EMPLOYER, Employer.SerializeToDocument()) .Replace(EMPLOYEE, Employee.SerializeToDocument()) .Replace(VALID_TIME, (Validity ?? ValidTime.Invalid).SerializeToDocument()) .Replace(ENACT_DATE, DateTime.Now.ToString("dd.MM.yyyy")) .Replace(ISSUE_DATE, DateTime.Now.ToString("dd.MM.yyyy")) - .Replace(DYNAMIC_DATA, Content); + .Replace(PAYMENT_LIT, Anal.ValueLiteral) + .Replace(PAYMENT, Payment.Value.ToString()) + .Replace(WORK_TYPE, Anal.JobPosition) + .Replace(DYNAMIC_DATA, string.Join('\n', Anal.Conditions.Select(a => $"- {a}"))); string tmp = Path.GetTempFileName(); await File.WriteAllTextAsync(tmp, xml); await Task.Run(async () => { - var proc = Process.Start("soffice", $"--headless --convert-to pdf {tmp}"); + var proc = Process.Start("soffice", $"--headless --convert-to pdf --outdir /tmp/ {tmp}"); await proc.WaitForExitAsync(); if (proc.ExitCode != 0) { @@ -44,6 +50,9 @@ public class UmoDocumentResult(UmoDoctype type, Person employee, IEntity employe byte[] data = await File.ReadAllBytesAsync(Path.ChangeExtension(tmp, "pdf")); + File.Delete(tmp); + File.Delete(Path.ChangeExtension(tmp, "pdf")); + return data; } } diff --git a/src/Nyanlabs.Umogen.Core/Pdf/PdfRenderer.cs b/src/Nyanlabs.Umogen.Core/Pdf/PdfRenderer.cs deleted file mode 100644 index d4220ed..0000000 --- a/src/Nyanlabs.Umogen.Core/Pdf/PdfRenderer.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Nyanlabs.Umogen.Core.Pdf; - -public class PdfRenderer -{ - -} diff --git a/src/Nyanlabs.Umogen.Core/Umogen.cs b/src/Nyanlabs.Umogen.Core/Umogen.cs index b2ec03f..f8471c6 100644 --- a/src/Nyanlabs.Umogen.Core/Umogen.cs +++ b/src/Nyanlabs.Umogen.Core/Umogen.cs @@ -5,7 +5,7 @@ namespace Nyanlabs.Umogen.Core; public class Umogen { public const string DEFAULT_API_KEY_FILE = "umogenkey.secret"; - public const string PROMPT = "You analyze employment prompt requests and return document type (one of these 'Umowa o Pracę', 'Umowa Zlecenie', 'Umowa o Dzieło') and elaborate description of job details as a list separated by hyphens and newlines. You write them in format: '\n'"; + public const string PROMPT = "You analyze employment prompt requests and return document type (one of these 'Umowa o Pracę', 'Umowa Zlecenie', 'Umowa o Dzieło') and elaborate description of job details as a list separated by hyphens and newlines. Also give a very generalized job position. You write them in format: '{ \"c_type\": \"\", \"j_pos\": \"\", \"k_lit\": \"\", \"conds\": [ ]'"; public static readonly JsonSerializerOptions JSON_OPTS = new(JsonSerializerDefaults.Web) { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower diff --git a/test/Nyanlabs.Umogen.WebTests/GenerationTests.cs b/test/Nyanlabs.Umogen.WebTests/GenerationTests.cs index 3448585..eed01b6 100644 --- a/test/Nyanlabs.Umogen.WebTests/GenerationTests.cs +++ b/test/Nyanlabs.Umogen.WebTests/GenerationTests.cs @@ -21,14 +21,11 @@ public class GenerationTests UmoDocument doc = new(UmoDoctype.CONTR_EMPLOYMENT, ModelEmployer, ModelEmployee); UmoProcess proc = new(eng); - var enu = proc.Ask("I would like to employ Jan Kowalski ID NO CBS3727348 for part time job as a shopkeeper."); - - await foreach (var str in enu) - { - Console.Write(str); - } - - Console.WriteLine("~"); + // var enu = proc.Ask("I would like to employ Jan Kowalski ID NO CBS3727348 for part time job as a shopkeeper and pay 1400zł."); + // await foreach (var str in enu) + // { + // Console.Write(str); + // } } } diff --git a/test/Nyanlabs.Umogen.WebTests/PdfTest.cs b/test/Nyanlabs.Umogen.WebTests/PdfTest.cs new file mode 100644 index 0000000..7987c31 --- /dev/null +++ b/test/Nyanlabs.Umogen.WebTests/PdfTest.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +using Nyanlabs.Umogen.Core; +using Nyanlabs.Umogen.Core.Models; + +namespace Nyanlabs.Umogen.CoreTests; + +public class PdfTest +{ + + private Person ModelEmployer => new("Kotek", "Miauczyński", "CBS4327563", "04281308999", new DateTime(2004, 08, 13)); + + private Person ModelEmployee => new("Miau", "Kotczyński", "CBS69696969", "76011694336", new DateTime(1976, 1, 16)); + + [Fact] + public async void RenderBoi() + { + // Given + string root = Environment.CurrentDirectory.Replace("test/Nyanlabs.Umogen.WebTests/bin/Debug/net8.0", ""); + UmoEngine eng = new(Path.Combine(root, Core.Umogen.DEFAULT_API_KEY_FILE)); + + UmoProcess p = new(eng); + + StringBuilder sb = new(); + + await foreach (var str in p.Ask($"Zatrudnij {ModelEmployee.NLQuerySerialize()} jako programistę z wynagrodzeniem 12137zł.")) + { + Console.Write(str); + sb.Append(str); + } + + var anal = JsonSerializer.Deserialize(sb.ToString(), Core.Umogen.JSON_OPTS); + UmoDocumentResult r = new(null, ModelEmployer, ModelEmployee, ValidTime.Invalid, 2137, anal); + + // When + byte[] b = await r.ProcessPdf(Path.Combine(root, $"templates/{r.DocType.TemplateDoc()}")); + // Then + + await File.WriteAllBytesAsync(Path.Combine(root, "out.pdf"), b); + + } +}