datasynth-generators 2.4.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
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
//! Production order generator for manufacturing processes.
//!
//! Generates realistic production orders with routing operations, costing,
//! yield calculations, and proper order lifecycle status distribution.

use chrono::{Datelike, NaiveDate};
use datasynth_config::schema::{ManufacturingCostingConfig, ProductionOrderConfig, RoutingConfig};
use datasynth_core::models::{
    CostBreakdown, OperationStatus, ProductionOrder, ProductionOrderStatus, ProductionOrderType,
    RoutingOperation,
};
use datasynth_core::utils::seeded_rng;
use datasynth_core::uuid_factory::{DeterministicUuidFactory, GeneratorType};
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
use rust_decimal::prelude::*;
use rust_decimal::Decimal;
use tracing::debug;

/// Work center identifiers used in routing operations.
const WORK_CENTERS: &[&str] = &["WC-100", "WC-200", "WC-300", "WC-400", "WC-500"];

/// Operation descriptions for routing steps.
const OPERATION_DESCRIPTIONS: &[&str] = &[
    "Material Preparation",
    "Cutting",
    "Machining",
    "Assembly",
    "Welding",
    "Heat Treatment",
    "Surface Finishing",
    "Quality Check",
    "Packaging",
    "Final Inspection",
];

/// Generates [`ProductionOrder`] instances with realistic routing operations,
/// costing, and lifecycle status distributions.
pub struct ProductionOrderGenerator {
    rng: ChaCha8Rng,
    uuid_factory: DeterministicUuidFactory,
}

impl ProductionOrderGenerator {
    /// Create a new production order generator with the given seed.
    pub fn new(seed: u64) -> Self {
        Self {
            rng: seeded_rng(seed, 0),
            uuid_factory: DeterministicUuidFactory::new(seed, GeneratorType::ProductionOrder),
        }
    }

    /// Generate production orders for the given period based on configuration.
    ///
    /// # Arguments
    ///
    /// * `company_code` - Company code for all generated orders.
    /// * `material_ids` - Available materials as `(material_id, description)` tuples.
    /// * `period_start` - Start of the generation period.
    /// * `period_end` - End of the generation period.
    /// * `config` - Production order configuration parameters.
    /// * `costing` - Manufacturing costing parameters.
    /// * `routing` - Routing configuration parameters.
    pub fn generate(
        &mut self,
        company_code: &str,
        material_ids: &[(String, String)],
        period_start: NaiveDate,
        period_end: NaiveDate,
        config: &ProductionOrderConfig,
        costing: &ManufacturingCostingConfig,
        routing: &RoutingConfig,
    ) -> Vec<ProductionOrder> {
        debug!(company_code, material_count = material_ids.len(), %period_start, %period_end, orders_per_month = config.orders_per_month, "Generating production orders");
        if material_ids.is_empty() {
            return Vec::new();
        }

        let mut orders = Vec::new();

        // Iterate month-by-month through the period
        let mut current = period_start;
        while current <= period_end {
            let month_end = end_of_month(current).min(period_end);

            for _ in 0..config.orders_per_month {
                let order = self.generate_one(
                    company_code,
                    material_ids,
                    current,
                    month_end,
                    config,
                    costing,
                    routing,
                );
                orders.push(order);
            }

            // Advance to the first day of the next month
            current = next_month_start(current);
        }

        orders
    }

