datasynth-core 2.4.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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
//! Material master data model.
//!
//! Provides material/product master data for realistic inventory
//! and procurement transaction generation.

use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::graph_properties::{GraphPropertyValue, ToNodeProperties};

/// Type of material in the system.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum MaterialType {
    /// Raw materials used in production
    #[default]
    RawMaterial,
    /// Semi-finished goods
    SemiFinished,
    /// Finished goods for sale
    FinishedGood,
    /// Trading goods (resale without transformation)
    TradingGood,
    /// Operating supplies (consumables)
    OperatingSupplies,
    /// Spare parts
    SparePart,
    /// Packaging material
    Packaging,
    /// Service (non-physical)
    Service,
}

impl MaterialType {
    /// Get the typical account category for this material type.
    pub fn inventory_account_category(&self) -> &'static str {
        match self {
            Self::RawMaterial => "Raw Materials Inventory",
            Self::SemiFinished => "Work in Progress",
            Self::FinishedGood => "Finished Goods Inventory",
            Self::TradingGood => "Trading Goods Inventory",
            Self::OperatingSupplies => "Supplies Inventory",
            Self::SparePart => "Spare Parts Inventory",
            Self::Packaging => "Packaging Materials",
            Self::Service => "N/A",
        }
    }

    /// Check if this material type has physical inventory.
    pub fn has_inventory(&self) -> bool {
        !matches!(self, Self::Service)
    }
}

/// Material group for categorization.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum MaterialGroup {
    /// Electronics and components
    #[default]
    Electronics,
    /// Mechanical parts
    Mechanical,
    /// Chemicals and raw materials
    Chemicals,
    /// Chemical (alias for Chemicals)
    Chemical,
    /// Office supplies
    OfficeSupplies,
    /// IT equipment
    ItEquipment,
    /// Furniture
    Furniture,
    /// Packaging materials
    PackagingMaterials,
    /// Safety equipment
    SafetyEquipment,
    /// Tools
    Tools,
    /// Services
    Services,
    /// Consumables
    Consumables,
    /// Finished goods
    FinishedGoods,
}

impl MaterialGroup {
    /// Get typical unit of measure for this material group.
    pub fn typical_uom(&self) -> &'static str {
        match self {
            Self::Electronics | Self::Mechanical | Self::ItEquipment => "EA",
            Self::Chemicals | Self::Chemical => "KG",
            Self::OfficeSupplies | Self::PackagingMaterials | Self::Consumables => "EA",
            Self::Furniture | Self::FinishedGoods => "EA",
            Self::SafetyEquipment | Self::Tools => "EA",
            Self::Services => "HR",
        }
    }
}

/// Inventory valuation method.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ValuationMethod {
    /// Standard cost valuation
    #[default]
    StandardCost,
    /// Moving average price
    MovingAverage,
    /// First-in, first-out
    Fifo,
    /// Last-in, first-out (where permitted)
    Lifo,
    /// Specific identification
    SpecificIdentification,
}

/// Unit of measure for materials.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct UnitOfMeasure {
    /// Unit code (e.g., "EA", "KG", "L")
    pub code: String,
    /// Full name
    pub name: String,
    /// Conversion factor to base unit (1.0 for base unit)
    pub conversion_factor: Decimal,
}

impl UnitOfMeasure {
    /// Create each (piece) unit.
    pub fn each() -> Self {
        Self {
            code: "EA".to_string(),
            name: "Each".to_string(),
            conversion_factor: Decimal::ONE,
        }
    }

    /// Create kilogram unit.
    pub fn kilogram() -> Self {
        Self {
            code: "KG".to_string(),
            name: "Kilogram".to_string(),
            conversion_factor: Decimal::ONE,
        }
    }

    /// Create liter unit.
    pub fn liter() -> Self {
        Self {
            code: "L".to_string(),
            name: "Liter".to_string(),
            conversion_factor: Decimal::ONE,
        }
    }

    /// Create hour unit (for services).
    pub fn hour() -> Self {
        Self {
            code: "HR".to_string(),
            name: "Hour".to_string(),
            conversion_factor: Decimal::ONE,
        }
    }
}

impl Default for UnitOfMeasure {
    fn default() -> Self {
        Self::each()
    }
}

/// Component in a bill of materials.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BomComponent {
    /// Component material ID
    pub component_material_id: String,
    /// Quantity required per parent unit
    pub quantity: Decimal,
    /// Unit of measure
    pub uom: String,
    /// Scrap percentage (waste factor)
    pub scrap_percentage: Decimal,
    /// Is this component optional?
    pub is_optional: bool,
    /// Position/sequence in BOM
    pub position: u16,

    // -- Graph export fields (DS-008) --
    /// Unique BOM line identifier
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// Company / entity code
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub entity_code: Option<String>,
    /// Parent (finished/semi-finished) material ID
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent_material: Option<String>,
    /// Component description
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub component_description: Option<String>,
    /// BOM level (1 = direct component, 2+ = sub-assembly)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub level: Option<u32>,
    /// Whether this is a phantom assembly (exploded during MRP)
    #[serde(default)]
    pub is_phantom: bool,
}

