doubleentry 0.4.0

An immutable, tamper-evident double-entry bookkeeping engine. Balanced-by-construction entries, exact integer money, deterministic identity, and an append-only Merkle log with inclusion and consistency proofs. Zero I/O. No async. No chart of accounts.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
//! Open-item clearing.
//!
//! A receivable is raised by one posting and settled by another. Knowing *which*
//! settled *which* is what turns a running balance into a list of open items —
//! and it is a ledger concern, not an application one, because it has to agree
//! with the postings exactly.
//!
//! Nothing is mutated. Clearing records which postings offset which, and by how
//! much; the postings themselves never change. A posting's **residual** is its
//! amount less everything applied to it, and it is *open* while the residual is
//! positive.
//!
//! # Partial application
//!
//! Applying less than the full amount leaves both sides open for the remainder,
//! which is the behaviour a partial payment needs: the invoice stays visible as
//! partly settled rather than vanishing or being rewritten.
//!
//! Booking the shortfall as a fresh item instead — clearing the original in full
//! and raising a new one for the difference — is an ordinary entry the
//! application posts. The engine does not choose between the two policies.
//!
//! # Resetting
//!
//! A clearing can be reset when it turns out to have matched the wrong items.
//! The reset is a new record that releases the applied amounts; the original
//! clearing remains in the register, because an assignment that was made and
//! withdrawn is itself part of the audit trail.

use std::collections::BTreeMap;

use time::Date;
use uuid::Uuid;

use crate::account::AccountId;
use crate::balance::{Balance, BalanceKey};
use crate::entry::EntryId;
use crate::money::{Amount, Currency, MoneyError};
use crate::posting::{Direction, Layer, Posting};

/// A reference to one posting inside one entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PostingRef {
    /// The entry holding the posting.
    pub entry: EntryId,
    /// Zero-based position within that entry's postings.
    pub index: u16,
}

impl PostingRef {
    /// Creates a reference.
    #[must_use]
    pub const fn new(entry: EntryId, index: u16) -> Self {
        Self { entry, index }
    }
}

impl std::fmt::Display for PostingRef {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}#{}", self.entry, self.index)
    }
}

/// Identifier for a clearing record.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ClearingId(Uuid);

impl ClearingId {
    /// Wraps an existing UUID.
    #[must_use]
    pub const fn from_uuid(id: Uuid) -> Self {
        Self(id)
    }

    /// The underlying UUID.
    #[must_use]
    pub const fn as_uuid(&self) -> &Uuid {
        &self.0
    }

    /// Generates a fresh time-ordered identifier.
    ///
    /// Reads the clock, so it is not part of the deterministic path; the engine
    /// never calls it.
    #[must_use]
    pub fn generate() -> Self {
        Self(Uuid::now_v7()) // purity-exempt: identity, not ledger state
    }
}

impl std::fmt::Display for ClearingId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(&self.0, f)
    }
}

/// One posting and the amount of it this clearing applies.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ClearedItem<const P: u8> {
    /// The posting being applied against.
    pub posting: PostingRef,
    /// How much of it this clearing consumes. A magnitude, never negative.
    pub applied: Amount<P>,
}

/// A record that a set of postings offset one another.
///
/// Scoped to one `(account, currency, layer)` — the same key a balance and an
/// open-item list are reported against. Anything wider would let a clearing
/// relate postings that never appear in the same statement.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Clearing<const P: u8> {
    /// Identifier.
    pub id: ClearingId,
    /// The account whose items are being cleared.
    pub account: AccountId,
    /// The currency the items are in.
    pub currency: Currency,
    /// Whether settled movements or reservations are being cleared.
    ///
    /// A reservation and a settled payment are different claims on the same
    /// account: netting one against the other would report an open item as
    /// closed while the money had not moved. Both layers clear the same way;
    /// they just do not clear against each other.
    pub layer: Layer,
    /// The date the assignment was made.
    pub cleared_on: Date,
    /// The postings and the amounts applied to each.
    pub items: Vec<ClearedItem<P>>,
}