    /// Generate a single production order.
    fn generate_one(
        &mut self,
        company_code: &str,
        material_ids: &[(String, String)],
        month_start: NaiveDate,
        month_end: NaiveDate,
        config: &ProductionOrderConfig,
        costing: &ManufacturingCostingConfig,
        routing: &RoutingConfig,
    ) -> ProductionOrder {
        let order_id = self.uuid_factory.next().to_string();

        // Pick a random material
        let (material_id, material_description) = material_ids
            .choose(&mut self.rng)
            .map(|(id, desc)| (id.clone(), desc.clone()))
            .unwrap_or_else(|| ("MAT-UNKNOWN".to_string(), "Unknown Material".to_string()));

        // Determine order type
        let order_type = self.pick_order_type(config);

        // Determine status
        let status = self.pick_status();

        // Batch size: avg * random factor (0.5 - 1.5)
        let batch_factor: f64 = self.rng.random_range(0.5..=1.5);
        let planned_qty_f64 = config.avg_batch_size as f64 * batch_factor;
        let planned_quantity = Decimal::from_f64_retain(planned_qty_f64.round())
            .unwrap_or(Decimal::from(config.avg_batch_size));

        // Yield: actual = planned * yield_rate * random(0.95 - 1.05)
        let yield_factor: f64 = self.rng.random_range(0.95..=1.05);
        let effective_yield = config.yield_rate * yield_factor;
        let actual_qty_f64 = planned_qty_f64 * effective_yield;
        let actual_quantity =
            Decimal::from_f64_retain(actual_qty_f64.round()).unwrap_or(planned_quantity);
        let scrap_quantity = (planned_quantity - actual_quantity).max(Decimal::ZERO);

        // Dates
        let days_in_month = (month_end - month_start).num_days().max(1);
        let start_offset = self.rng.random_range(0..days_in_month);
        let planned_start = month_start + chrono::Duration::days(start_offset);
        let production_days = self.rng.random_range(3..=14);
        let planned_end = planned_start + chrono::Duration::days(production_days);

        // Actual dates depend on status
        let (actual_start, actual_end) = match status {
            ProductionOrderStatus::Planned => (None, None),
            ProductionOrderStatus::Released => (None, None),
            ProductionOrderStatus::InProcess => {
                let offset = self.rng.random_range(0..=2);
                (Some(planned_start + chrono::Duration::days(offset)), None)
            }
            ProductionOrderStatus::Completed | ProductionOrderStatus::Closed => {
                let start_offset = self.rng.random_range(0..=2);
                let end_offset = self.rng.random_range(-1..=3);
                (
                    Some(planned_start + chrono::Duration::days(start_offset)),
                    Some(planned_end + chrono::Duration::days(end_offset)),
                )
            }
            ProductionOrderStatus::Cancelled => (None, None),
        };

        // Work center
        let work_center = WORK_CENTERS
            .choose(&mut self.rng)
            .unwrap_or(&"WC-100")
            .to_string();

        // Routing operations
        let variation: i32 = self.rng.random_range(-1..=1);
        let num_operations = (routing.avg_operations as i32 + variation).max(1) as u32;
        let operations = self.generate_operations(
            num_operations,
            planned_quantity,
            actual_quantity,
            routing,
            &status,
            planned_start,
            planned_end,
        );

        // Labor hours: sum of operation times * (1 + variation)
        let raw_labor_hours: f64 = operations
            .iter()
            .map(|op| op.setup_time_hours + op.run_time_hours)
            .sum();
        let labor_variation: f64 = self
            .rng
            .random_range(-routing.run_time_variation..=routing.run_time_variation);
        let labor_hours = raw_labor_hours * (1.0 + labor_variation);

        // Standard costs (based on config rates and routing operations)
        let standard_labor_cost_f64 = labor_hours * costing.labor_rate_per_hour;
        let standard_overhead_f64 = standard_labor_cost_f64 * costing.overhead_rate;
        let standard_material_cost_f64 = planned_qty_f64 * self.rng.random_range(5.0..50.0);
        let standard_unit_cost_f64 = if planned_qty_f64 > 0.0 {
            (standard_material_cost_f64 + standard_labor_cost_f64 + standard_overhead_f64)
                / planned_qty_f64
        } else {
            0.0
        };

        // Actual costs (with realistic variance from standard)
        let material_price_factor: f64 = self.rng.random_range(0.92..=1.10);
        let material_usage_factor: f64 = self.rng.random_range(0.95..=1.08);
        let actual_material_cost_f64 =
            standard_material_cost_f64 * material_price_factor * material_usage_factor;

        let labor_rate_factor: f64 = self.rng.random_range(0.95..=1.12);
        let labor_efficiency_factor: f64 = self.rng.random_range(0.90..=1.10);
        let actual_labor_cost_f64 =
            standard_labor_cost_f64 * labor_rate_factor * labor_efficiency_factor;

        let actual_overhead_f64 = actual_labor_cost_f64 * costing.overhead_rate;

        let to_dec = |v: f64| {
            Decimal::from_f64_retain(v)
                .unwrap_or(Decimal::ZERO)
                .round_dp(2)
        };

        let cost_breakdown = CostBreakdown {
            material_cost: to_dec(actual_material_cost_f64),
            labor_cost: to_dec(actual_labor_cost_f64),
            overhead_cost: to_dec(actual_overhead_f64),
            standard_material_cost: to_dec(standard_material_cost_f64),
            standard_labor_cost: to_dec(standard_labor_cost_f64),
            standard_overhead_cost: to_dec(standard_overhead_f64),
            standard_unit_cost: to_dec(standard_unit_cost_f64),
        };

        let actual_cost = cost_breakdown.total_actual();
        let planned_cost = cost_breakdown.total_standard();

        // Actual labor hours factored by efficiency
        let labor_hours_actual = labor_hours * labor_efficiency_factor;

        // Machine hours: 70% of actual labor hours
        let machine_hours = labor_hours_actual * 0.7;

        let routing_id = Some(format!("RT-{}", order_id.get(..8).unwrap_or("00000000")));
        let batch_number = Some(format!(
            "BATCH-{}-{:04}",
            planned_start.format("%Y%m%d"),
            self.rng.random_range(1..=9999)
        ));

        ProductionOrder {
            order_id,
            company_code: company_code.to_string(),
            material_id,
            material_description,
            order_type,
            status,
            planned_quantity,
            actual_quantity,
            scrap_quantity,
            planned_start,
            planned_end,
            actual_start,
            actual_end,
            work_center,
            routing_id,
            planned_cost,
            actual_cost,
            cost_breakdown: Some(cost_breakdown),
            labor_hours: labor_hours_actual,
            machine_hours,
            yield_rate: effective_yield,
            batch_number,
            operations,
        }
    }

