datasynth-core 2.3.0

Core domain models, traits, and distributions for synthetic enterprise data generation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
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
//! Sales Order document model.
//!
//! Represents sales orders in the O2C (Order-to-Cash) process flow.

use chrono::NaiveDate;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

use super::{DocumentHeader, DocumentLineItem, DocumentStatus, DocumentType};

/// Sales Order type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum SalesOrderType {
    /// Standard sales order
    #[default]
    Standard,
    /// Rush order
    Rush,
    /// Cash sale
    CashSale,
    /// Return order
    Return,
    /// Free of charge delivery
    FreeOfCharge,
    /// Consignment order
    Consignment,
    /// Service order
    Service,
    /// Credit memo request
    CreditMemoRequest,
    /// Debit memo request
    DebitMemoRequest,
}

impl SalesOrderType {
    /// Check if this order type requires delivery.
    pub fn requires_delivery(&self) -> bool {
        !matches!(
            self,
            Self::Service | Self::CreditMemoRequest | Self::DebitMemoRequest
        )
    }

    /// Check if this creates revenue.
    pub fn creates_revenue(&self) -> bool {
        !matches!(
            self,
            Self::FreeOfCharge | Self::Return | Self::CreditMemoRequest
        )
    }
}

/// Sales Order item.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SalesOrderItem {
    /// Base line item fields
    #[serde(flatten)]
    pub base: DocumentLineItem,

    /// Item category
    pub item_category: String,

    /// Schedule line (delivery schedule)
    pub schedule_lines: Vec<ScheduleLine>,

    /// Quantity confirmed
    pub quantity_confirmed: Decimal,

    /// Quantity delivered
    pub quantity_delivered: Decimal,

    /// Quantity invoiced
    pub quantity_invoiced: Decimal,

    /// Is this line fully delivered?
    pub is_fully_delivered: bool,

    /// Is this line fully invoiced?
    pub is_fully_invoiced: bool,

    /// Rejection reason (if rejected)
    pub rejection_reason: Option<String>,

    /// Is this line rejected?
    pub is_rejected: bool,

    /// Route
    pub route: Option<String>,

    /// Shipping point
    pub shipping_point: Option<String>,
}

/// Schedule line for delivery.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScheduleLine {
    /// Schedule line number
    pub schedule_number: u16,
    /// Requested delivery date
    pub requested_date: NaiveDate,
    /// Confirmed delivery date
    pub confirmed_date: Option<NaiveDate>,
    /// Scheduled quantity
    pub quantity: Decimal,
    /// Delivered quantity
    pub delivered_quantity: Decimal,
}

impl SalesOrderItem {
    /// Create a new sales order item.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        line_number: u16,
        description: impl Into<String>,
        quantity: Decimal,
        unit_price: Decimal,
    ) -> Self {
        Self {
            base: DocumentLineItem::new(line_number, description, quantity, unit_price),
            item_category: "TAN".to_string(), // Standard item
            schedule_lines: Vec::new(),
            quantity_confirmed: Decimal::ZERO,
            quantity_delivered: Decimal::ZERO,
            quantity_invoiced: Decimal::ZERO,
            is_fully_delivered: false,
            is_fully_invoiced: false,
            rejection_reason: None,
            is_rejected: false,
            route: None,
            shipping_point: None,
        }
    }

    /// Set material.
    pub fn with_material(mut self, material_id: impl Into<String>) -> Self {
        self.base = self.base.with_material(material_id);
        self
    }

    /// Set plant.
    pub fn with_plant(mut self, plant: impl Into<String>) -> Self {
        self.base.plant = Some(plant.into());
        self
    }

    /// Add a schedule line.
    pub fn add_schedule_line(&mut self, requested_date: NaiveDate, quantity: Decimal) {
        let schedule_number = (self.schedule_lines.len() + 1) as u16;
        self.schedule_lines.push(ScheduleLine {
            schedule_number,
            requested_date,
            confirmed_date: None,
            quantity,
            delivered_quantity: Decimal::ZERO,
        });
    }

    /// Confirm the schedule line.
    pub fn confirm_schedule(&mut self, schedule_number: u16, confirmed_date: NaiveDate) {
        if let Some(line) = self
            .schedule_lines
            .iter_mut()
            .find(|l| l.schedule_number == schedule_number)
        {
            line.confirmed_date = Some(confirmed_date);
            self.quantity_confirmed += line.quantity;
        }
    }

    /// Record delivery.
    pub fn record_delivery(&mut self, quantity: Decimal) {
        self.quantity_delivered += quantity;
        if self.quantity_delivered >= self.base.quantity {
            self.is_fully_delivered = true;
        }
    }

    /// Record invoice.
    pub fn record_invoice(&mut self, quantity: Decimal) {
        self.quantity_invoiced += quantity;
        if self.quantity_invoiced >= self.base.quantity {
            self.is_fully_invoiced = true;
        }
    }

    /// Open quantity for delivery.
    pub fn open_quantity_delivery(&self) -> Decimal {
        (self.base.quantity - self.quantity_delivered).max(Decimal::ZERO)
    }

    /// Open quantity for billing.
    pub fn open_quantity_billing(&self) -> Decimal {
        (self.quantity_delivered - self.quantity_invoiced).max(Decimal::ZERO)
    }

    /// Reject the line.
    pub fn reject(&mut self, reason: impl Into<String>) {
        self.is_rejected = true;
        self.rejection_reason = Some(reason.into());
    }
}

