diff --git a/herodb/src/cmd/dbexample2/main.rs b/herodb/src/cmd/dbexample2/main.rs index 162576a..da68861 100644 --- a/herodb/src/cmd/dbexample2/main.rs +++ b/herodb/src/cmd/dbexample2/main.rs @@ -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> { 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> { .register_model::() .register_model::() .register_model::() + .register_model::() + .register_model::() + .register_model::() + .register_model::() .build()?; println!("\n1. Creating Products with Builder Pattern"); @@ -281,6 +289,176 @@ fn main() -> Result<(), Box> { 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(()) } \ No newline at end of file diff --git a/herodb/tmp/dbexample2/currency/conf b/herodb/tmp/dbexample2/currency/conf deleted file mode 100644 index 4154d7c..0000000 --- a/herodb/tmp/dbexample2/currency/conf +++ /dev/null @@ -1,4 +0,0 @@ -segment_size: 524288 -use_compression: false -version: 0.34 -vQÁ \ No newline at end of file diff --git a/herodb/tmp/dbexample2/currency/db b/herodb/tmp/dbexample2/currency/db deleted file mode 100644 index d580733..0000000 Binary files a/herodb/tmp/dbexample2/currency/db and /dev/null differ diff --git a/herodb/tmp/dbexample2/exchange_rate/conf b/herodb/tmp/dbexample2/exchange_rate/conf deleted file mode 100644 index 4154d7c..0000000 --- a/herodb/tmp/dbexample2/exchange_rate/conf +++ /dev/null @@ -1,4 +0,0 @@ -segment_size: 524288 -use_compression: false -version: 0.34 -vQÁ \ No newline at end of file diff --git a/herodb/tmp/dbexample2/exchange_rate/db b/herodb/tmp/dbexample2/exchange_rate/db deleted file mode 100644 index 47e94fd..0000000 Binary files a/herodb/tmp/dbexample2/exchange_rate/db and /dev/null differ diff --git a/herodb/tmp/dbexample2/product/conf b/herodb/tmp/dbexample2/product/conf deleted file mode 100644 index 4154d7c..0000000 --- a/herodb/tmp/dbexample2/product/conf +++ /dev/null @@ -1,4 +0,0 @@ -segment_size: 524288 -use_compression: false -version: 0.34 -vQÁ \ No newline at end of file diff --git a/herodb/tmp/dbexample2/product/db b/herodb/tmp/dbexample2/product/db deleted file mode 100644 index c29f221..0000000 Binary files a/herodb/tmp/dbexample2/product/db and /dev/null differ diff --git a/herodb/tmp/dbexample2/sale/conf b/herodb/tmp/dbexample2/sale/conf deleted file mode 100644 index 4154d7c..0000000 --- a/herodb/tmp/dbexample2/sale/conf +++ /dev/null @@ -1,4 +0,0 @@ -segment_size: 524288 -use_compression: false -version: 0.34 -vQÁ \ No newline at end of file diff --git a/herodb/tmp/dbexample2/sale/db b/herodb/tmp/dbexample2/sale/db deleted file mode 100644 index 0cfa073..0000000 Binary files a/herodb/tmp/dbexample2/sale/db and /dev/null differ