forked from erp-dev/erp
fix
This commit is contained in:
@@ -122,13 +122,20 @@ class PreSalesOrderItemSerializer(serializers.ModelSerializer):
|
||||
|
||||
|
||||
class PreSalesOrderItemDetailSerializer(PreSalesOrderItemSerializer):
|
||||
allocation_records = AllocationRecordSerializer(many=True, read_only=True)
|
||||
allocation_records = serializers.SerializerMethodField(read_only=True)
|
||||
|
||||
class Meta(PreSalesOrderItemSerializer.Meta):
|
||||
fields = PreSalesOrderItemSerializer.Meta.fields + [
|
||||
'allocation_records',
|
||||
]
|
||||
|
||||
def get_allocation_records(self, obj: business_models.PreSalesOrderItem):
|
||||
"""返回非已撤销的配货记录"""
|
||||
active_records = obj.allocation_records.exclude(
|
||||
status=business_models.AllocationRecordStatusEnum.CANCELLED
|
||||
)
|
||||
return AllocationRecordSerializer(active_records, many=True).data
|
||||
|
||||
|
||||
class PreSalesOrderSerializer(serializers.ModelSerializer):
|
||||
human_id = serializers.CharField(read_only=True)
|
||||
@@ -136,6 +143,8 @@ class PreSalesOrderSerializer(serializers.ModelSerializer):
|
||||
warehouse_name = serializers.CharField(source='warehouse.name', read_only=True)
|
||||
operator_name = serializers.CharField(source='operator.name', read_only=True)
|
||||
items = PreSalesOrderItemSerializer(many=True, read_only=True)
|
||||
allocated_quantity = serializers.SerializerMethodField(read_only=True)
|
||||
progress = serializers.SerializerMethodField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = business_models.PreSalesOrder
|
||||
@@ -153,6 +162,8 @@ class PreSalesOrderSerializer(serializers.ModelSerializer):
|
||||
'remarks',
|
||||
'created_at',
|
||||
'items',
|
||||
'allocated_quantity',
|
||||
'progress',
|
||||
]
|
||||
read_only_fields = [
|
||||
'id',
|
||||
@@ -162,8 +173,34 @@ class PreSalesOrderSerializer(serializers.ModelSerializer):
|
||||
'warehouse_name',
|
||||
'operator_name',
|
||||
'items',
|
||||
'allocated_quantity',
|
||||
'progress',
|
||||
]
|
||||
|
||||
def get_allocated_quantity(self, obj: business_models.PreSalesOrder) -> str:
|
||||
"""计算整单已配货总量"""
|
||||
total = Decimal('0')
|
||||
for item in obj.items.all():
|
||||
result = item.allocation_records.filter(
|
||||
status=business_models.AllocationRecordStatusEnum.ACTIVE
|
||||
).aggregate(total=Sum('quantity'))
|
||||
total += result.get('total') or Decimal('0')
|
||||
return str(total)
|
||||
|
||||
def get_progress(self, obj: business_models.PreSalesOrder) -> str:
|
||||
"""计算整单配货进度(已配货总量/总需求量)"""
|
||||
required_total = Decimal('0')
|
||||
allocated_total = Decimal('0')
|
||||
for item in obj.items.all():
|
||||
required_total += item.quantity or Decimal('0')
|
||||
result = item.allocation_records.filter(
|
||||
status=business_models.AllocationRecordStatusEnum.ACTIVE
|
||||
).aggregate(total=Sum('quantity'))
|
||||
allocated_total += result.get('total') or Decimal('0')
|
||||
if required_total <= 0:
|
||||
return '0'
|
||||
return str((allocated_total / required_total).quantize(Decimal('0.0001')))
|
||||
|
||||
|
||||
class PreSalesOrderDetailSerializer(PreSalesOrderSerializer):
|
||||
items = PreSalesOrderItemDetailSerializer(many=True, read_only=True)
|
||||
|
||||
Reference in New Issue
Block a user