spoolCost.ts 875 B

123456789101112131415161718
  1. import type { InventorySpool } from '../api/client';
  2. /**
  3. * The cost per kg to show for a spool: the rate prints off it are charged
  4. * (SM-C8). A Spoolman spool's own price wins when it is above 0; otherwise
  5. * prints pay its filament's catalogue price (``effective_cost_per_kg``), as
  6. * the charge treats a missing or 0 spool price as "not set". Without one, the
  7. * spool's own value stands, so a built-in spool priced 0 still shows 0.
  8. */
  9. export function shownCostPerKg(
  10. spool: Pick<InventorySpool, 'cost_per_kg' | 'effective_cost_per_kg'>,
  11. ): { rate: number | null; fromFilament: boolean } {
  12. const own = spool.cost_per_kg;
  13. if (own != null && own > 0) return { rate: own, fromFilament: false };
  14. const charged = spool.effective_cost_per_kg;
  15. if (charged != null) return { rate: charged, fromFilament: true };
  16. return { rate: own ?? null, fromFilament: false };
  17. }