datasynth-generators 2.2.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
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
//! Manufacturing cost accounting journal entry pipeline.
//!
//! Generates the full multi-stage cost flow JEs for production orders:
//! material issue to WIP, labor absorption, overhead application, FG transfer,
//! scrap recognition, and standard cost variance entries.

use chrono::NaiveDate;
use datasynth_core::accounts::{control_accounts, expense_accounts, manufacturing_accounts};
use datasynth_core::models::documents::Delivery;
use datasynth_core::models::{
    InspectionResult, JournalEntry, JournalEntryLine, ProductionOrder, ProductionOrderStatus,
    QualityInspection,
};
use rust_decimal::Decimal;

/// Generates multi-stage manufacturing cost accounting journal entries.
///
/// Covers the full WIP cost flow from material issue through FG transfer,
/// including scrap write-offs and standard cost variance recognition.
pub struct ManufacturingCostAccounting;

impl ManufacturingCostAccounting {
    /// Generate all manufacturing cost flow JEs for a set of production orders.
    ///
    /// # Arguments
    ///
    /// * `orders` - Production orders to generate JEs for.
    /// * `inspections` - Quality inspections linked to the orders (by `reference_id`).
    /// * `currency` - ISO 4217 transaction currency code.
    ///
    /// # Returns
    ///
    /// A `Vec<JournalEntry>` containing all balanced journal entries.
    pub fn generate_all_jes(
        orders: &[ProductionOrder],
        inspections: &[QualityInspection],
        currency: &str,
    ) -> Vec<JournalEntry> {
        let mut jes = Vec::new();

        for order in orders {
            // Skip orders that have no meaningful activity yet.
            // Released orders haven't started production; InProcess and beyond are costed.
            if matches!(
                order.status,
                ProductionOrderStatus::Planned
                    | ProductionOrderStatus::Cancelled
                    | ProductionOrderStatus::Released
            ) {
                continue;
            }

            let cost = match &order.cost_breakdown {
                Some(c) => c,
                None => continue,
            };

            // Use actual_end if available, else planned_end as the posting date.
            let posting_date = order.actual_end.unwrap_or(order.planned_end);

            // ------------------------------------------------------------------
            // 1. Material issue to WIP
            // ------------------------------------------------------------------
            if cost.material_cost > Decimal::ZERO {
                let mut je = JournalEntry::new_simple(
                    format!("JE-MFG-MAT-{}", order.order_id),
                    order.company_code.clone(),
                    posting_date,
                    format!("material issue to WIP for order {}", order.order_id),
                );
                je.header.currency = currency.to_string();
                let doc_id = je.header.document_id;

                je.add_line(JournalEntryLine {
                    document_id: doc_id,
                    line_number: 1,
                    gl_account: manufacturing_accounts::WIP.to_string(),
                    account_code: manufacturing_accounts::WIP.to_string(),
                    debit_amount: cost.material_cost,
                    local_amount: cost.material_cost,
                    reference: Some(order.order_id.clone()),
                    text: Some(format!("WIP - material: {}", order.material_description)),
                    quantity: Some(order.planned_quantity),
                    unit: Some("EA".to_string()),
                    ..Default::default()
                });
                je.add_line(JournalEntryLine {
                    document_id: doc_id,
                    line_number: 2,
                    gl_account: control_accounts::INVENTORY.to_string(),
                    account_code: control_accounts::INVENTORY.to_string(),
                    credit_amount: cost.material_cost,
                    local_amount: -cost.material_cost,
                    reference: Some(order.order_id.clone()),
                    text: Some(format!("Inventory credit: {}", order.material_description)),
                    quantity: Some(order.planned_quantity),
                    unit: Some("EA".to_string()),
                    ..Default::default()
                });
                jes.push(je);
            }

            // ------------------------------------------------------------------
            // 2. Labor absorption to WIP
            // ------------------------------------------------------------------
            if cost.labor_cost > Decimal::ZERO {
                let labor_qty = Decimal::from_f64_retain(order.labor_hours)
                    .unwrap_or(Decimal::ZERO)
                    .round_dp(2);

                let mut je = JournalEntry::new_simple(
                    format!("JE-MFG-LAB-{}", order.order_id),
                    order.company_code.clone(),
                    posting_date,
                    format!("labor absorption to WIP for order {}", order.order_id),
                );
                je.header.currency = currency.to_string();
                let doc_id = je.header.document_id;

                je.add_line(JournalEntryLine {
                    document_id: doc_id,
                    line_number: 1,
                    gl_account: manufacturing_accounts::WIP.to_string(),
                    account_code: manufacturing_accounts::WIP.to_string(),
                    debit_amount: cost.labor_cost,
                    local_amount: cost.labor_cost,
                    reference: Some(order.order_id.clone()),
                    text: Some("WIP - labor absorption".to_string()),
                    quantity: Some(labor_qty),
                    unit: Some("HR".to_string()),
                    ..Default::default()
                });
                je.add_line(JournalEntryLine {
                    document_id: doc_id,
                    line_number: 2,
                    gl_account: manufacturing_accounts::LABOR_ACCRUAL.to_string(),
                    account_code: manufacturing_accounts::LABOR_ACCRUAL.to_string(),
                    credit_amount: cost.labor_cost,
                    local_amount: -cost.labor_cost,
                    reference: Some(order.order_id.clone()),
                    text: Some("Labor accrual credit".to_string()),
                    quantity: Some(labor_qty),
                    unit: Some("HR".to_string()),
                    ..Default::default()
                });
                jes.push(je);
            }

            // ------------------------------------------------------------------
            // 3. Overhead applied to WIP
            // ------------------------------------------------------------------
            if cost.overhead_cost > Decimal::ZERO {
                let mut je = JournalEntry::new_simple(
                    format!("JE-MFG-OVH-{}", order.order_id),
                    order.company_code.clone(),
                    posting_date,
                    format!("overhead applied to WIP for order {}", order.order_id),
                );
                je.header.currency = currency.to_string();
                let doc_id = je.header.document_id;

                je.add_line(JournalEntryLine {
                    document_id: doc_id,
                    line_number: 1,
                    gl_account: manufacturing_accounts::WIP.to_string(),
                    account_code: manufacturing_accounts::WIP.to_string(),
                    debit_amount: cost.overhead_cost,
                    local_amount: cost.overhead_cost,
                    reference: Some(order.order_id.clone()),
                    text: Some("WIP - overhead applied".to_string()),
                    ..Default::default()
                });
                je.add_line(JournalEntryLine {
                    document_id: doc_id,
                    line_number: 2,
                    gl_account: manufacturing_accounts::OVERHEAD_APPLIED.to_string(),
                    account_code: manufacturing_accounts::OVERHEAD_APPLIED.to_string(),
                    credit_amount: cost.overhead_cost,
                    local_amount: -cost.overhead_cost,
                    reference: Some(order.order_id.clone()),
                    text: Some("Overhead applied credit".to_string()),
                    ..Default::default()
                });
                jes.push(je);
            }

            // ------------------------------------------------------------------
            // 4. FG transfer (Completed / Closed only)
            // ------------------------------------------------------------------
            let is_complete = matches!(
                order.status,
                ProductionOrderStatus::Completed | ProductionOrderStatus::Closed
            );
            let total_actual = cost.total_actual();

            // FG transfer uses standard cost so WIP retains the actual-vs-standard
            // residual, which is then cleared by the variance JEs below.
            let total_standard = cost.total_standard();

            if is_complete && total_standard > Decimal::ZERO {
                let mut je = JournalEntry::new_simple(
                    format!("JE-MFG-FG-{}", order.order_id),
                    order.company_code.clone(),
                    posting_date,
                    format!("FG transfer for order {}", order.order_id),
                );
                je.header.currency = currency.to_string();
                let doc_id = je.header.document_id;

                je.add_line(JournalEntryLine {
                    document_id: doc_id,
                    line_number: 1,
                    gl_account: manufacturing_accounts::FINISHED_GOODS.to_string(),
                    account_code: manufacturing_accounts::FINISHED_GOODS.to_string(),
                    debit_amount: total_standard,
                    local_amount: total_standard,
                    reference: Some(order.order_id.clone()),
                    text: Some(format!("FG receipt: {}", order.material_description)),
                    quantity: Some(order.actual_quantity),
                    unit: Some("EA".to_string()),
                    ..Default::default()
                });
                je.add_line(JournalEntryLine {
                    document_id: doc_id,
                    line_number: 2,
                    gl_account: manufacturing_accounts::WIP.to_string(),
                    account_code: manufacturing_accounts::WIP.to_string(),
                    credit_amount: total_standard,
                    local_amount: -total_standard,
                    reference: Some(order.order_id.clone()),
                    text: Some("WIP clearance to FG at standard cost".to_string()),
                    quantity: Some(order.actual_quantity),
                    unit: Some("EA".to_string()),
                    ..Default::default()
                });
                jes.push(je);
            }

            // ------------------------------------------------------------------
            // 5. Scrap JE (at most ONE per order, regardless of how many
            //    rejected inspections exist for it)
            // ------------------------------------------------------------------
            let first_rejected: Option<&QualityInspection> = inspections.iter().find(|insp| {
                insp.reference_id == order.order_id
                    && matches!(insp.result, InspectionResult::Rejected)
            });

            if let Some(insp) = first_rejected {
                let scrap_value = Self::compute_scrap_value(order, total_actual);
                if scrap_value > Decimal::ZERO {
                    let scrap_posting_date = Self::scrap_posting_date(order, posting_date);

                    let mut je = JournalEntry::new_simple(
                        format!("JE-MFG-SCRAP-{}", order.order_id),
                        order.company_code.clone(),
                        scrap_posting_date,
                        format!(
                            "scrap recognition for order {} inspection {}",
                            order.order_id, insp.inspection_id
                        ),
                    );
                    je.header.currency = currency.to_string();
                    let doc_id = je.header.document_id;

                    // Credit WIP for in-process orders, FG for completed/closed.
                    let credit_account = if is_complete {
                        manufacturing_accounts::FINISHED_GOODS
                    } else {
                        manufacturing_accounts::WIP
                    };

                    je.add_line(JournalEntryLine {
                        document_id: doc_id,
                        line_number: 1,
                        gl_account: manufacturing_accounts::SCRAP_EXPENSE.to_string(),
                        account_code: manufacturing_accounts::SCRAP_EXPENSE.to_string(),
                        debit_amount: scrap_value,
                        local_amount: scrap_value,
                        reference: Some(order.order_id.clone()),
                        text: Some(format!(
                            "Scrap expense: {}",
                            insp.disposition.as_deref().unwrap_or("scrap")
                        )),
                        quantity: Some(order.scrap_quantity),
                        unit: Some("EA".to_string()),
                        ..Default::default()
                    });
                    je.add_line(JournalEntryLine {
                        document_id: doc_id,
                        line_number: 2,
                        gl_account: credit_account.to_string(),
                        account_code: credit_account.to_string(),
                        credit_amount: scrap_value,
                        local_amount: -scrap_value,
                        reference: Some(order.order_id.clone()),
                        text: Some(format!("Scrap relief from {}", credit_account)),
                        quantity: Some(order.scrap_quantity),
                        unit: Some("EA".to_string()),
                        ..Default::default()
                    });
                    jes.push(je);
                }
            }

            // ------------------------------------------------------------------
            // 6. Variance JEs (Completed / Closed only)
            // ------------------------------------------------------------------
            if is_complete {
                // Material price variance
                if let Some(je) = Self::variance_je(
                    order,
                    posting_date,
                    currency,
                    cost.material_cost,
                    cost.standard_material_cost,
                    manufacturing_accounts::MATERIAL_PRICE_VARIANCE,
                    "material price variance",
                ) {
                    jes.push(je);
                }

                // Labor rate variance
                if let Some(je) = Self::variance_je(
                    order,
                    posting_date,
                    currency,
                    cost.labor_cost,
                    cost.standard_labor_cost,
                    manufacturing_accounts::LABOR_RATE_VARIANCE,
                    "labor rate variance",
                ) {
                    jes.push(je);
                }

                // Overhead volume variance
                if let Some(je) = Self::variance_je(
                    order,
                    posting_date,
                    currency,
                    cost.overhead_cost,
                    cost.standard_overhead_cost,
                    manufacturing_accounts::OVERHEAD_VOLUME_VARIANCE,
                    "overhead volume variance",
                ) {
                    jes.push(je);
                }
            }
        }

        jes
    }

