Skip to content

Commit

Permalink
Added 'GetInvoice' in 'fakturowniaClient'
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmatys committed Sep 25, 2024
1 parent 654b442 commit e487b1f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 15 deletions.
5 changes: 4 additions & 1 deletion FAKTUROWNIA/SOFTURE.Fakturownia.Playground/Playground.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using SOFTURE.Fakturownia.Abstractions;
using SOFTURE.Fakturownia.Models.Enums;

namespace SOFTURE.Fakturownia.Playground;

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ public interface IFakturowniaApi
Task<ApiResponse<IReadOnlyCollection<InvoiceDetails>>> GetInvoicesAsync(
[AliasAs("period")] Period period,
[AliasAs("client_id")] int clientId);

[Get("/invoices/{invoiceId}.json")]
Task<ApiResponse<InvoiceDetails>> GetInvoiceAsync(int invoiceId);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using CSharpFunctionalExtensions;
using SOFTURE.Fakturownia.Models;
using SOFTURE.Fakturownia.Models.Client;
using SOFTURE.Fakturownia.Models.Enums;

namespace SOFTURE.Fakturownia.Abstractions;

public interface IFakturowniaClient
{
Task<Result<MonthlyStatement>> GetCurrentMonthStatement(int clientId);
Task<Result<MonthlyStatement>> GetMonthlyStatement(int clientId, int month, int year);
Task<Result<Invoice>> GetInvoice(int invoiceId, DocumentKind kind);
}
32 changes: 26 additions & 6 deletions FAKTUROWNIA/SOFTURE.Fakturownia/FakturowniaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ public async Task<Result<MonthlyStatement>> GetCurrentMonthStatement(int clientI
return Result.Failure<MonthlyStatement>($"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);
}
Expand Down Expand Up @@ -64,16 +63,37 @@ public async Task<Result<MonthlyStatement>> GetMonthlyStatement(int clientId, in
return Result.Failure<MonthlyStatement>($"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<Result<Invoice>> GetInvoice(int invoiceId, DocumentKind kind)
{
var response = await fakturowniaApi.GetInvoiceAsync(invoiceId);

if (!response.IsSuccessStatusCode)
return Result.Failure<Invoice>(
$"Failed to get invoice with id: {invoiceId} - {response.Error.Content}"
);

if(response.Content == null)
return Result.Failure<Invoice>($"Missing invoice with id: {invoiceId}");

var invoice = response.Content;

if (invoice.Kind != kind)
return Result.Failure<Invoice>(
$"Invalid invoice kind. Expected: {kind}, got: {invoice.Kind}"
);

return Result.Success(new Invoice(invoice.Id, invoice.CreatedAt, invoice.PriceNet));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
4 changes: 3 additions & 1 deletion FAKTUROWNIA/SOFTURE.Fakturownia/Models/Client/Invoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IComparable> GetEqualityComponents()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e487b1f

Please sign in to comment.