rustledger-booking 0.16.1

Beancount booking engine with 7 lot matching methods and interpolation
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
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
//! Transaction booking with lot matching.
//!
//! This module handles:
//! - Tracking inventory across transactions
//! - Matching sold lots against existing holdings
//! - Calculating capital gains/losses
//! - Filling in cost specs for lot reductions

// ratchet: fxhash-only — hot path; use FxHashMap/FxHashSet, not std SipHash collections (#1237).
use rustc_hash::{FxHashMap, FxHashSet};
use rustledger_core::{
    AccountedBookingError, Amount, BookingMethod, Cost, CostSpec, Directive, IncompleteAmount,
    Inventory, Position, Posting, ReductionScope, Transaction,
};
use thiserror::Error;

use crate::{InterpolationError, InterpolationResult, interpolate};

// Note: We no longer quantize calculated values during booking.
// Python beancount preserves full precision during booking and only
// rounds at display time. Premature rounding of per-unit costs (e.g.,
// from total cost / units) causes cost basis errors when selling.
// For example: 300.00 / 1.763 = 170.16505... should NOT be rounded
// to 170.17, because 1.763 * 170.17 = 300.00971 ≠ 300.00.

/// Errors that can occur during booking.
///
/// Inventory-level failures (insufficient units, no matching lot, ambiguous
/// match, currency mismatch) are unified under [`BookingError::Inventory`],
/// which carries an [`AccountedBookingError`] from `rustledger-core`. This
/// keeps the user-facing wording in **one place** so it cannot drift between
/// the booking layer and the validator — see #748 / #750.
#[derive(Debug, Clone, Error)]
pub enum BookingError {
    /// An inventory-level booking failure (insufficient units, no matching
    /// lot, ambiguous match, currency mismatch).
    ///
    /// `Display` is delegated to the inner [`AccountedBookingError`], which
    /// is the single canonical source of wording for booking errors. The
    /// pta-standards `reduction-exceeds-inventory` conformance test depends
    /// on this Display containing the literal substring `"not enough"`.
    #[error(transparent)]
    Inventory(AccountedBookingError),

    /// Interpolation failed after booking.
    #[error("interpolation failed: {0}")]
    Interpolation(#[from] InterpolationError),
}

/// Result of booking a single transaction.
#[derive(Debug, Clone)]
pub struct BookedTransaction {
    /// The transaction with costs filled in.
    pub transaction: Transaction,
    /// Capital gains/losses generated by this transaction.
    pub gains: Vec<CapitalGain>,
    /// Which posting indices had costs filled in.
    pub booked_indices: Vec<usize>,
}

/// A capital gain or loss from a lot sale.
#[derive(Debug, Clone)]
pub struct CapitalGain {
    /// The account holding the asset.
    pub account: rustledger_core::Account,
    /// The currency of the asset.
    pub currency: rustledger_core::Currency,
    /// The gain amount (positive) or loss (negative).
    pub amount: Amount,
    /// Cost basis of the sold lot.
    pub cost_basis: Amount,
    /// Sale proceeds.
    pub proceeds: Amount,
}

/// Booking engine that tracks inventory across transactions.
#[derive(Debug, Default)]
pub struct BookingEngine {
    /// Inventory per account.
    inventories: FxHashMap<rustledger_core::Account, Inventory>,
    /// Default booking method, used for accounts without an explicit
    /// booking method on their `open` directive.
    booking_method: BookingMethod,
    /// Per-account booking method overrides (from `open` directives).
    /// Looked up first, falling back to `booking_method` if absent.
    account_methods: FxHashMap<rustledger_core::Account, BookingMethod>,
}

impl BookingEngine {
    /// Create a new booking engine with default FIFO booking.
    #[must_use]
    pub fn new() -> Self {
        Self {
            inventories: FxHashMap::default(),
            booking_method: BookingMethod::Fifo,
            account_methods: FxHashMap::default(),
        }
    }

    /// Create a booking engine with a specific default booking method.
    #[must_use]
    pub fn with_method(method: BookingMethod) -> Self {
        Self {
            inventories: FxHashMap::default(),
            booking_method: method,
            account_methods: FxHashMap::default(),
        }
    }

    /// Register the booking method for a specific account.
    ///
    /// Call this for each `open` directive *before* booking transactions for
    /// that account, so the engine uses the per-account method (e.g. FIFO,
    /// LIFO, NONE) rather than the engine-wide default. Subsequent calls
    /// overwrite the previous method for the account.
    pub fn set_account_method(&mut self, account: rustledger_core::Account, method: BookingMethod) {
        self.account_methods.insert(account, method);
    }

    /// Scan a sequence of directives and register any per-account booking
    /// methods found on `open` directives. Open directives whose booking
    /// method is absent or fails to parse are silently ignored (they fall
    /// back to the engine-wide default).
    ///
    /// This is a convenience wrapper around [`Self::set_account_method`] for
    /// the common pipeline pattern of scanning all directives once before
    /// the booking loop. Call this before booking any transactions so the
    /// engine uses each account's declared method rather than the
    /// engine-wide default for every account.
    pub fn register_account_methods<'a, I>(&mut self, directives: I)
    where
        I: IntoIterator<Item = &'a rustledger_core::Directive>,
    {
        for directive in directives {
            if let rustledger_core::Directive::Open(open) = directive
                && let Some(method_str) = &open.booking
                && let Ok(method) = method_str.parse::<BookingMethod>()
            {
                self.set_account_method(open.account.clone(), method);
            }
        }
    }

    /// Resolve the booking method for an account, falling back to the
    /// engine-wide default if not registered.
    fn method_for(&self, account: &rustledger_core::Account) -> BookingMethod {
        self.account_methods
            .get(account)
            .copied()
            .unwrap_or(self.booking_method)
    }

    /// Get the inventory for an account.
    #[must_use]
    pub fn inventory(&self, account: &rustledger_core::Account) -> Option<&Inventory> {
        self.inventories.get(account)
    }