impl<const P: u8> Clearing<P> {
    /// The balance key this clearing operates on.
    #[must_use]
    pub fn key(&self) -> BalanceKey {
        BalanceKey {
            account: self.account,
            currency: self.currency,
            layer: self.layer,
        }
    }

    /// Starts a settled clearing on one account and currency.
    #[must_use]
    pub fn new(id: ClearingId, key: BalanceKey, cleared_on: Date) -> Self {
        Self {
            id,
            account: key.account,
            currency: key.currency,
            layer: key.layer,
            cleared_on,
            items: Vec::new(),
        }
    }

    /// Applies `applied` of `posting` in this clearing.
    #[must_use]
    pub fn apply(mut self, posting: PostingRef, applied: Amount<P>) -> Self {
        self.items.push(ClearedItem { posting, applied });
        self
    }
}

/// Something that can resolve a [`PostingRef`] to the posting it names.
pub trait PostingLookup<const P: u8> {
    /// Returns the posting, or `None` if the reference does not resolve.
    fn posting(&self, reference: PostingRef) -> Option<&Posting<P>>;
}

/// Failure recording or resetting a clearing.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ClearingError {
    /// Fewer than two items: a clearing must relate at least two postings.
    #[error("a clearing needs at least two items, got {count}")]
    TooFewItems {
        /// Number of items supplied.
        count: usize,
    },
    /// The same posting appeared twice in one clearing.
    #[error("posting {posting} appears more than once in the same clearing")]
    DuplicateItem {
        /// The repeated reference.
        posting: PostingRef,
    },
    /// A referenced posting does not exist.
    #[error("posting {posting} does not resolve")]
    UnknownPosting {
        /// The unresolvable reference.
        posting: PostingRef,
    },
    /// A referenced posting is on a different account.
    #[error("posting {posting} is not on account {expected}")]
    WrongAccount {
        /// The offending reference.
        posting: PostingRef,
        /// The account the clearing is for.
        expected: AccountId,
    },
    /// A referenced posting is in a different currency.
    #[error("posting {posting} is not in {expected}")]
    WrongCurrency {
        /// The offending reference.
        posting: PostingRef,
        /// The currency the clearing is for.
        expected: Currency,
    },
    /// A referenced posting is in a different layer.
    #[error("posting {posting} is not in the {expected} layer")]
    WrongLayer {
        /// The offending reference.
        posting: PostingRef,
        /// The layer the clearing is for.
        expected: Layer,
    },
    /// An applied amount was zero or negative.
    #[error("posting {posting} was applied a non-positive amount")]
    NonPositiveApplication {
        /// The offending reference.
        posting: PostingRef,
    },
    /// Applying this much would exceed what the posting still has open.
    ///
    /// Amounts are minor units at `scale`, so the variant stays independent of
    /// the ledger's compile-time precision.
    #[error(
        "applying {requested_minor} to {posting} exceeds its residual of \
         {residual_minor} (scale {scale})"
    )]
    OverApplied {
        /// The offending reference.
        posting: PostingRef,
        /// The amount requested, in minor units.
        requested_minor: i64,
        /// The amount still open, in minor units.
        residual_minor: i64,
        /// Decimal places the minor units are expressed in.
        scale: u8,
    },
    /// The debit and credit sides of the clearing did not match.
    #[error(
        "clearing is unbalanced at scale {scale}: debits {debits_minor}, \
         credits {credits_minor}"
    )]
    Unbalanced {
        /// Total applied on the debit side, in minor units.
        debits_minor: i64,
        /// Total applied on the credit side, in minor units.
        credits_minor: i64,
        /// Decimal places the minor units are expressed in.
        scale: u8,
    },
    /// The clearing identifier was already used.
    #[error("clearing {id} is already recorded")]
    DuplicateId {
        /// The offending identifier.
        id: ClearingId,
    },
    /// The clearing being reset does not exist.
    #[error("clearing {id} is not recorded")]
    UnknownClearing {
        /// The missing identifier.
        id: ClearingId,
    },
    /// The clearing being reset has already been reset.
    #[error("clearing {id} has already been reset")]
    AlreadyReset {
        /// The offending identifier.
        id: ClearingId,
    },
    /// Arithmetic overflowed.
    #[error(transparent)]
    Money(#[from] MoneyError),
}

