datasynth-generators 3.1.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
//! Healthcare transaction types.

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

use super::super::common::{IndustryGlAccount, IndustryJournalLine, IndustryTransaction};

/// Payer type.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PayerType {
    /// Medicare (CMS).
    Medicare,
    /// Medicaid (state).
    Medicaid,
    /// Commercial insurance.
    Commercial { carrier: String },
    /// Self-pay patient.
    SelfPay,
    /// Workers compensation.
    WorkersComp,
    /// Tricare (military).
    Tricare,
    /// Veterans Affairs.
    Va,
}

impl PayerType {
    /// Returns the payer type code.
    pub fn code(&self) -> &str {
        match self {
            PayerType::Medicare => "MCR",
            PayerType::Medicaid => "MCD",
            PayerType::Commercial { .. } => "COM",
            PayerType::SelfPay => "SP",
            PayerType::WorkersComp => "WC",
            PayerType::Tricare => "TRC",
            PayerType::Va => "VA",
        }
    }

    /// Returns the expected reimbursement rate compared to charges.
    pub fn expected_reimbursement_rate(&self) -> f64 {
        match self {
            PayerType::Medicare => 0.35,
            PayerType::Medicaid => 0.25,
            PayerType::Commercial { .. } => 0.55,
            PayerType::SelfPay => 0.15,
            PayerType::WorkersComp => 0.70,
            PayerType::Tricare => 0.40,
            PayerType::Va => 0.38,
        }
    }
}

/// Coding system types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CodingSystem {
    /// ICD-10-CM diagnosis codes.
    Icd10Cm,
    /// ICD-10-PCS procedure codes.
    Icd10Pcs,
    /// CPT procedure codes.
    Cpt,
    /// HCPCS Level II codes.
    Hcpcs,
    /// DRG codes.
    Drg,
    /// Revenue codes.
    RevCode,
}

impl CodingSystem {
    /// Returns the code format description.
    pub fn format_description(&self) -> &'static str {
        match self {
            CodingSystem::Icd10Cm => "A00-Z99.99",
            CodingSystem::Icd10Pcs => "0-F16ABCD",
            CodingSystem::Cpt => "00100-99499",
            CodingSystem::Hcpcs => "A0000-V5999",
            CodingSystem::Drg => "001-999",
            CodingSystem::RevCode => "0001-0999",
        }
    }
}

/// Revenue cycle transactions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RevenueCycleTransaction {
    /// Patient registration.
    PatientRegistration {
        patient_id: String,
        encounter_id: String,
        payer: PayerType,
        date: NaiveDate,
    },
    /// Charge capture.
    ChargeCapture {
        encounter_id: String,
        charges: Vec<Charge>,
        total_charges: Decimal,
        date: NaiveDate,
    },
    /// Claim submission.
    ClaimSubmission {
        claim_id: String,
        encounter_id: String,
        payer: PayerType,
        billed_amount: Decimal,
        diagnosis_codes: Vec<String>,
        procedure_codes: Vec<String>,
        date: NaiveDate,
    },
    /// Payment posting.
    PaymentPosting {
        claim_id: String,
        payer: PayerType,
        payment_amount: Decimal,
        adjustments: Vec<Adjustment>,
        date: NaiveDate,
    },
    /// Denial management.
    DenialPosting {
        claim_id: String,
        denial_reason: DenialReason,
        denial_code: String,
        denied_amount: Decimal,
        date: NaiveDate,
    },
    /// Contractual adjustment.
    ContractualAdjustment {
        claim_id: String,
        adjustment_amount: Decimal,
        reason_code: String,
        date: NaiveDate,
    },
    /// Patient responsibility posting.
    PatientResponsibility {
        claim_id: String,
        patient_id: String,
        responsibility_type: PatientResponsibilityType,
        amount: Decimal,
        date: NaiveDate,
    },
    /// Bad debt write-off.
    BadDebtWriteOff {
        patient_id: String,
        amount: Decimal,
        aging_days: u32,
        date: NaiveDate,
    },
}

/// A charge line.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Charge {
    /// Charge ID.
    pub charge_id: String,
    /// CPT/HCPCS code.
    pub procedure_code: String,
    /// Revenue code.
    pub revenue_code: String,
    /// Service description.
    pub description: String,
    /// Quantity.
    pub quantity: u32,
    /// Unit charge amount.
    pub unit_amount: Decimal,
    /// Total charge.
    pub total_amount: Decimal,
    /// Service date.
    pub service_date: NaiveDate,
    /// Modifier codes.
    pub modifiers: Vec<String>,
}

/// An adjustment line.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Adjustment {
    /// Adjustment reason code.
    pub reason_code: String,
    /// Adjustment amount.
    pub amount: Decimal,
    /// Adjustment type.
    pub adjustment_type: AdjustmentType,
}