    /// Generate COGS journal entries when goods are delivered/sold.
    ///
    /// For each delivered line item, looks up the average unit cost of the material
    /// from completed/closed production orders and posts:
    ///   DR COGS ("5000"), CR Finished Goods ("1410")
    ///
    /// # Arguments
    ///
    /// * `deliveries` - Outbound delivery documents from the O2C flow.
    /// * `production_orders` - Production orders used to derive average unit cost.
    ///
    /// # Returns
    ///
    /// One balanced `JournalEntry` per delivery that has at least one costed line item.
    pub fn generate_cogs_on_sale(
        deliveries: &[Delivery],
        production_orders: &[ProductionOrder],
        currency: &str,
    ) -> Vec<JournalEntry> {
        // ------------------------------------------------------------------
        // Build average-unit-cost map: material_id → avg_unit_cost
        // Only Completed / Closed orders with a cost breakdown contribute.
        // ------------------------------------------------------------------
        use std::collections::HashMap;

        // Accumulate (total_cost, total_qty) per material.
        let mut material_cost: HashMap<&str, (Decimal, Decimal)> = HashMap::new();
        for order in production_orders {
            if !matches!(
                order.status,
                ProductionOrderStatus::Completed | ProductionOrderStatus::Closed
            ) {
                continue;
            }
            if order.actual_quantity <= Decimal::ZERO {
                continue;
            }
            let total = match &order.cost_breakdown {
                Some(c) => c.total_actual(),
                None => order.actual_cost,
            };
            if total <= Decimal::ZERO {
                continue;
            }
            let entry = material_cost
                .entry(order.material_id.as_str())
                .or_insert((Decimal::ZERO, Decimal::ZERO));
            entry.0 += total;
            entry.1 += order.actual_quantity;
        }

        // Derive per-material average unit cost.
        let avg_unit_cost: HashMap<&str, Decimal> = material_cost
            .into_iter()
            .filter(|(_, (_, qty))| *qty > Decimal::ZERO)
            .map(|(mat, (cost, qty))| (mat, (cost / qty).round_dp(4)))
            .collect();

        // ------------------------------------------------------------------
        // Generate one JE per delivery (skip if no costed items).
        // ------------------------------------------------------------------
        let mut jes = Vec::new();

        for delivery in deliveries {
            let posting_date = delivery
                .header
                .posting_date
                .unwrap_or(delivery.header.document_date);

            // Accumulate COGS amount across all line items in this delivery.
            let mut cogs_total = Decimal::ZERO;
            let mut total_qty = Decimal::ZERO;

            for item in &delivery.items {
                let Some(ref mat_id) = item.base.material_id else {
                    continue;
                };
                let Some(&unit_cost) = avg_unit_cost.get(mat_id.as_str()) else {
                    continue;
                };
                let qty = item.base.quantity;
                if qty <= Decimal::ZERO || unit_cost <= Decimal::ZERO {
                    continue;
                }
                cogs_total += (unit_cost * qty).round_dp(2);
                total_qty += qty;
            }

            if cogs_total <= Decimal::ZERO {
                continue;
            }

            let mut je = JournalEntry::new_simple(
                format!("JE-COGS-{}", delivery.header.document_id),
                delivery.header.company_code.clone(),
                posting_date,
                format!("COGS on delivery {}", delivery.header.document_id),
            );
            je.header.currency = currency.to_string();
            let doc_id = je.header.document_id;

            // DR COGS
            je.add_line(JournalEntryLine {
                document_id: doc_id,
                line_number: 1,
                gl_account: expense_accounts::COGS.to_string(),
                account_code: expense_accounts::COGS.to_string(),
                debit_amount: cogs_total,
                local_amount: cogs_total,
                reference: Some(delivery.header.document_id.clone()),
                text: Some(format!("COGS - delivery {}", delivery.header.document_id)),
                quantity: Some(total_qty),
                unit: Some("EA".to_string()),
                ..Default::default()
            });

            // CR Finished Goods
            je.add_line(JournalEntryLine {
                document_id: doc_id,
                line_number: 2,
                gl_account: manufacturing_accounts::FINISHED_GOODS.to_string(),
                account_code: manufacturing_accounts::FINISHED_GOODS.to_string(),
                credit_amount: cogs_total,
                local_amount: -cogs_total,
                reference: Some(delivery.header.document_id.clone()),
                text: Some(format!(
                    "FG relief - delivery {}",
                    delivery.header.document_id
                )),
                quantity: Some(total_qty),
                unit: Some("EA".to_string()),
                ..Default::default()
            });

            jes.push(je);
        }

        jes
    }

