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
//! IFRS 8 / ASC 280 Operating Segment Reporting generator.
//!
//! Produces:
//! - A set of [`OperatingSegment`] records that partition the consolidated
//! financials into reportable segments (geographic or product-line).
//! - A [`SegmentReconciliation`] that proves segment totals tie back to
//! the consolidated income-statement and balance-sheet totals.
//!
//! ## Segment derivation logic
//!
//! | Config | Segment basis |
//! |--------|---------------|
//! | Multi-entity (≥2 companies) | One `Geographic` segment per company |
//! | Single-entity | 2–3 `ProductLine` segments from CoA revenue sub-ranges |
//!
//! ## Reconciliation identity
//!
//! ```text
//! consolidated_revenue = Σ revenue_external
//! = segment_revenue_total + intersegment_eliminations
//! consolidated_profit = segment_profit_total + corporate_overhead
//! consolidated_assets = segment_assets_total + unallocated_assets
//! ```
use datasynth_core::models::{JournalEntry, OperatingSegment, SegmentReconciliation, SegmentType};
use datasynth_core::utils::seeded_rng;
use datasynth_core::uuid_factory::{DeterministicUuidFactory, GeneratorType};
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
use rust_decimal::Decimal;
use tracing::debug;
/// Generates IFRS 8 / ASC 280 segment reporting data.
pub struct SegmentGenerator {
rng: ChaCha8Rng,
uuid_factory: DeterministicUuidFactory,
}
/// Lightweight description of one entity / business unit used to seed segment names.
#[derive(Debug, Clone)]
pub struct SegmentSeed {
/// Company or business-unit code (e.g. "C001")
pub code: String,
/// Human-readable name (e.g. "North America" or "Software Products")
pub name: String,
/// Currency used by this entity (informational only)
pub currency: String,
}
impl SegmentGenerator {
/// Create a new segment generator with the given seed.
pub fn new(seed: u64) -> Self {
Self {
rng: seeded_rng(seed, 0),
uuid_factory: DeterministicUuidFactory::new(seed, GeneratorType::SegmentReport),
}
}
/// Generate operating segments and a reconciliation for one fiscal period.
///
/// # Arguments
/// * `company_code` – group / parent company code used in output records
/// * `period` – fiscal period label (e.g. "2024-03")
/// * `consolidated_revenue` – total external revenue from the consolidated IS
/// * `consolidated_profit` – consolidated operating profit
/// * `consolidated_assets` – consolidated total assets
/// * `entity_seeds` – one entry per legal entity / product line to derive segments from
/// * `total_depreciation` – consolidated D&A from the depreciation run (e.g.
/// `DepreciationRun.total_depreciation`). When `Some`, distributed to segments
/// proportionally to their share of total assets. When `None`, D&A is approximated
/// as 50–80 % of each segment's CapEx (a reasonable synthetic-data heuristic).
///
/// # Returns
/// `(segments, reconciliation)` where the reconciliation ties segment totals
/// back to the consolidated figures passed in.
pub fn generate(
&mut self,
company_code: &str,
period: &str,
consolidated_revenue: Decimal,
consolidated_profit: Decimal,
consolidated_assets: Decimal,
entity_seeds: &[SegmentSeed],
total_depreciation: Option<Decimal>,
) -> (Vec<OperatingSegment>, SegmentReconciliation) {
debug!(
company_code,
period,
consolidated_revenue = consolidated_revenue.to_string(),
consolidated_profit = consolidated_profit.to_string(),
consolidated_assets = consolidated_assets.to_string(),
n_seeds = entity_seeds.len(),
"Generating segment reports"
);
// Determine segment type and names
let (segment_type, segment_names) = if entity_seeds.len() >= 2 {
// Multi-entity: geographic segments = one per legal entity
let names: Vec<String> = entity_seeds.iter().map(|s| s.name.clone()).collect();
(SegmentType::Geographic, names)
} else {
// Single-entity: create 2-3 product line segments
(SegmentType::ProductLine, self.default_product_lines())
};
let n = segment_names.len().clamp(2, 8);
// Generate proportional splits for revenue, profit, assets
let rev_splits = self.random_proportions(n);
let profit_multipliers = self.profit_multipliers(n);
let asset_splits = self.random_proportions(n);
// Build segments
let mut segments: Vec<OperatingSegment> = Vec::with_capacity(n);
// Intersegment revenue: only meaningful for geographic / multi-entity
// Use 3–8 % of gross revenue as intersegment transactions
let intersegment_rate = if segment_type == SegmentType::Geographic && n >= 2 {
let rate_bps = self.rng.random_range(300u32..=800);
Decimal::from(rate_bps) / Decimal::from(10_000u32)
} else {
Decimal::ZERO
};
let total_intersegment = consolidated_revenue.abs() * intersegment_rate;
// We want: Σ revenue_external = consolidated_revenue
// Therefore allocate consolidated_revenue proportionally as external revenue.
// Intersegment revenue is additional on top (it cancels in consolidation).
let mut remaining_rev = consolidated_revenue;
let mut remaining_profit = consolidated_profit;
let mut remaining_assets = consolidated_assets;
for (i, name) in segment_names.iter().take(n).enumerate() {
let is_last = i == n - 1;
let ext_rev = if is_last {
remaining_rev
} else {
let r = consolidated_revenue * rev_splits[i];
remaining_rev -= r;
r
};
// Intersegment: distribute evenly across all segments (they net to zero)
let interseg_rev = if intersegment_rate > Decimal::ZERO {
total_intersegment * rev_splits[i]
} else {
Decimal::ZERO
};
// Operating profit: apply per-segment margin multiplier
let seg_profit = if is_last {
remaining_profit
} else {
let base_margin = if consolidated_revenue != Decimal::ZERO {
consolidated_profit / consolidated_revenue
} else {
Decimal::ZERO
};
let adjusted_margin = base_margin * profit_multipliers[i];
let p = ext_rev * adjusted_margin;
remaining_profit -= p;
p
};
let seg_assets = if is_last {
remaining_assets
} else {
let a = consolidated_assets * asset_splits[i];
remaining_assets -= a;
a
};
// Liabilities: assume ~40-60 % of assets ratio with some noise
let liab_ratio =
Decimal::from(self.rng.random_range(35u32..=55)) / Decimal::from(100u32);
let seg_liabilities = (seg_assets * liab_ratio).max(Decimal::ZERO);
// CapEx: ~3-8 % of segment assets
let capex_rate =
Decimal::from(self.rng.random_range(30u32..=80)) / Decimal::from(1_000u32);
let capex = (seg_assets * capex_rate).max(Decimal::ZERO);
// D&A: when `total_depreciation` is provided (from the FA subledger depreciation
// run), distribute it proportionally to each segment's share of total assets.
// Fall back to the 50–80 % of CapEx heuristic when no actual depreciation data
// is available (e.g. when the FA subledger is not generated).
let da = if let Some(total_depr) = total_depreciation {
let asset_share = if consolidated_assets != Decimal::ZERO {
seg_assets / consolidated_assets
} else {
asset_splits[i]
};
(total_depr * asset_share).max(Decimal::ZERO)
} else {
let da_ratio =
Decimal::from(self.rng.random_range(50u32..=80)) / Decimal::from(100u32);
capex * da_ratio
};
segments.push(OperatingSegment {
segment_id: self.uuid_factory.next().to_string(),
name: name.clone(),
segment_type,
revenue_external: ext_rev,
revenue_intersegment: interseg_rev,
operating_profit: seg_profit,
total_assets: seg_assets,
total_liabilities: seg_liabilities,
capital_expenditure: capex,
depreciation_amortization: da,
period: period.to_string(),
company_code: company_code.to_string(),
});
}
// Unallocated assets: goodwill, deferred tax, etc — 5-12 % of total
let unalloc_rate = Decimal::from(self.rng.random_range(5u32..=12)) / Decimal::from(100u32);
let unallocated_assets = consolidated_assets.abs() * unalloc_rate;
// Segment totals
let segment_revenue_total: Decimal = segments
.iter()
.map(|s| s.revenue_external + s.revenue_intersegment)
.sum();
// Intersegment eliminations are derived to enforce the exact identity:
// segment_revenue_total + intersegment_eliminations = consolidated_revenue
// This avoids any decimal precision residual from proportion arithmetic.
let intersegment_eliminations = consolidated_revenue - segment_revenue_total;
let segment_profit_total: Decimal = segments.iter().map(|s| s.operating_profit).sum();
let segment_assets_total: Decimal = segments.iter().map(|s| s.total_assets).sum();
// Corporate overhead: derived to enforce the identity
// segment_profit_total + corporate_overhead = consolidated_profit
let corporate_overhead = consolidated_profit - segment_profit_total;
let reconciliation = SegmentReconciliation {
period: period.to_string(),
company_code: company_code.to_string(),
segment_revenue_total,
intersegment_eliminations,
consolidated_revenue,
segment_profit_total,
corporate_overhead,
consolidated_profit,
segment_assets_total,
unallocated_assets,
consolidated_assets: segment_assets_total + unallocated_assets,
};
(segments, reconciliation)
}
/// Generate operating segments from actual journal entry data per entity.
///
/// For each company, aggregates JEs by GL account prefix:
/// - Revenue (4xxx): net credits
/// - COGS (5xxx): net debits
/// - OpEx (6xxx + 7xxx): net debits
/// - Assets (1xxx): debit balances (snapshot from all entries)
/// - Liabilities (2xxx): credit balances
///
/// # Arguments
/// * `journal_entries` – slice of all journal entries to aggregate
/// * `companies` – `(code, name)` tuples identifying each segment entity
/// * `period` – fiscal period label (e.g. `"2025-Q1"`)
/// * `ic_elimination_amount` – known intercompany elimination to embed in the reconciliation
///
/// # Returns
/// `(segments, reconciliation)` where each segment corresponds to one company
/// and the reconciliation ties all segment totals to the consolidated figures.
pub fn generate_from_journal_entries(
&mut self,
journal_entries: &[JournalEntry],
companies: &[(String, String)],
period: &str,
ic_elimination_amount: Decimal,
) -> (Vec<OperatingSegment>, SegmentReconciliation) {
let mut segments: Vec<OperatingSegment> = Vec::with_capacity(companies.len());
for (code, name) in companies {
// Aggregate amounts for this company across all its JE lines.
let mut revenue = Decimal::ZERO;
let mut cogs = Decimal::ZERO;
let mut opex = Decimal::ZERO;
let mut assets = Decimal::ZERO;
let mut liabilities = Decimal::ZERO;
for je in journal_entries
.iter()
.filter(|je| je.company_code() == code)
{
for line in &je.lines {
let prefix = line.gl_account.chars().next().unwrap_or('0');
match prefix {
'4' => {
// Revenue: net credits (credits - debits)
revenue += line.credit_amount - line.debit_amount;
}
'5' => {
// COGS: net debits (debits - credits)
cogs += line.debit_amount - line.credit_amount;
}
'6' | '7' => {
// OpEx: net debits (debits - credits)
opex += line.debit_amount - line.credit_amount;
}
'1' => {
// Assets: debit balances (debits - credits)
assets += line.debit_amount - line.credit_amount;
}
'2' => {
// Liabilities: credit balances (credits - debits)
liabilities += line.credit_amount - line.debit_amount;
}
_ => {}
}
}
}
let operating_profit = revenue - cogs - opex;
segments.push(OperatingSegment {
segment_id: self.uuid_factory.next().to_string(),
name: name.clone(),
segment_type: SegmentType::LegalEntity,
revenue_external: revenue,
revenue_intersegment: Decimal::ZERO,
operating_profit,
total_assets: assets,
total_liabilities: liabilities,
capital_expenditure: Decimal::ZERO,
depreciation_amortization: Decimal::ZERO,
period: period.to_string(),
company_code: code.clone(),
});
}
// Build reconciliation
let segment_revenue_total: Decimal = segments.iter().map(|s| s.revenue_external).sum();
let segment_profit_total: Decimal = segments.iter().map(|s| s.operating_profit).sum();
let segment_assets_total: Decimal = segments.iter().map(|s| s.total_assets).sum();
let corporate_overhead = Decimal::ZERO;
let unallocated_assets = Decimal::ZERO;
let reconciliation = SegmentReconciliation {
period: period.to_string(),
// Use the first company code as group code, or empty string if none
company_code: companies
.first()
.map(|(c, _)| c.clone())
.unwrap_or_default(),
segment_revenue_total,
intersegment_eliminations: ic_elimination_amount,
consolidated_revenue: segment_revenue_total - ic_elimination_amount,
segment_profit_total,
corporate_overhead,
consolidated_profit: segment_profit_total, // no corporate overhead allocation for JE-derived segments
segment_assets_total,
unallocated_assets,
consolidated_assets: segment_assets_total + unallocated_assets,
};
(segments, reconciliation)
}
// -----------------------------------------------------------------------
// Private helpers
// -----------------------------------------------------------------------
/// Generate a default list of 3 product-line segment names.
fn default_product_lines(&mut self) -> Vec<String> {
let options: &[&[&str]] = &[
&["Products", "Services", "Licensing"],
&["Hardware", "Software", "Support"],
&["Consumer", "Commercial", "Enterprise"],
&["Core", "Growth", "Emerging"],
&["Domestic", "International", "Other"],
];
let idx = self.rng.random_range(0..options.len());
options[idx].iter().map(|s| s.to_string()).collect()
}
/// Generate n random proportions that sum to 1.
fn random_proportions(&mut self, n: usize) -> Vec<Decimal> {
// Draw n uniform samples and normalise
let raw: Vec<f64> = (0..n)
.map(|_| self.rng.random_range(1u32..=100) as f64)
.collect();
let total: f64 = raw.iter().sum();
raw.iter()
.map(|v| Decimal::from_f64_retain(v / total).unwrap_or(Decimal::ZERO))
.collect()
}
/// Generate per-segment profit multipliers (relative margin adjustments) around 1.0.
fn profit_multipliers(&mut self, n: usize) -> Vec<Decimal> {
(0..n)
.map(|_| {
let m = self.rng.random_range(70u32..=130);
Decimal::from(m) / Decimal::from(100u32)
})
.collect()
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
fn make_seeds(names: &[&str]) -> Vec<SegmentSeed> {
names
.iter()
.enumerate()
.map(|(i, n)| SegmentSeed {
code: format!("C{:03}", i + 1),
name: n.to_string(),
currency: "USD".to_string(),
})
.collect()
}
#[test]
fn test_segment_totals_match_consolidated_revenue() {
let mut gen = SegmentGenerator::new(42);
let seeds = make_seeds(&["North America", "Europe"]);
let rev = Decimal::from(1_000_000);
let profit = Decimal::from(150_000);
let assets = Decimal::from(5_000_000);
let (segments, recon) = gen.generate("GROUP", "2024-03", rev, profit, assets, &seeds, None);
assert!(!segments.is_empty());
// Σ external revenues = consolidated_revenue
let sum_ext: Decimal = segments.iter().map(|s| s.revenue_external).sum();
assert_eq!(
sum_ext, rev,
"Σ external revenue should equal consolidated_revenue"
);
// Reconciliation identity: segment_revenue_total + eliminations = consolidated_revenue
let computed = recon.segment_revenue_total + recon.intersegment_eliminations;
assert_eq!(
computed, recon.consolidated_revenue,
"segment_revenue_total + eliminations ≠ consolidated_revenue"
);
}
#[test]
fn test_reconciliation_profit_math() {
let mut gen = SegmentGenerator::new(99);
let seeds = make_seeds(&["Americas", "EMEA", "APAC"]);
let (_, recon) = gen.generate(
"CORP",
"2024-06",
Decimal::from(2_000_000),
Decimal::from(300_000),
Decimal::from(8_000_000),
&seeds,
None,
);
// consolidated_profit = segment_profit_total + corporate_overhead
assert_eq!(
recon.consolidated_profit,
recon.segment_profit_total + recon.corporate_overhead,
"Profit reconciliation identity failed"
);
}
#[test]
fn test_reconciliation_assets_math() {
let mut gen = SegmentGenerator::new(7);
let seeds = make_seeds(&["ProductA", "ProductB"]);
let (_, recon) = gen.generate(
"C001",
"2024-01",
Decimal::from(500_000),
Decimal::from(50_000),
Decimal::from(3_000_000),
&seeds,
None,
);
// consolidated_assets = segment_assets_total + unallocated_assets
assert_eq!(
recon.consolidated_assets,
recon.segment_assets_total + recon.unallocated_assets,
"Asset reconciliation identity failed"
);
}
#[test]
fn test_each_segment_has_positive_external_revenue() {
let mut gen = SegmentGenerator::new(42);
// Use seeds with all positive consolidated numbers
let seeds = make_seeds(&["SegA", "SegB", "SegC"]);
let rev = Decimal::from(3_000_000);
let profit = Decimal::from(600_000);
let assets = Decimal::from(10_000_000);
let (segments, _) = gen.generate("GRP", "2024-12", rev, profit, assets, &seeds, None);
for seg in &segments {
assert!(
seg.revenue_external >= Decimal::ZERO,
"Segment '{}' has negative external revenue: {}",
seg.name,
seg.revenue_external
);
}
}
#[test]
fn test_single_entity_uses_product_lines() {
let mut gen = SegmentGenerator::new(1234);
let seeds = make_seeds(&["OnlyEntity"]);
let (segments, _) = gen.generate(
"C001",
"2024-03",
Decimal::from(1_000_000),
Decimal::from(100_000),
Decimal::from(4_000_000),
&seeds,
None,
);
// With a single seed, product-line segments should be generated (≥ 2)
assert!(segments.len() >= 2, "Expected ≥ 2 product-line segments");
// All should be ProductLine type
for seg in &segments {
assert_eq!(seg.segment_type, SegmentType::ProductLine);
}
}
#[test]
fn test_multi_entity_uses_geographic_segments() {
let mut gen = SegmentGenerator::new(5678);
let seeds = make_seeds(&["US", "DE", "JP"]);
let (segments, _) = gen.generate(
"GROUP",
"2024-03",
Decimal::from(9_000_000),
Decimal::from(900_000),
Decimal::from(30_000_000),
&seeds,
None,
);
assert_eq!(segments.len(), 3);
for seg in &segments {
assert_eq!(seg.segment_type, SegmentType::Geographic);
}
}
#[test]
fn test_deterministic() {
let seeds = make_seeds(&["A", "B"]);
let rev = Decimal::from(1_000_000);
let profit = Decimal::from(200_000);
let assets = Decimal::from(5_000_000);
let (segs1, recon1) =
SegmentGenerator::new(42).generate("G", "2024-01", rev, profit, assets, &seeds, None);
let (segs2, recon2) =
SegmentGenerator::new(42).generate("G", "2024-01", rev, profit, assets, &seeds, None);
assert_eq!(segs1.len(), segs2.len());
for (a, b) in segs1.iter().zip(segs2.iter()) {
assert_eq!(a.segment_id, b.segment_id);
assert_eq!(a.revenue_external, b.revenue_external);
}
assert_eq!(recon1.consolidated_revenue, recon2.consolidated_revenue);
assert_eq!(recon1.segment_profit_total, recon2.segment_profit_total);
}
}