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
//! Inventory valuation models.

use chrono::{DateTime, NaiveDate, Utc};
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};

use super::{InventoryPosition, ValuationMethod};

/// Inventory valuation report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InventoryValuationReport {
    /// Company code.
    pub company_code: String,
    /// As-of date.
    pub as_of_date: NaiveDate,
    /// Valuation method.
    pub valuation_method: ValuationMethod,
    /// Material valuations.
    pub materials: Vec<MaterialValuation>,
    /// Total inventory value.
    pub total_value: Decimal,
    /// Total quantity.
    pub total_quantity: Decimal,
    /// Value by plant.
    pub by_plant: HashMap<String, Decimal>,
    /// Value by material group.
    pub by_material_group: HashMap<String, Decimal>,
    /// Generated at.
    #[serde(with = "crate::serde_timestamp::utc")]
    pub generated_at: DateTime<Utc>,
}

impl InventoryValuationReport {
    /// Creates a valuation report from positions.
    pub fn from_positions(
        company_code: String,
        positions: &[InventoryPosition],
        as_of_date: NaiveDate,
    ) -> Self {
        let mut materials = Vec::new();
        let mut total_value = Decimal::ZERO;
        let mut total_quantity = Decimal::ZERO;
        let mut by_plant: HashMap<String, Decimal> = HashMap::new();
        let by_material_group: HashMap<String, Decimal> = HashMap::new();

        for pos in positions.iter().filter(|p| p.company_code == company_code) {
            let value = pos.total_value();
            total_value += value;
            total_quantity += pos.quantity_on_hand;

            *by_plant.entry(pos.plant.clone()).or_default() += value;

            materials.push(MaterialValuation {
                material_id: pos.material_id.clone(),
                description: pos.description.clone(),
                plant: pos.plant.clone(),
                storage_location: pos.storage_location.clone(),
                quantity: pos.quantity_on_hand,
                unit: pos.unit.clone(),
                unit_cost: pos.valuation.unit_cost,
                total_value: value,
                valuation_method: pos.valuation.method,
                standard_cost: pos.valuation.standard_cost,
                price_variance: pos.valuation.price_variance,
            });
        }

        // Sort by value descending
        materials.sort_by(|a, b| b.total_value.cmp(&a.total_value));

        Self {
            company_code,
            as_of_date,
            valuation_method: ValuationMethod::StandardCost,
            materials,
            total_value,
            total_quantity,
            by_plant,
            by_material_group,
            generated_at: Utc::now(),
        }
    }

    /// Gets top N materials by value.
    pub fn top_materials(&self, n: usize) -> Vec<&MaterialValuation> {
        self.materials.iter().take(n).collect()
    }

    /// Gets ABC classification.
    pub fn abc_analysis(&self) -> ABCAnalysis {
        ABCAnalysis::from_valuations(&self.materials, self.total_value)
    }
}

/// Material valuation detail.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MaterialValuation {
    /// Material ID.
    pub material_id: String,
    /// Description.
    pub description: String,
    /// Plant.
    pub plant: String,
    /// Storage location.
    pub storage_location: String,
    /// Quantity.
    pub quantity: Decimal,
    /// Unit.
    pub unit: String,
    /// Unit cost.
    pub unit_cost: Decimal,
    /// Total value.
    pub total_value: Decimal,
    /// Valuation method.
    pub valuation_method: ValuationMethod,
    /// Standard cost.
    pub standard_cost: Decimal,
    /// Price variance.
    pub price_variance: Decimal,
}

/// ABC Analysis result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ABCAnalysis {
    /// A items (typically 80% of value, 20% of items).
    pub a_items: Vec<ABCItem>,
    /// B items (typically 15% of value, 30% of items).
    pub b_items: Vec<ABCItem>,
    /// C items (typically 5% of value, 50% of items).
    pub c_items: Vec<ABCItem>,
    /// A threshold percentage.
    pub a_threshold: Decimal,
    /// B threshold percentage.
    pub b_threshold: Decimal,
    /// Summary statistics.
    pub summary: ABCSummary,
}

