orders.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import time
  2. from logging import getLogger
  3. from threading import Thread
  4. from uuid import uuid4
  5. from enums.locations import Locations
  6. from fastapi import HTTPException, APIRouter, Depends
  7. from fastapi.responses import JSONResponse
  8. from fudo import fudo
  9. from models.sales import ItemWeb, OrderWeb
  10. from models.items import Item, Order
  11. from models.user import User
  12. from services.fudo_service import add_product_to_fudo
  13. from services.email_service import get_email_sender
  14. import services.print_service as ps
  15. from services.data_service import DataServiceFactory
  16. from config.mails import PRINTER_DISCONNECTED_MAIL
  17. from config.messages import ErrorResponse, SuccessResponse, UserResponse
  18. from config.settings import DEVELOPMENT
  19. from auth.security import get_current_user
  20. from data.product_category import CAT_ITEMS
  21. from utils.responses import error_response, success_response
  22. from datetime import datetime
  23. logger = getLogger(__name__)
  24. # Data services initialization
  25. user_data_service = DataServiceFactory.get_user_service()
  26. product_data_service = DataServiceFactory.get_product_service()
  27. sale_data_service = DataServiceFactory.get_sales_service()
  28. # Global variables
  29. printer_orders = []
  30. order_router = APIRouter()
  31. name_promo = "Shop"
  32. @order_router.post("/send")
  33. async def printer_order(order: OrderWeb, current_user: User = Depends(get_current_user)):
  34. """Process printer order"""
  35. logger.info(f"Printer order received from user {current_user.email} for table {order.table}")
  36. # Extract order data
  37. items = order.items
  38. table = order.table
  39. # Input validation
  40. if not items or not table:
  41. logger.warning(f"Invalid order data from user {current_user.email}: missing items or table")
  42. return error_response(message=ErrorResponse.MISSING_FIELDS, status_code=400)
  43. if not isinstance(table, int):
  44. logger.warning(f"Invalid table type from user {current_user.email}: {type(table)}")
  45. return error_response(message=ErrorResponse.INVALID_TABLE_TYPE, status_code=400)
  46. logger.info(f"Processing order for table {table} with {len(items)} items")
  47. # Get products data
  48. try:
  49. products = product_data_service.get_products([item.id for item in items])
  50. # Me aseguro de que los items y los productos esten en el mismo orden
  51. products = list(sorted(products, key=lambda x: x.id))
  52. items = list(sorted(items, key=lambda x: x.id))
  53. logger.info(f"Retrieved {len(products)} products from database")
  54. except Exception as e:
  55. error_msg = f"Error getting products: {e}"
  56. logger.error(error_msg)
  57. return error_response(message=error_msg, status_code=500)
  58. printers = {}
  59. for product in products:
  60. location = CAT_ITEMS[product.type or ""]
  61. if location.value not in printers:
  62. printers[location.value] = ps.get_status(location)
  63. # Printer status validation
  64. if not DEVELOPMENT:
  65. try:
  66. printer_status = [key for key, value in printers.items() if value == False]
  67. print(printer_status)
  68. if list(printer_status):
  69. logger.error(f"Printer is not connected. Order from user {current_user.email} cannot be processed.")
  70. # Send notification email to admins
  71. email_thread = Thread(
  72. target=get_email_sender().send_email,
  73. args=(
  74. PRINTER_DISCONNECTED_MAIL["subject"].format(location=", ".join([loc for loc in printer_status])),
  75. PRINTER_DISCONNECTED_MAIL["body"].format(location=", ".join([loc for loc in printer_status]), timestamp=datetime.now().strftime("%Y-%m-%d %H:%M:%S")), #type: ignore
  76. ["erwinjacimino2003@gmail.com", "mompyn@gmail.com"
  77. # , "i.perez03@ufromail.cl", "marceloburkart94@gmail.com"
  78. ]
  79. ),
  80. daemon=True
  81. )
  82. email_thread.start()
  83. return error_response(message=ErrorResponse.PRINTER_DISCONNECTED, status_code=424)
  84. except Exception as e:
  85. logger.error(f"Error checking printer status: {e}")
  86. return error_response(message=f"Error checking printer status: {e}", status_code=424)
  87. # Input validation
  88. # Add products to Fudo
  89. product_errors = []
  90. beers_for_promo = 0
  91. try:
  92. fudo.get_token()
  93. logger.info("Fudo token obtained successfully")
  94. for item, product in zip(items, products):
  95. try:
  96. # Si es dia de promo y es menos de las 8 pm
  97. if time.localtime().tm_wday + 1 == product.promo_day and product.promo_id and time.localtime().tm_hour < 20:
  98. logger.info(f"Applying promotion for product {product.id} on table {table}")
  99. fudo_product = add_product_to_fudo(product.promo_id, item.quantity, table)
  100. #en caso contrario
  101. else:
  102. if product.type == name_promo and current_user.name != "Guest":
  103. beers_for_promo += item.quantity
  104. logger.debug(f"Added {item.quantity} beers for promotion calculation")
  105. fudo_product = add_product_to_fudo(item.id, item.quantity, table)
  106. logger.info(f"Added product {item.id} to table {table} with quantity {item.quantity} in fudo")
  107. if not fudo_product:
  108. error_msg = f"Error adding product {item.id} to table {table}."
  109. product_errors.append(error_msg)
  110. logger.error(error_msg)
  111. except Exception as e:
  112. error_msg = f"Error processing product {item.id}: {e}"
  113. logger.error(error_msg)
  114. product_errors.append(error_msg)
  115. except Exception as e:
  116. error_msg = f"Error with Fudo integration: {e}"
  117. logger.error(error_msg)
  118. return error_response(message=error_msg, status_code=500)
  119. if product_errors:
  120. logger.error(f"Product errors occurred: {product_errors}")
  121. return error_response(error={"errors": product_errors}, message=ErrorResponse.PRODUCT_ADD_ERROR, status_code=424)
  122. # Get active sale
  123. try:
  124. active_sale_id = fudo.get_active_sale(fudo.get_table(table))
  125. if not active_sale_id:
  126. error_msg = f"No active sale found for table {table}"
  127. logger.error(error_msg)
  128. return error_response(message=error_msg, status_code=404)
  129. active_sale_id = active_sale_id['id']
  130. logger.info(f"Active sale found for table {table}: {active_sale_id}")
  131. except Exception as e:
  132. error_msg = f"Error retrieving active sale for table {table}: {e}"
  133. logger.error(error_msg)
  134. return error_response(message=error_msg, status_code=500)
  135. # Update user reward progress
  136. try:
  137. new_progress = current_user.reward_progress + beers_for_promo * 10
  138. user_data_service.set_reward_progress(current_user.id, new_progress)
  139. logger.info(f"Updated reward progress for user {current_user.email}: {current_user.reward_progress} -> {new_progress}")
  140. except Exception as e:
  141. error_msg = f"Error updating reward progress for user {current_user.id}: {e}"
  142. logger.error(error_msg)
  143. # Don't fail the order for this, just log it
  144. new_progress = current_user.reward_progress
  145. # Create sale record
  146. try:
  147. sale = sale_data_service.create(
  148. order.customerId,
  149. active_sale_id or uuid4().hex,
  150. order.totalAmount,
  151. order.table,
  152. [item.id for item in items],
  153. [item.quantity for item in items]
  154. )
  155. if sale > 0:
  156. logger.info(f"Sale created successfully: ID {sale}")
  157. else:
  158. error_msg = "Failed to create sale record"
  159. logger.error(error_msg)
  160. return error_response(message=error_msg, status_code=500)
  161. except Exception as e:
  162. error_msg = f"Error creating sale record: {e}"
  163. logger.error(error_msg)
  164. return error_response(message=error_msg, status_code=500)
  165. # Print order
  166. try:
  167. print("items:")
  168. print(items)
  169. print("products:")
  170. print(products)
  171. pizza_items = [ Item(name=product.name, price=item.price, quantity=item.quantity, comment=item.comment) for item, product in zip(items, products) if CAT_ITEMS.get(product.type) == Locations.PIZZAS]#type: ignore
  172. burger_items = [ Item(name=product.name, price=item.price, quantity=item.quantity, comment=item.comment) for item, product in zip(items, products) if CAT_ITEMS.get(product.type) == Locations.BURGUER]#type: ignore
  173. bar_items = [ Item(name=product.name, price=item.price, quantity=item.quantity, comment=item.comment) for item, product in zip(items, products) if CAT_ITEMS.get(product.type) == Locations.BAR]#type: ignore
  174. coctelery_items = [ Item(name=product.name, price=item.price, quantity=item.quantity, comment=item.comment) for item, product in zip(items, products) if CAT_ITEMS.get(product.type) == Locations.COCTELERY]#type: ignore
  175. print("bar_items:")
  176. print(list(map(lambda x: x.model_dump(), bar_items)))
  177. if pizza_items:
  178. ps.print_order(Order(table=table, items=pizza_items, customerName=current_user.name, totalAmount=order.totalAmount, orderDate=order.orderDate), location=Locations.PIZZAS)
  179. logger.info(f"Order pizza printed successfully for table {table}")
  180. if burger_items:
  181. ps.print_order(Order(table=table, items=burger_items, customerName=current_user.name, totalAmount=order.totalAmount, orderDate=order.orderDate), location=Locations.BURGUER)
  182. logger.info(f"Order burger printed successfully for table {table}")
  183. if bar_items:
  184. ps.print_order(Order(table=table, items=bar_items, customerName=current_user.name, totalAmount=order.totalAmount, orderDate=order.orderDate), location=Locations.BAR)
  185. logger.info(f"Order bar printed successfully for table {table}")
  186. if coctelery_items:
  187. ps.print_order(Order(table=table, items=coctelery_items, customerName=current_user.name, totalAmount=order.totalAmount, orderDate=order.orderDate), location=Locations.COCTELERY)
  188. logger.info(f"Order coctelery printed successfully for table {table}")
  189. logger.info(f"Order printed successfully for table {table}")
  190. except Exception as e:
  191. error_msg = f"Error printing order for table {table}: {e}"
  192. logger.error(error_msg)
  193. return error_response(message=error_msg, status_code=500)
  194. # Don't fail the order for print issues, just log it
  195. logger.info(f"Logging order for table {table} with sale ID {sale}, products= {[(product.name, item.quantity) for product, item in zip(products, items)]}")
  196. logger.info(f"Order processing completed successfully for table {table}, sale ID: {sale}")
  197. return success_response(data={"new_progress": new_progress}, message=SuccessResponse.ORDER_SUCCESS)