...
This commit is contained in:
		@@ -5,7 +5,11 @@ use herodb::models::biz::{
 | 
			
		||||
    Product, ProductBuilder, ProductComponentBuilder,
 | 
			
		||||
    ProductType, ProductStatus,
 | 
			
		||||
    Sale, SaleBuilder, SaleItemBuilder, SaleStatus,
 | 
			
		||||
    ExchangeRate, ExchangeRateBuilder, EXCHANGE_RATE_SERVICE
 | 
			
		||||
    ExchangeRate, ExchangeRateBuilder, EXCHANGE_RATE_SERVICE,
 | 
			
		||||
    Service, ServiceBuilder, ServiceItemBuilder, ServiceStatus, BillingFrequency,
 | 
			
		||||
    Customer, CustomerBuilder,
 | 
			
		||||
    Contract, ContractBuilder, ContractStatus,
 | 
			
		||||
    Invoice, InvoiceBuilder, InvoiceItemBuilder, InvoiceStatus, PaymentStatus, Payment
 | 
			
		||||
};
 | 
			
		||||
use std::path::PathBuf;
 | 
			
		||||
use std::fs;
 | 
			
		||||
@@ -15,7 +19,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
    println!("============================================================");
 | 
			
		||||
 | 
			
		||||
    // Create a temporary directory for the database
 | 
			
		||||
    let db_path = PathBuf::from("./tmp/dbexample2");
 | 
			
		||||
    let db_path = PathBuf::from("/tmp/dbexample2");
 | 
			
		||||
    if db_path.exists() {
 | 
			
		||||
        fs::remove_dir_all(&db_path)?;
 | 
			
		||||
    }
 | 
			
		||||
@@ -28,6 +32,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
        .register_model::<Currency>()
 | 
			
		||||
        .register_model::<Sale>()
 | 
			
		||||
        .register_model::<ExchangeRate>()
 | 
			
		||||
        .register_model::<Service>()
 | 
			
		||||
        .register_model::<Customer>()
 | 
			
		||||
        .register_model::<Contract>()
 | 
			
		||||
        .register_model::<Invoice>()
 | 
			
		||||
        .build()?;
 | 
			
		||||
 | 
			
		||||
    println!("\n1. Creating Products with Builder Pattern");
 | 
			
		||||
