pricing.py 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. from typing import List
  2. import db
  3. def calculate_estimated_price(material_id: int, file_sizes: List[int], file_quantities: List[int] = None, return_details=False) -> float:
  4. """
  5. Internal logic to estimate price per file based on file size (proxy for volume).
  6. """
  7. material = db.execute_query("SELECT price_per_cm3 FROM materials WHERE id = %s", (material_id,))
  8. if not material:
  9. return (0.0, []) if return_details else 0.0
  10. price_per_cm3 = float(material[0]['price_per_cm3'])
  11. if file_quantities is None or len(file_quantities) != len(file_sizes):
  12. file_quantities = [1] * len(file_sizes)
  13. estimated_total = 0.0
  14. file_costs = []
  15. base_fee = 5.0 # Minimum setup fee per file
  16. for size, qty in zip(file_sizes, file_quantities):
  17. total_size_mb = size / (1024 * 1024)
  18. # Empirical conversion: ~8cm3 per 1MB of STL (binary)
  19. estimated_volume = total_size_mb * 8.0
  20. file_cost = round(base_fee + (estimated_volume * price_per_cm3), 2)
  21. file_costs.append(file_cost)
  22. estimated_total += file_cost * qty
  23. if return_details:
  24. return round(estimated_total, 2), file_costs
  25. return round(estimated_total, 2)