    /// Book a transaction: fill in empty cost specs and calculate gains.
    ///
    /// This does NOT modify the internal inventories - call `apply` for that.
    ///
    /// When a reduction matches multiple lots (e.g., selling shares that were purchased
    /// across multiple buy transactions), the posting is expanded into multiple postings,
    /// one for each matched lot. This matches Python beancount's behavior.
    pub fn book(&self, txn: &Transaction) -> Result<BookedTransaction, BookingError> {
        // Fast path: if no postings have cost specs, no booking is needed.
        // This avoids expensive inventory cloning for simple transactions.
        let has_cost_specs = txn.postings.iter().any(|p| p.cost.is_some());
        if !has_cost_specs {
            return Ok(BookedTransaction {
                transaction: txn.clone(),
                gains: Vec::new(),
                booked_indices: Vec::new(),
            });
        }

        let mut result = txn.clone();
        let mut gains = Vec::new();
        let mut booked_indices: FxHashSet<usize> =
            FxHashSet::with_capacity_and_hasher(txn.postings.len(), Default::default());
        // Track posting expansions: (original_idx, expanded_postings)
        let mut expansions: Vec<(usize, Vec<rustledger_core::Spanned<Posting>>)> =
            Vec::with_capacity(txn.postings.len());

        // Create working copies of inventories for this transaction.
        // This allows us to track inventory changes across multiple postings
        // within the same transaction (e.g., main sale + fee posting).
        //
        // Clone only the inventories we actually need for this transaction's
        // accounts. Use `entry().or_insert_with(...)` so that a posting list
        // with repeated accounts (e.g., two postings on `Assets:Stock`) only
        // triggers one clone per unique account instead of cloning the same
        // inventory every time it appears. Without deduping, the optimization
        // would be silently undone by transactions that list the same
        // account more than once.
        let mut working_inventories: FxHashMap<rustledger_core::Account, Inventory> =
            FxHashMap::with_capacity_and_hasher(txn.postings.len(), Default::default());
        for posting in &txn.postings {
            if let Some(inv) = self.inventories.get(&posting.account) {
                working_inventories
                    .entry(posting.account.clone())
                    .or_insert_with(|| inv.clone());
            }
        }

        // First pass: identify postings that need lot matching (reductions)
        for (idx, posting) in txn.postings.iter().enumerate() {
            // Check if this is a reduction with a cost spec
            if let Some(IncompleteAmount::Complete(units)) = &posting.units
                && let Some(cost_spec) = &posting.cost
            {
                // Check if this is a reduction (units have opposite sign of inventory)
                // This handles both:
                // - Selling long positions (negative units, positive inventory)
                // - Closing short positions (positive units, negative inventory)
                if let Some(inv) = working_inventories.get_mut(&posting.account) {
                    // Check if these units reduce existing cost-bearing inventory lots.
                    // Only positions with a cost basis are considered; simple (no-cost)
                    // positions are ignored to avoid misclassifying augmentations.
                    //
                    // Under `option "booking_method" "NONE"` (issue #1182),
                    // reduction matching is skipped entirely: NONE means
                    // "accumulate positions without booking against
                    // existing lots." Otherwise the booker would replace
                    // the user-written `{{ total }}` cost spec with a
                    // FIFO-matched per-unit (line ~282 below), and the
                    // residual calculation downstream would weigh the
                    // posting by the matched lots' costs instead of the
                    // user's stated total — producing a phantom
                    // E3001 imbalance for ledgers that round-trip
                    // cleanly through Python beancount.
                    let method = self.method_for(&posting.account);
                    let is_reduction = method != BookingMethod::None
                        && inv.is_reduced_by(units, ReductionScope::CostBearingOnly);

                    if is_reduction {
                        // Use reduce (not try_reduce) to actually update the working inventory.
                        // This ensures subsequent postings in the same transaction see
                        // the updated inventory state (e.g., after first posting exhausts a lot).
                        //
                        // Booking errors (ambiguous match, no matching lot, insufficient
                        // units) are propagated so callers see them once. The full
                        // pipeline path in `rustledger check` filters failed transactions
                        // out of the validator's input to avoid double-reporting against
                        // the validator's independent lot-matching pass.
                        // (`method` is resolved above next to the NONE-method gate.)
                        let booking_result = inv
                            .reduce(units, Some(cost_spec), method)
                            .map_err(|e| convert_core_booking_error(e, &posting.account))?;
                        {
                            // Check if multiple lots were matched
                            if booking_result.matched.len() > 1 {
                                // Expand single posting into multiple postings
                                let mut expanded = Vec::new();
                                for matched_pos in &booking_result.matched {
                                    let mut new_posting = posting.clone();
                                    // Set units to the matched portion with NEGATED sign
                                    // (matched_pos.units has the inventory sign, but we need
                                    // the reduction sign which is opposite)
                                    let expanded_units = rustledger_core::Amount::new(
                                        -matched_pos.units.number, // Negate: inventory→reduction
                                        matched_pos.units.currency.clone(),
                                    );
                                    new_posting.units =
                                        Some(IncompleteAmount::Complete(expanded_units));
                                    // Set cost from the matched lot
                                    if let Some(cost) = &matched_pos.cost {
                                        new_posting.cost = Some(CostSpec {
                                            number: Some(rustledger_core::CostNumber::PerUnit {
                                                value: cost.number,
                                            }),
                                            currency: Some(cost.currency.clone()),
                                            date: cost.date,
                                            label: cost.label.clone(),
                                            merge: false,
                                        });
                                    }
                                    expanded.push(new_posting);
                                }
                                expansions.push((idx, expanded));
                                booked_indices.insert(idx);
                            } else if let Some(cost_basis) = &booking_result.cost_basis {
                                // Single lot match - update posting in place
                                let per_unit = cost_basis.number / units.number.abs();
                                // Use new_calculated since per_unit is computed from total/units
                                let matched_cost =
                                    Cost::new_calculated(per_unit, cost_basis.currency.clone())
                                        .with_date_opt(
                                            booking_result
                                                .matched
                                                .first()
                                                .and_then(|p| p.cost.as_ref())
                                                .and_then(|c| c.date),
                                        );

                                // Update posting with filled cost
                                result.postings[idx].cost = Some(CostSpec {
                                    number: Some(rustledger_core::CostNumber::PerUnit {
                                        value: matched_cost.number,
                                    }),
                                    currency: Some(matched_cost.currency.clone()),
                                    date: matched_cost.date,
                                    label: None,
                                    merge: false,
                                });
                                booked_indices.insert(idx);
                            }

                            // Calculate capital gain if there's a price
                            if let Some(cost_basis) = &booking_result.cost_basis
                                && let Some(price) = &posting.price
                                && let Some(amt) =
                                    price.amount.as_ref().and_then(IncompleteAmount::as_amount)
                            {
                                let sale_price = match price.kind {
                                    rustledger_core::PriceKind::Unit => {
                                        amt.number * units.number.abs()
                                    }
                                    rustledger_core::PriceKind::Total => amt.number,
                                };

                                let gain_amount = sale_price - cost_basis.number;
                                if !gain_amount.is_zero() {
                                    gains.push(CapitalGain {
                                        account: posting.account.clone(),
                                        currency: units.currency.clone(),
                                        amount: Amount::new(gain_amount, &cost_basis.currency),
                                        cost_basis: cost_basis.clone(),
                                        proceeds: Amount::new(sale_price, &cost_basis.currency),
                                    });
                                }
                            }
                        }
                    }
                    // If not a reduction: fall through to augmentation code below
                }

                if let Some(rustledger_core::CostNumber::Total { value: total }) = cost_spec.number
                {
                    // Augmentation with total cost — convert to the
                    // post-booking `PerUnitFromTotal` shape:
                    //   `1.763 VIIIX {{300.00 USD}}` → derived per-unit
                    //   170.165… with total 300.00 preserved.
                    // The preserved total is load-bearing for
                    // precision-preserving residual math (#1026) —
                    // division-then-multiplication at the
                    // `rust_decimal` 28-digit ceiling loses precision.
                    if let Some(currency) = &cost_spec.currency
                        && !units.number.is_zero()
                    {
                        let per_unit = total / units.number.abs();
                        result.postings[idx].cost = Some(CostSpec {
                            number: Some(rustledger_core::CostNumber::PerUnitFromTotal(
                                rustledger_core::BookedCost::new(per_unit, total, units.number),
                            )),
                            currency: Some(currency.clone()),
                            // Fill in transaction date if no date specified
                            date: cost_spec.date.or(Some(txn.date)),
                            label: cost_spec.label.clone(),
                            merge: cost_spec.merge,
                        });
                        booked_indices.insert(idx);
                    }
                }

                // Fill in dates and currencies for augmentations (not already booked)
                if !booked_indices.contains(&idx) && cost_spec.number.is_some() {
                    // Cost spec has a number but may be missing date or currency
                    // Fill in missing parts from price annotation, other postings, and transaction date
                    let inferred_currency = cost_spec.currency.clone().or_else(|| {
                        // First try price annotation on this posting.
                        // `kind` (Unit vs Total) doesn't change the currency,
                        // so it's irrelevant here — we just want whatever
                        // currency the price names, complete or incomplete.
                        posting
                                .price
                                .as_ref()
                                .and_then(|p| p.amount.as_ref())
                                .and_then(|inc| inc.currency().map(Into::into))
                                // Then try inferring from other postings in the transaction
                                .or_else(|| crate::infer_cost_currency_from_postings(txn))
                    });

                    // Check if this is a reduction (opposite sign exists in inventory)
                    // Reductions get their date from matched lot, augmentations get txn date
                    let is_reduction = self.inventories.get(&posting.account).is_some_and(|inv| {
                        inv.is_reduced_by(units, ReductionScope::CostBearingOnly)
                    });

                    // Fill in date for augmentations only (not reductions)
                    let inferred_date = if is_reduction {
                        None // Reductions get their date from matched lot
                    } else {
                        cost_spec.date.or(Some(txn.date))
                    };

                    // Only update if we actually inferred something
                    if inferred_currency.is_some() || inferred_date.is_some() {
                        result.postings[idx].cost = Some(CostSpec {
                            number: cost_spec.number,
                            currency: inferred_currency.or_else(|| cost_spec.currency.clone()),
                            date: inferred_date.or(cost_spec.date),
                            label: cost_spec.label.clone(),
                            merge: cost_spec.merge,
                        });
                    }
                }
            }
        }

        // Apply posting expansions (replace single postings with multiple)
        // Build new postings Vec in one O(n) pass instead of O(n²) remove+insert
        if !expansions.is_empty() {
            // Sort expansions by index for forward iteration
            expansions.sort_by_key(|(idx, _)| *idx);

            let mut new_postings = Vec::with_capacity(
                result.postings.len() + expansions.iter().map(|(_, e)| e.len()).sum::<usize>(),
            );
            let mut expansion_iter = expansions.into_iter().peekable();

            for (idx, posting) in result.postings.into_iter().enumerate() {
                if expansion_iter
                    .peek()
                    .is_some_and(|(exp_idx, _)| *exp_idx == idx)
                {
                    // Replace this posting with expanded postings
                    let (_, expanded) = expansion_iter.next().unwrap();
                    new_postings.extend(expanded);
                } else {
                    // Keep original posting
                    new_postings.push(posting);
                }
            }
            result.postings = new_postings;
        }

        // NOTE: Price normalization (@@→@) is NOT done here to preserve exact
        // total prices for precise residual calculation. Call `normalize_prices()`
        // on the transaction after validation to convert total prices to per-unit.

        Ok(BookedTransaction {
            transaction: result,
            gains,
            booked_indices: booked_indices.into_iter().collect(),
        })
    }

