78 lines
4.5 KiB
Python
78 lines
4.5 KiB
Python
"""Apply quote edits in chronological order, including derived carton quantities."""
|
|
|
|
from copy import deepcopy
|
|
from decimal import Decimal
|
|
|
|
from .quote_intent import kg,MISSING_WEIGHT_UNIT
|
|
|
|
|
|
QUANTITY_QUESTION='箱数已更改,请确认新的总重或各箱重量,原分箱明细不能直接套用。'
|
|
|
|
|
|
def merge_intent(current,previous):
|
|
result=deepcopy(current)
|
|
excluded={'mode','selection','clear_selection','text','errors','dim_unit','quantity_note','quantity_uncertain','preserve_total','metal_weight_update','pending_weight','weight_unit'}
|
|
unit=current.get('weight_unit')
|
|
pending=previous.get('pending_weight')
|
|
weight_change=current['weight'] is not None or bool(unit and pending is not None)
|
|
if unit:
|
|
if pending is None:result['errors'].append('请同时提供重量数值和单位,例如800KG。')
|
|
else:result['weight']=str(kg(pending,unit))
|
|
elif pending is not None and current['weight'] is None and current.get('pending_weight') is None:
|
|
result['pending_weight']=pending
|
|
result['errors'].append(MISSING_WEIGHT_UNIT)
|
|
if not current.get('selection') and not current.get('clear_selection'):
|
|
result['selection']=previous.get('selection')
|
|
if current.get('clear_selection'):
|
|
excluded.update(('services','kinds','transport','air_only','air_requested'))
|
|
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 weight_change:
|
|
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 (weight_change or any(current.get(k) is not None for k in ('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 not weight_change 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
|