blob: 5a4259b1ec3507f6dbf097b1211f9389b28c289b (
plain)
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
|
namespace FundLab.Domain
open System
module DividendPolicy =
let cashMaximum = 999999999999999999.99m
let unitNavMaximum = 99999999999999999999.99999999m
type DividendMode =
| Cash
| Reinvest
let modeText (mode: DividendMode) : string =
match mode with
| Cash -> "cash"
| Reinvest -> "reinvest"
/// Deterministic provenance key of a booked scheme. Shared by the writer (idempotency
/// identity) and the read model (历史列表的“来源 schema key”), so both stay in lockstep.
let schemeKey (fundId: Guid) (instrumentCode: string) (navDate: DateOnly) (mode: DividendMode) : string =
sprintf "dividend-scheme:%O:%s:%s:%s" fundId instrumentCode (navDate.ToString("yyyy-MM-dd")) (modeText mode)
let parseMode (text: string) : DividendMode option =
match text with
| null -> None
| "cash" -> Some Cash
| "reinvest" -> Some Reinvest
| _ -> None
/// Distribution per unit: positive, eight-decimal precision (same scale as unit NAV).
let validateDps (dps: decimal) : Result<unit, string> =
if dps <= 0m then
Error "dividend per unit must be positive"
elif Decimal.Round(dps, 8) <> dps then
Error "dividend per unit exceeds supported precision"
elif dps > unitNavMaximum then
Error "dividend per unit exceeds database precision"
else
Ok ()
/// No look-ahead: a dividend dated in the future cannot be booked or confirmed today.
let validateNavDate (dividendNavDate: DateOnly) (today: DateOnly) : Result<unit, string> =
if dividendNavDate > today then
Error "dividend nav date is in the future"
else
Ok ()
type CashDividendOutcome =
{
GrossCash: decimal
}
type ReinvestDividendOutcome =
{
GrossCash: decimal
RequestedUnits: decimal
NAV: decimal
}
/// Cash dividend credits truncate(units * dps, 2) to available cash; units and cost
/// are untouched (already-realized income returns to the fund, exactly one credit).
let computeCashPayout (heldUnits: decimal) (dps: decimal) : Result<CashDividendOutcome, string> =
if heldUnits <= 0m then
Error "held units must be positive for a cash dividend"
elif dps <= 0m then
Error "dividend per unit must be positive"
else
let gross = Decimal.Truncate(heldUnits * dps * 100m) / 100m
if gross <= 0m then
Error "dividend proceeds round to zero"
else
Ok { GrossCash = gross }
/// Reinvestment prices the distribution at the confirmation-date NAV (the ex-date NAV)
/// and reuses the subscription math: units = truncate8(amount / NAV), with the booked
/// invested cash = truncate2(units * NAV) — identical to a manual order with fee 0.
let computeReinvest (heldUnits: decimal) (dps: decimal) (unitNav: decimal) : Result<ReinvestDividendOutcome, string> =
if heldUnits <= 0m then
Error "held units must be positive for a reinvestment"
elif dps <= 0m then
Error "dividend per unit must be positive"
elif unitNav <= 0m then
Error "unit nav must be positive"
elif unitNav < 0.00000001m then
Error "unit nav is below database precision"
else
let gross = Decimal.Truncate(heldUnits * dps * 100m) / 100m
if gross <= 0m then
Error "dividend proceeds round to zero"
else
let requestedUnits = Decimal.Truncate(gross / unitNav * 100000000m) / 100000000m
if requestedUnits <= 0m then
Error "reinvestment units round to zero"
else
let invested = Decimal.Truncate(requestedUnits * unitNav * 100m) / 100m
if invested <= 0m then
Error "reinvestment invested cash rounds to zero"
else
Ok
{
GrossCash = gross
RequestedUnits = requestedUnits
NAV = unitNav
}
/// Deterministic keys: one record per scheme; replays with the same key never credit
/// cash or create duplicate reinvestment orders.
let recordKey (recordId: Guid) : string = sprintf "dividend:%O" recordId
let confirmKey (recordId: Guid) : string = sprintf "dividend-confirm:%O" recordId
|