/// What a clearing did, recorded in order.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ClearingEvent<const P: u8> {
    /// Items were assigned to one another.
    Cleared(Clearing<P>),
    /// An earlier assignment was released.
    Reset {
        /// The clearing being released.
        clearing: ClearingId,
        /// When it was released.
        on: Date,
    },
}

/// A posting with something still outstanding on it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OpenItem<const P: u8> {
    /// Which posting.
    pub posting: PostingRef,
    /// Which side it falls on.
    pub direction: Direction,
    /// The posting's original amount.
    pub original: Amount<P>,
    /// How much has been applied to it.
    pub applied: Amount<P>,
    /// What remains open.
    pub residual: Amount<P>,
}

/// The append-only record of clearings, and the open items it implies.
///
/// Applied amounts are derived by replaying the events, so a reset genuinely
/// releases what it released and nothing is edited in place.
#[derive(Debug, Clone)]
pub struct ClearingRegister<const P: u8> {
    events: Vec<ClearingEvent<P>>,
    /// Applied amount per posting, derived from `events`.
    applied: BTreeMap<PostingRef, Amount<P>>,
    /// Clearings that have been reset.
    reset: BTreeMap<ClearingId, Date>,
    /// Index of every recorded clearing.
    by_id: BTreeMap<ClearingId, usize>,
}

impl<const P: u8> Default for ClearingRegister<P> {
    fn default() -> Self {
        Self::new()
    }
}

impl<const P: u8> ClearingRegister<P> {
    /// Creates an empty register.
    #[must_use]
    pub fn new() -> Self {
        Self {
            events: Vec::new(),
            applied: BTreeMap::new(),
            reset: BTreeMap::new(),
            by_id: BTreeMap::new(),
        }
    }

    /// How much has been applied to a posting.
    #[must_use]
    pub fn applied_to(&self, posting: PostingRef) -> Amount<P> {
        self.applied.get(&posting).copied().unwrap_or(Amount::ZERO)
    }

    /// What remains open on a posting.
    pub fn residual_of(
        &self,
        posting: PostingRef,
        lookup: &impl PostingLookup<P>,
    ) -> Result<Amount<P>, ClearingError> {
        let found = lookup
            .posting(posting)
            .ok_or(ClearingError::UnknownPosting { posting })?;
        Ok(found.amount.checked_sub(self.applied_to(posting))?)
    }

    /// Records a clearing after checking every rule.
    pub fn clear(
        &mut self,
        clearing: Clearing<P>,
        lookup: &impl PostingLookup<P>,
    ) -> Result<(), ClearingError> {
        if self.by_id.contains_key(&clearing.id) {
            return Err(ClearingError::DuplicateId { id: clearing.id });
        }
        if clearing.items.len() < 2 {
            return Err(ClearingError::TooFewItems {
                count: clearing.items.len(),
            });
        }

        let mut seen: BTreeMap<PostingRef, ()> = BTreeMap::new();
        let mut sides = Balance::<P>::ZERO;

        for item in &clearing.items {
            if seen.insert(item.posting, ()).is_some() {
                return Err(ClearingError::DuplicateItem {
                    posting: item.posting,
                });
            }
            if !item.applied.is_positive() {
                return Err(ClearingError::NonPositiveApplication {
                    posting: item.posting,
                });
            }

            let posting = lookup
                .posting(item.posting)
                .ok_or(ClearingError::UnknownPosting {
                    posting: item.posting,
                })?;
            if posting.account != clearing.account {
                return Err(ClearingError::WrongAccount {
                    posting: item.posting,
                    expected: clearing.account,
                });
            }
            if posting.currency != clearing.currency {
                return Err(ClearingError::WrongCurrency {
                    posting: item.posting,
                    expected: clearing.currency,
                });
            }
            if posting.layer != clearing.layer {
                return Err(ClearingError::WrongLayer {
                    posting: item.posting,
                    expected: clearing.layer,
                });
            }

            let residual = posting.amount.checked_sub(self.applied_to(item.posting))?;
            if item.applied > residual {
                return Err(ClearingError::OverApplied {
                    posting: item.posting,
                    requested_minor: item.applied.to_minor(),
                    residual_minor: residual.to_minor(),
                    scale: P,
                });
            }

            sides.add(posting.direction, item.applied)?;
        }

        if !sides.is_balanced() {
            return Err(ClearingError::Unbalanced {
                debits_minor: sides.debits.to_minor(),
                credits_minor: sides.credits.to_minor(),
                scale: P,
            });
        }

        let mut staged = Vec::with_capacity(clearing.items.len());
        for item in &clearing.items {
            let updated = self.applied_to(item.posting).checked_add(item.applied)?;
            staged.push((item.posting, updated));
        }
        for (posting, updated) in staged {
            self.applied.insert(posting, updated);
        }
        self.by_id.insert(clearing.id, self.events.len());
        self.events.push(ClearingEvent::Cleared(clearing));
        Ok(())
    }