impl ABCAnalysis {
    /// Creates ABC analysis from valuations.
    pub fn from_valuations(valuations: &[MaterialValuation], total_value: Decimal) -> Self {
        let a_threshold = dec!(80);
        let b_threshold = dec!(95);

        let mut sorted: Vec<_> = valuations.iter().collect();
        sorted.sort_by(|a, b| b.total_value.cmp(&a.total_value));

        let mut a_items = Vec::new();
        let mut b_items = Vec::new();
        let mut c_items = Vec::new();

        let mut cumulative_value = Decimal::ZERO;

        for val in sorted {
            cumulative_value += val.total_value;
            let cumulative_percent = if total_value > Decimal::ZERO {
                cumulative_value / total_value * dec!(100)
            } else {
                Decimal::ZERO
            };

            let item = ABCItem {
                material_id: val.material_id.clone(),
                description: val.description.clone(),
                value: val.total_value,
                cumulative_percent,
            };

            if cumulative_percent <= a_threshold {
                a_items.push(item);
            } else if cumulative_percent <= b_threshold {
                b_items.push(item);
            } else {
                c_items.push(item);
            }
        }

        let summary = ABCSummary {
            a_count: a_items.len() as u32,
            a_value: a_items.iter().map(|i| i.value).sum(),
            a_percent: if total_value > Decimal::ZERO {
                a_items.iter().map(|i| i.value).sum::<Decimal>() / total_value * dec!(100)
            } else {
                Decimal::ZERO
            },
            b_count: b_items.len() as u32,
            b_value: b_items.iter().map(|i| i.value).sum(),
            b_percent: if total_value > Decimal::ZERO {
                b_items.iter().map(|i| i.value).sum::<Decimal>() / total_value * dec!(100)
            } else {
                Decimal::ZERO
            },
            c_count: c_items.len() as u32,
            c_value: c_items.iter().map(|i| i.value).sum(),
            c_percent: if total_value > Decimal::ZERO {
                c_items.iter().map(|i| i.value).sum::<Decimal>() / total_value * dec!(100)
            } else {
                Decimal::ZERO
            },
        };

        Self {
            a_items,
            b_items,
            c_items,
            a_threshold,
            b_threshold,
            summary,
        }
    }
}

/// Item in ABC classification.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ABCItem {
    /// Material ID.
    pub material_id: String,
    /// Description.
    pub description: String,
    /// Value.
    pub value: Decimal,
    /// Cumulative percentage.
    pub cumulative_percent: Decimal,
}

/// ABC analysis summary.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ABCSummary {
    /// A item count.
    pub a_count: u32,
    /// A total value.
    pub a_value: Decimal,
    /// A percentage.
    pub a_percent: Decimal,
    /// B item count.
    pub b_count: u32,
    /// B total value.
    pub b_value: Decimal,
    /// B percentage.
    pub b_percent: Decimal,
    /// C item count.
    pub c_count: u32,
    /// C total value.
    pub c_value: Decimal,
    /// C percentage.
    pub c_percent: Decimal,
}

/// FIFO valuation layer.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FIFOLayer {
    /// Receipt date.
    pub receipt_date: NaiveDate,
    /// Receipt document.
    pub receipt_document: String,
    /// Quantity remaining.
    pub quantity: Decimal,
    /// Unit cost.
    pub unit_cost: Decimal,
}

/// FIFO inventory tracker.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FIFOTracker {
    /// Material ID.
    pub material_id: String,
    /// Layers (oldest first).
    pub layers: VecDeque<FIFOLayer>,
    /// Total quantity.
    pub total_quantity: Decimal,
    /// Total value.
    pub total_value: Decimal,
}

impl FIFOTracker {
    /// Creates a new FIFO tracker.
    pub fn new(material_id: String) -> Self {
        Self {
            material_id,
            layers: VecDeque::new(),
            total_quantity: Decimal::ZERO,
            total_value: Decimal::ZERO,
        }
    }

    /// Adds a receipt.
    pub fn receive(
        &mut self,
        date: NaiveDate,
        document: String,
        quantity: Decimal,
        unit_cost: Decimal,
    ) {
        self.layers.push_back(FIFOLayer {
            receipt_date: date,
            receipt_document: document,
            quantity,
            unit_cost,
        });
        self.total_quantity += quantity;
        self.total_value += quantity * unit_cost;
    }

