datasynth-generators 2.2.0

50+ data generators covering GL, P2P, O2C, S2C, HR, manufacturing, audit, tax, treasury, and ESG
Documentation
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
//! Period close engine for orchestrating the close process.

use rust_decimal::Decimal;
use tracing::debug;

use datasynth_core::models::{
    CloseSchedule, CloseTask, CloseTaskResult, CloseTaskStatus, FiscalPeriod, JournalEntry,
    PeriodCloseRun, PeriodCloseStatus, PeriodStatus,
};

/// Configuration for the close engine.
#[derive(Debug, Clone)]
pub struct CloseEngineConfig {
    /// Whether to stop on first error.
    pub stop_on_error: bool,
    /// Whether to generate reversal entries for accruals.
    pub auto_reverse_accruals: bool,
    /// Whether to validate subledger reconciliation.
    pub require_reconciliation: bool,
    /// Tolerance for reconciliation differences.
    pub reconciliation_tolerance: Decimal,
}

impl Default for CloseEngineConfig {
    fn default() -> Self {
        Self {
            stop_on_error: false,
            auto_reverse_accruals: true,
            require_reconciliation: true,
            reconciliation_tolerance: Decimal::new(1, 2), // 0.01
        }
    }
}

/// Period close engine that orchestrates the close process.
pub struct CloseEngine {
    config: CloseEngineConfig,
    run_counter: u64,
}

impl CloseEngine {
    /// Creates a new close engine.
    pub fn new(config: CloseEngineConfig) -> Self {
        Self {
            config,
            run_counter: 0,
        }
    }

    /// Executes a period close for a company.
    pub fn execute_close(
        &mut self,
        company_code: &str,
        fiscal_period: FiscalPeriod,
        schedule: &CloseSchedule,
        context: &mut CloseContext,
    ) -> PeriodCloseRun {
        debug!(
            company_code,
            period = fiscal_period.period,
            year = fiscal_period.year,
            task_count = schedule.tasks.len(),
            "Executing period close"
        );
        self.run_counter += 1;
        let run_id = format!("CLOSE-{:08}", self.run_counter);

        let mut run = PeriodCloseRun::new(run_id, company_code.to_string(), fiscal_period.clone());
        run.status = PeriodCloseStatus::InProgress;
        run.started_at = Some(fiscal_period.end_date);

        // Execute tasks in sequence order
        let mut _current_sequence = 0u32;
        for scheduled_task in &schedule.tasks {
            // Skip year-end tasks if not year-end
            if scheduled_task.task.is_year_end_only() && !fiscal_period.is_year_end {
                let mut result = CloseTaskResult::new(
                    scheduled_task.task.clone(),
                    company_code.to_string(),
                    fiscal_period.clone(),
                );
                result.status = CloseTaskStatus::Skipped("Not year-end period".to_string());
                run.task_results.push(result);
                continue;
            }

            // Check dependencies
            let deps_met = scheduled_task.depends_on.iter().all(|dep| {
                run.task_results
                    .iter()
                    .any(|r| r.task == *dep && r.is_success())
            });

            if !deps_met {
                let mut result = CloseTaskResult::new(
                    scheduled_task.task.clone(),
                    company_code.to_string(),
                    fiscal_period.clone(),
                );
                result.status = CloseTaskStatus::Skipped("Dependencies not met".to_string());
                run.task_results.push(result);
                continue;
            }

            // Execute the task
            let result =
                self.execute_task(&scheduled_task.task, company_code, &fiscal_period, context);

            run.total_journal_entries += result.journal_entries_created;

            if let CloseTaskStatus::Failed(ref err) = result.status {
                run.errors
                    .push(format!("{}: {}", scheduled_task.task.name(), err));
                if self.config.stop_on_error {
                    run.task_results.push(result);
                    run.status = PeriodCloseStatus::Failed;
                    return run;
                }
            }

            run.task_results.push(result);
            _current_sequence = scheduled_task.sequence;
        }

        // Determine final status
        run.completed_at = Some(fiscal_period.end_date);
        if run.errors.is_empty() {
            run.status = PeriodCloseStatus::Completed;
        } else {
            run.status = PeriodCloseStatus::CompletedWithErrors;
        }

        run
    }