    /// Pick a production order type based on configured rates.
    fn pick_order_type(&mut self, config: &ProductionOrderConfig) -> ProductionOrderType {
        let roll: f64 = self.rng.random();
        if roll < config.make_to_order_rate {
            ProductionOrderType::MakeToOrder
        } else if roll < config.make_to_order_rate + config.rework_rate {
            ProductionOrderType::Rework
        } else if self.rng.random_bool(0.5) {
            ProductionOrderType::Standard
        } else {
            ProductionOrderType::MakeToStock
        }
    }

    /// Pick a production order status with realistic distribution.
    fn pick_status(&mut self) -> ProductionOrderStatus {
        let roll: f64 = self.rng.random();
        if roll < 0.50 {
            ProductionOrderStatus::Completed
        } else if roll < 0.70 {
            ProductionOrderStatus::InProcess
        } else if roll < 0.85 {
            ProductionOrderStatus::Released
        } else if roll < 0.95 {
            ProductionOrderStatus::Closed
        } else {
            ProductionOrderStatus::Planned
        }
    }

    /// Generate routing operations for a production order.
    fn generate_operations(
        &mut self,
        count: u32,
        planned_quantity: Decimal,
        actual_quantity: Decimal,
        routing: &RoutingConfig,
        order_status: &ProductionOrderStatus,
        planned_start: NaiveDate,
        planned_end: NaiveDate,
    ) -> Vec<RoutingOperation> {
        let total_days = (planned_end - planned_start).num_days().max(1);
        let days_per_op = total_days / count as i64;

        (0..count)
            .map(|i| {
                let op_number = (i + 1) * 10;
                let desc_idx = (i as usize) % OPERATION_DESCRIPTIONS.len();
                let description = OPERATION_DESCRIPTIONS[desc_idx].to_string();

                let wc = WORK_CENTERS
                    .choose(&mut self.rng)
                    .unwrap_or(&"WC-100")
                    .to_string();

                // Setup time with variation
                let setup_variation: f64 = self.rng.random_range(0.8..=1.2);
                let setup_time_hours = routing.setup_time_hours * setup_variation;

                // Run time based on quantity and variation
                let base_run_hours: f64 = planned_quantity.to_f64().unwrap_or(100.0) * 0.05; // 0.05 hours per unit base
                let run_variation: f64 = self.rng.random_range(
                    1.0 - routing.run_time_variation..=1.0 + routing.run_time_variation,
                );
                let run_time_hours = base_run_hours * run_variation;

                // Operation status depends on order status and sequence position
                let op_status = match order_status {
                    ProductionOrderStatus::Planned | ProductionOrderStatus::Released => {
                        OperationStatus::Pending
                    }
                    ProductionOrderStatus::InProcess => {
                        if i < count / 2 {
                            OperationStatus::Completed
                        } else if i == count / 2 {
                            OperationStatus::InProcess
                        } else {
                            OperationStatus::Pending
                        }
                    }
                    ProductionOrderStatus::Completed | ProductionOrderStatus::Closed => {
                        OperationStatus::Completed
                    }
                    ProductionOrderStatus::Cancelled => OperationStatus::Cancelled,
                };

                let op_start_offset = i as i64 * days_per_op;
                let started_at = match op_status {
                    OperationStatus::InProcess | OperationStatus::Completed => {
                        Some(planned_start + chrono::Duration::days(op_start_offset))
                    }
                    _ => None,
                };
                let completed_at = match op_status {
                    OperationStatus::Completed => {
                        Some(planned_start + chrono::Duration::days(op_start_offset + days_per_op))
                    }
                    _ => None,
                };

                RoutingOperation {
                    operation_number: op_number,
                    operation_description: description,
                    work_center: wc,
                    setup_time_hours,
                    run_time_hours,
                    planned_quantity,
                    actual_quantity,
                    status: op_status,
                    started_at,
                    completed_at,
                }
            })
            .collect()
    }
}