    /// Issues quantity using FIFO.
    pub fn issue(&mut self, quantity: Decimal) -> Option<Decimal> {
        if quantity > self.total_quantity {
            return None;
        }

        let mut remaining = quantity;
        let mut total_cost = Decimal::ZERO;

        while remaining > Decimal::ZERO && !self.layers.is_empty() {
            let front = self
                .layers
                .front_mut()
                .expect("FIFO layer exists when remaining > 0");

            if front.quantity <= remaining {
                // Consume entire layer
                total_cost += front.quantity * front.unit_cost;
                remaining -= front.quantity;
                self.layers.pop_front();
            } else {
                // Partial consumption
                total_cost += remaining * front.unit_cost;
                front.quantity -= remaining;
                remaining = Decimal::ZERO;
            }
        }

        self.total_quantity -= quantity;
        self.total_value -= total_cost;

        Some(total_cost)
    }

    /// Gets weighted average cost.
    pub fn weighted_average_cost(&self) -> Decimal {
        if self.total_quantity > Decimal::ZERO {
            self.total_value / self.total_quantity
        } else {
            Decimal::ZERO
        }
    }
}

/// Standard cost variance analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CostVarianceAnalysis {
    /// Company code.
    pub company_code: String,
    /// Period.
    pub period: String,
    /// Material variances.
    pub variances: Vec<MaterialCostVariance>,
    /// Total price variance.
    pub total_price_variance: Decimal,
    /// Total quantity variance.
    pub total_quantity_variance: Decimal,
    /// Generated at.
    #[serde(with = "crate::serde_timestamp::utc")]
    pub generated_at: DateTime<Utc>,
}

/// Material cost variance.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MaterialCostVariance {
    /// Material ID.
    pub material_id: String,
    /// Description.
    pub description: String,
    /// Standard cost.
    pub standard_cost: Decimal,
    /// Actual cost.
    pub actual_cost: Decimal,
    /// Quantity.
    pub quantity: Decimal,
    /// Price variance.
    pub price_variance: Decimal,
    /// Variance percentage.
    pub variance_percent: Decimal,
    /// Is favorable.
    pub is_favorable: bool,
}

impl MaterialCostVariance {
    /// Creates a new variance record.
    pub fn new(
        material_id: String,
        description: String,
        standard_cost: Decimal,
        actual_cost: Decimal,
        quantity: Decimal,
    ) -> Self {
        let price_variance = (standard_cost - actual_cost) * quantity;
        let variance_percent = if actual_cost > Decimal::ZERO {
            ((standard_cost - actual_cost) / actual_cost * dec!(100)).round_dp(2)
        } else {
            Decimal::ZERO
        };
        let is_favorable = price_variance > Decimal::ZERO;

        Self {
            material_id,
            description,
            standard_cost,
            actual_cost,
            quantity,
            price_variance,
            variance_percent,
            is_favorable,
        }
    }
}

/// Inventory turnover analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InventoryTurnover {
    /// Company code.
    pub company_code: String,
    /// Period start.
    pub period_start: NaiveDate,
    /// Period end.
    pub period_end: NaiveDate,
    /// Average inventory.
    pub average_inventory: Decimal,
    /// Cost of goods sold.
    pub cogs: Decimal,
    /// Turnover ratio.
    pub turnover_ratio: Decimal,
    /// Days inventory outstanding.
    pub dio_days: Decimal,
    /// By material.
    pub by_material: Vec<MaterialTurnover>,
}

impl InventoryTurnover {
    /// Calculates turnover from COGS and inventory levels.
    pub fn calculate(
        company_code: String,
        period_start: NaiveDate,
        period_end: NaiveDate,
        beginning_inventory: Decimal,
        ending_inventory: Decimal,
        cogs: Decimal,
    ) -> Self {
        let average_inventory = (beginning_inventory + ending_inventory) / dec!(2);
        let days_in_period = (period_end - period_start).num_days() as i32;

        let turnover_ratio = if average_inventory > Decimal::ZERO {
            (cogs / average_inventory).round_dp(2)
        } else {
            Decimal::ZERO
        };

        let dio_days = if cogs > Decimal::ZERO {
            (average_inventory / cogs * Decimal::from(days_in_period)).round_dp(1)
        } else {
            Decimal::ZERO
        };

        Self {
            company_code,
            period_start,
            period_end,
            average_inventory,
            cogs,
            turnover_ratio,
            dio_days,
            by_material: Vec::new(),
        }
    }
}

