views.py 48 KB

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