@@ -281,6 +289,176 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
        println!("  - {}", product.name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    println!("\n8. Creating a Customer");
 | 
			
		||||
    println!("--------------------");
 | 
			
		||||
 | 
			
		||||
    // Create a customer using the builder
 | 
			
		||||
    let customer = CustomerBuilder::new()
 | 
			
		||||
        .id(1001)
 | 
			
		||||
        .name("Jane Smith")
 | 
			
		||||
        .description("Enterprise customer")
 | 
			
		||||
        .pubkey("abc123def456")
 | 
			
		||||
        .add_contact(5001)
 | 
			
		||||
        .add_contact(5002)
 | 
			
		||||
        .build()?;
 | 
			
		||||
 | 
			
		||||
    // Insert the customer
 | 
			
		||||
    db.insert_customer(&customer)?;
 | 
			
		||||
    println!("Customer created: {} (ID: {})", customer.name, customer.id);
 | 
			
		||||
    println!("Contacts: {:?}", customer.contact_ids);
 | 
			
		||||
 | 
			
		||||
    println!("\n9. Creating a Service");
 | 
			
		||||
    println!("-------------------");
 | 
			
		||||
 | 
			
		||||
    // Create service items using the builder
 | 
			
		||||
    let service_item1 = ServiceItemBuilder::new()
 | 
			
		||||
        .id(301)
 | 
			
		||||
        .service_id(2001)
 | 
			
		||||
        .product_id(1)
 | 
			
		||||
        .name("Standard Plan - Monthly")
 | 
			
		||||
        .quantity(1)
 | 
			
		||||
        .unit_price(CurrencyBuilder::new()
 | 
			
		||||
            .amount(29.99)
 | 
			
		||||
            .currency_code("USD")
 | 
			
		||||
            .build()?)
 | 
			
		||||
        .tax_rate(0.07) // 7% tax
 | 
			
		||||
        .is_taxable(true)
 | 
			
		||||
        .active_till(now + Duration::days(30))
 | 
			
		||||
        .build()?;
 | 
			
		||||
 | 
			
		||||
    // Create a service using the builder
 | 
			
		||||
    let service = ServiceBuilder::new()
 | 
			
		||||
        .id(2001)
 | 
			
		||||
        .customer_id(1001)
 | 
			
		||||
        .currency_code("USD")
 | 
			
		||||
        .status(ServiceStatus::Active)
 | 
			
		||||
        .billing_frequency(BillingFrequency::Monthly)
 | 
			
		||||
        .add_item(service_item1)
 | 
			
		||||
        .build()?;
 | 
			
		||||
 | 
			
		||||
    // Insert the service
 | 
			
		||||
    db.insert_service(&service)?;
 | 
			
		||||
    println!("Service created: #{} for customer #{}", service.id, service.customer_id);
 | 
			
		||||
    println!("Total amount: ${} USD (including tax)", service.total_amount.amount);
 | 
			
		||||
    println!("Billing frequency: {:?}", service.billing_frequency);
 | 
			
		||||
 | 
			
		||||
    println!("\n10. Creating a Contract");
 | 
			
		||||
    println!("---------------------");
 | 
			
		||||
 | 
			
		||||
    // Create a contract using the builder
 | 
			
		||||
    let contract = ContractBuilder::new()
 | 
			
		||||
        .id(3001)
 | 
			
		||||
        .customer_id(1001)
 | 
			
		||||
        .service_id(2001)
 | 
			
		||||
        .terms("Monthly service contract with auto-renewal")
 | 
			
		||||
        .start_date(now)
 | 
			
		||||
        .end_date(now + Duration::days(365))
 | 
			
		||||
        .auto_renewal(true)
 | 
			
		||||
        .renewal_terms("Renews automatically for 1 year unless cancelled 30 days prior")
 | 
			
		||||
        .status(ContractStatus::Active)
 | 
			
		||||
        .build()?;
 | 
			
		||||
 | 
			
		||||
    // Insert the contract
 | 
			
		||||
    db.insert_contract(&contract)?;
 | 
			
		||||
    println!("Contract created: #{} for customer #{}", contract.id, contract.customer_id);
 | 
			
		||||
    println!("Contract period: {} to {}",
 | 
			
		||||
        contract.start_date.format("%Y-%m-%d"),
 | 
			
		||||
        contract.end_date.format("%Y-%m-%d")
 | 
			
		||||
    );
 | 
			
		||||
    println!("Auto-renewal: {}", if contract.auto_renewal { "Yes" } else { "No" });
 | 
			
		||||
 | 
			
		||||
    println!("\n11. Creating an Invoice");
 | 
			
		||||
    println!("---------------------");
 | 
			
		||||
 | 
			
		||||
    // Create invoice items using the builder
 | 
			
		||||
    let invoice_item1 = InvoiceItemBuilder::new()
 | 
			
		||||
        .id(401)
 | 
			
		||||
        .invoice_id(4001)
 | 
			
		||||
        .description("Monthly service fee - Standard Plan")
 | 
			
		||||
        .amount(CurrencyBuilder::new()
 | 
			
		||||
            .amount(32.09) // Price with tax
 | 
			
		||||
            .currency_code("USD")
 | 
			
		||||
            .build()?)
 | 
			
		||||
        .service_id(2001)
 | 
			
		||||
        .build()?;
 | 
			
		||||
 | 
			
		||||
    // Create an invoice using the builder
 | 
			
		||||
    let invoice = InvoiceBuilder::new()
 | 
			
		||||
        .id(4001)
 | 
			
		||||
        .customer_id(1001)
 | 
			
		||||
        .currency_code("USD")
 | 
			
		||||
        .issue_date(now)
 | 
			
		||||
        .due_date(now + Duration::days(15))
 | 
			
		||||
        .add_item(invoice_item1)
 | 
			
		||||
        .build()?;
 | 
			
		||||
 | 
			
		||||
    // Insert the invoice
 | 
			
		||||
    db.insert_invoice(&invoice)?;
 | 
			
		||||
    println!("Invoice created: #{} for customer #{}", invoice.id, invoice.customer_id);
 | 
			
		||||
    println!("Total amount: ${} USD", invoice.total_amount.amount);
 | 
			
		||||
    println!("Balance due: ${} USD", invoice.balance_due.amount);
 | 
			
		||||
    println!("Status: {:?}, Payment status: {:?}", invoice.status, invoice.payment_status);
 | 
			
		||||
 | 
			
		||||
    println!("\n12. Processing a Payment");
 | 
			
		||||
    println!("----------------------");
 | 
			
		||||
 | 
			
		||||
    // Retrieve the invoice, add a payment, and save it back
 | 
			
		||||
    let mut retrieved_invoice = db.get_invoice(4001)?;
 | 
			
		||||
    
 | 
			
		||||
    // Create a payment
 | 
			
		||||
    let payment = Payment::new(
 | 
			
		||||
        CurrencyBuilder::new()
 | 
			
		||||
            .amount(32.09)
 | 
			
		||||
            .currency_code("USD")
 | 
			
		||||
            .build()?,
 | 
			
		||||
        "Credit Card".to_string()
 | 
			
		||||
    );
 | 
			
		||||
    
 | 
			
		||||
    // Add the payment to the invoice
 | 
			
		||||
    retrieved_invoice.add_payment(payment);
 | 
			
		||||
    
 | 
			
		||||
    // Save the updated invoice
 | 
			
		||||
    db.insert_invoice(&retrieved_invoice)?;
 | 
			
		||||
    
 | 
			
		||||
    println!("Payment processed for invoice #{}", retrieved_invoice.id);
 | 
			
		||||
    println!("New balance due: ${} USD", retrieved_invoice.balance_due.amount);
 | 
			
		||||
    println!("New status: {:?}, Payment status: {:?}",
 | 
			
		||||
        retrieved_invoice.status,
 | 
			
		||||
        retrieved_invoice.payment_status
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    println!("\n13. Retrieving Related Objects");
 | 
			
		||||
    println!("----------------------------");
 | 
			
		||||
 | 
			
		||||
    // Retrieve customer and related objects
 | 
			
		||||
    let retrieved_customer = db.get_customer(1001)?;
 | 
			
		||||
    println!("Customer: {} (ID: {})", retrieved_customer.name, retrieved_customer.id);
 | 
			
		||||
    
 | 
			
		||||
    // Retrieve service for this customer
 | 
			
		||||
    let retrieved_service = db.get_service(2001)?;
 | 
			
		||||
    println!("Service: #{} with {} items",
 | 
			
		||||
        retrieved_service.id,
 | 
			
		||||
        retrieved_service.items.len()
 | 
			
		||||
    );
 | 
			
		||||
    
 | 
			
		||||
    // Retrieve contract for this customer
 | 
			
		||||
    let retrieved_contract = db.get_contract(3001)?;
 | 
			
		||||
    println!("Contract: #{} ({})",
 | 
			
		||||
        retrieved_contract.id,
 | 
			
		||||
        if retrieved_contract.is_active() { "Active" } else { "Inactive" }
 | 
			
		||||
    );
 | 
			
		||||
    
 | 
			
		||||
    // Retrieve invoice for this customer
 | 
			
		||||
    let retrieved_invoice = db.get_invoice(4001)?;
 | 
			
		||||
    println!("Invoice: #{} ({})",
 | 
			
		||||
        retrieved_invoice.id,
 | 
			
		||||
        match retrieved_invoice.payment_status {
 | 
			
		||||
            PaymentStatus::Paid => "Paid",
 | 
			
		||||
            PaymentStatus::PartiallyPaid => "Partially Paid",
 | 
			
		||||
            PaymentStatus::Unpaid => "Unpaid",
 | 
			
		||||
        }
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    println!("\nExample completed successfully!");
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user