    /// Executes a single close task.
    fn execute_task(
        &self,
        task: &CloseTask,
        company_code: &str,
        fiscal_period: &FiscalPeriod,
        context: &mut CloseContext,
    ) -> CloseTaskResult {
        let mut result = CloseTaskResult::new(
            task.clone(),
            company_code.to_string(),
            fiscal_period.clone(),
        );
        result.status = CloseTaskStatus::InProgress;
        result.started_at = Some(fiscal_period.end_date);

        // Delegate to appropriate handler
        match task {
            CloseTask::RunDepreciation => {
                if let Some(handler) = &context.depreciation_handler {
                    let (entries, total) = handler(company_code, fiscal_period);
                    result.journal_entries_created = entries.len() as u32;
                    result.total_amount = total;
                    context.journal_entries.extend(entries);
                    result.status = CloseTaskStatus::Completed;
                } else {
                    result.status = CloseTaskStatus::Skipped("No depreciation handler".to_string());
                }
            }
            CloseTask::PostAccruedExpenses | CloseTask::PostAccruedRevenue => {
                if let Some(handler) = &context.accrual_handler {
                    let (entries, total) = handler(company_code, fiscal_period, task);
                    result.journal_entries_created = entries.len() as u32;
                    result.total_amount = total;
                    context.journal_entries.extend(entries);
                    result.status = CloseTaskStatus::Completed;
                } else {
                    result.status = CloseTaskStatus::Skipped("No accrual handler".to_string());
                }
            }
            CloseTask::PostPrepaidAmortization => {
                if let Some(handler) = &context.prepaid_handler {
                    let (entries, total) = handler(company_code, fiscal_period);
                    result.journal_entries_created = entries.len() as u32;
                    result.total_amount = total;
                    context.journal_entries.extend(entries);
                    result.status = CloseTaskStatus::Completed;
                } else {
                    result.status = CloseTaskStatus::Skipped("No prepaid handler".to_string());
                }
            }
            CloseTask::ReconcileArToGl
            | CloseTask::ReconcileApToGl
            | CloseTask::ReconcileFaToGl
            | CloseTask::ReconcileInventoryToGl => {
                if let Some(handler) = &context.reconciliation_handler {
                    match handler(company_code, fiscal_period, task) {
                        Ok(diff) => {
                            if diff.abs() <= self.config.reconciliation_tolerance {
                                result.status = CloseTaskStatus::Completed;
                            } else if self.config.require_reconciliation {
                                result.status = CloseTaskStatus::Failed(format!(
                                    "Reconciliation difference: {diff}"
                                ));
                            } else {
                                result.status =
                                    CloseTaskStatus::CompletedWithWarnings(vec![format!(
                                        "Reconciliation difference: {}",
                                        diff
                                    )]);
                            }
                            result.total_amount = diff;
                        }
                        Err(e) => {
                            result.status = CloseTaskStatus::Failed(e);
                        }
                    }
                } else {
                    result.status =
                        CloseTaskStatus::Skipped("No reconciliation handler".to_string());
                }
            }
            CloseTask::RevalueForeignCurrency => {
                if let Some(handler) = &context.fx_revaluation_handler {
                    let (entries, total) = handler(company_code, fiscal_period);
                    result.journal_entries_created = entries.len() as u32;
                    result.total_amount = total;
                    context.journal_entries.extend(entries);
                    result.status = CloseTaskStatus::Completed;
                } else {
                    result.status =
                        CloseTaskStatus::Skipped("No FX revaluation handler".to_string());
                }
            }
            CloseTask::AllocateCorporateOverhead => {
                if let Some(handler) = &context.overhead_handler {
                    let (entries, total) = handler(company_code, fiscal_period);
                    result.journal_entries_created = entries.len() as u32;
                    result.total_amount = total;
                    context.journal_entries.extend(entries);
                    result.status = CloseTaskStatus::Completed;
                } else {
                    result.status = CloseTaskStatus::Skipped("No overhead handler".to_string());
                }
            }
            CloseTask::PostIntercompanySettlements => {
                if let Some(handler) = &context.ic_settlement_handler {
                    let (entries, total) = handler(company_code, fiscal_period);
                    result.journal_entries_created = entries.len() as u32;
                    result.total_amount = total;
                    context.journal_entries.extend(entries);
                    result.status = CloseTaskStatus::Completed;
                } else {
                    result.status =
                        CloseTaskStatus::Skipped("No IC settlement handler".to_string());
                }
            }
            CloseTask::TranslateForeignSubsidiaries => {
                if let Some(handler) = &context.translation_handler {
                    let (entries, total) = handler(company_code, fiscal_period);
                    result.journal_entries_created = entries.len() as u32;
                    result.total_amount = total;
                    context.journal_entries.extend(entries);
                    result.status = CloseTaskStatus::Completed;
                } else {
                    result.status = CloseTaskStatus::Skipped("No translation handler".to_string());
                }
            }
            CloseTask::EliminateIntercompany => {
                if let Some(handler) = &context.elimination_handler {
                    let (entries, total) = handler(company_code, fiscal_period);
                    result.journal_entries_created = entries.len() as u32;
                    result.total_amount = total;
                    context.journal_entries.extend(entries);
                    result.status = CloseTaskStatus::Completed;
                } else {
                    result.status = CloseTaskStatus::Skipped("No elimination handler".to_string());
                }
            }
            CloseTask::CalculateTaxProvision => {
                if let Some(handler) = &context.tax_provision_handler {
                    let (entries, total) = handler(company_code, fiscal_period);
                    result.journal_entries_created = entries.len() as u32;
                    result.total_amount = total;
                    context.journal_entries.extend(entries);
                    result.status = CloseTaskStatus::Completed;
                } else {
                    result.status =
                        CloseTaskStatus::Skipped("No tax provision handler".to_string());
                }
            }
            CloseTask::CloseIncomeStatement => {
                if let Some(handler) = &context.income_close_handler {
                    let (entries, total) = handler(company_code, fiscal_period);
                    result.journal_entries_created = entries.len() as u32;
                    result.total_amount = total;
                    context.journal_entries.extend(entries);
                    result.status = CloseTaskStatus::Completed;
                } else {
                    result.status = CloseTaskStatus::Skipped("No income close handler".to_string());
                }
            }
            CloseTask::PostRetainedEarningsRollforward => {
                if let Some(handler) = &context.re_rollforward_handler {
                    let (entries, total) = handler(company_code, fiscal_period);
                    result.journal_entries_created = entries.len() as u32;
                    result.total_amount = total;
                    context.journal_entries.extend(entries);
                    result.status = CloseTaskStatus::Completed;
                } else {
                    result.status =
                        CloseTaskStatus::Skipped("No RE rollforward handler".to_string());
                }
            }
            CloseTask::GenerateTrialBalance | CloseTask::GenerateFinancialStatements => {
                // These are reporting tasks, not JE generators
                result.status = CloseTaskStatus::Completed;
                result.notes.push("Report generation completed".to_string());
            }
            CloseTask::PostInventoryRevaluation => {
                if let Some(handler) = &context.inventory_reval_handler {
                    let (entries, total) = handler(company_code, fiscal_period);
                    result.journal_entries_created = entries.len() as u32;
                    result.total_amount = total;
                    context.journal_entries.extend(entries);
                    result.status = CloseTaskStatus::Completed;
                } else {
                    result.status =
                        CloseTaskStatus::Skipped("No inventory reval handler".to_string());
                }
            }
            CloseTask::Custom(name) => {
                if let Some(handler) = context.custom_handlers.get(name) {
                    let (entries, total) = handler(company_code, fiscal_period);
                    result.journal_entries_created = entries.len() as u32;
                    result.total_amount = total;
                    context.journal_entries.extend(entries);
                    result.status = CloseTaskStatus::Completed;
                } else {
                    // Return error status instead of silently skipping
                    result.status = CloseTaskStatus::Failed(format!(
                        "Custom close task '{name}' has no registered handler. \
                         Register a handler via CloseContext.custom_handlers.insert(\"{name}\",...)"
                    ));
                }
            }
        }

        result.completed_at = Some(fiscal_period.end_date);
        result
    }

