"""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'} 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') 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