/// Types of adjustments.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AdjustmentType {
    /// Contractual allowance.
    Contractual,
    /// Denial.
    Denial,
    /// Write-off.
    WriteOff,
    /// Bad debt.
    BadDebt,
    /// Charity care.
    Charity,
    /// Administrative.
    Administrative,
}

/// Denial reason categories.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DenialReason {
    /// Medical necessity not established.
    MedicalNecessity,
    /// Prior authorization not obtained.
    PriorAuthorization,
    /// Coverage terminated.
    CoverageTerminated,
    /// Duplicate claim.
    DuplicateClaim,
    /// Invalid coding.
    InvalidCoding,
    /// Timely filing exceeded.
    TimelyFiling,
    /// Bundled service.
    BundledService,
    /// Coordination of benefits.
    CoordinationOfBenefits,
}

/// Patient responsibility types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PatientResponsibilityType {
    /// Copayment.
    Copay,
    /// Coinsurance.
    Coinsurance,
    /// Deductible.
    Deductible,
    /// Non-covered service.
    NonCovered,
}

/// Clinical transactions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ClinicalTransaction {
    /// Procedure coding.
    ProcedureCoding {
        encounter_id: String,
        cpt_codes: Vec<String>,
        icd10_pcs_codes: Vec<String>,
        date: NaiveDate,
    },
    /// Diagnosis coding.
    DiagnosisCoding {
        encounter_id: String,
        icd10_codes: Vec<String>,
        principal_diagnosis: String,
        date: NaiveDate,
    },
    /// DRG assignment.
    DrgAssignment {
        encounter_id: String,
        drg_code: String,
        drg_weight: Decimal,
        expected_reimbursement: Decimal,
        date: NaiveDate,
    },
    /// Supply consumption.
    SupplyConsumption {
        encounter_id: String,
        supplies: Vec<SupplyLine>,
        total_cost: Decimal,
        date: NaiveDate,
    },
    /// Pharmacy dispensing.
    PharmacyDispensing {
        encounter_id: String,
        medications: Vec<MedicationLine>,
        total_cost: Decimal,
        date: NaiveDate,
    },
}

/// Supply line item.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SupplyLine {
    /// Supply item ID.
    pub item_id: String,
    /// Quantity used.
    pub quantity: u32,
    /// Unit cost.
    pub unit_cost: Decimal,
}

/// Medication line item.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MedicationLine {
    /// NDC code.
    pub ndc: String,
    /// Drug name.
    pub drug_name: String,
    /// Quantity.
    pub quantity: Decimal,
    /// Unit cost.
    pub unit_cost: Decimal,
}

/// Union type for healthcare transactions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum HealthcareTransaction {
    /// Revenue cycle transaction.
    RevenueCycle(RevenueCycleTransaction),
    /// Clinical transaction.
    Clinical(ClinicalTransaction),
}

impl IndustryTransaction for HealthcareTransaction {
    fn transaction_type(&self) -> &str {
        match self {
            HealthcareTransaction::RevenueCycle(rc) => match rc {
                RevenueCycleTransaction::PatientRegistration { .. } => "patient_registration",
                RevenueCycleTransaction::ChargeCapture { .. } => "charge_capture",
                RevenueCycleTransaction::ClaimSubmission { .. } => "claim_submission",
                RevenueCycleTransaction::PaymentPosting { .. } => "payment_posting",
                RevenueCycleTransaction::DenialPosting { .. } => "denial_posting",
                RevenueCycleTransaction::ContractualAdjustment { .. } => "contractual_adjustment",
                RevenueCycleTransaction::PatientResponsibility { .. } => "patient_responsibility",
                RevenueCycleTransaction::BadDebtWriteOff { .. } => "bad_debt_writeoff",
            },
            HealthcareTransaction::Clinical(clinical) => match clinical {
                ClinicalTransaction::ProcedureCoding { .. } => "procedure_coding",
                ClinicalTransaction::DiagnosisCoding { .. } => "diagnosis_coding",
                ClinicalTransaction::DrgAssignment { .. } => "drg_assignment",
                ClinicalTransaction::SupplyConsumption { .. } => "supply_consumption",
                ClinicalTransaction::PharmacyDispensing { .. } => "pharmacy_dispensing",
            },
        }
    }

