-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
156 lines (126 loc) Β· 5.6 KB
/
Copy pathutils.py
File metadata and controls
156 lines (126 loc) Β· 5.6 KB
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
from typing import Dict, List, Optional
class SplitBillCalculator:
"""Smart Split Bill Calculator"""
def __init__(self):
self.items = []
self.participants = []
def add_item(self, name: str, price: float, quantity: int = 1):
"""Tambah item ke bill"""
self.items.append({
"name": name,
"price": price,
"quantity": quantity,
"total": price * quantity
})
return self.items[-1]
def add_participant(self, name: str):
"""Tambah peserta patungan"""
self.participants.append({
"name": name,
"items": [],
"split_percentage": None
})
return self.participants[-1]
def assign_item_to_person(self, person_name: str, item_index: int):
"""Assign item ke orang tertentu"""
for p in self.participants:
if p["name"] == person_name:
if item_index not in p["items"]:
p["items"].append(item_index)
return True
return False
def calculate_equal_split(self, subtotal: float, tip_pct: float = 0,
tax_pct: float = 0, discount: float = 0) -> Dict:
"""Hitung split rata"""
tax_amount = subtotal * (tax_pct / 100)
tip_amount = subtotal * (tip_pct / 100)
total = subtotal - discount + tax_amount + tip_amount
if len(self.participants) == 0:
return {"error": "Belum ada peserta"}
per_person = total / len(self.participants)
return {
"subtotal": subtotal,
"discount": discount,
"tax_amount": tax_amount,
"tip_amount": tip_amount,
"total": total,
"num_people": len(self.participants),
"per_person": per_person,
"breakdown": [
{
"person": p["name"],
"amount": per_person
} for p in self.participants
]
}
def calculate_individual_split(self, subtotal: float, tip_pct: float = 0,
tax_pct: float = 0, discount: float = 0) -> Dict:
"""Hitung split berdasarkan pesanan individual"""
if not self.items:
return {"error": "Belum ada item"}
tax_amount = subtotal * (tax_pct / 100)
tip_amount = subtotal * (tip_pct / 100)
total = subtotal - discount + tax_amount + tip_amount
breakdown = []
total_check = 0
for person in self.participants:
# Hitung total item orang ini
person_items_total = sum(
self.items[i]["total"] for i in person["items"]
)
# Proporsi orang ini terhadap subtotal
proportion = person_items_total / subtotal if subtotal > 0 else 0
# Hitung bagian tax, tip, discount secara proporsional
person_tax = tax_amount * proportion
person_tip = tip_amount * proportion
person_discount = discount * proportion
person_total = person_items_total + person_tax + person_tip - person_discount
breakdown.append({
"person": person["name"],
"items_total": person_items_total,
"tax_share": person_tax,
"tip_share": person_tip,
"discount_share": person_discount,
"total_to_pay": person_total,
"items": [self.items[i]["name"] for i in person["items"]]
})
total_check += person_total
return {
"subtotal": subtotal,
"discount": discount,
"tax_amount": tax_amount,
"tip_amount": tip_amount,
"total": total,
"breakdown": breakdown,
"total_check": total_check,
"is_balanced": abs(total_check - total) < 0.01
}
def format_currency(self, amount: float) -> str:
"""Format ke Rupiah"""
return f"Rp {amount:,.0f}"
# Utility functions
def parse_bill_text(text: str) -> Dict:
"""Parse text bill dari user"""
# Ini bakal dipake AI agent nanti
return {"raw_text": text}
def validate_split(total_bill: float, individual_amounts: List[float]) -> bool:
"""Validasi apakah total individual = total bill"""
return abs(sum(individual_amounts) - total_bill) < 0.01
def generate_summary(calculation_result: Dict) -> str:
"""Generate ringkasan text dari hasil kalkulasi"""
calc = calculation_result
summary = "π **RINGKASAN SPLIT BILL**\n\n"
summary += f"π° Subtotal: Rp {calc['subtotal']:,.0f}\n"
summary += f"π·οΈ Tax: Rp {calc['tax_amount']:,.0f}\n"
summary += f"π Tip: Rp {calc['tip_amount']:,.0f}\n"
summary += f"π« Discount: Rp {calc.get('discount', 0):,.0f}\n"
summary += f"π΅ **TOTAL: Rp {calc['total']:,.0f}**\n\n"
if 'per_person' in calc:
summary += f"π€ Per orang (rata): **Rp {calc['per_person']:,.0f}**\n\n"
summary += "**Detail per Orang:**\n"
for person in calc['breakdown']:
summary += f"\nπ§ {person['person']}:\n"
if 'items' in person:
summary += f" π Pesanan: {', '.join(person['items'])}\n"
summary += f" π° Harus bayar: **Rp {person.get('total_to_pay', person.get('amount', 0)):,.0f}**\n"
return summary