feat: apps/enm 新增成本计算

This commit is contained in:
zty 2024-10-10 13:33:58 +08:00
parent 74ece85b0a
commit 96f4b0c445
6 changed files with 64 additions and 8 deletions

View File

@ -0,0 +1,23 @@
# Generated by Django 3.2.12 on 2024-10-10 03:40
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('enm', '0045_merge_20240913_1401'),
]
operations = [
migrations.AddField(
model_name='enstat',
name='total_production_cost',
field=models.FloatField(default=0, help_text='t', verbose_name='总产量(用于成本)'),
),
migrations.AlterField(
model_name='enstat',
name='imaterial_data_dict',
field=models.JSONField(blank=True, default=dict, verbose_name='成本物料数据'),
),
]

View File

@ -123,6 +123,7 @@ class EnStat(BaseModel):
month_s = models.PositiveSmallIntegerField("班月", null=True, blank=True)
day_s = models.PositiveSmallIntegerField("班日", null=True, blank=True)
total_production = models.FloatField("总产量", default=0, help_text="t")
total_production_cost = models.FloatField("总产量(用于成本)", default=0, help_text="t")
elec_consume = models.FloatField("总电耗", default=0, help_text="kw·h")
elec_coal_consume = models.FloatField("电量折标煤", default=0, help_text="tce")
pcoal_heat = models.FloatField("煤粉热值", default=0)

View File

@ -433,15 +433,19 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
if "material" in this_cal_attrs:
# 消耗物料统计(包括电耗)
input_materials = []
has_product = False
input_materials = mgroup.input_materials
input_materials_dict = {}
has_p_cost = False
if mgroup.product:
input_materials.insert(0, mgroup.product.id)
has_product = True
input_materials_dict[mgroup.product.id] = "P"
if mgroup.product_cost:
input_materials_dict[mgroup.product_cost.id] = "P_COST"
has_p_cost = True
input_materials_dict.update({i.id: "M" for i in input_materials if i.id not in input_materials_dict})
imaterial_cost_unit = 0
imaterial_data = []
imaterial_data_dict = {}
for ind, mid in enumerate(input_materials):
for mid, mtype in input_materials_dict:
material = Material.objects.get(id=mid)
if type == "hour_s":
mps = MpointStat.objects.filter(type="hour_s", mgroup=mgroup, year_s=year_s, month_s=month_s, day_s=day_s, hour=hour, mpoint__material=material)
@ -460,8 +464,10 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
amount_consume = mps.aggregate(sum=Sum("val"))["sum"]
if amount_consume is None:
amount_consume = 0
if ind == 0 and has_product: # 如果是产量
if mtype == "P": # 如果是产量
enstat.total_production = amount_consume
elif mtype == "P_COST": # 如果是产量(用于计算成本)
enstat.total_production_cost = amount_consume
else:
if material.code in ["pcoal", "cair", "steam"]:
price_unit = 0
@ -469,7 +475,7 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
price_unit = get_price_unit(material, year_s, month_s)
cost = amount_consume * price_unit
try:
cost_unit = cost / enstat.total_production
cost_unit = cost / enstat.total_production if has_p_cost is False else enstat.total_production_cost
except ZeroDivisionError:
cost_unit = 0
imaterial_cost_unit = imaterial_cost_unit + cost_unit

View File

@ -26,7 +26,7 @@ class MpointViewSet(CustomModelViewSet):
queryset = Mpoint.objects.all()
serializer_class = MpointSerializer
select_related_fields = ["create_by", "belong_dept", "ep_monitored", "ep_belong", "mgroup"]
select_related_fields = ["create_by", "belong_dept", "ep_monitored", "ep_belong", "mgroup", "material__name", "material__code", "nickname"]
filterset_fields = {
"belong_dept": ["exact"],
"ep_monitored": ["exact"],

View File

@ -0,0 +1,24 @@
# Generated by Django 3.2.12 on 2024-10-10 03:40
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('mtm', '0041_process_mstate_json'),
]
operations = [
migrations.AddField(
model_name='mgroup',
name='product_cost',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='mgroup_product_cost', to='mtm.material', verbose_name='主要产品(成本计算)'),
),
migrations.AlterField(
model_name='mgroup',
name='product',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='mgroup_product', to='mtm.material', verbose_name='主要产品'),
),
]

View File

@ -118,7 +118,9 @@ class Mgroup(CommonBModel):
process = models.ForeignKey(
Process, verbose_name='工序', on_delete=models.SET_NULL, null=True, blank=True)
product = models.ForeignKey(
Material, verbose_name='主要产品', on_delete=models.SET_NULL, null=True, blank=True)
Material, verbose_name='主要产品', on_delete=models.SET_NULL, null=True, blank=True, related_name='mgroup_product')
product_cost = models.ForeignKey(
Material, verbose_name='主要产品(成本计算)', on_delete=models.SET_NULL, null=True, blank=True, related_name='mgroup_product_cost')
input_materials = models.JSONField(
'直接材料', default=list, blank=True, help_text='material的ID列表')
test_materials = models.JSONField(