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
//! Project and WBS (Work Breakdown Structure) models.
//!
//! Provides project master data for capital projects, internal projects,
//! and associated WBS elements for cost tracking.

use rand::seq::IndexedRandom;
use rand::Rng;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Type of project.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ProjectType {
    /// Capital expenditure project (assets)
    #[default]
    Capital,
    /// Internal project (expensed)
    Internal,
    /// Research and development
    RandD,
    /// Customer project (billable)
    Customer,
    /// Maintenance project
    Maintenance,
    /// IT/Technology project
    Technology,
}

impl ProjectType {
    /// Check if this project type typically capitalizes costs.
    pub fn is_capitalizable(&self) -> bool {
        matches!(self, Self::Capital | Self::RandD)
    }

    /// Get typical account type for this project.
    pub fn typical_account_prefix(&self) -> &'static str {
        match self {
            Self::Capital => "1",     // Assets
            Self::Internal => "5",    // Expenses
            Self::RandD => "1",       // Assets (capitalized) or "5" (expensed)
            Self::Customer => "4",    // Revenue
            Self::Maintenance => "5", // Expenses
            Self::Technology => "1",  // Assets (often capitalized)
        }
    }
}

/// Status of a project.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ProjectStatus {
    /// Project is planned but not started
    #[default]
    Planned,
    /// Project is active
    Active,
    /// Project is on hold
    OnHold,
    /// Project is complete
    Completed,
    /// Project was cancelled
    Cancelled,
    /// Project is in closing phase
    Closing,
}

impl ProjectStatus {
    /// Check if project can receive postings.
    pub fn allows_postings(&self) -> bool {
        matches!(self, Self::Active | Self::Closing)
    }
}

/// WBS (Work Breakdown Structure) element.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WbsElement {
    /// WBS element ID (e.g., "P-001.01.001")
    pub wbs_id: String,

    /// Parent project ID
    pub project_id: String,

    /// Description
    pub description: String,

    /// Level in the hierarchy (1 = top level)
    pub level: u8,

    /// Parent WBS element ID (None for top-level)
    pub parent_wbs: Option<String>,

    /// Budget amount
    pub budget: Decimal,

    /// Actual costs to date
    pub actual_costs: Decimal,

    /// Is this element active for postings
    pub is_active: bool,

    /// Responsible cost center
    pub responsible_cost_center: Option<String>,
}

impl WbsElement {
    /// Create a new WBS element.
    pub fn new(wbs_id: &str, project_id: &str, description: &str) -> Self {
        Self {
            wbs_id: wbs_id.to_string(),
            project_id: project_id.to_string(),
            description: description.to_string(),
            level: 1,
            parent_wbs: None,
            budget: Decimal::ZERO,
            actual_costs: Decimal::ZERO,
            is_active: true,
            responsible_cost_center: None,
        }
    }

    /// Set the level and parent.
    pub fn with_parent(mut self, parent_wbs: &str, level: u8) -> Self {
        self.parent_wbs = Some(parent_wbs.to_string());
        self.level = level;
        self
    }

    /// Set the budget.
    pub fn with_budget(mut self, budget: Decimal) -> Self {
        self.budget = budget;
        self
    }

    /// Calculate remaining budget.
    pub fn remaining_budget(&self) -> Decimal {
        self.budget - self.actual_costs
    }

    /// Check if over budget.
    pub fn is_over_budget(&self) -> bool {
        self.actual_costs > self.budget
    }
}

/// Project master data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Project {
    /// Project ID (e.g., "P-001234")
    pub project_id: String,

    /// Project name
    pub name: String,

    /// Project description
    pub description: String,

    /// Type of project
    pub project_type: ProjectType,

    /// Current status
    pub status: ProjectStatus,

    /// Total budget
    pub budget: Decimal,

    /// Responsible cost center
    pub responsible_cost_center: String,

    /// WBS elements
    pub wbs_elements: Vec<WbsElement>,

    /// Company code
    pub company_code: String,

    /// Start date (YYYY-MM-DD)
    pub start_date: Option<String>,

    /// End date (YYYY-MM-DD)
    pub end_date: Option<String>,
}

impl Project {
    /// Create a new project.
    pub fn new(project_id: &str, name: &str, project_type: ProjectType) -> Self {
        Self {
            project_id: project_id.to_string(),
            name: name.to_string(),
            description: String::new(),
            project_type,
            status: ProjectStatus::Active,
            budget: Decimal::ZERO,
            responsible_cost_center: "1000".to_string(),
            wbs_elements: Vec::new(),
            company_code: "1000".to_string(),
            start_date: None,
            end_date: None,
        }
    }