    /// Releases a clearing, reopening the items it had assigned.
    pub fn reset(&mut self, id: ClearingId, on: Date) -> Result<(), ClearingError> {
        let Some(index) = self.by_id.get(&id).copied() else {
            return Err(ClearingError::UnknownClearing { id });
        };
        if self.reset.contains_key(&id) {
            return Err(ClearingError::AlreadyReset { id });
        }
        let Some(ClearingEvent::Cleared(clearing)) = self.events.get(index) else {
            return Err(ClearingError::UnknownClearing { id });
        };

        let items = clearing.items.clone();
        let mut staged = Vec::with_capacity(items.len());
        for item in &items {
            let updated = self.applied_to(item.posting).checked_sub(item.applied)?;
            staged.push((item.posting, updated));
        }
        for (posting, updated) in staged {
            self.applied.insert(posting, updated);
        }
        self.reset.insert(id, on);
        self.events.push(ClearingEvent::Reset { clearing: id, on });
        Ok(())
    }

    /// True when a clearing has been released.
    #[must_use]
    pub fn is_reset(&self, id: ClearingId) -> bool {
        self.reset.contains_key(&id)
    }

    /// The clearing with a given identifier.
    #[must_use]
    pub fn get(&self, id: ClearingId) -> Option<&Clearing<P>> {
        match self.by_id.get(&id).and_then(|i| self.events.get(*i)) {
            Some(ClearingEvent::Cleared(c)) => Some(c),
            _ => None,
        }
    }

    /// Every recorded event, in order.
    #[must_use]
    pub fn events(&self) -> &[ClearingEvent<P>] {
        &self.events
    }

    /// Number of clearings recorded, including any since reset.
    #[must_use]
    pub fn len(&self) -> usize {
        self.by_id.len()
    }