/// Sales Order document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SalesOrder {
    /// Document header
    pub header: DocumentHeader,

    /// SO type
    pub so_type: SalesOrderType,

    /// Customer ID
    pub customer_id: String,

    /// Sold-to party (if different from customer)
    pub sold_to: Option<String>,

    /// Ship-to party
    pub ship_to: Option<String>,

    /// Bill-to party
    pub bill_to: Option<String>,

    /// Payer
    pub payer: Option<String>,

    /// Sales organization
    pub sales_org: String,

    /// Distribution channel
    pub distribution_channel: String,

    /// Division
    pub division: String,

    /// Sales office
    pub sales_office: Option<String>,

    /// Sales group
    pub sales_group: Option<String>,

    /// Line items
    pub items: Vec<SalesOrderItem>,

    /// Total net amount
    pub total_net_amount: Decimal,

    /// Total tax amount
    pub total_tax_amount: Decimal,

    /// Total gross amount
    pub total_gross_amount: Decimal,

    /// Payment terms
    pub payment_terms: String,

    /// Incoterms
    pub incoterms: Option<String>,

    /// Shipping condition
    pub shipping_condition: Option<String>,

    /// Requested delivery date
    pub requested_delivery_date: Option<NaiveDate>,

    /// Customer PO number
    pub customer_po_number: Option<String>,

    /// Is this order complete?
    pub is_complete: bool,

    /// Credit status
    pub credit_status: CreditStatus,

    /// Credit block reason
    pub credit_block_reason: Option<String>,

    /// Is order released for delivery?
    pub is_delivery_released: bool,

    /// Is order released for billing?
    pub is_billing_released: bool,

    /// Related quote (if from quote)
    pub quote_id: Option<String>,

    /// Contract reference
    pub contract_id: Option<String>,

    /// Customer display name (denormalized, DS-011)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub customer_name: Option<String>,
}

/// Credit check status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum CreditStatus {
    /// Not checked
    #[default]
    NotChecked,
    /// Passed
    Passed,
    /// Failed - blocked
    Failed,
    /// Manually released
    Released,
}

impl SalesOrder {
    /// Create a new sales order.
    pub fn new(
        so_id: impl Into<String>,
        company_code: impl Into<String>,
        customer_id: impl Into<String>,
        fiscal_year: u16,
        fiscal_period: u8,
        document_date: NaiveDate,
        created_by: impl Into<String>,
    ) -> Self {
        let header = DocumentHeader::new(
            so_id,
            DocumentType::SalesOrder,
            company_code,
            fiscal_year,
            fiscal_period,
            document_date,
            created_by,
        );

        Self {
            header,
            so_type: SalesOrderType::Standard,
            customer_id: customer_id.into(),
            sold_to: None,
            ship_to: None,
            bill_to: None,
            payer: None,
            sales_org: "1000".to_string(),
            distribution_channel: "10".to_string(),
            division: "00".to_string(),
            sales_office: None,
            sales_group: None,
            items: Vec::new(),
            total_net_amount: Decimal::ZERO,
            total_tax_amount: Decimal::ZERO,
            total_gross_amount: Decimal::ZERO,
            payment_terms: "NET30".to_string(),
            incoterms: None,
            shipping_condition: None,
            requested_delivery_date: None,
            customer_po_number: None,
            is_complete: false,
            credit_status: CreditStatus::NotChecked,
            credit_block_reason: None,
            is_delivery_released: false,
            is_billing_released: false,
            quote_id: None,
            contract_id: None,
            customer_name: None,
        }
    }

    /// Set SO type.
    pub fn with_so_type(mut self, so_type: SalesOrderType) -> Self {
        self.so_type = so_type;
        self
    }