impl BomComponent {
    /// Create a new BOM component.
    pub fn new(
        component_material_id: impl Into<String>,
        quantity: Decimal,
        uom: impl Into<String>,
    ) -> Self {
        Self {
            component_material_id: component_material_id.into(),
            quantity,
            uom: uom.into(),
            scrap_percentage: Decimal::ZERO,
            is_optional: false,
            position: 0,
            id: None,
            entity_code: None,
            parent_material: None,
            component_description: None,
            level: None,
            is_phantom: false,
        }
    }

    /// Set scrap percentage.
    pub fn with_scrap(mut self, scrap_percentage: Decimal) -> Self {
        self.scrap_percentage = scrap_percentage;
        self
    }

    /// Calculate effective quantity including scrap.
    pub fn effective_quantity(&self) -> Decimal {
        self.quantity * (Decimal::ONE + self.scrap_percentage / Decimal::from(100))
    }
}

impl ToNodeProperties for BomComponent {
    fn node_type_name(&self) -> &'static str {
        "bom_component"
    }
    fn node_type_code(&self) -> u16 {
        343
    }
    fn to_node_properties(&self) -> HashMap<String, GraphPropertyValue> {
        let mut p = HashMap::new();
        if let Some(ref ec) = self.entity_code {
            p.insert("entityCode".into(), GraphPropertyValue::String(ec.clone()));
        }
        if let Some(ref pm) = self.parent_material {
            p.insert(
                "parentMaterial".into(),
                GraphPropertyValue::String(pm.clone()),
            );
        }
        p.insert(
            "componentMaterial".into(),
            GraphPropertyValue::String(self.component_material_id.clone()),
        );
        if let Some(ref desc) = self.component_description {
            p.insert(
                "componentDescription".into(),
                GraphPropertyValue::String(desc.clone()),
            );
        }
        if let Some(lvl) = self.level {
            p.insert("level".into(), GraphPropertyValue::Int(lvl as i64));
        }
        p.insert(
            "quantityPer".into(),
            GraphPropertyValue::Decimal(self.quantity),
        );
        p.insert("unit".into(), GraphPropertyValue::String(self.uom.clone()));
        p.insert(
            "scrapRate".into(),
            GraphPropertyValue::Decimal(self.scrap_percentage),
        );
        p.insert(
            "isPhantom".into(),
            GraphPropertyValue::Bool(self.is_phantom),
        );
        p
    }
}

/// Account determination rules for material transactions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MaterialAccountDetermination {
    /// Inventory account
    pub inventory_account: String,
    /// COGS account (for sales)
    pub cogs_account: String,
    /// Revenue account (for sales)
    pub revenue_account: String,
    /// Purchase expense account (for non-inventory items)
    pub purchase_expense_account: String,
    /// Price difference account
    pub price_difference_account: String,
    /// GR/IR clearing account
    pub gr_ir_account: String,
}

impl Default for MaterialAccountDetermination {
    fn default() -> Self {
        Self {
            inventory_account: "140000".to_string(),
            cogs_account: "500000".to_string(),
            revenue_account: "400000".to_string(),
            purchase_expense_account: "600000".to_string(),
            price_difference_account: "580000".to_string(),
            gr_ir_account: "290000".to_string(),
        }
    }
}

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

    /// Material description
    pub description: String,

    /// Type of material
    pub material_type: MaterialType,

    /// Material group
    pub material_group: MaterialGroup,

    /// Base unit of measure
    pub base_uom: UnitOfMeasure,

    /// Valuation method
    pub valuation_method: ValuationMethod,

    /// Standard cost per base unit
    pub standard_cost: Decimal,

    /// List price (selling price) per base unit
    pub list_price: Decimal,

    /// Purchase price per base unit
    pub purchase_price: Decimal,

    /// Bill of materials components (if this is a produced item)
    pub bom_components: Option<Vec<BomComponent>>,

    /// Account determination rules
    pub account_determination: MaterialAccountDetermination,

    /// Weight per base unit (kg)
    pub weight_kg: Option<Decimal>,

    /// Volume per base unit (m3)
    pub volume_m3: Option<Decimal>,

    /// Shelf life in days (for perishables)
    pub shelf_life_days: Option<u32>,

    /// Is this material active?
    pub is_active: bool,

    /// Company code (if material is company-specific)
    pub company_code: Option<String>,

    /// Plant/location codes where material is available
    pub plants: Vec<String>,

    /// Minimum order quantity
    pub min_order_quantity: Decimal,

    /// Lead time in days for procurement
    pub lead_time_days: u16,

    /// Safety stock quantity
    pub safety_stock: Decimal,

    /// Reorder point
    pub reorder_point: Decimal,

    /// Preferred vendor ID
    pub preferred_vendor_id: Option<String>,

    /// ABC classification (A=high value, C=low value)
    pub abc_classification: char,
}

