HimoolERP/apps/finance/models.py

178 lines
8.5 KiB
Python
Raw Normal View History

2021-12-17 17:14:23 +08:00
from extensions.common.base import *
2021-11-04 23:49:56 +08:00
from extensions.models import *
2021-11-04 17:35:34 +08:00
2021-11-04 23:49:56 +08:00
2021-11-07 18:12:52 +08:00
class PaymentOrder(Model):
"""付款单据"""
2021-11-05 18:49:51 +08:00
2021-11-07 18:12:52 +08:00
number = CharField(max_length=32, verbose_name='编号')
supplier = ForeignKey('data.Supplier', on_delete=PROTECT, related_name='payment_orders', verbose_name='供应商')
handler = ForeignKey('system.User', on_delete=PROTECT, related_name='payment_orders', verbose_name='经手人')
handle_time = DateTimeField(verbose_name='处理时间')
remark = CharField(max_length=256, null=True, blank=True, verbose_name='备注')
total_amount = AmountField(null=True, verbose_name='总金额')
discount_amount = AmountField(default=0, verbose_name='优惠金额')
is_void = BooleanField(default=False, verbose_name='作废状态')
creator = ForeignKey('system.User', on_delete=PROTECT,
related_name='created_payment_orders', verbose_name='创建人')
create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间')
team = ForeignKey('system.Team', on_delete=CASCADE, related_name='payment_orders')
2021-11-05 18:49:51 +08:00
2021-11-07 18:12:52 +08:00
class Meta:
unique_together = [('number', 'team')]
2021-11-11 23:58:18 +08:00
@classmethod
def get_number(cls, team):
2021-12-17 17:14:23 +08:00
start_date, end_date = pendulum.today(), pendulum.tomorrow()
2021-11-11 23:58:18 +08:00
instance = cls.objects.filter(team=team, create_time__gte=start_date, create_time__lt=end_date).last()
try:
result = re.match('^(.*?)([1-9]+)$', instance.number)
number = result.group(1) + str(int(result.group(2)) + 1)
except AttributeError:
2021-12-17 17:14:23 +08:00
number = 'FK' + pendulum.today().format('YYYYMMDD') + '0001'
2021-11-11 23:58:18 +08:00
return number
2021-11-07 18:12:52 +08:00
class PaymentAccount(Model):
"""付款账户"""
payment_order = ForeignKey('finance.PaymentOrder', on_delete=CASCADE,
related_name='payment_accounts', verbose_name='付款单据')
account = ForeignKey('data.Account', on_delete=PROTECT, related_name='payment_accounts', verbose_name='结算账户')
payment_amount = AmountField(verbose_name='付款金额')
team = ForeignKey('system.Team', on_delete=CASCADE, related_name='payment_accounts')
class Meta:
2021-11-08 02:15:37 +08:00
unique_together = [('payment_order', 'account')]
2021-11-07 18:12:52 +08:00
class CollectionOrder(Model):
"""收款单据"""
number = CharField(max_length=32, verbose_name='编号')
client = ForeignKey('data.Client', on_delete=PROTECT, related_name='collection_orders', verbose_name='客户')
handler = ForeignKey('system.User', on_delete=PROTECT, related_name='collection_orders', verbose_name='经手人')
handle_time = DateTimeField(verbose_name='处理时间')
remark = CharField(max_length=256, null=True, blank=True, verbose_name='备注')
total_amount = AmountField(null=True, verbose_name='总金额')
discount_amount = AmountField(default=0, verbose_name='优惠金额')
is_void = BooleanField(default=False, verbose_name='作废状态')
creator = ForeignKey('system.User', on_delete=PROTECT,
related_name='created_collection_orders', verbose_name='创建人')
create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间')
team = ForeignKey('system.Team', on_delete=CASCADE, related_name='collection_orders')
class Meta:
unique_together = [('number', 'team')]
2021-11-12 15:37:43 +08:00
@classmethod
def get_number(cls, team):
2021-12-17 17:14:23 +08:00
start_date, end_date = pendulum.today(), pendulum.tomorrow()
2021-11-12 15:37:43 +08:00
instance = cls.objects.filter(team=team, create_time__gte=start_date, create_time__lt=end_date).last()
try:
result = re.match('^(.*?)([1-9]+)$', instance.number)
number = result.group(1) + str(int(result.group(2)) + 1)
except AttributeError:
2021-12-17 17:14:23 +08:00
number = 'CK' + pendulum.today().format('YYYYMMDD') + '0001'
2021-11-12 15:37:43 +08:00
return number
2021-11-07 18:12:52 +08:00
class CollectionAccount(Model):
"""收款账户"""
collection_order = ForeignKey('finance.CollectionOrder', on_delete=CASCADE,
related_name='collection_accounts', verbose_name='收款单据')
account = ForeignKey('data.Account', on_delete=PROTECT, related_name='collection_accounts', verbose_name='结算账户')
2021-11-14 15:23:15 +08:00
collection_amount = AmountField(verbose_name='收款金额')
2021-11-07 18:12:52 +08:00
team = ForeignKey('system.Team', on_delete=CASCADE, related_name='collection_accounts')
class Meta:
unique_together = [('collection_order', 'account')]
2021-11-05 18:49:51 +08:00
2021-11-08 00:25:38 +08:00
class ChargeOrder(Model):
"""收支单据"""
2021-11-16 02:16:44 +08:00
class Type(TextChoices):
"""收支类型"""
INCOME = ('income', '收入')
EXPENDITURE = ('expenditure', '支出')
2021-11-08 00:25:38 +08:00
number = CharField(max_length=32, verbose_name='编号')
2021-11-16 02:16:44 +08:00
type = CharField(max_length=32, choices=Type.choices, verbose_name='收支类型')
2021-11-08 00:25:38 +08:00
supplier = ForeignKey('data.Supplier', on_delete=PROTECT, null=True,
related_name='charge_orders', verbose_name='供应商')
2021-11-16 02:16:44 +08:00
client = ForeignKey('data.Client', on_delete=PROTECT, null=True,
related_name='charge_orders', verbose_name='客户')
2021-11-08 00:25:38 +08:00
handler = ForeignKey('system.User', on_delete=PROTECT, related_name='charge_orders', verbose_name='经手人')
handle_time = DateTimeField(verbose_name='处理时间')
charge_item = ForeignKey('data.ChargeItem', on_delete=SET_NULL, null=True,
related_name='charge_orders', verbose_name='收支项目')
charge_item_name = CharField(max_length=64, verbose_name='收支项目名称')
account = ForeignKey('data.Account', on_delete=PROTECT, related_name='charge_orders', verbose_name='结算账户')
2021-11-17 00:36:24 +08:00
total_amount = AmountField(verbose_name='应收/付金额')
charge_amount = AmountField(verbose_name='实收/付金额')
2021-11-08 00:25:38 +08:00
remark = CharField(max_length=256, null=True, blank=True, verbose_name='备注')
is_void = BooleanField(default=False, verbose_name='作废状态')
creator = ForeignKey('system.User', on_delete=PROTECT,
related_name='created_charge_orders', verbose_name='创建人')
create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间')
team = ForeignKey('system.Team', on_delete=CASCADE, related_name='charge_orders')
class Meta:
unique_together = [('number', 'team')]
2021-11-05 18:49:51 +08:00
2021-11-12 15:37:43 +08:00
@classmethod
def get_number(cls, team):
2021-12-17 17:14:23 +08:00
start_date, end_date = pendulum.today(), pendulum.tomorrow()
2021-11-12 15:37:43 +08:00
instance = cls.objects.filter(team=team, create_time__gte=start_date, create_time__lt=end_date).last()
try:
result = re.match('^(.*?)([1-9]+)$', instance.number)
number = result.group(1) + str(int(result.group(2)) + 1)
except AttributeError:
2021-12-17 17:14:23 +08:00
number = 'SZ' + pendulum.today().format('YYYYMMDD') + '0001'
2021-11-12 15:37:43 +08:00
return number
2021-11-05 18:49:51 +08:00
2021-11-06 00:55:40 +08:00
class AccountTransferRecord(Model):
"""结算账户转账记录"""
2021-11-08 00:25:38 +08:00
class ServiceChargePayer(TextChoices):
"""手续费支付方"""
TRANSFER_IN = ('transfer_in', '转入方')
TRANSFER_OUT = ('transfer_out', '转出方')
2021-11-16 14:01:31 +08:00
out_account = ForeignKey('data.Account', on_delete=PROTECT,
related_name='out_account_transfer_records', verbose_name='转出账户')
2021-11-08 00:25:38 +08:00
transfer_out_time = DateTimeField(verbose_name='转出时间')
2021-11-16 14:01:31 +08:00
in_account = ForeignKey('data.Account', on_delete=PROTECT,
related_name='in_account_transfer_records', verbose_name='转入账户')
2021-11-08 00:25:38 +08:00
transfer_in_time = DateTimeField(verbose_name='转入时间')
transfer_amount = AmountField(verbose_name='转账金额')
2021-11-17 00:36:24 +08:00
service_charge_amount = AmountField(default=0, verbose_name='手续费金额')
2021-11-08 00:25:38 +08:00
service_charge_payer = CharField(max_length=32, choices=ServiceChargePayer.choices,
2021-11-17 00:36:24 +08:00
default=ServiceChargePayer.TRANSFER_OUT, verbose_name='手续费支付方')
2021-11-08 00:25:38 +08:00
handler = ForeignKey('system.User', on_delete=PROTECT, related_name='account_transfer_records', verbose_name='经手人')
handle_time = DateTimeField(verbose_name='处理时间')
remark = CharField(max_length=256, null=True, blank=True, verbose_name='备注')
is_void = BooleanField(default=False, verbose_name='作废状态')
creator = ForeignKey('system.User', on_delete=PROTECT,
related_name='created_account_transfer_records', verbose_name='创建人')
create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间')
team = ForeignKey('system.Team', on_delete=CASCADE, related_name='account_transfer_records')
2021-11-06 00:55:40 +08:00
2021-11-04 23:49:56 +08:00
__all__ = [
2021-11-08 00:25:38 +08:00
'PaymentOrder', 'PaymentAccount',
'CollectionOrder', 'CollectionAccount',
'ChargeOrder', 'AccountTransferRecord',
2021-11-04 23:49:56 +08:00
]