71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
import sys
|
|
import json
|
|
from datetime import datetime
|
|
from escpos.printer import Win32Raw
|
|
|
|
LINE_WIDTH = 48 # 80mm paper
|
|
|
|
def safe_float(v):
|
|
return float(v) if v is not None else 0.0
|
|
|
|
def money(n):
|
|
return f"{int(safe_float(n)):,}"
|
|
|
|
def space_between(left, right, width=LINE_WIDTH):
|
|
space = max(1, width - len(str(left)) - len(str(right)))
|
|
return f"{left}{' ' * space}{right}"
|
|
|
|
def print_receipt(data, printer_name="XP-80C"):
|
|
p = Win32Raw(printer_name=printer_name)
|
|
|
|
# Big bold header
|
|
p.set(align='center', bold=True, double_height=True, double_width=True)
|
|
p.text("ERP\n")
|
|
|
|
p.set(align='center', bold=False, normal_textsize=True)
|
|
p.text(f"Invoice: {data['code']}\n")
|
|
p.text(f"{data['ordered_at'][:10]}\n")
|
|
p.text("-" * LINE_WIDTH + "\n")
|
|
|
|
p.set(align='left')
|
|
p.text(f"Customer: {data['customer_name']}\n")
|
|
p.text(f"Phone: {data['customer_phone']}\n")
|
|
p.text(f"Address: {data['shipping_address']}\n")
|
|
p.text("-" * LINE_WIDTH + "\n")
|
|
|
|
for item in data['items']:
|
|
p.text(f"{item['name']}\n")
|
|
p.text(f" {item['variant']}\n")
|
|
p.text(space_between("", money(item['price'])) + "\n")
|
|
|
|
p.text("-" * LINE_WIDTH + "\n")
|
|
|
|
p.text(space_between("Subtotal:", money(data['product_amount'])) + "\n")
|
|
if safe_float(data['shipping_fee']) > 0:
|
|
p.text(space_between("Shipping:", money(data['shipping_fee'])) + "\n")
|
|
if safe_float(data['discount_amount']) > 0:
|
|
p.text(space_between("Discount:", f"-{money(data['discount_amount'])}") + "\n")
|
|
|
|
p.set(bold=True)
|
|
p.text(space_between("TOTAL:", money(data['final_amount']) + " VND") + "\n")
|
|
p.set(bold=False)
|
|
|
|
p.text("-" * LINE_WIDTH + "\n")
|
|
|
|
if safe_float(data['cash_amount']) > 0:
|
|
p.text(space_between("Cash:", money(data['cash_amount'])) + "\n")
|
|
if safe_float(data['transfer_amount']) > 0:
|
|
p.text(space_between("Transfer:", money(data['transfer_amount'])) + "\n")
|
|
|
|
p.text("\n\n")
|
|
p.set(align='center')
|
|
now_str = datetime.now().strftime("%H:%M %d/%m/%Y")
|
|
p.text(f"{now_str}\n")
|
|
|
|
p.text("\n" * 3)
|
|
p.cut()
|
|
|
|
if __name__ == "__main__":
|
|
data = json.loads(sys.argv[1])
|
|
print_receipt(data)
|
|
print("Sent to printer.") |