impl Material {
    /// Create a new material with minimal required fields.
    pub fn new(
        material_id: impl Into<String>,
        description: impl Into<String>,
        material_type: MaterialType,
    ) -> Self {
        Self {
            material_id: material_id.into(),
            description: description.into(),
            material_type,
            material_group: MaterialGroup::default(),
            base_uom: UnitOfMeasure::default(),
            valuation_method: ValuationMethod::default(),
            standard_cost: Decimal::ZERO,
            list_price: Decimal::ZERO,
            purchase_price: Decimal::ZERO,
            bom_components: None,
            account_determination: MaterialAccountDetermination::default(),
            weight_kg: None,
            volume_m3: None,
            shelf_life_days: None,
            is_active: true,
            company_code: None,
            plants: vec!["1000".to_string()],
            min_order_quantity: Decimal::ONE,
            lead_time_days: 7,
            safety_stock: Decimal::ZERO,
            reorder_point: Decimal::ZERO,
            preferred_vendor_id: None,
            abc_classification: 'B',
        }
    }

    /// Set material group.
    pub fn with_group(mut self, group: MaterialGroup) -> Self {
        self.material_group = group;
        self
    }

    /// Set standard cost.
    pub fn with_standard_cost(mut self, cost: Decimal) -> Self {
        self.standard_cost = cost;
        self
    }

    /// Set list price.
    pub fn with_list_price(mut self, price: Decimal) -> Self {
        self.list_price = price;
        self
    }

    /// Set purchase price.
    pub fn with_purchase_price(mut self, price: Decimal) -> Self {
        self.purchase_price = price;
        self
    }

    /// Set BOM components.
    pub fn with_bom(mut self, components: Vec<BomComponent>) -> Self {
        self.bom_components = Some(components);
        self
    }

    /// Set company code.
    pub fn with_company_code(mut self, code: impl Into<String>) -> Self {
        self.company_code = Some(code.into());
        self
    }

    /// Set preferred vendor.
    pub fn with_preferred_vendor(mut self, vendor_id: impl Into<String>) -> Self {
        self.preferred_vendor_id = Some(vendor_id.into());
        self
    }

    /// Set ABC classification.
    pub fn with_abc_classification(mut self, classification: char) -> Self {
        self.abc_classification = classification;
        self
    }

    /// Calculate the theoretical cost from BOM.
    pub fn calculate_bom_cost(
        &self,
        component_costs: &std::collections::HashMap<String, Decimal>,
    ) -> Option<Decimal> {
        self.bom_components.as_ref().map(|components| {
            components
                .iter()
                .map(|c| {
                    let unit_cost = component_costs
                        .get(&c.component_material_id)
                        .copied()
                        .unwrap_or(Decimal::ZERO);
                    unit_cost * c.effective_quantity()
                })
                .sum()
        })
    }

    /// Calculate gross margin percentage.
    pub fn gross_margin_percent(&self) -> Decimal {
        if self.list_price > Decimal::ZERO {
            (self.list_price - self.standard_cost) / self.list_price * Decimal::from(100)
        } else {
            Decimal::ZERO
        }
    }

    /// Check if reorder is needed based on current stock.
    pub fn needs_reorder(&self, current_stock: Decimal) -> bool {
        current_stock <= self.reorder_point
    }

    /// Calculate reorder quantity based on EOQ principles.
    pub fn suggested_reorder_quantity(&self) -> Decimal {
        // Simplified: order enough to cover lead time plus safety stock
        self.reorder_point + self.safety_stock + self.min_order_quantity
    }
}

/// Pool of materials for transaction generation.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MaterialPool {
    /// All materials
    pub materials: Vec<Material>,
    /// Index by material type
    #[serde(skip)]
    type_index: std::collections::HashMap<MaterialType, Vec<usize>>,
    /// Index by material group
    #[serde(skip)]
    group_index: std::collections::HashMap<MaterialGroup, Vec<usize>>,
    /// Index by ABC classification
    #[serde(skip)]
    abc_index: std::collections::HashMap<char, Vec<usize>>,
}

impl MaterialPool {
    /// Create a new empty material pool.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a material pool from a vector of materials.
    ///
    /// This is the preferred way to create a pool from generated master data,
    /// ensuring JEs reference real entities.
    pub fn from_materials(materials: Vec<Material>) -> Self {
        let mut pool = Self::new();
        for material in materials {
            pool.add_material(material);
        }
        pool
    }

