pricing.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from typing import List
  2. import db
  3. import config
  4. def calculate_estimated_price(material_id: int, file_sizes: List[int], file_quantities: List[int] = None, return_details=False) -> float:
  5. """
  6. Internal logic to estimate price per file based on file size (proxy for volume)
  7. and print time (proxy for machine wear/energy).
  8. """
  9. material = db.execute_query("""
  10. SELECT m.price_per_cm3,
  11. COALESCE(m.price_per_hour, s.price_per_hour, %s) as price_per_hour,
  12. m.speed_coeff
  13. FROM materials m
  14. LEFT JOIN services s ON m.service_id = s.id
  15. WHERE m.id = %s
  16. """, (config.DEFAULT_PRINTER_HOUR_PRICE, material_id))
  17. if not material:
  18. return (0.0, []) if return_details else 0.0
  19. price_per_cm3 = float(material[0]['price_per_cm3'])
  20. price_per_hour = float(material[0]['price_per_hour'])
  21. speed_coeff = float(material[0]['speed_coeff'] or 1.0)
  22. if file_quantities is None or len(file_quantities) != len(file_sizes):
  23. file_quantities = [1] * len(file_sizes)
  24. estimated_total = 0.0
  25. file_costs = []
  26. base_fee = 5.0 # Minimum setup fee per file
  27. for size, qty in zip(file_sizes, file_quantities):
  28. total_size_mb = size / (1024 * 1024)
  29. # Empirical conversion: ~8cm3 per 1MB of STL (binary)
  30. estimated_volume_cm3 = total_size_mb * 8.0
  31. # Estimate printing time (same logic as in slicer_utils.py: volume_mm3 / 10.0)
  32. # Apply speed_coeff to adjust time based on material difficulty
  33. estimated_hours = ((estimated_volume_cm3 * 100.0) / 3600.0) * speed_coeff
  34. material_cost = estimated_volume_cm3 * price_per_cm3
  35. operation_cost = estimated_hours * price_per_hour
  36. file_cost = round(base_fee + material_cost + operation_cost, 2)
  37. file_costs.append(file_cost)
  38. estimated_total += file_cost * qty
  39. if return_details:
  40. return round(estimated_total, 2), file_costs
  41. return round(estimated_total, 2)