Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,319 +1,160 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import sqlite3
|
| 3 |
-
import json
|
| 4 |
-
from huggingface_hub import hf_hub_download, hf_hub_upload
|
| 5 |
-
import os
|
| 6 |
-
from pathlib import Path
|
| 7 |
-
from reportlab.lib.pagesizes import A4
|
| 8 |
-
from reportlab.lib.units import cm
|
| 9 |
-
from reportlab.pdfgen import canvas
|
| 10 |
-
from reportlab.pdfbase import pdfmetrics
|
| 11 |
-
from reportlab.pdfbase.ttfonts import TTFont
|
| 12 |
-
import qrcode
|
| 13 |
-
import zipfile
|
| 14 |
-
import io
|
| 15 |
-
from dotenv import load_dotenv
|
| 16 |
-
import time
|
| 17 |
-
|
| 18 |
-
# อ่าน .env (ถ้ามี)
|
| 19 |
-
load_dotenv()
|
| 20 |
-
REPO_ID = os.getenv("REPO_ID", "your-username/worker-management")
|
| 21 |
-
ORIGINAL_REPO_ID = os.getenv("ORIGINAL_REPO_ID", None)
|
| 22 |
-
|
| 23 |
-
# ตั้งค่าฟอนต์
|
| 24 |
-
try:
|
| 25 |
-
pdfmetrics.registerFont(TTFont('THSarabun', 'THSarabunNew.ttf'))
|
| 26 |
-
pdfmetrics.registerFont(TTFont('THSarabunBold', 'THSarabunNew-Bold.ttf'))
|
| 27 |
-
except Exception as e:
|
| 28 |
-
print(f"Warning: Could not load fonts: {e}")
|
| 29 |
-
# ใช้ฟอนต์เริ่มต้นแทน
|
| 30 |
-
pass
|
| 31 |
-
|
| 32 |
-
# ฟังก์ชันแปลงวันที่เป็นภาษาไทย
|
| 33 |
-
def thai_date_time(timestamp):
|
| 34 |
-
months = {
|
| 35 |
-
1: "มกราคม", 2: "กุมภาพันธ์", 3: "มีนาคม", 4: "เมษายน",
|
| 36 |
-
5: "พฤษภาคม", 6: "มิถุนายน", 7: "กรกฎาคม", 8: "สิงหาคม",
|
| 37 |
-
9: "กันยายน", 10: "ตุลาคม", 11: "พฤศจิกายน", 12: "ธันวาคม"
|
| 38 |
-
}
|
| 39 |
-
dt = time.localtime(timestamp)
|
| 40 |
-
day = dt.tm_mday
|
| 41 |
-
month = months[dt.tm_mon]
|
| 42 |
-
year = dt.tm_year + 543
|
| 43 |
-
hour = dt.tm_hour
|
| 44 |
-
minute = dt.tm_min
|
| 45 |
-
period = "น"
|
| 46 |
-
return f"{day:02d} {month} {year} {hour:02d}:{minute:02d} {period}"
|
| 47 |
-
|
| 48 |
-
# เริ่มต้นฐานข้อมูล
|
| 49 |
-
def init_db():
|
| 50 |
-
conn = sqlite3.connect("workers.db")
|
| 51 |
-
c = conn.cursor()
|
| 52 |
-
c.execute('''CREATE TABLE IF NOT EXISTS workers (
|
| 53 |
-
request_number TEXT PRIMARY KEY,
|
| 54 |
-
english_name TEXT,
|
| 55 |
-
foreign_reference_number TEXT,
|
| 56 |
-
id_number TEXT,
|
| 57 |
-
nationality TEXT,
|
| 58 |
-
receipt_number TEXT,
|
| 59 |
-
payment_number TEXT
|
| 60 |
-
)''')
|
| 61 |
-
c.execute('''CREATE TABLE IF NOT EXISTS attachments (
|
| 62 |
-
request_number TEXT,
|
| 63 |
-
file_url TEXT,
|
| 64 |
-
FOREIGN KEY(request_number) REFERENCES workers(request_number)
|
| 65 |
-
)''')
|
| 66 |
-
c.execute('''CREATE TABLE IF NOT EXISTS pdfs (
|
| 67 |
-
request_number TEXT,
|
| 68 |
-
pdf_url TEXT,
|
| 69 |
-
FOREIGN KEY(request_number) REFERENCES workers(request_number)
|
| 70 |
-
)''')
|
| 71 |
-
conn.commit()
|
| 72 |
-
conn.close()
|
| 73 |
-
|
| 74 |
-
# นำเข้า JSON
|
| 75 |
-
def import_json_to_db():
|
| 76 |
-
try:
|
| 77 |
-
json_path = hf_hub_download(repo_id=REPO_ID, filename="worker-database.json", repo_type="space")
|
| 78 |
-
with open(json_path, 'r', encoding='utf-8') as f:
|
| 79 |
-
data = json.load(f)
|
| 80 |
-
|
| 81 |
-
conn = sqlite3.connect("workers.db")
|
| 82 |
-
c = conn.cursor()
|
| 83 |
-
for record in data:
|
| 84 |
-
c.execute('''INSERT OR IGNORE INTO workers (
|
| 85 |
-
request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number
|
| 86 |
-
) VALUES (?, ?, ?, ?, ?, ?, ?)''', (
|
| 87 |
-
record['เลขคำขอ'],
|
| 88 |
-
record['ชื่อภาษาอังกฤษ'],
|
| 89 |
-
record['หมายเลขอ้างอิงคนต่างด้าว'],
|
| 90 |
-
record['หมายเลขประจำตัว'],
|
| 91 |
-
record['สัญชาติ'],
|
| 92 |
-
record['เลขที่บนขวาใบเสร็จ'],
|
| 93 |
-
record['หมายเลขชำระเงิน']
|
| 94 |
-
))
|
| 95 |
-
conn.commit()
|
| 96 |
conn.close()
|
| 97 |
-
except Exception as e:
|
| 98 |
-
print(f"Error importing JSON: {e}")
|
| 99 |
-
|
| 100 |
-
# สำรองฐานข้อมูล
|
| 101 |
-
def backup_db():
|
| 102 |
-
try:
|
| 103 |
-
hf_hub_upload(repo_id=REPO_ID, path_in_repo="workers.db", path_or_fileobj="workers.db", repo_type="space")
|
| 104 |
-
except Exception as e:
|
| 105 |
-
print(f"Error backing up database: {e}")
|
| 106 |
-
|
| 107 |
-
# ดึงเลขคำขอถัดไป
|
| 108 |
-
def get_next_request_number():
|
| 109 |
-
conn = sqlite3.connect("workers.db")
|
| 110 |
-
c = conn.cursor()
|
| 111 |
-
c.execute("SELECT request_number FROM workers")
|
| 112 |
-
request_numbers = c.fetchall()
|
| 113 |
-
conn.close()
|
| 114 |
-
|
| 115 |
-
if not request_numbers:
|
| 116 |
-
return "WP-68-366-150"
|
| 117 |
-
|
| 118 |
-
max_num = max([int(num[0].split('-')[-1]) for num in request_numbers])
|
| 119 |
-
return f"WP-68-366-{max_num + 1:03d}"
|
| 120 |
-
|
| 121 |
-
# สร้าง PDF
|
| 122 |
-
def create_receipt_pdf(request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
|
| 123 |
-
filename = f"pdfs/receipt_{request_number}.pdf"
|
| 124 |
-
local_path = f"temp_{request_number}.pdf"
|
| 125 |
-
c = canvas.Canvas(local_path, pagesize=A4)
|
| 126 |
-
|
| 127 |
-
# ใช้ฟอนต์ที่มี หรือ Helvetica ถ้าไม่มี
|
| 128 |
-
try:
|
| 129 |
-
c.setFont("THSarabunBold", 18)
|
| 130 |
-
except:
|
| 131 |
-
c.setFont("Helvetica-Bold", 18)
|
| 132 |
-
|
| 133 |
-
c.drawString(2*cm, 28*cm, "Ministry of Labour")
|
| 134 |
-
|
| 135 |
-
try:
|
| 136 |
-
c.setFont("THSarabun", 12)
|
| 137 |
-
except:
|
| 138 |
-
c.setFont("Helvetica", 12)
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
c.drawString(2*cm, 25.5*cm, "Office: Bureau of Foreign Workers Administration")
|
| 143 |
-
c.drawString(2*cm, 25*cm, "Date: 01 April 2025")
|
| 144 |
-
c.drawString(2*cm, 24.5*cm, f"Payment Number: {payment_number}")
|
| 145 |
-
c.drawString(2*cm, 24*cm, f"Request Number: {request_number}")
|
| 146 |
-
c.drawString(2*cm, 23.5*cm, f"Payer Name: {english_name}")
|
| 147 |
-
c.drawString(2*cm, 23*cm, f"Nationality: {nationality}")
|
| 148 |
-
c.drawString(2*cm, 22.5*cm, f"Foreign Reference Number: {foreign_reference_number}")
|
| 149 |
-
c.drawString(2*cm, 22*cm, f"Foreign Worker ID: {id_number}")
|
| 150 |
-
c.drawString(2*cm, 21.5*cm, "Employer: Ban Kong Engineering Co., Ltd.")
|
| 151 |
-
c.drawString(2*cm, 21*cm, "Employer ID: 0415567000061")
|
| 152 |
-
|
| 153 |
-
try:
|
| 154 |
-
c.setFont("THSarabunBold", 12)
|
| 155 |
-
except:
|
| 156 |
-
c.setFont("Helvetica-Bold", 12)
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
c.setFont("THSarabun", 12)
|
| 162 |
-
except:
|
| 163 |
-
c.setFont("Helvetica", 12)
|
| 164 |
|
| 165 |
-
|
| 166 |
-
c.drawString(2*cm, 19*cm, "2. Service fee: 900.00")
|
| 167 |
-
c.drawString(2*cm, 18.5*cm, "Total amount (Baht): (One thousand Baht) 1,000.00")
|
| 168 |
-
c.drawString(2*cm, 18*cm, "Payment received correctly")
|
| 169 |
-
c.drawString(2*cm, 17.5*cm, "(Signature) Ms. Areewan Pho Nim Daeng (Receiver)")
|
| 170 |
-
c.drawString(2*cm, 17*cm, "Position: Senior Labor Academic")
|
| 171 |
-
|
| 172 |
-
# QR Code
|
| 173 |
-
pdf_url = f"https://huggingface.co/spaces/{REPO_ID}/raw/main/{filename}"
|
| 174 |
-
try:
|
| 175 |
-
qr = qrcode.make(pdf_url)
|
| 176 |
-
qr.save("temp_qr.png")
|
| 177 |
-
c.drawImage("temp_qr.png", 15*cm, 15*cm, width=3*cm, height=3*cm)
|
| 178 |
except Exception as e:
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
# เพิ่ม print timestamp ที่ด้านล่าง
|
| 182 |
-
try:
|
| 183 |
-
c.setFont("THSarabun", 8)
|
| 184 |
-
except:
|
| 185 |
-
c.setFont("Helvetica", 8)
|
| 186 |
-
|
| 187 |
-
print_time = thai_date_time(time.time())
|
| 188 |
-
c.drawString(2*cm, 2*cm, f"Printed: {print_time}")
|
| 189 |
-
|
| 190 |
-
c.showPage()
|
| 191 |
-
c.save()
|
| 192 |
-
|
| 193 |
-
# อัพโหลด PDF ไปยัง Space ใหม่
|
| 194 |
-
try:
|
| 195 |
-
hf_hub_upload(repo_id=REPO_ID, path_in_repo=filename, path_or_fileobj=local_path, repo_type="space")
|
| 196 |
-
except Exception as e:
|
| 197 |
-
print(f"Error uploading PDF to Space: {e}")
|
| 198 |
-
|
| 199 |
-
# อัพโหลดไปยัง Space เดิม (ถ้ามี)
|
| 200 |
-
if ORIGINAL_REPO_ID:
|
| 201 |
-
try:
|
| 202 |
-
hf_hub_upload(repo_id=ORIGINAL_REPO_ID, path_in_repo=filename, path_or_fileobj=local_path, repo_type="space")
|
| 203 |
-
except Exception as e:
|
| 204 |
-
print(f"Error uploading to original Space: {e}")
|
| 205 |
-
|
| 206 |
-
# บันทึกข้อมูล PDF
|
| 207 |
-
conn = sqlite3.connect("workers.db")
|
| 208 |
-
c = conn.cursor()
|
| 209 |
-
c.execute("INSERT OR REPLACE INTO pdfs (request_number, pdf_url) VALUES (?, ?)",
|
| 210 |
-
(request_number, pdf_url))
|
| 211 |
-
conn.commit()
|
| 212 |
-
conn.close()
|
| 213 |
-
|
| 214 |
-
return filename
|
| 215 |
-
|
| 216 |
-
# ตรวจสอบข้อมูล
|
| 217 |
-
def validate_input(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
|
| 218 |
-
if not id_number or not id_number.isdigit() or len(id_number) != 13:
|
| 219 |
-
return "หมายเลขประจำตัวต้องเป็นตัวเลข 13 หลัก"
|
| 220 |
-
if not receipt_number or not payment_number:
|
| 221 |
-
return "กรุณากรอกเลขที่ใบเสร็จและหมายเลขชำระเงิน"
|
| 222 |
-
return None
|
| 223 |
|
| 224 |
-
#
|
| 225 |
-
def add_worker(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number, attachments):
|
| 226 |
-
error = validate_input(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number)
|
| 227 |
-
if error:
|
| 228 |
-
return error
|
| 229 |
-
|
| 230 |
-
request_number = get_next_request_number()
|
| 231 |
-
|
| 232 |
-
# บันทึกข้อมูล
|
| 233 |
-
conn = sqlite3.connect("workers.db")
|
| 234 |
-
c = conn.cursor()
|
| 235 |
-
c.execute('''INSERT INTO workers (
|
| 236 |
-
request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number
|
| 237 |
-
) VALUES (?, ?, ?, ?, ?, ?, ?)''', (
|
| 238 |
-
request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number
|
| 239 |
-
))
|
| 240 |
-
|
| 241 |
-
# ���ัดการไฟล์แนบ
|
| 242 |
-
attachment_urls = []
|
| 243 |
-
for i, file_data in enumerate(attachments or []):
|
| 244 |
-
filename = f"attachments/file_{request_number}_{i}_{os.path.basename(file_data.name)}"
|
| 245 |
-
try:
|
| 246 |
-
hf_hub_upload(repo_id=REPO_ID, path_in_repo=filename, path_or_fileobj=file_data, repo_type="space")
|
| 247 |
-
url = f"https://huggingface.co/spaces/{REPO_ID}/raw/main/{filename}"
|
| 248 |
-
c.execute("INSERT INTO attachments (request_number, file_url) VALUES (?, ?)", (request_number, url))
|
| 249 |
-
attachment_urls.append(url)
|
| 250 |
-
except Exception as e:
|
| 251 |
-
print(f"Error uploading attachment: {e}")
|
| 252 |
-
|
| 253 |
-
conn.commit()
|
| 254 |
-
conn.close()
|
| 255 |
-
|
| 256 |
-
# สร้าง PDF
|
| 257 |
-
pdf_path = create_receipt_pdf(request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number)
|
| 258 |
-
|
| 259 |
-
# สำรองฐานข้อมูล
|
| 260 |
-
backup_db()
|
| 261 |
-
|
| 262 |
-
return f"เพิ่มข้อมูลพนักงานสำเร็จ! เลขคำขอ: {request_number}\nPDF: {pdf_path}\nไฟล์แนบ: {', '.join(attachment_urls) if attachment_urls else 'ไม่มีไฟล์แนบ'}"
|
| 263 |
-
|
| 264 |
-
# ดาวน์โหลด PDF ทั้งหมด
|
| 265 |
-
def download_all_pdfs():
|
| 266 |
-
conn = sqlite3.connect("workers.db")
|
| 267 |
-
c = conn.cursor()
|
| 268 |
-
c.execute("SELECT request_number, pdf_url FROM pdfs")
|
| 269 |
-
pdfs = c.fetchall()
|
| 270 |
-
conn.close()
|
| 271 |
-
|
| 272 |
-
zip_buffer = io.BytesIO()
|
| 273 |
-
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
| 274 |
-
for request_number, pdf_url in pdfs:
|
| 275 |
-
try:
|
| 276 |
-
local_path = hf_hub_download(repo_id=REPO_ID, filename=f"pdfs/receipt_{request_number}.pdf", repo_type="space")
|
| 277 |
-
zip_file.write(local_path, f"receipt_{request_number}.pdf")
|
| 278 |
-
except:
|
| 279 |
-
continue
|
| 280 |
-
|
| 281 |
-
zip_path = "all_receipts.zip"
|
| 282 |
-
with open(zip_path, 'wb') as f:
|
| 283 |
-
f.write(zip_buffer.getvalue())
|
| 284 |
-
|
| 285 |
-
try:
|
| 286 |
-
hf_hub_upload(repo_id=REPO_ID, path_in_repo="all_receipts.zip", path_or_fileobj=zip_path, repo_type="space")
|
| 287 |
-
except Exception as e:
|
| 288 |
-
print(f"Error uploading ZIP: {e}")
|
| 289 |
-
|
| 290 |
-
return f"pdfs/all_receipts.zip"
|
| 291 |
-
|
| 292 |
-
# เริ่มต้นฐานข้อมูลและนำเข้า JSON
|
| 293 |
init_db()
|
| 294 |
-
import_json_to_db()
|
| 295 |
|
| 296 |
-
#
|
| 297 |
-
with gr.Blocks(
|
| 298 |
-
gr.
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
| 311 |
|
| 312 |
-
gr.Markdown("
|
| 313 |
-
|
| 314 |
-
|
|
|
|
| 315 |
|
| 316 |
-
|
| 317 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
if not workers:
|
| 4 |
+
return "ไม่มีข้อมูลพนักงานในระบบ"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
result = f"📋 รายการพนักงานทั้งหมด ({len(workers)} คน)\n" + "="*50 + "\n"
|
| 7 |
+
for worker in workers:
|
| 8 |
+
result += f"🆔 {worker[0]} | {worker[1]} | {worker[2]} | {worker[3]}\n"
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
except Exception as e:
|
| 12 |
+
return f"❌ เกิดข้อผิดพลาด: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# เริ่มต้นฐานข้อมูล
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
init_db()
|
|
|
|
| 16 |
|
| 17 |
+
# UI Gradio
|
| 18 |
+
with gr.Blocks(
|
| 19 |
+
theme=gr.themes.Soft(),
|
| 20 |
+
title="ระบบจัดการข้อมูลพนักงานต่างด้าว",
|
| 21 |
+
css="""
|
| 22 |
+
.gradio-container {
|
| 23 |
+
max-width: 1200px !important;
|
| 24 |
+
margin: auto !important;
|
| 25 |
+
}
|
| 26 |
+
.form-container {
|
| 27 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 28 |
+
border-radius: 15px;
|
| 29 |
+
padding: 20px;
|
| 30 |
+
margin: 10px 0;
|
| 31 |
+
}
|
| 32 |
+
"""
|
| 33 |
+
) as demo:
|
| 34 |
|
| 35 |
+
gr.Markdown("""
|
| 36 |
+
# 🏢 ระบบจัดการข้อมูลพนักงานต่างด้าว
|
| 37 |
+
### Worker Management System - Ban Kong Engineering Co., Ltd.
|
| 38 |
+
""")
|
| 39 |
|
| 40 |
+
with gr.Tab("📝 เพิ่มข้อมูลพนั���งานใหม่"):
|
| 41 |
+
gr.Markdown("### กรอกข้อมูลพนักงานใหม่")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
english_name = gr.Textbox(
|
| 45 |
+
label="🏷️ ชื่อภาษาอังกฤษ",
|
| 46 |
+
placeholder="เช่น MR. AUNG KYAW",
|
| 47 |
+
lines=1
|
| 48 |
+
)
|
| 49 |
+
foreign_reference_number = gr.Textbox(
|
| 50 |
+
label="🔢 หมายเลขอ้างอิงคนต่างด้าว",
|
| 51 |
+
placeholder="เช่น 2492102076212",
|
| 52 |
+
lines=1
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
id_number = gr.Textbox(
|
| 57 |
+
label="🆔 หมายเลขประจำตัว (13 หลัก)",
|
| 58 |
+
placeholder="เช่น 6685490000472",
|
| 59 |
+
lines=1
|
| 60 |
+
)
|
| 61 |
+
nationality = gr.Dropdown(
|
| 62 |
+
label="🌍 สัญชาติ",
|
| 63 |
+
choices=["เมียนมา", "กัมพูชา", "ลาว", "เวียดนาม"],
|
| 64 |
+
value="เมียนมา"
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
receipt_number = gr.Textbox(
|
| 69 |
+
label="🧾 เลขที่บนขวาใบเสร็จ",
|
| 70 |
+
placeholder="เช่น 2100680001130",
|
| 71 |
+
lines=1
|
| 72 |
+
)
|
| 73 |
+
payment_number = gr.Textbox(
|
| 74 |
+
label="💳 หมายเลขชำระเงิน",
|
| 75 |
+
placeholder="เช่น IV680210/002350",
|
| 76 |
+
lines=1
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
attachments = gr.File(
|
| 80 |
+
label="📎 แนบไฟล์ (สูงสุด 5MB ต่อไฟล์)",
|
| 81 |
+
file_count="multiple",
|
| 82 |
+
file_types=[".jpg", ".png", ".pdf", ".jpeg"],
|
| 83 |
+
max_file_size="5MB"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
submit_btn = gr.Button(
|
| 87 |
+
"💾 บันทึกข้อมูลและสร้าง PDF",
|
| 88 |
+
variant="primary",
|
| 89 |
+
size="lg"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
output = gr.Textbox(
|
| 93 |
+
label="📋 ผลลัพธ์",
|
| 94 |
+
lines=6,
|
| 95 |
+
interactive=False
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
with gr.Tab("📊 จัดการข้อมูล"):
|
| 99 |
+
gr.Markdown("### ดูและจัดการข้อมูลพนักงาน")
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
view_workers_btn = gr.Button("👥 ดูรายการพนักงานทั้งหมด", variant="secondary")
|
| 103 |
+
download_all_btn = gr.Button("📥 ดาวน์โหลด PDF ทั้งหมด", variant="primary")
|
| 104 |
+
|
| 105 |
+
management_output = gr.Textbox(
|
| 106 |
+
label="📋 ผลลัพธ์",
|
| 107 |
+
lines=10,
|
| 108 |
+
interactive=False
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
with gr.Tab("ℹ️ คำแนะนำ"):
|
| 112 |
+
gr.Markdown("""
|
| 113 |
+
## 📖 วิธีการใช้งาน
|
| 114 |
+
|
| 115 |
+
### 1. เพิ่มข้อมูลพนักงานใหม่
|
| 116 |
+
- กรอกข้อมูลในฟอร์มให้ครบถ้วน
|
| 117 |
+
- หมายเลขประจำตัวต้องเป็นตัวเลข 13 หลัก
|
| 118 |
+
- สามารถแนบไฟล์เอกสารได้ (JPG, PNG, PDF)
|
| 119 |
+
- ระบบจะสร้าง PDF ใบเสร็จพร้อม QR Code อัตโนมัติ
|
| 120 |
+
|
| 121 |
+
### 2. ดาวน์โหลด PDF
|
| 122 |
+
- ใช้ QR Code ในใบเสร็จเพื่อดาวน์โหลด PDF แต่ละไฟล์
|
| 123 |
+
- หรือดาวน์โหลด PDF ทั้งหมดเป็นไฟล์ ZIP
|
| 124 |
+
|
| 125 |
+
### 3. ข้อมูลที่เก็บ
|
| 126 |
+
- ข้อมูลพนักงาน
|
| 127 |
+
- ไฟล์ PDF ใบเสร็จ
|
| 128 |
+
- ไฟล์แนบเอกสาร
|
| 129 |
+
- ประวัติการทำรายการ
|
| 130 |
+
|
| 131 |
+
### 4. คุณสมบัติ
|
| 132 |
+
- 🔒 ข้อมูลปลอดภัย บันทึกใน Hugging Face Space
|
| 133 |
+
- 📱 QR Code สำหรับดาวน์โหลดใบเสร็จ
|
| 134 |
+
- 📊 รายงานและสถิติ
|
| 135 |
+
- 💾 สำรองข้อมูลอัตโนมัติ
|
| 136 |
+
|
| 137 |
+
---
|
| 138 |
+
**🏢 พัฒนาโดย: Ban Kong Engineering Co., Ltd.**
|
| 139 |
+
""")
|
| 140 |
+
|
| 141 |
+
# Event handlers
|
| 142 |
+
submit_btn.click(
|
| 143 |
+
fn=add_worker,
|
| 144 |
+
inputs=[english_name, foreign_reference_number, id_number, nationality,
|
| 145 |
+
receipt_number, payment_number, attachments],
|
| 146 |
+
outputs=output
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
view_workers_btn.click(
|
| 150 |
+
fn=get_worker_list,
|
| 151 |
+
outputs=management_output
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
download_all_btn.click(
|
| 155 |
+
fn=download_all_pdfs,
|
| 156 |
+
outputs=management_output
|
| 157 |
+
)
|
| 158 |
|
| 159 |
+
if __name__ == "__main__":
|
| 160 |
+
demo.launch()
|