diff --git a/apps/fim/serializers.py b/apps/fim/serializers.py index a6440cac..3fc17301 100644 --- a/apps/fim/serializers.py +++ b/apps/fim/serializers.py @@ -5,6 +5,7 @@ from apps.fim.models import PriceSet, FeeSet, Fee from apps.mtm.models import Mgroup from apps.wpm.models import SfLog from rest_framework.exceptions import ParseError +from apps.fim.tasks import cal_enstat_when_priceset_change, cal_enstat_when_feeset_change class FeeSerializer(CustomModelSerializer): class Meta: @@ -20,6 +21,17 @@ class FeeSetSerializer(CustomModelSerializer): fields = '__all__' read_only_fields = EXCLUDE_FIELDS + def update(self, instance, validated_data): + validated_data.pop('year', None) + validated_data.pop('month', None) + validated_data.pop('mgroup', None) + validated_data.pop('fee', None) + old_cost_unit = instance.cost_unit + instance = super().update(instance, validated_data) + new_cost_unit = instance.cost_unit + if new_cost_unit != old_cost_unit: + cal_enstat_when_feeset_change.delay(instance.id) + return instance class PriceSetSerializer(CustomModelSerializer): material_name = serializers.CharField(source='material.name', read_only=True) @@ -28,6 +40,17 @@ class PriceSetSerializer(CustomModelSerializer): fields = '__all__' read_only_fields = EXCLUDE_FIELDS + def update(self, instance, validated_data): + validated_data.pop('year', None) + validated_data.pop('month', None) + validated_data.pop('material', None) + old_price_unit = instance.price_unit + instance = super().update(instance, validated_data) + new_price_unit = instance.price_unit + if new_price_unit != old_price_unit: + cal_enstat_when_priceset_change.delay(instance.id) + return instance + class CostStatSerializer(serializers.Serializer): type = serializers.CharField(label="统计维度", help_text="sflog/day_s/month_s/year_s") diff --git a/apps/fim/tasks.py b/apps/fim/tasks.py new file mode 100644 index 00000000..503db643 --- /dev/null +++ b/apps/fim/tasks.py @@ -0,0 +1,72 @@ +# Create your tasks here +from __future__ import absolute_import, unicode_literals +from apps.utils.tasks import CustomTask +from celery import shared_task +from apps.utils.sql import DbConnection +from server.settings import get_sysconfig +from django.core.cache import cache +from apps.fim.models import PriceSet, FeeSet +from apps.mtm.models import Mgroup + + + +@shared_task(base=CustomTask) +def cal_enstat_when_priceset_change(pricesetId): + from apps.enm.models import EnStat + priceset = PriceSet.objects.get(id=pricesetId) + # 影响物料成本的计算 + price_unit = priceset.price_unit + material = priceset.material + materialId = material.id + # 受影响的工段 + mgroups = Mgroup.objects.filter(input_materials__contains=material.id) + enstats = EnStat.objects.filter(mgroup__in=mgroups) + for enstat in enstats: + old_cost_unit = 0 + new_cost_unit = 0 + imaterial_data = enstat.imaterial_data + for idata in imaterial_data: + old_cost_unit = old_cost_unit + idata['cost_unit'] + if idata['material'] == materialId: + idata['price_unit'] = price_unit + idata['cost'] = idata['price_unit'] * idata['amount_consume'] + try: + idata['cost_unit'] = idata['cost']/enstat.total_production + except: + idata['cost_unit'] = 0 + new_cost_unit = new_cost_unit + idata['cost_unit'] + + # 更新一些数据 + enstat.imaterial_data = imaterial_data + enstat.production_cost_unit = enstat.production_cost_unit - old_cost_unit + new_cost_unit + enstat.save() + if material.code in ['bulk_cement', 'bag_cement', 'clinker']: # 需要更新enstat2 + from apps.enm.tasks import cal_enstat2 + cal_enstat2(priceset.year, priceset.month) + + +@shared_task(base=CustomTask) +def cal_enstat_when_feeset_change(feesetId): + from apps.enm.models import EnStat + feeset = FeeSet.objects.get(id=feesetId) + # 影响物料成本的计算 + cost_unit = feeset.price_unit + mgroup = feeset.mgroup + fee = feeset.fee + feeId = fee.id + # 受影响的工段 + enstats = EnStat.objects.filter(mgroup=mgroup) + for enstat in enstats: + old_cost_unit = 0 + new_cost_unit = 0 + other_cost_data = enstat.other_cost_data + for idata in other_cost_data: + old_cost_unit = old_cost_unit + idata['cost_unit'] + if idata['id'] == feeId: + idata['cost_unit'] = cost_unit + new_cost_unit = new_cost_unit + idata['cost_unit'] + + # 更新一些数据 + enstat.other_cost_data = other_cost_data + enstat.production_cost_unit = enstat.production_cost_unit - old_cost_unit + new_cost_unit + enstat.save()