    /// Apply a transaction to the inventories (update balances).
    pub fn apply(&mut self, txn: &Transaction) {
        for posting in &txn.postings {
            if let Some(IncompleteAmount::Complete(units)) = &posting.units {
                // Resolve the per-account booking method before mutably
                // borrowing the inventories map.
                let method = self.method_for(&posting.account);
                let inv = self.inventories.entry(posting.account.clone()).or_default();

                // Determine if this is a reduction: units reduce inventory when
                // signs differ for the same currency. Only cost-bearing positions
                // are considered, so simple (no-cost) positions don't trigger
                // false reduction detection.
                //
                // NONE booking (issue #1182) treats every posting as an
                // augmentation. Mixed-sign positions accumulate in the
                // inventory — Python beancount's semantic for the NONE
                // method, which the parallel guard in `book()` mirrors.
                let is_reduction = method != BookingMethod::None
                    && posting.cost.is_some()
                    && inv.is_reduced_by(units, ReductionScope::CostBearingOnly);

                if is_reduction {
                    // Reduce from inventory
                    let _ = inv.reduce(units, posting.cost.as_ref(), method);
                } else {
                    // Add to inventory
                    let position = if let Some(cost_spec) = &posting.cost {
                        // Resolve per-unit cost: PerUnit/PerUnitFromTotal
                        // carry it directly; Total needs the
                        // total/|units| division.
                        let per_unit_cost = match cost_spec.number {
                            Some(rustledger_core::CostNumber::PerUnit { value: per }) => Some(per),
                            Some(rustledger_core::CostNumber::PerUnitFromTotal(b)) => {
                                Some(b.per_unit)
                            }
                            Some(rustledger_core::CostNumber::Total { value: total })
                                if !units.number.is_zero() =>
                            {
                                Some(total / units.number.abs())
                            }
                            Some(rustledger_core::CostNumber::Total { value: _ }) | None => None,
                        };

                        // Infer cost currency from price annotation or other postings.
                        // `kind` doesn't affect the currency — see the parallel
                        // block above for the same pattern.
                        let cost_currency = cost_spec.currency.clone().or_else(|| {
                            posting
                                .price
                                .as_ref()
                                .and_then(|p| p.amount.as_ref())
                                .and_then(|inc| inc.currency().map(Into::into))
                                .or_else(|| crate::infer_cost_currency_from_postings(txn))
                        });

                        if let (Some(per_unit), Some(currency)) = (per_unit_cost, cost_currency) {
                            Position::with_cost(
                                units.clone(),
                                Cost::new(per_unit, currency)
                                    .with_date_opt(cost_spec.date.or(Some(txn.date)))
                                    .with_label_opt(cost_spec.label.clone()),
                            )
                        } else {
                            Position::simple(units.clone())
                        }
                    } else {
                        Position::simple(units.clone())
                    };
                    inv.add(position);
                }
            }
        }
    }

