35 lines
1.6 KiB
Python
35 lines
1.6 KiB
Python
"""Compare calculable CNY costs on the same destination and billing basis."""
|
|
|
|
from decimal import Decimal,ROUND_HALF_UP
|
|
|
|
from .quote_fees import charge_details
|
|
|
|
|
|
def speed_difference_lines(intent, book, rates, fees):
|
|
from .quote_engine import quantity, number, select_timed_rates
|
|
|
|
groups = {}
|
|
for rate in rates:
|
|
key = (rate['origin'], rate['zone'], rate['kind'], rate['unit'], rate['tax'])
|
|
groups.setdefault(key, []).append(rate)
|
|
lines = []
|
|
for (origin, zone, kind, unit, tax), candidates in groups.items():
|
|
costs = []
|
|
for selected in (select_timed_rates(candidates), select_timed_rates(candidates, slowest=True)):
|
|
values = []
|
|
for rate in selected:
|
|
amount = quantity(intent, rate)
|
|
if amount is not None:
|
|
total=amount * Decimal(rate['price']) + charge_details(intent, rate, fees, book)['cny']
|
|
values.append(total.quantize(Decimal('.01'),rounding=ROUND_HALF_UP))
|
|
costs.append(values)
|
|
fast, slow = costs
|
|
if not fast or not slow:
|
|
continue
|
|
low, high = min(fast) - max(slow), max(fast) - min(slow)
|
|
label = ' · '.join(value for value in (origin, zone, '包税' if tax == 'included' else '不包税',
|
|
'海派' if kind == 'parcel' else '海卡/海铁') if value)
|
|
difference = number(low) if low == high else number(low) + '至' + number(high)
|
|
lines.append(f'- {label},快慢方案可计算运费差额为{difference}元。')
|
|
return lines
|