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
//! Expected Credit Loss (ECL) models — IFRS 9 / ASC 326.
//!
//! This module provides data structures for the simplified approach ECL model
//! applied to trade receivables via a provision matrix based on AR aging.
//!
//! Key IFRS 9 / ASC 326 concepts modelled:
//! - Simplified approach (trade receivables): lifetime ECL at all times
//! - Provision matrix: historical loss rates by aging bucket + forward-looking adjustment
//! - ECL = Exposure × PD × LGD (Stage 1/2/3 for completeness)
//! - Provision movement: opening → new originations → stage transfers → write-offs → closing
use chrono::NaiveDate;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use crate::models::subledger::ar::AgingBucket;
// ============================================================================
// Enums
// ============================================================================
/// ECL measurement approach.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum EclApproach {
/// Simplified approach — lifetime ECL at all times (used for trade receivables).
Simplified,
/// General approach — 3-stage model based on credit deterioration.
General,
}
/// IFRS 9 / ASC 326 stage classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum EclStage {
/// Stage 1 — performing; 12-month ECL.
Stage1Month12,
/// Stage 2 — significant credit deterioration; lifetime ECL.
Stage2Lifetime,
/// Stage 3 — credit-impaired; lifetime ECL, interest on net carrying amount.
Stage3CreditImpaired,
}
// ============================================================================
// Core model structs
// ============================================================================
/// Top-level ECL model for one entity / measurement date.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EclModel {
/// Unique model ID.
pub id: String,
/// Entity (company) code.
pub entity_code: String,
/// Measurement approach.
pub approach: EclApproach,
/// Measurement date (balance-sheet date).
pub measurement_date: NaiveDate,
/// Accounting framework ("IFRS_9" or "ASC_326").
pub framework: String,
/// Portfolio segments within this model.
pub portfolio_segments: Vec<EclPortfolioSegment>,
/// Provision matrix (simplified approach only).
pub provision_matrix: Option<ProvisionMatrix>,
/// Total ECL across all segments.
#[serde(with = "crate::serde_decimal")]
pub total_ecl: Decimal,
/// Total gross exposure.
#[serde(with = "crate::serde_decimal")]
pub total_exposure: Decimal,
}
/// A portfolio segment within the ECL model (e.g. "Trade Receivables", "Intercompany").
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EclPortfolioSegment {
/// Segment name.
pub segment_name: String,
/// Gross exposure at the measurement date.
#[serde(with = "crate::serde_decimal")]
pub exposure_at_default: Decimal,
/// Stage allocations within this segment.
pub staging: Vec<EclStageAllocation>,
/// Total ECL for this segment (sum of stage ECLs).
#[serde(with = "crate::serde_decimal")]
pub total_ecl: Decimal,
}
/// ECL split by IFRS 9 stage.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EclStageAllocation {
/// IFRS 9 / ASC 326 stage.
pub stage: EclStage,
/// Gross exposure in this stage.
#[serde(with = "crate::serde_decimal")]
pub exposure: Decimal,
/// Probability of default (0–1).
#[serde(with = "crate::serde_decimal")]
pub probability_of_default: Decimal,
/// Loss given default (0–1).
#[serde(with = "crate::serde_decimal")]
pub loss_given_default: Decimal,
/// Computed ECL = exposure × PD × LGD × forward_looking_adjustment.
#[serde(with = "crate::serde_decimal")]
pub ecl_amount: Decimal,
/// Forward-looking multiplier applied to historical rate (1.0 = no adjustment).
#[serde(with = "crate::serde_decimal")]
pub forward_looking_adjustment: Decimal,
}
// ============================================================================
// Provision matrix
// ============================================================================
/// Provision matrix for the simplified approach — one row per aging bucket.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProvisionMatrix {
/// Entity code.
pub entity_code: String,
/// Measurement date.
pub measurement_date: NaiveDate,
/// Scenario weights used for forward-looking adjustment.
pub scenario_weights: ScenarioWeights,
/// One row per AR aging bucket.
pub aging_buckets: Vec<ProvisionMatrixRow>,
/// Sum of all provisions across all buckets.
#[serde(with = "crate::serde_decimal")]
pub total_provision: Decimal,
/// Sum of all exposures across all buckets.
#[serde(with = "crate::serde_decimal")]
pub total_exposure: Decimal,
/// Blended loss rate = total_provision / total_exposure.
#[serde(with = "crate::serde_decimal")]
pub blended_loss_rate: Decimal,
}
/// Scenario weights for forward-looking macro adjustment.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScenarioWeights {
/// Weight for base scenario.
#[serde(with = "crate::serde_decimal")]
pub base: Decimal,
/// Multiplier applied to historical rates under base scenario (typically 1.0).
#[serde(with = "crate::serde_decimal")]
pub base_multiplier: Decimal,
/// Weight for optimistic scenario.
#[serde(with = "crate::serde_decimal")]
pub optimistic: Decimal,
/// Multiplier applied to historical rates under optimistic scenario (< 1.0).
#[serde(with = "crate::serde_decimal")]
pub optimistic_multiplier: Decimal,
/// Weight for pessimistic scenario.
#[serde(with = "crate::serde_decimal")]
pub pessimistic: Decimal,
/// Multiplier applied to historical rates under pessimistic scenario (> 1.0).
#[serde(with = "crate::serde_decimal")]
pub pessimistic_multiplier: Decimal,
/// Resulting blended forward-looking multiplier
/// = base*base_m + optimistic*opt_m + pessimistic*pes_m.
#[serde(with = "crate::serde_decimal")]
pub blended_multiplier: Decimal,
}
/// One row of the provision matrix, corresponding to an AR aging bucket.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProvisionMatrixRow {
/// Aging bucket this row covers.
pub bucket: AgingBucket,
/// Historical loss rate for this bucket (e.g. 0.005 = 0.5%).
#[serde(with = "crate::serde_decimal")]
pub historical_loss_rate: Decimal,
/// Forward-looking adjustment multiplier (scenario-weighted).
#[serde(with = "crate::serde_decimal")]
pub forward_looking_adjustment: Decimal,
/// Applied loss rate = historical_loss_rate × forward_looking_adjustment.
#[serde(with = "crate::serde_decimal")]
pub applied_loss_rate: Decimal,
/// Gross exposure in this bucket.
#[serde(with = "crate::serde_decimal")]
pub exposure: Decimal,
/// Provision = exposure × applied_loss_rate.
#[serde(with = "crate::serde_decimal")]
pub provision: Decimal,
}
// ============================================================================
// Provision movement
// ============================================================================
/// Provision movement (roll-forward) for one fiscal period.
///
/// Reconciles the opening and closing allowance for doubtful accounts.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EclProvisionMovement {
/// Unique movement record ID.
pub id: String,
/// Entity code.
pub entity_code: String,
/// Fiscal period label (e.g. "2024-Q1", "2024-12").
pub period: String,
/// Opening allowance balance.
#[serde(with = "crate::serde_decimal")]
pub opening: Decimal,
/// New originations charged to P&L (increase in allowance).
#[serde(with = "crate::serde_decimal")]
pub new_originations: Decimal,
/// Stage-transfer adjustments (positive = provision increase).
#[serde(with = "crate::serde_decimal")]
pub stage_transfers: Decimal,
/// Write-offs charged against the allowance (reduces allowance balance).
#[serde(with = "crate::serde_decimal")]
pub write_offs: Decimal,
/// Cash recoveries on previously written-off receivables (increases allowance).
#[serde(with = "crate::serde_decimal")]
pub recoveries: Decimal,
/// Closing allowance = opening + new_originations + stage_transfers - write_offs + recoveries.
#[serde(with = "crate::serde_decimal")]
pub closing: Decimal,
/// P&L charge for the period = new_originations + stage_transfers + recoveries - write_offs.
#[serde(with = "crate::serde_decimal")]
pub pl_charge: Decimal,
}