factory/apps/fim/services.py

49 lines
1.7 KiB
Python

from apps.fim.models import FeeSet, Fee, PriceSet
from apps.mtm.models import Mgroup, Material
import datetime
def get_cost_unit(mgroup: Mgroup, fee: Fee, year: int = 0, month: int = 0):
"""
获取某工段某费用的月成本, 默认本月
"""
now = datetime.datetime.now()
if year and month:
params = {'mgroup': mgroup, 'fee': fee, 'year': year, 'month': month}
else:
params = {'mgroup': mgroup, 'fee': fee, 'year': now.year, 'month': now.month}
qs = FeeSet.objects.filter(**params)
if qs.exists():
feeset = qs.first()
return feeset.cost_unit
else:
feeset_last = FeeSet.objects.filter(mgroup=mgroup, fee=fee).order_by('year', 'month').last()
if feeset_last:
params.update({'cost_unit': feeset_last.cost_unit})
else:
params.update({'cost_unit': 0})
feeset = FeeSet.objects.create(**params)
return feeset.cost_unit
def get_price_unit(material: Material, year: int = 0, month: int = 0):
"""
获取某物料的月成本, 默认本月
"""
now = datetime.datetime.now()
if year and month:
params = {'material': material, 'year': year, 'month': month}
else:
params = {'material': material, 'year': now.year, 'month': now.month}
qs = PriceSet.objects.filter(**params)
if qs.exists():
ps = qs.first()
return ps.price_unit
else:
ps_last = PriceSet.objects.filter(material=material).order_by('year', 'month').last()
if ps_last:
params.update({'price_unit': ps_last.price_unit})
else:
params.update({'price_unit': 0})
ps = PriceSet.objects.create(**params)
return ps.price_unit