    /// Build a single variance JE, or return `None` if the variance rounds to zero.
    ///
    /// Convention:
    /// - Unfavorable (actual > standard): DR variance account, CR WIP
    /// - Favorable   (actual < standard): DR WIP, CR variance account
    fn variance_je(
        order: &ProductionOrder,
        posting_date: NaiveDate,
        currency: &str,
        actual: Decimal,
        standard: Decimal,
        variance_account: &str,
        label: &str,
    ) -> Option<JournalEntry> {
        let variance = (actual - standard).round_dp(2);
        if variance == Decimal::ZERO {
            return None;
        }

        let abs_variance = variance.abs();
        let mut je = JournalEntry::new_simple(
            format!("JE-MFG-VAR-{}-{}", label.replace(' ', "-"), order.order_id),
            order.company_code.clone(),
            posting_date,
            format!("{} for order {}", label, order.order_id),
        );
        je.header.currency = currency.to_string();
        let doc_id = je.header.document_id;

        let (debit_acct, credit_acct) = if variance > Decimal::ZERO {
            // Unfavorable: DR variance, CR WIP
            (variance_account, manufacturing_accounts::WIP)
        } else {
            // Favorable: DR WIP, CR variance
            (manufacturing_accounts::WIP, variance_account)
        };

        je.add_line(JournalEntryLine {
            document_id: doc_id,
            line_number: 1,
            gl_account: debit_acct.to_string(),
            account_code: debit_acct.to_string(),
            debit_amount: abs_variance,
            local_amount: abs_variance,
            reference: Some(order.order_id.clone()),
            text: Some(format!("{} - debit", label)),
            ..Default::default()
        });
        je.add_line(JournalEntryLine {
            document_id: doc_id,
            line_number: 2,
            gl_account: credit_acct.to_string(),
            account_code: credit_acct.to_string(),
            credit_amount: abs_variance,
            local_amount: -abs_variance,
            reference: Some(order.order_id.clone()),
            text: Some(format!("{} - credit", label)),
            ..Default::default()
        });

        Some(je)
    }

