61 lines
2.3 KiB
Python
61 lines
2.3 KiB
Python
import logging
|
|
from datetime import timezone
|
|
from zoneinfo import ZoneInfo
|
|
|
|
import requests
|
|
from bson import ObjectId
|
|
|
|
from elm.types import Seat
|
|
from elm.types.CateringTypes import CateringOrder
|
|
from elm.types.ConfigurationTypes import ReceiptPrintingConfiguration
|
|
from elm.types.User import User
|
|
|
|
logger = logging.getLogger(__name__.split(".")[-1])
|
|
logging.getLogger("urllib3").setLevel(logging.FATAL) # Disable logging for urllib3
|
|
|
|
class ReceiptPrintingService:
|
|
def __init__(self, config: ReceiptPrintingConfiguration, dev_mode_enabled: bool) -> None:
|
|
self._config = config
|
|
self._dev_mode_enabled = dev_mode_enabled
|
|
self._url = f"http://{self._config.host}:{self._config.port}/{self._config.order_print_endpoint}"
|
|
|
|
async def print_order(self, user: User, order: CateringOrder) -> None:
|
|
seat = await Seat.find_one({"user.$id": ObjectId(user.id)})
|
|
if seat is None:
|
|
seat_id = " - "
|
|
else:
|
|
seat_id = str(seat.seat_id)
|
|
|
|
|
|
menu_items_payload = []
|
|
for item in order.items:
|
|
menu_items_payload.append({
|
|
"menu_item_name": item.name,
|
|
"mods": [f"{'MIT' if mod.selected else 'OHNE'} {mod.label}" for mod in item.selected_modifiers],
|
|
"amount": 1
|
|
})
|
|
|
|
payload = {
|
|
"order_id": str(order.id)[-5:],
|
|
"order_date": order.created_at.replace(tzinfo=timezone.utc).astimezone(ZoneInfo("Europe/Berlin")).strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z',
|
|
"customer_name": user.user_name,
|
|
"seat_id": seat_id,
|
|
"items": menu_items_payload
|
|
}
|
|
|
|
logger.info(f"Sending print order to {self._url}: {payload}")
|
|
try:
|
|
response = requests.post(
|
|
self._url,
|
|
json=payload,
|
|
headers={"x-password": self._config.password},
|
|
timeout=2.0
|
|
)
|
|
if response.status_code != 200:
|
|
logger.error(f"Received an error with code {response.status_code}: {response.text}")
|
|
except Exception as e:
|
|
if self._dev_mode_enabled:
|
|
logger.info("An error occurred trying to print a receipt: %s", e)
|
|
return
|
|
logger.error("An error occurred trying to print a receipt: %s", e)
|