forked from erp-dev/erp
fea: business completed
This commit is contained in:
@@ -127,6 +127,10 @@ class HaoBuYeFinanceClient:
|
||||
payload = response.json()
|
||||
except ValueError:
|
||||
payload = {}
|
||||
# 404 且返回了 status=not_found 的 JSON 视为正常业务响应(如客户无退货单),
|
||||
# 由调用方根据 status 字段决定后续逻辑,而不是在此处抛异常中断整个同步。
|
||||
if response.status_code == 404 and isinstance(payload, dict) and payload.get('status') == 'not_found':
|
||||
return payload
|
||||
message = payload.get('message') or payload.get('error') or f'外部接口异常状态码: {response.status_code}'
|
||||
raise ExternalFinanceSyncError(str(message))
|
||||
try:
|
||||
@@ -775,12 +779,18 @@ def _apply_sale_discount_zk(
|
||||
progress_callback: Callable[[str], None] | None = None,
|
||||
) -> int:
|
||||
"""
|
||||
从 sale_discount 数据(F_Skd 的 XS% 行)中提取 ZkJinE,
|
||||
更新到对应的 ExternalCustomerStatementOrder.zk_amount。
|
||||
从 sale_discount 数据(F_Skd 的 XS% 行)中提取 ZkJinE 和 YfJinE,
|
||||
更新到对应的 ExternalCustomerStatementOrder.zk_amount 和 total_amount。
|
||||
|
||||
关键修正:I_Sale.SfJinE 始终为 0(数据源缺陷),导致从 I_Sale.JinE 计算的
|
||||
total_amount 未扣除实付部分。F_Skd XS% 的 YfJinE 已经扣除了实付,是正确的
|
||||
欠款基数,因此用它覆盖 total_amount。
|
||||
|
||||
返回更新的记录数。
|
||||
"""
|
||||
# 按 BianHaoID 聚合折扣(一个 BianHaoID 可能有多条 F_Skd 记录)
|
||||
# 按 BianHaoID 聚合折扣和应付金额
|
||||
zk_by_source: dict[str, Decimal] = {}
|
||||
yf_by_source: dict[str, Decimal] = {}
|
||||
for record in sale_discounts:
|
||||
if not isinstance(record, dict):
|
||||
continue
|
||||
@@ -788,13 +798,31 @@ def _apply_sale_discount_zk(
|
||||
if not source_id:
|
||||
continue
|
||||
zk_value = abs(_to_decimal(record.get('ZkJinE'), field_name='ZkJinE', default=Decimal('0')))
|
||||
# YfJinE 不取 abs():负值代表退款/冲减,应保留原始正负
|
||||
yf_value = _to_decimal(record.get('YfJinE'), field_name='YfJinE', default=Decimal('0'))
|
||||
if zk_value:
|
||||
zk_by_source[source_id] = zk_by_source.get(source_id, Decimal('0')) + zk_value
|
||||
# YfJinE 是每个销售单的正确欠款金额(已扣除实付 SfJinE)
|
||||
# 一个 BianHaoID 通常只有一条 F_Skd XS% 记录,但为安全起见做聚合
|
||||
# 注意:不过滤零值,因为 YfJinE=0 也是有效数据(表示全额实付)
|
||||
yf_by_source[source_id] = yf_by_source.get(source_id, Decimal('0')) + yf_value
|
||||
|
||||
if not zk_by_source:
|
||||
if not zk_by_source and not yf_by_source:
|
||||
return 0
|
||||
|
||||
updated_count = 0
|
||||
|
||||
# 更新 total_amount:用 F_Skd XS%.YfJinE 覆盖从 I_Sale.JinE 计算的值
|
||||
for source_id, yf_total in yf_by_source.items():
|
||||
updated = business_models.ExternalCustomerStatementOrder.objects.filter(
|
||||
merchant=merchant,
|
||||
customer=customer,
|
||||
category=business_models.ExternalCustomerStatementCategoryEnum.SALE,
|
||||
external_source_id=source_id,
|
||||
).exclude(total_amount=yf_total).update(total_amount=yf_total)
|
||||
updated_count += updated
|
||||
|
||||
# 更新 zk_amount
|
||||
for source_id, zk_total in zk_by_source.items():
|
||||
updated = business_models.ExternalCustomerStatementOrder.objects.filter(
|
||||
merchant=merchant,
|
||||
@@ -807,7 +835,7 @@ def _apply_sale_discount_zk(
|
||||
if updated_count:
|
||||
_emit_progress(
|
||||
progress_callback,
|
||||
f'销售折扣更新: {updated_count} 条记录的 zk_amount 已从 sale_discount 数据更新',
|
||||
f'销售折扣/应付更新: {updated_count} 条记录已从 sale_discount 数据更新 (total_amount via YfJinE, zk_amount via ZkJinE)',
|
||||
)
|
||||
return updated_count
|
||||
|
||||
|
||||
Reference in New Issue
Block a user