datasynth-core 2.3.0

Core domain models, traits, and distributions for synthetic enterprise data generation
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Department and organizational structure models.
//!
//! Provides department definitions with associated cost centers,
//! business processes, and typical user personas.

use crate::models::{BusinessProcess, UserPersona};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Department definition for organizational structure.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Department {
    /// Department code (e.g., "FIN", "AP", "AR")
    pub code: String,

    /// Department name (e.g., "Finance", "Accounts Payable")
    pub name: String,

    /// Parent department code (for hierarchy)
    pub parent_code: Option<String>,

    /// Associated cost center
    pub cost_center: String,

    /// Typical user personas in this department
    pub typical_personas: Vec<UserPersona>,

    /// Primary business processes handled
    pub primary_processes: Vec<BusinessProcess>,

    /// Standard headcount for this department
    pub standard_headcount: DepartmentHeadcount,

    /// Is this department active
    pub is_active: bool,
}

impl Department {
    /// Create a new department.
    pub fn new(code: &str, name: &str, cost_center: &str) -> Self {
        Self {
            code: code.to_string(),
            name: name.to_string(),
            parent_code: None,
            cost_center: cost_center.to_string(),
            typical_personas: Vec::new(),
            primary_processes: Vec::new(),
            standard_headcount: DepartmentHeadcount::default(),
            is_active: true,
        }
    }

    /// Set parent department.
    pub fn with_parent(mut self, parent_code: &str) -> Self {
        self.parent_code = Some(parent_code.to_string());
        self
    }

    /// Add typical personas.
    pub fn with_personas(mut self, personas: Vec<UserPersona>) -> Self {
        self.typical_personas = personas;
        self
    }

    /// Add primary business processes.
    pub fn with_processes(mut self, processes: Vec<BusinessProcess>) -> Self {
        self.primary_processes = processes;
        self
    }

    /// Set headcount.
    pub fn with_headcount(mut self, headcount: DepartmentHeadcount) -> Self {
        self.standard_headcount = headcount;
        self
    }

    /// Check if this department handles a specific business process.
    pub fn handles_process(&self, process: BusinessProcess) -> bool {
        self.primary_processes.contains(&process)
    }

    /// Check if a persona is typical for this department.
    pub fn is_typical_persona(&self, persona: UserPersona) -> bool {
        self.typical_personas.contains(&persona)
    }
}

/// Headcount configuration for a department.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DepartmentHeadcount {
    /// Number of junior accountants
    pub junior_accountant: usize,
    /// Number of senior accountants
    pub senior_accountant: usize,
    /// Number of controllers
    pub controller: usize,
    /// Number of managers
    pub manager: usize,
    /// Number of executives (usually 0 or 1)
    pub executive: usize,
    /// Number of automated systems/batch jobs
    pub automated_system: usize,
}

impl Default for DepartmentHeadcount {
    fn default() -> Self {
        Self {
            junior_accountant: 2,
            senior_accountant: 1,
            controller: 0,
            manager: 0,
            executive: 0,
            automated_system: 1,
        }
    }
}

impl DepartmentHeadcount {
    /// Create an empty headcount.
    pub fn empty() -> Self {
        Self {
            junior_accountant: 0,
            senior_accountant: 0,
            controller: 0,
            manager: 0,
            executive: 0,
            automated_system: 0,
        }
    }

    /// Total headcount.
    pub fn total(&self) -> usize {
        self.junior_accountant
            + self.senior_accountant
            + self.controller
            + self.manager
            + self.executive
            + self.automated_system
    }

    /// Apply a multiplier to all counts.
    pub fn scaled(&self, multiplier: f64) -> Self {
        Self {
            junior_accountant: (self.junior_accountant as f64 * multiplier).round() as usize,
            senior_accountant: (self.senior_accountant as f64 * multiplier).round() as usize,
            controller: (self.controller as f64 * multiplier).round() as usize,
            manager: (self.manager as f64 * multiplier).round() as usize,
            executive: (self.executive as f64 * multiplier).round() as usize,
            automated_system: (self.automated_system as f64 * multiplier).round() as usize,
        }
    }
}

/// Organization structure containing all departments.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrganizationStructure {
    /// Company code this structure belongs to
    pub company_code: String,

    /// All departments in the organization
    pub departments: Vec<Department>,

    /// Index by department code for quick lookup
    #[serde(skip)]
    department_index: HashMap<String, usize>,
}

impl OrganizationStructure {
    /// Create a new empty organization structure.
    pub fn new(company_code: &str) -> Self {
        Self {
            company_code: company_code.to_string(),
            departments: Vec::new(),
            department_index: HashMap::new(),
        }
    }

