orders.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. import db
  2. import schemas
  3. import auth_utils
  4. import notifications
  5. import config
  6. from typing import List, Optional
  7. import json
  8. import os
  9. import uuid
  10. import shutil
  11. import hashlib
  12. import preview_utils
  13. import slicer_utils
  14. from fastapi import APIRouter, Request, Form, Depends, HTTPException, BackgroundTasks, UploadFile, File
  15. from services import pricing, order_processing, event_hooks
  16. from services.audit_service import audit_service
  17. from services.rate_limit_service import rate_limit_service
  18. from dependencies import get_current_user, require_admin, get_current_user_optional
  19. from pydantic import BaseModel
  20. from datetime import datetime
  21. import locales
  22. from services.global_manager import global_manager
  23. router = APIRouter(prefix="/orders", tags=["orders"])
  24. @router.post("")
  25. async def create_order(
  26. request: Request,
  27. background_tasks: BackgroundTasks,
  28. first_name: str = Form(...),
  29. last_name: str = Form(...),
  30. phone: str = Form(...),
  31. email: str = Form(...),
  32. shipping_address: str = Form(...),
  33. model_link: Optional[str] = Form(None),
  34. allow_portfolio: bool = Form(False),
  35. notes: Optional[str] = Form(None),
  36. material_id: int = Form(...),
  37. file_ids: str = Form("[]"),
  38. file_quantities: str = Form("[]"),
  39. quantity: int = Form(1),
  40. color_name: Optional[str] = Form(None),
  41. is_company: bool = Form(False),
  42. company_name: Optional[str] = Form(None),
  43. company_pib: Optional[str] = Form(None),
  44. company_address: Optional[str] = Form(None),
  45. user: Optional[dict] = Depends(get_current_user_optional)
  46. ):
  47. ip = request.client.host if request.client else "unknown"
  48. email_addr = email.lower()
  49. lang = request.query_params.get("lang", "en")
  50. is_admin = user.get("role") == "admin" if user else False
  51. if not is_admin and rate_limit_service.is_order_flooding(email_addr, ip):
  52. raise HTTPException(
  53. status_code=429,
  54. detail=locales.translate_error("flood_control", lang)
  55. )
  56. user_id = user.get("id") if user else None
  57. parsed_ids = []
  58. parsed_quantities = []
  59. if file_ids:
  60. try:
  61. parsed_ids = json.loads(file_ids)
  62. parsed_quantities = json.loads(file_quantities)
  63. except:
  64. pass
  65. name_col = f"name_{lang}" if lang in ["en", "ru", "me"] else "name_en"
  66. mat_info = db.execute_query(f"SELECT {name_col}, price_per_cm3 FROM materials WHERE id = %s", (material_id,))
  67. mat_name = mat_info[0][name_col] if mat_info else "Unknown"
  68. mat_price = mat_info[0]['price_per_cm3'] if mat_info else 0.0
  69. file_sizes = []
  70. if parsed_ids:
  71. format_strings = ','.join(['%s'] * len(parsed_ids))
  72. file_rows = db.execute_query(f"SELECT file_size FROM order_files WHERE id IN ({format_strings})", tuple(parsed_ids))
  73. file_sizes = [r['file_size'] for r in file_rows]
  74. estimated_price, item_prices = pricing.calculate_estimated_price(material_id, file_sizes, parsed_quantities if parsed_quantities else None, return_details=True)
  75. # Snapshoting initial parameters
  76. original_params = json.dumps({
  77. "material_name": mat_name,
  78. "material_price": float(mat_price) if mat_price is not None else 0.0,
  79. "estimated_price": float(estimated_price) if estimated_price is not None else 0.0,
  80. "quantity": quantity,
  81. "color_name": color_name,
  82. "notes": notes,
  83. "first_name": first_name,
  84. "last_name": last_name,
  85. "phone": phone,
  86. "email": email,
  87. "shipping_address": shipping_address,
  88. "model_link": model_link,
  89. "is_company": is_company,
  90. "company_name": company_name,
  91. "company_pib": company_pib,
  92. "company_address": company_address
  93. })
  94. order_query = """
  95. INSERT INTO orders (user_id, material_id, first_name, last_name, phone, email, shipping_address, model_link, status, is_company, company_name, company_pib, company_address, allow_portfolio, estimated_price, material_name, material_price, color_name, quantity, notes, original_params)
  96. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, 'pending', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  97. """
  98. order_params = (user_id, material_id, first_name, last_name, phone, email, shipping_address, model_link, is_company, company_name, company_pib, company_address, allow_portfolio, estimated_price, mat_name, mat_price, color_name, quantity, notes, original_params)
  99. try:
  100. order_insert_id = db.execute_commit(order_query, order_params)
  101. if parsed_ids:
  102. for idx, f_id in enumerate(parsed_ids):
  103. qty = parsed_quantities[idx] if idx < len(parsed_quantities) else 1
  104. unit_p = item_prices[idx] if idx < len(item_prices) else 0.0
  105. db.execute_commit("UPDATE order_files SET order_id = %s, quantity = %s, unit_price = %s WHERE id = %s", (order_insert_id, qty, unit_p, f_id))
  106. # Create corresponding fixed order item
  107. file_row = db.execute_query("SELECT filename FROM order_files WHERE id = %s", (f_id,))
  108. fname = file_row[0]['filename'] if file_row else f"File #{f_id}"
  109. db.execute_commit(
  110. "INSERT INTO order_items (order_id, description, quantity, unit_price, total_price) VALUES (%s, %s, %s, %s, %s)",
  111. (order_insert_id, f"3D Print: {fname}", qty, unit_p, round(qty * unit_p, 2))
  112. )
  113. background_tasks.add_task(order_processing.process_order_slicing, order_insert_id)
  114. background_tasks.add_task(event_hooks.on_order_created, order_insert_id)
  115. # Record placement for rate limiting
  116. rate_limit_service.record_order_placement(email_addr, ip)
  117. return {"status": "success", "order_id": order_insert_id, "message": "Order submitted successfully"}
  118. except Exception as e:
  119. print(f"Error creating order: {e}")
  120. raise HTTPException(status_code=500, detail="Internal server error occurred while processing order")
  121. @router.get("/my")
  122. async def get_my_orders(
  123. page: int = 1,
  124. size: int = 10,
  125. user: dict = Depends(get_current_user)
  126. ):
  127. user_id = user.get("id")
  128. offset = (page - 1) * size
  129. # Get total count
  130. count_query = "SELECT COUNT(*) as total FROM orders WHERE user_id = %s"
  131. count_res = db.execute_query(count_query, (user_id,))
  132. total = count_res[0]['total'] if count_res else 0
  133. query = """
  134. SELECT o.*,
  135. (SELECT count(*) FROM order_messages om WHERE om.order_id = o.id AND om.is_from_admin = TRUE AND om.is_read = FALSE) as unread_count,
  136. GROUP_CONCAT(IF(f.id IS NOT NULL, JSON_OBJECT('id', f.id, 'filename', f.filename, 'file_path', f.file_path, 'quantity', f.quantity, 'preview_path', f.preview_path, 'print_time', f.print_time, 'filament_g', f.filament_g), NULL)) as files
  137. FROM orders o
  138. LEFT JOIN order_files f ON o.id = f.order_id
  139. WHERE o.user_id = %s
  140. GROUP BY o.id
  141. ORDER BY
  142. CASE
  143. WHEN status IN ('pending', 'processing', 'shipped') THEN 0
  144. ELSE 1
  145. END ASC,
  146. o.created_at DESC
  147. LIMIT %s OFFSET %s
  148. """
  149. results = db.execute_query(query, (user_id, size, offset))
  150. for row in results:
  151. if row['files']:
  152. try: row['files'] = json.loads(f"[{row['files']}]")
  153. except: row['files'] = []
  154. else: row['files'] = []
  155. row['items'] = db.execute_query("SELECT * FROM order_items WHERE order_id = %s", (row['id'],))
  156. return {"orders": results, "total": total}
  157. @router.post("/estimate")
  158. async def get_price_estimate(data: schemas.EstimateRequest):
  159. material = db.execute_query("SELECT price_per_cm3 FROM materials WHERE id = %s", (data.material_id,))
  160. price_per_cm3 = float(material[0]['price_per_cm3']) if material else 0.0
  161. file_prices = []
  162. base_fee = 5.0
  163. for size in data.file_sizes:
  164. total_size_mb = size / (1024 * 1024)
  165. estimated_volume = total_size_mb * 8.0
  166. file_cost = base_fee + (estimated_volume * price_per_cm3)
  167. file_prices.append(round(file_cost, 2))
  168. qts = data.file_quantities if data.file_quantities else [1]*len(data.file_sizes)
  169. return {"file_prices": file_prices, "total_estimate": round(sum(p * q for p, q in zip(file_prices, qts)), 2)}
  170. # --- ADMIN ORDER ENDPOINTS ---
  171. @router.get("/admin/list") # Using /admin/list to avoid conflict with /my
  172. async def get_admin_orders(
  173. search: Optional[str] = None,
  174. status: Optional[str] = None,
  175. date_from: Optional[str] = None,
  176. date_to: Optional[str] = None,
  177. admin: dict = Depends(require_admin)
  178. ):
  179. where_clauses = []
  180. params = []
  181. if search:
  182. search_term = f"%{search}%"
  183. where_clauses.append("(o.id LIKE %s OR o.email LIKE %s OR o.first_name LIKE %s OR o.last_name LIKE %s OR o.company_name LIKE %s OR o.phone LIKE %s OR o.shipping_address LIKE %s)")
  184. params.extend([search_term] * 7)
  185. if status and status != 'all':
  186. where_clauses.append("o.status = %s")
  187. params.append(status)
  188. if date_from:
  189. where_clauses.append("o.created_at >= %s")
  190. params.append(date_from)
  191. if date_to:
  192. where_clauses.append("o.created_at <= %s")
  193. params.append(date_to)
  194. where_sql = f"WHERE {' AND '.join(where_clauses)}" if where_clauses else ""
  195. query = f"""
  196. SELECT o.*, u.can_chat,
  197. (SELECT count(*) FROM order_messages om WHERE om.order_id = o.id AND om.is_from_admin = FALSE AND om.is_read = FALSE) as unread_count,
  198. GROUP_CONCAT(IF(f.id IS NOT NULL, JSON_OBJECT('id', f.id, 'filename', f.filename, 'file_path', f.file_path, 'file_size', f.file_size, 'quantity', f.quantity, 'preview_path', f.preview_path, 'print_time', f.print_time, 'filament_g', f.filament_g), NULL)) as files
  199. FROM orders o
  200. LEFT JOIN users u ON o.user_id = u.id
  201. LEFT JOIN order_files f ON o.id = f.order_id
  202. {where_sql}
  203. GROUP BY o.id
  204. ORDER BY o.created_at DESC
  205. """
  206. results = db.execute_query(query, tuple(params))
  207. import session_utils
  208. for row in results:
  209. row['is_online'] = session_utils.is_user_online(row['user_id']) if row.get('user_id') else False
  210. if row['files']:
  211. try: row['files'] = json.loads(f"[{row['files']}]")
  212. except: row['files'] = []
  213. else: row['files'] = []
  214. row['items'] = db.execute_query("SELECT * FROM order_items WHERE order_id = %s", (row['id'],))
  215. photos = db.execute_query("SELECT id, file_path, is_public FROM order_photos WHERE order_id = %s", (row['id'],))
  216. row['photos'] = photos
  217. return results
  218. @router.patch("/{order_id}")
  219. async def update_order(
  220. request: Request,
  221. order_id: int,
  222. data: schemas.AdminOrderUpdate,
  223. background_tasks: BackgroundTasks,
  224. admin: dict = Depends(require_admin)
  225. ):
  226. order_info = db.execute_query("SELECT * FROM orders WHERE id = %s", (order_id,))
  227. if not order_info: raise HTTPException(status_code=404, detail="Order not found")
  228. update_fields = []
  229. params = []
  230. if data.status:
  231. if order_info:
  232. background_tasks.add_task(
  233. event_hooks.on_order_status_changed,
  234. order_id,
  235. data.status,
  236. order_info[0],
  237. data.send_notification
  238. )
  239. # Generate Payment Documents based on status transitions
  240. from services.invoice_service import InvoiceService
  241. o = order_info[0]
  242. price_val = data.total_price if data.total_price is not None else (o.get('total_price') or o.get('estimated_price') or 0)
  243. price = float(price_val)
  244. # 1. Proforma / Payment Slip (on 'processing' or any initial active state)
  245. if data.status in ['processing', 'shipped']:
  246. try:
  247. if o.get('is_company'):
  248. pdf_path = InvoiceService.generate_document(o, doc_type="predracun", override_price=price)
  249. else:
  250. payer_name = f"{o['first_name']} {o.get('last_name', '')}".strip()
  251. pdf_path = InvoiceService.generate_uplatnica(order_id, payer_name, o.get('shipping_address', ''), price)
  252. update_fields.append("proforma_path = %s")
  253. params.append(pdf_path)
  254. except Exception as e:
  255. print(f"Failed to generate proforma: {e}")
  256. # 2. Final Invoice (only on 'shipped')
  257. if data.status == 'shipped':
  258. try:
  259. pdf_path = InvoiceService.generate_document(o, doc_type="faktura", override_price=price)
  260. update_fields.append("invoice_path = %s")
  261. params.append(pdf_path)
  262. except Exception as e:
  263. print(f"Failed to generate final invoice: {e}")
  264. if data.status:
  265. update_fields.append("status = %s")
  266. params.append(data.status)
  267. if data.total_price is not None:
  268. update_fields.append("total_price = %s")
  269. params.append(data.total_price)
  270. if data.material_id is not None:
  271. update_fields.append("material_id = %s")
  272. params.append(data.material_id)
  273. # Also update snapshot names and prices from handbook
  274. mat_info = db.execute_query("SELECT name_en, price_per_cm3 FROM materials WHERE id = %s", (data.material_id,))
  275. if mat_info:
  276. update_fields.append("material_name = %s")
  277. params.append(mat_info[0]['name_en'])
  278. update_fields.append("material_price = %s")
  279. params.append(mat_info[0]['price_per_cm3'])
  280. elif data.material_name is not None:
  281. update_fields.append("material_name = %s")
  282. params.append(data.material_name)
  283. if data.color_name is not None:
  284. update_fields.append("color_name = %s")
  285. params.append(data.color_name)
  286. if data.quantity is not None:
  287. update_fields.append("quantity = %s")
  288. params.append(data.quantity)
  289. if data.fiscal_qr_url is not None:
  290. update_fields.append("fiscal_qr_url = %s")
  291. params.append(data.fiscal_qr_url)
  292. # Auto-set fiscalized_at if adding URL for the first time
  293. if order_info[0].get('fiscalized_at') is None:
  294. update_fields.append("fiscalized_at = %s")
  295. params.append(datetime.now())
  296. if data.ikof is not None:
  297. update_fields.append("ikof = %s")
  298. params.append(data.ikof)
  299. if data.jikr is not None:
  300. update_fields.append("jikr = %s")
  301. params.append(data.jikr)
  302. if update_fields:
  303. query = f"UPDATE orders SET {', '.join(update_fields)} WHERE id = %s"
  304. params.append(order_id)
  305. db.execute_commit(query, tuple(params))
  306. # LOG ACTION
  307. await audit_service.log(
  308. user_id=admin.get("id"),
  309. action="update_order",
  310. target_type="order",
  311. target_id=order_id,
  312. details={"updated_fields": {k.split(" = ")[0]: v for k, v in zip(update_fields, params)}},
  313. request=request
  314. )
  315. # NOTIFY USER VIA WEBSOCKET
  316. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  317. return {"id": order_id, "status": "updated"}
  318. @router.post("/{order_id}/attach-file")
  319. async def admin_attach_file(
  320. order_id: int,
  321. file: UploadFile = File(...),
  322. admin: dict = Depends(require_admin)
  323. ):
  324. unique_filename = f"{uuid.uuid4()}{os.path.splitext(file.filename)[1]}"
  325. file_path = os.path.join(config.UPLOAD_DIR, unique_filename)
  326. db_file_path = f"uploads/{unique_filename}"
  327. sha256_hash = hashlib.sha256()
  328. with open(file_path, "wb") as buffer:
  329. while chunk := file.file.read(8192):
  330. sha256_hash.update(chunk)
  331. buffer.write(chunk)
  332. preview_path = None
  333. db_preview_path = None
  334. if file_path.lower().endswith(".stl"):
  335. preview_filename = f"{uuid.uuid4()}.png"
  336. preview_path = os.path.join(config.PREVIEW_DIR, preview_filename)
  337. db_preview_path = f"uploads/previews/{preview_filename}"
  338. preview_utils.generate_stl_preview(file_path, preview_path)
  339. filament_g = None
  340. print_time = None
  341. if file_path.lower().endswith(".stl"):
  342. result = slicer_utils.slice_model(file_path)
  343. if result and result.get('success'):
  344. filament_g = result.get('filament_g')
  345. print_time = result.get('print_time_str')
  346. query = "INSERT INTO order_files (order_id, filename, file_path, file_size, quantity, file_hash, print_time, filament_g, preview_path) VALUES (%s, %s, %s, %s, 1, %s, %s, %s, %s)"
  347. f_id = db.execute_commit(query, (order_id, file.filename, db_file_path, file.size, sha256_hash.hexdigest(), print_time, filament_g, db_preview_path))
  348. # NOTIFY USER VIA WEBSOCKET
  349. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  350. if order_info:
  351. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  352. return {"file_id": f_id, "filename": file.filename, "preview_path": db_preview_path, "filament_g": filament_g, "print_time": print_time}
  353. @router.delete("/{order_id}/files/{file_id}")
  354. async def admin_delete_file(
  355. order_id: int,
  356. file_id: int,
  357. request: Request,
  358. admin: dict = Depends(require_admin)
  359. ):
  360. file_record = db.execute_query("SELECT file_path, preview_path FROM order_files WHERE id = %s AND order_id = %s", (file_id, order_id))
  361. if not file_record:
  362. raise HTTPException(status_code=404, detail="File not found")
  363. base_dir = config.BASE_DIR
  364. try:
  365. if file_record[0]['file_path']:
  366. os.remove(os.path.join(base_dir, file_record[0]['file_path']))
  367. if file_record[0]['preview_path']:
  368. os.remove(os.path.join(base_dir, file_record[0]['preview_path']))
  369. except Exception as e:
  370. print(f"Error removing file from disk: {e}")
  371. db.execute_commit("DELETE FROM order_files WHERE id = %s AND order_id = %s", (file_id, order_id))
  372. # LOG ACTION
  373. await audit_service.log(
  374. user_id=admin.get("id"),
  375. action="delete_order_file",
  376. target_type="order",
  377. target_id=order_id,
  378. details={"file_id": file_id},
  379. request=request
  380. )
  381. # NOTIFY USER VIA WEBSOCKET
  382. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  383. if order_info:
  384. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  385. return {"status": "success"}
  386. class OrderItemSchema(BaseModel):
  387. description: str
  388. quantity: int
  389. unit_price: float
  390. @router.get("/{order_id}/items")
  391. async def get_order_items(order_id: int):
  392. items = db.execute_query("SELECT * FROM order_items WHERE order_id = %s", (order_id,))
  393. return items
  394. @router.put("/{order_id}/items")
  395. async def update_order_items(order_id: int, items: List[OrderItemSchema], admin: dict = Depends(require_admin)):
  396. db.execute_commit("DELETE FROM order_items WHERE order_id = %s", (order_id,))
  397. total_order_price = 0
  398. for item in items:
  399. tot_p = round(item.quantity * item.unit_price, 2)
  400. total_order_price += tot_p
  401. db.execute_commit(
  402. "INSERT INTO order_items (order_id, description, quantity, unit_price, total_price) VALUES (%s, %s, %s, %s, %s)",
  403. (order_id, item.description, item.quantity, item.unit_price, tot_p)
  404. )
  405. # Sync main order total_price
  406. db.execute_commit("UPDATE orders SET total_price = %s WHERE id = %s", (total_order_price, order_id))
  407. # NOTIFY USER VIA WEBSOCKET
  408. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  409. if order_info:
  410. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  411. return {"status": "success", "total_price": total_order_price}
  412. @router.delete("/{order_id}/admin")
  413. async def delete_order_admin(
  414. order_id: int,
  415. request: Request,
  416. admin: dict = Depends(require_admin)
  417. ):
  418. # Fetch user_id before deletion to notify later
  419. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  420. if not order_info: raise HTTPException(status_code=404, detail="Order not found")
  421. customer_id = order_info[0]['user_id']
  422. # 1. Find all related files to delete from disk
  423. files = db.execute_query("SELECT file_path, preview_path FROM order_files WHERE order_id = %s", (order_id,))
  424. photos = db.execute_query("SELECT file_path FROM order_photos WHERE order_id = %s", (order_id,))
  425. base_dir = config.BASE_DIR
  426. # Delete order_files from disk
  427. for f in files:
  428. try:
  429. if f.get('file_path'):
  430. fpath = os.path.join(base_dir, f['file_path'])
  431. if os.path.exists(fpath): os.remove(fpath)
  432. if f.get('preview_path'):
  433. ppath = os.path.join(base_dir, f['preview_path'])
  434. if os.path.exists(ppath): os.remove(ppath)
  435. except Exception as e:
  436. print(f"Error deleting order file {f.get('file_path')}: {e}")
  437. # Delete photos from disk
  438. for p in photos:
  439. try:
  440. if p.get('file_path'):
  441. fpath = os.path.join(base_dir, p['file_path'])
  442. if os.path.exists(fpath): os.remove(fpath)
  443. except Exception as e:
  444. print(f"Error deleting order photo {p.get('file_path')}: {e}")
  445. # 2. Delete from DB tables (due to possible lack of CASCADE or to be explicit)
  446. try:
  447. db.execute_commit("DELETE FROM order_messages WHERE order_id = %s", (order_id,))
  448. db.execute_commit("DELETE FROM order_items WHERE order_id = %s", (order_id,))
  449. db.execute_commit("DELETE FROM order_files WHERE order_id = %s", (order_id,))
  450. db.execute_commit("DELETE FROM order_photos WHERE order_id = %s", (order_id,))
  451. db.execute_commit("DELETE FROM orders WHERE id = %s", (order_id,))
  452. # LOG ACTION
  453. await audit_service.log(
  454. user_id=admin.get("id"),
  455. action="delete_order_entirely",
  456. target_type="order",
  457. target_id=order_id,
  458. details={"order_id": order_id},
  459. request=request
  460. )
  461. # NOTIFY USER VIA WEBSOCKET
  462. await global_manager.notify_order_update(customer_id, order_id)
  463. return {"status": "success", "message": f"Order {order_id} deleted entirely"}
  464. except Exception as e:
  465. print(f"Failed to delete order {order_id}: {e}")
  466. raise HTTPException(status_code=500, detail=str(e))