98 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			98 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Python
		
	
	
	
| from django.db.models.expressions import F
 | |
| from django.db.models.signals import post_save
 | |
| from apps.mtm.models import Step, SubprodctionMaterial
 | |
| from apps.pm.models import SubProductionPlan, SubProductionProgress
 | |
| from apps.qm.models import TestRecord
 | |
| from apps.wf.models import Ticket
 | |
| from django.dispatch import receiver
 | |
| from rest_framework import exceptions
 | |
| from apps.wpm.models import WProduct, WproductFlow, WprouctTicket
 | |
| from apps.wpm.models import OperationWproduct
 | |
| from apps.wpm.services import WpmServies
 | |
| 
 | |
| 
 | |
| @receiver(post_save, sender=Ticket)
 | |
| def handleTicket(sender, instance, created, **kwargs):
 | |
|     if instance.workflow.name == '不合格品审理单':
 | |
|         if created:
 | |
|             ticket_data = instance.ticket_data
 | |
|             """
 | |
|             创建关联信息表
 | |
|             """
 | |
|             obj = WprouctTicket()
 | |
|             wproduct = WProduct.objects.get(id=ticket_data['wproduct'])
 | |
|             obj.wproduct = wproduct
 | |
|             obj.number = wproduct.number
 | |
|             obj.material = wproduct.material
 | |
|             obj.step = wproduct.step
 | |
|             obj.subproduction_plan = wproduct.subproduction_plan
 | |
|             obj.ticket = instance
 | |
|             
 | |
|             test_record = TestRecord.objects.filter(wproduct=wproduct, is_deleted=False, is_testok=False).order_by('-id').first()
 | |
|             obj.save()
 | |
| 
 | |
|             # 工单绑定半成品
 | |
|             wproduct.ticket = instance
 | |
|             wproduct.save()
 | |
| 
 | |
|             # 检验员
 | |
|             if not ticket_data.get('tester', None):
 | |
|                 ticket_data['tester'] = test_record.create_by.id
 | |
|                 instance.ticket_data = ticket_data
 | |
|                 instance.save()
 | |
| 
 | |
|         elif instance.act_state == Ticket.TICKET_ACT_STATE_FINISH:
 | |
|             """
 | |
|             执行操作决定
 | |
|             """
 | |
|             ticket_data = instance.ticket_data
 | |
|             wt = WprouctTicket.objects.get(ticket=instance)
 | |
|             wp = wt.wproduct
 | |
|             decision = WProduct.NG_BACK_WORK
 | |
|             
 | |
|             if 'decision_1' in ticket_data and ticket_data['decision_1']:
 | |
|                 decision = ticket_data['decision_1']
 | |
|             elif 'decision_2' in ticket_data and ticket_data['decision_2']:
 | |
|                 decision = ticket_data['decision_2']
 | |
| 
 | |
| 
 | |
|             wp.ng_sign = decision
 | |
|             if decision in [WProduct.NG_BACK_WORK, WProduct.NG_BACK_FIX]:
 | |
|                 step = Step.objects.get(id=ticket_data['back_step'])
 | |
|                 wp.step = step
 | |
|                 # 找到当时所属的计划
 | |
|                 sp = SubProductionPlan.objects.filter(ow_subplan__wproduct=wp, 
 | |
|                 ow_subplan__operation__is_submited=True, ow_subplan__operation__step=step).first()
 | |
|                 if sp:
 | |
|                     wp.subproduction_plan = sp
 | |
|                     wt.save()
 | |
|                     wp.ticket = None # 解除当前工单
 | |
|                     wp.act_state = WProduct.WPR_ACT_STATE_DOWAIT
 | |
|                     wp.save()
 | |
|                     # 更新子计划合格进度
 | |
|                     WpmServies.update_subproduction_progress_main(sp=sp)
 | |
|                     
 | |
|                 else:
 | |
|                     raise exceptions.APIException('返回步骤点错误')
 | |
|     
 | |
|             elif decision in [WProduct.NG_ACCEPT, WProduct.NG_PERMIT]:
 | |
|                 wp.act_state = WProduct.WPR_ACT_STATE_OK
 | |
|                 wp.ng_sign = decision
 | |
|                 wt.save()
 | |
|                 wp.ticket = None # 解除当前工单
 | |
|                 wp.save()
 | |
|             
 | |
| 
 | |
| @receiver(post_save, sender=WProduct)
 | |
| def update_wproduct_log(sender, instance, created, **kwargs):
 | |
|     """
 | |
|     更新产品变动日志
 | |
|     """
 | |
|     # update_fields = kwargs['update_fields']
 | |
|     WproductFlow.objects.filter(wproduct=instance, subproduction_plan=instance.subproduction_plan).update(is_lastlog=False)
 | |
|     ins = WproductFlow()
 | |
|     ins.wproduct = instance
 | |
|     for f in WproductFlow._meta.fields:
 | |
|         if f.name not in ['id', 'create_time', 'update_time', 'wproduct', 'is_lastlog']:
 | |
|             setattr(ins, f.name, getattr(instance, f.name, None))
 | |
|     ins.save() |