    /// Add a department to the structure.
    pub fn add_department(&mut self, department: Department) {
        let idx = self.departments.len();
        self.department_index.insert(department.code.clone(), idx);
        self.departments.push(department);
    }

    /// Get a department by code.
    pub fn get_department(&self, code: &str) -> Option<&Department> {
        self.department_index
            .get(code)
            .map(|&idx| &self.departments[idx])
    }

    /// Get departments that handle a specific business process.
    pub fn get_departments_for_process(&self, process: BusinessProcess) -> Vec<&Department> {
        self.departments
            .iter()
            .filter(|d| d.handles_process(process))
            .collect()
    }

    /// Get departments with a specific persona type.
    pub fn get_departments_for_persona(&self, persona: UserPersona) -> Vec<&Department> {
        self.departments
            .iter()
            .filter(|d| d.is_typical_persona(persona))
            .collect()
    }

    /// Rebuild the index (call after deserialization).
    pub fn rebuild_index(&mut self) {
        self.department_index.clear();
        for (idx, dept) in self.departments.iter().enumerate() {
            self.department_index.insert(dept.code.clone(), idx);
        }
    }

    /// Get total headcount across all departments.
    pub fn total_headcount(&self) -> usize {
        self.departments
            .iter()
            .map(|d| d.standard_headcount.total())
            .sum()
    }

    /// Generate a standard organization structure.
    pub fn standard(company_code: &str) -> Self {
        let mut org = Self::new(company_code);

        // Finance department (parent)
        org.add_department(
            Department::new("FIN", "Finance", "1000")
                .with_personas(vec![
                    UserPersona::Controller,
                    UserPersona::SeniorAccountant,
                    UserPersona::Manager,
                    UserPersona::Executive,
                ])
                .with_processes(vec![BusinessProcess::R2R])
                .with_headcount(DepartmentHeadcount {
                    junior_accountant: 0,
                    senior_accountant: 2,
                    controller: 2,
                    manager: 1,
                    executive: 1,
                    automated_system: 2,
                }),
        );

        // Accounts Payable
        org.add_department(
            Department::new("AP", "Accounts Payable", "1100")
                .with_parent("FIN")
                .with_personas(vec![
                    UserPersona::JuniorAccountant,
                    UserPersona::SeniorAccountant,
                ])
                .with_processes(vec![BusinessProcess::P2P])
                .with_headcount(DepartmentHeadcount {
                    junior_accountant: 5,
                    senior_accountant: 2,
                    controller: 0,
                    manager: 1,
                    executive: 0,
                    automated_system: 5,
                }),
        );

        // Accounts Receivable
        org.add_department(
            Department::new("AR", "Accounts Receivable", "1200")
                .with_parent("FIN")
                .with_personas(vec![
                    UserPersona::JuniorAccountant,
                    UserPersona::SeniorAccountant,
                ])
                .with_processes(vec![BusinessProcess::O2C])
                .with_headcount(DepartmentHeadcount {
                    junior_accountant: 4,
                    senior_accountant: 2,
                    controller: 0,
                    manager: 1,
                    executive: 0,
                    automated_system: 5,
                }),
        );

        // General Ledger
        org.add_department(
            Department::new("GL", "General Ledger", "1300")
                .with_parent("FIN")
                .with_personas(vec![UserPersona::SeniorAccountant, UserPersona::Controller])
                .with_processes(vec![BusinessProcess::R2R])
                .with_headcount(DepartmentHeadcount {
                    junior_accountant: 2,
                    senior_accountant: 3,
                    controller: 1,
                    manager: 0,
                    executive: 0,
                    automated_system: 3,
                }),
        );

        // Payroll / HR Accounting
        org.add_department(
            Department::new("HR", "Human Resources", "2000")
                .with_personas(vec![
                    UserPersona::JuniorAccountant,
                    UserPersona::SeniorAccountant,
                ])
                .with_processes(vec![BusinessProcess::H2R])
                .with_headcount(DepartmentHeadcount {
                    junior_accountant: 2,
                    senior_accountant: 1,
                    controller: 0,
                    manager: 1,
                    executive: 0,
                    automated_system: 2,
                }),
        );

        // Fixed Assets
        org.add_department(
            Department::new("FA", "Fixed Assets", "1400")
                .with_parent("FIN")
                .with_personas(vec![
                    UserPersona::JuniorAccountant,
                    UserPersona::SeniorAccountant,
                ])
                .with_processes(vec![BusinessProcess::A2R])
                .with_headcount(DepartmentHeadcount {
                    junior_accountant: 1,
                    senior_accountant: 1,
                    controller: 0,
                    manager: 0,
                    executive: 0,
                    automated_system: 2,
                }),
        );

        // Treasury
        org.add_department(
            Department::new("TRE", "Treasury", "1500")
                .with_parent("FIN")
                .with_personas(vec![
                    UserPersona::SeniorAccountant,
                    UserPersona::Controller,
                    UserPersona::Manager,
                ])
                .with_processes(vec![BusinessProcess::Treasury])
                .with_headcount(DepartmentHeadcount {
                    junior_accountant: 0,
                    senior_accountant: 2,
                    controller: 1,
                    manager: 1,
                    executive: 0,
                    automated_system: 2,
                }),
        );

        // Tax
        org.add_department(
            Department::new("TAX", "Tax", "1600")
                .with_parent("FIN")
                .with_personas(vec![UserPersona::SeniorAccountant, UserPersona::Controller])
                .with_processes(vec![BusinessProcess::Tax])
                .with_headcount(DepartmentHeadcount {
                    junior_accountant: 1,
                    senior_accountant: 2,
                    controller: 1,
                    manager: 0,
                    executive: 0,
                    automated_system: 1,
                }),
        );

        // Procurement
        org.add_department(
            Department::new("PROC", "Procurement", "3000")
                .with_personas(vec![UserPersona::SeniorAccountant, UserPersona::Manager])
                .with_processes(vec![BusinessProcess::P2P])
                .with_headcount(DepartmentHeadcount {
                    junior_accountant: 2,
                    senior_accountant: 2,
                    controller: 0,
                    manager: 1,
                    executive: 0,
                    automated_system: 3,
                }),
        );

        // IT (batch jobs)
        org.add_department(
            Department::new("IT", "Information Technology", "4000")
                .with_personas(vec![UserPersona::AutomatedSystem])
                .with_processes(vec![BusinessProcess::R2R])
                .with_headcount(DepartmentHeadcount {
                    junior_accountant: 0,
                    senior_accountant: 0,
                    controller: 0,
                    manager: 0,
                    executive: 0,
                    automated_system: 10,
                }),
        );

        org
    }

