views.py 47 KB

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