From e487b1f5e1f317deee810696a597a00183e7f8d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Armatys?= Date: Wed, 25 Sep 2024 17:24:15 +0200 Subject: [PATCH] Added 'GetInvoice' in 'fakturowniaClient' --- .../Playground.cs | 5 ++- .../Abstractions/IFakturowniaApi.cs | 3 ++ .../Abstractions/IFakturowniaClient.cs | 2 ++ .../SOFTURE.Fakturownia/FakturowniaClient.cs | 32 +++++++++++++++---- .../Models/Api/InvoiceDetails.cs | 2 +- .../Models/Client/Invoice.cs | 4 ++- .../Models/Client/MonthlyStatement.cs | 9 ++---- 7 files changed, 42 insertions(+), 15 deletions(-) diff --git a/FAKTUROWNIA/SOFTURE.Fakturownia.Playground/Playground.cs b/FAKTUROWNIA/SOFTURE.Fakturownia.Playground/Playground.cs index e678d4d..86203bf 100644 --- a/FAKTUROWNIA/SOFTURE.Fakturownia.Playground/Playground.cs +++ b/FAKTUROWNIA/SOFTURE.Fakturownia.Playground/Playground.cs @@ -1,4 +1,5 @@ using SOFTURE.Fakturownia.Abstractions; +using SOFTURE.Fakturownia.Models.Enums; namespace SOFTURE.Fakturownia.Playground; @@ -6,12 +7,14 @@ public sealed class Playground(IFakturowniaClient fakturowniaClient) { public async Task Run() { - // var currentMonthStatement = await fakturowniaClient.GetCurrentMonthStatement(clientId: 135057762); + var currentMonthStatement = await fakturowniaClient.GetCurrentMonthStatement(clientId: 135057762); var monthlyStatement = await fakturowniaClient.GetMonthlyStatement( clientId: 135057762, month: 7, year: 2024 ); + + var invoice = await fakturowniaClient.GetInvoice(invoiceId: 315738314, kind: DocumentKind.Proforma); } } \ No newline at end of file diff --git a/FAKTUROWNIA/SOFTURE.Fakturownia/Abstractions/IFakturowniaApi.cs b/FAKTUROWNIA/SOFTURE.Fakturownia/Abstractions/IFakturowniaApi.cs index 70e5908..dd8a09e 100644 --- a/FAKTUROWNIA/SOFTURE.Fakturownia/Abstractions/IFakturowniaApi.cs +++ b/FAKTUROWNIA/SOFTURE.Fakturownia/Abstractions/IFakturowniaApi.cs @@ -10,4 +10,7 @@ public interface IFakturowniaApi Task>> GetInvoicesAsync( [AliasAs("period")] Period period, [AliasAs("client_id")] int clientId); + + [Get("/invoices/{invoiceId}.json")] + Task> GetInvoiceAsync(int invoiceId); } \ No newline at end of file diff --git a/FAKTUROWNIA/SOFTURE.Fakturownia/Abstractions/IFakturowniaClient.cs b/FAKTUROWNIA/SOFTURE.Fakturownia/Abstractions/IFakturowniaClient.cs index 7b3436e..a34ea89 100644 --- a/FAKTUROWNIA/SOFTURE.Fakturownia/Abstractions/IFakturowniaClient.cs +++ b/FAKTUROWNIA/SOFTURE.Fakturownia/Abstractions/IFakturowniaClient.cs @@ -1,6 +1,7 @@ using CSharpFunctionalExtensions; using SOFTURE.Fakturownia.Models; using SOFTURE.Fakturownia.Models.Client; +using SOFTURE.Fakturownia.Models.Enums; namespace SOFTURE.Fakturownia.Abstractions; @@ -8,4 +9,5 @@ public interface IFakturowniaClient { Task> GetCurrentMonthStatement(int clientId); Task> GetMonthlyStatement(int clientId, int month, int year); + Task> GetInvoice(int invoiceId, DocumentKind kind); } \ No newline at end of file diff --git a/FAKTUROWNIA/SOFTURE.Fakturownia/FakturowniaClient.cs b/FAKTUROWNIA/SOFTURE.Fakturownia/FakturowniaClient.cs index a61eb0e..a0a0d9f 100644 --- a/FAKTUROWNIA/SOFTURE.Fakturownia/FakturowniaClient.cs +++ b/FAKTUROWNIA/SOFTURE.Fakturownia/FakturowniaClient.cs @@ -27,15 +27,14 @@ public async Task> GetCurrentMonthStatement(int clientI return Result.Failure($"Missing proforma invoice for client with id: {clientId}"); var statement = MonthlyStatement.Create( - proFormaInvoice.PriceNet!, - new Invoice(proFormaInvoice.Id, proFormaInvoice.CreatedAt) + new Invoice(proFormaInvoice.Id, proFormaInvoice.CreatedAt, proFormaInvoice.PriceNet) ); var invoice = invoices.SingleOrDefault(i => i.Kind == DocumentKind.Vat && i.FromInvoiceId == proFormaInvoice.Id); if (invoice != null) - statement.Paid(new Invoice(invoice.Id, invoice.CreatedAt)); + statement.Paid(new Invoice(invoice.Id, invoice.CreatedAt, invoice.PriceNet)); return Result.Success(statement); } @@ -64,16 +63,37 @@ public async Task> GetMonthlyStatement(int clientId, in return Result.Failure($"Missing proforma invoice for client with id: {clientId}"); var statement = MonthlyStatement.Create( - proFormaInvoice.PriceNet!, - new Invoice(proFormaInvoice.Id, proFormaInvoice.CreatedAt) + new Invoice(proFormaInvoice.Id, proFormaInvoice.CreatedAt, proFormaInvoice.PriceNet) ); var invoice = allInvoice.SingleOrDefault(i => i.Kind == DocumentKind.Vat && i.FromInvoiceId == proFormaInvoice.Id); if (invoice != null) - statement.Paid(new Invoice(invoice.Id, invoice.CreatedAt)); + statement.Paid(new Invoice(invoice.Id, invoice.CreatedAt, invoice.PriceNet)); return Result.Success(statement); } + + public async Task> GetInvoice(int invoiceId, DocumentKind kind) + { + var response = await fakturowniaApi.GetInvoiceAsync(invoiceId); + + if (!response.IsSuccessStatusCode) + return Result.Failure( + $"Failed to get invoice with id: {invoiceId} - {response.Error.Content}" + ); + + if(response.Content == null) + return Result.Failure($"Missing invoice with id: {invoiceId}"); + + var invoice = response.Content; + + if (invoice.Kind != kind) + return Result.Failure( + $"Invalid invoice kind. Expected: {kind}, got: {invoice.Kind}" + ); + + return Result.Success(new Invoice(invoice.Id, invoice.CreatedAt, invoice.PriceNet)); + } } \ No newline at end of file diff --git a/FAKTUROWNIA/SOFTURE.Fakturownia/Models/Api/InvoiceDetails.cs b/FAKTUROWNIA/SOFTURE.Fakturownia/Models/Api/InvoiceDetails.cs index 83c6be2..49eaa0c 100644 --- a/FAKTUROWNIA/SOFTURE.Fakturownia/Models/Api/InvoiceDetails.cs +++ b/FAKTUROWNIA/SOFTURE.Fakturownia/Models/Api/InvoiceDetails.cs @@ -29,7 +29,7 @@ public sealed class InvoiceDetails [JsonPropertyName("payment_type")] public string? PaymentType { get; set; } - [JsonPropertyName("price_net")] public string? PriceNet { get; set; } + [JsonPropertyName("price_net")] public string PriceNet { get; set; } = null!; [JsonPropertyName("price_gross")] public string? PriceGross { get; set; } diff --git a/FAKTUROWNIA/SOFTURE.Fakturownia/Models/Client/Invoice.cs b/FAKTUROWNIA/SOFTURE.Fakturownia/Models/Client/Invoice.cs index 6949606..8d0cb0c 100644 --- a/FAKTUROWNIA/SOFTURE.Fakturownia/Models/Client/Invoice.cs +++ b/FAKTUROWNIA/SOFTURE.Fakturownia/Models/Client/Invoice.cs @@ -4,14 +4,16 @@ namespace SOFTURE.Fakturownia.Models.Client; public sealed class Invoice : ValueObject { - public Invoice(int identifier, DateTime createdAt) + public Invoice(int identifier, DateTime createdAt, string priceNet) { Identifier = identifier; CreatedAt = createdAt; + PriceNet = decimal.Parse(priceNet); } public int Identifier { get; } public DateTime CreatedAt { get; } + public decimal PriceNet { get; } protected override IEnumerable GetEqualityComponents() { diff --git a/FAKTUROWNIA/SOFTURE.Fakturownia/Models/Client/MonthlyStatement.cs b/FAKTUROWNIA/SOFTURE.Fakturownia/Models/Client/MonthlyStatement.cs index 8a131d3..cfc7258 100644 --- a/FAKTUROWNIA/SOFTURE.Fakturownia/Models/Client/MonthlyStatement.cs +++ b/FAKTUROWNIA/SOFTURE.Fakturownia/Models/Client/MonthlyStatement.cs @@ -2,22 +2,19 @@ namespace SOFTURE.Fakturownia.Models.Client; public sealed class MonthlyStatement { - private MonthlyStatement(decimal priceNet, Invoice proFormaInvoice) + private MonthlyStatement(Invoice proFormaInvoice) { - PriceNet = priceNet; ProFormaInvoice = proFormaInvoice; } public Invoice ProFormaInvoice { get; set; } public Invoice? Invoice { get; set; } - public decimal PriceNet { get; set; } - public bool IsPaid => Invoice != null; - public static MonthlyStatement Create(string priceNet, Invoice proFormaInvoice) + public static MonthlyStatement Create(Invoice proFormaInvoice) { - return new MonthlyStatement(decimal.Parse(priceNet), proFormaInvoice); + return new MonthlyStatement(proFormaInvoice); } public void Paid(Invoice invoice)