    /// True when nothing has been cleared.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.by_id.is_empty()
    }

    /// Computes the open items among `candidates`.
    ///
    /// Ordered by posting reference, so the result is reproducible.
    pub fn open_items(
        &self,
        candidates: impl IntoIterator<Item = PostingRef>,
        lookup: &impl PostingLookup<P>,
    ) -> Result<Vec<OpenItem<P>>, ClearingError> {
        let mut out = Vec::new();
        for reference in candidates {
            let Some(posting) = lookup.posting(reference) else {
                return Err(ClearingError::UnknownPosting { posting: reference });
            };
            let applied = self.applied_to(reference);
            let residual = posting.amount.checked_sub(applied)?;
            if residual.is_positive() {
                out.push(OpenItem {
                    posting: reference,
                    direction: posting.direction,
                    original: posting.amount,
                    applied,
                    residual,
                });
            }
        }
        out.sort_by_key(|i| i.posting);
        Ok(out)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use time::macros::date;

    type Eur = Amount<2>;

    /// A hand-built posting table, so these tests exercise clearing alone.
    struct Table {
        postings: BTreeMap<PostingRef, Posting<2>>,
    }

    impl PostingLookup<2> for Table {
        fn posting(&self, reference: PostingRef) -> Option<&Posting<2>> {
            self.postings.get(&reference)
        }
    }

    fn account() -> AccountId {
        AccountId::from_index(0)
    }

    fn table(entries: &[(u16, Direction, i64)]) -> (Table, Vec<PostingRef>) {
        let entry = EntryId::generate();
        let mut postings = BTreeMap::new();
        let mut refs = Vec::new();
        for (index, direction, amount) in entries {
            let reference = PostingRef::new(entry, *index);
            postings.insert(
                reference,
                Posting::new(
                    account(),
                    *direction,
                    Eur::from_minor(*amount),
                    Currency::EUR,
                ),
            );
            refs.push(reference);
        }
        (Table { postings }, refs)
    }

    fn key(layer: Layer) -> BalanceKey {
        BalanceKey {
            account: account(),
            currency: Currency::EUR,
            layer,
        }
    }

    fn clearing(items: &[(PostingRef, i64)]) -> Clearing<2> {
        clearing_in(Layer::Settled, items)
    }

    fn clearing_in(layer: Layer, items: &[(PostingRef, i64)]) -> Clearing<2> {
        items.iter().fold(
            Clearing::new(ClearingId::generate(), key(layer), date!(2026 - 03 - 15)),
            |c, (posting, applied)| c.apply(*posting, Eur::from_minor(*applied)),
        )
    }

    #[test]
    fn a_full_clearing_closes_both_items() {
        let (t, r) = table(&[(0, Direction::Debit, 1000), (1, Direction::Credit, 1000)]);
        let mut reg = ClearingRegister::<2>::new();
        reg.clear(clearing(&[(r[0], 1000), (r[1], 1000)]), &t)
            .expect("clears");

        let open = reg.open_items(r.iter().copied(), &t).expect("ok");
        assert!(open.is_empty(), "nothing should remain open");
        assert_eq!(reg.len(), 1);
    }

    #[test]
    fn a_partial_payment_leaves_both_sides_open() {
        let (t, r) = table(&[(0, Direction::Debit, 1000), (1, Direction::Credit, 400)]);
        let mut reg = ClearingRegister::<2>::new();
        reg.clear(clearing(&[(r[0], 400), (r[1], 400)]), &t)
            .expect("clears");

        let open = reg.open_items(r.iter().copied(), &t).expect("ok");
        assert_eq!(open.len(), 1, "the invoice remains partly open");
        let item = open.first().expect("present");
        assert_eq!(item.posting, r[0]);
        assert_eq!(item.original, Eur::from_minor(1000));
        assert_eq!(item.applied, Eur::from_minor(400));
        assert_eq!(item.residual, Eur::from_minor(600));
    }

    #[test]
    fn successive_partial_payments_accumulate() {
        let (t, r) = table(&[
            (0, Direction::Debit, 1000),
            (1, Direction::Credit, 400),
            (2, Direction::Credit, 600),
        ]);
        let mut reg = ClearingRegister::<2>::new();
        reg.clear(clearing(&[(r[0], 400), (r[1], 400)]), &t)
            .expect("first payment");
        reg.clear(clearing(&[(r[0], 600), (r[2], 600)]), &t)
            .expect("second payment");

        assert_eq!(reg.applied_to(r[0]), Eur::from_minor(1000));
        assert!(
            reg.open_items(r.iter().copied(), &t)
                .expect("ok")
                .is_empty()
        );
    }

    #[test]
    fn over_application_is_refused() {
        let (t, r) = table(&[(0, Direction::Debit, 1000), (1, Direction::Credit, 1000)]);
        let mut reg = ClearingRegister::<2>::new();
        reg.clear(clearing(&[(r[0], 600), (r[1], 600)]), &t)
            .expect("first");

        let err = reg
            .clear(clearing(&[(r[0], 500), (r[1], 500)]), &t)
            .expect_err("only 400 remains");
        assert!(matches!(err, ClearingError::OverApplied { .. }));

        // The failed attempt applied nothing.
        assert_eq!(reg.applied_to(r[0]), Eur::from_minor(600));
    }

    #[test]
    fn an_unbalanced_clearing_is_refused() {
        let (t, r) = table(&[(0, Direction::Debit, 1000), (1, Direction::Credit, 1000)]);
        let mut reg = ClearingRegister::<2>::new();
        let err = reg
            .clear(clearing(&[(r[0], 1000), (r[1], 900)]), &t)
            .expect_err("sides differ");
        assert!(matches!(err, ClearingError::Unbalanced { .. }));
        assert_eq!(reg.applied_to(r[0]), Eur::ZERO);
    }

    #[test]
    fn same_side_items_alone_cannot_clear() {
        let (t, r) = table(&[(0, Direction::Debit, 500), (1, Direction::Debit, 500)]);
        let mut reg = ClearingRegister::<2>::new();
        assert!(matches!(
            reg.clear(clearing(&[(r[0], 500), (r[1], 500)]), &t),
            Err(ClearingError::Unbalanced { .. })
        ));
    }

    #[test]
    fn a_reset_reopens_the_items() {
        let (t, r) = table(&[(0, Direction::Debit, 1000), (1, Direction::Credit, 1000)]);
        let mut reg = ClearingRegister::<2>::new();
        let c = clearing(&[(r[0], 1000), (r[1], 1000)]);
        let id = c.id;
        reg.clear(c, &t).expect("clears");
        assert!(
            reg.open_items(r.iter().copied(), &t)
                .expect("ok")
                .is_empty()
        );

        reg.reset(id, date!(2026 - 04 - 01)).expect("resets");
        let open = reg.open_items(r.iter().copied(), &t).expect("ok");
        assert_eq!(open.len(), 2, "both items reopen");
        assert!(reg.is_reset(id));

        // The withdrawn assignment stays in the record.
        assert_eq!(reg.events().len(), 2);
        assert!(reg.get(id).is_some());
    }

    #[test]
    fn a_clearing_cannot_be_reset_twice() {
        let (t, r) = table(&[(0, Direction::Debit, 100), (1, Direction::Credit, 100)]);
        let mut reg = ClearingRegister::<2>::new();
        let c = clearing(&[(r[0], 100), (r[1], 100)]);
        let id = c.id;
        reg.clear(c, &t).expect("clears");
        reg.reset(id, date!(2026 - 04 - 01)).expect("resets");
        assert!(matches!(
            reg.reset(id, date!(2026 - 04 - 02)),
            Err(ClearingError::AlreadyReset { .. })
        ));
    }

    #[test]
    fn resetting_an_unknown_clearing_is_an_error() {
        let mut reg = ClearingRegister::<2>::new();
        assert!(matches!(
            reg.reset(ClearingId::generate(), date!(2026 - 04 - 01)),
            Err(ClearingError::UnknownClearing { .. })
        ));
    }

    #[test]
    fn reset_then_reclear_works() {
        let (t, r) = table(&[
            (0, Direction::Debit, 1000),
            (1, Direction::Credit, 1000),
            (2, Direction::Credit, 1000),
        ]);
        let mut reg = ClearingRegister::<2>::new();

        // Matched to the wrong payment, then corrected.
        let wrong = clearing(&[(r[0], 1000), (r[1], 1000)]);
        let wrong_id = wrong.id;
        reg.clear(wrong, &t).expect("clears");
        reg.reset(wrong_id, date!(2026 - 04 - 01)).expect("resets");
        reg.clear(clearing(&[(r[0], 1000), (r[2], 1000)]), &t)
            .expect("re-clears against the right item");

        assert_eq!(reg.applied_to(r[1]), Eur::ZERO);
        assert_eq!(reg.applied_to(r[2]), Eur::from_minor(1000));
    }

    #[test]
    fn structural_rules_are_enforced() {
        let (t, r) = table(&[(0, Direction::Debit, 100), (1, Direction::Credit, 100)]);
        let mut reg = ClearingRegister::<2>::new();

        assert!(matches!(
            reg.clear(clearing(&[(r[0], 100)]), &t),
            Err(ClearingError::TooFewItems { count: 1 })
        ));
        assert!(matches!(
            reg.clear(clearing(&[(r[0], 50), (r[0], 50)]), &t),
            Err(ClearingError::DuplicateItem { .. })
        ));
        assert!(matches!(
            reg.clear(clearing(&[(r[0], 0), (r[1], 0)]), &t),
            Err(ClearingError::NonPositiveApplication { .. })
        ));

        let ghost = PostingRef::new(EntryId::generate(), 9);
        assert!(matches!(
            reg.clear(clearing(&[(r[0], 100), (ghost, 100)]), &t),
            Err(ClearingError::UnknownPosting { .. })
        ));
    }

    #[test]
    fn items_on_another_account_or_currency_are_refused() {
        let entry = EntryId::generate();
        let a = PostingRef::new(entry, 0);
        let b = PostingRef::new(entry, 1);
        let mut postings = BTreeMap::new();
        postings.insert(
            a,
            Posting::new(
                account(),
                Direction::Debit,
                Eur::from_minor(100),
                Currency::EUR,
            ),
        );
        postings.insert(
            b,
            Posting::new(
                AccountId::from_index(7),
                Direction::Credit,
                Eur::from_minor(100),
                Currency::EUR,
            ),
        );
        let t = Table { postings };
        let mut reg = ClearingRegister::<2>::new();
        assert!(matches!(
            reg.clear(clearing(&[(a, 100), (b, 100)]), &t),
            Err(ClearingError::WrongAccount { .. })
        ));

        let mut postings = BTreeMap::new();
        postings.insert(
            a,
            Posting::new(
                account(),
                Direction::Debit,
                Eur::from_minor(100),
                Currency::EUR,
            ),
        );
        postings.insert(
            b,
            Posting::new(
                account(),
                Direction::Credit,
                Eur::from_minor(100),
                Currency::USD,
            ),
        );
        let t = Table { postings };
        assert!(matches!(
            reg.clear(clearing(&[(a, 100), (b, 100)]), &t),
            Err(ClearingError::WrongCurrency { .. })
        ));
    }

    #[test]
    fn a_duplicate_clearing_id_is_refused() {
        let (t, r) = table(&[(0, Direction::Debit, 1000), (1, Direction::Credit, 1000)]);
        let mut reg = ClearingRegister::<2>::new();
        let mut first = clearing(&[(r[0], 400), (r[1], 400)]);
        let id = first.id;
        reg.clear(first.clone(), &t).expect("clears");

        first.id = id;
        first.items = vec![
            ClearedItem {
                posting: r[0],
                applied: Eur::from_minor(100),
            },
            ClearedItem {
                posting: r[1],
                applied: Eur::from_minor(100),
            },
        ];
        assert!(matches!(
            reg.clear(first, &t),
            Err(ClearingError::DuplicateId { .. })
        ));
    }

    #[test]
    fn reservations_clear_against_reservations() {
        let entry = EntryId::generate();
        let a = PostingRef::new(entry, 0);
        let b = PostingRef::new(entry, 1);
        let mut postings = BTreeMap::new();
        for (reference, direction) in [(a, Direction::Debit), (b, Direction::Credit)] {
            postings.insert(
                reference,
                Posting::new(account(), direction, Eur::from_minor(50), Currency::EUR)
                    .in_layer(Layer::Pending),
            );
        }
        let t = Table { postings };
        let mut reg = ClearingRegister::<2>::new();
        assert!(
            reg.clear(clearing_in(Layer::Pending, &[(a, 50), (b, 50)]), &t)
                .is_ok()
        );
    }

    #[test]
    fn a_reservation_does_not_clear_against_a_settled_movement() {
        // Netting the two would report an open item closed while the money had
        // not moved.
        let entry = EntryId::generate();
        let reserved = PostingRef::new(entry, 0);
        let settled = PostingRef::new(entry, 1);
        let mut postings = BTreeMap::new();
        postings.insert(
            reserved,
            Posting::new(
                account(),
                Direction::Debit,
                Eur::from_minor(50),
                Currency::EUR,
            )
            .in_layer(Layer::Pending),
        );
        postings.insert(
            settled,
            Posting::new(
                account(),
                Direction::Credit,
                Eur::from_minor(50),
                Currency::EUR,
            ),
        );
        let t = Table { postings };
        let mut reg = ClearingRegister::<2>::new();
        assert!(matches!(
            reg.clear(
                clearing_in(Layer::Settled, &[(reserved, 50), (settled, 50)]),
                &t
            ),
            Err(ClearingError::WrongLayer { .. })
        ));
    }
}