1
0
forked from erp-dev/erp

fix: added some fields to business.statements api

This commit is contained in:
2025-12-12 17:06:17 +08:00
parent 9e1f994416
commit 4c9b8e6a42
8 changed files with 351 additions and 5 deletions

View File

@@ -1869,6 +1869,7 @@ class _StatementBuilder:
recorded_at,
status: int,
status_label: str,
warehouse: basic_info_models.WareHouse | None = None,
positive_amount,
negative_amount,
items: List[dict] | None = None,
@@ -1885,6 +1886,8 @@ class _StatementBuilder:
'recorded_at': recorded_at,
'status': status,
'status_label': status_label,
# 注意:收款单/付款单等资金类单据无仓库概念warehouse 将为 None
'warehouse': warehouse,
'positive_amount': _normalize_statement_amount(positive_amount),
'negative_amount': _normalize_statement_amount(negative_amount),
'items': items,
@@ -1901,7 +1904,11 @@ class _StatementBuilder:
product_name = getattr(product, 'name', '')
unit = getattr(item, 'unit', '')
price = getattr(item, 'price', Decimal('0'))
key = (product_id, product_name, unit, price)
# 这些字段均来自订单明细(*OrderItem用于在对账单中对 items 进行合理聚合:
# - 同一产品不同颜色/规格应拆分展示,避免合并后信息丢失
color = getattr(item, 'color', None) or ''
spec = getattr(item, 'spec', None) or ''
key = (product_id, product_name, unit, price, color, spec)
if key not in aggregated:
aggregated[key] = {
'product_id': product_id,
@@ -1909,9 +1916,36 @@ class _StatementBuilder:
'quantity': Decimal('0'),
'price': price,
'unit': unit,
'color': color,
'spec': spec,
# 统一输出各条数数量(严格模式下为每条数量列表;非严格模式/未填则为空列表)
# 来源于各 *OrderItem 模型新增的 split_quantity_of_rolls() 方法
'quantity_of_rolls': [],
# 条数:来自各 *OrderItem.num_of_rolls。聚合时对相同产品/单价的多行明细累加。
'num_of_rolls': 0,
}
quantity_value = getattr(item, 'quantity', 0) or 0
aggregated[key]['quantity'] += Decimal(str(quantity_value))
num_of_rolls_value = getattr(item, 'num_of_rolls', None)
try:
num_of_rolls_int = int(num_of_rolls_value) if num_of_rolls_value is not None else 0
except (TypeError, ValueError): # pragma: no cover - defensive
num_of_rolls_int = 0
split_quantity_of_rolls = getattr(item, 'split_quantity_of_rolls', None)
if callable(split_quantity_of_rolls):
try:
rolls = split_quantity_of_rolls() or []
except Exception: # pragma: no cover - defensive
rolls = []
if rolls:
aggregated[key]['quantity_of_rolls'].extend(list(rolls))
# 若无 num_of_rolls或为 0兜底使用 rolls 长度
if not num_of_rolls_int:
num_of_rolls_int = len(rolls)
aggregated[key]['num_of_rolls'] += num_of_rolls_int
return list(aggregated.values())
def _sort_records(self, records: Iterable[dict]) -> List[dict]:
@@ -1951,7 +1985,7 @@ class _CustomerStatementBuilder(_StatementBuilder):
customer=customer,
status=models.SalesOrderStatusEnum.APPROVED,
)
.select_related('customer')
.select_related('customer', 'warehouse')
.prefetch_related('items__product')
)
records = []
@@ -1968,6 +2002,7 @@ class _CustomerStatementBuilder(_StatementBuilder):
recorded_at=order.created_at,
status=order.status,
status_label=order.get_status_display(),
warehouse=order.warehouse,
positive_amount=order.get_total_amount(),
negative_amount=_STATEMENT_ZERO,
items=items,
@@ -1982,7 +2017,7 @@ class _CustomerStatementBuilder(_StatementBuilder):
customer=customer,
status=models.SalesReturnStatusEnum.APPROVED,
)
.select_related('customer')
.select_related('customer', 'warehouse')
.prefetch_related('items__product')
)
records = []
@@ -1999,6 +2034,7 @@ class _CustomerStatementBuilder(_StatementBuilder):
recorded_at=order.created_at,
status=order.status,
status_label=order.get_status_display(),
warehouse=order.warehouse,
positive_amount=_STATEMENT_ZERO,
negative_amount=order.get_total_amount(),
items=items,
@@ -2050,7 +2086,7 @@ class _SupplierStatementBuilder(_StatementBuilder):
supplier=supplier,
status=models.PurchaseOrderStatusEnum.APPROVED,
)
.select_related('supplier')
.select_related('supplier', 'warehouse')
.prefetch_related('items__product')
)
records = []
@@ -2067,6 +2103,7 @@ class _SupplierStatementBuilder(_StatementBuilder):
recorded_at=order.created_at,
status=order.status,
status_label=order.get_status_display(),
warehouse=order.warehouse,
positive_amount=order.get_total_amount(),
negative_amount=_STATEMENT_ZERO,
items=items,
@@ -2081,7 +2118,7 @@ class _SupplierStatementBuilder(_StatementBuilder):
supplier=supplier,
status=models.PurchaseReturnStatusEnum.APPROVED,
)
.select_related('supplier')
.select_related('supplier', 'warehouse')
.prefetch_related('items__product')
)
records = []
@@ -2098,6 +2135,7 @@ class _SupplierStatementBuilder(_StatementBuilder):
recorded_at=order.created_at,
status=order.status,
status_label=order.get_status_display(),
warehouse=order.warehouse,
positive_amount=_STATEMENT_ZERO,
negative_amount=order.get_total_amount(),
items=items,