fix: Se Añade soporte para impresión
This commit is contained in:
parent
b17a2b83e9
commit
50674b797b
102
api.py
102
api.py
@ -1,6 +1,6 @@
|
|||||||
from fastapi import FastAPI, Response
|
from fastapi import FastAPI, Response
|
||||||
from escpos.printer import Dummy, Network
|
from escpos.printer import Dummy, Network, Usb
|
||||||
import sys
|
# import sys
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@ -15,27 +15,38 @@ app = FastAPI(
|
|||||||
|
|
||||||
class Info(BaseModel):
|
class Info(BaseModel):
|
||||||
content: str
|
content: str
|
||||||
printer_type: str
|
|
||||||
ip_printer: str
|
ip_printer: str
|
||||||
|
user_name: str
|
||||||
|
printer_type: str
|
||||||
idVendor: str
|
idVendor: str
|
||||||
idProduct: str
|
idProduct: str
|
||||||
user_name: str
|
|
||||||
|
|
||||||
|
|
||||||
def print_bill(data, address=None, idVendor=None idProduct=None, waiter):
|
def open_conexion(info):
|
||||||
d = data
|
if info.get('ip_printer'):
|
||||||
# Crea una instancia de la impresora ficticia
|
# Crea una instancia de la impresora de Red
|
||||||
if address:
|
printer = Network(str(info.get('ip_printer')))
|
||||||
printer = Network(str(address))
|
printer.open()
|
||||||
elif idVendor and idProduct:
|
elif info.get('idVendor') and info.get('idProduct'):
|
||||||
printer = Usb(int(idVendor), int(idProduct), 0)
|
# Crea una instancia de la impresora USB
|
||||||
|
printer = Usb(
|
||||||
|
int(info.get('idVendor')),
|
||||||
|
int(info.get('idProduct')), 0)
|
||||||
else:
|
else:
|
||||||
|
# Crea una instancia de la impresora ficticia
|
||||||
printer = Dummy()
|
printer = Dummy()
|
||||||
|
|
||||||
# Imprime el encabezado
|
return printer
|
||||||
|
|
||||||
|
|
||||||
|
def print_bill(data, info, waiter=None):
|
||||||
|
d = data
|
||||||
|
|
||||||
|
# Abre Conexión Con la Impresora
|
||||||
|
printer = open_conexion(info)
|
||||||
printer.set(align='center', bold=False, height=1, width=1)
|
printer.set(align='center', bold=False, height=1, width=1)
|
||||||
printer.text(d["shop_name"]+'\n')
|
printer.text(d["shop_name"] + '\n')
|
||||||
printer.text(d["shop_address"]+'\n')
|
printer.text(d["shop_address"] + '\n')
|
||||||
printer.set(align='left', bold=False, height=1, width=1)
|
printer.set(align='left', bold=False, height=1, width=1)
|
||||||
printer.textln('===============================================')
|
printer.textln('===============================================')
|
||||||
text = d['state']
|
text = d['state']
|
||||||
@ -45,14 +56,14 @@ def print_bill(data, address=None, idVendor=None idProduct=None, waiter):
|
|||||||
str(d['invoice']['resolution']['resolution_number']) \
|
str(d['invoice']['resolution']['resolution_number']) \
|
||||||
+ "\nValida desde " + \
|
+ "\nValida desde " + \
|
||||||
d['invoice']['resolution']['valid_date_time_from'] + \
|
d['invoice']['resolution']['valid_date_time_from'] + \
|
||||||
" hasta "+str(d['invoice']['resolution']['valid_date_time_to'])
|
" hasta " + str(d['invoice']['resolution']['valid_date_time_to'])
|
||||||
printer.textln(text)
|
printer.textln(text)
|
||||||
printer.ln()
|
printer.ln()
|
||||||
text = "Factura #: " + d['invoice']['invoice_number']
|
text = "Factura #: " + d['invoice']['invoice_number']
|
||||||
printer.textln(text)
|
printer.textln(text)
|
||||||
printer.text("Cliente: " + d["party"]+'\n')
|
printer.text("Cliente: " + d["party"] + '\n')
|
||||||
printer.text("CC/NIT: " + d["tax_identifier_code"]+'\n')
|
printer.text("CC/NIT: " + d["tax_identifier_code"] + '\n')
|
||||||
printer.text("Direccion: " + d["address"]+'\n')
|
printer.text("Direccion: " + d["address"] + '\n')
|
||||||
text = 'MESA: ' + str(d['table'] + "\n")
|
text = 'MESA: ' + str(d['table'] + "\n")
|
||||||
printer.text(text)
|
printer.text(text)
|
||||||
printer.textln('===============================================')
|
printer.textln('===============================================')
|
||||||
@ -68,13 +79,13 @@ def print_bill(data, address=None, idVendor=None idProduct=None, waiter):
|
|||||||
|
|
||||||
printer.set(align='right', bold=False, height=1, width=1)
|
printer.set(align='right', bold=False, height=1, width=1)
|
||||||
printer.textln('================================================')
|
printer.textln('================================================')
|
||||||
text = "Descuento Realizado: "+str(d["total_discount"])+"\n"
|
text = "Descuento Realizado: " + str(d["total_discount"]) + "\n"
|
||||||
printer.text(text)
|
printer.text(text)
|
||||||
text = "Total (sin impuestos): "+str(d["untaxed_amount"])+"\n"
|
text = "Total (sin impuestos): " + str(d["untaxed_amount"]) + "\n"
|
||||||
printer.text(text)
|
printer.text(text)
|
||||||
text = "Impuestos (INC): "+str(d["tax_amount"])+"\n"
|
text = "Impuestos (INC): " + str(d["tax_amount"]) + "\n"
|
||||||
printer.text(text)
|
printer.text(text)
|
||||||
text = "Total: "+str(d["total"])+"\n"
|
text = "Total: " + str(d["total"]) + "\n"
|
||||||
printer.text(text)
|
printer.text(text)
|
||||||
printer.ln()
|
printer.ln()
|
||||||
if 'payments' in d.keys():
|
if 'payments' in d.keys():
|
||||||
@ -82,20 +93,20 @@ def print_bill(data, address=None, idVendor=None idProduct=None, waiter):
|
|||||||
printer.textln('================================================')
|
printer.textln('================================================')
|
||||||
|
|
||||||
for payment in d['payments']:
|
for payment in d['payments']:
|
||||||
text = str(payment["statement"])+" $"+str(payment["amount"])
|
text = str(payment["statement"]) + " $" + str(payment["amount"])
|
||||||
printer.textln(text)
|
printer.textln(text)
|
||||||
printer.set(align='center', bold=False, height=1, width=1)
|
printer.set(align='center', bold=False, height=1, width=1)
|
||||||
printer.textln('==============================================\n')
|
printer.textln('==============================================\n')
|
||||||
printer.text("Sigue nuestras redes sociales\n")
|
printer.text("Sigue nuestras redes sociales\n")
|
||||||
printer.text("@bicipizza\n")
|
printer.text("@Rustik\n")
|
||||||
printer.text("Recuerde que la propina es voluntaria.\n")
|
printer.text("Recuerde que la propina es voluntaria.\n")
|
||||||
printer.text("Gracias por visitarnos, vuelva pronto.\n")
|
printer.text("Gracias por visitarnos, vuelva pronto.\n")
|
||||||
printer.text("SOFTWARE POTENCIADO POR ONECLUSTER.ORG.\n")
|
printer.text("SOFTWARE POTENCIADO POR ONECLUSTER.ORG.\n")
|
||||||
format_date_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
format_date_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||||
printer.text(str(format_date_time)+'\n')
|
printer.text(str(format_date_time) + '\n')
|
||||||
if waiter:
|
if waiter:
|
||||||
printer.text("Atendido Por: \n")
|
printer.text("Atendido Por: \n")
|
||||||
printer.text(str(waiter)+'\n')
|
printer.text(str(waiter) + '\n')
|
||||||
|
|
||||||
# Corta el papel (solo para impresoras que soportan esta función)
|
# Corta el papel (solo para impresoras que soportan esta función)
|
||||||
printer.cut()
|
printer.cut()
|
||||||
@ -106,20 +117,18 @@ def print_bill(data, address=None, idVendor=None idProduct=None, waiter):
|
|||||||
# sys.stdout.write(ticket_contenido.decode('utf-8'))
|
# sys.stdout.write(ticket_contenido.decode('utf-8'))
|
||||||
|
|
||||||
|
|
||||||
def print_customer_order(data, address, waiter):
|
def print_customer_order(data, info, waiter):
|
||||||
d = data
|
d = data
|
||||||
# Crea una instancia de la impresora ficticia
|
# Abre Conexión Con la Impresora
|
||||||
# printer = Network(str(address))
|
printer = open_conexion(info)
|
||||||
# printer.open()
|
|
||||||
printer = Dummy()
|
|
||||||
# Imprime el encabezado
|
# Imprime el encabezado
|
||||||
printer.set(align='center', bold=False, height=1, width=1)
|
printer.set(align='center', bold=False, height=1, width=1)
|
||||||
format_date_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
format_date_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||||
printer.text(str(format_date_time)+'\n')
|
printer.text(str(format_date_time) + '\n')
|
||||||
|
|
||||||
if waiter:
|
if waiter:
|
||||||
printer.text("Pedido Por: \n")
|
printer.text("Pedido Por: \n")
|
||||||
printer.text(str(waiter)+'\n')
|
printer.text(str(waiter) + '\n')
|
||||||
printer.set(
|
printer.set(
|
||||||
align='center', bold=False, height=2, width=2, custom_size=True
|
align='center', bold=False, height=2, width=2, custom_size=True
|
||||||
)
|
)
|
||||||
@ -133,17 +142,18 @@ def print_customer_order(data, address, waiter):
|
|||||||
for line in d["lines"]:
|
for line in d["lines"]:
|
||||||
if line['type'] != 'title':
|
if line['type'] != 'title':
|
||||||
if combination_pizza and pizza < 2:
|
if combination_pizza and pizza < 2:
|
||||||
printer.set(align='center', bold=False, height=2, width=2, custom_size=True)
|
printer.set(align='center', bold=False, height=2, width=2,
|
||||||
|
custom_size=True)
|
||||||
pizza += 1
|
pizza += 1
|
||||||
elif pizza >= 2:
|
elif pizza >= 2:
|
||||||
combination_pizza = False
|
combination_pizza = False
|
||||||
printer.set(
|
printer.set(
|
||||||
align='left', bold=False, height=2, width=2, custom_size=True
|
align='left', bold=False, height=2, width=2,
|
||||||
)
|
custom_size=True)
|
||||||
else:
|
else:
|
||||||
printer.set(
|
printer.set(
|
||||||
align='left', bold=False, height=2, width=2, custom_size=True
|
align='left', bold=False, height=2, width=2,
|
||||||
)
|
custom_size=True)
|
||||||
|
|
||||||
text = line['product'] + " " + str(line['quantity']) + "\n"
|
text = line['product'] + " " + str(line['quantity']) + "\n"
|
||||||
printer.text(text)
|
printer.text(text)
|
||||||
@ -166,21 +176,19 @@ def print_customer_order(data, address, waiter):
|
|||||||
printer.cut()
|
printer.cut()
|
||||||
printer.close()
|
printer.close()
|
||||||
# Obtiene el contenido del ticket de prueba
|
# Obtiene el contenido del ticket de prueba
|
||||||
ticket_contenido = printer.output
|
# ticket_contenido = printer.output
|
||||||
|
|
||||||
# Imprime el contenido en la consola
|
# Imprime el contenido en la consola
|
||||||
sys.stdout.write(ticket_contenido.decode('utf-8'))
|
# sys.stdout.write(ticket_contenido.decode('utf-8'))
|
||||||
|
|
||||||
|
|
||||||
@app.post("/print_bill")
|
@app.post("/print_bill")
|
||||||
def print_ticket_bill(info: Info):
|
def print_ticket_bill(info: Info):
|
||||||
info = dict(info)
|
info = dict(info)
|
||||||
data = info["content"]
|
data = info["content"]
|
||||||
address = info["ip_printer"]
|
|
||||||
|
|
||||||
waiter = info["user_name"]
|
waiter = info["user_name"]
|
||||||
data = json.loads(data.replace("'", "\""))
|
data = json.loads(data.replace("'", "\""))
|
||||||
print_bill(data, address, waiter)
|
print_bill(data, info, waiter)
|
||||||
|
|
||||||
message = "!Impresión Realizada!"
|
message = "!Impresión Realizada!"
|
||||||
|
|
||||||
@ -190,11 +198,11 @@ def print_ticket_bill(info: Info):
|
|||||||
@app.post("/order_kitchen")
|
@app.post("/order_kitchen")
|
||||||
def print_ticket_file_kitchen(info: Info):
|
def print_ticket_file_kitchen(info: Info):
|
||||||
info = dict(info)
|
info = dict(info)
|
||||||
|
print(info)
|
||||||
data = info["content"]
|
data = info["content"]
|
||||||
address = info["ip_printer"]
|
|
||||||
waiter = info["user_name"]
|
waiter = info["user_name"]
|
||||||
data = json.loads(data.replace("'", "\""))
|
data = json.loads(data.replace("'", "\""))
|
||||||
print_customer_order(data, address, waiter)
|
print_customer_order(data, info, waiter)
|
||||||
|
|
||||||
message = "!Impresión Realizada!"
|
message = "!Impresión Realizada!"
|
||||||
|
|
||||||
@ -203,12 +211,12 @@ def print_ticket_file_kitchen(info: Info):
|
|||||||
|
|
||||||
@app.post("/order_bar")
|
@app.post("/order_bar")
|
||||||
def print_ticket_file_bar(info: Info):
|
def print_ticket_file_bar(info: Info):
|
||||||
|
print(info)
|
||||||
info = dict(info)
|
info = dict(info)
|
||||||
data = info["content"]
|
data = info["content"]
|
||||||
address = info["ip_printer"]
|
|
||||||
waiter = info["user_name"]
|
waiter = info["user_name"]
|
||||||
data = json.loads(data.replace("'", "\""))
|
data = json.loads(data.replace("'", "\""))
|
||||||
print_customer_order(data, address, waiter)
|
print_customer_order(data, info, waiter)
|
||||||
|
|
||||||
message = "!Impresión Realizada!"
|
message = "!Impresión Realizada!"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user