Read broker amounts at the precision the export actually uses
A real Scalable export reinvests a distribution as -29,579989728, and the import refused the whole file: "broker record 14 has an invalid amount, require signed 64-bit value with at most eight fractional digits". An amount is the row's share count times its price, so it carries as many decimal places as the two columns together need - nine here, from six places of shares and three of price - and scalableMoney was computing its discarded remainder by parsing the cell through the share-count parser, whose ceiling is eight. The leaked wording "invalid quantity" for an amount cell was the tell. Money cells are now read at arbitrary precision, rounded to money's four places half away from zero, and the residue is accumulated exactly and reported as a trimmed decimal. ScalableImport.Rounding stops being a domain.Quantity, which carried the same eight-place ceiling, and becomes the exact decimal string it always claimed to be; the JSON shape and the review copy are unchanged. Share counts and prices are still refused beyond their own precision instead of rounded. Rounding a share count misstates a holding, and rounding a price would break the shares-times-price identity that every security row's amount is checked against. The reported row is now covered end to end: both legs of that distribution, the single reference the broker reuses across them surviving deduplication, the exact 0.000010272 residue, and the dividend paid in cancelling to the cent against the units bought with it.
This commit is contained in:
@@ -228,6 +228,56 @@ func TestSingleShareRowCatchesOnlyInconsistentArithmetic(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A reinvested distribution settles shares times price, so it carries as many
|
||||
// decimal places as the two together need. A real export reinvests to nine,
|
||||
// which is past what money holds and past what a share count holds, so reading
|
||||
// the cell at either precision rejects the row outright. It is rounded to
|
||||
// money's four and the discarded remainder is reported exactly.
|
||||
func TestReinvestmentKeepsNineDecimalPlacesOutOfTheBalance(t *testing.T) {
|
||||
const reference = "617007_rrCjP4EcbpefpNiVQeD495"
|
||||
result := readBroker(t,
|
||||
`2026-05-28;02:00:00;Executed;`+reference+`;iShares Global Clean Energy Transition (Dist);Cash;Distribution;IE00B1XNHC34;;;29,58;0,00;8,56;EUR`,
|
||||
`2026-05-28;02:00:00;Executed;`+reference+`;iShares Global Clean Energy Transition (Dist);Security;Reinvestment_Distribution;IE00B1XNHC34;3,144131;9,408;-29,579984448;0,00;0,00;EUR`,
|
||||
)
|
||||
if len(result.Facts) != 2 {
|
||||
t.Fatalf("read %d of 2 legs", len(result.Facts))
|
||||
}
|
||||
cash, reinvest := result.Facts[0], result.Facts[1]
|
||||
if cash.Amount != "29.58" || cash.Investment.Tax != "8.56" {
|
||||
t.Errorf("distribution settled %s with tax %s, want 29.58 and 8.56 recorded", cash.Amount, cash.Investment.Tax)
|
||||
}
|
||||
// 29.579984448 rounds up at the fifth place, and the residue is exact.
|
||||
if reinvest.Amount != "-29.58" || reinvest.Investment.Gross != "-29.58" {
|
||||
t.Errorf("reinvestment settled %s against gross %s, want -29.58 for both", reinvest.Amount, reinvest.Investment.Gross)
|
||||
}
|
||||
if reinvest.Investment.Quantity != "3.144131" {
|
||||
t.Errorf("reinvested %s shares, want 3.144131", reinvest.Investment.Quantity)
|
||||
}
|
||||
if result.Rounded != 1 || result.Rounding != "0.000015552" {
|
||||
t.Errorf("rounding reported as %d row(s) and %s, want 1 and 0.000015552", result.Rounded, result.Rounding)
|
||||
}
|
||||
// The dividend paid in and the units bought with it cancel to the cent.
|
||||
total := int64(0)
|
||||
for _, f := range result.Facts {
|
||||
minor, err := f.Amount.Minor()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
total += minor
|
||||
}
|
||||
if total != 0 {
|
||||
t.Errorf("the pair moved %s of net cash, want none", domain.FormatMoney(total))
|
||||
}
|
||||
// Both legs carry one reference byte for byte and must both survive.
|
||||
data := domain.NewDataset()
|
||||
data.Accounts = []domain.Account{brokerAccount()}
|
||||
data.Instruments = result.Instruments
|
||||
added, err := NormalizeAndDedupe(data, result.Facts)
|
||||
if err != nil || len(added) != 2 {
|
||||
t.Fatalf("dedupe kept %d of 2 legs sharing a reference: %v", len(added), err)
|
||||
}
|
||||
}
|
||||
|
||||
// A thousands dot and a decimal dot are both present in one share column.
|
||||
func TestBrokerShareColumnDistinguishesGroupingFromDecimals(t *testing.T) {
|
||||
result := readBroker(t,
|
||||
|
||||
Reference in New Issue
Block a user