    /// Set the budget.
    pub fn with_budget(mut self, budget: Decimal) -> Self {
        self.budget = budget;
        self
    }

    /// Set the company code.
    pub fn with_company(mut self, company_code: &str) -> Self {
        self.company_code = company_code.to_string();
        self
    }

    /// Add a WBS element.
    pub fn add_wbs_element(&mut self, element: WbsElement) {
        self.wbs_elements.push(element);
    }

    /// Get active WBS elements.
    pub fn active_wbs_elements(&self) -> Vec<&WbsElement> {
        self.wbs_elements.iter().filter(|w| w.is_active).collect()
    }

    /// Check if project allows postings.
    pub fn allows_postings(&self) -> bool {
        self.status.allows_postings()
    }

    /// Get total actual costs across all WBS elements.
    pub fn total_actual_costs(&self) -> Decimal {
        self.wbs_elements.iter().map(|w| w.actual_costs).sum()
    }

    /// Check if project is over budget.
    pub fn is_over_budget(&self) -> bool {
        self.total_actual_costs() > self.budget
    }
}

/// Pool of projects for transaction generation.
#[derive(Debug, Clone, Default)]
pub struct ProjectPool {
    /// All projects
    pub projects: Vec<Project>,
    /// Index by project type
    type_index: HashMap<ProjectType, Vec<usize>>,
}

impl ProjectPool {
    /// Create a new empty project pool.
    pub fn new() -> Self {
        Self {
            projects: Vec::new(),
            type_index: HashMap::new(),
        }
    }

    /// Add a project to the pool.
    pub fn add_project(&mut self, project: Project) {
        let idx = self.projects.len();
        let project_type = project.project_type;
        self.projects.push(project);
        self.type_index.entry(project_type).or_default().push(idx);
    }

    /// Get a random active project.
    pub fn random_active_project(&self, rng: &mut impl Rng) -> Option<&Project> {
        let active: Vec<_> = self
            .projects
            .iter()
            .filter(|p| p.allows_postings())
            .collect();
        active.choose(rng).copied()
    }

    /// Get a random project of a specific type.
    pub fn random_project_of_type(
        &self,
        project_type: ProjectType,
        rng: &mut impl Rng,
    ) -> Option<&Project> {
        self.type_index
            .get(&project_type)
            .and_then(|indices| indices.choose(rng))
            .map(|&idx| &self.projects[idx])
            .filter(|p| p.allows_postings())
    }

    /// Rebuild the type index.
    pub fn rebuild_index(&mut self) {
        self.type_index.clear();
        for (idx, project) in self.projects.iter().enumerate() {
            self.type_index
                .entry(project.project_type)
                .or_default()
                .push(idx);
        }
    }

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

        // Capital projects
        let capital_projects = [
            (
                "PRJ-CAP-001",
                "Data Center Expansion",
                Decimal::from(5000000),
            ),
            (
                "PRJ-CAP-002",
                "Manufacturing Line Upgrade",
                Decimal::from(2500000),
            ),
            (
                "PRJ-CAP-003",
                "Office Building Renovation",
                Decimal::from(1500000),
            ),
            (
                "PRJ-CAP-004",
                "Fleet Vehicle Replacement",
                Decimal::from(800000),
            ),
            (
                "PRJ-CAP-005",
                "Warehouse Automation",
                Decimal::from(3000000),
            ),
        ];

        for (id, name, budget) in capital_projects {
            let mut project = Project::new(id, name, ProjectType::Capital)
                .with_budget(budget)
                .with_company(company_code);

            // Add WBS elements
            project.add_wbs_element(
                WbsElement::new(&format!("{id}.01"), id, "Planning & Design").with_budget(
                    budget * Decimal::from_f64_retain(0.1).expect("valid decimal fraction"),
                ),
            );
            project.add_wbs_element(
                WbsElement::new(&format!("{id}.02"), id, "Procurement").with_budget(
                    budget * Decimal::from_f64_retain(0.4).expect("valid decimal fraction"),
                ),
            );
            project.add_wbs_element(
                WbsElement::new(&format!("{id}.03"), id, "Implementation").with_budget(
                    budget * Decimal::from_f64_retain(0.4).expect("valid decimal fraction"),
                ),
            );
            project.add_wbs_element(
                WbsElement::new(&format!("{id}.04"), id, "Testing & Validation").with_budget(
                    budget * Decimal::from_f64_retain(0.1).expect("valid decimal fraction"),
                ),
            );

            pool.add_project(project);
        }

