db/herodb/src/models/biz/rhai/examples/example.rhai
2025-04-20 01:52:12 +02:00

168 lines
5.1 KiB
Plaintext

// Example script demonstrating the use of Business module operations
// Custom repeat function since Rhai doesn't have a built-in repeat method
fn repeat_str(str, count) {
let result = "";
for i in 0..count {
result += str;
}
return result;
}
// Print a section header
fn print_section(title) {
let line = repeat_str("=", 50);
print(line);
print(" " + title);
print(line);
}
print_section("BUSINESS MODULE OPERATIONS EXAMPLE");
// Currency Operations
print_section("CURRENCY OPERATIONS");
// Create a currency
print("\nCreating currencies...");
let usd = create_currency(100.0, "USD");
print("USD Currency created: " + usd.amount + " " + usd.currency_code);
// Convert currencies
print("\nConverting currencies...");
let eur = create_currency(150.0, "EUR");
print("EUR Currency created: " + eur.amount + " " + eur.currency_code);
let eur_to_usd = convert_currency(eur, "USD");
print("EUR to USD: " + eur.amount + " EUR = " + eur_to_usd.amount + " USD");
// Product Component Operations
print_section("PRODUCT COMPONENT OPERATIONS");
// Create a product component
print("\nCreating product components...");
let component1 = create_product_component(1, "CPU", "Intel i7 Processor", 1);
print("Component created: " + component1.name);
print(" ID: " + component1.id);
print(" Description: " + component1.description);
print(" Quantity: " + component1.quantity);
// Create another component
let component2 = create_product_component(2, "RAM", "32GB DDR4 Memory", 2);
print("Component created: " + component2.name);
print(" ID: " + component2.id);
print(" Description: " + component2.description);
print(" Quantity: " + component2.quantity);
// Product Operations
print_section("PRODUCT OPERATIONS");
// Create a product using builder pattern
print("\nCreating a product...");
let product = create_product_builder()
.product_name("High-End Gaming PC")
.product_description("Ultimate gaming experience")
.product_price(create_currency(1999.99, "USD"))
.product_validity_days(365)
.build();
print("Product created: " + product.name);
print(" ID: " + product.id);
print(" Description: " + product.description);
print(" Price: " + product.price.amount + " " + product.price.currency_code);
// Add components to the product
print("\nAdding components to product...");
let product_with_components = product
.add_component(component1)
.add_component(component2);
print("Components added to product");
// Get components from the product
let components = get_product_components(product_with_components);
print("Number of components: " + components.len());
// Customer Operations
print_section("CUSTOMER OPERATIONS");
// Create a customer
print("\nCreating a customer...");
let customer = create_customer("John Doe", "john@example.com", "+1234567890", "123 Main St");
print("Customer created: " + customer.name);
print(" Email: " + customer.email);
print(" Phone: " + customer.phone);
print(" Address: " + customer.address);
// Sale Operations
print_section("SALE OPERATIONS");
// Create a sale
print("\nCreating a sale...");
let sale = create_sale(customer, "2025-04-19");
print("Sale created for customer: " + sale.customer.name);
print(" Date: " + sale.date);
// Add product to sale
let sale_with_item = add_sale_item(sale, product_with_components, 1);
print("Added product to sale: " + product_with_components.name);
// Service Operations
print_section("SERVICE OPERATIONS");
// Create a service
print("\nCreating a service...");
let service = create_service(
"Premium Support",
"24/7 Technical Support",
create_currency(49.99, "USD"),
30
);
print("Service created: " + service.name);
print(" Description: " + service.description);
print(" Price: " + service.price.amount + " " + service.price.currency_code);
print(" Duration: " + service.duration + " days");
// Contract Operations
print_section("CONTRACT OPERATIONS");
// Create a contract
print("\nCreating a contract...");
let contract = create_contract(
"Support Agreement",
"Annual support contract",
"2025-04-19",
"2026-04-19",
create_currency(599.99, "USD"),
"Active"
);
print("Contract created: " + contract.title);
print(" Description: " + contract.description);
print(" Start Date: " + contract.start_date);
print(" End Date: " + contract.end_date);
print(" Value: " + contract.value.amount + " " + contract.value.currency_code);
print(" Status: " + contract.status);
// Invoice Operations
print_section("INVOICE OPERATIONS");
// Create an invoice
print("\nCreating an invoice...");
let invoice = create_invoice(
"INV-2025-001",
"2025-04-19",
"2025-05-19",
customer,
create_currency(2499.99, "USD"),
"Issued",
"Pending"
);
print("Invoice created: " + invoice.number);
print(" Date: " + invoice.date);
print(" Due Date: " + invoice.due_date);
print(" Customer: " + invoice.customer.name);
print(" Amount: " + invoice.amount.amount + " " + invoice.amount.currency_code);
print(" Status: " + invoice.status);
print(" Payment Status: " + invoice.payment_status);
print_section("EXAMPLE COMPLETED");
print("All business module operations completed successfully!");