63 lines
3.6 KiB
Python
63 lines
3.6 KiB
Python
"""Apply quote edits in chronological order, including derived carton quantities."""
|
|
|
|
from copy import deepcopy
|
|
from decimal import Decimal
|
|
|
|
|
|
QUANTITY_QUESTION='箱数已更改,请确认新的总重或各箱重量,原分箱明细不能直接套用。'
|
|
|
|
|
|
def merge_intent(current,previous):
|
|
result=deepcopy(current)
|
|
excluded={'mode','text','errors','dim_unit','quantity_note','quantity_uncertain','preserve_total','metal_weight_update'}
|
|
if current.get('metal_weight_update') and previous.get('metal_chargeable_weight') is None:
|
|
result['metal_chargeable_weight']=None
|
|
result['errors'].append('请确认铁铝部分的计费重,不能把毛重直接当作计费重。')
|
|
if current.get('warehouse') or current.get('unknown_warehouses'):
|
|
excluded.update(('postal','zone','zones'))
|
|
elif current.get('postal'):
|
|
excluded.update(('warehouse','warehouses','unknown_warehouses','zone','zones'))
|
|
elif current.get('zone'):
|
|
excluded.update(('warehouse','warehouses','unknown_warehouses','postal'))
|
|
if any(current.get(k) for k in ('warehouse','postal','zone')) and not current.get('unknown_warehouses'):
|
|
excluded.add('unknown_warehouses')
|
|
if any(current.get(k) for k in ('warehouse','unknown_warehouses','postal','zone')) and not current.get('allocations'):
|
|
excluded.add('allocations')
|
|
if current.get('allocations'):excluded.update(('packages','boxes','dims','cbm'))
|
|
if current['transport'] or current['kinds'] or current['services']:excluded.update(('air_only','air_requested'))
|
|
if current['air_only'] or current['tax']=='excluded':excluded.add('kinds')
|
|
if current['weight'] is not None:
|
|
if not current.get('packages'):excluded.add('packages')
|
|
if not current.get('allocations'):excluded.add('allocations')
|
|
if current['dims']:excluded.add('cbm')
|
|
if current['cbm'] is not None:excluded.add('dims')
|
|
if any(current.get(k) is not None for k in ('weight','dims','cbm','boxes','packages','allocations','transport','kinds','services','tax')) and current.get('metal_chargeable_weight') is None:
|
|
excluded.add('metal_chargeable_weight')
|
|
if current.get('material')=='ordinary':excluded.update(('metal_scope','metal_chargeable_weight'))
|
|
if current.get('metal_scope')=='all' and current.get('metal_chargeable_weight') is None:
|
|
excluded.add('metal_chargeable_weight')
|
|
for key,value in previous.items():
|
|
if result.get(key) is None and key not in excluded:result[key]=deepcopy(value)
|
|
if current['dims'] is None and 'dims' not in excluded and previous['dims']:
|
|
result['dim_unit']=previous['dim_unit']
|
|
pieces=result.get('packages') or []
|
|
if current['dims']:
|
|
for piece in pieces:piece.update(dims=current['dims'],dim_unit=current['dim_unit'])
|
|
elif current['cbm'] is not None:
|
|
for piece in pieces:piece.update(dims=None,dim_unit='cm')
|
|
if previous.get('quantity_uncertain') and current['weight'] is None and not current.get('preserve_total'):
|
|
result['quantity_uncertain']=True
|
|
result['errors'].append(QUANTITY_QUESTION)
|
|
if pieces and result['boxes']!=sum(p['count'] for p in pieces):
|
|
if current.get('preserve_total'):
|
|
result['packages']=None
|
|
elif len(pieces)==1:
|
|
pieces[0]['count']=result['boxes']
|
|
result['weight']=str(Decimal(pieces[0]['weight'])*result['boxes'])
|
|
result['quantity_note']=f'按每箱{pieces[0]["weight"]}KG不变,更新为{result["boxes"]}箱。'
|
|
else:
|
|
result['packages']=None
|
|
result['quantity_uncertain']=True
|
|
result['errors'].append(QUANTITY_QUESTION)
|
|
return result
|