        // Internal projects
        let internal_projects = [
            (
                "PRJ-INT-001",
                "Process Improvement Initiative",
                Decimal::from(250000),
            ),
            (
                "PRJ-INT-002",
                "Employee Training Program",
                Decimal::from(150000),
            ),
            (
                "PRJ-INT-003",
                "Quality Certification",
                Decimal::from(100000),
            ),
        ];

        for (id, name, budget) in internal_projects {
            let mut project = Project::new(id, name, ProjectType::Internal)
                .with_budget(budget)
                .with_company(company_code);

            project.add_wbs_element(
                WbsElement::new(&format!("{id}.01"), id, "Phase 1").with_budget(
                    budget * Decimal::from_f64_retain(0.5).expect("valid decimal fraction"),
                ),
            );
            project.add_wbs_element(
                WbsElement::new(&format!("{id}.02"), id, "Phase 2").with_budget(
                    budget * Decimal::from_f64_retain(0.5).expect("valid decimal fraction"),
                ),
            );

            pool.add_project(project);
        }

        // Technology projects
        let tech_projects = [
            (
                "PRJ-IT-001",
                "ERP System Implementation",
                Decimal::from(2000000),
            ),
            ("PRJ-IT-002", "Cloud Migration", Decimal::from(1000000)),
            (
                "PRJ-IT-003",
                "Cybersecurity Enhancement",
                Decimal::from(500000),
            ),
        ];

        for (id, name, budget) in tech_projects {
            let mut project = Project::new(id, name, ProjectType::Technology)
                .with_budget(budget)
                .with_company(company_code);

            project.add_wbs_element(
                WbsElement::new(&format!("{id}.01"), id, "Assessment").with_budget(
                    budget * Decimal::from_f64_retain(0.15).expect("valid decimal fraction"),
                ),
            );
            project.add_wbs_element(
                WbsElement::new(&format!("{id}.02"), id, "Development").with_budget(
                    budget * Decimal::from_f64_retain(0.50).expect("valid decimal fraction"),
                ),
            );
            project.add_wbs_element(
                WbsElement::new(&format!("{id}.03"), id, "Deployment").with_budget(
                    budget * Decimal::from_f64_retain(0.25).expect("valid decimal fraction"),
                ),
            );
            project.add_wbs_element(
                WbsElement::new(&format!("{id}.04"), id, "Support").with_budget(
                    budget * Decimal::from_f64_retain(0.10).expect("valid decimal fraction"),
                ),
            );

            pool.add_project(project);
        }

        pool
    }
}

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

    #[test]
    fn test_project_creation() {
        let project = Project::new("P-001", "Test Project", ProjectType::Capital)
            .with_budget(Decimal::from(1000000));

        assert_eq!(project.project_id, "P-001");
        assert!(project.allows_postings());
        assert!(project.project_type.is_capitalizable());
    }

    #[test]
    fn test_wbs_element() {
        let wbs =
            WbsElement::new("P-001.01", "P-001", "Phase 1").with_budget(Decimal::from(100000));

        assert_eq!(wbs.remaining_budget(), Decimal::from(100000));
        assert!(!wbs.is_over_budget());
    }

    #[test]
    fn test_project_pool() {
        let pool = ProjectPool::standard("1000");

        assert!(!pool.projects.is_empty());

        let mut rng = ChaCha8Rng::seed_from_u64(42);
        let project = pool.random_active_project(&mut rng);
        assert!(project.is_some());

        let cap_project = pool.random_project_of_type(ProjectType::Capital, &mut rng);
        assert!(cap_project.is_some());
    }

    #[test]
    fn test_project_budget_tracking() {
        let mut project =
            Project::new("P-001", "Test", ProjectType::Capital).with_budget(Decimal::from(100000));

        let mut wbs =
            WbsElement::new("P-001.01", "P-001", "Phase 1").with_budget(Decimal::from(100000));
        wbs.actual_costs = Decimal::from(50000);
        project.add_wbs_element(wbs);

        assert_eq!(project.total_actual_costs(), Decimal::from(50000));
        assert!(!project.is_over_budget());
    }
}