/// Material-level turnover.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MaterialTurnover {
    /// Material ID.
    pub material_id: String,
    /// Description.
    pub description: String,
    /// Average inventory.
    pub average_inventory: Decimal,
    /// Usage/COGS.
    pub usage: Decimal,
    /// Turnover ratio.
    pub turnover_ratio: Decimal,
    /// Days of supply.
    pub days_of_supply: Decimal,
    /// Classification (fast/slow/dead).
    pub classification: TurnoverClassification,
}

/// Turnover classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TurnoverClassification {
    /// Fast moving.
    FastMoving,
    /// Normal.
    Normal,
    /// Slow moving.
    SlowMoving,
    /// Dead/obsolete stock.
    Dead,
}

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

    #[test]
    fn test_fifo_tracker() {
        let mut tracker = FIFOTracker::new("MAT001".to_string());

        // Receive 100 @ $10
        tracker.receive(
            NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            "GR001".to_string(),
            dec!(100),
            dec!(10),
        );

        // Receive 100 @ $12
        tracker.receive(
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            "GR002".to_string(),
            dec!(100),
            dec!(12),
        );

        assert_eq!(tracker.total_quantity, dec!(200));
        assert_eq!(tracker.total_value, dec!(2200)); // 1000 + 1200

        // Issue 150 (should use FIFO - 100 @ $10 + 50 @ $12)
        let cost = tracker.issue(dec!(150)).unwrap();
        assert_eq!(cost, dec!(1600)); // (100 * 10) + (50 * 12)
        assert_eq!(tracker.total_quantity, dec!(50));
    }

    #[test]
    fn test_abc_analysis() {
        let valuations = vec![
            MaterialValuation {
                material_id: "A".to_string(),
                description: "High value".to_string(),
                plant: "P1".to_string(),
                storage_location: "S1".to_string(),
                quantity: dec!(10),
                unit: "EA".to_string(),
                unit_cost: dec!(100),
                total_value: dec!(1000),
                valuation_method: ValuationMethod::StandardCost,
                standard_cost: dec!(100),
                price_variance: Decimal::ZERO,
            },
            MaterialValuation {
                material_id: "B".to_string(),
                description: "Medium value".to_string(),
                plant: "P1".to_string(),
                storage_location: "S1".to_string(),
                quantity: dec!(50),
                unit: "EA".to_string(),
                unit_cost: dec!(10),
                total_value: dec!(500),
                valuation_method: ValuationMethod::StandardCost,
                standard_cost: dec!(10),
                price_variance: Decimal::ZERO,
            },
            MaterialValuation {
                material_id: "C".to_string(),
                description: "Low value".to_string(),
                plant: "P1".to_string(),
                storage_location: "S1".to_string(),
                quantity: dec!(100),
                unit: "EA".to_string(),
                unit_cost: dec!(1),
                total_value: dec!(100),
                valuation_method: ValuationMethod::StandardCost,
                standard_cost: dec!(1),
                price_variance: Decimal::ZERO,
            },
        ];

        let total = dec!(1600);
        let analysis = ABCAnalysis::from_valuations(&valuations, total);

        // Material A (1000/1600 = 62.5%) should be A
        assert!(!analysis.a_items.is_empty());
    }

    #[test]
    fn test_inventory_turnover() {
        let turnover = InventoryTurnover::calculate(
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            NaiveDate::from_ymd_opt(2024, 12, 31).unwrap(),
            dec!(100_000),
            dec!(120_000),
            dec!(1_000_000),
        );

        // Average inventory = 110,000
        // Turnover = 1,000,000 / 110,000 = 9.09
        assert!(turnover.turnover_ratio > dec!(9));

        // DIO = 110,000 / 1,000,000 * 365 ≈ 40 days
        assert!(turnover.dio_days > dec!(30) && turnover.dio_days < dec!(50));
    }

    #[test]
    fn test_cost_variance() {
        let variance = MaterialCostVariance::new(
            "MAT001".to_string(),
            "Test Material".to_string(),
            dec!(10),  // Standard
            dec!(11),  // Actual (higher)
            dec!(100), // Quantity
        );

        // Variance = (10 - 11) * 100 = -100 (unfavorable)
        assert_eq!(variance.price_variance, dec!(-100));
        assert!(!variance.is_favorable);
    }
}