model_handler.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. from .models import *
  2. from Info.func import delete_file
  3. from Info.model_handler import UserHandler
  4. class VendorHandler():
  5. @staticmethod
  6. def get_by_code(code, company):
  7. try:
  8. vendor = Vendor.objects.get(code=code, company=company)
  9. return vendor
  10. except:
  11. return None
  12. @staticmethod
  13. def search_like_name(keyword, company):
  14. vendors = Vendor.objects.filter(name__contains = keyword, company=company)
  15. return vendors
  16. @staticmethod
  17. def search_by_name(search_name, company):
  18. vendors = Vendor.objects.filter(company=company, name = search_name)
  19. return vendors
  20. class VendorAttachmentHandler():
  21. @staticmethod
  22. def get_by_id(ID):
  23. try:
  24. attachment = VendorAttachment.objects.get(id=ID)
  25. return attachment
  26. except:
  27. return None
  28. class PartHandler():
  29. @staticmethod
  30. def my_part(user: User, life_cycle=None):
  31. if life_cycle:
  32. return Part.objects.filter(owner=user, life_cycle=life_cycle)
  33. else:
  34. return Part.objects.filter(owner=user, life_cycle='released')
  35. @staticmethod
  36. def get_by_id(ID):
  37. try:
  38. part = Part.objects.get(id=ID)
  39. part = PartHandler.part_ver_display(part)
  40. return part
  41. except:
  42. return None
  43. @staticmethod
  44. def exist_pn(pn, company): # 检查料号是否已经使用过
  45. try:
  46. part = Part.objects.get(part_number=pn, company=company)
  47. return part
  48. except:
  49. return None
  50. @staticmethod
  51. def get_by_pn(pn, company, ver=None):
  52. try:
  53. if ver:
  54. part = Part.objects.get(part_number=pn, company=company, version=ver)
  55. else:
  56. parts= Part.objects.filter(part_number=pn, company=company, life_cycle='released').order_by('-version')
  57. part = parts[0]
  58. part = PartHandler.part_ver_display(part)
  59. return part
  60. except:
  61. return None
  62. @staticmethod
  63. def search_like_pn(keyword, company, lifecycle=None):
  64. parts = Part.objects.filter(part_number__contains = keyword, company=company)
  65. if lifecycle:
  66. parts.filter(life_cycle__in=lifecycle)
  67. else:
  68. parts.filter(life_cycle__in=['released'])
  69. return parts
  70. @staticmethod
  71. def my_draft_parts(creator):
  72. parts = Part.objects.filter(creator=creator, life_cycle='draft')
  73. return parts
  74. @staticmethod
  75. def all_ver_by_pn(part_number, company):
  76. return Part.objects.filter(part_number=part_number, company=company).order_by('-version')
  77. @staticmethod
  78. def new_ver(old_part, company, user):
  79. current_part = PartHandler.get_by_id(old_part.id)
  80. new_part = Part()
  81. new_part.part_number = current_part.part_number
  82. new_part.description = current_part.description
  83. new_part.version = current_part.version + 1
  84. new_part.company = current_part.company
  85. new_part.creator = user
  86. new_part.owner = user
  87. new_part.source_type = current_part.source_type
  88. new_part.vendor = current_part.vendor
  89. new_part.sub_parts = current_part.sub_parts
  90. new_part.vendor_part_number = current_part.vendor_part_number
  91. new_part.save()
  92. return new_part
  93. @staticmethod
  94. def part_local_CHN(part: Part): # 将part的信息中文化
  95. if part.life_cycle == 'draft':
  96. part.life_cycle = '草稿'
  97. if part.life_cycle == 'sign-off':
  98. part.life_cycle = '签核中'
  99. if part.life_cycle == 'released':
  100. part.life_cycle = '使用中'
  101. if part.life_cycle == 'expired':
  102. part.life_cycle = '停用'
  103. if part.life_cycle == 'EOL':
  104. part.life_cycle = '不再使用'
  105. # if part.version < 10:
  106. # part.version_display = '0'+str(part.version)
  107. # else:
  108. # part.version_display = part.version
  109. if part.source_type == 'build':
  110. part.source_type = '自产'
  111. if part.source_type == 'purchase':
  112. part.source_type = '外购'
  113. # part = PartHandler.part_ver_display(part)
  114. return part
  115. @staticmethod
  116. def part_ver_display(part: Part):
  117. if part.version < 10:
  118. part.version_display = '0'+str(part.version)
  119. else:
  120. part.version_display = part.version
  121. return part
  122. @staticmethod
  123. def expire_old_version(part: Part):
  124. old_version_parts = Part.objects.filter(version__gt = part.version)
  125. old_version_parts.update(life_cycle='expired')
  126. @staticmethod
  127. def part_list_local_CHN(part_list): # 将part_list的信息中文化
  128. for part in part_list:
  129. part = PartHandler.part_local_CHN(part)
  130. return part_list
  131. @staticmethod
  132. def part_search(company, part_number, description, owner, include_EOL=False):
  133. owner_list = UserHandler.search_like_name(owner, company)
  134. if include_EOL:
  135. life_cycle_allowed = ['released', 'EOL']
  136. else:
  137. life_cycle_allowed = ['released']
  138. parts = Part.objects.filter(company=company, part_number__contains = part_number, description__contains = description, owner__in = owner_list, life_cycle__in = life_cycle_allowed)
  139. for part in parts:
  140. part = PartHandler.part_ver_display(part)
  141. return parts
  142. class PartAttachmentHandler():
  143. @staticmethod
  144. def get_by_id(ID):
  145. try:
  146. attachment = PartAttachment.objects.get(id=ID)
  147. return attachment
  148. except:
  149. return None
  150. @staticmethod
  151. def search_by_part(part):
  152. attchments = PartAttachment.objects.filter(part=part)
  153. return attchments
  154. @staticmethod
  155. def flush_by_part(part):
  156. attachments = PartAttachment.objects.filter(part=part)
  157. for file in attachments:
  158. delete_file(file.url)
  159. attachments.delete()
  160. @staticmethod
  161. def del_by_id(ID):
  162. try:
  163. attachment = PartAttachment.objects.get(id=ID)
  164. delete_file(attachment.url)
  165. attachment.delete()
  166. return True
  167. except:
  168. return False
  169. class BOMHandler():
  170. @staticmethod
  171. def retrieve_BOM(parent_part: Part, company: Company):
  172. BOM_breakdown = BOM.objects.filter(parent_part=parent_part)
  173. for item in BOM_breakdown:
  174. part = PartHandler.get_by_pn(item.child_pn, company=company)
  175. if part:
  176. item.description = part.description
  177. else:
  178. item.description = None
  179. return BOM_breakdown
  180. @staticmethod
  181. def where_used(child_pn, include_EOL=False):
  182. if include_EOL:
  183. parent_part_life_cycle_allowed = ['released', 'EOL']
  184. else:
  185. parent_part_life_cycle_allowed = ['released']
  186. where_used_result = BOM.objects.filter(child_pn=child_pn, parent_part__life_cycle__in = parent_part_life_cycle_allowed)
  187. parent_parts=[]
  188. for result in where_used_result:
  189. part = PartHandler.part_ver_display(result.parent_part)
  190. parent_parts.append(part)
  191. return parent_parts
  192. @staticmethod
  193. def clear_BOM(parent_part):
  194. BOM.objects.filter(parent_part=parent_part).delete()
  195. @staticmethod
  196. def flush_and_save_BOM(parent_part, sub_parts:list,) -> bool:
  197. try:
  198. BOMHandler.clear_BOM(parent_part)
  199. for s_p in sub_parts:
  200. bom = BOM()
  201. bom.parent_part = parent_part
  202. bom.creator = parent_part.creator
  203. bom.child_pn = s_p['part_number']
  204. bom.qty = s_p['qty']
  205. bom.save()
  206. return True
  207. except:
  208. return False
  209. class EcnHandler:
  210. @staticmethod
  211. def generate_ecn_number(company):
  212. qty = ECN.objects.filter(company=company).count()+1
  213. ECN_number = str(qty).zfill(8)
  214. return ECN_number
  215. @staticmethod
  216. def get_by_id(ID):
  217. try:
  218. ecn = ECN.objects.get(id=ID)
  219. return ecn
  220. except:
  221. return None
  222. @staticmethod
  223. def my_ecn(user: User):
  224. return ECN.objects.filter(creator=user)
  225. class EcnSignOffHandler():
  226. @staticmethod
  227. def my_ECN(user, status=None):
  228. ECNs = EcnSignOff.objects.filter(owner=user)
  229. if status:
  230. ECNs = ECNs.filter(status=status)
  231. return ECNs
  232. @staticmethod
  233. def my_pending_eco_count(user):
  234. ECNs = EcnSignOffHandler.my_ECN(user=user, status='签核中')
  235. return ECNs.count()
  236. @staticmethod
  237. def get_by_owner_ecn(user: User, ecn: ECN): # 通过user和ecn查找签核记录
  238. try:
  239. sign_off = EcnSignOff.objects.get(owner=user, ecn=ecn)
  240. return sign_off
  241. except:
  242. return None
  243. @staticmethod
  244. def search_by_ecn(ecn: ECN):
  245. sign_offs = EcnSignOff.objects.filter(ecn=ecn)
  246. return sign_offs
  247. class BOMsettingHandler():
  248. @staticmethod
  249. def get_attachment_maxsize(company:Company) -> int:
  250. try:
  251. BOMsetting = BOMSetting.objects.get(company=company)
  252. return BOMsetting.attachment_maxsize
  253. except:
  254. return None