134 lines
6.8 KiB
Python
134 lines
6.8 KiB
Python
from django.db import models
|
|
from django.db.models import Sum
|
|
from decimal import Decimal
|
|
from django.utils import timezone
|
|
|
|
from application.busi_tbl.models import UpdateLog
|
|
from application.org_mgnt.models import PrimaryDepartment
|
|
from application.pjt_mgnt.models import ProjectLedger
|
|
|
|
|
|
class DepartmentOperationalData(models.Model):
|
|
department = models.CharField(max_length=255, verbose_name="一级部门", help_text="部门名称", null=True, blank=True)
|
|
year_month = models.CharField(max_length=7, verbose_name="年月", help_text="格式为'YYYY-MM',表示统计数据的年月", null=True, blank=True)
|
|
current_month_revenue = models.FloatField(verbose_name="当月收入(万元)", help_text="当月总收入", null=True, blank=True)
|
|
current_month_new_revenue = models.FloatField(verbose_name="当月新增收入(万元)", help_text="当月新增的收入", null=True, blank=True)
|
|
current_month_existing_revenue = models.FloatField(verbose_name="当月存量收入(万元)", help_text="当月存量收入", null=True, blank=True)
|
|
current_month_cost = models.FloatField(verbose_name="当月成本(万元)", help_text="当月总成本", null=True, blank=True)
|
|
gross_profit = models.FloatField(verbose_name="毛利润(万元)", help_text="当月收入减去当月成本", null=True, blank=True)
|
|
taxes_and_surcharges = models.FloatField(verbose_name="税金及附加(万元)", help_text="根据税额累计后乘以1.12得到的税金及附加总额", null=True, blank=True)
|
|
expenses = models.FloatField(verbose_name="费用(万元)", help_text="毛利润减去税金及附加和营业利润得到的费用", null=True, blank=True)
|
|
operating_profit = models.FloatField(verbose_name="营业利润(万元)", help_text="当月收入减去成本、税金及附加和费用后的利润", null=True, blank=True)
|
|
operational_cash_flow = models.FloatField(verbose_name="经营性现金流(万元)", help_text="经营活动产生的现金流量,需手动添加", null=True, blank=True)
|
|
|
|
class Meta:
|
|
verbose_name = "各一级部门经营情况"
|
|
verbose_name_plural = "各一级部门经营情况"
|
|
db_table_comment = '存储各一级部门经营情况数据,包括一级部门、年月、当月收入、当月新增收入、当月存量收入、当月成本、毛利润、税金及附加、费用、营业利润和经营性现金流等'
|
|
|
|
def __str__(self):
|
|
return f"Operational Data for {self.department} in {self.year_month}"
|
|
|
|
@staticmethod
|
|
def get_field_labels():
|
|
return {
|
|
'department': '一级部门',
|
|
'year_month': '年月',
|
|
'current_month_revenue': '当月收入',
|
|
'current_month_new_revenue': '当月新增收入',
|
|
'current_month_existing_revenue': '当月存量收入',
|
|
'current_month_cost': '当月成本',
|
|
'gross_profit': '毛利润',
|
|
'taxes_and_surcharges': '税金及附加',
|
|
'expenses': '费用',
|
|
'operating_profit': '营业利润',
|
|
'operational_cash_flow': '经营性现金流',
|
|
}
|
|
|
|
def get_field_label(self, field_name):
|
|
field_labels = self.get_field_labels()
|
|
return field_labels.get(field_name, field_name)
|
|
|
|
@staticmethod
|
|
def calculate_and_update_for_month(year_month):
|
|
primary_departments = PrimaryDepartment.objects.all()
|
|
year, month = map(int, year_month.split('-'))
|
|
|
|
for department in primary_departments:
|
|
primary_department = department.department_name
|
|
|
|
# 当月收入
|
|
current_month_revenue = ProjectLedger.objects.filter(
|
|
primary_department=primary_department,
|
|
start_date__year=year,
|
|
start_date__month=month
|
|
).aggregate(total=Sum('total_amount_including_tax'))['total'] or Decimal('0')
|
|
|
|
# 当月新增收入
|
|
current_month_new_revenue = ProjectLedger.objects.filter(
|
|
primary_department=primary_department,
|
|
start_date__year=year,
|
|
start_date__month=month,
|
|
project_nature="新增"
|
|
).aggregate(total=Sum('total_amount_including_tax'))['total'] or Decimal('0')
|
|
|
|
# 当月存量收入
|
|
current_month_existing_revenue = ProjectLedger.objects.filter(
|
|
primary_department=primary_department,
|
|
start_date__year=year,
|
|
start_date__month=month,
|
|
project_nature="存量"
|
|
).aggregate(total=Sum('total_amount_including_tax'))['total'] or Decimal('0')
|
|
|
|
# 当月成本
|
|
total_amount_including_tax = ProjectLedger.objects.filter(
|
|
primary_department=primary_department,
|
|
start_date__year=year,
|
|
start_date__month=month
|
|
).aggregate(total=Sum('total_amount_including_tax'))['total'] or Decimal('0')
|
|
|
|
net_receivable_amount = ProjectLedger.objects.filter(
|
|
primary_department=primary_department,
|
|
start_date__year=year,
|
|
start_date__month=month
|
|
).aggregate(total=Sum('receivable_net_income'))['total'] or Decimal('0')
|
|
|
|
current_month_cost = total_amount_including_tax - net_receivable_amount
|
|
|
|
# 毛利润
|
|
gross_profit = current_month_revenue - current_month_cost
|
|
|
|
# 税金及附加
|
|
taxes_and_surcharges = gross_profit * Decimal('0.12')
|
|
|
|
# 费用
|
|
expenses = gross_profit - taxes_and_surcharges
|
|
|
|
# 营业利润
|
|
operating_profit = current_month_revenue - current_month_cost - taxes_and_surcharges - expenses
|
|
|
|
# 更新或创建模型实例
|
|
operational_data, created = DepartmentOperationalData.objects.update_or_create(
|
|
department=primary_department,
|
|
year_month=year_month,
|
|
defaults={
|
|
'current_month_revenue': float(current_month_revenue),
|
|
'current_month_new_revenue': float(current_month_new_revenue),
|
|
'current_month_existing_revenue': float(current_month_existing_revenue),
|
|
'current_month_cost': float(current_month_cost),
|
|
'gross_profit': float(gross_profit),
|
|
'taxes_and_surcharges': float(taxes_and_surcharges),
|
|
'expenses': float(expenses),
|
|
'operating_profit': float(operating_profit),
|
|
}
|
|
)
|
|
|
|
# 更新或创建更新时间记录
|
|
# UpdateLog.objects.update_or_create(
|
|
# table_name='DepartmentOperationalData',
|
|
# defaults={'last_update': timezone.now().date()}
|
|
# )
|
|
|
|
@classmethod
|
|
def update_all_departments_for_month(cls, year_month):
|
|
cls.calculate_and_update_for_month(year_month) |