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

@ -61,6 +61,22 @@ namespace Yavsc.Models
builder.Entity<NominativeServiceCommand>()
.Property(command => command.AmountPaid)
.HasDefaultValue(0m);
builder.Entity<NominativeServiceCommand>()
.Property(command => command.PaymentRevision)
.HasDefaultValue(Guid.Empty);
builder.Entity<NominativeServiceCommand>()
.HasOne<Estimate>().WithMany()
.HasForeignKey(command => command.InvoiceEstimateId)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<NominativeServiceCommand>()
.HasIndex(command => command.InvoiceEstimateId).IsUnique();
builder.Entity<InvoicePayment>()
.HasIndex(payment => new { payment.Source, payment.ReferenceScope, payment.Reference })
.IsUnique();
builder.Entity<InvoicePayment>()
.HasOne(payment => payment.Command).WithMany()
.HasForeignKey(payment => payment.CommandId)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<ResourceUsageRecord>(entity =>
{
@ -358,6 +374,7 @@ namespace Yavsc.Models
public DbSet<BlogTag> TagsDomain { get; set; }
public DbSet<EstimateTemplate> EstimateTemplates { get; set; }
public DbSet<InvoicePayment> InvoicePayments { get; set; }
public DbSet<Contact> Contact { get; set; }

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()

View file

@ -60,6 +60,7 @@ namespace Yavsc.Models.Haircut
public override List<IBillItem> GetBillItems()
{
if (InvoiceEstimateId.HasValue) return GetInvoiceBillItems();
string longhairsuffix = " (cheveux longs)";
string halflonghairsuffix = " (cheveux mi-longs)";
string shorthairsuffix = " (cheveux courts)";

View file

@ -39,6 +39,7 @@ namespace Yavsc.Models.Haircut
public override List<IBillItem> GetBillItems()
{
if (InvoiceEstimateId.HasValue) return GetInvoiceBillItems();
throw new NotImplementedException();
}
}

View file

@ -44,6 +44,7 @@ namespace Yavsc.Server.Models.IT
}
public override List<IBillItem> GetBillItems()
{
if (InvoiceEstimateId.HasValue) return GetInvoiceBillItems();
return bill;
}
@ -62,4 +63,3 @@ namespace Yavsc.Server.Models.IT
}
}

View file

@ -71,6 +71,7 @@ namespace Yavsc.Models.Workflow
public override List<IBillItem> GetBillItems()
{
if (InvoiceEstimateId.HasValue) return GetInvoiceBillItems();
throw new NotImplementedException();
}
}