askrobots / dbbasic Public

Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.

0 downloads

dbbasic/services/calculate_discount.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add DBBasic crate engine logo and update README 0d628db 1 year, 2 months ago History
calculate_discount.py · 2.3 KB · 70 lines
1
2#!/usr/bin/env python3
3"""
4AI-Generated Service: calculate_discount
5Description: Calculate discount based on customer loyalty level and order amount. Premium customers get 15%, Gold get 10%, Silver get 5%. Additional 5% for orders over $500.
6Generated: 2025-09-18T14:18:22.752042
7"""
8
9from typing import Dict, Any, Optional
10import json
11import asyncio
12from datetime import datetime
13
14# AI-Generated Implementation
15async def calculate_discount(data: Dict[str, Any]) -> Dict[str, Any]:
16 """
17 Calculate discount based on customer loyalty level and order amount. Premium customers get 15%, Gold get 10%, Silver get 5%. Additional 5% for orders over $500.
18
19 Inputs: customer_type, order_amount
20 Outputs: discount_amount, final_price, discount_percentage, discount_reason
21 """
22
23 try:
24 # Extract inputs
25 customer_type = data.get('customer_type', None)
26 order_amount = data.get('order_amount', None)
27
28 # AI-Generated Business Logic
29 # Calculate discount based on rules
30 discount_amount = 0
31 discount_reason = []
32
33 # Loyalty discount
34 if customer_type == 'premium':
35 discount_amount += order_total * 0.1
36 discount_reason.append('10% premium member discount')
37
38 # Volume discount
39 if quantity >= 10:
40 discount_amount += order_total * 0.05
41 discount_reason.append('5% bulk discount')
42
43 result = {
44 'original_total': order_total,
45 'discount_amount': round(discount_amount, 2),
46 'final_total': round(order_total - discount_amount, 2),
47 'discount_reasons': discount_reason
48 }
49
50 # Return results
51 return {
52 "success": True,
53 "timestamp": datetime.now().isoformat(),
54 "data": result
55 }
56
57 except Exception as e:
58 return {
59 "success": False,
60 "error": str(e),
61 "timestamp": datetime.now().isoformat()
62 }
63
64# Synchronous wrapper for compatibility
65def calculate_discount_sync(data: Dict[str, Any]) -> Dict[str, Any]:
66 """Synchronous wrapper for the async function"""
67 return asyncio.run(calculate_discount(data))
68
69# Export the service
70__all__ = ['calculate_discount', 'calculate_discount_sync']