    /// Generate a minimal organization structure for small companies.
    pub fn minimal(company_code: &str) -> Self {
        let mut org = Self::new(company_code);

        org.add_department(
            Department::new("FIN", "Finance", "1000")
                .with_personas(vec![
                    UserPersona::JuniorAccountant,
                    UserPersona::SeniorAccountant,
                    UserPersona::Controller,
                    UserPersona::Manager,
                ])
                .with_processes(vec![
                    BusinessProcess::O2C,
                    BusinessProcess::P2P,
                    BusinessProcess::R2R,
                    BusinessProcess::H2R,
                    BusinessProcess::A2R,
                ])
                .with_headcount(DepartmentHeadcount {
                    junior_accountant: 3,
                    senior_accountant: 2,
                    controller: 1,
                    manager: 1,
                    executive: 0,
                    automated_system: 5,
                }),
        );

        org
    }
}

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

    #[test]
    fn test_department_creation() {
        let dept = Department::new("FIN", "Finance", "1000")
            .with_personas(vec![UserPersona::Controller])
            .with_processes(vec![BusinessProcess::R2R]);

        assert_eq!(dept.code, "FIN");
        assert_eq!(dept.name, "Finance");
        assert!(dept.handles_process(BusinessProcess::R2R));
        assert!(!dept.handles_process(BusinessProcess::P2P));
        assert!(dept.is_typical_persona(UserPersona::Controller));
    }

    #[test]
    fn test_standard_organization() {
        let org = OrganizationStructure::standard("1000");

        assert!(!org.departments.is_empty());
        assert!(org.get_department("FIN").is_some());
        assert!(org.get_department("AP").is_some());
        assert!(org.get_department("AR").is_some());

        // Check process mapping
        let p2p_depts = org.get_departments_for_process(BusinessProcess::P2P);
        assert!(!p2p_depts.is_empty());

        // Check total headcount
        assert!(org.total_headcount() > 0);
    }

    #[test]
    fn test_headcount_scaling() {
        let headcount = DepartmentHeadcount {
            junior_accountant: 10,
            senior_accountant: 5,
            controller: 2,
            manager: 1,
            executive: 0,
            automated_system: 3,
        };

        let scaled = headcount.scaled(0.5);
        assert_eq!(scaled.junior_accountant, 5);
        assert_eq!(scaled.senior_accountant, 3); // 2.5 rounds to 3
        assert_eq!(scaled.controller, 1);
    }

    #[test]
    fn test_minimal_organization() {
        let org = OrganizationStructure::minimal("1000");

        assert_eq!(org.departments.len(), 1);
        let fin = org.get_department("FIN").unwrap();
        assert!(fin.handles_process(BusinessProcess::O2C));
        assert!(fin.handles_process(BusinessProcess::P2P));
    }
}