/// Return the last day of the month for the given date.
fn end_of_month(date: NaiveDate) -> NaiveDate {
    let (y, m) = if date.month() == 12 {
        (date.year() + 1, 1)
    } else {
        (date.year(), date.month() + 1)
    };
    NaiveDate::from_ymd_opt(y, m, 1)
        .unwrap_or(date)
        .pred_opt()
        .unwrap_or(date)
}

/// Return the first day of the month following the given date.
fn next_month_start(date: NaiveDate) -> NaiveDate {
    let (y, m) = if date.month() == 12 {
        (date.year() + 1, 1)
    } else {
        (date.year(), date.month() + 1)
    };
    NaiveDate::from_ymd_opt(y, m, 1).unwrap_or(date)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn sample_materials() -> Vec<(String, String)> {
        vec![
            ("MAT-001".to_string(), "Widget Alpha".to_string()),
            ("MAT-002".to_string(), "Widget Beta".to_string()),
            ("MAT-003".to_string(), "Widget Gamma".to_string()),
        ]
    }

    #[test]
    fn test_basic_generation() {
        let mut gen = ProductionOrderGenerator::new(42);
        let materials = sample_materials();
        let config = ProductionOrderConfig::default();
        let costing = ManufacturingCostingConfig::default();
        let routing = RoutingConfig::default();

        let start = NaiveDate::from_ymd_opt(2024, 1, 1).unwrap();
        let end = NaiveDate::from_ymd_opt(2024, 1, 31).unwrap();

        let orders = gen.generate("C001", &materials, start, end, &config, &costing, &routing);

        assert_eq!(orders.len(), config.orders_per_month as usize);
        for order in &orders {
            assert_eq!(order.company_code, "C001");
            assert!(!order.order_id.is_empty());
            assert!(!order.material_id.is_empty());
            assert!(order.planned_quantity > Decimal::ZERO);
            assert!(order.planned_cost > Decimal::ZERO);
            assert!(!order.operations.is_empty());
            assert!(order.labor_hours > 0.0);
            assert!(order.machine_hours > 0.0);
        }
    }

    #[test]
    fn test_deterministic() {
        let materials = sample_materials();
        let config = ProductionOrderConfig::default();
        let costing = ManufacturingCostingConfig::default();
        let routing = RoutingConfig::default();

        let start = NaiveDate::from_ymd_opt(2024, 3, 1).unwrap();
        let end = NaiveDate::from_ymd_opt(2024, 3, 31).unwrap();

        let mut gen1 = ProductionOrderGenerator::new(12345);
        let orders1 = gen1.generate("C001", &materials, start, end, &config, &costing, &routing);

        let mut gen2 = ProductionOrderGenerator::new(12345);
        let orders2 = gen2.generate("C001", &materials, start, end, &config, &costing, &routing);

        assert_eq!(orders1.len(), orders2.len());
        for (o1, o2) in orders1.iter().zip(orders2.iter()) {
            assert_eq!(o1.order_id, o2.order_id);
            assert_eq!(o1.planned_quantity, o2.planned_quantity);
            assert_eq!(o1.actual_quantity, o2.actual_quantity);
            assert_eq!(o1.planned_cost, o2.planned_cost);
            assert_eq!(o1.actual_cost, o2.actual_cost);
        }
    }

    #[test]
    fn test_yield_and_scrap() {
        let mut gen = ProductionOrderGenerator::new(99);
        let materials = sample_materials();
        let config = ProductionOrderConfig {
            orders_per_month: 200,
            yield_rate: 0.90,
            ..Default::default()
        };
        let costing = ManufacturingCostingConfig::default();
        let routing = RoutingConfig::default();

        let start = NaiveDate::from_ymd_opt(2024, 6, 1).unwrap();
        let end = NaiveDate::from_ymd_opt(2024, 6, 30).unwrap();

        let orders = gen.generate("C001", &materials, start, end, &config, &costing, &routing);

        // With a 90% yield rate, on average actual < planned
        let total_planned: Decimal = orders.iter().map(|o| o.planned_quantity).sum();
        let total_actual: Decimal = orders.iter().map(|o| o.actual_quantity).sum();
        assert!(
            total_actual < total_planned,
            "With 90% yield, total actual ({}) should be less than planned ({})",
            total_actual,
            total_planned,
        );

        // Scrap should be non-negative for all orders
        for order in &orders {
            assert!(
                order.scrap_quantity >= Decimal::ZERO,
                "Scrap quantity should be non-negative, got {}",
                order.scrap_quantity,
            );
        }
    }

    #[test]
    fn test_multi_month_period() {
        let mut gen = ProductionOrderGenerator::new(77);
        let materials = sample_materials();
        let config = ProductionOrderConfig {
            orders_per_month: 10,
            ..Default::default()
        };
        let costing = ManufacturingCostingConfig::default();
        let routing = RoutingConfig::default();

        let start = NaiveDate::from_ymd_opt(2024, 1, 1).unwrap();
        let end = NaiveDate::from_ymd_opt(2024, 3, 31).unwrap();

        let orders = gen.generate("C001", &materials, start, end, &config, &costing, &routing);

        // 3 months * 10 orders/month = 30 orders
        assert_eq!(orders.len(), 30);
    }
}