    /// Validates that a period can be closed.
    pub fn validate_close_readiness(
        &self,
        company_code: &str,
        fiscal_period: &FiscalPeriod,
        context: &CloseContext,
    ) -> CloseReadinessResult {
        let mut result = CloseReadinessResult {
            company_code: company_code.to_string(),
            fiscal_period: fiscal_period.clone(),
            is_ready: true,
            blockers: Vec::new(),
            warnings: Vec::new(),
        };

        // Check period status
        if fiscal_period.status == PeriodStatus::Closed {
            result.is_ready = false;
            result.blockers.push("Period is already closed".to_string());
        }

        if fiscal_period.status == PeriodStatus::Locked {
            result.is_ready = false;
            result
                .blockers
                .push("Period is locked for audit".to_string());
        }

        // Check for required handlers
        if context.depreciation_handler.is_none() {
            result
                .warnings
                .push("No depreciation handler configured".to_string());
        }

        if context.accrual_handler.is_none() {
            result
                .warnings
                .push("No accrual handler configured".to_string());
        }

        if self.config.require_reconciliation && context.reconciliation_handler.is_none() {
            result.is_ready = false;
            result
                .blockers
                .push("Reconciliation required but no handler configured".to_string());
        }

        result
    }
}

/// Context for close execution containing handlers and state.
#[derive(Default)]
pub struct CloseContext {
    /// Journal entries generated during close.
    pub journal_entries: Vec<JournalEntry>,
    /// Handler for depreciation.
    pub depreciation_handler:
        Option<Box<dyn Fn(&str, &FiscalPeriod) -> (Vec<JournalEntry>, Decimal)>>,
    /// Handler for accruals.
    pub accrual_handler:
        Option<Box<dyn Fn(&str, &FiscalPeriod, &CloseTask) -> (Vec<JournalEntry>, Decimal)>>,
    /// Handler for prepaid amortization.
    pub prepaid_handler: Option<Box<dyn Fn(&str, &FiscalPeriod) -> (Vec<JournalEntry>, Decimal)>>,
    /// Handler for reconciliation.
    pub reconciliation_handler:
        Option<Box<dyn Fn(&str, &FiscalPeriod, &CloseTask) -> Result<Decimal, String>>>,
    /// Handler for FX revaluation.
    pub fx_revaluation_handler:
        Option<Box<dyn Fn(&str, &FiscalPeriod) -> (Vec<JournalEntry>, Decimal)>>,
    /// Handler for overhead allocation.
    pub overhead_handler: Option<Box<dyn Fn(&str, &FiscalPeriod) -> (Vec<JournalEntry>, Decimal)>>,
    /// Handler for IC settlements.
    pub ic_settlement_handler:
        Option<Box<dyn Fn(&str, &FiscalPeriod) -> (Vec<JournalEntry>, Decimal)>>,
    /// Handler for currency translation.
    pub translation_handler:
        Option<Box<dyn Fn(&str, &FiscalPeriod) -> (Vec<JournalEntry>, Decimal)>>,
    /// Handler for IC elimination.
    pub elimination_handler:
        Option<Box<dyn Fn(&str, &FiscalPeriod) -> (Vec<JournalEntry>, Decimal)>>,
    /// Handler for tax provision.
    pub tax_provision_handler:
        Option<Box<dyn Fn(&str, &FiscalPeriod) -> (Vec<JournalEntry>, Decimal)>>,
    /// Handler for income statement close.
    pub income_close_handler:
        Option<Box<dyn Fn(&str, &FiscalPeriod) -> (Vec<JournalEntry>, Decimal)>>,
    /// Handler for retained earnings rollforward.
    pub re_rollforward_handler:
        Option<Box<dyn Fn(&str, &FiscalPeriod) -> (Vec<JournalEntry>, Decimal)>>,
    /// Handler for inventory revaluation.
    pub inventory_reval_handler:
        Option<Box<dyn Fn(&str, &FiscalPeriod) -> (Vec<JournalEntry>, Decimal)>>,
    /// Handlers for custom close tasks, keyed by task name.
    pub custom_handlers: std::collections::HashMap<
        String,
        Box<dyn Fn(&str, &FiscalPeriod) -> (Vec<JournalEntry>, Decimal)>,
    >,
}

/// Result of close readiness validation.
#[derive(Debug, Clone)]
pub struct CloseReadinessResult {
    /// Company code.
    pub company_code: String,
    /// Fiscal period.
    pub fiscal_period: FiscalPeriod,
    /// Whether the period is ready to close.
    pub is_ready: bool,
    /// Blocking issues that prevent close.
    pub blockers: Vec<String>,
    /// Non-blocking warnings.
    pub warnings: Vec<String>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn test_close_engine_creation() {
        let engine = CloseEngine::new(CloseEngineConfig::default());
        assert!(!engine.config.stop_on_error);
    }

    #[test]
    fn test_close_readiness() {
        let engine = CloseEngine::new(CloseEngineConfig::default());
        let period = FiscalPeriod::monthly(2024, 1);
        let context = CloseContext::default();

        let result = engine.validate_close_readiness("1000", &period, &context);
        // Without reconciliation handler, should not be ready
        assert!(!result.is_ready);
    }
}