feat: mio自动生成编号

This commit is contained in:
caoqianming 2025-03-11 12:40:45 +08:00
parent f59cb2cbc2
commit 5bd17c4a37
3 changed files with 36 additions and 11 deletions

View File

@ -4,6 +4,8 @@ from apps.pum.models import Supplier, PuOrder
from apps.sam.models import Customer, Order
from apps.mtm.models import Material, Mgroup
from apps.system.models import User
from datetime import datetime
from django.db.models import Max
# Create your models here.
@ -49,6 +51,15 @@ class MaterialBatchA(BaseModel):
MaterialBatch, verbose_name='关联物料批次', on_delete=models.CASCADE, related_name='a_mb')
MIO_TYPE_PREFIX = {
'do_out': 'SCLL', # 生产领料 (Shēngchǎn Lǐngliào)
'sale_out': 'XSFH', # 销售发货 (Xiāoshòu Fāhuò)
'pur_in': 'CGRK', # 采购入库 (Cǎigòu Rùkù)
'do_in': 'SCRK', # 生产入库 (Shēngchǎn Rùkù)
'other_in': 'QTRK', # 其他入库 (Qítā Rùkù)
'other_out': 'QTCK' # 其他出库 (Qítā Chūkù)
}
class MIO(CommonBDModel):
"""
出入库记录
@ -101,8 +112,17 @@ class MIO(CommonBDModel):
materials = models.ManyToManyField(
Material, verbose_name='物料明细', through='inm.mioitem', blank=True)
def get_a_number(self, type):
pass
@classmethod
def get_a_number(cls, mio_type:str):
today_str = datetime.now().strftime('%Y%m%d')
prefix = MIO_TYPE_PREFIX[mio_type]
last_record = MIO.objects.filter(number__startswith=f"{prefix}-{today_str}") \
.aggregate(Max('number'))['number__max']
if last_record:
last_number = int(last_record.split('-')[-1]) + 1
else:
last_number = 1
return f"{prefix}-{today_str}-{last_number:04d}"
class MIOItem(BaseModel):
"""

View File

@ -85,7 +85,7 @@ class MIOListSerializer(CustomModelSerializer):
model = MIO
fields = '__all__'
read_only_fields = EXCLUDE_FIELDS + \
['state', 'submit_time', 'submit_user']
['state', 'submit_time', 'submit_user', 'number']
class MIOItemACreateSerializer(CustomModelSerializer):
@ -230,7 +230,8 @@ class MIODoSerializer(CustomModelSerializer):
model = MIO
fields = ['id', 'number', 'note', 'do_user',
'belong_dept', 'type', 'inout_date', 'mgroup', 'mio_user']
extra_kwargs = {'inout_date': {'required': True}, 'do_user': {'required': True}}
extra_kwargs = {'inout_date': {'required': True},
'do_user': {'required': True}, 'number': {"readonly": True}}
def validate(self, attrs):
if 'mgroup' in attrs and attrs['mgroup']:
@ -238,6 +239,7 @@ class MIODoSerializer(CustomModelSerializer):
return attrs
def create(self, validated_data):
validated_data["number"] = MIO.get_a_number(validated_data["type"])
if validated_data['type'] not in [MIO.MIO_TYPE_DO_OUT, MIO.MIO_TYPE_DO_IN]:
raise ValidationError('出入库类型错误')
return super().create(validated_data)
@ -283,10 +285,11 @@ class MIOPurSerializer(CustomModelSerializer):
class Meta:
model = MIO
fields = ['id', 'number', 'note', 'pu_order', 'inout_date', 'supplier', 'mio_user']
extra_kwargs = {'inout_date': {'required': True}}
extra_kwargs = {'inout_date': {'required': True}, 'number': {"readonly": True}}
def create(self, validated_data):
validated_data['type'] = MIO.MIO_TYPE_PUR_IN
validated_data['number'] = MIO.get_a_number(validated_data["type"])
pu_order: PuOrder = validated_data.get('pu_order', None)
if pu_order:
if pu_order.state in [PuOrder.PUORDER_CREATE, PuOrder.PUORDER_DONE]:
@ -309,9 +312,10 @@ class MIOOtherSerializer(CustomModelSerializer):
model = MIO
fields = ['id', 'number', 'note', 'supplier',
'customer', 'type', 'inout_date', 'mio_user']
extra_kwargs = {'inout_date': {'required': True}}
extra_kwargs = {'inout_date': {'required': True}, 'number': {"readonly": True}}
def create(self, validated_data):
validated_data['number'] = MIO.get_a_number(validated_data["type"])
if validated_data['type'] not in [MIO.MIO_TYPE_OTHER_OUT, MIO.MIO_TYPE_OTHER_IN]:
raise ValidationError('出入库类型错误')
return super().create(validated_data)

View File

@ -713,18 +713,19 @@ class MlogbwViewSet(CustomModelViewSet):
wm_in = mlogbin.wm_in
mlog = mlogbin.mlog
div_number = route.div_number
if ins.mlogb_to:
mlogbout = ins.mlogb_to
else:
m_dict = {
"mtask": mlogbin.mtask,
"mlog": mlog,
"batch": ins.number,
"material_out": material_out,
"batch_ofrom": wm_in.batch_ofrom,
"material_ofrom": wm_in.material_ofrom,
"count_real": div_number,
"count_ok": div_number, "qct": mlog.qct
}
if ins.mlogb_to:
mlogbout = ins.mlogb_to
else:
mlogbout = Mlogb.objects.create(**m_dict)
ins.mlogb_to = mlogbout
ins.save(update_fields=["mlogb_to"])