feat(invoicing): connect signed estimates and verified payment receipts

Freeze invoice totals and parties, record offline receipts and verify completed PayPal transactions. Protect idempotence and concurrent balance updates; add migration, API client, documentation and tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Paul Schneider 2026-10-19 20:29:08 +01:00
commit 2dcbcd35b3
Signed by: notazof
GPG key ID: AD4B9B545FBD7D12
37 changed files with 6686 additions and 12 deletions

View file

@ -0,0 +1,28 @@
#nullable enable
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
namespace Yavsc.Models.Billing;
public sealed class InvoicePayment
{
public long Id { get; set; }
public long CommandId { get; set; }
[ForeignKey(nameof(CommandId)), JsonIgnore, Newtonsoft.Json.JsonIgnore]
public NominativeServiceCommand Command { get; set; } = null!;
[MaxLength(16)]
public string Source { get; set; } = "";
[MaxLength(450)]
public string ReferenceScope { get; set; } = "";
[MaxLength(128)]
public string Reference { get; set; } = "";
public decimal Amount { get; set; }
[MaxLength(3)]
public string Currency { get; set; } = "";
public DateTimeOffset ReceivedAtUtc { get; set; }
public DateTimeOffset RecordedAtUtc { get; set; }
[MaxLength(450)]
public string RecordedBy { get; set; } = "";
}

View file

@ -88,6 +88,50 @@ namespace Yavsc.Models.Billing
public decimal AmountPaid { get; private set; }
public long? InvoiceEstimateId { get; private set; }
public DateTimeOffset? InvoicedAtUtc { get; private set; }
[MaxLength(3)]
public string? InvoiceCurrency { get; private set; }
public string? InvoiceClientId { get; private set; }
public string? InvoicePerformerId { get; private set; }
[JsonIgnore, System.Text.Json.Serialization.JsonIgnore]
public string? InvoiceLinesJson { get; private set; }
[ConcurrencyCheck]
public Guid PaymentRevision { get; private set; }
public void EstablishInvoice(long estimateId, string currency, IReadOnlyList<InvoiceLineResponse> lines)
{
ArgumentNullException.ThrowIfNull(lines);
if (InvoiceEstimateId.HasValue || AmountDue.HasValue || AmountPaid != 0m)
throw new InvalidOperationException("A balance or invoice already exists for this command.");
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(estimateId);
if (currency != "EUR" || lines.Count == 0
|| lines.Any(line => line.Count <= 0 || decimal.Round(line.UnitaryCost, 2) != line.UnitaryCost))
throw new ArgumentException("An invoice requires EUR lines with positive quantities.");
var due = lines.Sum(line => checked(line.Count * line.UnitaryCost));
SetPaymentAmounts(due, 0m);
InvoiceEstimateId = estimateId;
InvoiceCurrency = currency;
InvoiceClientId = ClientId;
InvoicePerformerId = PerformerId;
var now = DateTimeOffset.UtcNow;
InvoicedAtUtc = new DateTimeOffset(now.UtcTicks - now.UtcTicks % 10, TimeSpan.Zero);
InvoiceLinesJson = System.Text.Json.JsonSerializer.Serialize(lines);
}
public List<IBillItem> GetInvoiceBillItems()
{
if (InvoiceLinesJson is null || InvoiceCurrency is null)
throw new InvalidOperationException("The invoice has no frozen lines.");
var lines = System.Text.Json.JsonSerializer.Deserialize<List<InvoiceLineResponse>>(InvoiceLinesJson)
?? throw new InvalidOperationException("The invoice lines are invalid.");
return lines.Select(line => (IBillItem)new CommandLine
{
Name = line.Name, Description = line.Description,
Count = line.Count, UnitaryCost = line.UnitaryCost, Currency = InvoiceCurrency
}).ToList();
}
[NotMapped]
public PaymentSummary? PaymentSummary => AmountDue.HasValue
? new PaymentSummary(AmountDue.Value, AmountPaid)
@ -95,9 +139,12 @@ namespace Yavsc.Models.Billing
public void SetPaymentAmounts(decimal amountDue, decimal amountPaid)
{
if (InvoiceEstimateId.HasValue && AmountDue != amountDue)
throw new InvalidOperationException("An established invoice total cannot be changed.");
var summary = new PaymentSummary(amountDue, amountPaid);
AmountDue = summary.AmountDue;
AmountPaid = summary.AmountPaid;
PaymentRevision = Guid.NewGuid();
}
public bool GetIsAcquitted()