views.py 46 KB

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