You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

3021 lines
113 KiB

  1. #!/usr/bin/env python3
  2. """
  3. Bag3 v3.2 – FPSMS job orders by plan date (this file is the maintained version).
  4. Uses the public API GET /py/job-orders and POST /py/job-order-print-submit (no login required).
  5. UI tuned for aged users: larger font, Traditional Chinese labels, prev/next date.
  6. Database print counts (py_job_order_print_submit):
  7. Each finished print run calls submit_job_order_print_submit() with jobOrderId, qty,
  8. and printChannel (DATAFLEX | LABEL | LASER). The server appends one row per call;
  9. GET /py/job-orders returns cumulative bagPrintedQty / labelPrintedQty / laserPrintedQty
  10. per job order. Re-printing the same job later adds another row (SUM increases).
  11. Bag2 is kept as a separate legacy v2.x line; do not assume Bag2 matches Bag3.
  12. Run: python Bag3.py
  13. """
  14. import errno
  15. import json
  16. import os
  17. import select
  18. import socket
  19. import sys
  20. import tempfile
  21. import threading
  22. import time
  23. import tkinter as tk
  24. from dataclasses import dataclass
  25. from datetime import date, datetime, timedelta
  26. from tkinter import messagebox, ttk
  27. from typing import Callable, Optional, Tuple
  28. import requests
  29. # UI "列印機" check uses short TCP probes; DataFlex may refuse connections briefly during E1005 recovery.
  30. _DATAFLEX_RECOVERY_GRACE_UNTIL: float = 0.0
  31. def touch_dataflex_recovery_grace(seconds: float = 22.0) -> None:
  32. """While host reset runs, avoid flashing printer status to red."""
  33. global _DATAFLEX_RECOVERY_GRACE_UNTIL
  34. u = time.time() + max(0.0, seconds)
  35. if u > _DATAFLEX_RECOVERY_GRACE_UNTIL:
  36. _DATAFLEX_RECOVERY_GRACE_UNTIL = u
  37. try:
  38. import serial
  39. except ImportError:
  40. serial = None # type: ignore
  41. try:
  42. import win32print # type: ignore[import]
  43. import win32ui # type: ignore[import]
  44. import win32con # type: ignore[import]
  45. import win32gui # type: ignore[import]
  46. except ImportError:
  47. win32print = None # type: ignore[assignment]
  48. win32ui = None # type: ignore[assignment]
  49. win32con = None # type: ignore[assignment]
  50. win32gui = None # type: ignore[assignment]
  51. try:
  52. from PIL import Image, ImageDraw, ImageFont, ImageOps
  53. try:
  54. from PIL import ImageWin # type: ignore
  55. except Exception:
  56. ImageWin = None # type: ignore[assignment]
  57. import qrcode
  58. _HAS_PIL_QR = True
  59. except ImportError:
  60. Image = None # type: ignore[assignment]
  61. ImageDraw = None # type: ignore[assignment]
  62. ImageFont = None # type: ignore[assignment]
  63. ImageOps = None # type: ignore[assignment]
  64. ImageWin = None # type: ignore[assignment]
  65. qrcode = None # type: ignore[assignment]
  66. _HAS_PIL_QR = False
  67. APP_VERSION = "3.2"
  68. DEFAULT_BASE_URL = os.environ.get("FPSMS_BASE_URL", "http://localhost:8090/api")
  69. # When run as PyInstaller exe, save settings next to the exe; otherwise next to script
  70. if getattr(sys, "frozen", False):
  71. _SETTINGS_DIR = os.path.dirname(sys.executable)
  72. else:
  73. _SETTINGS_DIR = os.path.dirname(os.path.abspath(__file__))
  74. # Bag3 has its own settings file so it doesn't share with Bag1/Bag2.
  75. SETTINGS_FILE = os.path.join(_SETTINGS_DIR, "bag3_settings.json")
  76. LASER_COUNTER_FILE = os.path.join(_SETTINGS_DIR, "bag3_last_batch_count.txt")
  77. DEFAULT_SETTINGS = {
  78. "api_ip": "localhost",
  79. "api_port": "8090",
  80. "dabag_ip": "",
  81. "dabag_port": "3008",
  82. "laser_ip": "192.168.17.10",
  83. "laser_port": "45678",
  84. # For 標簽機 on Windows, this is the Windows printer name, e.g. "TSC TTP-246M Pro"
  85. "label_com": "TSC TTP-246M Pro",
  86. }
  87. def load_settings() -> dict:
  88. """Load settings from JSON file; return defaults if missing or invalid."""
  89. try:
  90. if os.path.isfile(SETTINGS_FILE):
  91. with open(SETTINGS_FILE, "r", encoding="utf-8") as f:
  92. data = json.load(f)
  93. return {**DEFAULT_SETTINGS, **data}
  94. except Exception:
  95. pass
  96. return dict(DEFAULT_SETTINGS)
  97. def save_settings(settings: dict) -> None:
  98. """Save settings to JSON file."""
  99. with open(SETTINGS_FILE, "w", encoding="utf-8") as f:
  100. json.dump(settings, f, indent=2, ensure_ascii=False)
  101. def build_base_url(api_ip: str, api_port: str) -> str:
  102. ip = (api_ip or "localhost").strip()
  103. port = (api_port or "8090").strip()
  104. return f"http://{ip}:{port}/api"
  105. def try_printer_connection(printer_name: str, sett: dict) -> bool:
  106. """Try to connect to the selected printer (TCP IP:port or COM). Returns True if OK."""
  107. if printer_name == "打袋機 DataFlex":
  108. ip = (sett.get("dabag_ip") or "").strip()
  109. port_str = (sett.get("dabag_port") or "3008").strip()
  110. if not ip:
  111. return False
  112. try:
  113. port = int(port_str)
  114. except ValueError:
  115. return False
  116. # Retry once: firmware often busy for ~1s after E1005 / blank label.
  117. timeout = max(PRINTER_SOCKET_TIMEOUT, 6.0)
  118. for attempt in range(2):
  119. try:
  120. s = socket.create_connection((ip, port), timeout=timeout)
  121. s.close()
  122. return True
  123. except (socket.error, OSError):
  124. if attempt == 0:
  125. time.sleep(0.35)
  126. continue
  127. return False
  128. if printer_name == "激光機":
  129. ip = (sett.get("laser_ip") or "").strip()
  130. port_str = (sett.get("laser_port") or "45678").strip()
  131. if not ip:
  132. return False
  133. try:
  134. port = int(port_str)
  135. s = socket.create_connection((ip, port), timeout=PRINTER_SOCKET_TIMEOUT)
  136. s.close()
  137. return True
  138. except (socket.error, ValueError, OSError):
  139. return False
  140. if printer_name == "標簽機":
  141. target = (sett.get("label_com") or "").strip()
  142. if not target:
  143. return False
  144. # On Windows, allow using a Windows printer name (e.g. "TSC TTP-246M Pro")
  145. # as an alternative to a COM port. If it doesn't look like a COM port,
  146. # try opening it via the Windows print spooler.
  147. if os.name == "nt" and not target.upper().startswith("COM"):
  148. if win32print is None:
  149. return False
  150. try:
  151. handle = win32print.OpenPrinter(target)
  152. win32print.ClosePrinter(handle)
  153. return True
  154. except Exception:
  155. return False
  156. # Fallback: treat as serial COM port (original behaviour)
  157. if serial is None:
  158. return False
  159. try:
  160. ser = serial.Serial(target, timeout=1)
  161. ser.close()
  162. return True
  163. except (serial.SerialException, OSError):
  164. return False
  165. return False
  166. # Larger font for aged users (point size)
  167. FONT_SIZE = 16
  168. FONT_SIZE_BUTTONS = 15
  169. # Printer selector: field + dropdown (use tk.OptionMenu so menu font is respected on Windows)
  170. FONT_SIZE_COMBO = 18
  171. FONT_SIZE_QTY = 12 # smaller for 需求數量 under batch no.
  172. FONT_SIZE_META = 11 # single-line 需求/已印 (compact list)
  173. # Less vertical padding so ~30 rows fit more comfortably
  174. LIST_ROW_PADY = 2
  175. LIST_ROW_IPADY = 5
  176. FONT_SIZE_ITEM = 20 # item code and item name (larger for readability)
  177. FONT_FAMILY = "Microsoft JhengHei UI" # Traditional Chinese; fallback to TkDefaultFont
  178. FONT_SIZE_ITEM_CODE = 20 # item code (larger for readability)
  179. FONT_SIZE_ITEM_NAME = 26 # item name (bigger than item code)
  180. # Column widths: fixed frame widths so 品號/品名 columns line up across rows
  181. LEFT_COL_WIDTH_PX = 300 # 工單 + 需求/已印 block
  182. ITEM_CODE_WRAP = 140 # Label wraplength (px)
  183. # Narrower than wrap+padding so short codes sit closer to 品名 (still aligned across rows)
  184. CODE_COL_WIDTH_PX = ITEM_CODE_WRAP + 6
  185. ITEM_NAME_WRAP = 640 # item name wraps in remaining space
  186. # Light blue theme (softer than pure grey)
  187. BG_TOP = "#E8F4FC"
  188. BG_LIST = "#D4E8F7"
  189. BG_ROOT = "#E1F0FF"
  190. BG_ROW = "#C5E1F5"
  191. BG_ROW_SELECTED = "#6BB5FF" # highlighted when selected (for printing)
  192. # Connection status bar
  193. BG_STATUS_ERROR = "#FFCCCB" # red background when disconnected
  194. FG_STATUS_ERROR = "#B22222" # red text
  195. BG_STATUS_OK = "#90EE90" # light green when connected
  196. FG_STATUS_OK = "#006400" # green text
  197. RETRY_MS = 30 * 1000 # 30 seconds reconnect
  198. # POST /py/job-order-print-submit: retries when server is briefly unavailable
  199. PRINT_SUBMIT_MAX_ATTEMPTS = 5
  200. PRINT_SUBMIT_RETRY_DELAY_SEC = 1.5
  201. PRINTER_CHECK_MS = 60 * 1000 # 1 minute when printer OK
  202. PRINTER_RETRY_MS = 30 * 1000 # 30 seconds when printer failed
  203. PRINTER_SOCKET_TIMEOUT = 3
  204. DATAFLEX_SEND_TIMEOUT = 10 # seconds when sending ZPL to DataFlex
  205. def _dataflex_float_env(name: str, default: float) -> float:
  206. raw = (os.environ.get(name) or "").strip()
  207. if not raw:
  208. return default
  209. try:
  210. return float(raw)
  211. except ValueError:
  212. return default
  213. def _dataflex_bool_env(name: str, default: bool) -> bool:
  214. raw = (os.environ.get(name) or "").strip().lower()
  215. if not raw:
  216. return default
  217. return raw in ("1", "true", "yes", "on")
  218. def _dataflex_int_env(name: str, default: int) -> int:
  219. raw = (os.environ.get(name) or "").strip()
  220. if not raw:
  221. return default
  222. try:
  223. return int(raw)
  224. except ValueError:
  225. return default
  226. # Job list auto-refresh interval (ms). 0 = off (list rebuild can hide DataFlex「停止列印」).
  227. # Re-enable: FPSMS_JOB_LIST_REFRESH_MS=60000
  228. JOB_LIST_AUTO_REFRESH_MS = max(0, _dataflex_int_env("FPSMS_JOB_LIST_REFRESH_MS", 0))
  229. # Defer full list rebuild while printing; retry after this interval until idle. FPSMS_JOB_LIST_DEFER_WHILE_PRINTING_MS
  230. JOB_LIST_DEFER_WHILE_PRINTING_MS = max(
  231. 500,
  232. _dataflex_int_env("FPSMS_JOB_LIST_DEFER_WHILE_PRINTING_MS", 1500),
  233. )
  234. # Gap between bag labels (after each job has fully left the client). Tune if bags are blank/skipped.
  235. # Override: FPSMS_DATAFLEX_INTER_LABEL_DELAY_SEC (e.g. 3.5 if ~3–5% blanks per 100).
  236. DATAFLEX_INTER_LABEL_DELAY_SEC = _dataflex_float_env(
  237. "FPSMS_DATAFLEX_INTER_LABEL_DELAY_SEC", 0.3
  238. )
  239. # Brief pause after each ZPL send so firmware can commit before we FIN the socket (reduces dropped/blank jobs).
  240. # Override: FPSMS_DATAFLEX_POST_LABEL_SETTLE_SEC
  241. DATAFLEX_POST_LABEL_SETTLE_SEC = _dataflex_float_env(
  242. "FPSMS_DATAFLEX_POST_LABEL_SETTLE_SEC", 0.08
  243. )
  244. # Before each print job: light reset only (~JA + ~RO). Must finish before first ^XA or first label can be lost.
  245. # Override: FPSMS_DATAFLEX_POST_PREPRINT_DELAY_SEC
  246. DATAFLEX_PREPRINT_BYTES = b"~JA\r\n~RO1\r\n~RO2\r\n"
  247. DATAFLEX_POST_PREPRINT_DELAY_SEC = _dataflex_float_env(
  248. "FPSMS_DATAFLEX_POST_PREPRINT_DELAY_SEC", 0.55
  249. )
  250. # Whether each new print job starts with full reset (~JR) so DataFlex batch counter returns to 0.
  251. # Set FPSMS_DATAFLEX_FULL_RESET_EACH_JOB=0 to keep only light preprint reset.
  252. DATAFLEX_FULL_RESET_EACH_JOB = _dataflex_bool_env(
  253. "FPSMS_DATAFLEX_FULL_RESET_EACH_JOB", False
  254. )
  255. # Extra-safe mode: run light preprint reset (~JA/~RO) before EVERY bag.
  256. # Slower, but reduces E1005 on unstable firmware.
  257. DATAFLEX_PREPRINT_EACH_LABEL = _dataflex_bool_env(
  258. "FPSMS_DATAFLEX_PREPRINT_EACH_LABEL", False
  259. )
  260. DATAFLEX_VERIFY_STATUS_AFTER_SEND = _dataflex_bool_env(
  261. "FPSMS_DATAFLEX_VERIFY_STATUS_AFTER_SEND", False
  262. )
  263. # Hard-disable automatic reset/counter commands during printing.
  264. # When False, normal print path sends ZPL only (no ~JA/~RO/~JR).
  265. DATAFLEX_AUTO_RESET_ENABLED = _dataflex_bool_env(
  266. "FPSMS_DATAFLEX_AUTO_RESET_ENABLED", False
  267. )
  268. # After a failed TCP send, always run host recovery + retry (recommended when E1005 stops the run).
  269. DATAFLEX_RECOVER_ON_SEND_ERROR = _dataflex_bool_env(
  270. "FPSMS_DATAFLEX_RECOVER_ON_SEND_ERROR", True
  271. )
  272. DATAFLEX_STATUS_QUERY_TIMEOUT_SEC = _dataflex_float_env(
  273. "FPSMS_DATAFLEX_STATUS_QUERY_TIMEOUT_SEC", 0.8
  274. )
  275. DATAFLEX_RECOVERY_MAX_ATTEMPTS = max(
  276. 1,
  277. _dataflex_int_env("FPSMS_DATAFLEX_RECOVERY_MAX_ATTEMPTS", 2),
  278. )
  279. DATAFLEX_RECOVERY_WAIT_SEC = _dataflex_float_env(
  280. "FPSMS_DATAFLEX_RECOVERY_WAIT_SEC", 0.8
  281. )
  282. # Prevent cumulative thermal/mechanical fault in long runs (E1000 after ~40 bags on some units):
  283. # pause briefly every N bags.
  284. DATAFLEX_COOLDOWN_EVERY_LABELS = max(
  285. 0,
  286. _dataflex_int_env("FPSMS_DATAFLEX_COOLDOWN_EVERY_LABELS", 8),
  287. )
  288. DATAFLEX_COOLDOWN_SEC = _dataflex_float_env(
  289. "FPSMS_DATAFLEX_COOLDOWN_SEC", 3.5
  290. )
  291. # Extra long pause every M bags (head cool-down). 0 = off. FPSMS_DATAFLEX_THERMAL_REST_EVERY_LABELS
  292. DATAFLEX_THERMAL_REST_EVERY_LABELS = max(
  293. 0,
  294. _dataflex_int_env("FPSMS_DATAFLEX_THERMAL_REST_EVERY_LABELS", 20),
  295. )
  296. DATAFLEX_THERMAL_REST_SEC = _dataflex_float_env(
  297. "FPSMS_DATAFLEX_THERMAL_REST_SEC", 5.0
  298. )
  299. # Light ~HS check every N bags. Default off — periodic checks + recovery caused long stalls with E1005 on some units.
  300. DATAFLEX_VERIFY_EVERY_LABELS = max(
  301. 0,
  302. _dataflex_int_env("FPSMS_DATAFLEX_VERIFY_EVERY_LABELS", 0),
  303. )
  304. # Status bar progress while printing (main thread). 0 = off.
  305. DATAFLEX_UI_PROGRESS_EVERY = max(
  306. 0,
  307. _dataflex_int_env("FPSMS_DATAFLEX_UI_PROGRESS_EVERY", 5),
  308. )
  309. # One TCP send: single ZPL with ^PQn (n identical bags). Some DataFlex units may fault (E1005); default off.
  310. DATAFLEX_SINGLE_TCP_JOB = _dataflex_bool_env(
  311. "FPSMS_DATAFLEX_SINGLE_TCP_JOB", False
  312. )
  313. # Link-OS SGD: raw-ZPL job shows this host id instead of a generic name (e.g. "ZPL. EMULATION").
  314. # Same id when the same job order is printed again. Disable: FPSMS_DATAFLEX_HOST_IDENTIFICATION_SGD=0
  315. DATAFLEX_HOST_IDENTIFICATION_SGD = _dataflex_bool_env(
  316. "FPSMS_DATAFLEX_HOST_IDENTIFICATION_SGD", True
  317. )
  318. # Bag ZPL size (dots). ^PW700 matched little content (mostly vertical ^A@R), so previews showed a wide strip with empty right margin.
  319. DATAFLEX_LABEL_PW = max(
  320. 280,
  321. _dataflex_int_env("FPSMS_DATAFLEX_LABEL_PW", 400),
  322. )
  323. DATAFLEX_LABEL_LL = max(
  324. 200,
  325. _dataflex_int_env("FPSMS_DATAFLEX_LABEL_LL", 500),
  326. )
  327. # Some Zebra/DataFlex units RST the socket on host half-close; Windows surfaces WinError 10054.
  328. # Set FPSMS_DATAFLEX_SKIP_SHUTDOWN_WR=1 to omit shutdown(SHUT_WR) and only close() (often avoids RST).
  329. DATAFLEX_SKIP_SHUTDOWN_WR = _dataflex_bool_env(
  330. "FPSMS_DATAFLEX_SKIP_SHUTDOWN_WR", False
  331. )
  332. # Full recovery (~JR soft reset) — used by「打袋重設」only; longer delay for firmware
  333. DATAFLEX_POST_FULL_RECOVERY_DELAY_SEC = 1.2
  334. # Zebra ~RO only (used when FPSMS_DATAFLEX_NO_JR is set for full recovery)
  335. DATAFLEX_RESET_BYTES = b"~RO1\r\n~RO2\r\n"
  336. # Full host recovery: ~JA clear buffers, ~RO counters, ~JR soft reset (clears latched errors without power cycle)
  337. DATAFLEX_FULL_RECOVERY_BYTES = b"~JA\r\n~RO1\r\n~RO2\r\n~JR\r\n"
  338. def _dataflex_full_recovery_payload() -> bytes:
  339. """~JA+~RO+~JR for manual「打袋重設」; set env FPSMS_DATAFLEX_NO_JR=1 to skip ~JR."""
  340. if os.environ.get("FPSMS_DATAFLEX_NO_JR", "").strip().lower() in ("1", "true", "yes"):
  341. return b"~JA\r\n" + DATAFLEX_RESET_BYTES
  342. return DATAFLEX_FULL_RECOVERY_BYTES
  343. def _zpl_escape(s: str) -> str:
  344. """Escape text for ZPL ^FD...^FS (backslash and caret)."""
  345. return s.replace("\\", "\\\\").replace("^", "\\^")
  346. def _dataflex_host_identification_sgd_prefix(job_order_id: Optional[int]) -> str:
  347. """
  348. Optional ASCII prefix before ^XA: set zpl.host_identification so the printer lists the job
  349. under the job order id instead of a generic raw-ZPL label.
  350. """
  351. if not DATAFLEX_HOST_IDENTIFICATION_SGD or job_order_id is None:
  352. return ""
  353. try:
  354. jid = str(int(job_order_id))
  355. except (TypeError, ValueError):
  356. return ""
  357. if not jid.isdigit():
  358. return ""
  359. return f'! U1 setvar "zpl.host_identification" "{jid}"\r\n'
  360. def _dataflex_zpl_bytes(zpl: str) -> bytes:
  361. """UTF-8 ZPL with one trailing CRLF so the printer sees a clear job boundary."""
  362. s = (zpl or "").rstrip("\r\n")
  363. return (s + "\r\n").encode("utf-8")
  364. def _dataflex_is_benign_tcp_reset(err: BaseException) -> bool:
  365. """True when peer closed with RST/FIN in a way that is normal for raw printer TCP (Windows 10054)."""
  366. if isinstance(err, (BrokenPipeError, ConnectionResetError, ConnectionAbortedError)):
  367. return True
  368. if isinstance(err, OSError):
  369. if getattr(err, "winerror", None) == 10054: # WSAECONNRESET
  370. return True
  371. if err.errno in (
  372. errno.ECONNRESET,
  373. errno.EPIPE,
  374. errno.ECONNABORTED,
  375. ):
  376. return True
  377. return False
  378. def _dataflex_shutdown_write_maybe(sock: socket.socket) -> None:
  379. """Half-close write side; ignore printer RST (common after ZPL on port 9100-style links)."""
  380. if DATAFLEX_SKIP_SHUTDOWN_WR:
  381. return
  382. try:
  383. sock.shutdown(socket.SHUT_WR)
  384. except OSError as e:
  385. if _dataflex_is_benign_tcp_reset(e):
  386. return
  387. raise
  388. def generate_zpl_dataflex(
  389. batch_no: str,
  390. item_code: str,
  391. item_name: str,
  392. item_id: Optional[int] = None,
  393. stock_in_line_id: Optional[int] = None,
  394. lot_no: Optional[str] = None,
  395. job_order_id: Optional[int] = None,
  396. font_regular: str = "E:STXihei.ttf",
  397. font_bold: str = "E:STXihei.ttf",
  398. ) -> str:
  399. """
  400. Row 1 (from zero): QR code, then item name (rotated 90°).
  401. Row 2: Batch/lot (left), item code (right).
  402. Label and QR use lotNo from API when present, else batch_no (Bxxxxx).
  403. Light preprint (~JA/~RO) is sent before labels; full ~JR recovery is only for「打袋重設」.
  404. """
  405. desc = _zpl_escape((item_name or "—").strip())
  406. code = _zpl_escape((item_code or "—").strip())
  407. label_line = (lot_no or batch_no or "").strip()
  408. label_esc = _zpl_escape(label_line)
  409. # QR payload: prefer JSON {"itemId":..., "stockInLineId":...} when both present; else fall back to lot/batch text
  410. if item_id is not None and stock_in_line_id is not None:
  411. qr_payload = json.dumps({"itemId": item_id, "stockInLineId": stock_in_line_id})
  412. else:
  413. qr_payload = label_line if label_line else batch_no.strip()
  414. qr_value = _zpl_escape(qr_payload)
  415. # Explicit ^PQ1: each ^XA…^XZ is exactly one bag. Avoids E1005 "over quantity" on some Zebra/DataFlex
  416. # firmware when many labels are sent on one TCP session without a per-job quantity.
  417. host_id = _dataflex_host_identification_sgd_prefix(job_order_id)
  418. return host_id + f"""^XA
  419. ^PQ1,0,1,N
  420. ^CI28
  421. ^PW{DATAFLEX_LABEL_PW}
  422. ^LL{DATAFLEX_LABEL_LL}
  423. ^PO N
  424. ^FO10,20
  425. ^BQN,2,4^FDQA,{qr_value}^FS
  426. ^FO170,20
  427. ^A@R,72,72,{font_regular}^FD{desc}^FS
  428. ^FO0,200
  429. ^A@R,72,72,{font_regular}^FD{label_esc}^FS
  430. ^FO55,200
  431. ^A@R,88,88,{font_bold}^FD{code}^FS
  432. ^XZ"""
  433. def dataflex_zpl_set_print_quantity(zpl: str, copies: int) -> str:
  434. """
  435. Replace the fixed ^PQ1 line from generate_zpl_dataflex() with ^PQn so one ZPL job prints
  436. n identical bags over one TCP connection.
  437. """
  438. if copies < 1:
  439. copies = 1
  440. old = "^PQ1,0,1,N"
  441. if old not in zpl:
  442. raise RuntimeError(
  443. "DataFlex ZPL 缺少預期的 ^PQ1 列(無法改為單次連線多張)。"
  444. )
  445. return zpl.replace(old, f"^PQ{copies},0,1,N", 1)
  446. def send_dataflex_preprint_reset(ip: str, port: int, *, force: bool = False) -> None:
  447. """
  448. Fast prep before printing: ~JA + ~RO (no ~JR). Clears buffer and zeros batch counters so the first
  449. bag starts quickly. Use before fixed-qty batch and continuous mode.
  450. """
  451. if not force and not DATAFLEX_AUTO_RESET_ENABLED:
  452. return
  453. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  454. try:
  455. sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
  456. except OSError:
  457. pass
  458. sock.settimeout(DATAFLEX_SEND_TIMEOUT)
  459. try:
  460. sock.connect((ip, port))
  461. sock.sendall(DATAFLEX_PREPRINT_BYTES)
  462. time.sleep(DATAFLEX_POST_PREPRINT_DELAY_SEC)
  463. _dataflex_shutdown_write_maybe(sock)
  464. finally:
  465. sock.close()
  466. def send_dataflex_job_counter_reset(ip: str, port: int, *, force: bool = False) -> None:
  467. """
  468. Full host recovery for「打袋重設」: ~JA, ~RO, and ~JR (soft reset) to clear latched E1005.
  469. Slower than [send_dataflex_preprint_reset]; do not use on every row click.
  470. """
  471. if not force and not DATAFLEX_AUTO_RESET_ENABLED:
  472. return
  473. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  474. try:
  475. sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
  476. except OSError:
  477. pass
  478. sock.settimeout(DATAFLEX_SEND_TIMEOUT)
  479. try:
  480. sock.connect((ip, port))
  481. sock.sendall(_dataflex_full_recovery_payload())
  482. time.sleep(DATAFLEX_POST_FULL_RECOVERY_DELAY_SEC)
  483. _dataflex_shutdown_write_maybe(sock)
  484. finally:
  485. sock.close()
  486. def send_dataflex_start_job_reset(ip: str, port: int, *, force: bool = False) -> None:
  487. """
  488. Start-of-job reset sequence.
  489. Full reset first (default) ensures printer-side batch quantity returns to 0 for each job;
  490. then light preprint reset prepares the first bag send.
  491. Use force=True for the start of each print job and when selecting a job row so batch
  492. counter resets even if FPSMS_DATAFLEX_AUTO_RESET_ENABLED=0 (that flag mainly gates
  493. extra per-label / recovery traffic).
  494. """
  495. if not force and not DATAFLEX_AUTO_RESET_ENABLED:
  496. return
  497. if DATAFLEX_FULL_RESET_EACH_JOB:
  498. send_dataflex_job_counter_reset(ip, port, force=force)
  499. send_dataflex_preprint_reset(ip, port, force=force)
  500. def send_dataflex_reset_and_labels(
  501. ip: str,
  502. port: int,
  503. zpl: str,
  504. copies: int,
  505. delay_sec: float,
  506. ) -> None:
  507. """
  508. One TCP connection: light preprint (~JA + ~RO), short pause, then `copies` identical ZPL labels
  509. with delay_sec between copies (not after the last). Avoids rapid connect/disconnect per bag.
  510. """
  511. if copies < 1:
  512. return
  513. raw_zpl = _dataflex_zpl_bytes(zpl)
  514. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  515. try:
  516. sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
  517. except OSError:
  518. pass
  519. sock.settimeout(DATAFLEX_SEND_TIMEOUT)
  520. try:
  521. sock.connect((ip, port))
  522. sock.sendall(DATAFLEX_PREPRINT_BYTES)
  523. time.sleep(DATAFLEX_POST_PREPRINT_DELAY_SEC)
  524. for i in range(copies):
  525. sock.sendall(raw_zpl)
  526. time.sleep(DATAFLEX_POST_LABEL_SETTLE_SEC)
  527. if i < copies - 1:
  528. time.sleep(delay_sec)
  529. _dataflex_shutdown_write_maybe(sock)
  530. finally:
  531. sock.close()
  532. def generate_zpl_label_small(
  533. batch_no: str,
  534. item_code: str,
  535. item_name: str,
  536. item_id: Optional[int] = None,
  537. stock_in_line_id: Optional[int] = None,
  538. lot_no: Optional[str] = None,
  539. font: str = "MingLiUHKSCS",
  540. ) -> str:
  541. """
  542. ZPL for 標簽機. Row 1: item name. Row 2: QR left | item code + lot no (or batch) right.
  543. QR contains {"itemId": xxx, "stockInLineId": xxx} when both present; else batch_no.
  544. Unicode (^CI28); font set for Big-5 (e.g. MingLiUHKSCS).
  545. """
  546. desc = _zpl_escape((item_name or "—").strip())
  547. code = _zpl_escape((item_code or "—").strip())
  548. label_line2 = (lot_no or batch_no or "—").strip()
  549. label_line2_esc = _zpl_escape(label_line2)
  550. if item_id is not None and stock_in_line_id is not None:
  551. qr_data = _zpl_escape(json.dumps({"itemId": item_id, "stockInLineId": stock_in_line_id}))
  552. else:
  553. qr_data = f"QA,{batch_no}"
  554. return f"""^XA
  555. ^CI28
  556. ^PW500
  557. ^LL500
  558. ^FO10,15
  559. ^FB480,3,0,L,0
  560. ^A@N,38,38,{font}^FD{desc}^FS
  561. ^FO10,110
  562. ^BQN,2,6^FD{qr_data}^FS
  563. ^FO150,110
  564. ^A@N,48,48,{font}^FD{code}^FS
  565. ^FO150,175
  566. ^A@N,40,40,{font}^FD{label_line2_esc}^FS
  567. ^XZ"""
  568. # Label image size (pixels) for 標簽機 image printing.
  569. # Enlarged for readability (approx +90% scale).
  570. LABEL_IMAGE_W = 720
  571. LABEL_IMAGE_H = 530
  572. LABEL_PADDING = 23
  573. LABEL_FONT_NAME_SIZE = 42
  574. LABEL_FONT_CODE_SIZE = 49
  575. LABEL_FONT_BATCH_SIZE = 34
  576. LABEL_QR_SIZE = 210
  577. def _get_chinese_font(size: int) -> Optional["ImageFont.FreeTypeFont"]:
  578. """Return a Chinese-capable font for PIL, or None to use default."""
  579. if ImageFont is None:
  580. return None
  581. # Prefer real font files on Windows (font *names* may fail and silently fallback).
  582. if os.name == "nt":
  583. fonts_dir = os.path.join(os.environ.get("WINDIR", r"C:\Windows"), "Fonts")
  584. for rel in (
  585. "msjh.ttc", # Microsoft JhengHei
  586. "msjhl.ttc", # Microsoft JhengHei Light
  587. "msjhbd.ttc", # Microsoft JhengHei Bold
  588. "mingliu.ttc",
  589. "mingliub.ttc",
  590. "kaiu.ttf",
  591. "msyh.ttc", # Microsoft YaHei
  592. "msyhbd.ttc",
  593. "simhei.ttf",
  594. "simsun.ttc",
  595. ):
  596. p = os.path.join(fonts_dir, rel)
  597. try:
  598. if os.path.exists(p):
  599. return ImageFont.truetype(p, size)
  600. except (OSError, IOError):
  601. continue
  602. # Fallback: try common font names (may still work depending on Pillow build)
  603. for name in (
  604. "Microsoft JhengHei UI",
  605. "Microsoft JhengHei",
  606. "MingLiU",
  607. "MingLiU_HKSCS",
  608. "Microsoft YaHei",
  609. "SimHei",
  610. "SimSun",
  611. ):
  612. try:
  613. return ImageFont.truetype(name, size)
  614. except (OSError, IOError):
  615. continue
  616. try:
  617. return ImageFont.load_default()
  618. except Exception:
  619. return None
  620. def render_label_to_image(
  621. batch_no: str,
  622. item_code: str,
  623. item_name: str,
  624. item_id: Optional[int] = None,
  625. stock_in_line_id: Optional[int] = None,
  626. lot_no: Optional[str] = None,
  627. ) -> "Image.Image":
  628. """
  629. Render 標簽機 label as a PIL Image (white bg, black text + QR).
  630. Use this image for printing so Chinese displays correctly; words are drawn bigger.
  631. Requires Pillow and qrcode. Raises RuntimeError if not available.
  632. """
  633. if not _HAS_PIL_QR or Image is None or qrcode is None:
  634. raise RuntimeError("Pillow and qrcode are required for image labels. Run: pip install Pillow qrcode[pil]")
  635. img = Image.new("RGB", (LABEL_IMAGE_W, LABEL_IMAGE_H), "white")
  636. draw = ImageDraw.Draw(img)
  637. # QR payload (same as ZPL)
  638. if item_id is not None and stock_in_line_id is not None:
  639. qr_data = json.dumps({"itemId": item_id, "stockInLineId": stock_in_line_id})
  640. else:
  641. qr_data = f"QA,{batch_no}"
  642. # Draw QR top-left area
  643. qr = qrcode.QRCode(box_size=4, border=2)
  644. qr.add_data(qr_data)
  645. qr.make(fit=True)
  646. qr_img = qr.make_image(fill_color="black", back_color="white")
  647. _resample = getattr(Image, "Resampling", Image).NEAREST
  648. qr_img = qr_img.resize((LABEL_QR_SIZE, LABEL_QR_SIZE), _resample)
  649. img.paste(qr_img, (LABEL_PADDING, LABEL_PADDING))
  650. # Fonts (bigger for readability)
  651. font_name = _get_chinese_font(LABEL_FONT_NAME_SIZE)
  652. font_code = _get_chinese_font(LABEL_FONT_CODE_SIZE)
  653. font_batch = _get_chinese_font(LABEL_FONT_BATCH_SIZE)
  654. x_right = LABEL_PADDING + LABEL_QR_SIZE + LABEL_PADDING
  655. y_line = LABEL_PADDING
  656. # Line 1: item name (wrap within remaining width)
  657. name_str = (item_name or "—").strip()
  658. max_name_w = LABEL_IMAGE_W - x_right - LABEL_PADDING
  659. if font_name:
  660. # Wrap rule: after 7 "words" (excl. parentheses). ()() not counted; +=*/. and A–Z/a–z count as 0.5.
  661. def _wrap_text(text: str, font, max_width: int) -> list:
  662. ignore = set("()()")
  663. half = set("+=*/.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  664. max_count = 6.5
  665. lines: list[str] = []
  666. current: list[str] = []
  667. count = 0.0
  668. for ch in text:
  669. if ch == "\n":
  670. lines.append("".join(current).strip())
  671. current = []
  672. count = 0.0
  673. continue
  674. ch_count = 0.0 if ch in ignore else (0.5 if ch in half else 1.0)
  675. if count + ch_count > max_count and current:
  676. lines.append("".join(current).strip())
  677. current = []
  678. count = 0.0
  679. current.append(ch)
  680. count += ch_count
  681. if current:
  682. lines.append("".join(current).strip())
  683. # Max 2 rows for item name. If still long, keep everything in row 2.
  684. if len(lines) > 2:
  685. lines = [lines[0], "".join(lines[1:]).strip()]
  686. # Safety: if any line still exceeds pixel width, wrap by width as well.
  687. if hasattr(draw, "textbbox"):
  688. out: list[str] = []
  689. for ln in lines:
  690. buf: list[str] = []
  691. for ch in ln:
  692. buf.append(ch)
  693. bbox = draw.textbbox((0, 0), "".join(buf), font=font)
  694. if bbox[2] - bbox[0] > max_width and len(buf) > 1:
  695. out.append("".join(buf[:-1]).strip())
  696. buf = [buf[-1]]
  697. if buf:
  698. out.append("".join(buf).strip())
  699. out = [x for x in out if x]
  700. if len(out) > 2:
  701. out = [out[0], "".join(out[1:]).strip()]
  702. return out
  703. lines = [x for x in lines if x]
  704. if len(lines) > 2:
  705. lines = [lines[0], "".join(lines[1:]).strip()]
  706. return lines
  707. lines = _wrap_text(name_str, font_name, max_name_w)
  708. for i, ln in enumerate(lines):
  709. draw.text((x_right, y_line + i * (LABEL_FONT_NAME_SIZE + 4)), ln, font=font_name, fill="black")
  710. y_line += len(lines) * (LABEL_FONT_NAME_SIZE + 4) + 8
  711. else:
  712. draw.text((x_right, y_line), name_str[:30], fill="black")
  713. y_line += LABEL_FONT_NAME_SIZE + 12
  714. # Item code (bigger)
  715. code_str = (item_code or "—").strip()
  716. if font_code:
  717. draw.text((x_right, y_line), code_str, font=font_code, fill="black")
  718. else:
  719. draw.text((x_right, y_line), code_str, fill="black")
  720. y_line += LABEL_FONT_CODE_SIZE + 6
  721. # Batch/lot line
  722. batch_str = (lot_no or batch_no or "—").strip()
  723. if font_batch:
  724. draw.text((x_right, y_line), batch_str, font=font_batch, fill="black")
  725. else:
  726. draw.text((x_right, y_line), batch_str, fill="black")
  727. return img
  728. def _image_to_zpl_gfa(pil_image: "Image.Image") -> str:
  729. """
  730. Convert a PIL image into ZPL ^GFA (ASCII hex) so we can print Chinese reliably
  731. on ZPL printers (USB/Windows printer or COM) without relying on GDI drivers.
  732. """
  733. if Image is None or ImageOps is None:
  734. raise RuntimeError("Pillow is required for image-to-ZPL conversion.")
  735. # Convert to 1-bit monochrome bitmap. Invert so '1' bits represent black in ZPL.
  736. img_bw = ImageOps.invert(pil_image.convert("L")).convert("1")
  737. w, h = img_bw.size
  738. bytes_per_row = (w + 7) // 8
  739. raw = img_bw.tobytes()
  740. total = bytes_per_row * h
  741. # Ensure length matches expected (Pillow should already pack per row).
  742. if len(raw) != total:
  743. raw = raw[:total].ljust(total, b"\x00")
  744. hex_data = raw.hex().upper()
  745. return f"""^XA
  746. ^PW{w}
  747. ^LL{h}
  748. ^FO0,0
  749. ^GFA,{total},{total},{bytes_per_row},{hex_data}
  750. ^FS
  751. ^XZ"""
  752. def zpl_apply_print_quantity(zpl: str, copies: int) -> str:
  753. """
  754. Ask the printer to output `copies` identical labels from one ZPL format by inserting ^PQ after ^XA.
  755. Results in a **single** spool job / one write — no N separate Windows jobs, no chained ^XA blocks
  756. (which broke some TSC drivers with white-on-white ^GFA output).
  757. """
  758. if copies <= 1:
  759. return zpl
  760. first_fmt = zpl.split("^XZ", 1)[0] if "^XZ" in zpl else zpl
  761. if "^PQ" in first_fmt.upper():
  762. return zpl
  763. lines = zpl.splitlines()
  764. new_lines: list[str] = []
  765. inserted = False
  766. for line in lines:
  767. new_lines.append(line)
  768. if not inserted and line.strip() == "^XA":
  769. # ZPL II: q labels, 0 pause between, 1 replicate (non-serial), N = default options.
  770. # Same graphic (^GFA) is repeated q times — e.g. 需求數量 150 → 150 identical labels, one spool job.
  771. new_lines.append(f"^PQ{copies},0,1,N")
  772. inserted = True
  773. if not inserted:
  774. raise RuntimeError(
  775. "標籤 ZPL 無法插入 ^PQ(格式非預期)。請聯絡程式維護。"
  776. )
  777. ending = "\n" if zpl.endswith("\n") else ""
  778. return "\n".join(new_lines) + ending
  779. def send_image_to_label_printer(printer_name: str, pil_image: "Image.Image") -> None:
  780. """
  781. Send a PIL Image to 標簽機 via Windows GDI (so Chinese and graphics print correctly).
  782. Only supported when target is a Windows printer name (not COM port). Requires pywin32.
  783. """
  784. dest = (printer_name or "").strip()
  785. if not dest:
  786. raise ValueError("Label printer destination is empty.")
  787. if os.name != "nt" or dest.upper().startswith("COM"):
  788. raise RuntimeError("Image printing is only supported for a Windows printer name (e.g. TSC TTP-246M Pro).")
  789. if win32print is None or win32ui is None or win32con is None or win32gui is None:
  790. raise RuntimeError("pywin32 is required. Run: pip install pywin32")
  791. dc = win32ui.CreateDC()
  792. dc.CreatePrinterDC(dest)
  793. dc.StartDoc("FPSMS Label")
  794. dc.StartPage()
  795. try:
  796. bmp_w = pil_image.width
  797. bmp_h = pil_image.height
  798. # Scale-to-fit printable area (important for smaller physical labels).
  799. try:
  800. page_w = int(dc.GetDeviceCaps(win32con.HORZRES))
  801. page_h = int(dc.GetDeviceCaps(win32con.VERTRES))
  802. except Exception:
  803. page_w, page_h = bmp_w, bmp_h
  804. if page_w <= 0 or page_h <= 0:
  805. page_w, page_h = bmp_w, bmp_h
  806. scale = min(page_w / max(1, bmp_w), page_h / max(1, bmp_h))
  807. out_w = max(1, int(bmp_w * scale))
  808. out_h = max(1, int(bmp_h * scale))
  809. x0 = max(0, (page_w - out_w) // 2)
  810. y0 = max(0, (page_h - out_h) // 2)
  811. # Most reliable: render via Pillow ImageWin directly to printer DC.
  812. if ImageWin is not None:
  813. dib = ImageWin.Dib(pil_image.convert("RGB"))
  814. dib.draw(dc.GetHandleOutput(), (x0, y0, x0 + out_w, y0 + out_h))
  815. else:
  816. # Fallback: Draw image to printer DC via temp BMP (GDI uses BMP)
  817. with tempfile.NamedTemporaryFile(suffix=".bmp", delete=False) as f:
  818. tmp_bmp = f.name
  819. try:
  820. pil_image.save(tmp_bmp, "BMP")
  821. hbm = win32gui.LoadImage(
  822. 0, tmp_bmp, win32con.IMAGE_BITMAP, 0, 0,
  823. win32con.LR_LOADFROMFILE | win32con.LR_CREATEDIBSECTION,
  824. )
  825. if hbm == 0:
  826. raise RuntimeError("Failed to load label image as bitmap.")
  827. try:
  828. mem_dc = win32ui.CreateDCFromHandle(win32gui.CreateCompatibleDC(dc.GetSafeHdc()))
  829. bmp = getattr(win32ui, "CreateBitmapFromHandle", lambda h: win32ui.PyCBitmap.FromHandle(h))(hbm)
  830. mem_dc.SelectObject(bmp)
  831. dc.StretchBlt((x0, y0), (out_w, out_h), mem_dc, (0, 0), (bmp_w, bmp_h), win32con.SRCCOPY)
  832. finally:
  833. win32gui.DeleteObject(hbm)
  834. finally:
  835. try:
  836. os.unlink(tmp_bmp)
  837. except OSError:
  838. pass
  839. finally:
  840. dc.EndPage()
  841. dc.EndDoc()
  842. def send_zpl_to_dataflex(ip: str, port: int, zpl: str) -> None:
  843. """Send ZPL label (^XA…^XZ) to DataFlex printer via TCP. Raises on connection/send error."""
  844. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  845. try:
  846. sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
  847. except OSError:
  848. pass
  849. sock.settimeout(DATAFLEX_SEND_TIMEOUT)
  850. try:
  851. sock.connect((ip, port))
  852. sock.sendall(_dataflex_zpl_bytes(zpl))
  853. time.sleep(DATAFLEX_POST_LABEL_SETTLE_SEC)
  854. _dataflex_shutdown_write_maybe(sock)
  855. finally:
  856. sock.close()
  857. def query_dataflex_host_status(ip: str, port: int) -> str:
  858. """
  859. Query DataFlex/Zebra host status (~HS). Returns decoded status text, or empty string
  860. when device does not return host status.
  861. """
  862. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  863. try:
  864. sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
  865. except OSError:
  866. pass
  867. sock.settimeout(max(0.2, DATAFLEX_STATUS_QUERY_TIMEOUT_SEC))
  868. try:
  869. sock.connect((ip, port))
  870. sock.sendall(b"~HS\r\n")
  871. chunks: list[bytes] = []
  872. while True:
  873. try:
  874. data = sock.recv(4096)
  875. except socket.timeout:
  876. break
  877. except OSError as ex:
  878. if _dataflex_is_benign_tcp_reset(ex):
  879. break
  880. raise
  881. if not data:
  882. break
  883. chunks.append(data)
  884. if sum(len(c) for c in chunks) >= 16384:
  885. break
  886. return b"".join(chunks).decode("utf-8", errors="ignore")
  887. finally:
  888. sock.close()
  889. def _dataflex_status_has_e1005(status_text: str) -> bool:
  890. s = (status_text or "").lower()
  891. return "e1005" in s or "1005" in s
  892. def _dataflex_status_problem_code(status_text: str) -> Optional[str]:
  893. """If host status (~HS) suggests a fault, return a short code like E1000; else None."""
  894. s = (status_text or "").lower()
  895. for code in ("e1000", "e1005", "e1004", "e1003", "e1002", "e1001"):
  896. if code in s:
  897. return code.upper()
  898. return None
  899. def assert_dataflex_host_ok(ip: str, port: int) -> None:
  900. """
  901. Query ~HS once. If printer reports a known fault token, stop the job early.
  902. Empty/short replies are ignored (some firmware is quiet).
  903. """
  904. st = query_dataflex_host_status(ip, port)
  905. if not (st or "").strip():
  906. return
  907. prob = _dataflex_status_problem_code(st)
  908. if prob is not None:
  909. raise RuntimeError(
  910. f"打袋機狀態異常 {prob}(~HS)。請看機台畫面處理後再印。"
  911. )
  912. def recover_dataflex_if_host_fault(ip: str, port: int) -> None:
  913. """
  914. If ~HS reports E1000/E1005/etc., clear host state once and continue — do not abort the whole run.
  915. Keeps work short so the print thread does not look "frozen".
  916. """
  917. st = query_dataflex_host_status(ip, port)
  918. if not (st or "").strip():
  919. return
  920. if _dataflex_status_problem_code(st) is None:
  921. return
  922. touch_dataflex_recovery_grace(14.0)
  923. send_dataflex_job_counter_reset(ip, port, force=True)
  924. send_dataflex_preprint_reset(ip, port, force=True)
  925. time.sleep(max(0.35, DATAFLEX_RECOVERY_WAIT_SEC))
  926. def send_dataflex_label_with_recovery(ip: str, port: int, zpl: str) -> None:
  927. """
  928. Send one bag label with one automatic recovery attempt.
  929. If first send fails (including firmware-latched states such as E1005),
  930. perform full recovery (~JA/~RO/~JR), then light preprint reset (~JA/~RO),
  931. and retry once.
  932. """
  933. last_err: Optional[Exception] = None
  934. for attempt in range(DATAFLEX_RECOVERY_MAX_ATTEMPTS):
  935. try:
  936. if DATAFLEX_AUTO_RESET_ENABLED and DATAFLEX_PREPRINT_EACH_LABEL:
  937. send_dataflex_preprint_reset(ip, port)
  938. send_zpl_to_dataflex(ip, port, zpl)
  939. if DATAFLEX_VERIFY_STATUS_AFTER_SEND:
  940. status_text = query_dataflex_host_status(ip, port)
  941. if _dataflex_status_has_e1005(status_text):
  942. raise RuntimeError("DataFlex E1005 detected from host status.")
  943. return
  944. except (ConnectionRefusedError, socket.timeout, OSError, RuntimeError) as ex:
  945. last_err = ex
  946. if attempt >= DATAFLEX_RECOVERY_MAX_ATTEMPTS - 1:
  947. break
  948. if DATAFLEX_AUTO_RESET_ENABLED or DATAFLEX_RECOVER_ON_SEND_ERROR:
  949. touch_dataflex_recovery_grace(14.0)
  950. send_dataflex_job_counter_reset(ip, port, force=True)
  951. send_dataflex_preprint_reset(ip, port, force=True)
  952. time.sleep(max(0.35, DATAFLEX_RECOVERY_WAIT_SEC))
  953. if last_err is not None:
  954. raise last_err
  955. raise RuntimeError("DataFlex label send failed.")
  956. def send_zpl_to_label_printer(target: str, zpl: str) -> None:
  957. """
  958. Send ZPL to 標簽機.
  959. On Windows, if target is not a COM port (e.g. "TSC TTP-246M Pro"),
  960. send raw ZPL to the named Windows printer via the spooler.
  961. Otherwise, treat target as a serial COM port (original behaviour).
  962. """
  963. dest = (target or "").strip()
  964. if not dest:
  965. raise ValueError("Label printer destination is empty.")
  966. # Unicode (^CI28); send UTF-8 to 標簽機
  967. raw_bytes = zpl.encode("utf-8")
  968. # Windows printer name path (USB printer installed as normal printer)
  969. if os.name == "nt" and not dest.upper().startswith("COM"):
  970. if win32print is None:
  971. raise RuntimeError("pywin32 not installed. Run: pip install pywin32")
  972. handle = win32print.OpenPrinter(dest)
  973. try:
  974. job = win32print.StartDocPrinter(handle, 1, ("FPSMS Label", None, "RAW"))
  975. win32print.StartPagePrinter(handle)
  976. win32print.WritePrinter(handle, raw_bytes)
  977. win32print.EndPagePrinter(handle)
  978. win32print.EndDocPrinter(handle)
  979. finally:
  980. win32print.ClosePrinter(handle)
  981. return
  982. # Fallback: serial COM port
  983. if serial is None:
  984. raise RuntimeError("pyserial not installed. Run: pip install pyserial")
  985. ser = serial.Serial(dest, timeout=5)
  986. try:
  987. ser.write(raw_bytes)
  988. finally:
  989. ser.close()
  990. def send_zpl_to_label_printer_batch(target: str, zpl: str, copies: int) -> None:
  991. """
  992. Print multiple identical ZPL labels in **exactly one** Windows spool job (or one COM write).
  993. Uses ZPL ^PQ so the printer firmware repeats the format N times — never one job per label.
  994. """
  995. if copies < 1:
  996. return
  997. zpl_out = zpl_apply_print_quantity(zpl, copies)
  998. send_zpl_to_label_printer(target, zpl_out)
  999. def load_laser_last_count() -> tuple[int, Optional[str]]:
  1000. """Load last batch count and date from laser counter file. Returns (count, date_str)."""
  1001. if not os.path.exists(LASER_COUNTER_FILE):
  1002. return 0, None
  1003. try:
  1004. with open(LASER_COUNTER_FILE, "r", encoding="utf-8") as f:
  1005. lines = f.read().strip().splitlines()
  1006. if len(lines) >= 2:
  1007. return int(lines[1].strip()), lines[0].strip()
  1008. except Exception:
  1009. pass
  1010. return 0, None
  1011. def save_laser_last_count(date_str: str, count: int) -> None:
  1012. """Save laser batch count and date to file."""
  1013. try:
  1014. with open(LASER_COUNTER_FILE, "w", encoding="utf-8") as f:
  1015. f.write(f"{date_str}\n{count}")
  1016. except Exception:
  1017. pass
  1018. LASER_PUSH_INTERVAL = 2 # seconds between pushes (like sample script)
  1019. # Click row with 激光機 selected: send this many times, delay between sends (not after last).
  1020. LASER_ROW_SEND_COUNT = 3
  1021. LASER_ROW_SEND_DELAY_SEC = 3
  1022. def laser_push_loop(
  1023. ip: str,
  1024. port: int,
  1025. stop_event: threading.Event,
  1026. root: tk.Tk,
  1027. on_error: Callable[[str], None],
  1028. ) -> None:
  1029. """
  1030. Run in a background thread: persistent connection to EZCAD, push B{yymmdd}{count:03d};;
  1031. every LASER_PUSH_INTERVAL seconds. Resets count each new day. Uses counter file.
  1032. """
  1033. conn = None
  1034. push_count, last_saved_date = load_laser_last_count()
  1035. while not stop_event.is_set():
  1036. try:
  1037. if conn is None:
  1038. conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  1039. conn.settimeout(0.4)
  1040. conn.connect((ip, port))
  1041. now = datetime.now()
  1042. today_str = now.strftime("%y%m%d")
  1043. if last_saved_date != today_str:
  1044. push_count = 1
  1045. last_saved_date = today_str
  1046. batch = f"B{today_str}{push_count:03d}"
  1047. reply = f"{batch};;"
  1048. conn.sendall(reply.encode("utf-8"))
  1049. save_laser_last_count(today_str, push_count)
  1050. rlist, _, _ = select.select([conn], [], [], 0.4)
  1051. if rlist:
  1052. data = conn.recv(4096)
  1053. if not data:
  1054. conn.close()
  1055. conn = None
  1056. push_count += 1
  1057. for _ in range(int(LASER_PUSH_INTERVAL * 2)):
  1058. if stop_event.is_set():
  1059. break
  1060. time.sleep(0.5)
  1061. except socket.timeout:
  1062. pass
  1063. except Exception as e:
  1064. if conn:
  1065. try:
  1066. conn.close()
  1067. except Exception:
  1068. pass
  1069. conn = None
  1070. try:
  1071. root.after(0, lambda msg=str(e): on_error(msg))
  1072. except Exception:
  1073. pass
  1074. for _ in range(6):
  1075. if stop_event.is_set():
  1076. break
  1077. time.sleep(0.5)
  1078. if conn:
  1079. try:
  1080. conn.close()
  1081. except Exception:
  1082. pass
  1083. def send_job_to_laser(
  1084. conn_ref: list,
  1085. ip: str,
  1086. port: int,
  1087. item_id: Optional[int],
  1088. stock_in_line_id: Optional[int],
  1089. item_code: str,
  1090. item_name: str,
  1091. ) -> tuple[bool, str]:
  1092. """
  1093. Send to laser using `;` separated 3 params:
  1094. {"itemID": itemId, "stockInLineId": stockInLineId} ; itemCode ; itemName ;;
  1095. conn_ref: [socket or None] - reused across calls; closed only when switching printer.
  1096. When both item_id and stock_in_line_id present, sends JSON first param; else fallback: 0;item_code;item_name;;
  1097. Returns (success, message).
  1098. """
  1099. code_str = (item_code or "").strip().replace(";", ",")
  1100. name_str = (item_name or "").strip().replace(";", ",")
  1101. if item_id is not None and stock_in_line_id is not None:
  1102. # Use compact JSON so device-side parser doesn't get spaces.
  1103. json_part = json.dumps(
  1104. {"itemId": item_id, "stockInLineId": stock_in_line_id},
  1105. separators=(",", ":"),
  1106. )
  1107. reply = f"{json_part};{code_str};{name_str};;"
  1108. else:
  1109. reply = f"0;{code_str};{name_str};;"
  1110. conn = conn_ref[0]
  1111. try:
  1112. if conn is None:
  1113. conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  1114. conn.settimeout(3.0)
  1115. conn.connect((ip, port))
  1116. conn_ref[0] = conn
  1117. conn.settimeout(3.0)
  1118. conn.sendall(reply.encode("utf-8"))
  1119. conn.settimeout(0.5)
  1120. try:
  1121. data = conn.recv(4096)
  1122. if data:
  1123. ack = data.decode("utf-8", errors="ignore").strip().lower()
  1124. if "receive" in ack and "invalid" not in ack:
  1125. return True, f"已送出激光機:{reply}(已確認)"
  1126. except socket.timeout:
  1127. pass
  1128. return True, f"已送出激光機:{reply}"
  1129. except (ConnectionRefusedError, socket.timeout, OSError) as e:
  1130. if conn_ref[0] is not None:
  1131. try:
  1132. conn_ref[0].close()
  1133. except Exception:
  1134. pass
  1135. conn_ref[0] = None
  1136. if isinstance(e, ConnectionRefusedError):
  1137. return False, f"無法連線至 {ip}:{port},請確認激光機已開機且 IP 正確。"
  1138. if isinstance(e, socket.timeout):
  1139. return False, f"連線逾時 ({ip}:{port}),請檢查網路與連接埠。"
  1140. return False, f"激光機送出失敗:{e}"
  1141. def send_job_to_laser_with_retry(
  1142. conn_ref: list,
  1143. ip: str,
  1144. port: int,
  1145. item_id: Optional[int],
  1146. stock_in_line_id: Optional[int],
  1147. item_code: str,
  1148. item_name: str,
  1149. ) -> tuple[bool, str]:
  1150. """Send job to laser; on failure, retry once. Returns (success, message)."""
  1151. ok, msg = send_job_to_laser(conn_ref, ip, port, item_id, stock_in_line_id, item_code, item_name)
  1152. if ok:
  1153. return True, msg
  1154. ok2, msg2 = send_job_to_laser(conn_ref, ip, port, item_id, stock_in_line_id, item_code, item_name)
  1155. return ok2, msg2
  1156. def run_laser_row_send_thread(
  1157. root: tk.Tk,
  1158. laser_conn_ref: list,
  1159. laser_busy_ref: list,
  1160. ip: str,
  1161. port: int,
  1162. item_id: Optional[int],
  1163. stock_in_line_id: Optional[int],
  1164. item_code: str,
  1165. item_name: str,
  1166. set_status_message: Callable[[str, bool], None],
  1167. base_url: Optional[str] = None,
  1168. job_order_id: Optional[int] = None,
  1169. on_recorded: Optional[Callable[[], None]] = None,
  1170. ) -> None:
  1171. """
  1172. On row click with 激光機: send LASER_ROW_SEND_COUNT times with LASER_ROW_SEND_DELAY_SEC between sends.
  1173. UI updates on main thread; work runs in background so the window does not freeze.
  1174. After success, POST LASER qty to API when job_order_id and base_url are set.
  1175. """
  1176. if laser_busy_ref[0]:
  1177. messagebox.showwarning("激光機", "請等待目前激光發送完成。")
  1178. return
  1179. laser_busy_ref[0] = True
  1180. def worker() -> None:
  1181. try:
  1182. n = LASER_ROW_SEND_COUNT
  1183. for i in range(n):
  1184. ok, msg = send_job_to_laser_with_retry(
  1185. laser_conn_ref,
  1186. ip,
  1187. port,
  1188. item_id,
  1189. stock_in_line_id,
  1190. item_code,
  1191. item_name,
  1192. )
  1193. if not ok:
  1194. root.after(
  1195. 0,
  1196. lambda m=msg: messagebox.showwarning("激光機", m),
  1197. )
  1198. return
  1199. if i < n - 1:
  1200. time.sleep(LASER_ROW_SEND_DELAY_SEC)
  1201. posted = False
  1202. if base_url and job_order_id is not None:
  1203. try:
  1204. submit_job_order_print_submit(base_url, int(job_order_id), n, "LASER")
  1205. posted = True
  1206. except requests.RequestException as ex:
  1207. root.after(
  1208. 0,
  1209. lambda err=str(ex): messagebox.showwarning(
  1210. "激光機",
  1211. f"已發送,但伺服器記錄失敗:{err}",
  1212. ),
  1213. )
  1214. elif base_url:
  1215. root.after(
  1216. 0,
  1217. lambda: messagebox.showwarning(
  1218. "激光機",
  1219. "已發送,但無工單 id,無法寫入伺服器記錄。",
  1220. ),
  1221. )
  1222. root.after(
  1223. 0,
  1224. lambda: set_status_message("已發送", is_error=False),
  1225. )
  1226. if on_recorded is not None and posted:
  1227. root.after(0, on_recorded)
  1228. except Exception as e:
  1229. root.after(
  1230. 0,
  1231. lambda err=str(e): messagebox.showwarning("激光機", f"送出失敗:{err}"),
  1232. )
  1233. finally:
  1234. laser_busy_ref[0] = False
  1235. threading.Thread(target=worker, daemon=True).start()
  1236. def run_dataflex_fixed_qty_thread(
  1237. root: tk.Tk,
  1238. dataflex_lock: threading.Lock,
  1239. dataflex_busy_ref: list,
  1240. ip: str,
  1241. port: int,
  1242. n: int,
  1243. zpl: str,
  1244. label_text: str,
  1245. jo_id: Optional[int],
  1246. base_url: str,
  1247. set_status_message: Callable[[str, bool], None],
  1248. on_recorded: Callable[[], None],
  1249. ) -> None:
  1250. """
  1251. Send n DataFlex labels with delay between copies. Runs off the Tk main thread so the UI
  1252. stays responsive (printer dropdown, other controls) during printing.
  1253. """
  1254. def worker() -> None:
  1255. with dataflex_lock:
  1256. if dataflex_busy_ref[0]:
  1257. root.after(
  1258. 0,
  1259. lambda: messagebox.showwarning(
  1260. "打袋機",
  1261. "請等待目前列印完成或先停止連續列印。",
  1262. ),
  1263. )
  1264. return
  1265. dataflex_busy_ref[0] = True
  1266. printed = 0
  1267. used_single_tcp = False
  1268. try:
  1269. send_dataflex_start_job_reset(ip, port, force=True)
  1270. if DATAFLEX_SINGLE_TCP_JOB and n >= 1:
  1271. # One TCP connection, one ZPL, ^PQn — printer firmware prints n identical bags.
  1272. used_single_tcp = True
  1273. zpl_one = dataflex_zpl_set_print_quantity(zpl, n)
  1274. root.after(
  1275. 0,
  1276. lambda tn=n: set_status_message(
  1277. f"打袋單次發送中… {tn} 張(^PQ{tn})",
  1278. is_error=False,
  1279. ),
  1280. )
  1281. send_dataflex_label_with_recovery(ip, port, zpl_one)
  1282. if DATAFLEX_VERIFY_EVERY_LABELS > 0:
  1283. recover_dataflex_if_host_fault(ip, port)
  1284. printed = n
  1285. else:
  1286. # One TCP job per bag. Slower but avoids E1005 on some units when ^PQ is large.
  1287. for i in range(n):
  1288. send_dataflex_label_with_recovery(ip, port, zpl)
  1289. printed += 1
  1290. if DATAFLEX_UI_PROGRESS_EVERY > 0 and (
  1291. printed == 1 or printed % DATAFLEX_UI_PROGRESS_EVERY == 0
  1292. ):
  1293. p, t = printed, n
  1294. root.after(
  1295. 0,
  1296. lambda p=p, t=t: set_status_message(
  1297. f"打袋列印中… {p}/{t}",
  1298. is_error=False,
  1299. ),
  1300. )
  1301. if (
  1302. DATAFLEX_VERIFY_EVERY_LABELS > 0
  1303. and printed % DATAFLEX_VERIFY_EVERY_LABELS == 0
  1304. ):
  1305. recover_dataflex_if_host_fault(ip, port)
  1306. if (
  1307. DATAFLEX_COOLDOWN_EVERY_LABELS > 0
  1308. and printed % DATAFLEX_COOLDOWN_EVERY_LABELS == 0
  1309. and i < n - 1
  1310. ):
  1311. time.sleep(max(0.0, DATAFLEX_COOLDOWN_SEC))
  1312. if (
  1313. DATAFLEX_THERMAL_REST_EVERY_LABELS > 0
  1314. and printed % DATAFLEX_THERMAL_REST_EVERY_LABELS == 0
  1315. and i < n - 1
  1316. ):
  1317. time.sleep(max(0.0, DATAFLEX_THERMAL_REST_SEC))
  1318. if i < n - 1:
  1319. time.sleep(DATAFLEX_INTER_LABEL_DELAY_SEC)
  1320. root.after(
  1321. 0,
  1322. lambda u=used_single_tcp: set_status_message(
  1323. (
  1324. f"已送出列印(單次 TCP):批次 {label_text} x {n} 張"
  1325. if u
  1326. else f"已送出列印:批次 {label_text} x {n} 張"
  1327. ),
  1328. is_error=False,
  1329. ),
  1330. )
  1331. if jo_id is not None:
  1332. try:
  1333. submit_job_order_print_submit(base_url, int(jo_id), n, "DATAFLEX")
  1334. root.after(0, on_recorded)
  1335. except requests.RequestException as ex:
  1336. root.after(
  1337. 0,
  1338. lambda err=str(ex): messagebox.showwarning(
  1339. "打袋機",
  1340. f"列印可能已完成,但伺服器記錄失敗(可再試):{err}",
  1341. ),
  1342. )
  1343. else:
  1344. root.after(
  1345. 0,
  1346. lambda: messagebox.showwarning(
  1347. "打袋機",
  1348. f"已送出列印 {n} 張,但無工單 id,無法寫入伺服器記錄。",
  1349. ),
  1350. )
  1351. except ConnectionRefusedError:
  1352. root.after(
  1353. 0,
  1354. lambda: set_status_message(
  1355. f"無法連線至 {ip}:{port},已送出 {printed}/{n} 張。",
  1356. is_error=True,
  1357. ),
  1358. )
  1359. except socket.timeout:
  1360. root.after(
  1361. 0,
  1362. lambda: set_status_message(
  1363. f"連線逾時 ({ip}:{port}),已送出 {printed}/{n} 張。",
  1364. is_error=True,
  1365. ),
  1366. )
  1367. except OSError as err:
  1368. root.after(
  1369. 0,
  1370. lambda e=err: set_status_message(
  1371. f"列印失敗:{e}(已送出 {printed}/{n} 張)",
  1372. is_error=True,
  1373. ),
  1374. )
  1375. except RuntimeError as err:
  1376. root.after(
  1377. 0,
  1378. lambda e=err: set_status_message(
  1379. f"打袋機錯誤:{e}(已送出 {printed}/{n} 張)",
  1380. is_error=True,
  1381. ),
  1382. )
  1383. except Exception as err:
  1384. root.after(
  1385. 0,
  1386. lambda e=err: set_status_message(
  1387. f"打袋機例外:{e}(已送出 {printed}/{n} 張)",
  1388. is_error=True,
  1389. ),
  1390. )
  1391. finally:
  1392. with dataflex_lock:
  1393. dataflex_busy_ref[0] = False
  1394. threading.Thread(target=worker, daemon=True).start()
  1395. def run_label_print_batch_thread(
  1396. root: tk.Tk,
  1397. label_lock: threading.Lock,
  1398. label_busy_ref: list,
  1399. com: str,
  1400. zpl_img: str,
  1401. n: int,
  1402. jo_id: Optional[int],
  1403. base_url: str,
  1404. set_status_message: Callable[[str, bool], None],
  1405. on_recorded: Callable[[], None],
  1406. ) -> None:
  1407. """
  1408. Send n label copies off the main thread so DataFlex / laser / UI stay usable in parallel.
  1409. """
  1410. def worker() -> None:
  1411. with label_lock:
  1412. if label_busy_ref[0]:
  1413. root.after(
  1414. 0,
  1415. lambda: messagebox.showwarning(
  1416. "標籤機",
  1417. "請等待目前標籤列印完成。",
  1418. ),
  1419. )
  1420. return
  1421. label_busy_ref[0] = True
  1422. try:
  1423. send_zpl_to_label_printer_batch(com, zpl_img, n)
  1424. root.after(
  1425. 0,
  1426. lambda: set_status_message(f"已送出列印:標籤 x {n} 張", is_error=False),
  1427. )
  1428. if jo_id is not None:
  1429. try:
  1430. submit_job_order_print_submit(base_url, int(jo_id), n, "LABEL")
  1431. root.after(0, on_recorded)
  1432. root.after(
  1433. 0,
  1434. lambda: messagebox.showinfo(
  1435. "標籤機",
  1436. f"已送出列印:{n} 張標籤(已記錄)",
  1437. ),
  1438. )
  1439. except requests.RequestException as ex:
  1440. root.after(
  1441. 0,
  1442. lambda err=str(ex): messagebox.showwarning(
  1443. "標籤機",
  1444. f"標籤已列印 {n} 張,但伺服器記錄失敗:{err}",
  1445. ),
  1446. )
  1447. else:
  1448. root.after(
  1449. 0,
  1450. lambda: messagebox.showwarning(
  1451. "標籤機",
  1452. f"已送出列印:{n} 張標籤(無工單 id,無法寫入伺服器記錄)",
  1453. ),
  1454. )
  1455. except Exception as err:
  1456. root.after(
  1457. 0,
  1458. lambda e=str(err): messagebox.showerror("標籤機", f"列印失敗:{e}"),
  1459. )
  1460. finally:
  1461. with label_lock:
  1462. label_busy_ref[0] = False
  1463. threading.Thread(target=worker, daemon=True).start()
  1464. def _printed_qty_int(raw) -> int:
  1465. """Parse API printed qty field (may be float JSON) to int."""
  1466. try:
  1467. return int(float(raw)) if raw is not None else 0
  1468. except (TypeError, ValueError):
  1469. return 0
  1470. def _filter_job_orders_by_search(data: list, needle: str) -> list:
  1471. """Substring match on item code, job order code, item name, lot (case-insensitive)."""
  1472. n = needle.strip().lower()
  1473. if not n:
  1474. return data
  1475. out: list = []
  1476. for jo in data:
  1477. parts = [
  1478. str(jo.get("itemCode") or ""),
  1479. str(jo.get("code") or ""),
  1480. str(jo.get("itemName") or ""),
  1481. str(jo.get("lotNo") or ""),
  1482. ]
  1483. if any(n in p.lower() for p in parts):
  1484. out.append(jo)
  1485. return out
  1486. def format_qty(val) -> str:
  1487. """Format quantity: integer without .0, with thousand separator."""
  1488. if val is None:
  1489. return "—"
  1490. try:
  1491. n = float(val)
  1492. if n == int(n):
  1493. return f"{int(n):,}"
  1494. return f"{n:,.2f}".rstrip("0").rstrip(".")
  1495. except (TypeError, ValueError):
  1496. return str(val)
  1497. def batch_no(year: int, job_order_id: int) -> str:
  1498. """Batch no.: B + 4-digit year + jobOrderId zero-padded to 6 digits."""
  1499. return f"B{year}{job_order_id:06d}"
  1500. def get_font(size: int = FONT_SIZE, bold: bool = False) -> tuple:
  1501. try:
  1502. return (FONT_FAMILY, size, "bold" if bold else "normal")
  1503. except Exception:
  1504. return ("TkDefaultFont", size, "bold" if bold else "normal")
  1505. def fetch_job_orders(base_url: str, plan_start: date) -> list:
  1506. """Call GET /py/job-orders and return the JSON list."""
  1507. url = f"{base_url.rstrip('/')}/py/job-orders"
  1508. params = {"planStart": plan_start.isoformat()}
  1509. resp = requests.get(url, params=params, timeout=30)
  1510. resp.raise_for_status()
  1511. return resp.json()
  1512. def submit_job_order_print_submit(
  1513. base_url: str,
  1514. job_order_id: int,
  1515. qty: int,
  1516. print_channel: str = "LABEL",
  1517. ) -> None:
  1518. """
  1519. Record printed quantity in the FPSMS database via PyController.
  1520. POST ``/api/py/job-order-print-submit`` (path under base_url) — **public endpoint, no login**
  1521. or API key required. Each successful call appends one row to ``py_job_order_print_submit``;
  1522. totals per job order and channel are aggregated server-side.
  1523. Raises ``requests.RequestException`` if all retry attempts fail.
  1524. """
  1525. url = f"{base_url.rstrip('/')}/py/job-order-print-submit"
  1526. payload = {
  1527. "jobOrderId": int(job_order_id),
  1528. "qty": int(qty),
  1529. "printChannel": print_channel,
  1530. }
  1531. last_err: Optional[Exception] = None
  1532. for attempt in range(PRINT_SUBMIT_MAX_ATTEMPTS):
  1533. try:
  1534. resp = requests.post(url, json=payload, timeout=30)
  1535. resp.raise_for_status()
  1536. return
  1537. except requests.RequestException as ex:
  1538. last_err = ex
  1539. if attempt < PRINT_SUBMIT_MAX_ATTEMPTS - 1:
  1540. time.sleep(PRINT_SUBMIT_RETRY_DELAY_SEC)
  1541. if last_err is not None:
  1542. raise last_err
  1543. raise RuntimeError("submit_job_order_print_submit: unexpected empty error")
  1544. def set_row_highlight(row_frame: tk.Frame, selected: bool) -> None:
  1545. """Set row and all nested Frame/Label children to selected or normal background."""
  1546. bg = BG_ROW_SELECTED if selected else BG_ROW
  1547. def _paint(w: tk.Misc) -> None:
  1548. if isinstance(w, (tk.Frame, tk.Label)):
  1549. w.configure(bg=bg)
  1550. for c in w.winfo_children():
  1551. _paint(c)
  1552. _paint(row_frame)
  1553. def on_job_order_click(jo: dict, batch: str) -> None:
  1554. """Show message and highlight row (keeps printing to selected printer)."""
  1555. item_code = jo.get("itemCode") or "—"
  1556. item_name = jo.get("itemName") or "—"
  1557. messagebox.showinfo(
  1558. "工單",
  1559. f'已點選:批次 {batch}\n品號 {item_code} {item_name}',
  1560. )
  1561. def ask_label_count(parent: tk.Tk) -> Optional[int]:
  1562. """
  1563. When printer is 標簽機, ask how many labels to print:
  1564. optional direct qty in text field (e.g. 150), +50/+10/+5/+1, 重置, then 確認送出.
  1565. That count becomes ZPL ^PQ in one job — 150 → 150 identical labels.
  1566. Returns count (>= 1), or None if cancelled.
  1567. """
  1568. result: list[Optional[int]] = [None]
  1569. qty_var = tk.StringVar(value="0")
  1570. win = tk.Toplevel(parent)
  1571. win.title("標簽印數")
  1572. win.geometry("580x280")
  1573. win.transient(parent)
  1574. win.grab_set()
  1575. win.configure(bg=BG_TOP)
  1576. ttk.Label(win, text="印多少個?", font=get_font(FONT_SIZE)).pack(pady=(12, 4))
  1577. entry_row = tk.Frame(win, bg=BG_TOP)
  1578. entry_row.pack(pady=8)
  1579. tk.Label(entry_row, text="需求數量:", font=get_font(FONT_SIZE), bg=BG_TOP).pack(side=tk.LEFT, padx=(0, 6))
  1580. qty_entry = tk.Entry(
  1581. entry_row,
  1582. textvariable=qty_var,
  1583. width=12,
  1584. font=get_font(FONT_SIZE),
  1585. bg="white",
  1586. justify=tk.RIGHT,
  1587. )
  1588. qty_entry.pack(side=tk.LEFT, padx=4)
  1589. def current_qty() -> int:
  1590. s = (qty_var.get() or "").strip().replace(",", "")
  1591. if not s:
  1592. return 0
  1593. try:
  1594. return max(0, int(s))
  1595. except ValueError:
  1596. return 0
  1597. def reset_qty() -> None:
  1598. qty_var.set("0")
  1599. ttk.Button(entry_row, text="重置", command=reset_qty, width=8).pack(side=tk.LEFT, padx=8)
  1600. def add(n: int) -> None:
  1601. qty_var.set(str(current_qty() + n))
  1602. def confirm() -> None:
  1603. q = current_qty()
  1604. if q < 1:
  1605. messagebox.showwarning("標簽機", "請輸入需求數量或按 +50、+10、+5、+1。", parent=win)
  1606. return
  1607. result[0] = q
  1608. win.destroy()
  1609. btn_row1 = tk.Frame(win, bg=BG_TOP)
  1610. btn_row1.pack(pady=8)
  1611. for label, value in [("+50", 50), ("+10", 10), ("+5", 5), ("+1", 1)]:
  1612. def make_add(v: int):
  1613. return lambda: add(v)
  1614. ttk.Button(btn_row1, text=label, command=make_add(value), width=8).pack(side=tk.LEFT, padx=4)
  1615. ttk.Button(win, text="確認送出", command=confirm, width=14).pack(pady=12)
  1616. qty_entry.bind("<Return>", lambda e: confirm())
  1617. win.protocol("WM_DELETE_WINDOW", win.destroy)
  1618. win.wait_window()
  1619. return result[0]
  1620. def ask_bag_count(parent: tk.Tk) -> Optional[Tuple[int, bool]]:
  1621. """
  1622. When printer is 打袋機 DataFlex: qty with +按鈕 then 確認送出, or big bottom「C」for continuous.
  1623. Returns (count, continuous_print). If continuous_print is True, count is ignored (use 0).
  1624. None if cancelled.
  1625. """
  1626. result: list[Optional[Tuple[int, bool]]] = [None]
  1627. qty_var = tk.StringVar(value="0")
  1628. win = tk.Toplevel(parent)
  1629. win.title("打袋列印數量")
  1630. win.geometry("580x420")
  1631. win.transient(parent)
  1632. win.grab_set()
  1633. win.configure(bg=BG_TOP)
  1634. ttk.Label(win, text="列印多少個袋?", font=get_font(FONT_SIZE)).pack(pady=(12, 4))
  1635. entry_row = tk.Frame(win, bg=BG_TOP)
  1636. entry_row.pack(pady=8)
  1637. tk.Label(entry_row, text="需求數量:", font=get_font(FONT_SIZE), bg=BG_TOP).pack(side=tk.LEFT, padx=(0, 6))
  1638. qty_entry = tk.Entry(
  1639. entry_row,
  1640. textvariable=qty_var,
  1641. width=12,
  1642. font=get_font(FONT_SIZE),
  1643. bg="white",
  1644. justify=tk.RIGHT,
  1645. )
  1646. qty_entry.pack(side=tk.LEFT, padx=4)
  1647. def current_qty() -> int:
  1648. s = (qty_var.get() or "").strip().replace(",", "")
  1649. if not s:
  1650. return 0
  1651. try:
  1652. return max(0, int(s))
  1653. except ValueError:
  1654. return 0
  1655. def reset_qty() -> None:
  1656. qty_var.set("0")
  1657. ttk.Button(entry_row, text="重置", command=reset_qty, width=8).pack(side=tk.LEFT, padx=8)
  1658. def add(n: int) -> None:
  1659. qty_var.set(str(current_qty() + n))
  1660. def confirm() -> None:
  1661. q = current_qty()
  1662. if q < 1:
  1663. messagebox.showwarning("打袋機", "請輸入需求數量或按 +50、+10、+5、+1。", parent=win)
  1664. return
  1665. result[0] = (q, False)
  1666. win.destroy()
  1667. def start_continuous() -> None:
  1668. """Big C: continuous print until 停止; counter reset at job start."""
  1669. result[0] = (0, True)
  1670. win.destroy()
  1671. btn_row1 = tk.Frame(win, bg=BG_TOP)
  1672. btn_row1.pack(pady=8)
  1673. for label, value in [("+50", 50), ("+10", 10), ("+5", 5), ("+1", 1)]:
  1674. def make_add(v: int):
  1675. return lambda: add(v)
  1676. ttk.Button(btn_row1, text=label, command=make_add(value), width=8).pack(side=tk.LEFT, padx=4)
  1677. ttk.Button(win, text="確認送出", command=confirm, width=14).pack(pady=12)
  1678. qty_entry.bind("<Return>", lambda e: confirm())
  1679. sep = ttk.Separator(win, orient=tk.HORIZONTAL)
  1680. sep.pack(fill=tk.X, padx=16, pady=(4, 8))
  1681. bottom = tk.Frame(win, bg=BG_TOP)
  1682. bottom.pack(fill=tk.X, padx=12, pady=(0, 12))
  1683. tk.Label(
  1684. bottom,
  1685. text="連續出袋 · 每單開始重設計數 · 另開視窗按「停止列印」結束",
  1686. font=get_font(FONT_SIZE_META),
  1687. bg=BG_TOP,
  1688. fg="#333333",
  1689. wraplength=540,
  1690. justify=tk.CENTER,
  1691. ).pack(fill=tk.X, pady=(0, 6))
  1692. tk.Button(
  1693. bottom,
  1694. text="C(連續印)",
  1695. command=start_continuous,
  1696. font=(FONT_FAMILY, 38, "bold"),
  1697. bg="#2E7D32",
  1698. fg="white",
  1699. activebackground="#1B5E20",
  1700. activeforeground="white",
  1701. relief=tk.RAISED,
  1702. bd=4,
  1703. cursor="hand2",
  1704. padx=24,
  1705. pady=18,
  1706. ).pack(fill=tk.X)
  1707. win.protocol("WM_DELETE_WINDOW", win.destroy)
  1708. win.wait_window()
  1709. return result[0]
  1710. @dataclass(frozen=True)
  1711. class DataflexPrintSession:
  1712. """
  1713. Snapshot taken when the user starts DataFlex print (especially C 連續印).
  1714. The worker must use only this object — not grid row index, scroll position, or selection.
  1715. """
  1716. job_order_id: Optional[int]
  1717. job_code: str
  1718. item_code: str
  1719. item_name: str
  1720. label_text: str
  1721. zpl: str
  1722. printer_ip: str
  1723. printer_port: int
  1724. batch_display: str
  1725. def build_dataflex_print_session(
  1726. jo: dict,
  1727. batch: str,
  1728. zpl: str,
  1729. label_text: str,
  1730. printer_ip: str,
  1731. printer_port: int,
  1732. ) -> DataflexPrintSession:
  1733. jo_id = jo.get("id")
  1734. jo_code = (jo.get("code") or "").strip()
  1735. if not jo_code and jo_id is not None:
  1736. jo_code = f"#{jo_id}"
  1737. elif not jo_code:
  1738. jo_code = "—"
  1739. return DataflexPrintSession(
  1740. job_order_id=int(jo_id) if jo_id is not None else None,
  1741. job_code=jo_code,
  1742. item_code=(jo.get("itemCode") or "—").strip(),
  1743. item_name=(jo.get("itemName") or "—").strip(),
  1744. label_text=label_text,
  1745. zpl=zpl,
  1746. printer_ip=printer_ip,
  1747. printer_port=printer_port,
  1748. batch_display=(batch or "—").strip(),
  1749. )
  1750. def run_dataflex_continuous_thread(
  1751. root: tk.Tk,
  1752. session: DataflexPrintSession,
  1753. stop_event: threading.Event,
  1754. stop_win: tk.Toplevel,
  1755. dataflex_lock: threading.Lock,
  1756. dataflex_busy_ref: list,
  1757. dataflex_stop_win_ref: list,
  1758. active_session_ref: list,
  1759. base_url: str,
  1760. set_status_message: Callable[[str, bool], None],
  1761. on_recorded: Callable[[], None],
  1762. ) -> None:
  1763. """Send bags in a loop until stop_event; all payload comes from session (in-memory snapshot)."""
  1764. def worker() -> None:
  1765. with dataflex_lock:
  1766. if dataflex_busy_ref[0]:
  1767. active_session_ref[0] = None
  1768. def _abort_start() -> None:
  1769. messagebox.showwarning(
  1770. "打袋機",
  1771. "請等待目前列印完成或先停止連續列印。",
  1772. )
  1773. dataflex_stop_win_ref[0] = None
  1774. try:
  1775. stop_win.destroy()
  1776. except tk.TclError:
  1777. pass
  1778. root.after(0, _abort_start)
  1779. return
  1780. dataflex_busy_ref[0] = True
  1781. ip = session.printer_ip
  1782. port = session.printer_port
  1783. zpl = session.zpl
  1784. label_text = session.label_text
  1785. printed = 0
  1786. error_shown = False
  1787. try:
  1788. send_dataflex_start_job_reset(ip, port, force=True)
  1789. while not stop_event.is_set():
  1790. send_dataflex_label_with_recovery(ip, port, zpl)
  1791. printed += 1
  1792. if DATAFLEX_UI_PROGRESS_EVERY > 0 and (
  1793. printed == 1 or printed % DATAFLEX_UI_PROGRESS_EVERY == 0
  1794. ):
  1795. p = printed
  1796. root.after(
  1797. 0,
  1798. lambda p=p, jc=session.job_code: set_status_message(
  1799. f"連續打袋 · 工單 {jc}… 已印 {p} 張",
  1800. is_error=False,
  1801. ),
  1802. )
  1803. if (
  1804. DATAFLEX_VERIFY_EVERY_LABELS > 0
  1805. and printed % DATAFLEX_VERIFY_EVERY_LABELS == 0
  1806. ):
  1807. recover_dataflex_if_host_fault(ip, port)
  1808. if (
  1809. DATAFLEX_COOLDOWN_EVERY_LABELS > 0
  1810. and printed % DATAFLEX_COOLDOWN_EVERY_LABELS == 0
  1811. ):
  1812. _sleep_interruptible(stop_event, max(0.0, DATAFLEX_COOLDOWN_SEC))
  1813. if (
  1814. DATAFLEX_THERMAL_REST_EVERY_LABELS > 0
  1815. and printed % DATAFLEX_THERMAL_REST_EVERY_LABELS == 0
  1816. ):
  1817. _sleep_interruptible(stop_event, max(0.0, DATAFLEX_THERMAL_REST_SEC))
  1818. _sleep_interruptible(stop_event, DATAFLEX_INTER_LABEL_DELAY_SEC)
  1819. except ConnectionRefusedError:
  1820. error_shown = True
  1821. root.after(
  1822. 0,
  1823. lambda: set_status_message(
  1824. f"無法連線至 {ip}:{port},請確認印表機已開機且 IP 正確。",
  1825. is_error=True,
  1826. ),
  1827. )
  1828. except socket.timeout:
  1829. error_shown = True
  1830. root.after(
  1831. 0,
  1832. lambda: set_status_message(
  1833. f"連線逾時 ({ip}:{port}),請檢查網路與連接埠。",
  1834. is_error=True,
  1835. ),
  1836. )
  1837. except OSError as err:
  1838. error_shown = True
  1839. root.after(
  1840. 0,
  1841. lambda e=err: set_status_message(f"列印失敗:{e}", is_error=True),
  1842. )
  1843. except RuntimeError as err:
  1844. error_shown = True
  1845. root.after(
  1846. 0,
  1847. lambda e=err: set_status_message(f"打袋機錯誤:{e}", is_error=True),
  1848. )
  1849. except Exception as err:
  1850. error_shown = True
  1851. root.after(
  1852. 0,
  1853. lambda e=err: set_status_message(f"打袋機例外:{e}", is_error=True),
  1854. )
  1855. finally:
  1856. with dataflex_lock:
  1857. dataflex_busy_ref[0] = False
  1858. active_session_ref[0] = None
  1859. def _done() -> None:
  1860. dataflex_stop_win_ref[0] = None
  1861. try:
  1862. if os.name == "nt":
  1863. stop_win.attributes("-topmost", False)
  1864. except tk.TclError:
  1865. pass
  1866. try:
  1867. stop_win.destroy()
  1868. except tk.TclError:
  1869. pass
  1870. jc = session.job_code
  1871. if printed > 0:
  1872. set_status_message(
  1873. f"連續列印結束:工單 {jc} · {label_text},已印 {printed} 張",
  1874. is_error=False,
  1875. )
  1876. if session.job_order_id is not None:
  1877. try:
  1878. submit_job_order_print_submit(
  1879. base_url,
  1880. session.job_order_id,
  1881. printed,
  1882. "DATAFLEX",
  1883. )
  1884. on_recorded()
  1885. except requests.RequestException as ex:
  1886. messagebox.showwarning(
  1887. "打袋機",
  1888. f"列印可能已完成,但伺服器記錄失敗(可再試):{ex}",
  1889. )
  1890. elif not error_shown:
  1891. set_status_message("連續列印未印出或已取消", is_error=True)
  1892. root.after(0, _done)
  1893. threading.Thread(target=worker, daemon=True).start()
  1894. def _sleep_interruptible(stop_event: threading.Event, total_sec: float) -> None:
  1895. """Sleep up to total_sec but return early if stop_event is set."""
  1896. end = time.perf_counter() + total_sec
  1897. while time.perf_counter() < end:
  1898. if stop_event.is_set():
  1899. return
  1900. remaining = end - time.perf_counter()
  1901. if remaining <= 0:
  1902. break
  1903. time.sleep(min(0.05, remaining))
  1904. def open_dataflex_stop_window(
  1905. parent: tk.Tk,
  1906. stop_event: threading.Event,
  1907. stop_win_ref: list,
  1908. session: DataflexPrintSession,
  1909. ) -> tk.Toplevel:
  1910. """
  1911. Small window with 停止列印 for DataFlex continuous mode (non-modal so stop stays usable).
  1912. Stays above other dialogs (e.g. 標籤機 quantity) via periodic lift + optional topmost on Windows,
  1913. so switching printer and printing labels does not hide the stop control. Ref is cleared on destroy.
  1914. Job details come from the in-memory session snapshot, not the grid selection.
  1915. """
  1916. win = tk.Toplevel(parent)
  1917. win.title("打袋機連續列印")
  1918. win.geometry("480x240")
  1919. # On Windows, transient(root) can hide this Toplevel when the menubutton / printer row
  1920. # updates (e.g. switching to 激光機); keep transient only on non-Windows.
  1921. if os.name != "nt":
  1922. win.transient(parent)
  1923. win.configure(bg=BG_TOP)
  1924. stop_win_ref[0] = win
  1925. if os.name == "nt":
  1926. try:
  1927. win.attributes("-topmost", True)
  1928. except tk.TclError:
  1929. pass
  1930. tk.Label(
  1931. win,
  1932. text="連續列印進行中(內容以按下 C 時的工單為準,與列表捲動/日期無關)",
  1933. font=get_font(FONT_SIZE_META),
  1934. bg=BG_TOP,
  1935. wraplength=440,
  1936. justify=tk.CENTER,
  1937. ).pack(pady=(12, 6))
  1938. detail = (
  1939. f"工單:{session.job_code}\n"
  1940. f"品號:{session.item_code}\n"
  1941. f"品名:{session.item_name}\n"
  1942. f"批次/批號:{session.label_text}"
  1943. )
  1944. tk.Label(
  1945. win,
  1946. text=detail,
  1947. font=get_font(FONT_SIZE),
  1948. bg=BG_TOP,
  1949. fg="#111111",
  1950. wraplength=440,
  1951. justify=tk.LEFT,
  1952. anchor=tk.W,
  1953. ).pack(padx=16, pady=(0, 8), fill=tk.X)
  1954. def clear_topmost() -> None:
  1955. if os.name == "nt":
  1956. try:
  1957. win.attributes("-topmost", False)
  1958. except tk.TclError:
  1959. pass
  1960. def stop() -> None:
  1961. stop_event.set()
  1962. stop_win_ref[0] = None
  1963. clear_topmost()
  1964. try:
  1965. win.destroy()
  1966. except tk.TclError:
  1967. pass
  1968. def periodic_lift() -> None:
  1969. if stop_win_ref[0] is not win:
  1970. return
  1971. try:
  1972. if not win.winfo_exists():
  1973. return
  1974. win.lift()
  1975. if os.name == "nt":
  1976. win.attributes("-topmost", True)
  1977. except tk.TclError:
  1978. return
  1979. parent.after(4000, periodic_lift)
  1980. tk.Button(
  1981. win,
  1982. text="停止列印",
  1983. command=stop,
  1984. font=get_font(FONT_SIZE_BUTTONS),
  1985. bg=BG_STATUS_ERROR,
  1986. fg=FG_STATUS_ERROR,
  1987. padx=20,
  1988. pady=10,
  1989. ).pack(pady=12)
  1990. win.protocol("WM_DELETE_WINDOW", stop)
  1991. parent.after(500, periodic_lift)
  1992. return win
  1993. def main() -> None:
  1994. settings = load_settings()
  1995. base_url_ref = [build_base_url(settings["api_ip"], settings["api_port"])]
  1996. root = tk.Tk()
  1997. root.title(f"FP-MTMS Bag3 v{APP_VERSION} 打袋機")
  1998. root.geometry("1120x960")
  1999. root.minsize(480, 360)
  2000. root.configure(bg=BG_ROOT)
  2001. # Style: larger font for aged users; light blue theme
  2002. style = ttk.Style()
  2003. try:
  2004. style.configure(".", font=get_font(FONT_SIZE), background=BG_TOP)
  2005. style.configure("TButton", font=get_font(FONT_SIZE_BUTTONS), background=BG_TOP)
  2006. style.configure("TLabel", font=get_font(FONT_SIZE), background=BG_TOP)
  2007. style.configure("TEntry", font=get_font(FONT_SIZE))
  2008. style.configure("TFrame", background=BG_TOP)
  2009. # TCombobox field (if other combos use ttk later)
  2010. style.configure("TCombobox", font=get_font(FONT_SIZE_COMBO))
  2011. except tk.TclError:
  2012. pass
  2013. # Status bar at top: connection state (no popup on error)
  2014. status_frame = tk.Frame(root, bg=BG_STATUS_ERROR, padx=12, pady=6)
  2015. status_frame.pack(fill=tk.X)
  2016. status_lbl = tk.Label(
  2017. status_frame,
  2018. text="連接不到服務器",
  2019. font=get_font(FONT_SIZE_BUTTONS),
  2020. bg=BG_STATUS_ERROR,
  2021. fg=FG_STATUS_ERROR,
  2022. anchor=tk.CENTER,
  2023. )
  2024. status_lbl.pack(fill=tk.X)
  2025. def set_status_ok():
  2026. status_frame.configure(bg=BG_STATUS_OK)
  2027. status_lbl.configure(text="連接正常", bg=BG_STATUS_OK, fg=FG_STATUS_OK)
  2028. def set_status_error():
  2029. status_frame.configure(bg=BG_STATUS_ERROR)
  2030. status_lbl.configure(text="連接不到服務器", bg=BG_STATUS_ERROR, fg=FG_STATUS_ERROR)
  2031. def set_status_message(msg: str, is_error: bool = False) -> None:
  2032. """Show a message on the status bar."""
  2033. if is_error:
  2034. status_frame.configure(bg=BG_STATUS_ERROR)
  2035. status_lbl.configure(text=msg, bg=BG_STATUS_ERROR, fg=FG_STATUS_ERROR)
  2036. else:
  2037. status_frame.configure(bg=BG_STATUS_OK)
  2038. status_lbl.configure(text=msg, bg=BG_STATUS_OK, fg=FG_STATUS_OK)
  2039. # Laser: keep connection open for repeated sends; close when switching away
  2040. laser_conn_ref: list = [None]
  2041. laser_send_busy_ref: list = [False]
  2042. # DataFlex: shared lock so fixed-qty and continuous jobs do not overlap (independent of laser/label)
  2043. dataflex_lock = threading.Lock()
  2044. dataflex_busy_ref: list = [False]
  2045. # Suppress transient DataFlex "disconnected" UI while we intentionally reset/print.
  2046. dataflex_status_grace_until_ref: list[float] = [0.0]
  2047. # 標籤機: own lock so label jobs do not overlap; does not block DataFlex or laser
  2048. label_lock = threading.Lock()
  2049. label_busy_ref: list = [False]
  2050. # DataFlex continuous: stop Toplevel ref so we can lift it after other dialogs
  2051. dataflex_stop_win_ref: list = [None]
  2052. # In-memory job snapshot for C 連續印 (not tied to grid row position after start)
  2053. active_dataflex_session_ref: list[Optional[DataflexPrintSession]] = [None]
  2054. def lift_dataflex_stop_if_running() -> None:
  2055. """After closing another dialog (e.g. 標籤印數), bring the stop panel forward again."""
  2056. w = dataflex_stop_win_ref[0]
  2057. if w is None:
  2058. return
  2059. try:
  2060. if w.winfo_exists():
  2061. w.lift()
  2062. if os.name == "nt":
  2063. w.attributes("-topmost", True)
  2064. except tk.TclError:
  2065. pass
  2066. def hold_dataflex_status_ok(seconds: float) -> None:
  2067. until = time.time() + max(0.0, seconds)
  2068. if until > dataflex_status_grace_until_ref[0]:
  2069. dataflex_status_grace_until_ref[0] = until
  2070. # Top: left [前一天] [date] [後一天] | right [printer dropdown]
  2071. top = tk.Frame(root, padx=12, pady=12, bg=BG_TOP)
  2072. top.pack(fill=tk.X)
  2073. date_var = tk.StringVar(value=date.today().isoformat())
  2074. printer_options = ["打袋機 DataFlex", "標簽機", "激光機"]
  2075. printer_var = tk.StringVar(value=printer_options[0])
  2076. def go_prev_day() -> None:
  2077. try:
  2078. d = date.fromisoformat(date_var.get().strip())
  2079. date_var.set((d - timedelta(days=1)).isoformat())
  2080. load_job_orders(from_user_date_change=True)
  2081. except ValueError:
  2082. date_var.set(date.today().isoformat())
  2083. load_job_orders(from_user_date_change=True)
  2084. def go_next_day() -> None:
  2085. try:
  2086. d = date.fromisoformat(date_var.get().strip())
  2087. date_var.set((d + timedelta(days=1)).isoformat())
  2088. load_job_orders(from_user_date_change=True)
  2089. except ValueError:
  2090. date_var.set(date.today().isoformat())
  2091. load_job_orders(from_user_date_change=True)
  2092. # 前一天 (previous day) with left arrow icon
  2093. btn_prev = ttk.Button(top, text="◀ 前一天", command=go_prev_day)
  2094. btn_prev.pack(side=tk.LEFT, padx=(0, 8))
  2095. # Date field (no "日期:" label); shorter width
  2096. date_entry = tk.Entry(
  2097. top,
  2098. textvariable=date_var,
  2099. font=get_font(FONT_SIZE),
  2100. width=10,
  2101. bg="white",
  2102. )
  2103. date_entry.pack(side=tk.LEFT, padx=(0, 8), ipady=4)
  2104. # 後一天 (next day) with right arrow icon
  2105. btn_next = ttk.Button(top, text="後一天 ▶", command=go_next_day)
  2106. btn_next.pack(side=tk.LEFT, padx=(0, 8))
  2107. # Top right: Setup button + printer selection
  2108. right_frame = tk.Frame(top, bg=BG_TOP)
  2109. right_frame.pack(side=tk.RIGHT)
  2110. ttk.Button(right_frame, text="設定", command=lambda: open_setup_window(root, settings, base_url_ref)).pack(
  2111. side=tk.LEFT, padx=(0, 12)
  2112. )
  2113. def on_dataflex_host_reset() -> None:
  2114. """Send ~JA/~RO/~JR to clear E1005 latch without turning the printer off."""
  2115. ip = (settings.get("dabag_ip") or "").strip()
  2116. port_str = (settings.get("dabag_port") or "3008").strip()
  2117. if not ip:
  2118. messagebox.showwarning("打袋機", "請先在「設定」填寫打袋機 IP。")
  2119. return
  2120. try:
  2121. port = int(port_str)
  2122. except ValueError:
  2123. port = 3008
  2124. hold_dataflex_status_ok(12.0)
  2125. def worker() -> None:
  2126. try:
  2127. send_dataflex_job_counter_reset(ip, port, force=True)
  2128. root.after(
  2129. 0,
  2130. lambda: messagebox.showinfo(
  2131. "打袋機",
  2132. "已送出主機重設(緩衝清除/計數/軟重設)。\n"
  2133. "若畫面仍顯示 E1005,請再按一次或關機重開。",
  2134. ),
  2135. )
  2136. except OSError as ex:
  2137. root.after(
  2138. 0,
  2139. lambda e=str(ex): messagebox.showerror(
  2140. "打袋機",
  2141. f"連線失敗,無法重設:{e}",
  2142. ),
  2143. )
  2144. threading.Thread(target=worker, daemon=True).start()
  2145. ttk.Button(right_frame, text="打袋重設", command=on_dataflex_host_reset).pack(
  2146. side=tk.LEFT, padx=(0, 8)
  2147. )
  2148. # 列印機 label: green when printer connected, red when not (checked periodically)
  2149. printer_status_lbl = tk.Label(
  2150. right_frame,
  2151. text="列印機:",
  2152. font=get_font(FONT_SIZE),
  2153. bg=BG_STATUS_ERROR,
  2154. fg="black",
  2155. padx=6,
  2156. pady=2,
  2157. )
  2158. printer_status_lbl.pack(side=tk.LEFT, padx=(0, 4))
  2159. # tk.OptionMenu (not ttk.Combobox): on Windows the ttk dropdown uses OS font and stays tiny;
  2160. # OptionMenu's menu supports font= for the open list.
  2161. printer_combo = tk.OptionMenu(right_frame, printer_var, *printer_options)
  2162. _combo_font = get_font(FONT_SIZE_COMBO)
  2163. printer_combo.configure(
  2164. font=_combo_font,
  2165. bg=BG_TOP,
  2166. fg="black",
  2167. activebackground=BG_TOP,
  2168. activeforeground="black",
  2169. width=14,
  2170. anchor="w",
  2171. highlightthickness=0,
  2172. bd=1,
  2173. relief=tk.GROOVE,
  2174. )
  2175. printer_combo["menu"].configure(font=_combo_font, tearoff=0)
  2176. printer_combo.pack(side=tk.LEFT)
  2177. printer_after_ref = [None]
  2178. def set_printer_status_ok():
  2179. printer_status_lbl.configure(bg=BG_STATUS_OK, fg=FG_STATUS_OK)
  2180. def set_printer_status_error():
  2181. printer_status_lbl.configure(bg=BG_STATUS_ERROR, fg=FG_STATUS_ERROR)
  2182. def check_printer() -> None:
  2183. if printer_after_ref[0] is not None:
  2184. root.after_cancel(printer_after_ref[0])
  2185. printer_after_ref[0] = None
  2186. if printer_var.get() == "打袋機 DataFlex":
  2187. if (
  2188. dataflex_busy_ref[0]
  2189. or time.time() < dataflex_status_grace_until_ref[0]
  2190. or time.time() < _DATAFLEX_RECOVERY_GRACE_UNTIL
  2191. ):
  2192. set_printer_status_ok()
  2193. printer_after_ref[0] = root.after(5000, check_printer)
  2194. return
  2195. ok = try_printer_connection(printer_var.get(), settings)
  2196. if ok:
  2197. set_printer_status_ok()
  2198. printer_after_ref[0] = root.after(PRINTER_CHECK_MS, check_printer)
  2199. else:
  2200. set_printer_status_error()
  2201. printer_after_ref[0] = root.after(PRINTER_RETRY_MS, check_printer)
  2202. def on_printer_selection_changed(*args) -> None:
  2203. check_printer()
  2204. if printer_var.get() != "激光機":
  2205. if laser_conn_ref[0] is not None:
  2206. try:
  2207. laser_conn_ref[0].close()
  2208. except Exception:
  2209. pass
  2210. laser_conn_ref[0] = None
  2211. # DataFlex continuous stop panel can drop behind after OptionMenu closes; re-lift for any choice.
  2212. root.after(100, lift_dataflex_stop_if_running)
  2213. printer_var.trace_add("write", on_printer_selection_changed)
  2214. def open_setup_window(parent_win: tk.Tk, sett: dict, base_url_ref_list: list) -> None:
  2215. """Modal setup: API IP/port, 打袋機/激光機 IP+port, 標簽機 COM port."""
  2216. d = tk.Toplevel(parent_win)
  2217. d.title("設定")
  2218. d.geometry("440x520")
  2219. d.transient(parent_win)
  2220. d.grab_set()
  2221. d.configure(bg=BG_TOP)
  2222. f = tk.Frame(d, padx=16, pady=16, bg=BG_TOP)
  2223. f.pack(fill=tk.BOTH, expand=True)
  2224. grid_row = [0] # use list so inner function can update
  2225. def _ensure_dot_in_entry(entry: tk.Entry) -> None:
  2226. """Allow typing dot (.) in Entry when IME or layout blocks it (e.g. 192.168.17.27)."""
  2227. def on_key(event):
  2228. if event.keysym in ("period", "decimal"):
  2229. pos = entry.index(tk.INSERT)
  2230. entry.insert(tk.INSERT, ".")
  2231. return "break"
  2232. entry.bind("<KeyPress>", on_key)
  2233. def add_section(label_text: str, key_ip: str | None, key_port: str | None, key_single: str | None):
  2234. out = []
  2235. ttk.Label(f, text=label_text, font=get_font(FONT_SIZE_BUTTONS)).grid(
  2236. row=grid_row[0], column=0, columnspan=2, sticky=tk.W, pady=(8, 2)
  2237. )
  2238. grid_row[0] += 1
  2239. if key_single:
  2240. ttk.Label(
  2241. f,
  2242. text="列印機名稱 (Windows):",
  2243. ).grid(
  2244. row=grid_row[0],
  2245. column=0,
  2246. sticky=tk.W,
  2247. pady=2,
  2248. )
  2249. var = tk.StringVar(value=sett.get(key_single, ""))
  2250. e = tk.Entry(f, textvariable=var, width=22, font=get_font(FONT_SIZE), bg="white")
  2251. e.grid(row=grid_row[0], column=1, sticky=tk.W, pady=2)
  2252. _ensure_dot_in_entry(e)
  2253. grid_row[0] += 1
  2254. return [(key_single, var)]
  2255. if key_ip:
  2256. ttk.Label(f, text="IP:").grid(row=grid_row[0], column=0, sticky=tk.W, pady=2)
  2257. var_ip = tk.StringVar(value=sett.get(key_ip, ""))
  2258. e_ip = tk.Entry(f, textvariable=var_ip, width=22, font=get_font(FONT_SIZE), bg="white")
  2259. e_ip.grid(row=grid_row[0], column=1, sticky=tk.W, pady=2)
  2260. _ensure_dot_in_entry(e_ip)
  2261. grid_row[0] += 1
  2262. out.append((key_ip, var_ip))
  2263. if key_port:
  2264. ttk.Label(f, text="Port:").grid(row=grid_row[0], column=0, sticky=tk.W, pady=2)
  2265. var_port = tk.StringVar(value=sett.get(key_port, ""))
  2266. e_port = tk.Entry(f, textvariable=var_port, width=12, font=get_font(FONT_SIZE), bg="white")
  2267. e_port.grid(row=grid_row[0], column=1, sticky=tk.W, pady=2)
  2268. _ensure_dot_in_entry(e_port)
  2269. grid_row[0] += 1
  2270. out.append((key_port, var_port))
  2271. return out
  2272. all_vars = []
  2273. all_vars.extend(add_section("API 伺服器", "api_ip", "api_port", None))
  2274. all_vars.extend(add_section("打袋機 DataFlex", "dabag_ip", "dabag_port", None))
  2275. all_vars.extend(add_section("激光機", "laser_ip", "laser_port", None))
  2276. all_vars.extend(add_section("標簽機 (USB)", None, None, "label_com"))
  2277. def on_save():
  2278. for key, var in all_vars:
  2279. sett[key] = var.get().strip()
  2280. save_settings(sett)
  2281. base_url_ref_list[0] = build_base_url(sett["api_ip"], sett["api_port"])
  2282. d.destroy()
  2283. btn_f = tk.Frame(d, bg=BG_TOP)
  2284. btn_f.pack(pady=12)
  2285. ttk.Button(btn_f, text="儲存", command=on_save).pack(side=tk.LEFT, padx=4)
  2286. ttk.Button(btn_f, text="取消", command=d.destroy).pack(side=tk.LEFT, padx=4)
  2287. d.wait_window()
  2288. job_orders_frame = tk.Frame(root, bg=BG_LIST)
  2289. job_orders_frame.pack(fill=tk.BOTH, expand=True, padx=12, pady=12)
  2290. search_var = tk.StringVar()
  2291. search_frame = tk.Frame(job_orders_frame, bg=BG_LIST)
  2292. search_frame.pack(fill=tk.X, pady=(0, 6))
  2293. tk.Label(
  2294. search_frame,
  2295. text="搜尋品號/工單/批號:",
  2296. font=get_font(FONT_SIZE_QTY),
  2297. bg=BG_LIST,
  2298. fg="black",
  2299. ).pack(side=tk.LEFT, padx=(0, 6))
  2300. search_entry = tk.Entry(
  2301. search_frame,
  2302. textvariable=search_var,
  2303. width=32,
  2304. font=get_font(FONT_SIZE_QTY),
  2305. bg="white",
  2306. )
  2307. search_entry.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 8))
  2308. # Scrollable area for buttons
  2309. canvas = tk.Canvas(job_orders_frame, highlightthickness=0, bg=BG_LIST)
  2310. scrollbar = ttk.Scrollbar(job_orders_frame, orient=tk.VERTICAL, command=canvas.yview)
  2311. inner = tk.Frame(canvas, bg=BG_LIST)
  2312. win_id = canvas.create_window((0, 0), window=inner, anchor=tk.NW)
  2313. canvas.configure(yscrollcommand=scrollbar.set)
  2314. def _on_inner_configure(event):
  2315. canvas.configure(scrollregion=canvas.bbox("all"))
  2316. def _on_canvas_configure(event):
  2317. canvas.itemconfig(win_id, width=event.width)
  2318. inner.bind("<Configure>", _on_inner_configure)
  2319. canvas.bind("<Configure>", _on_canvas_configure)
  2320. # Mouse wheel: default Tk scroll speed (one unit per notch)
  2321. def _on_mousewheel(event):
  2322. if getattr(event, "delta", None) is not None:
  2323. canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
  2324. elif event.num == 5:
  2325. canvas.yview_scroll(1, "units")
  2326. elif event.num == 4:
  2327. canvas.yview_scroll(-1, "units")
  2328. canvas.bind("<MouseWheel>", _on_mousewheel)
  2329. inner.bind("<MouseWheel>", _on_mousewheel)
  2330. canvas.bind("<Button-4>", _on_mousewheel)
  2331. canvas.bind("<Button-5>", _on_mousewheel)
  2332. inner.bind("<Button-4>", _on_mousewheel)
  2333. inner.bind("<Button-5>", _on_mousewheel)
  2334. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  2335. canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  2336. # Track which row is highlighted (selected for printing) and which job id
  2337. selected_row_holder = [None] # [tk.Frame | None]
  2338. selected_jo_id_ref = [None] # [int | None] job order id for selection preservation
  2339. last_data_ref = [None] # [list | None] last successful fetch for current date
  2340. last_plan_start_ref = [date.today()] # plan date for the current list (search filter uses same)
  2341. after_id_ref = [None] # [str | None] root.after id to cancel retry/refresh
  2342. def _data_equal(a: Optional[list], b: Optional[list]) -> bool:
  2343. if a is None or b is None:
  2344. return a is b
  2345. if len(a) != len(b):
  2346. return False
  2347. for x, y in zip(a, b):
  2348. if x.get("id") != y.get("id"):
  2349. return False
  2350. for k in ("bagPrintedQty", "labelPrintedQty", "laserPrintedQty"):
  2351. if x.get(k) != y.get(k):
  2352. return False
  2353. return True
  2354. def _build_list_from_data(data: list, plan_start: date, preserve_selection: bool) -> None:
  2355. selected_row_holder[0] = None
  2356. year = plan_start.year
  2357. selected_id = selected_jo_id_ref[0] if preserve_selection else None
  2358. found_row = None
  2359. for jo in data:
  2360. jo_id = jo.get("id")
  2361. raw_batch = batch_no(year, jo_id) if jo_id is not None else "—"
  2362. lot_no_val = jo.get("lotNo")
  2363. batch = (lot_no_val or "—").strip() if lot_no_val else "—"
  2364. jo_no_display = (jo.get("code") or "").strip()
  2365. if not jo_no_display and jo_id is not None:
  2366. jo_no_display = raw_batch
  2367. elif not jo_no_display:
  2368. jo_no_display = "—"
  2369. # Line 1: job order no.; line 2: 需求 + 已印(袋/標/激)on one row for compact scrolling
  2370. head_line = f"工單:{jo_no_display}"
  2371. item_code = jo.get("itemCode") or "—"
  2372. item_name = jo.get("itemName") or "—"
  2373. req_qty = jo.get("reqQty")
  2374. qty_str = format_qty(req_qty)
  2375. bag_pq = _printed_qty_int(jo.get("bagPrintedQty"))
  2376. label_pq = _printed_qty_int(jo.get("labelPrintedQty"))
  2377. laser_pq = _printed_qty_int(jo.get("laserPrintedQty"))
  2378. meta_line = (
  2379. f"需求:{qty_str} "
  2380. f"已印 袋{bag_pq:,} 標{label_pq:,} 激{laser_pq:,}"
  2381. )
  2382. # Columns: fixed-width left | fixed-width 品號 | 品名 (expand)
  2383. row = tk.Frame(
  2384. inner,
  2385. bg=BG_ROW,
  2386. relief=tk.RAISED,
  2387. bd=2,
  2388. cursor="hand2",
  2389. padx=10,
  2390. pady=LIST_ROW_IPADY,
  2391. )
  2392. row.pack(fill=tk.X, pady=LIST_ROW_PADY)
  2393. left = tk.Frame(row, bg=BG_ROW, width=LEFT_COL_WIDTH_PX)
  2394. left.pack_propagate(False)
  2395. left.pack(side=tk.LEFT, anchor=tk.NW, fill=tk.Y)
  2396. batch_lbl = tk.Label(
  2397. left,
  2398. text=head_line,
  2399. font=get_font(FONT_SIZE_BUTTONS),
  2400. bg=BG_ROW,
  2401. fg="black",
  2402. )
  2403. batch_lbl.pack(anchor=tk.W)
  2404. meta_lbl = tk.Label(
  2405. left,
  2406. text=meta_line,
  2407. font=get_font(FONT_SIZE_META),
  2408. bg=BG_ROW,
  2409. fg="#222222",
  2410. anchor=tk.W,
  2411. justify=tk.LEFT,
  2412. wraplength=LEFT_COL_WIDTH_PX - 8,
  2413. )
  2414. meta_lbl.pack(anchor=tk.W)
  2415. code_col = tk.Frame(row, bg=BG_ROW, width=CODE_COL_WIDTH_PX)
  2416. code_col.pack_propagate(False)
  2417. code_col.pack(side=tk.LEFT, anchor=tk.NW, fill=tk.Y, padx=(6, 2))
  2418. code_lbl = tk.Label(
  2419. code_col,
  2420. text=item_code,
  2421. font=get_font(FONT_SIZE_ITEM_CODE),
  2422. bg=BG_ROW,
  2423. fg="black",
  2424. wraplength=ITEM_CODE_WRAP,
  2425. justify=tk.LEFT,
  2426. anchor=tk.NW,
  2427. )
  2428. code_lbl.pack(anchor=tk.NW)
  2429. name_col = tk.Frame(row, bg=BG_ROW)
  2430. name_col.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, anchor=tk.NW)
  2431. name_lbl = tk.Label(
  2432. name_col,
  2433. text=item_name or "—",
  2434. font=get_font(FONT_SIZE_ITEM_NAME),
  2435. bg=BG_ROW,
  2436. fg="black",
  2437. wraplength=ITEM_NAME_WRAP,
  2438. justify=tk.LEFT,
  2439. anchor=tk.NW,
  2440. )
  2441. name_lbl.pack(anchor=tk.NW)
  2442. def _on_click(e, j=jo, b=batch, r=row):
  2443. if (
  2444. printer_var.get() == "打袋機 DataFlex"
  2445. and dataflex_busy_ref[0]
  2446. and active_dataflex_session_ref[0] is not None
  2447. ):
  2448. s = active_dataflex_session_ref[0]
  2449. messagebox.showwarning(
  2450. "打袋機",
  2451. f"連續列印進行中,請先按「停止列印」。\n\n"
  2452. f"工單:{s.job_code}\n"
  2453. f"品號:{s.item_code}\n"
  2454. f"品名:{s.item_name}",
  2455. )
  2456. return
  2457. if selected_row_holder[0] is not None:
  2458. set_row_highlight(selected_row_holder[0], False)
  2459. set_row_highlight(r, True)
  2460. selected_row_holder[0] = r
  2461. selected_jo_id_ref[0] = j.get("id")
  2462. if printer_var.get() == "打袋機 DataFlex":
  2463. ip = (settings.get("dabag_ip") or "").strip()
  2464. port_str = (settings.get("dabag_port") or "3008").strip()
  2465. try:
  2466. port = int(port_str)
  2467. except ValueError:
  2468. port = 3008
  2469. if not ip:
  2470. messagebox.showerror("打袋機", "請在設定中填寫打袋機 DataFlex 的 IP。")
  2471. else:
  2472. hold_dataflex_status_ok(12.0)
  2473. def _after_row_select_reset() -> None:
  2474. bag_ans = ask_bag_count(root)
  2475. if bag_ans is not None:
  2476. n, continuous = bag_ans
  2477. hold_dataflex_status_ok(12.0)
  2478. item_code = j.get("itemCode") or "—"
  2479. item_name = j.get("itemName") or "—"
  2480. item_id = j.get("itemId")
  2481. stock_in_line_id = j.get("stockInLineId")
  2482. lot_no = j.get("lotNo")
  2483. zpl = generate_zpl_dataflex(
  2484. b,
  2485. item_code,
  2486. item_name,
  2487. item_id=item_id,
  2488. stock_in_line_id=stock_in_line_id,
  2489. lot_no=lot_no,
  2490. job_order_id=j.get("id"),
  2491. )
  2492. label_text = (lot_no or b).strip()
  2493. if continuous:
  2494. if dataflex_busy_ref[0]:
  2495. messagebox.showwarning(
  2496. "打袋機",
  2497. "請等待目前列印完成或先停止連續列印。",
  2498. )
  2499. return
  2500. session = build_dataflex_print_session(
  2501. j,
  2502. b,
  2503. zpl,
  2504. label_text,
  2505. ip,
  2506. port,
  2507. )
  2508. active_dataflex_session_ref[0] = session
  2509. stop_ev = threading.Event()
  2510. stop_win = open_dataflex_stop_window(
  2511. root,
  2512. stop_ev,
  2513. dataflex_stop_win_ref,
  2514. session,
  2515. )
  2516. run_dataflex_continuous_thread(
  2517. root=root,
  2518. session=session,
  2519. stop_event=stop_ev,
  2520. stop_win=stop_win,
  2521. dataflex_lock=dataflex_lock,
  2522. dataflex_busy_ref=dataflex_busy_ref,
  2523. dataflex_stop_win_ref=dataflex_stop_win_ref,
  2524. active_session_ref=active_dataflex_session_ref,
  2525. base_url=base_url_ref[0],
  2526. set_status_message=set_status_message,
  2527. on_recorded=lambda: load_job_orders(
  2528. from_user_date_change=False
  2529. ),
  2530. )
  2531. else:
  2532. run_dataflex_fixed_qty_thread(
  2533. root=root,
  2534. dataflex_lock=dataflex_lock,
  2535. dataflex_busy_ref=dataflex_busy_ref,
  2536. ip=ip,
  2537. port=port,
  2538. n=n,
  2539. zpl=zpl,
  2540. label_text=label_text,
  2541. jo_id=j.get("id"),
  2542. base_url=base_url_ref[0],
  2543. set_status_message=set_status_message,
  2544. on_recorded=lambda: load_job_orders(
  2545. from_user_date_change=False
  2546. ),
  2547. )
  2548. def _row_select_reset_worker() -> None:
  2549. try:
  2550. send_dataflex_start_job_reset(ip, port, force=True)
  2551. except OSError as ex:
  2552. root.after(
  2553. 0,
  2554. lambda e=str(ex): messagebox.showwarning(
  2555. "打袋機",
  2556. f"點選工單時重設批次計數失敗(仍可比對數量):{e}",
  2557. ),
  2558. )
  2559. root.after(0, _after_row_select_reset)
  2560. threading.Thread(target=_row_select_reset_worker, daemon=True).start()
  2561. elif printer_var.get() == "標簽機":
  2562. com = (settings.get("label_com") or "").strip()
  2563. if not com:
  2564. messagebox.showerror("標簽機", "請在設定中填寫標簽機名稱 (例如:TSC TTP-246M Pro)。")
  2565. else:
  2566. count = ask_label_count(root)
  2567. lift_dataflex_stop_if_running()
  2568. if count is not None:
  2569. item_code = j.get("itemCode") or "—"
  2570. item_name = j.get("itemName") or "—"
  2571. item_id = j.get("itemId")
  2572. stock_in_line_id = j.get("stockInLineId")
  2573. lot_no = j.get("lotNo")
  2574. n = count
  2575. try:
  2576. # Always render to image (Chinese OK), then send as ZPL graphic (^GFA).
  2577. # This is more reliable than Windows GDI and works for both Windows printer name and COM.
  2578. if not _HAS_PIL_QR:
  2579. raise RuntimeError("請先安裝 Pillow + qrcode(pip install Pillow qrcode[pil])。")
  2580. label_img = render_label_to_image(
  2581. b, item_code, item_name,
  2582. item_id=item_id, stock_in_line_id=stock_in_line_id,
  2583. lot_no=lot_no,
  2584. )
  2585. zpl_img = _image_to_zpl_gfa(label_img)
  2586. run_label_print_batch_thread(
  2587. root=root,
  2588. label_lock=label_lock,
  2589. label_busy_ref=label_busy_ref,
  2590. com=com,
  2591. zpl_img=zpl_img,
  2592. n=n,
  2593. jo_id=j.get("id"),
  2594. base_url=base_url_ref[0],
  2595. set_status_message=set_status_message,
  2596. on_recorded=lambda: load_job_orders(
  2597. from_user_date_change=False
  2598. ),
  2599. )
  2600. except Exception as err:
  2601. messagebox.showerror("標簽機", f"列印失敗:{err}")
  2602. elif printer_var.get() == "激光機":
  2603. ip = (settings.get("laser_ip") or "").strip()
  2604. port_str = (settings.get("laser_port") or "45678").strip()
  2605. try:
  2606. port = int(port_str)
  2607. except ValueError:
  2608. port = 45678
  2609. if not ip:
  2610. set_status_message("請在設定中填寫激光機的 IP。", is_error=True)
  2611. else:
  2612. item_id = j.get("itemId")
  2613. stock_in_line_id = j.get("stockInLineId")
  2614. item_code_val = j.get("itemCode") or ""
  2615. item_name_val = j.get("itemName") or ""
  2616. run_laser_row_send_thread(
  2617. root=root,
  2618. laser_conn_ref=laser_conn_ref,
  2619. laser_busy_ref=laser_send_busy_ref,
  2620. ip=ip,
  2621. port=port,
  2622. item_id=item_id,
  2623. stock_in_line_id=stock_in_line_id,
  2624. item_code=item_code_val,
  2625. item_name=item_name_val,
  2626. set_status_message=set_status_message,
  2627. base_url=base_url_ref[0],
  2628. job_order_id=j.get("id"),
  2629. on_recorded=lambda: load_job_orders(from_user_date_change=False),
  2630. )
  2631. for w in (
  2632. row,
  2633. left,
  2634. batch_lbl,
  2635. meta_lbl,
  2636. code_col,
  2637. code_lbl,
  2638. name_col,
  2639. name_lbl,
  2640. ):
  2641. w.bind("<Button-1>", _on_click)
  2642. w.bind("<MouseWheel>", _on_mousewheel)
  2643. w.bind("<Button-4>", _on_mousewheel)
  2644. w.bind("<Button-5>", _on_mousewheel)
  2645. if preserve_selection and selected_id is not None and jo.get("id") == selected_id:
  2646. found_row = row
  2647. if found_row is not None:
  2648. set_row_highlight(found_row, True)
  2649. selected_row_holder[0] = found_row
  2650. def refresh_visible_list() -> None:
  2651. """Re-apply search filter to last fetched rows without hitting the API."""
  2652. raw = last_data_ref[0]
  2653. if raw is None:
  2654. return
  2655. ps = last_plan_start_ref[0]
  2656. needle = search_var.get().strip()
  2657. shown = _filter_job_orders_by_search(raw, needle) if needle else raw
  2658. for w in inner.winfo_children():
  2659. w.destroy()
  2660. _build_list_from_data(shown, ps, preserve_selection=True)
  2661. search_entry.bind("<KeyRelease>", lambda e: refresh_visible_list())
  2662. def load_job_orders(from_user_date_change: bool = False) -> None:
  2663. if after_id_ref[0] is not None:
  2664. root.after_cancel(after_id_ref[0])
  2665. after_id_ref[0] = None
  2666. date_str = date_var.get().strip()
  2667. try:
  2668. plan_start = date.fromisoformat(date_str)
  2669. except ValueError:
  2670. messagebox.showerror("日期錯誤", f"請使用 yyyy-MM-dd 格式。目前:{date_str}")
  2671. return
  2672. if from_user_date_change:
  2673. selected_row_holder[0] = None
  2674. selected_jo_id_ref[0] = None
  2675. try:
  2676. data = fetch_job_orders(base_url_ref[0], plan_start)
  2677. except requests.RequestException:
  2678. set_status_error()
  2679. after_id_ref[0] = root.after(RETRY_MS, lambda: load_job_orders(from_user_date_change=False))
  2680. return
  2681. set_status_ok()
  2682. old_data = last_data_ref[0]
  2683. last_data_ref[0] = data
  2684. last_plan_start_ref[0] = plan_start
  2685. data_changed = not _data_equal(old_data, data)
  2686. if data_changed or from_user_date_change:
  2687. printing_busy = (
  2688. dataflex_busy_ref[0]
  2689. or label_busy_ref[0]
  2690. or laser_send_busy_ref[0]
  2691. )
  2692. # Do not destroy/rebuild all rows while printing — that removes click bindings and
  2693. # can hide DataFlex「停止列印」. Retry until idle (one deferred pass at a time).
  2694. if printing_busy and not from_user_date_change:
  2695. after_id_ref[0] = root.after(
  2696. JOB_LIST_DEFER_WHILE_PRINTING_MS,
  2697. lambda: load_job_orders(from_user_date_change=False),
  2698. )
  2699. else:
  2700. # Rebuild list: clear and rebuild from current data (last_data_ref already updated)
  2701. for w in inner.winfo_children():
  2702. w.destroy()
  2703. preserve = not from_user_date_change
  2704. needle = search_var.get().strip()
  2705. shown = _filter_job_orders_by_search(data, needle) if needle else data
  2706. _build_list_from_data(shown, plan_start, preserve_selection=preserve)
  2707. if from_user_date_change:
  2708. canvas.yview_moveto(0)
  2709. if JOB_LIST_AUTO_REFRESH_MS > 0:
  2710. after_id_ref[0] = root.after(
  2711. JOB_LIST_AUTO_REFRESH_MS,
  2712. lambda: load_job_orders(from_user_date_change=False),
  2713. )
  2714. # Load default (today) on start; then start printer connection check
  2715. root.after(100, lambda: load_job_orders(from_user_date_change=True))
  2716. root.after(300, check_printer)
  2717. root.mainloop()
  2718. def _startup_error_log_path() -> str:
  2719. if getattr(sys, "frozen", False):
  2720. base = os.path.dirname(sys.executable)
  2721. else:
  2722. base = os.path.dirname(os.path.abspath(__file__))
  2723. return os.path.join(base, "bag3_startup_error.log")
  2724. if __name__ == "__main__":
  2725. try:
  2726. main()
  2727. except SystemExit:
  2728. raise
  2729. except Exception:
  2730. import traceback
  2731. log_path = _startup_error_log_path()
  2732. try:
  2733. with open(log_path, "w", encoding="utf-8") as f:
  2734. traceback.print_exc(file=f)
  2735. except OSError:
  2736. log_path = "(could not write log file)"
  2737. msg = f"Bag3 啟動失敗,詳情已寫入:\n{log_path}"
  2738. print(msg, file=sys.stderr)
  2739. traceback.print_exc()
  2740. try:
  2741. _err_root = tk.Tk()
  2742. _err_root.withdraw()
  2743. messagebox.showerror("Bag3", msg)
  2744. _err_root.destroy()
  2745. except Exception:
  2746. pass
  2747. if getattr(sys, "frozen", False):
  2748. try:
  2749. input("按 Enter 關閉…")
  2750. except (EOFError, KeyboardInterrupt):
  2751. pass
  2752. sys.exit(1)