forked from erp-dev/erp
feat: big
This commit is contained in:
@@ -2,9 +2,10 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
from decimal import Decimal, ROUND_HALF_UP
|
||||
from typing import Protocol, runtime_checkable
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
|
||||
from flower.common import ModelBase
|
||||
@@ -44,6 +45,8 @@ class CostCategory(ModelBase):
|
||||
class CostEntry(ModelBase):
|
||||
"""支出明细"""
|
||||
|
||||
AMOUNT_QUANT = Decimal('0.01')
|
||||
|
||||
merchant = models.ForeignKey(
|
||||
'basic_info.Merchant',
|
||||
on_delete=models.PROTECT,
|
||||
@@ -56,7 +59,27 @@ class CostEntry(ModelBase):
|
||||
related_name='entries',
|
||||
verbose_name='支出类目',
|
||||
)
|
||||
amount = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='金额')
|
||||
amount = models.DecimalField(max_digits=15, decimal_places=2, blank=True, verbose_name='金额')
|
||||
unit_amount = models.DecimalField(
|
||||
max_digits=15,
|
||||
decimal_places=4,
|
||||
null=True,
|
||||
blank=True,
|
||||
verbose_name='单价/基数金额',
|
||||
)
|
||||
quantity = models.DecimalField(
|
||||
max_digits=12,
|
||||
decimal_places=4,
|
||||
null=True,
|
||||
blank=True,
|
||||
verbose_name='数量/倍数',
|
||||
)
|
||||
unit_name = models.CharField(
|
||||
max_length=20,
|
||||
null=True,
|
||||
blank=True,
|
||||
verbose_name='单位名称',
|
||||
)
|
||||
occurred_at = models.DateField(verbose_name='发生日期')
|
||||
operator = models.ForeignKey(
|
||||
'basic_info.Employee',
|
||||
@@ -84,6 +107,41 @@ class CostEntry(ModelBase):
|
||||
def __str__(self):
|
||||
return f'{self.category.name} {self.amount} ({self.occurred_at})'
|
||||
|
||||
@property
|
||||
def has_amount_formula(self) -> bool:
|
||||
"""是否使用 unit_amount * quantity 推导最终金额。"""
|
||||
return self.unit_amount is not None or self.quantity is not None
|
||||
|
||||
def calculate_amount(self) -> Decimal | None:
|
||||
"""根据单价和数量计算最终金额。"""
|
||||
if self.unit_amount is None and self.quantity is None:
|
||||
return None
|
||||
if self.unit_amount is None or self.quantity is None:
|
||||
raise ValidationError({
|
||||
'unit_amount': 'unit_amount 和 quantity 必须同时填写或同时为空',
|
||||
'quantity': 'unit_amount 和 quantity 必须同时填写或同时为空',
|
||||
})
|
||||
return (self.unit_amount * self.quantity).quantize(self.AMOUNT_QUANT, rounding=ROUND_HALF_UP)
|
||||
|
||||
def apply_amount_formula(self) -> None:
|
||||
"""如果存在公式字段,则用公式结果覆盖 amount。"""
|
||||
calculated_amount = self.calculate_amount()
|
||||
if calculated_amount is not None:
|
||||
self.amount = calculated_amount
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
self.apply_amount_formula()
|
||||
if self.amount is None:
|
||||
raise ValidationError({'amount': '普通支出必须填写 amount'})
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.clean()
|
||||
update_fields = kwargs.get('update_fields')
|
||||
if update_fields is not None and self.has_amount_formula:
|
||||
kwargs['update_fields'] = set(update_fields) | {'amount'}
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
|
||||
# ==================== 六边形端口协议 ====================
|
||||
|
||||
@@ -94,10 +152,13 @@ class CostEntryInput:
|
||||
|
||||
category_key: str
|
||||
category_name: str
|
||||
amount: Decimal
|
||||
amount: Decimal | None
|
||||
occurred_at: date
|
||||
source_module: str
|
||||
source_id: str
|
||||
unit_amount: Decimal | None = None
|
||||
quantity: Decimal | None = None
|
||||
unit_name: str = ''
|
||||
remarks: str = ''
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user