    /// Set sales organization.
    pub fn with_sales_org(
        mut self,
        sales_org: impl Into<String>,
        dist_channel: impl Into<String>,
        division: impl Into<String>,
    ) -> Self {
        self.sales_org = sales_org.into();
        self.distribution_channel = dist_channel.into();
        self.division = division.into();
        self
    }

    /// Set partner functions.
    pub fn with_partners(
        mut self,
        sold_to: impl Into<String>,
        ship_to: impl Into<String>,
        bill_to: impl Into<String>,
    ) -> Self {
        self.sold_to = Some(sold_to.into());
        self.ship_to = Some(ship_to.into());
        self.bill_to = Some(bill_to.into());
        self
    }

    /// Set customer PO.
    pub fn with_customer_po(mut self, po_number: impl Into<String>) -> Self {
        self.customer_po_number = Some(po_number.into());
        self
    }

    /// Set requested delivery date.
    pub fn with_requested_delivery_date(mut self, date: NaiveDate) -> Self {
        self.requested_delivery_date = Some(date);
        self
    }

    /// Add a line item.
    pub fn add_item(&mut self, item: SalesOrderItem) {
        self.items.push(item);
        self.recalculate_totals();
    }

    /// Recalculate totals.
    pub fn recalculate_totals(&mut self) {
        self.total_net_amount = self
            .items
            .iter()
            .filter(|i| !i.is_rejected)
            .map(|i| i.base.net_amount)
            .sum();
        self.total_tax_amount = self
            .items
            .iter()
            .filter(|i| !i.is_rejected)
            .map(|i| i.base.tax_amount)
            .sum();
        self.total_gross_amount = self.total_net_amount + self.total_tax_amount;
    }

    /// Perform credit check.
    pub fn check_credit(&mut self, passed: bool, block_reason: Option<String>) {
        if passed {
            self.credit_status = CreditStatus::Passed;
            self.credit_block_reason = None;
        } else {
            self.credit_status = CreditStatus::Failed;
            self.credit_block_reason = block_reason;
        }
    }

    /// Release credit block.
    pub fn release_credit_block(&mut self, user: impl Into<String>) {
        self.credit_status = CreditStatus::Released;
        self.credit_block_reason = None;
        self.header.update_status(DocumentStatus::Released, user);
    }

    /// Release for delivery.
    pub fn release_for_delivery(&mut self) {
        self.is_delivery_released = true;
    }

    /// Release for billing.
    pub fn release_for_billing(&mut self) {
        self.is_billing_released = true;
    }

    /// Check if order is complete.
    pub fn check_complete(&mut self) {
        self.is_complete = self
            .items
            .iter()
            .all(|i| i.is_rejected || i.is_fully_invoiced);
    }

    /// Get total open delivery value.
    pub fn open_delivery_value(&self) -> Decimal {
        self.items
            .iter()
            .filter(|i| !i.is_rejected)
            .map(|i| i.open_quantity_delivery() * i.base.unit_price)
            .sum()
    }

    /// Get total open billing value.
    pub fn open_billing_value(&self) -> Decimal {
        self.items
            .iter()
            .filter(|i| !i.is_rejected)
            .map(|i| i.open_quantity_billing() * i.base.unit_price)
            .sum()
    }
}

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

    #[test]
    fn test_sales_order_creation() {
        let so = SalesOrder::new(
            "SO-1000-0000000001",
            "1000",
            "C-000001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            "JSMITH",
        );

        assert_eq!(so.customer_id, "C-000001");
        assert_eq!(so.header.status, DocumentStatus::Draft);
    }

    #[test]
    fn test_sales_order_items() {
        let mut so = SalesOrder::new(
            "SO-1000-0000000001",
            "1000",
            "C-000001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            "JSMITH",
        );

        let mut item = SalesOrderItem::new(1, "Product A", Decimal::from(10), Decimal::from(100));
        item.add_schedule_line(
            NaiveDate::from_ymd_opt(2024, 1, 20).unwrap(),
            Decimal::from(10),
        );

        so.add_item(item);

        assert_eq!(so.total_net_amount, Decimal::from(1000));
        assert_eq!(so.items[0].schedule_lines.len(), 1);
    }

    #[test]
    fn test_delivery_tracking() {
        let mut item = SalesOrderItem::new(1, "Product A", Decimal::from(100), Decimal::from(10));

        assert_eq!(item.open_quantity_delivery(), Decimal::from(100));

        item.record_delivery(Decimal::from(60));
        assert_eq!(item.open_quantity_delivery(), Decimal::from(40));
        assert!(!item.is_fully_delivered);

        item.record_delivery(Decimal::from(40));
        assert!(item.is_fully_delivered);
    }
}