feat(order): 지오영 주문도 선별 주문 적용
- quick_order로 장바구니 담기 (auto_confirm 제거) - submit_order_selective로 선별 주문 - 기존 품목 보존, 이번에 담은 것만 주문
This commit is contained in:
parent
be95f8b3d1
commit
760aea6f89
@ -287,33 +287,30 @@ def submit_geoyoung_order(order: dict, dry_run: bool, cart_only: bool = True) ->
|
|||||||
|
|
||||||
geo_session = get_geo_session()
|
geo_session = get_geo_session()
|
||||||
|
|
||||||
|
# 1단계: 모든 품목을 장바구니에만 담기 (auto_confirm=False)
|
||||||
for item in items:
|
for item in items:
|
||||||
kd_code = item.get('kd_code') or item.get('drug_code')
|
kd_code = item.get('kd_code') or item.get('drug_code')
|
||||||
order_qty = item['order_qty']
|
order_qty = item['order_qty']
|
||||||
spec = item.get('specification', '')
|
spec = item.get('specification', '')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 지오영 주문 실행
|
# 장바구니에만 담기 (주문 확정은 나중에 한번에)
|
||||||
# cart_only=True: 장바구니만 (auto_confirm=False)
|
result = geo_session.quick_order(
|
||||||
# cart_only=False: 주문 확정까지 (auto_confirm=True)
|
|
||||||
result = geo_session.full_order(
|
|
||||||
kd_code=kd_code,
|
kd_code=kd_code,
|
||||||
quantity=order_qty,
|
quantity=order_qty,
|
||||||
specification=spec if spec else None,
|
specification=spec if spec else None,
|
||||||
check_stock=True,
|
check_stock=True
|
||||||
auto_confirm=not cart_only, # cart_only면 확정 안함
|
|
||||||
memo=f"자동주문 - {item.get('product_name', '')}"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if result.get('success'):
|
if result.get('success'):
|
||||||
status = 'success'
|
status = 'success'
|
||||||
result_code = 'CART_ADDED' if cart_only else 'OK'
|
result_code = 'CART_ADDED'
|
||||||
result_message = '장바구니 추가 완료' if cart_only else result.get('message', '주문 완료')
|
result_message = '장바구니 추가 완료'
|
||||||
success_count += 1
|
success_count += 1
|
||||||
else:
|
else:
|
||||||
status = 'failed'
|
status = 'failed'
|
||||||
result_code = result.get('error', 'UNKNOWN')
|
result_code = result.get('error', 'UNKNOWN')
|
||||||
result_message = result.get('message', '주문 실패')
|
result_message = result.get('message', '장바구니 추가 실패')
|
||||||
failed_count += 1
|
failed_count += 1
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -321,10 +318,14 @@ def submit_geoyoung_order(order: dict, dry_run: bool, cart_only: bool = True) ->
|
|||||||
result_code = 'ERROR'
|
result_code = 'ERROR'
|
||||||
result_message = str(e)
|
result_message = str(e)
|
||||||
failed_count += 1
|
failed_count += 1
|
||||||
|
result = {}
|
||||||
|
|
||||||
update_item_result(item['id'], status, result_code, result_message)
|
update_item_result(item['id'], status, result_code, result_message)
|
||||||
|
|
||||||
# AI 학습용 컨텍스트 저장 (실제 주문)
|
# quick_order 결과에서 product_code 가져오기
|
||||||
|
product_code = result.get('product', {}).get('product_code') if result.get('success') else None
|
||||||
|
|
||||||
|
# AI 학습용 컨텍스트 저장
|
||||||
save_order_context(item['id'], {
|
save_order_context(item['id'], {
|
||||||
'drug_code': item['drug_code'],
|
'drug_code': item['drug_code'],
|
||||||
'product_name': item['product_name'],
|
'product_name': item['product_name'],
|
||||||
@ -332,7 +333,9 @@ def submit_geoyoung_order(order: dict, dry_run: bool, cart_only: bool = True) ->
|
|||||||
'usage_7d': item.get('usage_qty', 0),
|
'usage_7d': item.get('usage_qty', 0),
|
||||||
'ordered_spec': spec,
|
'ordered_spec': spec,
|
||||||
'ordered_qty': order_qty,
|
'ordered_qty': order_qty,
|
||||||
'selection_reason': 'user_order'
|
'selection_reason': 'user_order',
|
||||||
|
'wholesaler_id': 'geoyoung',
|
||||||
|
'product_code': product_code
|
||||||
})
|
})
|
||||||
|
|
||||||
results.append({
|
results.append({
|
||||||
@ -343,32 +346,51 @@ def submit_geoyoung_order(order: dict, dry_run: bool, cart_only: bool = True) ->
|
|||||||
'order_qty': order_qty,
|
'order_qty': order_qty,
|
||||||
'status': status,
|
'status': status,
|
||||||
'result_code': result_code,
|
'result_code': result_code,
|
||||||
'result_message': result_message
|
'result_message': result_message,
|
||||||
|
'product_code': product_code # 선별 주문용
|
||||||
})
|
})
|
||||||
|
|
||||||
# 주문 상태 업데이트
|
# 2단계: cart_only=False면 선별 주문 (기존 품목 보존)
|
||||||
if cart_only:
|
if not cart_only and success_count > 0:
|
||||||
# 장바구니만 담은 경우
|
try:
|
||||||
|
# 이번에 담은 품목의 product_code만 수집
|
||||||
|
ordered_codes = [r['product_code'] for r in results
|
||||||
|
if r['status'] == 'success' and r.get('product_code')]
|
||||||
|
|
||||||
|
if ordered_codes:
|
||||||
|
# 선별 주문: 기존 품목은 건드리지 않고, 이번에 담은 것만 주문
|
||||||
|
confirm_result = geo_session.submit_order_selective(ordered_codes)
|
||||||
|
|
||||||
|
if confirm_result.get('success'):
|
||||||
|
restored_info = f", 기존 {confirm_result.get('restored_count', 0)}개 복원" if confirm_result.get('restored_count', 0) > 0 else ""
|
||||||
|
update_order_status(order_id, 'submitted',
|
||||||
|
f'지오영 주문 확정 완료: {success_count}개{restored_info}')
|
||||||
|
# 결과 메시지 업데이트
|
||||||
|
for r in results:
|
||||||
|
if r['status'] == 'success':
|
||||||
|
r['result_code'] = 'OK'
|
||||||
|
r['result_message'] = '주문 확정 완료'
|
||||||
|
else:
|
||||||
|
update_order_status(order_id, 'partial',
|
||||||
|
f'지오영 장바구니 담김, 확정 실패: {confirm_result.get("error", "알 수 없는 오류")}')
|
||||||
|
else:
|
||||||
|
update_order_status(order_id, 'partial',
|
||||||
|
f'지오영 장바구니 담김, product_code 없음')
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"지오영 주문 확정 오류: {e}")
|
||||||
|
update_order_status(order_id, 'partial',
|
||||||
|
f'지오영 장바구니 담김, 확정 중 오류: {str(e)}')
|
||||||
|
elif success_count > 0:
|
||||||
|
# cart_only=True: 장바구니만
|
||||||
if failed_count == 0:
|
if failed_count == 0:
|
||||||
update_order_status(order_id, 'pending',
|
update_order_status(order_id, 'pending',
|
||||||
f'지오영 장바구니 추가 완료: {success_count}개 (사이트에서 확정 필요)')
|
f'지오영 장바구니 추가 완료: {success_count}개 (사이트에서 확정 필요)')
|
||||||
elif success_count == 0:
|
|
||||||
update_order_status(order_id, 'failed',
|
|
||||||
f'장바구니 추가 실패: {failed_count}개 품목')
|
|
||||||
else:
|
else:
|
||||||
update_order_status(order_id, 'partial',
|
update_order_status(order_id, 'partial',
|
||||||
f'부분 성공: {success_count}개 장바구니, {failed_count}개 실패')
|
f'부분 성공: {success_count}개 장바구니, {failed_count}개 실패')
|
||||||
else:
|
else:
|
||||||
# 실제 주문까지 한 경우
|
update_order_status(order_id, 'failed',
|
||||||
if failed_count == 0:
|
f'지오영 주문 실패: {failed_count}개')
|
||||||
update_order_status(order_id, 'submitted',
|
|
||||||
f'주문 제출 완료: {success_count}개 품목')
|
|
||||||
elif success_count == 0:
|
|
||||||
update_order_status(order_id, 'failed',
|
|
||||||
f'주문 실패: {failed_count}개 품목')
|
|
||||||
else:
|
|
||||||
update_order_status(order_id, 'partial',
|
|
||||||
f'부분 주문: {success_count}개 성공, {failed_count}개 실패')
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'success': True,
|
'success': True,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user