print_service.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import requests
  2. from models.sales import OrderWeb, ItemWeb
  3. from services.data_service import DataServiceFactory
  4. from models.items import Order
  5. user_data_service = DataServiceFactory.get_user_service()
  6. def print_order(order:Order):
  7. """Send order to printer"""
  8. if not order.items or not order.table:
  9. raise ValueError("Order must have items and a table number.")
  10. # Prepare the order data for printing
  11. order_data = {
  12. "table": order.table,
  13. "items": [{"name": item.name,"price": item.price, "quantity": item.quantity} for item in order.items],
  14. "customerName": order.customerName,
  15. "totalAmount": order.totalAmount,
  16. "orderDate": order.orderDate
  17. }
  18. print("Order data prepared for printing:", order_data)
  19. # Send the order data to the printer service
  20. response = requests.post("http://localhost:5004/print", json=order_data)
  21. if response.status_code != 200:
  22. raise Exception(f"Failed to print order: {response.text}")
  23. return response.json() # Return the response from the printer service
  24. def get_status():
  25. """Get the status of the printer service"""
  26. try:
  27. response = requests.get("http://localhost:5004/status")
  28. data = response.json()
  29. print("Printer service status:", data)
  30. if data.get("status"):
  31. return True
  32. else:
  33. return False
  34. except requests.RequestException as e:
  35. raise Exception(f"Error connecting to printer service: {e}")