    /// Add a material to the pool.
    pub fn add_material(&mut self, material: Material) {
        let idx = self.materials.len();
        let material_type = material.material_type;
        let material_group = material.material_group;
        let abc = material.abc_classification;

        self.materials.push(material);

        self.type_index.entry(material_type).or_default().push(idx);
        self.group_index
            .entry(material_group)
            .or_default()
            .push(idx);
        self.abc_index.entry(abc).or_default().push(idx);
    }

    /// Get a random material.
    pub fn random_material(&self, rng: &mut impl rand::Rng) -> Option<&Material> {
        use rand::seq::IndexedRandom;
        self.materials.choose(rng)
    }

    /// Get a random material of a specific type.
    pub fn random_material_of_type(
        &self,
        material_type: MaterialType,
        rng: &mut impl rand::Rng,
    ) -> Option<&Material> {
        use rand::seq::IndexedRandom;
        self.type_index
            .get(&material_type)
            .and_then(|indices| indices.choose(rng))
            .map(|&idx| &self.materials[idx])
    }

    /// Get materials by ABC classification.
    pub fn get_by_abc(&self, classification: char) -> Vec<&Material> {
        self.abc_index
            .get(&classification)
            .map(|indices| indices.iter().map(|&i| &self.materials[i]).collect())
            .unwrap_or_default()
    }

    /// Rebuild indices after deserialization.
    pub fn rebuild_indices(&mut self) {
        self.type_index.clear();
        self.group_index.clear();
        self.abc_index.clear();

        for (idx, material) in self.materials.iter().enumerate() {
            self.type_index
                .entry(material.material_type)
                .or_default()
                .push(idx);
            self.group_index
                .entry(material.material_group)
                .or_default()
                .push(idx);
            self.abc_index
                .entry(material.abc_classification)
                .or_default()
                .push(idx);
        }
    }

    /// Get material by ID.
    pub fn get_by_id(&self, material_id: &str) -> Option<&Material> {
        self.materials.iter().find(|m| m.material_id == material_id)
    }

    /// Get count of materials.
    pub fn len(&self) -> usize {
        self.materials.len()
    }

    /// Check if pool is empty.
    pub fn is_empty(&self) -> bool {
        self.materials.is_empty()
    }
}

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

    #[test]
    fn test_material_creation() {
        let material = Material::new("MAT-001", "Test Material", MaterialType::RawMaterial)
            .with_standard_cost(Decimal::from(100))
            .with_list_price(Decimal::from(150))
            .with_abc_classification('A');

        assert_eq!(material.material_id, "MAT-001");
        assert_eq!(material.standard_cost, Decimal::from(100));
        assert_eq!(material.abc_classification, 'A');
    }

    #[test]
    fn test_gross_margin() {
        let material = Material::new("MAT-001", "Test", MaterialType::FinishedGood)
            .with_standard_cost(Decimal::from(60))
            .with_list_price(Decimal::from(100));

        let margin = material.gross_margin_percent();
        assert_eq!(margin, Decimal::from(40));
    }

    #[test]
    fn test_bom_cost_calculation() {
        let mut component_costs = std::collections::HashMap::new();
        component_costs.insert("COMP-001".to_string(), Decimal::from(10));
        component_costs.insert("COMP-002".to_string(), Decimal::from(20));

        let material = Material::new("FG-001", "Finished Good", MaterialType::FinishedGood)
            .with_bom(vec![
                BomComponent::new("COMP-001", Decimal::from(2), "EA"),
                BomComponent::new("COMP-002", Decimal::from(3), "EA"),
            ]);

        let bom_cost = material.calculate_bom_cost(&component_costs).unwrap();
        assert_eq!(bom_cost, Decimal::from(80)); // 2*10 + 3*20
    }

    #[test]
    fn test_material_pool() {
        let mut pool = MaterialPool::new();

        pool.add_material(Material::new("MAT-001", "Raw 1", MaterialType::RawMaterial));
        pool.add_material(Material::new(
            "MAT-002",
            "Finished 1",
            MaterialType::FinishedGood,
        ));
        pool.add_material(Material::new("MAT-003", "Raw 2", MaterialType::RawMaterial));

        assert_eq!(pool.len(), 3);
        assert!(pool.get_by_id("MAT-001").is_some());
        assert!(pool.get_by_id("MAT-999").is_none());
    }

    #[test]
    fn test_bom_component_scrap() {
        let component =
            BomComponent::new("COMP-001", Decimal::from(100), "EA").with_scrap(Decimal::from(5)); // 5% scrap

        let effective = component.effective_quantity();
        assert_eq!(effective, Decimal::from(105)); // 100 * 1.05
    }
}