    /// Compute the scrap value proportional to the order's scrap quantity.
    ///
    /// `scrap_value = total_actual * (scrap_quantity / planned_quantity)`
    fn compute_scrap_value(order: &ProductionOrder, total_actual: Decimal) -> Decimal {
        if order.planned_quantity <= Decimal::ZERO || order.scrap_quantity <= Decimal::ZERO {
            return Decimal::ZERO;
        }
        let fraction = order.scrap_quantity / order.planned_quantity;
        (total_actual * fraction).round_dp(2)
    }

    /// Determine the posting date for a scrap entry.
    ///
    /// Uses `actual_end` when available, otherwise falls back to the order's `posting_date`.
    fn scrap_posting_date(order: &ProductionOrder, fallback: NaiveDate) -> NaiveDate {
        order.actual_end.unwrap_or(fallback)
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use datasynth_config::schema::{
        ManufacturingCostingConfig, ProductionOrderConfig, RoutingConfig,
    };
    use datasynth_core::models::ProductionOrderStatus;

    use crate::manufacturing::ProductionOrderGenerator;

    fn make_orders(status: ProductionOrderStatus) -> Vec<ProductionOrder> {
        let mut gen = ProductionOrderGenerator::new(42);
        let config = ProductionOrderConfig::default();
        let costing = ManufacturingCostingConfig::default();
        let routing = RoutingConfig::default();
        let materials = vec![("MAT-001".to_string(), "Widget".to_string())];
        let start = chrono::NaiveDate::from_ymd_opt(2025, 1, 1).unwrap();
        let end = chrono::NaiveDate::from_ymd_opt(2025, 1, 31).unwrap();
        let mut orders = gen.generate("C001", &materials, start, end, &config, &costing, &routing);
        for o in &mut orders {
            o.status = status;
            if matches!(
                status,
                ProductionOrderStatus::Completed | ProductionOrderStatus::Closed
            ) {
                o.actual_end = Some(end);
            }
        }
        orders
    }

    #[test]
    fn test_in_process_generates_wip_jes() {
        let orders = make_orders(ProductionOrderStatus::InProcess);
        let jes = ManufacturingCostAccounting::generate_all_jes(&orders, &[], "USD");
        assert!(!jes.is_empty());
        for je in &jes {
            assert!(
                je.is_balanced(),
                "JE '{}' is unbalanced",
                je.description().unwrap_or("")
            );
        }
    }

    #[test]
    fn test_completed_generates_fg_and_variances() {
        let orders = make_orders(ProductionOrderStatus::Completed);
        let jes = ManufacturingCostAccounting::generate_all_jes(&orders, &[], "USD");

        let fg: Vec<_> = jes
            .iter()
            .filter(|je| {
                je.description()
                    .map_or(false, |d| d.contains("FG transfer"))
            })
            .collect();
        assert!(!fg.is_empty(), "Should generate FG transfer JEs");

        let var: Vec<_> = jes
            .iter()
            .filter(|je| je.description().map_or(false, |d| d.contains("variance")))
            .collect();
        assert!(!var.is_empty(), "Should generate variance JEs");

        for je in &jes {
            assert!(
                je.is_balanced(),
                "JE '{}' is unbalanced",
                je.description().unwrap_or("")
            );
        }
    }

    #[test]
    fn test_planned_produces_no_jes() {
        let orders = make_orders(ProductionOrderStatus::Planned);
        let jes = ManufacturingCostAccounting::generate_all_jes(&orders, &[], "USD");
        assert!(jes.is_empty(), "Planned orders should produce no JEs");
    }

    #[test]
    fn test_cancelled_produces_no_jes() {
        let orders = make_orders(ProductionOrderStatus::Cancelled);
        let jes = ManufacturingCostAccounting::generate_all_jes(&orders, &[], "USD");
        assert!(jes.is_empty(), "Cancelled orders should produce no JEs");
    }
}