Update app.py
Browse files
app.py
CHANGED
@@ -1,150 +1,140 @@
|
|
1 |
-
!pip install gradio==3.30.0
|
2 |
-
|
3 |
import gradio as gr
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
def calculate_price(calculation_type, wht_type, input_amount):
|
9 |
-
"""
|
10 |
-
calculation_type: เลือก "Forward" (Base -> Net) หรือ "Reverse" (Net -> Base)
|
11 |
-
wht_type: เลือก "WHT from Base" หรือ "WHT from Total"
|
12 |
-
input_amount: จำนวนเงินที่ใส่เข้ามา
|
13 |
-
"""
|
14 |
-
|
15 |
-
# เตรียมตัวแปรผลลัพธ์ (เก็บเป็น dict เพื่อส่งกลับไปแสดง)
|
16 |
-
result = {
|
17 |
-
"ราคาเริ่มต้น (Base)": 0.0,
|
18 |
-
"VAT 7%": 0.0,
|
19 |
-
"ยอดรวม (รวม VAT)": 0.0,
|
20 |
-
"หัก ณ ที่จ่าย": 0.0,
|
21 |
-
"ยอดสุทธิที่ได้รับ (Net)": 0.0
|
22 |
-
}
|
23 |
-
|
24 |
-
# ------------------------------------------------
|
25 |
-
# 1) กรณีหัก ณ ที่จ่ายจาก "Base"
|
26 |
-
# ------------------------------------------------
|
27 |
-
if wht_type == "WHT จากฐาน (Base)":
|
28 |
-
if calculation_type == "Forward (รู้ Base -> หา Net)":
|
29 |
-
base = input_amount
|
30 |
-
vat_amount = base * VAT_RATE
|
31 |
-
total_with_vat = base + vat_amount
|
32 |
-
wht_amount = base * WITHHOLD_RATE
|
33 |
-
net_amount = total_with_vat - wht_amount
|
34 |
-
|
35 |
-
result["ราคาเริ่มต้น (Base)"] = base
|
36 |
-
result["VAT 7%"] = vat_amount
|
37 |
-
result["ยอดรวม (รวม VAT)"] = total_with_vat
|
38 |
-
result["หัก ณ ที่จ่าย"] = wht_amount
|
39 |
-
result["ยอดสุทธิที่ได้รับ (Net)"] = net_amount
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
vat_amount = base * VAT_RATE
|
47 |
-
total_with_vat = base + vat_amount
|
48 |
-
wht_amount = base * WITHHOLD_RATE
|
49 |
-
|
50 |
-
result["ราคาเริ่มต้น (Base)"] = base
|
51 |
-
result["VAT 7%"] = vat_amount
|
52 |
-
result["ยอดรวม (รวม VAT)"] = total_with_vat
|
53 |
-
result["หัก ณ ที่จ่าย"] = wht_amount
|
54 |
-
result["ยอดสุทธิที่ได้รับ (Net)"] = net
|
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 |
-
result["หัก ณ ที่จ่าย"] = wht_amount
|
87 |
-
result["ยอดสุทธิที่ได้รับ (Net)"] = net
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
result[k] = f"{v:,.2f}"
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
f"หัก ณ ที่จ่าย: {result['หัก ณ ที่จ่าย']} บาท\n"
|
99 |
-
f"ยอดสุทธิที่ได้รับ (Net): {result['ยอดสุทธิที่ได้รับ (Net)']} บาท\n"
|
100 |
-
)
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
# -------------------------------------------------------
|
106 |
-
# สร้าง UI ด้วย Gradio
|
107 |
-
# -------------------------------------------------------
|
108 |
-
with gr.Blocks() as demo:
|
109 |
-
gr.Markdown(
|
110 |
-
"""
|
111 |
-
# โปรแกรมคำนวณราคา (VAT 7% และ หัก ณ ที่จ่าย 3%)
|
112 |
-
เลือกเงื่อนไขและกรอกตัวเลขเพื่อดูผลการคำนวณได้ทั้งแบบ "เดินหน้า" (รู้ Base -> หา Net)
|
113 |
-
หรือ "ถอยหลัง" (รู้ Net -> หา Base) และยังเลือกวิธีการคำนวณหัก ณ ที่จ่ายได้
|
114 |
-
ว่าจะหักจาก "Base" หรือจาก "Total (Base+VAT)"
|
115 |
-
"""
|
116 |
-
)
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
label="วิธีการหัก ณ ที่จ่าย",
|
127 |
-
choices=["WHT จากฐาน (Base)", "WHT จากยอดรวม (Total)"],
|
128 |
-
value="WHT จากฐาน (Base)"
|
129 |
-
)
|
130 |
-
|
131 |
-
input_amount = gr.Number(
|
132 |
-
label="กรอกจำนวนเงิน (บาท)",
|
133 |
-
value=6990.00,
|
134 |
-
precision=2
|
135 |
-
)
|
136 |
-
|
137 |
-
calc_button = gr.Button("คำนวณ")
|
138 |
-
|
139 |
-
output_area = gr.Textbox(
|
140 |
-
label="ผลลัพธ์การคำนวณ",
|
141 |
-
lines=6
|
142 |
)
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
outputs=output_area
|
148 |
)
|
149 |
|
150 |
-
demo.launch()
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
vat_rate = 0.07
|
4 |
+
withholding_rate = 0.03
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
def debug_print(data, label=""):
|
7 |
+
"""ฟังก์ชันสำหรับ debug ข้อมูล"""
|
8 |
+
print(f"\n🔍 [DEBUG] {label}:")
|
9 |
+
for k, v in data.items():
|
10 |
+
print(f"{k}: {v} ({type(v)})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
def calculate_tax(
|
13 |
+
base: float = None,
|
14 |
+
after_vat: float = None,
|
15 |
+
after_vat_withholding: float = None,
|
16 |
+
after_withholding: float = None
|
17 |
+
):
|
18 |
+
try:
|
19 |
+
# ตรวจสอบประเภทข้อมูลและค่าที่ได้รับ
|
20 |
+
inputs = {
|
21 |
+
"base": base,
|
22 |
+
"after_vat": after_vat,
|
23 |
+
"after_vat_withholding": after_vat_withholding,
|
24 |
+
"after_withholding": after_withholding
|
25 |
+
}
|
26 |
+
debug_print(inputs, "Inputs Received")
|
27 |
+
|
28 |
+
# เช็คค่าที่กรอกเข้ามา
|
29 |
+
filled = 0
|
30 |
+
for key, value in inputs.items():
|
31 |
+
if value not in [None, 0.0, 0]:
|
32 |
+
filled += 1
|
33 |
+
active_field = key
|
34 |
+
debug_print({"filled": filled, "active_field": active_field}, "Validation")
|
35 |
+
|
36 |
+
if filled != 1:
|
37 |
+
raise ValueError(f"กรุณากรอกข้อมูลเพียง 1 ช่อง (ตรวจพบ {filled} ช่อง)")
|
38 |
+
|
39 |
+
# คำนวณจากช่องที่กรอก
|
40 |
+
if active_field == "base":
|
41 |
+
base = float(base)
|
42 |
+
vat = base * vat_rate
|
43 |
+
withholding = base * withholding_rate
|
44 |
+
results = {
|
45 |
+
"after_vat": base + vat,
|
46 |
+
"after_vat_withholding": (base + vat) - withholding,
|
47 |
+
"after_withholding": base - withholding,
|
48 |
+
"vat": vat,
|
49 |
+
"withholding": withholding
|
50 |
+
}
|
51 |
+
elif active_field == "after_vat":
|
52 |
+
after_vat = float(after_vat)
|
53 |
+
base = after_vat / (1 + vat_rate)
|
54 |
+
vat = base * vat_rate
|
55 |
+
withholding = base * withholding_rate
|
56 |
+
results = {
|
57 |
+
"base": base,
|
58 |
+
"after_vat_withholding": after_vat - withholding,
|
59 |
+
"after_withholding": base - withholding,
|
60 |
+
"vat": vat,
|
61 |
+
"withholding": withholding
|
62 |
+
}
|
63 |
+
elif active_field == "after_vat_withholding":
|
64 |
+
after_vat_withholding = float(after_vat_withholding)
|
65 |
+
base = after_vat_withholding / (1 + vat_rate - withholding_rate)
|
66 |
+
vat = base * vat_rate
|
67 |
+
withholding = base * withholding_rate
|
68 |
+
results = {
|
69 |
+
"base": base,
|
70 |
+
"after_vat": base + vat,
|
71 |
+
"after_withholding": base - withholding,
|
72 |
+
"vat": vat,
|
73 |
+
"withholding": withholding
|
74 |
+
}
|
75 |
+
elif active_field == "after_withholding":
|
76 |
+
after_withholding = float(after_withholding)
|
77 |
+
base = after_withholding / (1 - withholding_rate)
|
78 |
+
vat = base * vat_rate
|
79 |
+
withholding = base * withholding_rate
|
80 |
+
results = {
|
81 |
+
"base": base,
|
82 |
+
"after_vat": base + vat,
|
83 |
+
"after_vat_withholding": (base + vat) - withholding,
|
84 |
+
"vat": vat,
|
85 |
+
"withholding": withholding
|
86 |
+
}
|
87 |
+
|
88 |
+
# ปัดเศษและแปลงรูปแบบ
|
89 |
+
formatted = {k: round(v, 2) for k, v in results.items()}
|
90 |
+
formatted["error"] = ""
|
91 |
+
debug_print(formatted, "Results")
|
92 |
+
return formatted
|
93 |
+
|
94 |
+
except Exception as e:
|
95 |
+
error_msg = f"⚠️ เกิดข้อผิดพลาด: {str(e)}"
|
96 |
+
debug_print({"error": error_msg}, "Error")
|
97 |
+
return {"error": error_msg}
|
98 |
|
99 |
+
def update_outputs(**kwargs):
|
100 |
+
results = calculate_tax(**kwargs)
|
101 |
+
outputs = {
|
102 |
+
"base": results.get("base", None),
|
103 |
+
"after_vat": results.get("after_vat", None),
|
104 |
+
"after_vat_withholding": results.get("after_vat_withholding", None),
|
105 |
+
"after_withholding": results.get("after_withholding", None),
|
106 |
+
"vat": results.get("vat", None),
|
107 |
+
"withholding": results.get("withholding", None),
|
108 |
+
"error_msg": results.get("error", "")
|
109 |
+
}
|
110 |
+
debug_print(outputs, "Final Outputs")
|
111 |
+
return outputs
|
|
|
|
|
112 |
|
113 |
+
with gr.Blocks(title="Tax Calculator Debug", theme=gr.themes.Soft()) as demo:
|
114 |
+
gr.Markdown("## 🧮 เครื่องคิดเลขภาษี (Debug Mode)")
|
|
|
115 |
|
116 |
+
with gr.Row():
|
117 |
+
base = gr.Number(label="1. เงินต้น (ก่อน VAT)", precision=2)
|
118 |
+
after_vat = gr.Number(label="2. ยอดรวม VAT 7%", precision=2)
|
119 |
+
after_vat_withholding = gr.Number(label="3. ยอดสุทธิ (หลัง VAT และหัก ณ ที่จ่าย)", precision=2)
|
120 |
+
after_withholding = gr.Number(label="4. ยอดหลังหัก ณ ที่จ่าย (ไม่มี VAT)", precision=2)
|
|
|
|
|
|
|
121 |
|
122 |
+
calculate_btn = gr.Button("คำนวณ", variant="primary")
|
123 |
+
reset_btn = gr.Button("รีเซ็ต")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
+
vat = gr.Number(label="ภาษีมูลค่าเพิ่ม (7%)", interactive=False, precision=2)
|
126 |
+
withholding = gr.Number(label="ภาษีหัก ณ ที่จ่าย (3%)", interactive=False, precision=2)
|
127 |
+
error_msg = gr.Textbox(label="สถานะ", interactive=False)
|
128 |
+
|
129 |
+
calculate_btn.click(
|
130 |
+
update_outputs,
|
131 |
+
inputs=[base, after_vat, after_vat_withholding, after_withholding],
|
132 |
+
outputs=[base, after_vat, after_vat_withholding, after_withholding, vat, withholding, error_msg]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
)
|
134 |
|
135 |
+
reset_btn.click(
|
136 |
+
lambda: [None]*4 + [None]*2 + [""],
|
137 |
+
outputs=[base, after_vat, after_vat_withholding, after_withholding, vat, withholding, error_msg]
|
|
|
138 |
)
|
139 |
|
140 |
+
demo.launch()
|