views.py 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. from django.db import transaction, DatabaseError
  2. import datetime
  3. # Create your views here.
  4. # 查询制程信息
  5. from django.http import Http404
  6. from rest_framework import status
  7. from rest_framework.authentication import SessionAuthentication
  8. from rest_framework.permissions import IsAuthenticated
  9. from rest_framework.response import Response
  10. from rest_framework.views import APIView
  11. from rest_framework_jwt.authentication import JSONWebTokenAuthentication
  12. from utils.monthly_odd import monthly_odd
  13. from utils.executeQuery import executeQuery,IseUpDelQuery,rpoold
  14. from utils.filters import filter
  15. from utils.ClssSql import ClassSqls
  16. from .models import Django_mf_bg,Django_tf_bg
  17. from .bgutils import uptz
  18. from .utils import MyException
  19. # 过滤通知单信息
  20. class TzInfo(APIView):
  21. # 1,设置局部认证
  22. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  23. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  24. # 2,设置局部权限
  25. permission_classes = (IsAuthenticated,)
  26. # permission_classes = (AllowAny,) #任何用户都能访问
  27. def get(self, request):
  28. params = request.query_params
  29. mo_no = params.get('mo_no')
  30. mo_no = mo_no.replace('\n', '') #去除换行
  31. mo_no = mo_no.replace(' ', '') #去除空格
  32. zc_no = params.get('zc_no')
  33. bg_id = params.get('bg_id')
  34. zc_no_dn = params.get('zc_no_dn')
  35. if bg_id is None:
  36. return Response('单据类别异常', status=status.HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE)
  37. if zc_no is None or mo_no is None:
  38. return Response('参数异常', status=status.HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE)
  39. if bg_id=='JS':
  40. sql = ClassSqls.TzInfoJs
  41. elif bg_id=='PG':
  42. sql = ClassSqls.TzInfoPg
  43. elif bg_id=='SH':
  44. sql = ClassSqls.TzInfoSh
  45. else:
  46. # 都不成立那么就是转移单('ZY')
  47. if mo_no:
  48. sql = ClassSqls.TzInfoZy
  49. else:
  50. sql = ClassSqls.TzInfoZy1
  51. # 判断单号是否多个
  52. ismulti=';' in mo_no
  53. # 用于存储结案掉的单据
  54. error_mo = []
  55. # 用户存储结果集
  56. result = []
  57. if ismulti:
  58. mo_no=list(set(mo_no.split(';')))
  59. for i in mo_no:
  60. if bg_id=='JS':
  61. row_result = executeQuery(sql.format(zc_no))
  62. if bg_id=='ZY':
  63. row_result = executeQuery(sql.format(zc_no, i,zc_no_dn))
  64. else:
  65. row_result = executeQuery(sql.format(zc_no, i))
  66. if len(row_result)==0:
  67. error_mo.append(i)
  68. else:
  69. for row in row_result:
  70. result.append(row)
  71. else:
  72. if bg_id == 'JS':
  73. result = executeQuery(sql.format(zc_no))
  74. elif bg_id == 'ZY':
  75. if mo_no:
  76. result = executeQuery(sql.format(zc_no, mo_no, zc_no_dn))
  77. else:
  78. result = executeQuery(sql.format(zc_no, zc_no_dn))
  79. else:
  80. result = executeQuery(sql.format(zc_no,mo_no))
  81. if len(result) == 0 or result is None:
  82. error_mo.append(mo_no)
  83. # 如果没有查询到单据直接返回
  84. if len(result) == 0:
  85. data = {
  86. "msg":"没有到单据",
  87. "error_mo": error_mo
  88. }
  89. return Response(data, status=status.HTTP_200_OK)
  90. # 拼接数据的返回项次
  91. for itm in range(len(result)):
  92. result[itm]['itm']=itm+1
  93. data = {
  94. "msg":"过滤成功",
  95. "result":result,
  96. "error_mo":error_mo
  97. }
  98. return Response(data, status=status.HTTP_200_OK)
  99. # 接收单
  100. class Bg(APIView):
  101. # 1,设置局部认证
  102. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  103. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  104. # 2,设置局部权限
  105. permission_classes = (IsAuthenticated,)
  106. def get(self,request):
  107. params = request.query_params
  108. # 如果单号存在那么就是查询单据,如果单号不存在就是弹窗查询
  109. # 如果单号存在则按单号进行查询数据
  110. filter_no = params.get('filter_no','')
  111. if filter_no:
  112. # 查询表头信息
  113. mf_sql = ClassSqls.CommonMf_sql.format(filter_no)
  114. mf_result = executeQuery(mf_sql)
  115. if len(mf_result)>1 or len(mf_result)==0:
  116. return Response('查询表头数据异常',status=status.HTTP_206_PARTIAL_CONTENT)
  117. # 查询表身信息
  118. tf_sql = ClassSqls.CommonTf_sql.format(filter_no)
  119. tf_result = executeQuery(tf_sql)
  120. if len(tf_result) == 0:
  121. return Response('查询表表身数据异常', status=status.HTTP_206_PARTIAL_CONTENT)
  122. # 计算应生产量
  123. for i in range(len(tf_result)):
  124. yscl_sql = ClassSqls.BgYscl.format(tf_result[i].get('zt_no'))
  125. yscl_qty = executeQuery(yscl_sql)[0]
  126. if tf_result[i]['zt_no'] == yscl_qty.get('tz_no'):
  127. print(print(yscl_qty.get('qty')))
  128. print(tf_result[i]['qty'])
  129. tf_result[i]['yscl'] = yscl_qty.get('qty')
  130. tf_result[i]['maxqty'] = yscl_qty.get('qty')+tf_result[i]['qty']
  131. tf_result[i]['totalqty'] = yscl_qty.get('totalqty')
  132. # 返回单据查询成功的数据
  133. # print(tf_result)
  134. data = {
  135. "mf_result":mf_result[0],
  136. "tf_result":tf_result,
  137. "msg":"查询成功"
  138. }
  139. return Response(data,status=status.HTTP_200_OK)
  140. to_date = params.get('to_date')#开始日期
  141. end_date = params.get('end_date') #结束日期
  142. bg_id = params.get('bg_id') #单据类别
  143. select_zc_no = params.get('select_zc_no') #制程代号
  144. # print(to_date)
  145. # print(end_date)
  146. condition = 'where left(Convert(varchar(100), no_dd, 23), 11)>=' + "'" + to_date + "'" + ' and left(Convert(varchar(100), no_dd, 23), 11)<=' + "'" + end_date + "'" + ' and bg_id=' + "'" + bg_id + "'"
  147. # 获取账号等级
  148. is_superuser = request.user.is_superuser
  149. # 获取登录用户
  150. users = request.user
  151. if is_superuser==False:
  152. condition = condition +" and create_user="+"'"+str(users)+"'"
  153. if select_zc_no:
  154. condition = condition + ' and zc_no='+ "'"+select_zc_no+"'"
  155. sql1 = ClassSqls.CommonFilter.format(condition)
  156. result = executeQuery(sql1)
  157. if len(result)<=0:
  158. data = {
  159. "result": result,
  160. "msg": "没有查询到数据"
  161. }
  162. return Response(data, status=status.HTTP_200_OK)
  163. data = {
  164. "result":result,
  165. "msg":"查询成功"
  166. }
  167. return Response(data,status=status.HTTP_200_OK)
  168. @transaction.atomic
  169. def post(self,request):
  170. # 1、获取参数
  171. data = request.data
  172. # 单据日期
  173. no_dd = data['no_dd']
  174. # 制程代号
  175. zc_no = data['zc_no']
  176. if zc_no is None:
  177. return Response("制程代码不能为空", status=status.HTTP_200_OK)
  178. # 创建日期
  179. create_user = data['create_user']
  180. # 创建用户
  181. username = data['username']
  182. #单据类别
  183. bg_id = data['bg_id']
  184. # 作业人员
  185. sal_no = data['sal_no']
  186. # 制令单号
  187. mo_no=data['mo_no']
  188. # 表身数据
  189. dataList = data['dataList']
  190. # 2、处理生成单号
  191. # 每次新增之前先把通知单没有自定栏位的单号插入到自定义表
  192. insert_sql = ClassSqls.CommonSql_z
  193. IseUpDelQuery(insert_sql)
  194. sql1 = ClassSqls.CommonSqlNo.format(bg_id)
  195. bg_no = bg_id+monthly_odd(sql1)
  196. # 判断单号是否重复
  197. ifbg_no_sql = ClassSqls.CommonIfoold.format(bg_no,bg_id)
  198. ifbg_no_qty = rpoold(ifbg_no_sql)
  199. if int(ifbg_no_qty)>0:
  200. data = {
  201. "msg": "单号重复请重新做单",
  202. }
  203. return Response(data, status=status.HTTP_200_OK)
  204. sid = transaction.savepoint() # 开启事物
  205. # 3、插入表头数据
  206. mf_bg = Django_mf_bg(bg_no=bg_no,no_dd=no_dd,bg_id=bg_id,zc_no=zc_no,sal_no=sal_no,create_user=username,mo_no=mo_no,create_time=datetime.datetime.now())
  207. mf_bg.save()
  208. # 4、插入表身数据
  209. # 查询外键
  210. fordata = Django_mf_bg.objects.get(bg_no=bg_no)
  211. for i in dataList:
  212. # 判断是否超
  213. if i['qty']>i['yscl']:
  214. data = {
  215. "msg": "新增失败,存在超出数量项次",
  216. }
  217. return Response(data, status=status.HTTP_200_OK)
  218. # filter(i['cc']),此方法是用来格式化数据的,如果数据为none则返回空
  219. prd_name = filter(i['prd_name'])
  220. cc = filter(i['cc'])
  221. zy = filter(i['zy'])
  222. zl = filter(i['zl'])
  223. ms = filter(i['ms'])
  224. rem = filter(i['rem'])
  225. dd = filter(i['dd'])
  226. prd_rem = filter(i['prd_rem'])
  227. zc_no_up = filter(i['zc_no_up'])
  228. zc_no_dn = filter(i['zc_no_dn'])
  229. spc_no = i['spc_no']
  230. tf_bg = Django_tf_bg(bg_no=fordata,no_dd=no_dd,itm=i['itm'],bg_id=bg_id,mo_no=i['mo_no'],zt_no=i['zt_no'],prd_no=i['prd_no'],
  231. prd_name=prd_name,cc=cc,zy=zy,qty=i['qty'],zl=zl,ms=ms,rem=rem,dd=dd,prd_rem=prd_rem,zc_no_end=zc_no_dn,zc_no_up=zc_no_up)
  232. tf_bg.save()
  233. # 如果spc_no为1的时候则跳过派工和收货
  234. if str(spc_no) == '1':
  235. update_sql = ClassSqls.BgUpSql1.format(i['zt_no'], bg_id)
  236. else:
  237. update_sql = ClassSqls.BgUpSql.format(i['zt_no'], bg_id)
  238. IseUpDelQuery(update_sql)
  239. transaction.savepoint_commit(sid) # 提交事物
  240. # 5、返回响应
  241. data = {
  242. "msg":"新增成功",
  243. "bg_no":bg_no
  244. }
  245. return Response(data,status=status.HTTP_200_OK)
  246. @transaction.atomic
  247. def put(self,request):
  248. # 1、获取参数
  249. data = request.data
  250. bg_id = data.get('bg_id')
  251. bg_no = data.get('bg_no')
  252. # print(data)
  253. # 2、修改表头
  254. sid = transaction.savepoint() # 开启事物
  255. mf_bg = Django_mf_bg.objects.get(bg_id=bg_id,bg_no=bg_no)
  256. mf_bg.no_dd = data.get('no_dd')
  257. mf_bg.sal_no = data.get('sal_no')
  258. mf_bg.mo_no = data.get('mo_no')
  259. mf_bg.save()
  260. # 3、修改表身
  261. for i in request.data.get('dataList'):
  262. # print(i)
  263. # 判断是否超
  264. if i['qty'] > i['maxqty']:
  265. data = {
  266. "msg": "修改失败,存在超出数量项次",
  267. }
  268. return Response(data, status=status.HTTP_200_OK)
  269. mf_bg = Django_tf_bg.objects.get(bg_id = bg_id,bg_no_id = bg_no,itm = i.get('itm'))
  270. mf_bg.qty = i.get('qty')
  271. mf_bg.zl = i.get('zl')
  272. mf_bg.save()
  273. if str(i.get('spc_no'))=='1':
  274. update_sql = ClassSqls.BgUpSql1.format(i['zt_no'], bg_id)
  275. else:
  276. update_sql = ClassSqls.BgUpSql.format(i['zt_no'], bg_id)
  277. IseUpDelQuery(update_sql)
  278. transaction.savepoint_commit(sid) # 提交事物
  279. data={
  280. "msg":"修改成功"
  281. }
  282. return Response(data,status=status.HTTP_200_OK)
  283. @transaction.atomic
  284. def delete(self, request):
  285. # 1、获取参数
  286. data = request.data
  287. scope = data.get('scope')
  288. bg_no = data.get('bg_no')
  289. # 是否删除行标志
  290. sign = data.get('sign')
  291. if scope is None and bg_no is None:
  292. data = {
  293. "msg": "删除失败"
  294. }
  295. return Response(data, status=status.HTTP_304_NOT_MODIFIED)
  296. if bg_no and sign is None:
  297. # 删除表身
  298. sid = transaction.savepoint() # 开启事物
  299. # 查询删除表身的通知单号
  300. sel_sql = ClassSqls.CommonTfSql.format(bg_no)
  301. del_zt_no = executeQuery(sel_sql)
  302. Django_tf_bg.objects.filter(bg_no_id=bg_no).delete()
  303. Django_mf_bg.objects.filter(bg_no=bg_no).delete()
  304. # print(del_zt_no)
  305. for i in del_zt_no:
  306. # 查询应生产量
  307. yscl_sql = ClassSqls.BgYscl.format(i.get('zt_no'))
  308. yscl_qty = executeQuery(yscl_sql)[0]
  309. if i.get('qty') > yscl_qty['jsqty']-yscl_qty['zyqty']:
  310. transaction.savepoint_rollback(sid)
  311. data = {
  312. "msg": "已转后续单据不允许删除"
  313. }
  314. return Response(data, status=status.HTTP_201_CREATED)
  315. if str(i.get('spc_no'))=='1':
  316. update_sql = ClassSqls.BgUpdel1.format(i.get('qty'), i.get('zl'),i.get('zt_no'),i.get('qty'),i.get('qty'))
  317. else:
  318. update_sql = ClassSqls.BgUpdel.format(i.get('qty'), i.get('zl'),i.get('zt_no'))
  319. IseUpDelQuery(update_sql)
  320. transaction.savepoint_commit(sid) # 提交事物
  321. else:
  322. sid = transaction.savepoint() # 开启事物
  323. try:
  324. # 删除的时候要先查询出删除数据的的数量,查询单据的数量和重量
  325. old_data = Django_tf_bg.objects.get(zt_no=scope.get('zt_no'), bg_id=scope.get('bg_id'),bg_no_id=scope.get('bg_no'),itm=scope.get('itm'))
  326. Django_tf_bg.objects.get(bg_no_id=scope.get('bg_no'), itm=scope.get('itm')).delete()
  327. # 查询应生产量
  328. yscl_sql = ClassSqls.BgYscl.format(old_data.get('zt_no'))
  329. yscl_qty = executeQuery(yscl_sql)[0]
  330. if old_data.get('qty') > yscl_qty['jsqty'] - yscl_qty['zyqty']:
  331. transaction.savepoint_rollback(sid)
  332. data = {
  333. "msg": "已转后续单据不允许删除"
  334. }
  335. return Response(data, status=status.HTTP_201_CREATED)
  336. if int(scope.get('spc_no'))==1:
  337. update_sql = ClassSqls.BgUpdel1.format(old_data.qty, old_data.zl, scope.get('zt_no'),old_data.qty,old_data.qty)
  338. else:
  339. update_sql = ClassSqls.BgUpdel.format(old_data.qty, old_data.zl, scope.get('zt_no'))
  340. IseUpDelQuery(update_sql)
  341. except:
  342. data = {
  343. "msg": "删除失败"
  344. }
  345. return Response(data, status=status.HTTP_200_OK)
  346. transaction.savepoint_commit(sid) # 提交事物
  347. data = {
  348. "msg": "删除成功"
  349. }
  350. return Response(data, status=status.HTTP_200_OK)
  351. # 派工单
  352. class Pg(APIView):
  353. # 1,设置局部认证
  354. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  355. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  356. # 2,设置局部权限
  357. permission_classes = (IsAuthenticated,)
  358. def get(self,request):
  359. params = request.query_params
  360. # 如果单号存在那么就是查询单据,如果单号不存在就是弹窗查询
  361. # 如果单号存在则按单号进行查询数据
  362. filter_no = params.get('filter_no','')
  363. if filter_no:
  364. # 查询表头信息
  365. mf_sql = ClassSqls.CommonMf_sql.format(filter_no)
  366. mf_result = executeQuery(mf_sql)
  367. if len(mf_result)>1 or len(mf_result)==0:
  368. return Response('查询表头数据异常',status=status.HTTP_206_PARTIAL_CONTENT)
  369. # 查询表身信息
  370. tf_sql = ClassSqls.CommonTf_sql.format(filter_no)
  371. tf_result = executeQuery(tf_sql)
  372. if len(tf_result) == 0:
  373. return Response('查询表表身数据异常', status=status.HTTP_206_PARTIAL_CONTENT)
  374. # 计算应生产量
  375. for i in range(len(tf_result)):
  376. yscl_sql = ClassSqls.PgYscl.format(tf_result[i].get('zt_no'))
  377. print(yscl_sql)
  378. yscl_qty = executeQuery(yscl_sql)[0]
  379. if tf_result[i]['zt_no']==yscl_qty.get('tz_no'):
  380. tf_result[i]['yscl'] = yscl_qty.get('qty')
  381. tf_result[i]['maxqty'] = yscl_qty.get('qty') + tf_result[i]['qty']
  382. tf_result[i]['totalqty'] = yscl_qty.get('totalqty')
  383. # 返回单据查询成功的数据
  384. data = {
  385. "mf_result":mf_result[0],
  386. "tf_result":tf_result,
  387. "msg":"查询成功"
  388. }
  389. return Response(data,status=status.HTTP_200_OK)
  390. to_date = params.get('to_date')#开始日期
  391. end_date = params.get('end_date') #结束日期
  392. bg_id = params.get('bg_id') #单据类别
  393. select_zc_no = params.get('select_zc_no') #制程代号
  394. condition = 'where left(Convert(varchar(100), no_dd, 23), 11)>=' + "'" + to_date + "'" + ' and left(Convert(varchar(100), no_dd, 23), 11)<=' + "'" + end_date + "'" + ' and bg_id=' + "'" + bg_id + "'"
  395. # 获取账号等级
  396. is_superuser = request.user.is_superuser
  397. # 获取登录用户
  398. users = request.user
  399. if is_superuser == False:
  400. condition = condition + " and create_user=" + "'" + str(users) + "'"
  401. if select_zc_no:
  402. condition = condition + ' and zc_no=' + "'" + select_zc_no + "'"
  403. sql1 = ClassSqls.CommonFilter.format(condition)
  404. result = executeQuery(sql1)
  405. if len(result) <= 0:
  406. data = {
  407. "result": result,
  408. "msg": "没有查询到数据"
  409. }
  410. return Response(data, status=status.HTTP_200_OK)
  411. data = {
  412. "result":result,
  413. "msg":"查询成功"
  414. }
  415. return Response(data,status=status.HTTP_200_OK)
  416. @transaction.atomic
  417. def post(self,request):
  418. # 1、获取参数
  419. data = request.data
  420. # 单据日期
  421. no_dd = data['no_dd']
  422. # 制程代号
  423. zc_no = data['zc_no']
  424. if zc_no is None:
  425. return Response("制程代码不能为空", status=status.HTTP_200_OK)
  426. # 创建日期
  427. create_user = data['create_user']
  428. # 创建用户
  429. username = data['username']
  430. #单据类别
  431. bg_id = data['bg_id']
  432. # 作业人员
  433. sal_no = data['sal_no']
  434. # 制令单号
  435. mo_no=data['mo_no']
  436. # 表身数据
  437. dataList = data['dataList']
  438. # print(dataList)
  439. # 2、处理生成单号
  440. # 每次新增之前先把通知单没有自定栏位的单号插入到自定义表
  441. insert_sql = ClassSqls.CommonSql_z
  442. IseUpDelQuery(insert_sql)
  443. sql1 = ClassSqls.CommonSqlNo.format(bg_id)
  444. bg_no = bg_id+monthly_odd(sql1)
  445. # 判断单号是否重复
  446. ifbg_no_sql = ClassSqls.CommonIfoold.format(bg_no, bg_id)
  447. ifbg_no_qty = rpoold(ifbg_no_sql)
  448. if int(ifbg_no_qty) > 0:
  449. data = {
  450. "msg": "单号重复请重新做单",
  451. }
  452. return Response(data, status=status.HTTP_200_OK)
  453. # sid = transaction.savepoint() # 开启事物
  454. save_id = transaction.savepoint() #
  455. # 3、插入表头数据
  456. mf_bg = Django_mf_bg(bg_no=bg_no,no_dd=no_dd,bg_id=bg_id,zc_no=zc_no,sal_no=sal_no,create_user=username,mo_no=mo_no,create_time=datetime.datetime.now())
  457. mf_bg.save()
  458. # 4、插入表身数据
  459. # 查询外键
  460. fordata = Django_mf_bg.objects.get(bg_no=bg_no)
  461. for i in dataList:
  462. # 判断是否超
  463. if i['qty'] > i['yscl']:
  464. data = {
  465. "msg": "新增失败,存在超出数量项次",
  466. }
  467. return Response(data, status=status.HTTP_200_OK)
  468. selPg = ClassSqls.PgSelSql.format(i['zt_no'])
  469. selold = executeQuery(selPg)[0]
  470. if selold['yscl']<selold['qty_pg_lx']+i['qty']:
  471. # 回滚事物
  472. transaction.savepoint_rollback(save_id)
  473. data = {
  474. "msg": "超出或者重复扫单超出了应发量",
  475. }
  476. return Response(data, status=status.HTTP_200_OK)
  477. # filter(i['cc']),此方法是用来格式化数据的,如果数据为none则返回空
  478. prd_name = filter(i['prd_name'])
  479. cc = filter(i['cc'])
  480. zy = filter(i['zy'])
  481. zl = filter(i['zl'])
  482. ms = filter(i['ms'])
  483. rem = filter(i['rem'])
  484. dd = filter(i['dd'])
  485. prd_rem = filter(i['prd_rem'])
  486. zc_no_up = filter(i['zc_no_up'])
  487. zc_no_dn = filter(i['zc_no_dn'])
  488. tf_bg = Django_tf_bg(bg_no=fordata, no_dd=no_dd, itm=i['itm'], bg_id=bg_id, mo_no=i['mo_no'],zt_no=i['zt_no'], prd_no=i['prd_no'],
  489. prd_name=prd_name, cc=cc, zy=zy, qty=i['qty'], zl=zl, ms=ms, rem=rem, dd=dd,prd_rem=prd_rem, zc_no_end=zc_no_dn, zc_no_up=zc_no_up)
  490. tf_bg.save()
  491. update_sql = ClassSqls.PgUpSql.format(i['zt_no'], bg_id)
  492. IseUpDelQuery(update_sql)
  493. transaction.savepoint_commit(save_id) # 提交事物
  494. # 5、返回响应
  495. data = {
  496. "msg":"新增成功",
  497. "bg_no":bg_no
  498. }
  499. return Response(data,status=status.HTTP_200_OK)
  500. @transaction.atomic
  501. def put(self,request):
  502. # 1、获取参数
  503. data = request.data
  504. bg_id = data.get('bg_id')
  505. bg_no = data.get('bg_no')
  506. # print(data)
  507. # 2、修改表头
  508. sid = transaction.savepoint() # 开启事物
  509. mf_bg = Django_mf_bg.objects.get(bg_id=bg_id,bg_no=bg_no)
  510. mf_bg.no_dd = data.get('no_dd')
  511. mf_bg.sal_no = data.get('sal_no')
  512. mf_bg.mo_no = data.get('mo_no')
  513. mf_bg.save()
  514. # 3、修改表身
  515. for i in request.data.get('dataList'):
  516. print(i)
  517. # 判断是否超
  518. if i['qty'] > i['maxqty']:
  519. data = {
  520. "msg": "新增失败,存在超出数量项次",
  521. }
  522. return Response(data, status=status.HTTP_200_OK)
  523. mf_bg = Django_tf_bg.objects.get(bg_id = bg_id,bg_no_id = bg_no,itm = i.get('itm'))
  524. mf_bg.qty = i.get('qty')
  525. mf_bg.zl = i.get('zl')
  526. mf_bg.save()
  527. update_sql = ClassSqls.PgUpSql.format(i['zt_no'], bg_id)
  528. IseUpDelQuery(update_sql)
  529. transaction.savepoint_commit(sid) # 提交事物
  530. data={
  531. "msg":"修改成功"
  532. }
  533. return Response(data,status=status.HTTP_200_OK)
  534. @transaction.atomic
  535. def delete(self, request):
  536. # 1、获取参数
  537. data = request.data
  538. scope = data.get('scope')
  539. bg_no = data.get('bg_no')
  540. # 是否删除行标志
  541. sign = data.get('sign')
  542. if scope is None and bg_no is None:
  543. data = {
  544. "msg": "删除失败"
  545. }
  546. return Response(data, status=status.HTTP_304_NOT_MODIFIED)
  547. if bg_no and sign is None:
  548. # 删除表身
  549. sid = transaction.savepoint() # 开启事物
  550. # 查询删除表身的通知单号
  551. sel_sql = ClassSqls.CommonTfSql.format(bg_no)
  552. del_zt_no = executeQuery(sel_sql)
  553. Django_tf_bg.objects.filter(bg_no_id=bg_no).delete()
  554. Django_mf_bg.objects.filter(bg_no=bg_no).delete()
  555. # print(del_zt_no)
  556. for i in del_zt_no:
  557. # 查询应生产量
  558. yscl_sql = ClassSqls.PgYscl.format(i['zt_no'])
  559. yscl_qty = executeQuery(yscl_sql)[0]
  560. if i['qty'] > yscl_qty['pgqty'] - yscl_qty['shqty']:
  561. transaction.savepoint_rollback(sid)
  562. data = {
  563. "msg": "已转后续单据不允许删除"
  564. }
  565. return Response(data, status=status.HTTP_201_CREATED)
  566. update_sql = ClassSqls.PgUpdel.format(i.get('qty'), i.get('zl'), i.get('zt_no'))
  567. IseUpDelQuery(update_sql)
  568. transaction.savepoint_commit(sid) # 提交事物
  569. else:
  570. sid = transaction.savepoint() # 开启事物
  571. try:
  572. # 删除的时候要先查询出删除数据的的数量,查询单据的数量和重量
  573. old_data = Django_tf_bg.objects.get(zt_no=scope.get('zt_no'), bg_id=scope.get('bg_id'),bg_no_id=scope.get('bg_no'),itm=scope.get('itm'))
  574. # 查询应生产量
  575. yscl_sql = ClassSqls.PgYscl.format(scope.get('zt_no'))
  576. yscl_qty = executeQuery(yscl_sql)[0]
  577. if scope.get['qty'] <= yscl_qty['pgqty'] - yscl_qty['shqty']:
  578. transaction.savepoint_rollback(sid)
  579. data = {
  580. "msg": "已转后续单据不允许删除"
  581. }
  582. return Response(data, status=status.HTTP_201_CREATED)
  583. Django_tf_bg.objects.filter(bg_no_id=scope.get('bg_no'), itm=scope.get('itm')).delete()
  584. update_sql = ClassSqls.PgUpdel.format(old_data.qty, old_data.zl, scope.get('zt_no'))
  585. IseUpDelQuery(update_sql)
  586. except:
  587. data = {
  588. "msg": "删除失败"
  589. }
  590. return Response(data, status=status.HTTP_200_OK)
  591. transaction.savepoint_commit(sid) # 提交事物
  592. data = {
  593. "msg": "删除成功"
  594. }
  595. return Response(data, status=status.HTTP_200_OK)
  596. # 收货单
  597. class Sh(APIView):
  598. # 1,设置局部认证
  599. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  600. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  601. # 2,设置局部权限
  602. permission_classes = (IsAuthenticated,)
  603. def get(self,request):
  604. params = request.query_params
  605. # 如果单号存在那么就是查询单据,如果单号不存在就是弹窗查询
  606. # 如果单号存在则按单号进行查询数据
  607. filter_no = params.get('filter_no','')
  608. if filter_no:
  609. # 查询表头信息
  610. mf_sql = ClassSqls.CommonMf_sql.format(filter_no)
  611. mf_result = executeQuery(mf_sql)
  612. if len(mf_result)>1 or len(mf_result)==0:
  613. return Response('查询表头数据异常',status=status.HTTP_206_PARTIAL_CONTENT)
  614. # 查询表身信息
  615. tf_sql = ClassSqls.CommonTf_sql.format(filter_no)
  616. tf_result = executeQuery(tf_sql)
  617. if len(tf_result) == 0:
  618. return Response('查询表表身数据异常', status=status.HTTP_206_PARTIAL_CONTENT)
  619. # 计算应生产量
  620. for i in range(len(tf_result)):
  621. yscl_sql = ClassSqls.ShYscl.format(tf_result[i].get('zt_no'))
  622. # print(yscl_sql)
  623. # print(1111)
  624. yscl_qty = executeQuery(yscl_sql)[0]
  625. if tf_result[i]['zt_no']==yscl_qty.get('tz_no'):
  626. tf_result[i]['yscl'] = yscl_qty.get('qty')
  627. tf_result[i]['maxqty'] = yscl_qty.get('qty') + tf_result[i]['qty']
  628. tf_result[i]['totalqty'] = yscl_qty.get('totalqty')
  629. # 返回单据查询成功的数据
  630. print(tf_result)
  631. data = {
  632. "mf_result":mf_result[0],
  633. "tf_result":tf_result,
  634. "msg":"查询成功"
  635. }
  636. return Response(data,status=status.HTTP_200_OK)
  637. to_date = params.get('to_date')#开始日期
  638. end_date = params.get('end_date') #结束日期
  639. bg_id = params.get('bg_id') #单据类别
  640. select_zc_no = params.get('select_zc_no') #制程代号
  641. condition = 'where left(Convert(varchar(100), no_dd, 23), 11)>=' + "'" + to_date + "'" + ' and left(Convert(varchar(100), no_dd, 23), 11)<=' + "'" + end_date + "'" + ' and bg_id=' + "'" + bg_id + "'"
  642. # 获取账号等级
  643. is_superuser = request.user.is_superuser
  644. # 获取登录用户
  645. users = request.user
  646. if is_superuser == False:
  647. condition = condition + " and create_user=" + "'" + str(users) + "'"
  648. if select_zc_no:
  649. condition = condition + ' and zc_no=' + "'" + select_zc_no + "'"
  650. sql1 = ClassSqls.CommonFilter.format(condition)
  651. result = executeQuery(sql1)
  652. data = {
  653. "result":result,
  654. "msg":"查询成功"
  655. }
  656. return Response(data,status=status.HTTP_200_OK)
  657. @transaction.atomic
  658. def post(self,request):
  659. # 1、获取参数
  660. data = request.data
  661. # 单据日期
  662. no_dd = data['no_dd']
  663. # 制程代号
  664. zc_no = data['zc_no']
  665. if zc_no is None:
  666. return Response("制程代码不能为空", status=status.HTTP_200_OK)
  667. # 创建日期
  668. create_user = data['create_user']
  669. # 创建用户
  670. username = data['username']
  671. #单据类别
  672. bg_id = data['bg_id']
  673. # 作业人员
  674. sal_no = data['sal_no']
  675. # 制令单号
  676. mo_no=data['mo_no']
  677. # 表身数据
  678. dataList = data['dataList']
  679. # print(dataList)
  680. # 2、处理生成单号
  681. # 每次新增之前先把通知单没有自定栏位的单号插入到自定义表
  682. insert_sql = ClassSqls.CommonSql_z
  683. IseUpDelQuery(insert_sql)
  684. sql1 = ClassSqls.CommonSqlNo.format(bg_id)
  685. bg_no = bg_id+monthly_odd(sql1)
  686. # 判断单号是否重复
  687. ifbg_no_sql = ClassSqls.CommonIfoold.format(bg_no, bg_id)
  688. ifbg_no_qty = rpoold(ifbg_no_sql)
  689. if int(ifbg_no_qty) > 0:
  690. data = {
  691. "msg": "单号重复请重新做单",
  692. }
  693. return Response(data, status=status.HTTP_200_OK)
  694. sid = transaction.savepoint() # 开启事物
  695. # 3、插入表头数据
  696. mf_bg = Django_mf_bg(bg_no=bg_no,no_dd=no_dd,bg_id=bg_id,zc_no=zc_no,sal_no=sal_no,create_user=username,mo_no=mo_no,create_time=datetime.datetime.now())
  697. mf_bg.save()
  698. # 4、插入表身数据
  699. # 查询外键
  700. fordata = Django_mf_bg.objects.get(bg_no=bg_no)
  701. for i in dataList:
  702. # 判断是否超
  703. if i['qty'] > i['yscl']:
  704. data = {
  705. "msg": "新增失败,存在超出数量项次",
  706. }
  707. return Response(data, status=status.HTTP_200_OK)
  708. selPg = ClassSqls.ShSelSql.format(i['zt_no'])
  709. selold = executeQuery(selPg)[0]
  710. if selold['yscl'] < selold['qty_sh_lx'] + i['qty']:
  711. # 回滚事物
  712. transaction.savepoint_rollback(sid)
  713. data = {
  714. "msg": "超出或者重复扫单超出了应发量",
  715. }
  716. return Response(data, status=status.HTTP_200_OK)
  717. # filter(i['cc']),此方法是用来格式化数据的,如果数据为none则返回空
  718. prd_name = filter(i['prd_name'])
  719. cc = filter(i['cc'])
  720. zy = filter(i['zy'])
  721. zl = filter(i['zl'])
  722. ms = filter(i['ms'])
  723. rem = filter(i['rem'])
  724. dd = filter(i['dd'])
  725. prd_rem = filter(i['prd_rem'])
  726. zc_no_up = filter(i['zc_no_up'])
  727. zc_no_dn = filter(i['zc_no_dn'])
  728. sal_no = filter(i['sal_no'])
  729. print(sal_no)
  730. tf_bg = Django_tf_bg(bg_no=fordata, no_dd=no_dd, itm=i['itm'], bg_id=bg_id, mo_no=i['mo_no'],zt_no=i['zt_no'], prd_no=i['prd_no'],
  731. prd_name=prd_name, cc=cc, zy=zy, qty=i['qty'], zl=zl, ms=ms, rem=rem, dd=dd,prd_rem=prd_rem, zc_no_end=zc_no_dn, zc_no_up=zc_no_up,sal_no=sal_no)
  732. tf_bg.save()
  733. update_sql = ClassSqls.ShUpSql.format(i['zt_no'], bg_id)
  734. IseUpDelQuery(update_sql)
  735. transaction.savepoint_commit(sid) # 提交事物
  736. # 5、返回响应
  737. data = {
  738. "msg":"新增成功",
  739. "bg_no":bg_no
  740. }
  741. return Response(data,status=status.HTTP_200_OK)
  742. @transaction.atomic
  743. def put(self,request):
  744. # 1、获取参数
  745. data = request.data
  746. bg_id = data.get('bg_id')
  747. bg_no = data.get('bg_no')
  748. # print(data)
  749. # 2、修改表头
  750. sid = transaction.savepoint() # 开启事物
  751. mf_bg = Django_mf_bg.objects.get(bg_id=bg_id,bg_no=bg_no)
  752. mf_bg.no_dd = data.get('no_dd')
  753. mf_bg.sal_no = data.get('sal_no')
  754. mf_bg.mo_no = data.get('mo_no')
  755. mf_bg.save()
  756. # 3、修改表身
  757. for i in request.data.get('dataList'):
  758. # print(i)
  759. # 判断是否超
  760. if i['qty'] > i['maxqty']:
  761. data = {
  762. "msg": "新增失败,存在超出数量项次",
  763. }
  764. return Response(data, status=status.HTTP_200_OK)
  765. mf_bg = Django_tf_bg.objects.get(bg_id = bg_id,bg_no_id = bg_no,itm = i.get('itm'))
  766. mf_bg.qty = i.get('qty')
  767. mf_bg.zl = i.get('zl')
  768. mf_bg.save()
  769. update_sql = ClassSqls.ShUpSql.format(i['zt_no'], bg_id)
  770. IseUpDelQuery(update_sql)
  771. transaction.savepoint_commit(sid) # 提交事物
  772. data={
  773. "msg":"修改成功"
  774. }
  775. return Response(data,status=status.HTTP_200_OK)
  776. @transaction.atomic
  777. def delete(self, request):
  778. # 1、获取参数
  779. data = request.data
  780. scope = data.get('scope')
  781. bg_no = data.get('bg_no')
  782. # 是否删除行标志
  783. sign = data.get('sign')
  784. if scope is None and bg_no is None:
  785. data = {
  786. "msg": "删除失败"
  787. }
  788. return Response(data, status=status.HTTP_304_NOT_MODIFIED)
  789. if bg_no and sign is None:
  790. # 删除表身
  791. sid = transaction.savepoint() # 开启事物
  792. # 查询删除表身的通知单号
  793. sel_sql = ClassSqls.CommonTfSql.format(bg_no)
  794. del_zt_no = executeQuery(sel_sql)
  795. Django_tf_bg.objects.filter(bg_no_id=bg_no).delete()
  796. Django_mf_bg.objects.filter(bg_no=bg_no).delete()
  797. # print(del_zt_no)
  798. for i in del_zt_no:
  799. # 查询应生产量
  800. yscl_sql = ClassSqls.ShYscl.format(i['zt_no'])
  801. yscl_qty = executeQuery(yscl_sql)[0]
  802. if i['qty'] > yscl_qty['shqty'] - yscl_qty['zyqty']:
  803. transaction.savepoint_rollback(sid)
  804. data = {
  805. "msg": "已转后续单据不允许删除"
  806. }
  807. return Response(data, status=status.HTTP_201_CREATED)
  808. update_sql = ClassSqls.ShUpdel.format(i.get('qty'), i.get('zl'), i.get('zt_no'))
  809. IseUpDelQuery(update_sql)
  810. transaction.savepoint_commit(sid) # 提交事物
  811. else:
  812. sid = transaction.savepoint() # 开启事物
  813. try:
  814. # 删除的时候要先查询出删除数据的的数量,查询单据的数量和重量
  815. old_data = Django_tf_bg.objects.get(zt_no=scope.get('zt_no'), bg_id=scope.get('bg_id'),bg_no_id=scope.get('bg_no'),itm=scope.get('itm'))
  816. yscl_sql = ClassSqls.ShYscl.format(scope.get('zt_no'))
  817. yscl_qty = executeQuery(yscl_sql)[0]
  818. if scope.get['qty'] > yscl_qty['shqty'] - yscl_qty['zyqty']:
  819. transaction.savepoint_rollback(sid)
  820. data = {
  821. "msg": "已转后续单据不允许删除"
  822. }
  823. return Response(data, status=status.HTTP_201_CREATED)
  824. Django_tf_bg.objects.filter(bg_no_id=scope.get('bg_no'), itm=scope.get('itm')).delete()
  825. update_sql = ClassSqls.ShUpdel.format(old_data.qty, old_data.zl, scope.get('zt_no'))
  826. IseUpDelQuery(update_sql)
  827. except:
  828. data = {
  829. "msg": "删除失败"
  830. }
  831. return Response(data, status=status.HTTP_200_OK)
  832. transaction.savepoint_commit(sid) # 提交事物
  833. data = {
  834. "msg": "删除成功"
  835. }
  836. return Response(data, status=status.HTTP_200_OK)
  837. # 转移单
  838. class Zy(APIView):
  839. # 1,设置局部认证
  840. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  841. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  842. # 2,设置局部权限
  843. permission_classes = (IsAuthenticated,)
  844. def get(self,request):
  845. params = request.query_params
  846. # 如果单号存在那么就是查询单据,如果单号不存在就是弹窗查询
  847. # 如果单号存在则按单号进行查询数据
  848. filter_no = params.get('filter_no','')
  849. if filter_no:
  850. # 查询表头信息
  851. mf_sql = ClassSqls.CommonMf_sql.format(filter_no)
  852. mf_result = executeQuery(mf_sql)
  853. if len(mf_result)>1 or len(mf_result)==0:
  854. return Response('查询表头数据异常',status=status.HTTP_206_PARTIAL_CONTENT)
  855. # 查询表身信息
  856. tf_sql = ClassSqls.CommonTf_sql.format(filter_no)
  857. tf_result = executeQuery(tf_sql)
  858. if len(tf_result) == 0:
  859. return Response('查询表表身数据异常', status=status.HTTP_206_PARTIAL_CONTENT)
  860. # 计算应生产量
  861. for i in range(len(tf_result)):
  862. yscl_sql = ClassSqls.ZyYscl.format(tf_result[i].get('zt_no'))
  863. # print(yscl_sql)
  864. # print(1111)
  865. yscl_qty = executeQuery(yscl_sql)[0]
  866. if tf_result[i]['zt_no']==yscl_qty.get('tz_no'):
  867. tf_result[i]['yscl'] = yscl_qty.get('qty')
  868. tf_result[i]['maxqty'] = yscl_qty.get('qty') + tf_result[i]['qty']
  869. tf_result[i]['totalqty'] = yscl_qty.get('totalqty')
  870. # 返回单据查询成功的数据
  871. data = {
  872. "mf_result":mf_result[0],
  873. "tf_result":tf_result,
  874. "msg":"查询成功"
  875. }
  876. return Response(data,status=status.HTTP_200_OK)
  877. to_date = params.get('to_date')#开始日期
  878. end_date = params.get('end_date') #结束日期
  879. bg_id = params.get('bg_id') #单据类别
  880. select_zc_no = params.get('select_zc_no') #制程代号
  881. condition = 'where left(Convert(varchar(100), no_dd, 23), 11)>=' + "'" + to_date + "'" + ' and left(Convert(varchar(100), no_dd, 23), 11)<=' + "'" + end_date + "'" + ' and bg_id=' + "'" + bg_id + "'"
  882. # 获取账号等级
  883. is_superuser = request.user.is_superuser
  884. # 获取登录用户
  885. users = request.user
  886. if is_superuser == False:
  887. condition = condition + " and create_user=" + "'" + str(users) + "'"
  888. if select_zc_no:
  889. condition = condition + ' and zc_no=' + "'" + select_zc_no + "'"
  890. sql1 = ClassSqls.CommonFilter.format(condition)
  891. result = executeQuery(sql1)
  892. data = {
  893. "result":result,
  894. "msg":"查询成功"
  895. }
  896. return Response(data,status=status.HTTP_200_OK)
  897. @transaction.atomic
  898. def post(self,request):
  899. # 1、获取参数
  900. data = request.data
  901. # 单据日期
  902. no_dd = data['no_dd']
  903. # 制程代号
  904. zc_no = data['zc_no']
  905. # 指定的制程代号
  906. zd_zc = data['zd_zc']
  907. if zc_no is None:
  908. return Response("制程代码不能为空", status=status.HTTP_200_OK)
  909. # 创建日期
  910. create_user = data['create_user']
  911. # 创建用户
  912. username = data['username']
  913. #单据类别
  914. bg_id = data['bg_id']
  915. # 作业人员
  916. sal_no = data['sal_no']
  917. # 制令单号
  918. mo_no=data['mo_no']
  919. # 是否异常单
  920. isbad = data['isbad']
  921. # 表身数据
  922. dataList = data['dataList']
  923. # print(dataList)
  924. # 2、处理生成单号
  925. # 每次新增之前先把通知单没有自定栏位的单号插入到自定义表
  926. insert_sql = ClassSqls.CommonSql_z
  927. IseUpDelQuery(insert_sql)
  928. sql1 = ClassSqls.CommonSqlNo.format(bg_id)
  929. bg_no = bg_id+monthly_odd(sql1)
  930. # 判断单号是否重复
  931. ifbg_no_sql = ClassSqls.CommonIfoold.format(bg_no, bg_id)
  932. ifbg_no_qty = rpoold(ifbg_no_sql)
  933. if int(ifbg_no_qty) > 0:
  934. data = {
  935. "msg": "单号重复请重新做单",
  936. }
  937. return Response(data, status=status.HTTP_200_OK)
  938. sid = transaction.savepoint() # 开启事物
  939. # 3、插入表头数据
  940. mf_bg = Django_mf_bg(bg_no=bg_no,no_dd=no_dd,bg_id=bg_id,zc_no=zc_no,sal_no=sal_no,isbad=isbad,create_user=username,mo_no=mo_no,zd_zc=zd_zc,create_time=datetime.datetime.now())
  941. mf_bg.save()
  942. # 4、插入表身数据
  943. # 查询外键
  944. fordata = Django_mf_bg.objects.get(bg_no=bg_no)
  945. for i in dataList:
  946. # 判断是否超
  947. if i['qty'] > i['yscl']:
  948. data = {
  949. "msg": "新增失败,存在超出数量项次",
  950. }
  951. return Response(data, status=status.HTTP_200_OK)
  952. # filter(i['cc']),此方法是用来格式化数据的,如果数据为none则返回空
  953. prd_name = filter(i['prd_name'])
  954. cc = filter(i['cc'])
  955. zy = filter(i['zy'])
  956. zl = filter(i['zl'])
  957. ms = filter(i['ms'])
  958. rem = filter(i['rem'])
  959. dd = filter(i['dd'])
  960. prd_rem = filter(i['prd_rem'])
  961. zc_no_up = filter(i['zc_no_up'])
  962. zc_no_dn = filter(i['zc_no_dn'])
  963. tf_bg = Django_tf_bg(bg_no=fordata, no_dd=no_dd, itm=i['itm'], bg_id=bg_id, mo_no=i['mo_no'],zt_no=i['zt_no'], prd_no=i['prd_no'],
  964. prd_name=prd_name, cc=cc, zy=zy, qty=i['qty'], zl=zl, ms=ms, rem=rem, dd=dd,prd_rem=prd_rem, zc_no_end=zc_no_dn, zc_no_up=zc_no_up,zd_zc=zd_zc)
  965. tf_bg.save()
  966. # 更新通知单自定义栏位
  967. # 0为正常单据 ,1为异常单据
  968. if isbad==0:
  969. update_sql = ClassSqls.ZyUpSql.format(i['zt_no'], bg_id)
  970. # 更新下制程的带接收数量
  971. update_sql_dai = ClassSqls.ZyAddSql_dai.format(i['mo_no'], bg_id,i['zd_zc'], bg_no)
  972. IseUpDelQuery(update_sql_dai)
  973. else:
  974. update_sql = ClassSqls.ZyUpSql1.format(i['zt_no'], bg_id)
  975. IseUpDelQuery(update_sql)
  976. # 更新通知单生产数量以及结案标识
  977. uptz(i['zt_no'])
  978. transaction.savepoint_commit(sid) # 提交事物
  979. # 5、返回响应
  980. data = {
  981. "msg":"新增成功",
  982. "bg_no":bg_no
  983. }
  984. return Response(data,status=status.HTTP_200_OK)
  985. @transaction.atomic
  986. def put(self,request):
  987. # 1、获取参数
  988. data = request.data
  989. bg_id = data.get('bg_id')
  990. bg_no = data.get('bg_no')
  991. isbad = data.get('isbad')
  992. zd_zc = data.get('zd_zc')
  993. zc_no = data.get('zc_no')
  994. # print(data)
  995. # 2、修改表头
  996. sid = transaction.savepoint() # 开启事物
  997. mf_bg = Django_mf_bg.objects.get(bg_id=bg_id,bg_no=bg_no)
  998. mf_bg.no_dd = data.get('no_dd')
  999. mf_bg.sal_no = data.get('sal_no')
  1000. mf_bg.mo_no = data.get('mo_no')
  1001. mf_bg.zd_zc = data.get('zd_zc')
  1002. mf_bg.save()
  1003. # 3、修改表身
  1004. for i in request.data.get('dataList'):
  1005. print(i)
  1006. # 判断是否超
  1007. if i['qty'] > i['maxqty']:
  1008. data = {
  1009. "msg": "新增失败,存在超出数量项次",
  1010. }
  1011. return Response(data, status=status.HTTP_200_OK)
  1012. # 更新通知单生产数量以及结案标识
  1013. uptz(i.get('zt_no'))
  1014. mf_bg = Django_tf_bg.objects.get(bg_id = bg_id,bg_no_id = bg_no,itm = i.get('itm'))
  1015. mf_bg.qty = i.get('qty')
  1016. mf_bg.zl = i.get('zl')
  1017. old_zd_zc=mf_bg.zd_zc
  1018. mf_bg.zd_zc = zd_zc
  1019. mf_bg.save()
  1020. # 更新通知单自定义栏位
  1021. # 0为正常单据 ,1为异常单据
  1022. if isbad == 0:
  1023. update_sql = ClassSqls.ZyUpSql.format(i['zt_no'], bg_id)
  1024. # 更新掉旧的待接收数量
  1025. update_old_dai_sql = ClassSqls.update_old_dai_sql.format(i.get('qty'), i.get('zl'), mf_bg.mo_no,old_zd_zc)
  1026. IseUpDelQuery(update_old_dai_sql)
  1027. # 查询更新新的指定制程带接收量
  1028. update_sql_dai = ClassSqls.ZyUpSql_dai.format(i['mo_no'], bg_id,zc_no, i['bg_no_id'])
  1029. IseUpDelQuery(update_sql_dai)
  1030. else:
  1031. update_sql = ClassSqls.ZyUpSql1.format(i['zt_no'], bg_id)
  1032. IseUpDelQuery(update_sql)
  1033. transaction.savepoint_commit(sid) # 提交事物
  1034. data={
  1035. "msg":"修改成功"
  1036. }
  1037. return Response(data,status=status.HTTP_200_OK)
  1038. @transaction.atomic
  1039. def delete(self, request):
  1040. # 1、获取参数
  1041. data = request.data
  1042. scope = data.get('scope')
  1043. bg_no = data.get('bg_no')
  1044. isbad = data.get('isbad')
  1045. # 是否删除行标志
  1046. sign = data.get('sign')
  1047. if scope is None and bg_no is None:
  1048. print(bg_no)
  1049. data = {
  1050. "msg": "删除失败"
  1051. }
  1052. return Response(data, status=status.HTTP_304_NOT_MODIFIED)
  1053. # 删除整单
  1054. if bg_no and sign is None:
  1055. # 删除表身
  1056. sid = transaction.savepoint() # 开启事物
  1057. # 查询删除表身的通知单号
  1058. sel_sql = ClassSqls.CommonTfSql.format(bg_no)
  1059. del_zt_no = executeQuery(sel_sql)
  1060. Django_tf_bg.objects.filter(bg_no_id=bg_no).delete()
  1061. Django_mf_bg.objects.filter(bg_no=bg_no).delete()
  1062. # print(del_zt_no)
  1063. for i in del_zt_no:
  1064. yscl_sql = ClassSqls.ZyYscl.format(i['zt_no'])
  1065. yscl_qty = executeQuery(yscl_sql)[0]
  1066. if i['qty'] > yscl_qty['zyqty'] - yscl_qty['jsqty']:
  1067. transaction.savepoint_rollback(sid)
  1068. data = {
  1069. "msg": "已转后续单据不允许删除"
  1070. }
  1071. return Response(data, status=status.HTTP_201_CREATED)
  1072. if isbad==0:
  1073. # 更新掉旧的待接收数量
  1074. update_old_dai_sql = ClassSqls.update_old_dai_sql.format(i.get('qty'), i.get('zl'), i.get('mo_no'),i.get('zd_zc'))
  1075. IseUpDelQuery(update_old_dai_sql)
  1076. update_sql = ClassSqls.ZyUpdel.format(i.get('qty'), i.get('zl'), i.get('zt_no'))
  1077. else:
  1078. update_sql = ClassSqls.ZyUpdel1.format(i.get('qty'), i.get('zl'), i.get('zt_no'))
  1079. IseUpDelQuery(update_sql)
  1080. # 更新结案标志
  1081. uptz(i.get('zt_no'))
  1082. transaction.savepoint_commit(sid) # 提交事物
  1083. else:
  1084. # 删除表身项次
  1085. sid = transaction.savepoint() # 开启事物
  1086. try:
  1087. # 删除的时候要先查询出删除数据的的数量,查询单据的数量和重量
  1088. old_data = Django_tf_bg.objects.get(zt_no=scope.get('zt_no'), bg_id=scope.get('bg_id'),bg_no_id=scope.get('bg_no'),itm=scope.get('itm'))
  1089. yscl_sql = ClassSqls.ZyYscl.format(scope.get('zt_no'))
  1090. yscl_qty = executeQuery(yscl_sql)[0]
  1091. if scope.get['qty'] > yscl_qty['zyqty'] - yscl_qty['jsqty']:
  1092. transaction.savepoint_rollback(sid)
  1093. data = {
  1094. "msg": "已转后续单据不允许删除"
  1095. }
  1096. return Response(data, status=status.HTTP_201_CREATED)
  1097. Django_tf_bg.objects.filter(bg_no_id=scope.get('bg_no'), itm=scope.get('itm')).delete()
  1098. # 正常单据删除
  1099. if isbad==0:
  1100. # 更新掉旧的待接收数量
  1101. update_old_dai_sql = ClassSqls.update_old_dai_sql.format(scope.get('qty'), scope.get('zl'), scope.get('mo_no'),scope.get('zd_zc'))
  1102. IseUpDelQuery(update_old_dai_sql)
  1103. update_sql = ClassSqls.ZyUpdel.format(old_data.qty, old_data.zl, scope.get('zt_no'))
  1104. else:
  1105. update_sql = ClassSqls.ZyUpdel1.format(old_data.qty, old_data.zl, scope.get('zt_no'))
  1106. # 异常单据删除
  1107. IseUpDelQuery(update_sql)
  1108. # 更新结案标志
  1109. uptz(scope.get('zt_no'))
  1110. except:
  1111. data = {
  1112. "msg": "删除成功"
  1113. }
  1114. return Response(data, status=status.HTTP_200_OK)
  1115. transaction.savepoint_commit(sid) # 提交事物
  1116. data = {
  1117. "msg": "删除成功"
  1118. }
  1119. return Response(data, status=status.HTTP_200_OK)