    fn date(&self) -> NaiveDate {
        match self {
            HealthcareTransaction::RevenueCycle(rc) => match rc {
                RevenueCycleTransaction::PatientRegistration { date, .. }
                | RevenueCycleTransaction::ChargeCapture { date, .. }
                | RevenueCycleTransaction::ClaimSubmission { date, .. }
                | RevenueCycleTransaction::PaymentPosting { date, .. }
                | RevenueCycleTransaction::DenialPosting { date, .. }
                | RevenueCycleTransaction::ContractualAdjustment { date, .. }
                | RevenueCycleTransaction::PatientResponsibility { date, .. }
                | RevenueCycleTransaction::BadDebtWriteOff { date, .. } => *date,
            },
            HealthcareTransaction::Clinical(clinical) => match clinical {
                ClinicalTransaction::ProcedureCoding { date, .. }
                | ClinicalTransaction::DiagnosisCoding { date, .. }
                | ClinicalTransaction::DrgAssignment { date, .. }
                | ClinicalTransaction::SupplyConsumption { date, .. }
                | ClinicalTransaction::PharmacyDispensing { date, .. } => *date,
            },
        }
    }

    fn amount(&self) -> Option<Decimal> {
        match self {
            HealthcareTransaction::RevenueCycle(rc) => match rc {
                RevenueCycleTransaction::ChargeCapture { total_charges, .. } => {
                    Some(*total_charges)
                }
                RevenueCycleTransaction::ClaimSubmission { billed_amount, .. } => {
                    Some(*billed_amount)
                }
                RevenueCycleTransaction::PaymentPosting { payment_amount, .. } => {
                    Some(*payment_amount)
                }
                RevenueCycleTransaction::DenialPosting { denied_amount, .. } => {
                    Some(*denied_amount)
                }
                RevenueCycleTransaction::ContractualAdjustment {
                    adjustment_amount, ..
                } => Some(*adjustment_amount),
                RevenueCycleTransaction::PatientResponsibility { amount, .. } => Some(*amount),
                RevenueCycleTransaction::BadDebtWriteOff { amount, .. } => Some(*amount),
                _ => None,
            },
            HealthcareTransaction::Clinical(clinical) => match clinical {
                ClinicalTransaction::DrgAssignment {
                    expected_reimbursement,
                    ..
                } => Some(*expected_reimbursement),
                ClinicalTransaction::SupplyConsumption { total_cost, .. } => Some(*total_cost),
                ClinicalTransaction::PharmacyDispensing { total_cost, .. } => Some(*total_cost),
                _ => None,
            },
        }
    }

    fn accounts(&self) -> Vec<String> {
        match self {
            HealthcareTransaction::RevenueCycle(rc) => match rc {
                RevenueCycleTransaction::ChargeCapture { .. } => {
                    vec!["1200".to_string(), "4100".to_string()]
                }
                RevenueCycleTransaction::PaymentPosting { .. } => {
                    vec!["1000".to_string(), "1200".to_string()]
                }
                RevenueCycleTransaction::ContractualAdjustment { .. } => {
                    vec!["4200".to_string(), "1200".to_string()]
                }
                RevenueCycleTransaction::BadDebtWriteOff { .. } => {
                    vec!["6100".to_string(), "1200".to_string()]
                }
                _ => Vec::new(),
            },
            _ => Vec::new(),
        }
    }

    fn to_journal_lines(&self) -> Vec<IndustryJournalLine> {
        match self {
            HealthcareTransaction::RevenueCycle(RevenueCycleTransaction::ChargeCapture {
                total_charges,
                ..
            }) => {
                vec![
                    IndustryJournalLine::debit("1200", *total_charges, "Accounts Receivable"),
                    IndustryJournalLine::credit("4100", *total_charges, "Patient Service Revenue"),
                ]
            }
            HealthcareTransaction::RevenueCycle(RevenueCycleTransaction::PaymentPosting {
                payment_amount,
                ..
            }) => {
                vec![
                    IndustryJournalLine::debit("1000", *payment_amount, "Cash"),
                    IndustryJournalLine::credit("1200", *payment_amount, "Accounts Receivable"),
                ]
            }
            HealthcareTransaction::RevenueCycle(
                RevenueCycleTransaction::ContractualAdjustment {
                    adjustment_amount, ..
                },
            ) => {
                vec![
                    IndustryJournalLine::debit("4200", *adjustment_amount, "Contractual Allowance"),
                    IndustryJournalLine::credit("1200", *adjustment_amount, "Accounts Receivable"),
                ]
            }
            HealthcareTransaction::RevenueCycle(RevenueCycleTransaction::BadDebtWriteOff {
                amount,
                ..
            }) => {
                vec![
                    IndustryJournalLine::debit("6100", *amount, "Bad Debt Expense"),
                    IndustryJournalLine::credit("1200", *amount, "Accounts Receivable"),
                ]
            }
            _ => Vec::new(),
        }
    }

