123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- from .models import *
- from Info.func import delete_file
- from Info.model_handler import UserHandler
- class VendorHandler():
- @staticmethod
- def get_by_code(code, company):
- try:
- vendor = Vendor.objects.get(code=code, company=company)
- return vendor
- except:
- return None
- @staticmethod
- def search_like_name(keyword, company):
- vendors = Vendor.objects.filter(name__contains = keyword, company=company)
- return vendors
-
- @staticmethod
- def search_by_name(search_name, company):
- vendors = Vendor.objects.filter(company=company, name = search_name)
- return vendors
- class VendorAttachmentHandler():
- @staticmethod
- def get_by_id(ID):
- try:
- attachment = VendorAttachment.objects.get(id=ID)
- return attachment
- except:
- return None
- class PartHandler():
- @staticmethod
- def my_part(user: User, life_cycle=None):
- if life_cycle:
- return Part.objects.filter(owner=user, life_cycle=life_cycle)
- else:
- return Part.objects.filter(owner=user, life_cycle='released')
- @staticmethod
- def get_by_id(ID):
- try:
- part = Part.objects.get(id=ID)
- part = PartHandler.part_ver_display(part)
- return part
- except:
- return None
- @staticmethod
- def exist_pn(pn, company): # 检查料号是否已经使用过
- try:
- part = Part.objects.get(part_number=pn, company=company)
- return part
- except:
- return None
- @staticmethod
- def get_by_pn(pn, company, ver=None):
- try:
- if ver:
- part = Part.objects.get(part_number=pn, company=company, version=ver)
- else:
- parts= Part.objects.filter(part_number=pn, company=company, life_cycle='released').order_by('-version')
- part = parts[0]
- part = PartHandler.part_ver_display(part)
- return part
- except:
- return None
- @staticmethod
- def search_like_pn(keyword, company, lifecycle=None):
- parts = Part.objects.filter(part_number__contains = keyword, company=company)
- if lifecycle:
- parts.filter(life_cycle__in=lifecycle)
- else:
- parts.filter(life_cycle__in=['released'])
- return parts
- @staticmethod
- def my_draft_parts(creator):
- parts = Part.objects.filter(creator=creator, life_cycle='draft')
- return parts
- @staticmethod
- def all_ver_by_pn(part_number, company):
- return Part.objects.filter(part_number=part_number, company=company).order_by('-version')
- @staticmethod
- def new_ver(old_part, company, user):
- current_part = PartHandler.get_by_id(old_part.id)
- new_part = Part()
- new_part.part_number = current_part.part_number
- new_part.description = current_part.description
- new_part.version = current_part.version + 1
- new_part.company = current_part.company
- new_part.creator = user
- new_part.owner = user
- new_part.source_type = current_part.source_type
- new_part.vendor = current_part.vendor
- new_part.sub_parts = current_part.sub_parts
- new_part.vendor_part_number = current_part.vendor_part_number
- new_part.save()
- return new_part
- @staticmethod
- def part_local_CHN(part: Part): # 将part的信息中文化
- if part.life_cycle == 'draft':
- part.life_cycle = '草稿'
- if part.life_cycle == 'sign-off':
- part.life_cycle = '签核中'
- if part.life_cycle == 'released':
- part.life_cycle = '使用中'
- if part.life_cycle == 'expired':
- part.life_cycle = '停用'
- if part.life_cycle == 'EOL':
- part.life_cycle = '不再使用'
- # if part.version < 10:
- # part.version_display = '0'+str(part.version)
- # else:
- # part.version_display = part.version
-
- if part.source_type == 'build':
- part.source_type = '自产'
- if part.source_type == 'purchase':
- part.source_type = '外购'
- # part = PartHandler.part_ver_display(part)
- return part
- @staticmethod
- def part_ver_display(part: Part):
- if part.version < 10:
- part.version_display = '0'+str(part.version)
- else:
- part.version_display = part.version
-
- return part
- @staticmethod
- def expire_old_version(part: Part):
- old_version_parts = Part.objects.filter(version__gt = part.version)
- old_version_parts.update(life_cycle='expired')
- @staticmethod
- def part_list_local_CHN(part_list): # 将part_list的信息中文化
- for part in part_list:
- part = PartHandler.part_local_CHN(part)
- return part_list
- @staticmethod
- def part_search(company, part_number, description, owner, include_EOL=False):
- owner_list = UserHandler.search_like_name(owner, company)
- if include_EOL:
- life_cycle_allowed = ['released', 'EOL']
- else:
- life_cycle_allowed = ['released']
- parts = Part.objects.filter(company=company, part_number__contains = part_number, description__contains = description, owner__in = owner_list, life_cycle__in = life_cycle_allowed)
- for part in parts:
- part = PartHandler.part_ver_display(part)
- return parts
- class PartAttachmentHandler():
- @staticmethod
- def get_by_id(ID):
- try:
- attachment = PartAttachment.objects.get(id=ID)
- return attachment
- except:
- return None
- @staticmethod
- def search_by_part(part):
- attchments = PartAttachment.objects.filter(part=part)
- return attchments
- @staticmethod
- def flush_by_part(part):
- attachments = PartAttachment.objects.filter(part=part)
- for file in attachments:
- delete_file(file.url)
- attachments.delete()
- @staticmethod
- def del_by_id(ID):
- try:
- attachment = PartAttachment.objects.get(id=ID)
- delete_file(attachment.url)
- attachment.delete()
- return True
- except:
- return False
- class BOMHandler():
- @staticmethod
- def retrieve_BOM(parent_part: Part, company: Company):
- BOM_breakdown = BOM.objects.filter(parent_part=parent_part)
- for item in BOM_breakdown:
- part = PartHandler.get_by_pn(item.child_pn, company=company)
- if part:
- item.description = part.description
- else:
- item.description = None
- return BOM_breakdown
- @staticmethod
- def where_used(child_pn, include_EOL=False):
- if include_EOL:
- parent_part_life_cycle_allowed = ['released', 'EOL']
- else:
- parent_part_life_cycle_allowed = ['released']
- where_used_result = BOM.objects.filter(child_pn=child_pn, parent_part__life_cycle__in = parent_part_life_cycle_allowed)
- parent_parts=[]
- for result in where_used_result:
- part = PartHandler.part_ver_display(result.parent_part)
- parent_parts.append(part)
- return parent_parts
- @staticmethod
- def clear_BOM(parent_part):
- BOM.objects.filter(parent_part=parent_part).delete()
- @staticmethod
- def flush_and_save_BOM(parent_part, sub_parts:list,) -> bool:
- try:
- BOMHandler.clear_BOM(parent_part)
- for s_p in sub_parts:
- bom = BOM()
- bom.parent_part = parent_part
- bom.creator = parent_part.creator
- bom.child_pn = s_p['part_number']
- bom.qty = s_p['qty']
- bom.save()
- return True
- except:
- return False
- class EcnHandler:
- @staticmethod
- def generate_ecn_number(company):
- qty = ECN.objects.filter(company=company).count()+1
- ECN_number = str(qty).zfill(8)
- return ECN_number
- @staticmethod
- def get_by_id(ID):
- try:
- ecn = ECN.objects.get(id=ID)
- return ecn
- except:
- return None
- @staticmethod
- def my_ecn(user: User):
- return ECN.objects.filter(creator=user)
-
- class EcnSignOffHandler():
- @staticmethod
- def my_ECN(user, status=None):
- ECNs = EcnSignOff.objects.filter(owner=user)
- if status:
- ECNs = ECNs.filter(status=status)
- return ECNs
- @staticmethod
- def my_pending_eco_count(user):
- ECNs = EcnSignOffHandler.my_ECN(user=user, status='签核中')
- return ECNs.count()
- @staticmethod
- def get_by_owner_ecn(user: User, ecn: ECN): # 通过user和ecn查找签核记录
- try:
- sign_off = EcnSignOff.objects.get(owner=user, ecn=ecn)
- return sign_off
- except:
- return None
- @staticmethod
- def search_by_ecn(ecn: ECN):
- sign_offs = EcnSignOff.objects.filter(ecn=ecn)
- return sign_offs
- class BOMsettingHandler():
- @staticmethod
- def get_attachment_maxsize(company:Company) -> int:
- try:
- BOMsetting = BOMSetting.objects.get(company=company)
- return BOMsetting.attachment_maxsize
- except:
- return None
|