feat(billing): model invoice payment balances and settlement
Persist amounts due and paid, derive partial and full settlement, and protect balances during command updates. Add migration, focused tests and invoicing documentation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
ed51e62b29
commit
5d03b2e477
13 changed files with 5434 additions and 2 deletions
|
|
@ -5,6 +5,11 @@
|
|||
|
||||
### Added
|
||||
|
||||
* [Yavsc.Abstract, Yavsc.Server] Modele de solde des commandes facturables :
|
||||
montants dus et payes persistants, reste a payer et etats non paye,
|
||||
partiellement paye ou acquitte. L'acquittement depend des montants,
|
||||
et non du seul statut PayPal ; migration sans montant historique invente.
|
||||
|
||||
* [PostIt.Desktop, Yavsc.Api] Podcasts en direct avec replay HLS conserve :
|
||||
capture camera et microphone par LibVLC (H.264/AAC, segments MPEG-TS),
|
||||
envoi ordonne et idempotent, catalogue et lecture authentifiee dans PostIt.
|
||||
|
|
|
|||
189
doc/invoicing.md
Normal file
189
doc/invoicing.md
Normal file
|
|
@ -0,0 +1,189 @@
|
|||
# Facturation et paiement après devis signé
|
||||
|
||||
## Objectif
|
||||
|
||||
Ce document fixe le plan métier et technique pour le flux qui suit la validation du devis :
|
||||
|
||||
- le devis est signé par les deux parties,
|
||||
- le prestataire déclenche la création d'une facture non acquittée,
|
||||
- la facture contient la ligne associée au mode de paiement choisi par le prestataire,
|
||||
- le client règle le montant partiel ou total,
|
||||
- la facture reste non acquittée tant que le total n'est pas intégralement payé.
|
||||
|
||||
## Règle métier de base
|
||||
|
||||
1. Le devis n'est considéré comme valide que lorsqu'il est signé par le fournisseur et par le client.
|
||||
2. Une facture ne peut être générée qu'après cette double validation.
|
||||
3. Le type de contrat choisi dans les settings du prestataire détermine le mode de règlement :
|
||||
- arrhes,
|
||||
- avance,
|
||||
- ou autre mode de facturation supporté par le système.
|
||||
4. Le montant payé partiellement est enregistré comme paiement partiel, sans transformer la facture en facture entièrement payée.
|
||||
5. La facture est marquée comme acquittée uniquement lorsque le montant total est effectivement réglé.
|
||||
6. L'addition / le relevé de paiement doit mentionner le paiement effectué, sans prétendre que la facture est déjà entièrement acquittée.
|
||||
|
||||
## Flux attendu
|
||||
|
||||
### 1. Devis signé par les deux parties
|
||||
|
||||
Le flux contractuel suit la séquence :
|
||||
|
||||
- commande / demande,
|
||||
- devis préparé,
|
||||
- validation prestataire,
|
||||
- validation client,
|
||||
- double signature persistée.
|
||||
|
||||
Une fois cette étape passée, le devis est un contrat exploitable pour la facture.
|
||||
|
||||
### 2. Génération de la facture non acquittée
|
||||
|
||||
Le prestataire déclenche la création de la facture depuis le contexte du devis validé.
|
||||
|
||||
Le système doit alors :
|
||||
|
||||
- identifier le devis validé,
|
||||
- récupérer le client et le prestataire,
|
||||
- sélectionner le mode de contrat défini dans les settings du prestataire,
|
||||
- ajouter la ligne de frais correspondante à la facture,
|
||||
- créer la facture dans un état "non acquittée".
|
||||
|
||||
### 3. Paiement partiel
|
||||
|
||||
Le client peut payer en premier :
|
||||
|
||||
- l'avance,
|
||||
- les arrhes,
|
||||
- ou une tranche partielle.
|
||||
|
||||
Dans ce cas :
|
||||
|
||||
- le paiement est enregistré,
|
||||
- le montant payé est ajouté au total enregistré,
|
||||
- la facture reste "non acquittée" tant qu'elle n'a pas été réglée intégralement,
|
||||
- l'addition doit refléter la transaction effectuée sans annoncer un paiement total abusif.
|
||||
|
||||
### 4. Acquittement total
|
||||
|
||||
Lorsque le client a payé la somme totale due :
|
||||
|
||||
- le statut de la facture passe à "payée" / "acquittée",
|
||||
- la facture peut être considérée comme totalement soldée,
|
||||
- la génération de documents ou notifications finales peut s'appuyer sur cet état.
|
||||
|
||||
## Règles de codage à respecter
|
||||
|
||||
### Règle 1 — ne pas confondre paiement partiel et paiement total
|
||||
|
||||
Le modèle de paiement ne doit pas se limiter à un booléen "payé / non payé".
|
||||
|
||||
Le bon niveau de modélisation est :
|
||||
|
||||
- montant dû,
|
||||
- montant payé,
|
||||
- montant restant,
|
||||
- statut de paiement partiel / total,
|
||||
- état d'acquittement final.
|
||||
|
||||
### Règle 2 — la facture ne doit pas être "payée" trop tôt
|
||||
|
||||
Le système ne doit pas marquer la facture comme entièrement payée à la première transaction.
|
||||
|
||||
L'état métier doit être calculé de manière explicite :
|
||||
|
||||
- `montantPaye < montantTotal` => facture non acquittée,
|
||||
- `montantPaye >= montantTotal` => facture acquittée (reste à payer borné à zéro).
|
||||
|
||||
### Règle 3 — les arrhes / avances doivent rester cohérents avec le contexte contractuel
|
||||
|
||||
Le mode choisi par le prestataire (arrhes ou avance) doit être représenté de façon explicite dans le flux de génération de facture et dans l'addition.
|
||||
|
||||
Le comportement attendu est :
|
||||
|
||||
- la ligne de frais correspondante est ajoutée à la facture,
|
||||
- le paiement initial est comptabilisé correctement,
|
||||
- le solde restant reste visible et exploitable.
|
||||
|
||||
## Plan d'implémentation proposé
|
||||
|
||||
### Étape 1 — modéliser les règles de paiement
|
||||
|
||||
Implémentée sur le point d'entrée existant `IBillable` /
|
||||
`NominativeServiceCommand`, sans créer encore d'entité Facture distincte :
|
||||
|
||||
- `AmountDue` et `AmountPaid` sont persistés sur la commande ; le montant dû
|
||||
reste `null` tant que le total de facture n'a pas été établi.
|
||||
- `SetPaymentAmounts(amountDue, amountPaid)` définit un instantané validé
|
||||
des montants (décimaux non négatifs), côté métier uniquement. Les setters
|
||||
privés empêchent leur alimentation par les requêtes de modification de commande.
|
||||
- `PaymentSummary` expose les montants dû, payé et restant, ainsi que
|
||||
`Unpaid`, `PartiallyPaid` ou `Paid` ; il est absent si le montant dû est inconnu.
|
||||
- Le restant est borné à zéro en cas de trop-perçu, sans perdre le montant payé.
|
||||
L'acquittement est atteint si `AmountPaid >= AmountDue`. Une facture
|
||||
explicitement établie à zéro est soldée ; un montant inconnu ne l'est pas.
|
||||
- `GetIsAcquitted()` et le suffixe documentaire `-ack` utilisent ce calcul,
|
||||
jamais le seul statut PayPal `SUCCESS`.
|
||||
|
||||
La migration ajoute les montants sans reconstituer de paiements historiques :
|
||||
les anciennes commandes ont un dû inconnu et un payé nul. Un rapprochement
|
||||
explicite est nécessaire avant de les considérer comme acquittées.
|
||||
La génération depuis le devis, la fixation du dû et l'alimentation du payé
|
||||
par des transactions confirmées restent à brancher aux étapes suivantes.
|
||||
L'appel `SetPaymentAmounts` remplace un total cumulé ; il n'enregistre pas
|
||||
une transaction et ne doit pas être utilisé pour additionner un retour PayPal
|
||||
sans contrôle d'idempotence.
|
||||
|
||||
Objectifs couverts :
|
||||
|
||||
- identifier le point d'entrée métier de la facture,
|
||||
- ajouter un modèle de montant dû / montant payé / montant restant,
|
||||
- distinguer clairement l'état partiel du statut de paiement total,
|
||||
- éviter toute logique qui force un booléen `IsPaid = true` dès le premier paiement.
|
||||
|
||||
### Étape 2 — brancher la génération de facture sur le devis signé
|
||||
|
||||
- vérifier que le devis est bien doublement validé,
|
||||
- déclencher la facture depuis le contexte du devis,
|
||||
- rattacher la facture au bon client / prestataire.
|
||||
|
||||
### Étape 3 — intégrer le mode de contrat du prestataire
|
||||
|
||||
- lire le setting du prestataire qui indique le type de contrat,
|
||||
- associer ce mode à la ligne de frais ajoutée sur la facture,
|
||||
- garantir que la ligne peut être différenciée selon arrhes ou avance.
|
||||
|
||||
### Étape 4 — enregistrer les paiements sans surinterpréter le statut
|
||||
|
||||
- enregister le paiement effectué,
|
||||
- alimenter les montants payés et restants,
|
||||
- ne pas considérer la facture comme totalement acquittée tant que le total n'est pas atteint.
|
||||
|
||||
### Étape 5 — finaliser l'état acquitté
|
||||
|
||||
- calculer le total payé,
|
||||
- si `totalPaye >= totalDu`, alors passer la facture en état acquittée,
|
||||
- envoyer les notifications / documents associés au cas échéant.
|
||||
|
||||
## Tests à couvrir
|
||||
|
||||
Le flux doit être validé par des tests ciblés sur :
|
||||
|
||||
- devis signé par les deux parties → génération d'une facture non acquittée,
|
||||
- mode arrhes / avance → ligne de frais correctement ajoutée,
|
||||
- paiement partiel → montant enregistré sans statut payé complet,
|
||||
- paiement total → facture marquée acquittée,
|
||||
- addition / message de paiement → mention du montant réellement versé sans faux état d'acquittement complet.
|
||||
|
||||
## Critère de sortie fonctionnel
|
||||
|
||||
Le comportement attendu est considéré comme atteint si le scénario suivant est vérifié :
|
||||
|
||||
1. le devis est doublé signé,
|
||||
2. le prestataire génère une facture non acquittée,
|
||||
3. le client paye une avance ou des arrhes,
|
||||
4. la facture reste non acquittée tant qu'elle n'est pas totalement soldée,
|
||||
5. la facture devient acquittée uniquement lorsque le total est réglé.
|
||||
|
||||
## Note de contexte
|
||||
|
||||
Ce document consolide la décision métier issue du workflow “Prestation / devis” et est volontairement limité au périmètre de la facturation et du paiement après signature du devis. Il ne remplace pas les règles plus larges de gestion des litiges, workflows d'annulation ou paiements transverses déjà documentés ailleurs.
|
||||
|
|
@ -20,6 +20,8 @@ namespace Yavsc.Billing
|
|||
|
||||
DateTime? ValidationDate { get; }
|
||||
|
||||
PaymentSummary? PaymentSummary { get; }
|
||||
|
||||
bool GetIsAcquitted ();
|
||||
|
||||
string GetFileBaseName (IBillingService service);
|
||||
|
|
|
|||
8
src/Yavsc.Abstract/Billing/PaymentStatus.cs
Normal file
8
src/Yavsc.Abstract/Billing/PaymentStatus.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
namespace Yavsc.Billing;
|
||||
|
||||
public enum PaymentStatus
|
||||
{
|
||||
Unpaid,
|
||||
PartiallyPaid,
|
||||
Paid
|
||||
}
|
||||
21
src/Yavsc.Abstract/Billing/PaymentSummary.cs
Normal file
21
src/Yavsc.Abstract/Billing/PaymentSummary.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
namespace Yavsc.Billing;
|
||||
|
||||
public sealed class PaymentSummary
|
||||
{
|
||||
public PaymentSummary(decimal amountDue, decimal amountPaid)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(amountDue);
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(amountPaid);
|
||||
|
||||
AmountDue = amountDue;
|
||||
AmountPaid = amountPaid;
|
||||
}
|
||||
|
||||
public decimal AmountDue { get; }
|
||||
public decimal AmountPaid { get; }
|
||||
public decimal AmountRemaining => Math.Max(0m, AmountDue - AmountPaid);
|
||||
public bool IsAcquitted => AmountPaid >= AmountDue;
|
||||
public PaymentStatus Status => IsAcquitted
|
||||
? PaymentStatus.Paid
|
||||
: AmountPaid > 0m ? PaymentStatus.PartiallyPaid : PaymentStatus.Unpaid;
|
||||
}
|
||||
|
|
@ -89,6 +89,8 @@ namespace Yavsc.ApiControllers
|
|||
}
|
||||
|
||||
_context.Entry(hairCutQuery).State = EntityState.Modified;
|
||||
_context.Entry(hairCutQuery).Property(q => q.AmountDue).IsModified = false;
|
||||
_context.Entry(hairCutQuery).Property(q => q.AmountPaid).IsModified = false;
|
||||
|
||||
try
|
||||
{
|
||||
|
|
|
|||
4867
src/Yavsc.Org/Migrations/20261019182518_InvoicePaymentAmounts.Designer.cs
generated
Normal file
4867
src/Yavsc.Org/Migrations/20261019182518_InvoicePaymentAmounts.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,39 @@
|
|||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Yavsc.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InvoicePaymentAmounts : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<decimal>(
|
||||
name: "AmountDue",
|
||||
table: "NominativeServiceCommands",
|
||||
type: "numeric",
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.AddColumn<decimal>(
|
||||
name: "AmountPaid",
|
||||
table: "NominativeServiceCommands",
|
||||
type: "numeric",
|
||||
nullable: false,
|
||||
defaultValue: 0m);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "AmountDue",
|
||||
table: "NominativeServiceCommands");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "AmountPaid",
|
||||
table: "NominativeServiceCommands");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -17,7 +17,7 @@ namespace Yavsc.Migrations
|
|||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.12")
|
||||
.HasAnnotation("ProductVersion", "10.0.13")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
|
@ -1454,6 +1454,14 @@ namespace Yavsc.Migrations
|
|||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<decimal?>("AmountDue")
|
||||
.HasColumnType("numeric");
|
||||
|
||||
b.Property<decimal>("AmountPaid")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("numeric")
|
||||
.HasDefaultValue(0m);
|
||||
|
||||
b.Property<string>("ClientId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
|
|
|||
|
|
@ -58,6 +58,10 @@ namespace Yavsc.Models
|
|||
}
|
||||
builder.UseIdentityByDefaultColumns();
|
||||
|
||||
builder.Entity<NominativeServiceCommand>()
|
||||
.Property(command => command.AmountPaid)
|
||||
.HasDefaultValue(0m);
|
||||
|
||||
builder.Entity<ResourceUsageRecord>(entity =>
|
||||
{
|
||||
entity.HasKey(r => r.Id);
|
||||
|
|
|
|||
|
|
@ -80,9 +80,29 @@ namespace Yavsc.Models.Billing
|
|||
|
||||
public abstract System.Collections.Generic.List<IBillItem> GetBillItems();
|
||||
|
||||
/// <summary>
|
||||
/// Null until an invoice total has been established; never inferred
|
||||
/// from a provisional price or a payment gateway acknowledgement.
|
||||
/// </summary>
|
||||
public decimal? AmountDue { get; private set; }
|
||||
|
||||
public decimal AmountPaid { get; private set; }
|
||||
|
||||
[NotMapped]
|
||||
public PaymentSummary? PaymentSummary => AmountDue.HasValue
|
||||
? new PaymentSummary(AmountDue.Value, AmountPaid)
|
||||
: null;
|
||||
|
||||
public void SetPaymentAmounts(decimal amountDue, decimal amountPaid)
|
||||
{
|
||||
var summary = new PaymentSummary(amountDue, amountPaid);
|
||||
AmountDue = summary.AmountDue;
|
||||
AmountPaid = summary.AmountPaid;
|
||||
}
|
||||
|
||||
public bool GetIsAcquitted()
|
||||
{
|
||||
return Regularization?.IsOk() ?? false;
|
||||
return PaymentSummary?.IsAcquitted ?? false;
|
||||
}
|
||||
|
||||
public string GetFileBaseName(IBillingService billingService)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Yavsc.ApiControllers;
|
||||
using Yavsc.Api.Test.Fixtures;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Haircut;
|
||||
|
|
@ -75,6 +78,14 @@ public sealed class HairCutQueryApiControllerTests : IClassFixture<ApiWebServerF
|
|||
Assert.NotEqual(0, created!.Id);
|
||||
Assert.Equal("alice", created.ClientId);
|
||||
|
||||
using (var scope = _fixture.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
var command = await db.HairCutQueries.SingleAsync(q => q.Id == created.Id, TestContext.Current.CancellationToken);
|
||||
command.SetPaymentAmounts(100m, 30m);
|
||||
await db.SaveChangesAsync("alice", TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
var getResponse = await http.GetAsync($"/api/v1/billing/Brush/{created.Id}", TestContext.Current.CancellationToken);
|
||||
Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
|
||||
|
||||
|
|
@ -87,6 +98,15 @@ public sealed class HairCutQueryApiControllerTests : IClassFixture<ApiWebServerF
|
|||
var putResponse = await http.PutAsJsonAsync($"/api/v1/billing/Brush/{fetched.Id}", fetched, TestContext.Current.CancellationToken);
|
||||
Assert.Equal(HttpStatusCode.NoContent, putResponse.StatusCode);
|
||||
|
||||
using (var scope = _fixture.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
var command = await db.HairCutQueries.SingleAsync(q => q.Id == created.Id, TestContext.Current.CancellationToken);
|
||||
Assert.Equal(100m, command.AmountDue);
|
||||
Assert.Equal(30m, command.AmountPaid);
|
||||
Assert.False(command.GetIsAcquitted());
|
||||
}
|
||||
|
||||
var deleteResponse = await http.DeleteAsync($"/api/v1/billing/Brush/{fetched.Id}", TestContext.Current.CancellationToken);
|
||||
Assert.Equal(HttpStatusCode.OK, deleteResponse.StatusCode);
|
||||
|
||||
|
|
@ -111,6 +131,44 @@ public sealed class HairCutQueryApiControllerTests : IClassFixture<ApiWebServerF
|
|||
&& item.Details == "Coupe · Brushing · Aucune technique spécifique · Shampoing · Sans soins");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Legacy_haircut_update_preserves_persisted_payment_amounts()
|
||||
{
|
||||
_fixture.ResetAndSeedHaircutGraph();
|
||||
HairCutQuery payload;
|
||||
using (var scope = _fixture.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
var command = await db.HairCutQueries.SingleAsync(TestContext.Current.CancellationToken);
|
||||
command.SetPaymentAmounts(100m, 30m);
|
||||
await db.SaveChangesAsync("alice", TestContext.Current.CancellationToken);
|
||||
payload = Newtonsoft.Json.JsonConvert.DeserializeObject<HairCutQuery>(
|
||||
Newtonsoft.Json.JsonConvert.SerializeObject(command))!;
|
||||
}
|
||||
|
||||
Assert.Null(payload.AmountDue);
|
||||
Assert.Equal(0m, payload.AmountPaid);
|
||||
payload.AdditionalInfo = "Updated without payment amounts";
|
||||
|
||||
using (var scope = _fixture.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
var controller = new HairCutController(db, NullLoggerFactory.Instance);
|
||||
var response = await controller.PutHairCutQuery(payload.Id, payload);
|
||||
Assert.Equal(204, Assert.IsType<StatusCodeResult>(response).StatusCode);
|
||||
}
|
||||
|
||||
using (var scope = _fixture.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
var command = await db.HairCutQueries.SingleAsync(q => q.Id == payload.Id, TestContext.Current.CancellationToken);
|
||||
Assert.Equal(payload.AdditionalInfo, command.AdditionalInfo);
|
||||
Assert.Equal(100m, command.AmountDue);
|
||||
Assert.Equal(30m, command.AmountPaid);
|
||||
Assert.False(command.GetIsAcquitted());
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<long> GetPrestationIdAsync()
|
||||
{
|
||||
using var scope = _fixture.Services.CreateScope();
|
||||
|
|
|
|||
209
src/test/Yavsc.Server.Tests/PaymentSummaryTests.cs
Normal file
209
src/test/Yavsc.Server.Tests/PaymentSummaryTests.cs
Normal file
|
|
@ -0,0 +1,209 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc.Abstract.Workflow;
|
||||
using Yavsc.Billing;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Billing;
|
||||
using Yavsc.Models.Payment;
|
||||
using Yavsc.Models.Workflow;
|
||||
using Yavsc.Services;
|
||||
|
||||
namespace Yavsc.Server.Tests;
|
||||
|
||||
public class PaymentSummaryTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(100, 0, 100, PaymentStatus.Unpaid, false)]
|
||||
[InlineData(100, 30, 70, PaymentStatus.PartiallyPaid, false)]
|
||||
[InlineData(100, 100, 0, PaymentStatus.Paid, true)]
|
||||
[InlineData(100, 120, 0, PaymentStatus.Paid, true)]
|
||||
[InlineData(0, 0, 0, PaymentStatus.Paid, true)]
|
||||
public void Summary_distinguishes_partial_and_total_payment(
|
||||
decimal due, decimal paid, decimal remaining, PaymentStatus status, bool acquitted)
|
||||
{
|
||||
var summary = new PaymentSummary(due, paid);
|
||||
|
||||
Assert.Equal(due, summary.AmountDue);
|
||||
Assert.Equal(paid, summary.AmountPaid);
|
||||
Assert.Equal(remaining, summary.AmountRemaining);
|
||||
Assert.Equal(status, summary.Status);
|
||||
Assert.Equal(acquitted, summary.IsAcquitted);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Summary_uses_exact_decimal_amounts()
|
||||
{
|
||||
var summary = new PaymentSummary(0.3m, 0.1m + 0.2m);
|
||||
|
||||
Assert.Equal(0m, summary.AmountRemaining);
|
||||
Assert.True(summary.IsAcquitted);
|
||||
Assert.False(new PaymentSummary(100m, 99.99m).IsAcquitted);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-1, 0, "amountDue")]
|
||||
[InlineData(100, -1, "amountPaid")]
|
||||
public void Negative_amounts_are_rejected(decimal due, decimal paid, string parameter)
|
||||
{
|
||||
var exception = Assert.Throws<ArgumentOutOfRangeException>(() => new PaymentSummary(due, paid));
|
||||
|
||||
Assert.Equal(parameter, exception.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Gateway_success_without_known_amounts_does_not_acquit_the_bill()
|
||||
{
|
||||
IBillable command = new TestCommand
|
||||
{
|
||||
Regularization = new PayPalPayment { State = "SUCCESS" }
|
||||
};
|
||||
|
||||
Assert.Null(command.PaymentSummary);
|
||||
Assert.False(command.GetIsAcquitted());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Acquittal_and_document_name_follow_the_balance_not_gateway_success()
|
||||
{
|
||||
var command = new TestCommand
|
||||
{
|
||||
Id = 42,
|
||||
Regularization = new PayPalPayment { State = "SUCCESS" }
|
||||
};
|
||||
var service = new TestBillingService();
|
||||
command.SetPaymentAmounts(100m, 30m);
|
||||
Assert.False(command.GetIsAcquitted());
|
||||
Assert.Equal(PaymentStatus.PartiallyPaid, command.PaymentSummary!.Status);
|
||||
Assert.Equal("facture-payment-summary-test-42", command.GetFileBaseName(service));
|
||||
|
||||
command.Regularization = null!;
|
||||
command.SetPaymentAmounts(100m, 100m);
|
||||
Assert.True(command.GetIsAcquitted());
|
||||
Assert.Equal("facture-payment-summary-test-42-ack", command.GetFileBaseName(service));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Invalid_amount_update_leaves_previous_balance_unchanged()
|
||||
{
|
||||
var command = new TestCommand();
|
||||
command.SetPaymentAmounts(100m, 30m);
|
||||
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => command.SetPaymentAmounts(50m, -1m));
|
||||
|
||||
Assert.Equal(100m, command.AmountDue);
|
||||
Assert.Equal(30m, command.AmountPaid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Amounts_and_derived_status_survive_reloading_the_command()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
||||
.UseInMemoryDatabase(Guid.NewGuid().ToString())
|
||||
.Options;
|
||||
long id;
|
||||
using (var context = new ApplicationDbContext(options))
|
||||
{
|
||||
var command = new RdvQuery
|
||||
{
|
||||
ClientId = "client",
|
||||
PerformerId = "provider",
|
||||
ActivityCode = "test",
|
||||
Reason = "Payment test",
|
||||
UserCreated = "client",
|
||||
UserModified = "client"
|
||||
};
|
||||
context.Add(command);
|
||||
await context.SaveChangesAsync();
|
||||
id = command.Id;
|
||||
}
|
||||
|
||||
using (var context = new ApplicationDbContext(options))
|
||||
{
|
||||
var command = await context.RdvQueries.SingleAsync(q => q.Id == id, TestContext.Current.CancellationToken);
|
||||
Assert.Null(command.AmountDue);
|
||||
Assert.Equal(0m, command.AmountPaid);
|
||||
Assert.False(command.GetIsAcquitted());
|
||||
command.SetPaymentAmounts(100.50m, 30.25m);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
using (var context = new ApplicationDbContext(options))
|
||||
{
|
||||
var command = await context.RdvQueries.SingleAsync(q => q.Id == id, TestContext.Current.CancellationToken);
|
||||
Assert.Equal(100.50m, command.AmountDue);
|
||||
Assert.Equal(30.25m, command.AmountPaid);
|
||||
Assert.Equal(70.25m, command.PaymentSummary!.AmountRemaining);
|
||||
Assert.Equal(PaymentStatus.PartiallyPaid, command.PaymentSummary.Status);
|
||||
Assert.False(command.GetIsAcquitted());
|
||||
command.SetPaymentAmounts(100.50m, 100.50m);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
using (var context = new ApplicationDbContext(options))
|
||||
{
|
||||
var command = await context.RdvQueries.SingleAsync(q => q.Id == id, TestContext.Current.CancellationToken);
|
||||
Assert.True(command.GetIsAcquitted());
|
||||
Assert.Equal(0m, command.PaymentSummary!.AmountRemaining);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_amounts_cannot_be_set_by_request_json()
|
||||
{
|
||||
const string json = """{"AmountDue":100,"AmountPaid":100}""";
|
||||
var newtonsoftCommand = Newtonsoft.Json.JsonConvert.DeserializeObject<RdvQuery>(json)!;
|
||||
var systemTextCommand = System.Text.Json.JsonSerializer.Deserialize<RdvQuery>(json)!;
|
||||
|
||||
Assert.Null(newtonsoftCommand.AmountDue);
|
||||
Assert.Equal(0m, newtonsoftCommand.AmountPaid);
|
||||
Assert.Null(systemTextCommand.AmountDue);
|
||||
Assert.Equal(0m, systemTextCommand.AmountPaid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Payment_migration_adds_only_amounts_without_inventing_historical_payments()
|
||||
{
|
||||
var migration = new Yavsc.Migrations.InvoicePaymentAmounts();
|
||||
Assert.Equal(2, migration.UpOperations.Count);
|
||||
var columns = migration.UpOperations
|
||||
.Cast<Microsoft.EntityFrameworkCore.Migrations.Operations.AddColumnOperation>()
|
||||
.ToList();
|
||||
Assert.All(columns, column => Assert.Equal("NominativeServiceCommands", column.Table));
|
||||
var due = Assert.Single(columns, column => column.Name == "AmountDue");
|
||||
Assert.True(due.IsNullable);
|
||||
Assert.Null(due.DefaultValue);
|
||||
var paid = Assert.Single(columns, column => column.Name == "AmountPaid");
|
||||
Assert.False(paid.IsNullable);
|
||||
Assert.Equal(0m, paid.DefaultValue);
|
||||
Assert.Equal(2, migration.DownOperations.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Payment_model_matches_the_postgresql_migration_snapshot()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
||||
.UseNpgsql("Host=localhost;Database=model_comparison_only",
|
||||
provider => provider.MigrationsAssembly("Yavsc.Org"))
|
||||
.Options;
|
||||
using var context = new ApplicationDbContext(options);
|
||||
|
||||
Assert.False(context.Database.HasPendingModelChanges());
|
||||
}
|
||||
|
||||
private sealed class TestCommand : NominativeServiceCommand
|
||||
{
|
||||
public override long Id { get; set; }
|
||||
public override string Description { get; set; } = "Test";
|
||||
public override List<IBillItem> GetBillItems() => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
private sealed class TestBillingService : IBillingService
|
||||
{
|
||||
public Dictionary<string, string> BillingMap { get; } = new()
|
||||
{
|
||||
[nameof(TestCommand)] = "payment-summary-test"
|
||||
};
|
||||
|
||||
public Task<IQuery> GetBillAsync(string billingCode, long queryId) => throw new NotSupportedException();
|
||||
public Task<IUserSettings> GetPerformersSettingsAsync(string activityCode, string userId) => throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue