Skip to main content

datasynth_generators/period_close/
segment_generator.rs

1//! IFRS 8 / ASC 280 Operating Segment Reporting generator.
2//!
3//! Produces:
4//! - A set of [`OperatingSegment`] records that partition the consolidated
5//!   financials into reportable segments (geographic or product-line).
6//! - A [`SegmentReconciliation`] that proves segment totals tie back to
7//!   the consolidated income-statement and balance-sheet totals.
8//!
9//! ## Segment derivation logic
10//!
11//! | Config | Segment basis |
12//! |--------|---------------|
13//! | Multi-entity (≥2 companies) | One `Geographic` segment per company |
14//! | Single-entity | 2–3 `ProductLine` segments from CoA revenue sub-ranges |
15//!
16//! ## Reconciliation identity
17//!
18//! ```text
19//! consolidated_revenue  = Σ revenue_external
20//!                       = segment_revenue_total + intersegment_eliminations
21//! consolidated_profit   = segment_profit_total  + corporate_overhead
22//! consolidated_assets   = segment_assets_total  + unallocated_assets
23//! ```
24
25use datasynth_core::models::{OperatingSegment, SegmentReconciliation, SegmentType};
26use datasynth_core::utils::seeded_rng;
27use datasynth_core::uuid_factory::{DeterministicUuidFactory, GeneratorType};
28use rand::prelude::*;
29use rand_chacha::ChaCha8Rng;
30use rust_decimal::Decimal;
31use tracing::debug;
32
33/// Generates IFRS 8 / ASC 280 segment reporting data.
34pub struct SegmentGenerator {
35    rng: ChaCha8Rng,
36    uuid_factory: DeterministicUuidFactory,
37}
38
39/// Lightweight description of one entity / business unit used to seed segment names.
40#[derive(Debug, Clone)]
41pub struct SegmentSeed {
42    /// Company or business-unit code (e.g. "C001")
43    pub code: String,
44    /// Human-readable name (e.g. "North America" or "Software Products")
45    pub name: String,
46    /// Currency used by this entity (informational only)
47    pub currency: String,
48}
49
50impl SegmentGenerator {
51    /// Create a new segment generator with the given seed.
52    pub fn new(seed: u64) -> Self {
53        Self {
54            rng: seeded_rng(seed, 0),
55            uuid_factory: DeterministicUuidFactory::new(seed, GeneratorType::SegmentReport),
56        }
57    }
58
59    /// Generate operating segments and a reconciliation for one fiscal period.
60    ///
61    /// # Arguments
62    /// * `company_code` – group / parent company code used in output records
63    /// * `period` – fiscal period label (e.g. "2024-03")
64    /// * `consolidated_revenue` – total external revenue from the consolidated IS
65    /// * `consolidated_profit` – consolidated operating profit
66    /// * `consolidated_assets` – consolidated total assets
67    /// * `entity_seeds` – one entry per legal entity / product line to derive segments from
68    /// * `total_depreciation` – consolidated D&A from the depreciation run (e.g.
69    ///   `DepreciationRun.total_depreciation`).  When `Some`, distributed to segments
70    ///   proportionally to their share of total assets.  When `None`, D&A is approximated
71    ///   as 50–80 % of each segment's CapEx (a reasonable synthetic-data heuristic).
72    ///
73    /// # Returns
74    /// `(segments, reconciliation)` where the reconciliation ties segment totals
75    /// back to the consolidated figures passed in.
76    pub fn generate(
77        &mut self,
78        company_code: &str,
79        period: &str,
80        consolidated_revenue: Decimal,
81        consolidated_profit: Decimal,
82        consolidated_assets: Decimal,
83        entity_seeds: &[SegmentSeed],
84        total_depreciation: Option<Decimal>,
85    ) -> (Vec<OperatingSegment>, SegmentReconciliation) {
86        debug!(
87            company_code,
88            period,
89            consolidated_revenue = consolidated_revenue.to_string(),
90            consolidated_profit = consolidated_profit.to_string(),
91            consolidated_assets = consolidated_assets.to_string(),
92            n_seeds = entity_seeds.len(),
93            "Generating segment reports"
94        );
95
96        // Determine segment type and names
97        let (segment_type, segment_names) = if entity_seeds.len() >= 2 {
98            // Multi-entity: geographic segments = one per legal entity
99            let names: Vec<String> = entity_seeds.iter().map(|s| s.name.clone()).collect();
100            (SegmentType::Geographic, names)
101        } else {
102            // Single-entity: create 2-3 product line segments
103            (SegmentType::ProductLine, self.default_product_lines())
104        };
105
106        let n = segment_names.len().clamp(2, 8);
107
108        // Generate proportional splits for revenue, profit, assets
109        let rev_splits = self.random_proportions(n);
110        let profit_multipliers = self.profit_multipliers(n);
111        let asset_splits = self.random_proportions(n);
112
113        // Build segments
114        let mut segments: Vec<OperatingSegment> = Vec::with_capacity(n);
115
116        // Intersegment revenue: only meaningful for geographic / multi-entity
117        // Use 3–8 % of gross revenue as intersegment transactions
118        let intersegment_rate = if segment_type == SegmentType::Geographic && n >= 2 {
119            let rate_bps = self.rng.random_range(300u32..=800);
120            Decimal::from(rate_bps) / Decimal::from(10_000u32)
121        } else {
122            Decimal::ZERO
123        };
124
125        let total_intersegment = consolidated_revenue.abs() * intersegment_rate;
126
127        // We want: Σ revenue_external = consolidated_revenue
128        // Therefore allocate consolidated_revenue proportionally as external revenue.
129        // Intersegment revenue is additional on top (it cancels in consolidation).
130        let mut remaining_rev = consolidated_revenue;
131        let mut remaining_profit = consolidated_profit;
132        let mut remaining_assets = consolidated_assets;
133
134        for (i, name) in segment_names.iter().take(n).enumerate() {
135            let is_last = i == n - 1;
136
137            let ext_rev = if is_last {
138                remaining_rev
139            } else {
140                let r = consolidated_revenue * rev_splits[i];
141                remaining_rev -= r;
142                r
143            };
144
145            // Intersegment: distribute evenly across all segments (they net to zero)
146            let interseg_rev = if intersegment_rate > Decimal::ZERO {
147                total_intersegment * rev_splits[i]
148            } else {
149                Decimal::ZERO
150            };
151
152            // Operating profit: apply per-segment margin multiplier
153            let seg_profit = if is_last {
154                remaining_profit
155            } else {
156                let base_margin = if consolidated_revenue != Decimal::ZERO {
157                    consolidated_profit / consolidated_revenue
158                } else {
159                    Decimal::ZERO
160                };
161                let adjusted_margin = base_margin * profit_multipliers[i];
162                let p = ext_rev * adjusted_margin;
163                remaining_profit -= p;
164                p
165            };
166
167            let seg_assets = if is_last {
168                remaining_assets
169            } else {
170                let a = consolidated_assets * asset_splits[i];
171                remaining_assets -= a;
172                a
173            };
174
175            // Liabilities: assume ~40-60 % of assets ratio with some noise
176            let liab_ratio =
177                Decimal::from(self.rng.random_range(35u32..=55)) / Decimal::from(100u32);
178            let seg_liabilities = (seg_assets * liab_ratio).max(Decimal::ZERO);
179
180            // CapEx: ~3-8 % of segment assets
181            let capex_rate =
182                Decimal::from(self.rng.random_range(30u32..=80)) / Decimal::from(1_000u32);
183            let capex = (seg_assets * capex_rate).max(Decimal::ZERO);
184
185            // D&A: when `total_depreciation` is provided (from the FA subledger depreciation
186            // run), distribute it proportionally to each segment's share of total assets.
187            // Fall back to the 50–80 % of CapEx heuristic when no actual depreciation data
188            // is available (e.g. when the FA subledger is not generated).
189            let da = if let Some(total_depr) = total_depreciation {
190                let asset_share = if consolidated_assets != Decimal::ZERO {
191                    seg_assets / consolidated_assets
192                } else {
193                    asset_splits[i]
194                };
195                (total_depr * asset_share).max(Decimal::ZERO)
196            } else {
197                let da_ratio =
198                    Decimal::from(self.rng.random_range(50u32..=80)) / Decimal::from(100u32);
199                capex * da_ratio
200            };
201
202            segments.push(OperatingSegment {
203                segment_id: self.uuid_factory.next().to_string(),
204                name: name.clone(),
205                segment_type,
206                revenue_external: ext_rev,
207                revenue_intersegment: interseg_rev,
208                operating_profit: seg_profit,
209                total_assets: seg_assets,
210                total_liabilities: seg_liabilities,
211                capital_expenditure: capex,
212                depreciation_amortization: da,
213                period: period.to_string(),
214                company_code: company_code.to_string(),
215            });
216        }
217
218        // Corporate overhead: 2–5 % of consolidated revenue (negative)
219        let overhead_rate = Decimal::from(self.rng.random_range(2u32..=5)) / Decimal::from(100u32);
220        let corporate_overhead = -(consolidated_revenue.abs() * overhead_rate);
221
222        // Unallocated assets: goodwill, deferred tax, etc — 5-12 % of total
223        let unalloc_rate = Decimal::from(self.rng.random_range(5u32..=12)) / Decimal::from(100u32);
224        let unallocated_assets = consolidated_assets.abs() * unalloc_rate;
225
226        // Segment totals
227        let segment_revenue_total: Decimal = segments
228            .iter()
229            .map(|s| s.revenue_external + s.revenue_intersegment)
230            .sum();
231
232        // Intersegment eliminations are derived to enforce the exact identity:
233        //   segment_revenue_total + intersegment_eliminations = consolidated_revenue
234        // This avoids any decimal precision residual from proportion arithmetic.
235        let intersegment_eliminations = consolidated_revenue - segment_revenue_total;
236
237        let segment_profit_total: Decimal = segments.iter().map(|s| s.operating_profit).sum();
238        let segment_assets_total: Decimal = segments.iter().map(|s| s.total_assets).sum();
239
240        let reconciliation = SegmentReconciliation {
241            period: period.to_string(),
242            company_code: company_code.to_string(),
243            segment_revenue_total,
244            intersegment_eliminations,
245            consolidated_revenue,
246            segment_profit_total,
247            corporate_overhead,
248            consolidated_profit: segment_profit_total + corporate_overhead,
249            segment_assets_total,
250            unallocated_assets,
251            consolidated_assets: segment_assets_total + unallocated_assets,
252        };
253
254        (segments, reconciliation)
255    }
256
257    // -----------------------------------------------------------------------
258    // Private helpers
259    // -----------------------------------------------------------------------
260
261    /// Generate a default list of 3 product-line segment names.
262    fn default_product_lines(&mut self) -> Vec<String> {
263        let options: &[&[&str]] = &[
264            &["Products", "Services", "Licensing"],
265            &["Hardware", "Software", "Support"],
266            &["Consumer", "Commercial", "Enterprise"],
267            &["Core", "Growth", "Emerging"],
268            &["Domestic", "International", "Other"],
269        ];
270        let idx = self.rng.random_range(0..options.len());
271        options[idx].iter().map(|s| s.to_string()).collect()
272    }
273
274    /// Generate n random proportions that sum to 1.
275    fn random_proportions(&mut self, n: usize) -> Vec<Decimal> {
276        // Draw n uniform samples and normalise
277        let raw: Vec<f64> = (0..n)
278            .map(|_| self.rng.random_range(1u32..=100) as f64)
279            .collect();
280        let total: f64 = raw.iter().sum();
281        raw.iter()
282            .map(|v| Decimal::from_f64_retain(v / total).unwrap_or(Decimal::ZERO))
283            .collect()
284    }
285
286    /// Generate per-segment profit multipliers (relative margin adjustments) around 1.0.
287    fn profit_multipliers(&mut self, n: usize) -> Vec<Decimal> {
288        (0..n)
289            .map(|_| {
290                let m = self.rng.random_range(70u32..=130);
291                Decimal::from(m) / Decimal::from(100u32)
292            })
293            .collect()
294    }
295}
296
297#[cfg(test)]
298#[allow(clippy::unwrap_used)]
299mod tests {
300    use super::*;
301
302    fn make_seeds(names: &[&str]) -> Vec<SegmentSeed> {
303        names
304            .iter()
305            .enumerate()
306            .map(|(i, n)| SegmentSeed {
307                code: format!("C{:03}", i + 1),
308                name: n.to_string(),
309                currency: "USD".to_string(),
310            })
311            .collect()
312    }
313
314    #[test]
315    fn test_segment_totals_match_consolidated_revenue() {
316        let mut gen = SegmentGenerator::new(42);
317        let seeds = make_seeds(&["North America", "Europe"]);
318
319        let rev = Decimal::from(1_000_000);
320        let profit = Decimal::from(150_000);
321        let assets = Decimal::from(5_000_000);
322
323        let (segments, recon) = gen.generate("GROUP", "2024-03", rev, profit, assets, &seeds, None);
324
325        assert!(!segments.is_empty());
326
327        // Σ external revenues = consolidated_revenue
328        let sum_ext: Decimal = segments.iter().map(|s| s.revenue_external).sum();
329        assert_eq!(
330            sum_ext, rev,
331            "Σ external revenue should equal consolidated_revenue"
332        );
333
334        // Reconciliation identity: segment_revenue_total + eliminations = consolidated_revenue
335        let computed = recon.segment_revenue_total + recon.intersegment_eliminations;
336        assert_eq!(
337            computed, recon.consolidated_revenue,
338            "segment_revenue_total + eliminations ≠ consolidated_revenue"
339        );
340    }
341
342    #[test]
343    fn test_reconciliation_profit_math() {
344        let mut gen = SegmentGenerator::new(99);
345        let seeds = make_seeds(&["Americas", "EMEA", "APAC"]);
346
347        let (_, recon) = gen.generate(
348            "CORP",
349            "2024-06",
350            Decimal::from(2_000_000),
351            Decimal::from(300_000),
352            Decimal::from(8_000_000),
353            &seeds,
354            None,
355        );
356
357        // consolidated_profit = segment_profit_total + corporate_overhead
358        assert_eq!(
359            recon.consolidated_profit,
360            recon.segment_profit_total + recon.corporate_overhead,
361            "Profit reconciliation identity failed"
362        );
363    }
364
365    #[test]
366    fn test_reconciliation_assets_math() {
367        let mut gen = SegmentGenerator::new(7);
368        let seeds = make_seeds(&["ProductA", "ProductB"]);
369
370        let (_, recon) = gen.generate(
371            "C001",
372            "2024-01",
373            Decimal::from(500_000),
374            Decimal::from(50_000),
375            Decimal::from(3_000_000),
376            &seeds,
377            None,
378        );
379
380        // consolidated_assets = segment_assets_total + unallocated_assets
381        assert_eq!(
382            recon.consolidated_assets,
383            recon.segment_assets_total + recon.unallocated_assets,
384            "Asset reconciliation identity failed"
385        );
386    }
387
388    #[test]
389    fn test_each_segment_has_positive_external_revenue() {
390        let mut gen = SegmentGenerator::new(42);
391        // Use seeds with all positive consolidated numbers
392        let seeds = make_seeds(&["SegA", "SegB", "SegC"]);
393        let rev = Decimal::from(3_000_000);
394        let profit = Decimal::from(600_000);
395        let assets = Decimal::from(10_000_000);
396
397        let (segments, _) = gen.generate("GRP", "2024-12", rev, profit, assets, &seeds, None);
398
399        for seg in &segments {
400            assert!(
401                seg.revenue_external >= Decimal::ZERO,
402                "Segment '{}' has negative external revenue: {}",
403                seg.name,
404                seg.revenue_external
405            );
406        }
407    }
408
409    #[test]
410    fn test_single_entity_uses_product_lines() {
411        let mut gen = SegmentGenerator::new(1234);
412        let seeds = make_seeds(&["OnlyEntity"]);
413
414        let (segments, _) = gen.generate(
415            "C001",
416            "2024-03",
417            Decimal::from(1_000_000),
418            Decimal::from(100_000),
419            Decimal::from(4_000_000),
420            &seeds,
421            None,
422        );
423
424        // With a single seed, product-line segments should be generated (≥ 2)
425        assert!(segments.len() >= 2, "Expected ≥ 2 product-line segments");
426        // All should be ProductLine type
427        for seg in &segments {
428            assert_eq!(seg.segment_type, SegmentType::ProductLine);
429        }
430    }
431
432    #[test]
433    fn test_multi_entity_uses_geographic_segments() {
434        let mut gen = SegmentGenerator::new(5678);
435        let seeds = make_seeds(&["US", "DE", "JP"]);
436
437        let (segments, _) = gen.generate(
438            "GROUP",
439            "2024-03",
440            Decimal::from(9_000_000),
441            Decimal::from(900_000),
442            Decimal::from(30_000_000),
443            &seeds,
444            None,
445        );
446
447        assert_eq!(segments.len(), 3);
448        for seg in &segments {
449            assert_eq!(seg.segment_type, SegmentType::Geographic);
450        }
451    }
452
453    #[test]
454    fn test_deterministic() {
455        let seeds = make_seeds(&["A", "B"]);
456        let rev = Decimal::from(1_000_000);
457        let profit = Decimal::from(200_000);
458        let assets = Decimal::from(5_000_000);
459
460        let (segs1, recon1) =
461            SegmentGenerator::new(42).generate("G", "2024-01", rev, profit, assets, &seeds, None);
462        let (segs2, recon2) =
463            SegmentGenerator::new(42).generate("G", "2024-01", rev, profit, assets, &seeds, None);
464
465        assert_eq!(segs1.len(), segs2.len());
466        for (a, b) in segs1.iter().zip(segs2.iter()) {
467            assert_eq!(a.segment_id, b.segment_id);
468            assert_eq!(a.revenue_external, b.revenue_external);
469        }
470        assert_eq!(recon1.consolidated_revenue, recon2.consolidated_revenue);
471        assert_eq!(recon1.segment_profit_total, recon2.segment_profit_total);
472    }
473}