protae5544 commited on
Commit
1ecca81
·
verified ·
1 Parent(s): 8292137

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +480 -389
app.py CHANGED
@@ -116,35 +116,6 @@ def init_db():
116
  conn.close()
117
 
118
  # สำรองฐานข้อมูล
119
- def backup_db():
120
- try:
121
- upload_file(
122
- path_or_fileobj="workers.db",
123
- path_in_repo="workers.db",
124
- repo_id=REPO_ID,
125
- repo_type="space"
126
- )
127
- except Exception as e:
128
- print(f"Error backing up database: {e}")
129
-
130
- # ดึงเลขคำขอถัดไป
131
- def get_next_request_number():
132
- conn = sqlite3.connect("workers.db")
133
- c = conn.cursor()
134
- c.execute("SELECT request_number FROM workers")
135
- request_numbers = c.fetchall()
136
- conn.close()
137
-
138
- if not request_numbers:
139
- return "WP-68-366-150"
140
-
141
- try:
142
- max_num = max([int(num[0].split('-')[-1]) for num in request_numbers if num[0].startswith('WP-68-366-')])
143
- return f"WP-68-366-{max_num + 1:03d}"
144
- except:
145
- return "WP-68-366-150"
146
-
147
- # สร้าง PDF
148
  def create_receipt_pdf(request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
149
  filename = f"pdfs/receipt_{request_number}.pdf"
150
  local_path = f"temp_{request_number}.pdf"
@@ -153,95 +124,137 @@ def create_receipt_pdf(request_number, english_name, foreign_reference_number, i
153
  os.makedirs("pdfs", exist_ok=True)
154
 
155
  c = canvas.Canvas(local_path, pagesize=A4)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
- # ใช้ฟอนต์ไทยถ้ามี ไม่งั้นใช้ Helvetica
158
- if font_available:
159
- try:
160
- c.setFont("THSarabunBold", 18)
161
- except:
162
- c.setFont("Helvetica-Bold", 16)
163
- font_available = False
164
- else:
165
- c.setFont("Helvetica-Bold", 16)
166
-
167
- # Header
168
- c.drawString(2*cm, 28*cm, "กระทรวงแรงงาน" if font_available else "Ministry of Labour")
169
-
170
- if font_available:
171
- try:
172
- c.setFont("THSarabun", 14)
173
- except:
174
- c.setFont("Helvetica", 12)
175
- font_available = False
176
- else:
177
- c.setFont("Helvetica", 12)
178
-
179
- c.drawString(2*cm, 27.5*cm, "ใบเสร็จรับเงิน (ต้นฉบับ)" if font_available else "Receipt (Original)")
180
- c.drawString(2*cm, 26*cm, f"เลขที่: {receipt_number}" if font_available else f"Number: {receipt_number}")
181
- c.drawString(2*cm, 25.5*cm, "ที่ทำการ: สำนักบริหารแรงงานต่างด้าว" if font_available else "Office: Bureau of Foreign Workers Administration")
182
- c.drawString(2*cm, 25*cm, "วันที่: 01 เมษายน 2568" if font_available else "Date: 01 April 2025")
183
- c.drawString(2*cm, 24.5*cm, f"เลขที่ใบชำระเงิน: {payment_number}" if font_available else f"Payment Number: {payment_number}")
184
- c.drawString(2*cm, 24*cm, f"เลขรับคำขอที่: {request_number}" if font_available else f"Request Number: {request_number}")
185
- c.drawString(2*cm, 23.5*cm, f"ชื่อผู้ชำระเงิน: {english_name}" if font_available else f"Payer Name: {english_name}")
186
- c.drawString(2*cm, 23*cm, f"สัญชาติ: {nationality}" if font_available else f"Nationality: {nationality}")
187
- c.drawString(2*cm, 22.5*cm, f"เลขอ้างอิงคนต่างด้าว: {foreign_reference_number}" if font_available else f"Foreign Reference: {foreign_reference_number}")
188
- c.drawString(2*cm, 22*cm, f"หมายเลขประจำตัวคนต่างด้าว: {id_number}" if font_available else f"Foreign Worker ID: {id_number}")
189
- c.drawString(2*cm, 21.5*cm, "ชื่อนายจ้าง/สถานประกอบการ: ��ริษัท บาน กง เอ็นจิเนียริ่ง จำกัด" if font_available else "Employer: Ban Kong Engineering Co., Ltd.")
190
- c.drawString(2*cm, 21*cm, "เลขประจำตัวนายจ้าง: 0415567000061" if font_available else "Employer ID: 0415567000061")
191
-
192
- # รายการ
193
- if font_available:
194
- try:
195
- c.setFont("THSarabunBold", 14)
196
- except:
197
- c.setFont("Helvetica-Bold", 12)
198
- else:
199
- c.setFont("Helvetica-Bold", 12)
200
-
201
- c.drawString(2*cm, 20*cm, "รายการ" if font_available else "Items")
202
-
203
- if font_available:
204
- try:
205
- c.setFont("THSarabun", 12)
206
- except:
207
- c.setFont("Helvetica", 10)
208
- else:
209
- c.setFont("Helvetica", 10)
210
-
211
- c.drawString(2*cm, 19.5*cm, "1. ค่าธรรมเนียมในการยื่นคำขอ ฉบับละ 100 บาท: 100.00" if font_available else "1. Application fee 100 Baht each: 100.00")
212
- c.drawString(2*cm, 19*cm, "2. ค่าธรรมเนียม: 900.00" if font_available else "2. Service fee: 900.00")
213
- c.drawString(2*cm, 18.5*cm, "รวมเป็นเงินทั้งสิ้น (บาท): (หนึ่งพันบาทถ้วน) 1,000.00" if font_available else "Total (Baht): (One thousand Baht) 1,000.00")
214
- c.drawString(2*cm, 18*cm, "ได้รับเงินไว้เป็นการถูกต้องแล้ว" if font_available else "Payment received correctly")
215
- c.drawString(2*cm, 17.5*cm, "(ลงชื่อ) นางสาวอารีวรรณ โพธิ์นิ่มแดง (ผู้รับเงิน)" if font_available else "(Signature) Ms. Areewan Pho Nim Daeng")
216
- c.drawString(2*cm, 17*cm, "ตำแหน่ง: นักวิชาการแรงงานชำนาญการ" if font_available else "Position: Senior Labor Academic")
217
-
218
- # QR Code
 
 
 
 
 
 
 
 
 
 
 
 
219
  pdf_url = f"https://huggingface.co/spaces/{REPO_ID}/resolve/main/{filename}"
220
  try:
221
- qr = qrcode.QRCode(version=1, box_size=10, border=5)
222
  qr.add_data(pdf_url)
223
  qr.make(fit=True)
224
  qr_img = qr.make_image(fill_color="black", back_color="white")
225
  qr_img.save("temp_qr.png")
226
- c.drawImage("temp_qr.png", 15*cm, 15*cm, width=3*cm, height=3*cm)
227
  except Exception as e:
228
  print(f"Error creating QR code: {e}")
229
 
230
- # Timestamp
231
- if font_available:
232
- try:
233
- c.setFont("THSarabun", 8)
234
- except:
235
- c.setFont("Helvetica", 8)
236
- else:
237
- c.setFont("Helvetica", 8)
238
-
239
  print_time = thai_date_time(time.time())
240
- c.drawString(2*cm, 2*cm, f"พิมพ์เมื่อ: {print_time}" if font_available else f"Printed: {print_time}")
 
 
 
241
 
242
  c.showPage()
243
  c.save()
244
 
 
 
 
 
245
  # อัพโหลด PDF
246
  try:
247
  upload_file(
@@ -265,298 +278,376 @@ def create_receipt_pdf(request_number, english_name, foreign_reference_number, i
265
 
266
  return pdf_url
267
 
268
- # ตรวจสอบข้อมูล
269
- def validate_input(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
270
- if not english_name or not english_name.strip():
271
- return "กรุณากรอกชื่อภาษาอังกฤษ"
272
- if not foreign_reference_number or not foreign_reference_number.strip():
273
- return "กรุณากรอกหมายเลขอ้างอิงคนต่างด้าว"
274
- if not id_number or not id_number.isdigit() or len(id_number) != 13:
275
- return "หมายเลขประจำตัวต้องเป็นตัวเลข 13 หลัก"
276
- if not nationality or not nationality.strip():
277
- return "กรุณากรอกสัญชาติ"
278
- if not receipt_number or not receipt_number.strip():
279
- return "กรุณากรอกเลขที่ใบเสร็จ"
280
- if not payment_number or not payment_number.strip():
281
- return "กรุณากรอกหมายเลขชำระเงิน"
282
- return None
283
-
284
- # เพิ่มข้อมูลพนักงาน
285
- def add_worker(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number, attachments):
286
- # ตรวจสอบข้อมูล
287
- error = validate_input(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number)
288
- if error:
289
- return f"❌ {error}"
290
 
291
- try:
292
- request_number = get_next_request_number()
293
-
294
- # บันทึกข้อมูลในฐานข้อมูล
295
- conn = sqlite3.connect("workers.db")
296
- c = conn.cursor()
297
- c.execute('''INSERT INTO workers (
298
- request_number, english_name, foreign_reference_number, id_number,
299
- nationality, receipt_number, payment_number, created_at
300
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)''', (
301
- request_number, english_name, foreign_reference_number, id_number,
302
- nationality, receipt_number, payment_number, time.time()
303
- ))
304
-
305
- # จัดการไฟล์แนบ
306
- attachment_urls = []
307
- if attachments:
308
- for i, file_data in enumerate(attachments):
309
- if file_data is not None:
310
- try:
311
- filename = f"attachments/file_{request_number}_{i}_{os.path.basename(file_data.name)}"
312
- upload_file(
313
- path_or_fileobj=file_data.name,
314
- path_in_repo=filename,
315
- repo_id=REPO_ID,
316
- repo_type="space"
317
- )
318
- url = f"https://huggingface.co/spaces/{REPO_ID}/resolve/main/{filename}"
319
- c.execute("INSERT INTO attachments (request_number, file_url) VALUES (?, ?)",
320
- (request_number, url))
321
- attachment_urls.append(url)
322
- except Exception as e:
323
- print(f"Error uploading attachment: {e}")
324
-
325
- conn.commit()
326
- conn.close()
327
-
328
- # สร้าง PDF
329
- pdf_url = create_receipt_pdf(request_number, english_name, foreign_reference_number,
330
- id_number, nationality, receipt_number, payment_number)
331
-
332
- if pdf_url:
333
- # สำรองฐานข้อมูล
334
- backup_db()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
 
336
- result = f"✅ เพิ่มข้อมูลพนักง���นสำเร็จ!\n"
337
- result += f"📋 เลขคำขอ: {request_number}\n"
338
- result += f"📄 PDF: {pdf_url}\n"
339
- if attachment_urls:
340
- result += f"📎 ไฟล์แนบ: {len(attachment_urls)} ไฟล์"
341
- else:
342
- result += f"📎 ไฟล์แนบ: ไม่มี"
343
- return result
344
- else:
345
- return f"❌ เกิดข้อผิดพลาดในการสร้าง PDF สำหรับเลขคำขอ: {request_number}"
346
 
347
- except Exception as e:
348
- return f"❌ เกิดข้อผิดพลาด: {str(e)}"
349
-
350
- # ดาวน์โหลด PDF ทั้งหมด
351
- def download_all_pdfs():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
352
  try:
353
- conn = sqlite3.connect("workers.db")
354
- c = conn.cursor()
355
- c.execute("SELECT request_number, pdf_url FROM pdfs")
356
- pdfs = c.fetchall()
357
- conn.close()
358
-
359
- if not pdfs:
360
- return "❌ ไม่มี PDF ในระบบ"
361
-
362
- zip_buffer = io.BytesIO()
363
- with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
364
- for request_number, pdf_url in pdfs:
365
- try:
366
- # ดาวน์โหลด PDF จาก Space
367
- local_path = hf_hub_download(
368
- repo_id=REPO_ID,
369
- filename=f"pdfs/receipt_{request_number}.pdf",
370
- repo_type="space"
371
- )
372
- zip_file.write(local_path, f"receipt_{request_number}.pdf")
373
- except Exception as e:
374
- print(f"Error downloading PDF {request_number}: {e}")
375
- continue
376
-
377
- zip_path = "all_receipts.zip"
378
- with open(zip_path, 'wb') as f:
379
- f.write(zip_buffer.getvalue())
380
-
381
- # อัพโหลด ZIP file
382
  upload_file(
383
- path_or_fileobj=zip_path,
384
- path_in_repo="downloads/all_receipts.zip",
385
  repo_id=REPO_ID,
386
  repo_type="space"
387
  )
388
-
389
- download_url = f"https://huggingface.co/spaces/{REPO_ID}/resolve/main/downloads/all_receipts.zip"
390
- return f"✅ สร้างไฟล์ ZIP สำเร็จ!\n📥 ดาวน์โหลด: {download_url}\n📊 จำนวน PDF: {len(pdfs)} ไฟล์"
391
-
392
  except Exception as e:
393
- return f" เกิดข้อผิดพลาด: {str(e)}"
394
-
395
- # ดูรายการพนักงาน
396
- def get_worker_list():
397
- try:
398
- conn = sqlite3.connect("workers.db")
399
- c = conn.cursor()
400
- c.execute("""SELECT request_number, english_name, nationality, id_number,
401
- receipt_number, payment_number FROM workers ORDER BY request_number DESC""")
402
- workers = c.fetchall()
403
- conn.close()
404
-
405
- if not workers:
406
- return "ไม่มีข้อมูลพนักงานในระบบ"
407
-
408
- result = f"📋 รายการพนักงานทั้งหมด ({len(workers)} คน)\n" + "="*50 + "\n"
409
- for worker in workers:
410
- result += f"🆔 {worker[0]} | {worker[1]} | {worker[2]} | {worker[3]}\n"
411
-
412
- return result
413
- except Exception as e:
414
- return f"❌ เกิดข้อผิดพลาด: {str(e)}"
415
-
416
- # เริ่มต้นฐานข้อมูล
417
- init_db()
418
-
419
- # UI Gradio
420
- with gr.Blocks(
421
- theme=gr.themes.Soft(),
422
- title="ระบบจัดการข้อมูลพนักงานต่างด้าว",
423
- css="""
424
- .gradio-container {
425
- max-width: 1200px !important;
426
- margin: auto !important;
427
- }
428
- .form-container {
429
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
430
- border-radius: 15px;
431
- padding: 20px;
432
- margin: 10px 0;
433
- }
434
- """
435
- ) as demo:
436
-
437
- gr.Markdown("""
438
- # 🏢 ระบบจัดการข้อมูลพนักงานต่างด้าว
439
- ### Worker Management System - Ban Kong Engineering Co., Ltd.
440
- """)
441
-
442
- with gr.Tab("📝 เพิ่มข้อมูลพนักงานใหม่"):
443
- gr.Markdown("### กรอกข้อมูลพนักงานใหม่")
444
-
445
- with gr.Row():
446
- english_name = gr.Textbox(
447
- label="🏷️ ชื่อภาษาอังกฤษ",
448
- placeholder="เช่น MR. AUNG KYAW",
449
- lines=1
450
- )
451
- foreign_reference_number = gr.Textbox(
452
- label="🔢 หมายเลขอ้างอิงคนต่างด้าว",
453
- placeholder="เช่น 2492102076212",
454
- lines=1
455
- )
456
-
457
- with gr.Row():
458
- id_number = gr.Textbox(
459
- label="🆔 หมายเลขประจำตัว (13 หลัก)",
460
- placeholder="เช่น 6685490000472",
461
- lines=1
462
- )
463
- nationality = gr.Dropdown(
464
- label="🌍 สัญชาติ",
465
- choices=["เมียนมา", "กัมพูชา", "ลาว", "เวียดนาม"],
466
- value="เมียนมา"
467
- )
468
-
469
- with gr.Row():
470
- receipt_number = gr.Textbox(
471
- label="🧾 เลขที่บนขวาใบเสร็จ",
472
- placeholder="เช่น 2100680001130",
473
- lines=1
474
- )
475
- payment_number = gr.Textbox(
476
- label="💳 หมายเลขชำระเงิน",
477
- placeholder="เช่น IV680210/002350",
478
- lines=1
479
- )
480
-
481
- # แก้ไข File component - ลบ max_file_size
482
- attachments = gr.File(
483
- label="📎 แนบไฟล์",
484
- file_count="multiple",
485
- file_types=[".jpg", ".png", ".pdf", ".jpeg"]
486
- )
487
-
488
- submit_btn = gr.Button(
489
- "💾 บันทึกข้อมูลและสร้าง PDF",
490
- variant="primary",
491
- size="lg"
492
- )
493
-
494
- output = gr.Textbox(
495
- label="📋 ผลลัพธ์",
496
- lines=6,
497
- interactive=False
498
- )
499
-
500
- with gr.Tab("📊 จัดการข้อมูล"):
501
- gr.Markdown("### ดูและจัดการข้อมูลพนักงาน")
502
-
503
- with gr.Row():
504
- view_workers_btn = gr.Button("👥 ดูรายการพนักงานทั้งหมด", variant="secondary")
505
- download_all_btn = gr.Button("📥 ดาวน์โหลด PDF ทั้งหมด", variant="primary")
506
-
507
- management_output = gr.Textbox(
508
- label="📋 ผลลัพธ์",
509
- lines=10,
510
- interactive=False
511
- )
512
-
513
- with gr.Tab("ℹ️ คำแนะนำ"):
514
- gr.Markdown("""
515
- ## 📖 วิธีการใช้งาน
516
-
517
- ### 1. เพิ่มข้อมูลพนักงานใหม่
518
- - กรอกข้อมูลในฟอร์มให้ครบถ้วน
519
- - หมายเลขประจำตัวต้องเป็นตัวเลข 13 หลัก
520
- - สามารถแนบไฟล์เอกสารได้ (JPG, PNG, PDF)
521
- - ระบบจะสร้าง PDF ใบเสร็จพร้อม QR Code อัตโนมัติ
522
-
523
- ### 2. ดาวน์โหลด PDF
524
- - ใช้ QR Code ในใบเสร็จเพื่อดาวน์โหลด PDF แต่ละไฟล์
525
- - หรือดาวน์โหลด PDF ทั้งหมดเป็นไฟล์ ZIP
526
-
527
- ### 3. ข้อมูลที่เก็บ
528
- - ข้อมูลพนักงาน
529
- - ไฟล์ PDF ใบเสร็จ
530
- - ไฟล์แนบเอกสาร
531
- - ประวัติการทำรายการ
532
-
533
- ### 4. คุณสมบัติ
534
- - 🔒 ข้อมูลปลอดภัย บันทึกใน Hugging Face Space
535
- - 📱 QR Code สำหรับดาวน์โหลดใบเสร็จ
536
- - 📊 รายงานและสถิติ
537
- - 💾 สำรองข้อมูลอัตโนมัติ
538
-
539
- ---
540
- **🏢 พัฒนาโดย: Ban Kong Engineering Co., Ltd.**
541
- """)
542
-
543
- # Event handlers
544
- submit_btn.click(
545
- fn=add_worker,
546
- inputs=[english_name, foreign_reference_number, id_number, nationality,
547
- receipt_number, payment_number, attachments],
548
- outputs=output
549
- )
550
-
551
- view_workers_btn.click(
552
- fn=get_worker_list,
553
- outputs=management_output
554
- )
555
-
556
- download_all_btn.click(
557
- fn=download_all_pdfs,
558
- outputs=management_output
559
- )
560
-
561
- if __name__ == "__main__":
562
- demo.launch()
 
116
  conn.close()
117
 
118
  # สำรองฐานข้อมูล
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  def create_receipt_pdf(request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
120
  filename = f"pdfs/receipt_{request_number}.pdf"
121
  local_path = f"temp_{request_number}.pdf"
 
124
  os.makedirs("pdfs", exist_ok=True)
125
 
126
  c = canvas.Canvas(local_path, pagesize=A4)
127
+ width, height = A4
128
+
129
+ # กำหนดตำแหน่งและขนาดตามเอกสารต้นฉบับ
130
+ # ใช้ Helvetica สำหรับความเข้ากันได้
131
+
132
+ # หัวเอกสาร - กระทรวงแรงงาน
133
+ c.setFont("Helvetica-Bold", 20)
134
+ c.drawCentredText(width/2, height-80, "กระทรวงแรงงาน")
135
+
136
+ c.setFont("Helvetica-Bold", 16)
137
+ c.drawCentredText(width/2, height-110, "ใบเสร็จรับเงิน (ต้นฉบับ)")
138
+
139
+ # ข้อมูลส่วนหัว
140
+ c.setFont("Helvetica", 12)
141
+ y_pos = height - 150
142
+
143
+ c.drawString(50, y_pos, f"เลขที่: {receipt_number}")
144
+ c.drawString(350, y_pos, "แบบ ภ.1600-034")
145
+
146
+ y_pos -= 25
147
+ c.drawString(50, y_pos, "ที่ทำการ: สำนักบริหารแรงงานต่างด้าว")
148
+
149
+ y_pos -= 25
150
+ c.drawString(50, y_pos, "วันที่: 01 เมษายน 2568")
151
+
152
+ y_pos -= 25
153
+ c.drawString(50, y_pos, f"เลขที่ใบชำระเงิน: {payment_number}")
154
+
155
+ y_pos -= 25
156
+ c.drawString(50, y_pos, f"เลขรับคำขอที่: {request_number}")
157
 
158
+ # ข้อมูลผู้ชำระเงิน
159
+ y_pos -= 40
160
+ c.setFont("Helvetica-Bold", 12)
161
+ c.drawString(50, y_pos, "ข้อมูลผู้ชำระเงิน:")
162
+
163
+ y_pos -= 25
164
+ c.setFont("Helvetica", 12)
165
+ c.drawString(50, y_pos, f"ชื่อผู้ชำระเงิน: {english_name}")
166
+
167
+ y_pos -= 20
168
+ c.drawString(50, y_pos, f"สัญชาติ: {nationality}")
169
+
170
+ y_pos -= 20
171
+ c.drawString(50, y_pos, f"เลขอ้างอิงคนต่างด้าว: {foreign_reference_number}")
172
+
173
+ y_pos -= 20
174
+ c.drawString(50, y_pos, f"หมายเลขประจำตัวคนต่างด้าว: {id_number}")
175
+
176
+ # ข้อมูลนายจ้าง
177
+ y_pos -= 40
178
+ c.setFont("Helvetica-Bold", 12)
179
+ c.drawString(50, y_pos, "ข้อมูลนายจ้าง:")
180
+
181
+ y_pos -= 25
182
+ c.setFont("Helvetica", 12)
183
+ c.drawString(50, y_pos, "ชื่อนายจ้าง/สถานประกอบการ: บริษัท บาน กง เอ็นจิเนียริ่ง จำกัด")
184
+
185
+ y_pos -= 20
186
+ c.drawString(50, y_pos, "เลขประจำตัวนายจ้าง: 0415567000061")
187
+
188
+ # รายการค่าธรรมเนียม
189
+ y_pos -= 40
190
+ c.setFont("Helvetica-Bold", 14)
191
+ c.drawString(50, y_pos, "รายการค่าธรรมเนียม")
192
+
193
+ # วาดเส้นใต้หัวข้อ
194
+ c.line(50, y_pos-5, 500, y_pos-5)
195
+
196
+ y_pos -= 30
197
+ c.setFont("Helvetica", 12)
198
+ c.drawString(70, y_pos, "1. ค่าธรรมเนียมในการยื่นคำขอ ฉบับละ 100 บาท")
199
+ c.drawString(450, y_pos, "100.00")
200
+
201
+ y_pos -= 25
202
+ c.drawString(70, y_pos, "2. ค่าธรรมเนียมการพิจารณา")
203
+ c.drawString(450, y_pos, "900.00")
204
+
205
+ # เส้นคั่น
206
+ y_pos -= 15
207
+ c.line(400, y_pos, 500, y_pos)
208
+
209
+ y_pos -= 25
210
+ c.setFont("Helvetica-Bold", 12)
211
+ c.drawString(70, y_pos, "รวมเป็นเงินทั้งสิ้น (บาท): (หนึ่งพันบาทถ้วน)")
212
+ c.drawString(430, y_pos, "1,000.00")
213
+
214
+ # เส้นใต้ยอดรวม
215
+ c.line(400, y_pos-5, 500, y_pos-5)
216
+ c.line(400, y_pos-8, 500, y_pos-8)
217
+
218
+ # ข้อความรับรอง
219
+ y_pos -= 40
220
+ c.setFont("Helvetica", 12)
221
+ c.drawString(50, y_pos, "ได้รับเงินไว้เป็นการถูกต้องแล้ว")
222
+
223
+ # ลายเซ็น
224
+ y_pos -= 60
225
+ c.drawString(300, y_pos, "(ลงชื่อ) นางสาวอารีวรรณ โพธิ์นิ่มแดง")
226
+ c.drawString(350, y_pos-20, "(ผู้รับเงิน)")
227
+
228
+ y_pos -= 45
229
+ c.drawString(300, y_pos, "ตำแหน่ง: นักวิชาการแ���งงานชำนาญการ")
230
+
231
+ # QR Code ที่มุมขวาล่าง
232
  pdf_url = f"https://huggingface.co/spaces/{REPO_ID}/resolve/main/{filename}"
233
  try:
234
+ qr = qrcode.QRCode(version=1, box_size=8, border=2)
235
  qr.add_data(pdf_url)
236
  qr.make(fit=True)
237
  qr_img = qr.make_image(fill_color="black", back_color="white")
238
  qr_img.save("temp_qr.png")
239
+ c.drawImage("temp_qr.png", width-150, 50, width=80, height=80)
240
  except Exception as e:
241
  print(f"Error creating QR code: {e}")
242
 
243
+ # Timestamp ที่ด้านล่าง
244
+ c.setFont("Helvetica", 8)
 
 
 
 
 
 
 
245
  print_time = thai_date_time(time.time())
246
+ c.drawString(50, 30, f"พิมพ์เมื่อ: {print_time}")
247
+
248
+ # เส้นขอบกรอบเอกสาร
249
+ c.rect(30, 30, width-60, height-60, stroke=1, fill=0)
250
 
251
  c.showPage()
252
  c.save()
253
 
254
+ # เพิ่ม HTML version สำหรับ preview และ print
255
+ create_html_receipt(request_number, english_name, foreign_reference_number,
256
+ id_number, nationality, receipt_number, payment_number)
257
+
258
  # อัพโหลด PDF
259
  try:
260
  upload_file(
 
278
 
279
  return pdf_url
280
 
281
+ # เพิ่มฟังก์ชันสร้าง HTML version
282
+ def create_html_receipt(request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
283
+ html_filename = f"receipts/receipt_{request_number}.html"
284
+ local_html_path = f"temp_{request_number}.html"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285
 
286
+ os.makedirs("receipts", exist_ok=True)
287
+
288
+ pdf_url = f"https://huggingface.co/spaces/{REPO_ID}/resolve/main/pdfs/receipt_{request_number}.pdf"
289
+ print_time = thai_date_time(time.time())
290
+
291
+ html_content = f"""
292
+ <!DOCTYPE html>
293
+ <html lang="th">
294
+ <head>
295
+ <meta charset="UTF-8">
296
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
297
+ <title>ใบเสร็จรับเงิน - {request_number}</title>
298
+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/qrcode.min.js"></script>
299
+ <style>
300
+ @import url('https://fonts.googleapis.com/css2?family=Sarabun:wght@400;600;700&display=swap');
301
+
302
+ * {{
303
+ margin: 0;
304
+ padding: 0;
305
+ box-sizing: border-box;
306
+ }}
307
+
308
+ body {{
309
+ font-family: 'Sarabun', Arial, sans-serif;
310
+ background: #f5f5f5;
311
+ padding: 20px;
312
+ }}
313
+
314
+ .receipt-container {{
315
+ max-width: 800px;
316
+ margin: 0 auto;
317
+ background: white;
318
+ border: 2px solid #000;
319
+ padding: 30px;
320
+ box-shadow: 0 0 20px rgba(0,0,0,0.1);
321
+ }}
322
+
323
+ .header {{
324
+ text-align: center;
325
+ margin-bottom: 30px;
326
+ border-bottom: 2px solid #000;
327
+ padding-bottom: 20px;
328
+ }}
329
+
330
+ .header h1 {{
331
+ font-size: 24px;
332
+ font-weight: 700;
333
+ margin-bottom: 10px;
334
+ }}
335
+
336
+ .header h2 {{
337
+ font-size: 18px;
338
+ font-weight: 600;
339
+ }}
340
+
341
+ .info-section {{
342
+ margin-bottom: 25px;
343
+ }}
344
+
345
+ .info-row {{
346
+ display: flex;
347
+ justify-content: space-between;
348
+ margin-bottom: 8px;
349
+ padding: 5px 0;
350
+ }}
351
+
352
+ .info-label {{
353
+ font-weight: 600;
354
+ }}
355
+
356
+ .section-title {{
357
+ font-size: 16px;
358
+ font-weight: 700;
359
+ margin: 20px 0 15px 0;
360
+ border-bottom: 1px solid #ccc;
361
+ padding-bottom: 5px;
362
+ }}
363
+
364
+ .fee-table {{
365
+ width: 100%;
366
+ border-collapse: collapse;
367
+ margin: 20px 0;
368
+ }}
369
+
370
+ .fee-table th,
371
+ .fee-table td {{
372
+ border: 1px solid #000;
373
+ padding: 10px;
374
+ text-align: left;
375
+ }}
376
+
377
+ .fee-table th {{
378
+ background: #f0f0f0;
379
+ font-weight: 700;
380
+ }}
381
+
382
+ .amount {{
383
+ text-align: right;
384
+ font-weight: 600;
385
+ }}
386
+
387
+ .total-row {{
388
+ background: #f9f9f9;
389
+ font-weight: 700;
390
+ }}
391
+
392
+ .signature-section {{
393
+ margin-top: 40px;
394
+ display: flex;
395
+ justify-content: space-between;
396
+ align-items: flex-end;
397
+ }}
398
+
399
+ .signature-box {{
400
+ text-align: center;
401
+ }}
402
+
403
+ .qr-section {{
404
+ display: flex;
405
+ flex-direction: column;
406
+ align-items: center;
407
+ }}
408
+
409
+ .footer {{
410
+ margin-top: 30px;
411
+ text-align: center;
412
+ font-size: 12px;
413
+ color: #666;
414
+ border-top: 1px solid #ccc;
415
+ padding-top: 15px;
416
+ }}
417
+
418
+ .download-section {{
419
+ text-align: center;
420
+ margin: 20px 0;
421
+ padding: 15px;
422
+ background: #e3f2fd;
423
+ border-radius: 8px;
424
+ border: 1px solid #2196f3;
425
+ }}
426
+
427
+ .download-btn {{
428
+ background: #2196f3;
429
+ color: white;
430
+ padding: 10px 20px;
431
+ border: none;
432
+ border-radius: 5px;
433
+ cursor: pointer;
434
+ font-size: 16px;
435
+ margin: 5px;
436
+ text-decoration: none;
437
+ display: inline-block;
438
+ }}
439
+
440
+ .download-btn:hover {{
441
+ background: #1976d2;
442
+ }}
443
+
444
+ /* Print Styles */
445
+ @media print {{
446
+ body {{
447
+ background: white;
448
+ padding: 0;
449
+ }}
450
 
451
+ .receipt-container {{
452
+ box-shadow: none;
453
+ border: 2px solid #000;
454
+ max-width: none;
455
+ width: 100%;
456
+ }}
 
 
 
 
457
 
458
+ .download-section {{
459
+ display: none;
460
+ }}
461
+
462
+ .no-print {{
463
+ display: none;
464
+ }}
465
+
466
+ /* ปรับขนาดสำหรับการพิมพ์ */
467
+ .header h1 {{
468
+ font-size: 22px;
469
+ }}
470
+
471
+ .header h2 {{
472
+ font-size: 16px;
473
+ }}
474
+
475
+ .info-row {{
476
+ font-size: 14px;
477
+ }}
478
+
479
+ .fee-table {{
480
+ font-size: 14px;
481
+ }}
482
+ }}
483
+
484
+ @page {{
485
+ margin: 1cm;
486
+ size: A4;
487
+ }}
488
+ </style>
489
+ </head>
490
+ <body>
491
+ <div class="receipt-container">
492
+ <!-- Header -->
493
+ <div class="header">
494
+ <h1>กระทรวงแรงงาน</h1>
495
+ <h2>ใบเสร็จรับเงิน (ต้นฉบับ)</h2>
496
+ </div>
497
+
498
+ <!-- Download Section (ไม่แสดงเวลาพิมพ์) -->
499
+ <div class="download-section no-print">
500
+ <h3>🔗 ดาวน์โหลดเอกสาร</h3>
501
+ <a href="{pdf_url}" class="download-btn" target="_blank">📄 ดาวน์โหลด PDF</a>
502
+ <button onclick="window.print()" class="download-btn">🖨️ พิมพ์เอกสาร</button>
503
+ </div>
504
+
505
+ <!-- Receipt Info -->
506
+ <div class="info-section">
507
+ <div class="info-row">
508
+ <span class="info-label">เลขที่:</span>
509
+ <span>{receipt_number}</span>
510
+ <span class="info-label">แบบ ภ.1600-034</span>
511
+ </div>
512
+ <div class="info-row">
513
+ <span class="info-label">ที่ทำการ:</span>
514
+ <span>สำนักบริหารแรงงานต่างด้าว</span>
515
+ </div>
516
+ <div class="info-row">
517
+ <span class="info-label">วันที่:</span>
518
+ <span>01 เมษายน 2568</span>
519
+ </div>
520
+ <div class="info-row">
521
+ <span class="info-label">เลขที่ใบชำระเงิน:</span>
522
+ <span>{payment_number}</span>
523
+ </div>
524
+ <div class="info-row">
525
+ <span class="info-label">เลขรับคำขอที่:</span>
526
+ <span>{request_number}</span>
527
+ </div>
528
+ </div>
529
+
530
+ <!-- Payer Info -->
531
+ <div class="section-title">ข้อมูลผู้ชำระเงิน</div>
532
+ <div class="info-section">
533
+ <div class="info-row">
534
+ <span class="info-label">ชื่อผู้ชำระเงิน:</span>
535
+ <span>{english_name}</span>
536
+ </div>
537
+ <div class="info-row">
538
+ <span class="info-label">สัญชาติ:</span>
539
+ <span>{nationality}</span>
540
+ </div>
541
+ <div class="info-row">
542
+ <span class="info-label">เลขอ้างอิงคนต่างด้าว:</span>
543
+ <span>{foreign_reference_number}</span>
544
+ </div>
545
+ <div class="info-row">
546
+ <span class="info-label">หมายเลขประจำตัวคนต่างด้าว:</span>
547
+ <span>{id_number}</span>
548
+ </div>
549
+ </div>
550
+
551
+ <!-- Employer Info -->
552
+ <div class="section-title">ข้อมูลนายจ้าง</div>
553
+ <div class="info-section">
554
+ <div class="info-row">
555
+ <span class="info-label">ชื่อนายจ้าง/สถานประกอบการ:</span>
556
+ <span>บริษัท บาน กง เอ็นจิเนียริ่ง จำกัด</span>
557
+ </div>
558
+ <div class="info-row">
559
+ <span class="info-label">เลขประจำตัวนายจ้าง:</span>
560
+ <span>0415567000061</span>
561
+ </div>
562
+ </div>
563
+
564
+ <!-- Fee Table -->
565
+ <div class="section-title">รายการค่าธรรมเนียม</div>
566
+ <table class="fee-table">
567
+ <thead>
568
+ <tr>
569
+ <th>รายการ</th>
570
+ <th class="amount">จำนวนเงิน (บาท)</th>
571
+ </tr>
572
+ </thead>
573
+ <tbody>
574
+ <tr>
575
+ <td>1. ค่าธรรมเนียมในการยื่นคำขอ ฉบับละ 100 บาท</td>
576
+ <td class="amount">100.00</td>
577
+ </tr>
578
+ <tr>
579
+ <td>2. ค่าธรรมเนียมการพิจารณา</td>
580
+ <td class="amount">900.00</td>
581
+ </tr>
582
+ <tr class="total-row">
583
+ <td><strong>รวมเป็นเงินทั้งสิ้น (บาท): (หนึ่งพันบาทถ้วน)</strong></td>
584
+ <td class="amount"><strong>1,000.00</strong></td>
585
+ </tr>
586
+ </tbody>
587
+ </table>
588
+
589
+ <div style="margin: 20px 0; font-weight: 600;">
590
+ ได้รับเงินไว้เป็นการถูกต้องแล้ว
591
+ </div>
592
+
593
+ <!-- Signature Section -->
594
+ <div class="signature-section">
595
+ <div class="signature-box">
596
+ <div style="margin-bottom: 50px;">_______________________</div>
597
+ <div>(ลงชื่อ) นางสาวอารีวรรณ โพธิ์นิ่มแดง</div>
598
+ <div>(ผู้รับเงิน)</div>
599
+ <div style="margin-top: 10px;">ตำแหน่ง: นักวิชาการแรงงานชำนาญการ</div>
600
+ </div>
601
+
602
+ <div class="qr-section">
603
+ <div id="qrcode"></div>
604
+ <div style="font-size: 12px; margin-top: 5px;">สแกนเพื่อตรวจสอบ</div>
605
+ </div>
606
+ </div>
607
+
608
+ <!-- Footer -->
609
+ <div class="footer">
610
+ <div>พิมพ์เมื่อ: {print_time}</div>
611
+ <div style="margin-top: 5px;">เอกสารนี้ออกโดยระบบอัตโนมัติ - บริษัท บาน กง เอ็นจิเนียริ่ง จำกัด</div>
612
+ </div>
613
+ </div>
614
+
615
+ <script>
616
+ // สร้าง QR Code
617
+ QRCode.toCanvas(document.getElementById('qrcode'), '{pdf_url}', {{
618
+ width: 80,
619
+ height: 80,
620
+ margin: 1,
621
+ color: {{
622
+ dark: '#000000',
623
+ light: '#FFFFFF'
624
+ }}
625
+ }}, function (error) {{
626
+ if (error) console.error(error);
627
+ console.log('QR Code generated successfully!');
628
+ }});
629
+
630
+ // Auto-focus สำหรับการพิมพ์
631
+ window.addEventListener('load', function() {{
632
+ console.log('Receipt loaded successfully');
633
+ }});
634
+ </script>
635
+ </body>
636
+ </html>
637
+ """
638
+
639
+ # บันทึกไฟล์ HTML
640
+ with open(local_html_path, 'w', encoding='utf-8') as f:
641
+ f.write(html_content)
642
+
643
+ # อัพโหลด HTML
644
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
645
  upload_file(
646
+ path_or_fileobj=local_html_path,
647
+ path_in_repo=html_filename,
648
  repo_id=REPO_ID,
649
  repo_type="space"
650
  )
651
+ print(f"HTML uploaded successfully: {html_filename}")
 
 
 
652
  except Exception as e:
653
+ print(f"Error uploading HTML: {e}")