    /// Book and interpolate a transaction.
    ///
    /// This fills in empty cost specs, then interpolates any missing amounts.
    pub fn book_and_interpolate(
        &self,
        txn: &Transaction,
    ) -> Result<InterpolationResult, BookingError> {
        // First book (fill in costs)
        let booked = self.book(txn)?;

        // Then interpolate (fill in missing amounts)
        let result = interpolate(&booked.transaction)?;

        Ok(result)
    }
}

/// Convert a core inventory `BookingError` into the booking-layer error,
/// attaching the account context that the core layer doesn't carry.
///
/// All inventory-level failures funnel into a single
/// [`BookingError::Inventory`] variant. The user-facing wording lives in the
/// `Display` impl on [`AccountedBookingError`] so it cannot drift between
/// the booking layer and the validator (#748 / #750).
fn convert_core_booking_error(
    err: rustledger_core::BookingError,
    account: &rustledger_core::Account,
) -> BookingError {
    BookingError::Inventory(err.with_account(account.clone()))
}

/// Book and interpolate a list of transactions.
///
/// This processes transactions in order, tracking inventory to enable
/// proper lot matching and capital gains calculation.
pub fn book_transactions(
    transactions: &[Transaction],
    method: BookingMethod,
) -> Vec<Result<InterpolationResult, BookingError>> {
    let mut engine = BookingEngine::with_method(method);
    let mut results = Vec::with_capacity(transactions.len());

    for txn in transactions {
        let result = engine.book_and_interpolate(txn);
        if let Ok(ref interpolated) = result {
            // Apply the booked transaction (with filled-in costs), not the original
            engine.apply(&interpolated.transaction);
        }
        results.push(result);
    }

    results
}

/// Outcome of booking an entire ledger in one shot — see [`book`].
#[derive(Debug, Clone)]
pub struct LedgerBookResult {
    /// Successfully booked directives, in the **original input order**.
    /// Every `Transaction` has its cost specs filled and elided amounts
    /// interpolated; all other directive kinds pass through unchanged.
    pub booked: Vec<Directive>,
    /// Directives whose `Transaction` failed to book, in original input
    /// order, paired with the error. They are left in their pre-booking
    /// shape so a caller can still surface the user's original input.
    pub failed: Vec<(Directive, BookingError)>,
}

/// Book and interpolate every transaction in a ledger in one shot.
///
/// This is the standalone equivalent of the loader's internal booking
/// pass. Transactions are processed in **booking order** — sorted by
/// `(date, priority, has_cost_reduction)` — so lot matching and
/// capital-gains tracking observe inventory in the correct sequence, while
/// the returned [`LedgerBookResult::booked`] / [`LedgerBookResult::failed`]
/// vectors preserve the caller's **original input order**. Non-transaction
/// directives pass through untouched. Per-account booking methods declared
/// via `Open ... "METHOD"` are honored; `method` is the fallback for
/// accounts that declare none.
///
/// Booking is a pure function of its inputs, so calling it twice on the
/// same `(directives, method)` yields equal results — this is the booking
/// half of the #1235 pipeline-boundary invariants.
#[must_use]
pub fn book(directives: &[Directive], method: BookingMethod) -> LedgerBookResult {
    let mut engine = BookingEngine::with_method(method);
    engine.register_account_methods(directives.iter());

    // Stable sort into booking order. Display order — `(date, priority,
    // file position)` — is already encoded in the input's positional order,
    // and a stable sort keeps that as the tiebreak.
    let mut order: Vec<usize> = (0..directives.len()).collect();
    order.sort_by_key(|&i| {
        let d = &directives[i];
        (d.date(), d.priority(), d.has_cost_reduction())
    });

    // Book in booking order, recording each transaction's outcome against
    // its original index so the result can be reassembled in input order.
    let mut booked_txns: Vec<Option<Transaction>> = directives.iter().map(|_| None).collect();
    let mut booking_errors: Vec<Option<BookingError>> = directives.iter().map(|_| None).collect();
    for &i in &order {
        if let Directive::Transaction(txn) = &directives[i] {
            match engine.book_and_interpolate(txn) {
                Ok(result) => {
                    // Apply the booked transaction (filled-in costs), not
                    // the original, so subsequent lot matching is correct.
                    engine.apply(&result.transaction);
                    booked_txns[i] = Some(result.transaction);
                }
                Err(e) => booking_errors[i] = Some(e),
            }
        }
    }

    // Reassemble in original input order, partitioning failures out.
    let mut booked = Vec::with_capacity(directives.len());
    let mut failed = Vec::new();
    for (i, directive) in directives.iter().enumerate() {
        if let Some(e) = booking_errors[i].take() {
            failed.push((directive.clone(), e));
        } else if let Some(txn) = booked_txns[i].take() {
            booked.push(Directive::Transaction(txn));
        } else {
            booked.push(directive.clone());
        }
    }

    LedgerBookResult { booked, failed }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rust_decimal_macros::dec;
    use rustledger_core::{NaiveDate, Posting, PriceAnnotation};

    fn date(year: i32, month: u32, day: u32) -> NaiveDate {
        rustledger_core::naive_date(year, month, day).unwrap()
    }

    #[test]
    fn test_book_simple_buy() {
        let mut engine = BookingEngine::new();

        // Buy 10 AAPL at $150
        let buy = Transaction::new(date(2024, 1, 15), "Buy stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(10), "AAPL")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::PerUnit {
                            value: dec!(150.00),
                        })
                        .with_currency("USD"),
                ),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(-1500.00), "USD"),
            ));

        engine.apply(&buy);

        // Check inventory
        let inv = engine.inventory(&"Assets:Stock".into()).unwrap();
        assert_eq!(inv.units("AAPL"), dec!(10));
    }

    #[test]
    fn test_book_sell_with_gain() {
        let mut engine = BookingEngine::new();

        // Buy 10 AAPL at $150
        let buy = Transaction::new(date(2024, 1, 15), "Buy stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(10), "AAPL")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::PerUnit {
                            value: dec!(150.00),
                        })
                        .with_currency("USD"),
                ),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(-1500.00), "USD"),
            ));

        engine.apply(&buy);

        // Sell 5 AAPL at $175 with empty cost (needs lot matching)
        let sell = Transaction::new(date(2024, 6, 15), "Sell stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(-5), "AAPL"))
                    .with_cost(CostSpec::empty()) // Empty - needs lot matching
                    .with_price(PriceAnnotation::unit(Amount::new(dec!(175.00), "USD"))),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(875.00), "USD"),
            ))
            .with_synthesized_posting(Posting::auto("Income:CapitalGains")); // Elided

        // Check inventory before sell
        let inv = engine.inventory(&"Assets:Stock".into()).unwrap();
        eprintln!("Inventory before sell: {inv:?}");

        let booked = engine.book(&sell).unwrap();
        eprintln!(
            "Booked: gains={:?}, indices={:?}",
            booked.gains, booked.booked_indices
        );
        eprintln!("Booked transaction: {:?}", booked.transaction);

        // Check that gain was calculated
        assert_eq!(
            booked.gains.len(),
            1,
            "Expected 1 gain, got {:?}",
            booked.gains
        );
        let gain = &booked.gains[0];
        // Gain = 5 * (175 - 150) = 125
        assert_eq!(gain.amount.number, dec!(125));
    }

    #[test]
    fn test_book_with_total_cost() {
        let mut engine = BookingEngine::new();

        // Buy 1.763 VIIIX with total cost of 300 USD (like healthequity file)
        let buy = Transaction::new(date(2016, 1, 16), "Buy stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(1.763), "VIIIX")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::Total {
                            value: dec!(300.00),
                        })
                        .with_currency("USD"),
                ),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(-300.00), "USD"),
            ));

        engine.apply(&buy);

        // Check inventory
        let inv = engine.inventory(&"Assets:Stock".into()).unwrap();
        eprintln!("Inventory after total cost buy: {inv:?}");
        assert_eq!(inv.units("VIIIX"), dec!(1.763));

        // Check cost was calculated correctly (300/1.763 ≈ 170.16)
        let pos = inv.positions().next().unwrap();
        assert!(pos.cost.is_some(), "Expected cost on position");
        eprintln!("Position cost: {:?}", pos.cost);
    }

    #[test]
    fn test_book_total_cost_then_sell() {
        // Test that book() correctly handles total cost syntax and preserves
        // full precision for accurate capital gains calculation.
        let mut engine = BookingEngine::new();

        // Buy 1.763 VIIIX with total cost {{300.00 USD}}
        let buy = Transaction::new(date(2016, 1, 16), "Buy stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(1.763), "VIIIX")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::Total {
                            value: dec!(300.00),
                        })
                        .with_currency("USD"),
                ),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(-300.00), "USD"),
            ));

        // Use book() to test the booking path with total cost
        let booked_buy = engine.book(&buy).unwrap();
        engine.apply(&booked_buy.transaction);

        // Check that per-unit cost was calculated (300/1.763)
        let buy_posting = &booked_buy.transaction.postings[0];
        assert!(buy_posting.cost.is_some());
        let cost_spec = buy_posting.cost.as_ref().unwrap();
        // Booking should have converted the user-written Total into
        // the post-booking PerUnitFromTotal shape — the per-unit value
        // is computed for lot tracking and the total is preserved for
        // exact residual math.
        assert!(matches!(
            cost_spec.number,
            Some(rustledger_core::CostNumber::PerUnitFromTotal(_))
        ));

        // Sell all shares at $191 per unit
        let sell = Transaction::new(date(2016, 6, 15), "Sell stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(-1.763), "VIIIX"))
                    .with_cost(CostSpec::empty())
                    .with_price(PriceAnnotation::unit(Amount::new(dec!(191.00), "USD"))),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(336.73), "USD"), // 1.763 * 191 = 336.733
            ))
            .with_synthesized_posting(Posting::auto("Income:CapitalGains"));

        let booked_sell = engine.book(&sell).unwrap();

        // Capital gain should be: 336.73 - 300.00 = 36.73
        // With full precision preserved, this should be accurate
        assert_eq!(booked_sell.gains.len(), 1);
        let gain = &booked_sell.gains[0];
        // The gain should be close to 36.73 (sale proceeds - cost basis)
        // Sale: 1.763 * 191 = 336.733, Cost: 300.00, Gain ≈ 36.73
        eprintln!("Capital gain: {:?}", gain.amount);
    }

    #[test]
    fn test_cost_spec_currency_inference() {
        let mut engine = BookingEngine::new();

        // Create SELLOPT: -1 AAPL {40.0} @ 0.4 USD
        // This has cost number (40.0) but NO cost currency - should infer from price
        let sell = Transaction::new(date(2022, 6, 17), "SELLOPT")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(-1), "AAPL"))
                    .with_cost(
                        CostSpec::empty().with_number(rustledger_core::CostNumber::PerUnit {
                            value: dec!(40.0),
                        }),
                    )
                    .with_price(PriceAnnotation::unit(Amount::new(dec!(0.4), "USD"))),
            )
            .with_synthesized_posting(Posting::new("Assets:Stock", Amount::new(dec!(40.0), "USD")));

        eprintln!("SELLOPT posting.cost = {:?}", sell.postings[0].cost);
        eprintln!("SELLOPT posting.price = {:?}", sell.postings[0].price);

        engine.apply(&sell);

        let inv = engine.inventory(&"Assets:Stock".into()).unwrap();
        eprintln!("Inventory after SELLOPT: {inv:?}");

        // Check that the AAPL position has cost with USD currency
        let aapl_pos = inv
            .positions()
            .find(|p| p.units.currency.as_ref() == "AAPL")
            .expect("Should have AAPL position");

        eprintln!("AAPL position: {aapl_pos:?}");

        assert!(aapl_pos.cost.is_some(), "AAPL position should have cost");
        let cost = aapl_pos.cost.as_ref().unwrap();
        assert_eq!(cost.currency.as_ref(), "USD", "Cost currency should be USD");
        assert_eq!(cost.number, dec!(40.0), "Cost number should be 40.0");
    }

    #[test]
    fn test_booking_engine_with_method() {
        // Test that with_method creates engine with specified booking method
        let engine = BookingEngine::with_method(BookingMethod::Lifo);
        assert!(engine.inventories.is_empty());

        // Also test default is FIFO
        let default_engine = BookingEngine::new();
        assert!(default_engine.inventories.is_empty());
    }

    #[test]
    fn test_book_sell_with_total_price() {
        let mut engine = BookingEngine::new();

        // Buy 10 AAPL at $150
        let buy = Transaction::new(date(2024, 1, 15), "Buy stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(10), "AAPL")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::PerUnit {
                            value: dec!(150.00),
                        })
                        .with_currency("USD"),
                ),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(-1500.00), "USD"),
            ));

        engine.apply(&buy);

        // Sell 5 AAPL with total price annotation (not per-unit)
        // Total price = $875 for 5 shares = $175/share
        let sell = Transaction::new(date(2024, 6, 15), "Sell stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(-5), "AAPL"))
                    .with_cost(CostSpec::empty())
                    .with_price(PriceAnnotation::total(Amount::new(dec!(875.00), "USD"))),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(875.00), "USD"),
            ))
            .with_synthesized_posting(Posting::auto("Income:CapitalGains"));

        let booked = engine.book(&sell).unwrap();

        // Check that gain was calculated correctly
        // Gain = 875 - (5 * 150) = 875 - 750 = 125
        assert_eq!(booked.gains.len(), 1, "Expected 1 gain");
        let gain = &booked.gains[0];
        assert_eq!(gain.amount.number, dec!(125));
    }

    #[test]
    fn test_book_transactions_multiple() {
        // Buy 10 AAPL at $150
        let buy = Transaction::new(date(2024, 1, 15), "Buy stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(10), "AAPL")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::PerUnit {
                            value: dec!(150.00),
                        })
                        .with_currency("USD"),
                ),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(-1500.00), "USD"),
            ));

        // Sell 5 AAPL
        let sell = Transaction::new(date(2024, 6, 15), "Sell stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(-5), "AAPL"))
                    .with_cost(CostSpec::empty())
                    .with_price(PriceAnnotation::unit(Amount::new(dec!(175.00), "USD"))),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(875.00), "USD"),
            ))
            .with_synthesized_posting(Posting::auto("Income:CapitalGains"));

        let transactions = vec![buy, sell];
        let results = book_transactions(&transactions, BookingMethod::Fifo);

        assert_eq!(results.len(), 2);
        assert!(results[0].is_ok());
        assert!(results[1].is_ok());
    }

    #[test]
    fn test_book_augmentation_not_reduction() {
        let mut engine = BookingEngine::new();

        // First, add existing inventory with positive AAPL
        let buy = Transaction::new(date(2024, 1, 15), "Buy stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(10), "AAPL")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::PerUnit {
                            value: dec!(150.00),
                        })
                        .with_currency("USD"),
                ),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(-1500.00), "USD"),
            ));

        engine.apply(&buy);

        // Now try to book another buy (augmentation, not reduction)
        // This has empty cost but same sign as inventory, so it's not a reduction
        let another_buy = Transaction::new(date(2024, 2, 15), "Buy more")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(5), "AAPL"))
                    .with_cost(CostSpec::empty()), // Empty cost but augmentation
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(-750.00), "USD"),
            ));

        // Should not error - just skip lot matching for augmentation
        let booked = engine.book(&another_buy).unwrap();
        assert!(
            booked.booked_indices.is_empty(),
            "Augmentation should not have booked indices"
        );
    }

    #[test]
    fn test_book_no_inventory_for_account() {
        let engine = BookingEngine::new();

        // Try to book a sell without any prior inventory
        let sell = Transaction::new(date(2024, 6, 15), "Sell stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(-5), "AAPL"))
                    .with_cost(CostSpec::empty()),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(875.00), "USD"),
            ));

        // Should succeed but with no booked indices (no inventory to match against)
        let booked = engine.book(&sell).unwrap();
        assert!(
            booked.booked_indices.is_empty(),
            "No inventory means no lot matching"
        );
    }

    #[test]
    fn test_book_zero_gain() {
        let mut engine = BookingEngine::new();

        // Buy 10 AAPL at $150
        let buy = Transaction::new(date(2024, 1, 15), "Buy stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(10), "AAPL")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::PerUnit {
                            value: dec!(150.00),
                        })
                        .with_currency("USD"),
                ),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(-1500.00), "USD"),
            ));

        engine.apply(&buy);

        // Sell at same price - zero gain
        let sell = Transaction::new(date(2024, 6, 15), "Sell stock")
            .with_synthesized_posting(
                Posting::new("Assets:Stock", Amount::new(dec!(-5), "AAPL"))
                    .with_cost(CostSpec::empty())
                    .with_price(PriceAnnotation::unit(Amount::new(dec!(150.00), "USD"))),
            )
            .with_synthesized_posting(Posting::new(
                "Assets:Cash",
                Amount::new(dec!(750.00), "USD"),
            ));

        let booked = engine.book(&sell).unwrap();

        // Zero gain should not be added to gains vector
        assert!(booked.gains.is_empty(), "Zero gain should not be recorded");
    }

    /// Test cost currency inference from other postings (issue #230).
    ///
    /// When a cost is specified without a currency (e.g., `{1}`), the currency
    /// should be inferred from simple postings in the same transaction.
    #[test]
    fn test_cost_currency_inference_from_other_postings() {
        let mut engine = BookingEngine::new();

        // Opening balance with cost without currency - should infer USD from other posting
        // 2026-01-01 * "Opening balance"
        //   Assets:Abc   1 ABC {1}           <- no currency, should infer USD
        //   Equity:Opening-Balances -1 USD
        let open = Transaction::new(date(2026, 1, 1), "Opening balance")
            .with_synthesized_posting(
                Posting::new("Assets:Abc", Amount::new(dec!(1), "ABC")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::PerUnit { value: dec!(1) }),
                ), // No currency!
            )
            .with_synthesized_posting(Posting::new(
                "Equity:Opening-Balances",
                Amount::new(dec!(-1), "USD"),
            ));

        // Book and apply the opening
        let booked = engine.book(&open).unwrap();
        engine.apply(&booked.transaction);

        // Check that the cost spec was filled in with USD
        let cost_spec = booked.transaction.postings[0].cost.as_ref().unwrap();
        assert_eq!(
            cost_spec.currency.as_deref(),
            Some("USD"),
            "Cost currency should be inferred as USD from other posting"
        );

        // Check inventory has the position with correct cost
        let inv = engine.inventory(&"Assets:Abc".into()).unwrap();
        let pos = inv.positions().next().unwrap();
        assert!(pos.cost.is_some(), "Position should have cost");
        let cost = pos.cost.as_ref().unwrap();
        assert_eq!(cost.currency.as_ref(), "USD", "Cost currency should be USD");
        assert_eq!(cost.number, dec!(1), "Cost number should be 1");

        // Now sell with explicit cost currency - should match the lot
        // 2026-01-02 * "Sale"
        //   Assets:Abc  -1 ABC {1 USD}
        //   Expenses:Abc
        let sell = Transaction::new(date(2026, 1, 2), "Sale")
            .with_synthesized_posting(
                Posting::new("Assets:Abc", Amount::new(dec!(-1), "ABC")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::PerUnit { value: dec!(1) })
                        .with_currency("USD"),
                ),
            )
            .with_synthesized_posting(Posting::auto("Expenses:Abc"));

        // This should succeed - the lot with {1 USD} should be found
        let booked_sell = engine.book(&sell).unwrap();

        // Check that the lot was matched
        assert!(
            !booked_sell.booked_indices.is_empty(),
            "Sale should match the lot created in opening"
        );
    }

    #[test]
    fn test_multi_posting_crosses_lot_boundary() {
        // Regression test: Multiple postings in the same transaction reducing
        // the same commodity should correctly track inventory state across postings.
        // Previously, each posting would see the original inventory instead of
        // the updated state after processing previous postings.

        let mut engine = BookingEngine::new();

        // Create two lots of ADA with different costs
        // Lot 1: 100 ADA at $0.50 (2021-01-01)
        let buy1 = Transaction::new(date(2021, 1, 1), "Buy lot 1")
            .with_synthesized_posting(
                Posting::new("Assets:Crypto", Amount::new(dec!(100), "ADA")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::PerUnit { value: dec!(0.50) })
                        .with_currency("USD")
                        .with_date(date(2021, 1, 1)),
                ),
            )
            .with_synthesized_posting(Posting::new("Assets:Cash", Amount::new(dec!(-50), "USD")));
        engine.apply(&buy1);

        // Lot 2: 100 ADA at $0.52 (2022-05-19)
        let buy2 = Transaction::new(date(2022, 5, 19), "Buy lot 2")
            .with_synthesized_posting(
                Posting::new("Assets:Crypto", Amount::new(dec!(100), "ADA")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::PerUnit { value: dec!(0.52) })
                        .with_currency("USD")
                        .with_date(date(2022, 5, 19)),
                ),
            )
            .with_synthesized_posting(Posting::new("Assets:Cash", Amount::new(dec!(-52), "USD")));
        engine.apply(&buy2);

        // Verify initial inventory: 200 ADA total
        let inv = engine.inventory(&"Assets:Crypto".into()).unwrap();
        assert_eq!(inv.units("ADA"), dec!(200));

        // Consume half of lot 1 first
        let sell1 = Transaction::new(date(2022, 5, 20), "Sell 50 ADA")
            .with_synthesized_posting(
                Posting::new("Assets:Crypto", Amount::new(dec!(-50), "ADA"))
                    .with_cost(CostSpec::empty()),
            )
            .with_synthesized_posting(Posting::new("Assets:Cash", Amount::new(dec!(25), "USD")));
        let booked1 = engine.book(&sell1).unwrap();
        engine.apply(&booked1.transaction);

        // Verify: 150 ADA remaining (50 in lot 1, 100 in lot 2)
        let inv = engine.inventory(&"Assets:Crypto".into()).unwrap();
        assert_eq!(inv.units("ADA"), dec!(150));

        // Now the critical test: TWO postings in the same transaction
        // that together cross the lot boundary.
        // - Posting 1: -75 ADA {} → takes 50 from lot 1 + 25 from lot 2
        // - Posting 2: -5 ADA {} → should take from lot 2 (continuing)
        let sell2 = Transaction::new(date(2022, 5, 22), "Sell 80 ADA (multi-posting)")
            .with_synthesized_posting(
                Posting::new("Assets:Crypto", Amount::new(dec!(-75), "ADA"))
                    .with_cost(CostSpec::empty()),
            )
            .with_synthesized_posting(
                Posting::new("Assets:Crypto", Amount::new(dec!(-5), "ADA"))
                    .with_cost(CostSpec::empty()),
            )
            .with_synthesized_posting(Posting::new("Assets:Cash", Amount::new(dec!(42), "USD")));

        // This should succeed - the bug was that the second posting would fail
        // with "No matching lot" because it was trying to match against lot 1
        // which was already exhausted by the first posting.
        let booked2 = engine.book(&sell2);
        assert!(
            booked2.is_ok(),
            "Multi-posting transaction should succeed: {:?}",
            booked2.err()
        );

        // Apply and verify final inventory: 70 ADA remaining (all in lot 2)
        engine.apply(&booked2.unwrap().transaction);
        let inv = engine.inventory(&"Assets:Crypto".into()).unwrap();
        assert_eq!(
            inv.units("ADA"),
            dec!(70),
            "Should have 70 ADA remaining in lot 2"
        );
    }

    #[test]
    fn test_book_no_cost_specs_fast_path() {
        // Test that the fast path for transactions without cost specs
        // returns correct empty gains and booked_indices.
        let engine = BookingEngine::new();

        // Simple expense transaction with no cost specs
        let txn = Transaction::new(date(2024, 1, 15), "Groceries")
            .with_synthesized_posting(Posting::new("Expenses:Food", Amount::new(dec!(50), "USD")))
            .with_synthesized_posting(Posting::new("Assets:Cash", Amount::new(dec!(-50), "USD")));

        let result = engine.book(&txn).unwrap();

        // Fast path should return empty gains and booked_indices
        assert!(result.gains.is_empty(), "Should have no capital gains");
        assert!(
            result.booked_indices.is_empty(),
            "Should have no booked indices"
        );

        // Transaction should be unchanged
        assert_eq!(result.transaction.postings.len(), 2);
        assert_eq!(
            result.transaction.postings[0].units,
            Some(IncompleteAmount::Complete(Amount::new(dec!(50), "USD")))
        );
    }

    /// Regression test for #748.
    ///
    /// The pta-standards `reduction-exceeds-inventory` conformance test
    /// asserts on `error_contains: ["not enough"]`. PR #745 made the booking
    /// layer propagate `InsufficientUnits` directly to the user instead of
    /// letting the validator's "Not enough units in ..." message win, which
    /// dropped the "not enough" phrasing. This test pins the user-facing
    /// Display string so the conformance assertion (and any downstream user
    /// tooling that greps the message) cannot regress silently again.
    ///
    /// After #750, the canonical Display lives on
    /// [`rustledger_core::AccountedBookingError`] and `BookingError::Inventory`
    /// delegates to it transparently — so this test exercises the same path
    /// the validator and `cmd/check.rs` use.

    // =========================================================================
    // Regression test for issue #875 / beancount#889
    //
    // Scenario: buy stock with cost, sell without cost spec (leaves a simple
    // negative position), then buy more with cost spec. The third transaction
    // must succeed as an augmentation, not fail as a reduction.
    // =========================================================================

    #[test]
    fn test_augmentation_after_sell_without_cost_spec() {
        // Regression test for issue #875 / beancount#889.
        //
        // Before the fix, the sell-without-cost-spec left a -25 HOOG simple
        // position, causing the subsequent buy-with-cost-spec to be
        // misclassified as a reduction (because is_reduced_by saw opposite
        // signs without distinguishing cost-bearing vs simple positions).
        let mut engine = BookingEngine::new();

        // 2024-01-10: Buy 100 HOOG {1.50 EUR}
        let buy1 = Transaction::new(date(2024, 1, 10), "Buy 100 HOOG")
            .with_synthesized_posting(
                Posting::new("Assets:Stocks", Amount::new(dec!(100), "HOOG")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::PerUnit { value: dec!(1.50) })
                        .with_currency("EUR"),
                ),
            )
            .with_synthesized_posting(Posting::new("Assets:Bank", Amount::new(dec!(-150), "EUR")));

        engine.apply(&buy1);

        // 2024-01-15: Sell 25 HOOG without cost spec (price-only)
        let sell = Transaction::new(date(2024, 1, 15), "Sell 25 HOOG without cost spec")
            .with_synthesized_posting(
                Posting::new("Assets:Stocks", Amount::new(dec!(-25), "HOOG"))
                    .with_price(PriceAnnotation::unit(Amount::new(dec!(1.60), "EUR"))),
            )
            .with_synthesized_posting(Posting::new("Assets:Bank", Amount::new(dec!(40), "EUR")));

        engine.apply(&sell);

        // 2024-01-20: Buy 50 more HOOG {1.70 EUR} - this MUST succeed
        let buy2 = Transaction::new(date(2024, 1, 20), "Buy 50 more HOOG - should succeed")
            .with_synthesized_posting(
                Posting::new("Assets:Stocks", Amount::new(dec!(50), "HOOG")).with_cost(
                    CostSpec::empty()
                        .with_number(rustledger_core::CostNumber::PerUnit { value: dec!(1.70) })
                        .with_currency("EUR"),
                ),
            )
            .with_synthesized_posting(Posting::new("Assets:Bank", Amount::new(dec!(-85), "EUR")));

        // This should NOT fail. Before the fix, the engine would see the
        // -25 HOOG simple position and try to reduce, which would fail
        // because the cost spec wouldn't match any existing lot.
        let result = engine.book(&buy2);
        assert!(
            result.is_ok(),
            "Buy with cost spec after sell without cost spec should succeed as augmentation, \
             but got error: {:?}",
            result.err()
        );

        let booked = result.unwrap();
        engine.apply(&booked.transaction);

        // Verify final inventory state
        let inv = engine.inventory(&"Assets:Stocks".into()).unwrap();
        // 100 (original) - 25 (sold simple) + 50 (new lot) = 125 HOOG total
        assert_eq!(inv.units("HOOG"), dec!(125));
    }

    #[test]
    fn test_insufficient_units_display_contains_not_enough() {
        let err = BookingError::Inventory(
            rustledger_core::BookingError::InsufficientUnits {
                currency: "AAPL".into(),
                requested: dec!(15),
                available: dec!(10),
            }
            .with_account("Assets:Stock".into()),
        );
        let rendered = format!("{err}");
        assert!(
            rendered.contains("not enough"),
            "InsufficientUnits Display must contain 'not enough' for beancount \
             compatibility (#748). Got: {rendered}"
        );
        assert!(
            rendered.contains("Assets:Stock"),
            "InsufficientUnits Display must include the account name. Got: {rendered}"
        );
        assert!(
            rendered.contains("15") && rendered.contains("10"),
            "InsufficientUnits Display must include requested and available amounts. Got: {rendered}"
        );
    }

    /// Helper: does any posting still have an unfilled (elided) amount?
    fn has_elided_posting(txn: &Transaction) -> bool {
        txn.postings.iter().any(|p| p.units.is_none())
    }

    #[test]
    fn book_interpolates_elided_posting_and_preserves_order() {
        use rustledger_core::Open;

        let directives = vec![
            Directive::Open(Open::new(date(2024, 1, 1), "Assets:Cash")),
            Directive::Open(Open::new(date(2024, 1, 1), "Expenses:Food")),
            Directive::Transaction(
                Transaction::new(date(2024, 1, 15), "Lunch")
                    .with_synthesized_posting(Posting::new(
                        "Expenses:Food",
                        Amount::new(dec!(50.00), "USD"),
                    ))
                    .with_synthesized_posting(Posting::auto("Assets:Cash")),
            ),
        ];

        let result = book(&directives, BookingMethod::Strict);
        assert!(result.failed.is_empty(), "nothing should fail to book");
        assert_eq!(result.booked.len(), 3, "all directives preserved");

        // Order preserved: the two Opens come first, unchanged.
        assert_eq!(result.booked[0], directives[0]);
        assert_eq!(result.booked[1], directives[1]);

        // The transaction's elided posting got filled in.
        let Directive::Transaction(booked_txn) = &result.booked[2] else {
            panic!("third directive should still be a transaction");
        };
        assert!(
            !has_elided_posting(booked_txn),
            "the auto posting should have been interpolated"
        );
    }

    #[test]
    fn book_is_deterministic() {
        use rustledger_core::Open;

        let directives = vec![
            Directive::Open(Open::new(date(2024, 1, 1), "Assets:Stock")),
            Directive::Open(Open::new(date(2024, 1, 1), "Assets:Cash")),
            Directive::Transaction(
                Transaction::new(date(2024, 1, 15), "Buy")
                    .with_synthesized_posting(
                        Posting::new("Assets:Stock", Amount::new(dec!(10), "AAPL"))
                            .with_price(PriceAnnotation::unit(Amount::new(dec!(150.00), "USD"))),
                    )
                    .with_synthesized_posting(Posting::auto("Assets:Cash")),
            ),
        ];

        let first = book(&directives, BookingMethod::Strict);
        let second = book(&directives, BookingMethod::Strict);
        assert_eq!(
            first.booked, second.booked,
            "booking the same ledger twice must produce identical output"
        );
    }

    #[test]
    fn book_partitions_failed_transaction() {
        use rustledger_core::Open;

        // Buy a lot at $150, then sell against a cost basis ($200) that
        // matches no existing lot. Under Strict this is a no-matching-lot
        // error, so the sell is partitioned into `failed`.
        let buy_cost = CostSpec::empty()
            .with_number(rustledger_core::CostNumber::PerUnit {
                value: dec!(150.00),
            })
            .with_currency("USD");
        let sell_cost = CostSpec::empty()
            .with_number(rustledger_core::CostNumber::PerUnit {
                value: dec!(200.00),
            })
            .with_currency("USD");

        let directives = vec![
            Directive::Open(Open::new(date(2024, 1, 1), "Assets:Stock")),
            Directive::Open(Open::new(date(2024, 1, 1), "Assets:Cash")),
            Directive::Transaction(
                Transaction::new(date(2024, 1, 10), "Buy")
                    .with_synthesized_posting(
                        Posting::new("Assets:Stock", Amount::new(dec!(10), "AAPL"))
                            .with_cost(buy_cost),
                    )
                    .with_synthesized_posting(Posting::new(
                        "Assets:Cash",
                        Amount::new(dec!(-1500.00), "USD"),
                    )),
            ),
            Directive::Transaction(
                Transaction::new(date(2024, 1, 15), "Sell at phantom cost basis")
                    .with_synthesized_posting(
                        Posting::new("Assets:Stock", Amount::new(dec!(-5), "AAPL"))
                            .with_cost(sell_cost),
                    )
                    .with_synthesized_posting(Posting::new(
                        "Assets:Cash",
                        Amount::new(dec!(1000.00), "USD"),
                    )),
            ),
        ];

        let result = book(&directives, BookingMethod::Strict);
        assert_eq!(result.failed.len(), 1, "the mismatched sell should fail");
        // The two Opens and the successful buy survive; the sell is dropped.
        assert_eq!(result.booked.len(), 3, "Opens + buy remain in booked");
        assert!(
            !result.booked.iter().any(|d| matches!(
                d,
                Directive::Transaction(t) if t.narration.as_ref() == "Sell at phantom cost basis"
            )),
            "failed sell must not appear in booked"
        );
    }
}