    fn metadata(&self) -> HashMap<String, String> {
        let mut meta = HashMap::new();
        meta.insert("industry".to_string(), "healthcare".to_string());
        meta.insert(
            "transaction_type".to_string(),
            self.transaction_type().to_string(),
        );
        meta
    }
}

/// Generator for healthcare transactions.
#[derive(Debug, Clone)]
pub struct HealthcareTransactionGenerator {
    /// Average encounters per day.
    pub avg_daily_encounters: u32,
    /// Denial rate (0.0-1.0).
    pub denial_rate: f64,
    /// Average charges per encounter.
    pub avg_charges_per_encounter: u32,
    /// Bad debt rate (0.0-1.0).
    pub bad_debt_rate: f64,
}

impl Default for HealthcareTransactionGenerator {
    fn default() -> Self {
        Self {
            avg_daily_encounters: 150,
            denial_rate: 0.05,
            avg_charges_per_encounter: 8,
            bad_debt_rate: 0.03,
        }
    }
}

impl HealthcareTransactionGenerator {
    /// Returns healthcare-specific GL accounts.
    pub fn gl_accounts() -> Vec<IndustryGlAccount> {
        vec![
            IndustryGlAccount::new("1000", "Cash and Cash Equivalents", "Asset", "Cash")
                .into_control(),
            IndustryGlAccount::new(
                "1200",
                "Patient Accounts Receivable",
                "Asset",
                "Receivables",
            )
            .into_control(),
            IndustryGlAccount::new(
                "1210",
                "Allowance for Doubtful Accounts",
                "Asset",
                "Receivables",
            )
            .with_normal_balance("Credit"),
            IndustryGlAccount::new("4100", "Patient Service Revenue", "Revenue", "Revenue")
                .with_normal_balance("Credit"),
            IndustryGlAccount::new("4200", "Contractual Allowances", "Revenue", "Deductions"),
            IndustryGlAccount::new("4210", "Charity Care", "Revenue", "Deductions"),
            IndustryGlAccount::new("4220", "Bad Debt Provision", "Revenue", "Deductions"),
            IndustryGlAccount::new("5100", "Salaries and Benefits", "Expense", "Labor"),
            IndustryGlAccount::new("5200", "Medical Supplies", "Expense", "Supplies"),
            IndustryGlAccount::new("5300", "Pharmaceuticals", "Expense", "Drugs"),
            IndustryGlAccount::new("5400", "Professional Fees", "Expense", "Professional"),
            IndustryGlAccount::new("6100", "Bad Debt Expense", "Expense", "Bad Debt"),
        ]
    }
}

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

    #[test]
    fn test_payer_type() {
        let medicare = PayerType::Medicare;
        assert_eq!(medicare.code(), "MCR");
        assert!(medicare.expected_reimbursement_rate() > 0.3);

        let commercial = PayerType::Commercial {
            carrier: "BlueCross".to_string(),
        };
        assert_eq!(commercial.code(), "COM");
    }

    #[test]
    fn test_charge_capture() {
        let tx = HealthcareTransaction::RevenueCycle(RevenueCycleTransaction::ChargeCapture {
            encounter_id: "E001".to_string(),
            charges: vec![Charge {
                charge_id: "CHG001".to_string(),
                procedure_code: "99213".to_string(),
                revenue_code: "0510".to_string(),
                description: "Office Visit".to_string(),
                quantity: 1,
                unit_amount: Decimal::new(150, 0),
                total_amount: Decimal::new(150, 0),
                service_date: NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
                modifiers: Vec::new(),
            }],
            total_charges: Decimal::new(150, 0),
            date: NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        });

        assert_eq!(tx.transaction_type(), "charge_capture");
        assert_eq!(tx.amount(), Some(Decimal::new(150, 0)));

        let lines = tx.to_journal_lines();
        assert_eq!(lines.len(), 2);
    }

    #[test]
    fn test_payment_posting() {
        let tx = HealthcareTransaction::RevenueCycle(RevenueCycleTransaction::PaymentPosting {
            claim_id: "CLM001".to_string(),
            payer: PayerType::Medicare,
            payment_amount: Decimal::new(100, 0),
            adjustments: vec![Adjustment {
                reason_code: "CO-45".to_string(),
                amount: Decimal::new(50, 0),
                adjustment_type: AdjustmentType::Contractual,
            }],
            date: NaiveDate::from_ymd_opt(2024, 1, 20).unwrap(),
        });

        assert_eq!(tx.transaction_type(), "payment_posting");
    }

    #[test]
    fn test_gl_accounts() {
        let accounts = HealthcareTransactionGenerator::gl_accounts();
        assert!(accounts.len() >= 10);

        let ar = accounts.iter().find(|a| a.account_number == "1200");
        assert!(ar.is_some());
    }
}