forked from erp-dev/erp
refactor: views and srzs of business-statement api
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import OrderedDict
|
||||
from decimal import Decimal, ROUND_HALF_UP
|
||||
from typing import Iterable, List
|
||||
|
||||
@@ -67,7 +68,6 @@ class StatementViewBase(StockChangeViewMixin, views.APIView):
|
||||
extra: dict | None = None,
|
||||
) -> dict:
|
||||
items = items or []
|
||||
print(items)
|
||||
record = {
|
||||
'counterparty': counterparty_id,
|
||||
'counterparty_name': counterparty_name,
|
||||
@@ -86,6 +86,27 @@ class StatementViewBase(StockChangeViewMixin, views.APIView):
|
||||
record['extra'] = extra
|
||||
return record
|
||||
|
||||
def _aggregate_items(self, order_items) -> List[dict]:
|
||||
"""Merge duplicate order items without changing the response schema."""
|
||||
|
||||
aggregated = OrderedDict()
|
||||
for item in order_items:
|
||||
product_id = getattr(item, 'product_id', None)
|
||||
product_name = getattr(getattr(item, 'product', None), 'name', '')
|
||||
unit = getattr(item, 'unit', '')
|
||||
price = getattr(item, 'price', Decimal('0'))
|
||||
key = (product_id, product_name, unit, price)
|
||||
if key not in aggregated:
|
||||
aggregated[key] = {
|
||||
'product_id': product_id,
|
||||
'product_name': product_name,
|
||||
'quantity': Decimal('0'),
|
||||
'price': price,
|
||||
'unit': unit,
|
||||
}
|
||||
aggregated[key]['quantity'] += Decimal(getattr(item, 'quantity', 0))
|
||||
return list(aggregated.values())
|
||||
|
||||
def _sort_records(self, records: Iterable[dict]) -> List[dict]:
|
||||
return sorted(
|
||||
records,
|
||||
@@ -168,17 +189,7 @@ class CustomerStatementView(StatementViewBase):
|
||||
)
|
||||
records = []
|
||||
for order in qs:
|
||||
items = [
|
||||
{
|
||||
'product_id': item.product_id,
|
||||
'product_name': item.product.name,
|
||||
'quantity': item.quantity,
|
||||
'price': item.price,
|
||||
'unit': item.unit,
|
||||
}
|
||||
for item in order.items.all()
|
||||
]
|
||||
print(items)
|
||||
items = self._aggregate_items(order.items.all())
|
||||
records.append(
|
||||
self._build_record(
|
||||
counterparty_id=order.customer_id,
|
||||
@@ -209,16 +220,7 @@ class CustomerStatementView(StatementViewBase):
|
||||
)
|
||||
records = []
|
||||
for order in qs:
|
||||
items = [
|
||||
{
|
||||
'product_id': item.product_id,
|
||||
'product_name': item.product.name,
|
||||
'quantity': item.quantity,
|
||||
'price': item.price,
|
||||
'unit': item.unit,
|
||||
}
|
||||
for item in order.items.all()
|
||||
]
|
||||
items = self._aggregate_items(order.items.all())
|
||||
records.append(
|
||||
self._build_record(
|
||||
counterparty_id=order.customer_id,
|
||||
@@ -309,19 +311,11 @@ class SupplierStatementView(StatementViewBase):
|
||||
status=business_models.PurchaseOrderStatusEnum.APPROVED,
|
||||
)
|
||||
.select_related('supplier')
|
||||
.prefetch_related('items__product')
|
||||
)
|
||||
records = []
|
||||
for order in qs:
|
||||
items = [
|
||||
{
|
||||
'product_id': item.product_id,
|
||||
'product_name': item.product.name,
|
||||
'quantity': item.quantity,
|
||||
'price': item.price,
|
||||
'unit': item.unit,
|
||||
}
|
||||
for item in order.items.all()
|
||||
]
|
||||
items = self._aggregate_items(order.items.all())
|
||||
records.append(
|
||||
self._build_record(
|
||||
counterparty_id=order.supplier_id,
|
||||
@@ -348,19 +342,11 @@ class SupplierStatementView(StatementViewBase):
|
||||
status=business_models.PurchaseReturnStatusEnum.APPROVED,
|
||||
)
|
||||
.select_related('supplier')
|
||||
.prefetch_related('items__product')
|
||||
)
|
||||
records = []
|
||||
for order in qs:
|
||||
items = [
|
||||
{
|
||||
'product_id': item.product_id,
|
||||
'product_name': item.product.name,
|
||||
'quantity': item.quantity,
|
||||
'price': item.price,
|
||||
'unit': item.unit,
|
||||
}
|
||||
for item in order.items.all()
|
||||
]
|
||||
items = self._aggregate_items(order.items.all())
|
||||
records.append(
|
||||
self._build_record(
|
||||
counterparty_id=order.supplier_id,
|
||||
|
||||
Reference in New Issue
Block a user