Browse Source

今天代7点的码结束

任翠亮 5 years ago
parent
commit
002eb91270

+ 427 - 197
awj/awj/apps/PaymentOperation/views.py

@@ -1,8 +1,9 @@
 import json
+import traceback
 
 from django import http
 from django.shortcuts import render
-
+from django.db import transaction
 # Create your views here.
 from django.views import View
 from django.db import connection
@@ -32,6 +33,7 @@ class PaymentOperationView(View):
         }
         return http.JsonResponse(context)
 
+    @transaction.atomic
     def post(self, request):
 
         # 获取参数
@@ -46,92 +48,105 @@ class PaymentOperationView(View):
 
 
         # 判断单号是否存在
-        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO)
+        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO,'RP_ID','2')
         if row_ZL_NO > 0:
-            return http.HttpResponseForbidden('单号重复')
+            return http.HttpResponseForbidden('付款单号已存在')
 
         # 判断用户是否存在,不存在则创建
         row_usr = usr_data(USR, USR_NAME,datetime.datetime.strptime(RP_DD, '%Y-%m-%d'))
         with connection.cursor() as cursor:
 
-            # cursor.execute("""UPDATE TF_PSS SET AMT_FP=%s,AMTN_NET_FP=%s WHERE PS_NO=%s""",[AMT, AMT, 'PC' + ZL_NO[2::]])
-            #插入付款单表头
-            cursor.execute("""INSERT INTO TF_MON(IRP_ID,RP_NO,ITM,RP_ID,CLS_ID,RP_DD,CUS_NO,INCLUDESON,AMTN_BC,AMTN_CLS,BC_NO,CACC_NO,EXC_RTO,DEP,USR,CHK_MAN,CLS_DATE,SYS_DATE,IEA_ID,IOR_ID,SK_TYPE)
-          VALUES('F',%s,1,2,'T',%s,%s,'F',%s,%s,%s,%s,1,'0000',%s,%s,%s,%s,'F','F',1)""",
-          [RP_NO, datetime.datetime.strptime(RP_DD, '%Y-%m-%d'), CUS_NO ,AMTN_BC,AMTN_BC,'BT'+RP_NO[2::],CACC_NO,USR,USR,datetime.datetime.strptime(RP_DD, '%Y-%m-%d'),
-          datetime.datetime.strptime(RP_DD, '%Y-%m-%d')])
-            # RP_NO   付款单号  RP_NO
-            #RP_DD    付款日期   datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
-            #CUS_NO   付款客户编码    CUS_NO
-            #AMTN_BC    付款金额     AMTN_BC
-            #AMTN_CLS    冲款金额      AMTN_CLS
-            #BC_NO      账户收支单号
-            #CACC_NO    'BT'+RP_NO[2::]
-            #CACC_NO        付款银行编码
-            #USR   制单人      USR
-            #CHK_MAN   审核人      USR
-            #CLS_DATE    制单时间       datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
-            #CLS_DATE       审核时间        datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
-
-            # 插入付款单表身-------------改CUR_ID  RMB
-            cursor.execute("""INSERT INTO TC_MON(RP_ID,RP_NO,ITM,ITM2,ARP_NO,AMTN_CLS,ARP_OPN_ID,RP_DD,CUS_NO,PRE_ITM)values
-            (2,%s,1,1,%s,%s,2,%s,%s,1)""",
-            [RP_NO, 'AP'+MF_LZ1[2::], AMTN_BC,datetime.datetime.strptime(RP_DD, '%Y-%m-%d'),CUS_NO])
-            # RP_NO   付款单号  RP_NO
-            #ARP_NO   立账单号      MF_LZ1
-            #AMTN_CLS  冲款金额  AMTN_BC
-            #RP_DD  冲款时间    datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
-            #CUS_NO   冲款客户      CUS_NO
-
-            # 插入回写冲款金额表
-            cursor.execute("""INSERT INTO MF_MON(RP_ID,RP_NO,RP_DD,DEP,AMTN,AMTN_ARP,AMTN_REST,FJ_NUM)VALUES
-            (2,%s,%s,'0000',%s,%s,%s,0)""",[RP_NO,datetime.datetime.strptime(RP_DD, '%Y-%m-%d'), AMTN_BC,AMTN_BC,AMTN_BC])
-            #RP_NO  付款单号  RP_NO
-            #AMTN   冲款金额
-            #AMTN_ARP  总金额
-            #AMTN_REST
-
-            # 插入账户收支单表头
-            cursor.execute("""INSERT INTO MF_BAC(BB_ID,BB_NO,BB_DD,BACC_NO,ACC_NO,DEP,BIL_NO,EXC_RTO,AMTN,USR,CHK_MAN,OPN_ID,CLS_DATE,SYS_DATE,BIL_ID_N,BIL_NO_N)VALUES
-            ('BT',%s,%s,%s,100903,'0000',%s,1,%s,%s,%s,'F',%s,%s,'PT',%s)""",
-                           ['BT'+RP_NO[2::], datetime.datetime.strptime(RP_DD, '%Y-%m-%d'), CACC_NO,'PT'+RP_NO,-float(AMTN_BC),USR,USR,datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
-            ,datetime.datetime.strptime(RP_DD, '%Y-%m-%d'),RP_NO])
-            #BB_NO  账户收支单单号  'BT'+RP_NO[2::]
-            #BB_DD   账户收支单日期  RP_NO[2::], datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
-            # BACC_NO  银行账户代号
-            #ACC_NO  会计科目      待定,需要会计确定自动取银行账号里面的会计科目
-            #BIL_NO  来源单号   'PT'+RP_NO
-            #AMTN 金额   付款是负数,收款是正数      -float(AMTN_BC)
-            #USR  制单人       USR
-            #  CHK_MAN  审核人     USR
-            #CLS_DATE  审核日期
-            # SYS_DATE  录入日期
-            #BIL_NO_N  来源单号
-
-            # 插入账户收支单表身
-            cursor.execute("""INSERT INTO TF_BAC(BB_ID,BB_NO,ITM,BB_DD,EXC_RTO,AMTN,DEP,CUS_NO,ADD_ID,PRE_ITM)values
-            ('BT',%s,1,%s,1,%s,'0000',%s,'-',1)""",['BT'+RP_NO[2::], datetime.datetime.strptime(RP_DD, '%Y-%m-%d'), AMTN_BC, CUS_NO])
-            # BB_NO  账户收支单号  'BT'+RP_NO[2::]
-            # BB_DD  账户收支单日期    datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
-            #AMTN   金额      AMTN_BC
-            #CUS_NO  客户编码   CUS_NO
+            MF_LZ1_NO = cursor.execute("""SELECT COUNT(*) FROM MF_ARP WHERE ARP_NO=%s""", ['AP' + MF_LZ1[2::]]).fetchall()[0][0]
+            if MF_LZ1_NO<=0:
+                return http.HttpResponseForbidden('开票单号不存在')
+            sid = transaction.savepoint()  #开启事物
+            try:
+                # 获取银行账号的会计科目
+                Acc_No_km = cursor.execute("""SELECT Acc_No FROM BACC WHERE BACC_NO=%s""",[CACC_NO]).fetchall()[0][0]
+
+                # cursor.execute("""UPDATE TF_PSS SET AMT_FP=%s,AMTN_NET_FP=%s WHERE PS_NO=%s""",[AMT, AMT, 'PC' + ZL_NO[2::]])
+                #插入付款单表头
+                cursor.execute("""INSERT INTO TF_MON(IRP_ID,RP_NO,ITM,RP_ID,CLS_ID,RP_DD,CUS_NO,INCLUDESON,AMTN_BC,AMTN_CLS,BC_NO,CACC_NO,EXC_RTO,DEP,USR,CHK_MAN,CLS_DATE,SYS_DATE,IEA_ID,IOR_ID,SK_TYPE)
+              VALUES('F',%s,1,2,'T',%s,%s,'F',%s,%s,%s,%s,1,'0000',%s,%s,%s,%s,'F','F',1)""",
+              [RP_NO, datetime.datetime.strptime(RP_DD, '%Y-%m-%d'), CUS_NO ,AMTN_BC,AMTN_BC,'BT'+RP_NO[2::],CACC_NO,USR,USR,datetime.datetime.strptime(RP_DD, '%Y-%m-%d'),
+              datetime.datetime.strptime(RP_DD, '%Y-%m-%d')])
+                # RP_NO   付款单号  RP_NO
+                #RP_DD    付款日期   datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+                #CUS_NO   付款客户编码    CUS_NO
+                #AMTN_BC    付款金额     AMTN_BC
+                #AMTN_CLS    冲款金额      AMTN_CLS
+                #BC_NO      账户收支单号
+                #CACC_NO    'BT'+RP_NO[2::]
+                #CACC_NO        付款银行编码
+                #USR   制单人      USR
+                #CHK_MAN   审核人      USR
+                #CLS_DATE    制单时间       datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+                #CLS_DATE       审核时间        datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+
+                # 插入付款单表身-------------改CUR_ID  RMB
+                cursor.execute("""INSERT INTO TC_MON(RP_ID,RP_NO,ITM,ITM2,ARP_NO,AMTN_CLS,ARP_OPN_ID,RP_DD,CUS_NO,PRE_ITM)values
+                (2,%s,1,1,%s,%s,2,%s,%s,1)""",
+                [RP_NO, 'AP'+MF_LZ1[2::], AMTN_BC,datetime.datetime.strptime(RP_DD, '%Y-%m-%d'),CUS_NO])
+                # RP_NO   付款单号  RP_NO
+                #ARP_NO   立账单号      MF_LZ1
+                #AMTN_CLS  冲款金额  AMTN_BC
+                #RP_DD  冲款时间    datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+                #CUS_NO   冲款客户      CUS_NO
+
+                # 插入回写冲款金额表
+                cursor.execute("""INSERT INTO MF_MON(RP_ID,RP_NO,RP_DD,DEP,AMTN,AMTN_ARP,AMTN_REST,FJ_NUM)VALUES
+                (2,%s,%s,'0000',%s,%s,%s,0)""",[RP_NO,datetime.datetime.strptime(RP_DD, '%Y-%m-%d'), AMTN_BC,AMTN_BC,AMTN_BC])
+                #RP_NO  付款单号  RP_NO
+                #AMTN   冲款金额
+                #AMTN_ARP  总金额
+                #AMTN_REST
+
+                # 插入账户收支单表头
+                cursor.execute("""INSERT INTO MF_BAC(BB_ID,BB_NO,BB_DD,BACC_NO,ACC_NO,DEP,BIL_NO,EXC_RTO,AMTN,USR,CHK_MAN,OPN_ID,CLS_DATE,SYS_DATE,BIL_ID_N,BIL_NO_N)VALUES
+                ('BT',%s,%s,%s,%s,'0000',%s,1,%s,%s,%s,'F',%s,%s,'PT',%s)""",
+                ['BT'+RP_NO[2::], datetime.datetime.strptime(RP_DD, '%Y-%m-%d'), CACC_NO,Acc_No_km,'PT'+RP_NO,-float(AMTN_BC),USR,USR,datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+                ,datetime.datetime.strptime(RP_DD, '%Y-%m-%d'),RP_NO])
+                #BB_NO  账户收支单单号  'BT'+RP_NO[2::]
+                #BB_DD   账户收支单日期  RP_NO[2::], datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+                # BACC_NO  银行账户代号
+                #ACC_NO  会计科目      待定,需要会计确定自动取银行账号里面的会计科目
+                #BIL_NO  来源单号   'PT'+RP_NO
+                #AMTN 金额   付款是负数,收款是正数      -float(AMTN_BC)
+                #USR  制单人       USR
+                #  CHK_MAN  审核人     USR
+                #CLS_DATE  审核日期
+                # SYS_DATE  录入日期
+                #BIL_NO_N  来源单号
+
+                # 插入账户收支单表身
+                cursor.execute("""INSERT INTO TF_BAC(BB_ID,BB_NO,ITM,BB_DD,EXC_RTO,AMTN,DEP,CUS_NO,ADD_ID,PRE_ITM)values
+                ('BT',%s,1,%s,1,%s,'0000',%s,'-',1)""",['BT'+RP_NO[2::], datetime.datetime.strptime(RP_DD, '%Y-%m-%d'), AMTN_BC, CUS_NO])
+                # BB_NO  账户收支单号  'BT'+RP_NO[2::]
+                # BB_DD  账户收支单日期    datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+                #AMTN   金额      AMTN_BC
+                #CUS_NO  客户编码   CUS_NO
+
+                #回写立账单的数据
+                cursor.execute("""UPDATE MF_ARP SET AMTN_RCV=ISNULL(AMTN_RCV,0)+%s,AMT=ISNULL(AMTN,0)-ISNULL(AMTN_RCV,0)-%s WHERE BIL_NO=%s""",[AMTN_BC, AMTN_BC,MF_LZ1])
+                # AMTN_RCV   已冲金额     ISNULL(AMTN_RCV,0)+%s
+                # AMT   未冲金额  ISNULL(AMT,0)-%s
+                # BIL_NO  开票单号
 
-            #回写立账单的数据
-            cursor.execute("""UPDATE MF_ARP SET AMTN_RCV=ISNULL(AMTN_RCV,0)+%s,AMT=ISNULL(AMTN,0)-ISNULL(AMTN_RCV,0)-%s WHERE BIL_NO=%s""",[AMTN_BC, AMTN_BC,MF_LZ1])
-            # AMTN_RCV   已冲金额     ISNULL(AMTN_RCV,0)+%s
-            # AMT   未冲金额  ISNULL(AMT,0)-%s
-            # BIL_NO  开票单号
-
-            #如果单据已经冲完,结案掉立账单,还有进货的立账结案标识
-            row_AMT = cursor.execute("SELECT CAST(ISNULL(AMT,0) AS INT ),BIL_NO FROM MF_ARP WHERE BIL_NO=%s", [MF_LZ1]).fetchall()[0][0]
-            if row_AMT<=0:
-                cursor.execute("""UPDATE MF_ARP SET CLOSE_ID='T' WHERE BIL_NO=%s""",[MF_LZ1])
-                cursor.execute("""UPDATE MF_PSS SET LZ_CLS_ID='T',CLSLZ='T' WHERE PS_NO=%s""",['PC'+MF_LZ1[2::]])
+                #如果单据已经冲完,结案掉立账单,还有进货的立账结案标识
+                row_AMT = cursor.execute("SELECT CAST(ISNULL(AMT,0) AS INT ),BIL_NO FROM MF_ARP WHERE BIL_NO=%s", [MF_LZ1]).fetchall()[0][0]
+                if row_AMT<=0:
+                    cursor.execute("""UPDATE MF_ARP SET CLOSE_ID='T' WHERE BIL_NO=%s""",[MF_LZ1])
+                    cursor.execute("""UPDATE MF_PSS SET LZ_CLS_ID='T',CLSLZ='T' WHERE PS_NO=%s""",['PC'+MF_LZ1[2::]])
+            except Exception:
+                transaction.savepoint_rollback(sid)
+                return http.HttpResponseForbidden("新增付款单sql语句执行异常")
+            transaction.savepoint_commit(sid)
         context = {
             'a': 'post付款作业'
         }
         return http.JsonResponse(context)
 
+    @transaction.atomic
     def put(self, request):
         req_data = json.loads(request.body.decode())
         # 获取参数
@@ -140,7 +155,7 @@ class PaymentOperationView(View):
         AMTN_BC = req_data.get("AMTN_BC")  #付款金额            数字类型
         CACC_NO = req_data.get("CACC_NO")  #付款银行编码          字符类型
         MF_LZ1 = req_data.get("MF_LZ1")  #进货开票单号         字符类型
-
+        UP_DD = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')  #修改时间
         print(RP_NO)
         print(RP_DD)
         print(AMTN_BC)
@@ -154,80 +169,97 @@ class PaymentOperationView(View):
         #     "MF_LZ1":"LP9A170002"
         # }
         # 判断单号是否存在
-        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO)
+        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO,'RP_ID','2')
         if row_ZL_NO <= 0:
-            return http.HttpResponseForbidden('单号不存在')
+            return http.HttpResponseForbidden('付款单号不存在')
 
         # 付款单不能修改客户
         # # 判断用户是否存在,不存在则创建
         # row_usr = usr_data(USR, USR_NAME, datetime.datetime.strptime(RP_DD, '%Y-%m-%d'))
         with connection.cursor() as cursor:
+
+            # 判断开票单号是否存在
+            MF_LZ1_NO = cursor.execute("""SELECT COUNT(*) FROM TC_MON WHERE ARP_NO=%s""", ['AP' + MF_LZ1[2::]]).fetchall()[0][0]
+
+            if MF_LZ1_NO<=0:
+                return http.HttpResponseForbidden('开票单号不存在')
+            # 获取银行账号的会计科目
+            Acc_No_km = cursor.execute("""SELECT Acc_No FROM BACC WHERE BACC_NO=%s""", [CACC_NO]).fetchall()[0][0]
+
             # 缓存之前冲款的金额用于回写立账冲款数据
-            row_AMTN = cursor.execute("""SELECT AMTN_BC FROM TF_MON WHERE RP_NO=%s""", [RP_NO]).fetchall()[0][0]
-            #修改付款单表头
-            cursor.execute("""UPDATE TF_MON SET RP_DD=%s,AMTN_BC=%s,CACC_NO=%s WHERE RP_NO=%s""",[RP_DD, AMTN_BC, CACC_NO,RP_NO])
-            # RP_NO   付款单号  RP_NO
-            # RP_DD    付款日期   datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
-            # AMTN_BC    付款金额     AMTN_BC
-            # CACC_NO        付款银行编码
-
-            # 修改付款单表身
-            cursor.execute("""UPDATE TC_MON SET RP_DD=%s,AMTN_CLS=%s WHERE RP_NO=%s""",[RP_DD, AMTN_BC, RP_NO])
-            # RP_NO   付款单号  RP_NO
-            # ARP_NO   立账单号      MF_LZ1==================不用此字段
-            # AMTN_CLS  冲款金额  AMTN_BC
-            # RP_DD  冲款时间    datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
-
-            # 修改回冲金额表
-            cursor.execute("""UPDATE MF_MON SET AMTN=AMTN+%s-%s,AMTN_REST=AMTN+%s-%s WHERE RP_NO=%s""", [AMTN_BC,float(row_AMTN),AMTN_BC,float(row_AMTN), RP_NO])
-            # RP_NO  付款单号  RP_NO
-            # AMTN   冲款金额
-            # AMTN_ARP  总金额  此栏位不需要改
-            # AMTN_REST
-
-            # 修改账户收支单表头
-            cursor.execute("""UPDATE MF_BAC SET BB_DD=%s,BACC_NO=%s,ACC_NO=%s,AMTN=%s WHERE BB_NO=%s""",
-            [ datetime.datetime.strptime(RP_DD, '%Y-%m-%d'), CACC_NO, 100903, -float(AMTN_BC),'BT'+RP_NO[2::]])
-            # BB_NO  账户收支单单号  'BT'+RP_NO[2::]
-            # BB_DD   账户收支单日期  RP_NO[2::], datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
-            # BACC_NO  银行账户代号       CACC_NO
-            # ACC_NO  会计科目      待定,需要会计确定自动取银行账号里面的会计科目     100903
-            # AMTN 金额   付款是负数,收款是正数      -float(AMTN_BC)
-
-            #修改账户收支单表身
-            cursor.execute("""UPDATE TF_BAC SET BB_DD=%s,AMTN=%s WHERE BB_NO=%s""",[datetime.datetime.strptime(RP_DD, '%Y-%m-%d'), AMTN_BC,'BT'+RP_NO[2::]])
-            # BB_NO  账户收支单号  'BT'+RP_NO[2::]
-            # BB_DD  账户收支单日期    datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
-            # AMTN   金额      AMTN_BC
-
-            # 回写立账单的数据需要减去之前冲款的金额  row_AMTN
-            cursor.execute("""UPDATE MF_ARP SET AMTN_RCV=ISNULL(AMTN_RCV,0)+%s-%s,AMT=ISNULL(AMT,0)-%s+%s WHERE BIL_NO=%s""",[AMTN_BC,float(row_AMTN), AMTN_BC, float(row_AMTN),MF_LZ1])
-            # AMTN_RCV   已冲金额     ISNULL(AMTN_RCV,0)+%s
-            # AMT   未冲金额  ISNULL(AMT,0)-%s
-            # BIL_NO  开票单号
-
-            # 如果单据已经冲完,结案掉立账单,还有进货的立账结案标识
-            row_AMT = cursor.execute("SELECT CAST(ISNULL(AMT,0) AS INT ),BIL_NO FROM MF_ARP WHERE BIL_NO=%s",
-                                     [MF_LZ1]).fetchall()[0][0]
-            if row_AMT > 0:
-                cursor.execute("""UPDATE MF_ARP SET CLOSE_ID='F' WHERE BIL_NO=%s""", [MF_LZ1])
-                cursor.execute("""UPDATE MF_PSS SET LZ_CLS_ID='F',CLSLZ='T' WHERE PS_NO=%s""", ['PC' + MF_LZ1[2::]])
+            row_AMTN = cursor.execute("""SELECT AMTN_BC FROM TF_MON WHERE RP_NO=%s AND RP_ID='2'""", [RP_NO]).fetchall()[0][0]
+
+            sid = transaction.savepoint()  # 开启事物
+            try:
+                #修改付款单表头
+                cursor.execute("""UPDATE TF_MON SET RP_DD=%s,AMTN_BC=%s,CACC_NO=%s,MODIFY_DD=%s,MODIFY_MAN=USR WHERE RP_NO=%s AND RP_ID='2'""",[RP_DD, AMTN_BC, CACC_NO,UP_DD,RP_NO])
+                # RP_NO   付款单号  RP_NO
+                # RP_DD    付款日期   datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+                # AMTN_BC    付款金额     AMTN_BC
+                # CACC_NO        付款银行编码
+                # 修改付款单表身
+                cursor.execute("""UPDATE TC_MON SET RP_DD=%s,AMTN_CLS=%s WHERE RP_NO=%s AND RP_ID='2'""",[RP_DD, AMTN_BC, RP_NO])
+                # RP_NO   付款单号  RP_NO
+                # ARP_NO   立账单号      MF_LZ1==================不用此字段
+                # AMTN_CLS  冲款金额  AMTN_BC
+                # RP_DD  冲款时间    datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+
+                # 修改回冲金额表
+                cursor.execute("""UPDATE MF_MON SET AMTN=AMTN+%s-%s,AMTN_REST=AMTN+%s-%s WHERE RP_NO=%s AND RP_ID='2'""", [AMTN_BC,float(row_AMTN),AMTN_BC,float(row_AMTN), RP_NO])
+                # RP_NO  付款单号  RP_NO
+                # AMTN   冲款金额
+                # AMTN_ARP  总金额  此栏位不需要改
+                # AMTN_REST
+
+                # 修改账户收支单表头
+                cursor.execute("""UPDATE MF_BAC SET BB_DD=%s,BACC_NO=%s,ACC_NO=%s,AMTN=%s WHERE BB_NO=%s AND BB_ID='BT'""",
+                [ datetime.datetime.strptime(RP_DD, '%Y-%m-%d'), CACC_NO, Acc_No_km, -float(AMTN_BC),'BT'+RP_NO[2::]])
+                # BB_NO  账户收支单单号  'BT'+RP_NO[2::]
+                # BB_DD   账户收支单日期  RP_NO[2::], datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+                # BACC_NO  银行账户代号       CACC_NO
+                # ACC_NO  会计科目      待定,需要会计确定自动取银行账号里面的会计科目     100903
+                # AMTN 金额   付款是负数,收款是正数      -float(AMTN_BC)
+
+                #修改账户收支单表身
+                cursor.execute("""UPDATE TF_BAC SET BB_DD=%s,AMTN=%s WHERE BB_NO=%s AND BB_ID='BT'""",[datetime.datetime.strptime(RP_DD, '%Y-%m-%d'), AMTN_BC,'BT'+RP_NO[2::]])
+                # BB_NO  账户收支单号  'BT'+RP_NO[2::]
+                # BB_DD  账户收支单日期    datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+                # AMTN   金额      AMTN_BC
+
+                # 回写立账单的数据需要减去之前冲款的金额  row_AMTN
+                cursor.execute("""UPDATE MF_ARP SET AMTN_RCV=ISNULL(AMTN_RCV,0)+%s-%s,AMT=ISNULL(AMT,0)-%s+%s WHERE BIL_NO=%s AND BIL_ID='LP'""",[AMTN_BC,float(row_AMTN), AMTN_BC, float(row_AMTN),MF_LZ1])
+                # AMTN_RCV   已冲金额     ISNULL(AMTN_RCV,0)+%s
+                # AMT   未冲金额  ISNULL(AMT,0)-%s
+                # BIL_NO  开票单号
+
+                # 如果单据已经冲完,结案掉立账单,还有进货的立账结案标识
+                row_AMT = cursor.execute("SELECT CAST(ISNULL(AMT,0) AS INT ),BIL_NO FROM MF_ARP WHERE BIL_NO=%s AND BIL_ID='LP'",
+                                         [MF_LZ1]).fetchall()[0][0]
+                if row_AMT > 0:
+                    cursor.execute("""UPDATE MF_ARP SET CLOSE_ID='F' WHERE BIL_NO=%s AND BIL_ID='LP'""", [MF_LZ1])
+                    cursor.execute("""UPDATE MF_PSS SET LZ_CLS_ID='F',CLSLZ='T' WHERE PS_NO=%s AND PS_ID='PC'""", ['PC' + MF_LZ1[2::]])
+            except Exception:
+                transaction.savepoint_rollback(sid)
+                return http.HttpResponseForbidden("修改付款单sql语句执行异常")
+            transaction.savepoint_commit(sid)
 
         context = {
             'a': 'put付款作业'
         }
         return http.JsonResponse(context)
 
+    @transaction.atomic
     def delete(self, request):
         # 获取参数
         req_data = json.loads(request.body.decode())
         # 获取参数
         RP_NO = req_data.get("RP_NO")  # 付款单号         字符类型
-        MF_LZ1 = req_data.get("MF_LZ1")  # 开票作业单号         字符类型
-        AMTN_BC = req_data.get("AMTN_BC")  # 原单付款金额        字符类型
-        print(RP_NO)
-        print(MF_LZ1)
-        print(AMTN_BC)
+        # # 获取开票作业单号
+        # MF_LZ1 = req_data.get("MF_LZ1")  # 开票作业单号         字符类型
+        # # 获取原付款单金额
+        # AMTN_BC = req_data.get("AMTN_BC")  # 原单付款金额        字符类型
+        # print(MF_LZ1)
+        # print(AMTN_BC)
         # {
         #     "RP_NO":"PT9A200001",
         #     "AMTN_BC":199,
@@ -235,46 +267,59 @@ class PaymentOperationView(View):
         # }
 
         # 判断单号是否存在
-        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO)
+        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO,'RP_ID','2')
         if row_ZL_NO <= 0:
             return http.HttpResponseForbidden('单号不存在')
 
         with connection.cursor() as cursor:
+
+            # 获取原单付款单总金额用于扣减预付款单总金额
+            AMTN_BC = cursor.execute("""SELECT ISNULL(AMTN_CLS,0) FROM TF_MON WHERE RP_NO=%s AND RP_ID='2'""", [RP_NO]).fetchall()[0][0]
+            # 获取开票单号
+            MF_LZ1 = cursor.execute("""SELECT ARP_NO FROM TC_MON WHERE RP_NO=%s AND RP_ID='2'""", [RP_NO]).fetchall()[0][0]
+            MF_LZ1 = 'LP' + MF_LZ1[2::]
         # 缓存之前冲款的金额用于回写立账冲款数据
-            row_AMTN = cursor.execute("""SELECT AMTN_BC FROM TF_MON WHERE RP_NO=%s""", [RP_NO]).fetchall()[0][0]
+            row_AMTN = cursor.execute("""SELECT AMTN_BC FROM TF_MON WHERE RP_NO=%s AND RP_ID='2'""", [RP_NO]).fetchall()[0][0]
             # row_usr = usr_data(USR, USR_NAME, datetime.datetime.strptime(RP_DD, '%Y-%m-%d'))
-        # 删除付款单表头
-            cursor.execute("""DELETE FROM TC_MON WHERE  RP_NO=%s""", [RP_NO])
-        # 删除付款单表身
-            cursor.execute("""DELETE FROM TF_MON WHERE  RP_NO=%s""", [RP_NO])
-
-        #冲款付款金额表,如果是分多次付款的话那么就不能直接删除,是否是付一次款那么就可以直接删除
-        # 如果原来已经付款等于目前传过来的金额那么就可以直接删除
-            row_AMTN_MF_MON = cursor.execute("""SELECT AMTN FROM MF_MON WHERE RP_NO=%s""", [RP_NO]).fetchall()[0][0]
-            if float(AMTN_BC)==float(row_AMTN_MF_MON):
-            #修改回冲金额表
-                cursor.execute("""DELETE FROM MF_MON WHERE RP_NO=%s""", [RP_NO])
-            else:
-                cursor.execute("""UPDATE MF_MON SET AMTN=AMTN+%s-%s,AMTN_REST=AMTN+%s-%s WHERE RP_NO=%s""",
-                           [AMTN_BC, row_AMTN, AMTN_BC, row_AMTN, RP_NO])
-            # 删除账户收支单表头
-            cursor.execute("""DELETE FROM MF_BAC WHERE  BB_NO=%s""", ['BT'+RP_NO[2::]])
-            # 删除账户收支单表身
-            cursor.execute("""DELETE FROM TF_BAC WHERE  BB_NO=%s""", ['BT' + RP_NO[2::]])
-
-            # 如果金额立账金额等于传过来的金额那么就直接删除
-            row_AMTN_MF_ARP = cursor.execute("""SELECT AMTN FROM MF_ARP WHERE BIL_NO=%s""", [MF_LZ1]).fetchall()[0][0]
-            print(float(row_AMTN_MF_ARP))
-            if float(AMTN_BC)==float(row_AMTN_MF_ARP):
-                cursor.execute(
-                    """UPDATE MF_ARP SET AMTN_RCV=0,AMT=0 WHERE BIL_NO=%s""",[MF_LZ1])
-                # 回写立账单的数据需要减去之前冲款的金额  row_AMTN
-            else:
-                cursor.execute(
-                    """UPDATE MF_ARP SET AMTN_RCV=ISNULL(AMTN_RCV,0)+%s,AMT=ISNULL(AMTN,0)-ISNULL(AMTN_RCV,0) WHERE BIL_NO=%s""",[AMTN_BC, MF_LZ1])
-            # 结案掉立账单, 还有进货的立账结案标识
-            cursor.execute("""UPDATE MF_ARP SET CLOSE_ID='F' WHERE BIL_NO=%s""", [MF_LZ1])
-            cursor.execute("""UPDATE MF_PSS SET LZ_CLS_ID='F',CLSLZ='T' WHERE PS_NO=%s""", ['PC' + MF_LZ1[2::]])
+
+            sid = transaction.savepoint()  # 开启事物
+            try:
+            # 删除付款单表头
+                cursor.execute("""DELETE FROM TC_MON WHERE  RP_NO=%s AND RP_ID='2'""", [RP_NO])
+            # 删除付款单表身
+                cursor.execute("""DELETE FROM TF_MON WHERE  RP_NO=%s AND RP_ID='2'""", [RP_NO])
+
+            #冲款付款金额表,如果是分多次付款的话那么就不能直接删除,是否是付一次款那么就可以直接删除
+            # 如果原来已经付款等于目前传过来的金额那么就可以直接删除
+            #     row_AMTN_MF_MON = cursor.execute("""SELECT AMTN FROM MF_MON WHERE RP_NO=%s AND RP_ID='2'""", [RP_NO]).fetchall()[0][0]
+            #     if float(AMTN_BC)==float(row_AMTN_MF_MON):
+                #修改回冲金额表
+                cursor.execute("""DELETE FROM MF_MON WHERE RP_NO=%s AND RP_ID='2'""", [RP_NO])
+                # else:
+                #     cursor.execute("""UPDATE MF_MON SET AMTN=AMTN+%s-%s,AMTN_REST=AMTN+%s-%s WHERE RP_NO=%s AND RP_ID='2'""",
+                #                [AMTN_BC, row_AMTN, AMTN_BC, row_AMTN, RP_NO])
+                # 删除账户收支单表头
+                cursor.execute("""DELETE FROM MF_BAC WHERE  BB_NO=%s AND BB_ID='BT'""", ['BT'+RP_NO[2::]])
+                # 删除账户收支单表身
+                cursor.execute("""DELETE FROM TF_BAC WHERE  BB_NO=%s AND BB_ID='BT'""", ['BT' + RP_NO[2::]])
+
+                # 如果金额立账金额等于传过来的金额那么就直接删除
+                row_AMTN_MF_ARP = cursor.execute("""SELECT AMTN FROM MF_ARP WHERE BIL_NO=%s""", [MF_LZ1]).fetchall()[0][0]
+                print(float(row_AMTN_MF_ARP))
+                if float(AMTN_BC)==float(row_AMTN_MF_ARP):
+                    cursor.execute(
+                        """UPDATE MF_ARP SET AMTN_RCV=0,AMT=0 WHERE BIL_NO=%s""",[MF_LZ1])
+                    # 回写立账单的数据需要减去之前冲款的金额  row_AMTN
+                else:
+                    cursor.execute(
+                        """UPDATE MF_ARP SET AMTN_RCV=ISNULL(AMTN_RCV,0)-%s,AMT=ISNULL(AMTN,0)-ISNULL(AMTN_RCV,0) WHERE BIL_NO=%s""",[AMTN_BC, MF_LZ1])
+                # 结案掉立账单, 还有进货的立账结案标识
+                cursor.execute("""UPDATE MF_ARP SET CLOSE_ID='F' WHERE BIL_NO=%s""", [MF_LZ1])
+                cursor.execute("""UPDATE MF_PSS SET LZ_CLS_ID='F',CLSLZ='T' WHERE PS_NO=%s""", ['PC' + MF_LZ1[2::]])
+            except Exception:
+                transaction.savepoint_rollback(sid)
+                return http.HttpResponseForbidden("删除付款单sql语句执行异常")
+            transaction.savepoint_commit(sid)
         context = {
             'a': 'delete付款作业'
         }
@@ -299,7 +344,7 @@ class ExpectedOperationView(View):
         USR = request.POST.get("USR")  # 制单人编码                   字符类型   ------------------1
         USR_NAME = request.POST.get("USR_NAME")  # 制单人姓名         字符类型   -----------------------1
         # 判断单号是否存在
-        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO)
+        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO,'RP_ID','2')
         if row_ZL_NO > 0:
             return http.HttpResponseForbidden('单号重复')
 
@@ -369,7 +414,7 @@ class ExpectedOperationView(View):
         CUS_NO_NAME = req_data.get("CUS_NO_NAME")  # 预付款客户名称       字符类型  ---------------------1
         AMTN_BC = req_data.get("AMTN_BC")  # 预付款金额            数字类型  ---------------------------1
         CACC_NO = req_data.get("CACC_NO")  # 预付款银行编码          字符类型    ------------1
-
+        UP_DD = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')  # 修改时间
         # {
         #     "RP_NO":"PT9A220001",
         #     "RP_DD":"2019-10-22",
@@ -380,7 +425,7 @@ class ExpectedOperationView(View):
         # }
 
         # 判断单号是否存在
-        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO)
+        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO,'RP_ID','2')
         if row_ZL_NO == 0:
             return http.HttpResponseForbidden('单号不存在')
 
@@ -390,8 +435,8 @@ class ExpectedOperationView(View):
         with connection.cursor() as cursor:
             # 修改预付款单
             # cursor.execute("SELECT CAST(ISNULL(AMT,0) AS INT ),BIL_NO FROM MF_ARP WHERE BIL_NO=%s", [MF_LZ1])
-            cursor.execute("""UPDATE TF_MON SET RP_DD=%s,CUS_NO=%s,AMTN_BC=%s,BC_NO=%s,CACC_NO=%s WHERE RP_NO=%s""",
-                           [datetime.datetime.strptime(RP_DD, '%Y-%m-%d'),CUS_NO,AMTN_BC,'BT'+RP_NO[2::],CACC_NO,RP_NO])
+            cursor.execute("""UPDATE TF_MON SET RP_DD=%s,CUS_NO=%s,AMTN_BC=%s,BC_NO=%s,CACC_NO=%s,MODIFY_DD=%s,MODIFY_MAN=USR WHERE RP_NO=%s""",
+                           [datetime.datetime.strptime(RP_DD, '%Y-%m-%d'),CUS_NO,AMTN_BC,'BT'+RP_NO[2::],CACC_NO,UP_DD,RP_NO])
 
             # RP_NO   预付款单号  RP_NO
             # RP_DD   预付款日期   datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
@@ -442,8 +487,13 @@ class ExpectedOperationView(View):
         #     "AMTN_BC":888,
         #     "CACC_NO":"01"
         # }
+        # 判断单号是不是已经产生付款单
+        row_ZL_NO = examine_ood('TF_MON', 'IRP_NO', RP_NO,'RP_NO','2')
+        if row_ZL_NO > 0:
+            return http.HttpResponseForbidden('预付款单已经产生付款单')
+
         # 判断单号是否存在
-        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO)
+        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO,'RP_NO','2')
         if row_ZL_NO == 0:
             return http.HttpResponseForbidden('单号不存在')
 
@@ -465,16 +515,27 @@ class ExpectedOperationView(View):
 
 #预付冲应付
 class ExpectedPtOperationView(View):
+
+    @transaction.atomic
     def get(self, request):
+        sid = transaction.savepoint()
         with connection.cursor() as cursor:
-            # 获取预付款单的预付金额和已冲金额,进行对比,如果两个值相同,那么就结案掉预付款单
-            row_AMTN_MF_MON = cursor.execute("""SELECT ISNULL(AMTN_BC,0) AS AMTN_BC, ISNULL(AMTN_CLS,0) AS AMTN_CLS FROM TF_MON  WHERE RP_NO='PT9A220001'""").fetchall()
-            if float(row_AMTN_MF_MON[0][0])==float(row_AMTN_MF_MON[0][1]):
-                cursor.execute("""UPDATE TF_MON SET CLS_ID='T' WHERE RP_NO='PT9A220001'""")
+            # cursor.execute("INSERT INTO CUST(CUS_NO,[NAME])VALUES ('QQ12345',%s)",[a])
+            # cursor.execute("UPDATE PRDT SET NAME='呜呜呜呜' where prd_no = %s", [a])
+            # cursor.execute("select * from prdt where prd_no = %s", [a])
+            try:
+                cursor.execute("""update cust set name='333'  where cus_no='ACDC438'""")
+                cursor.execute("""insert into cust(cus_no,name)values('BAG037211','weqwe')""")
+            except Exception:
+                transaction.savepoint_rollback(sid)
+                return http.HttpResponseForbidden("数据库执行异常")
+            transaction.savepoint_commit(sid)
+
         context = {
             'a': 'get预付冲应付作业'
         }
         return http.JsonResponse(context)
+
     def post(self, request):
 
         req_data = json.loads(request.body.decode())
@@ -489,14 +550,15 @@ class ExpectedPtOperationView(View):
         # print(RP_DD)
         # print(IRP_NO)
         # print(list[0])
-        # 获取总金额
-        sum_ATM=0           #冲款金额
-        yf_sum=0        # 获取单据应付款总金额
+
         # 判断单号是否存在
         row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO)
         if row_ZL_NO > 0:
             return http.HttpResponseForbidden('单号重复')
 
+        # 获取总金额
+        sum_ATM = 0  # 冲款金额
+        yf_sum = 0  # 获取单据应付款总金额
         for i in list:
             sum_ATM = round(sum_ATM + i["ATM"], 4)
             with connection.cursor() as cursor:
@@ -505,7 +567,6 @@ class ExpectedPtOperationView(View):
         if sum_ATM>yf_sum:
             return http.HttpResponseForbidden('冲款金额大于应付金额')
 
-        # 插入预付款表头
         with connection.cursor() as cursor:
             # 插入回冲预付款扣款表
             # cursor.execute("SELECT COUNT(*) FROM CUST WHERE CUS_NO=%s", [CUS_NO])
@@ -568,7 +629,7 @@ class ExpectedPtOperationView(View):
                 if row_AMT <= 0:
                     cursor.execute("""UPDATE MF_ARP SET CLOSE_ID='T' WHERE BIL_NO=%s""", [i["DH"]])
                     cursor.execute("""UPDATE MF_PSS SET LZ_CLS_ID='T',CLSLZ='T' WHERE PS_NO=%s""", ['PC' + i["DH"][2::]])
-
+            print(1)
             # 插入预付款已冲金额表  ------
             cursor.execute(
                 """INSERT INTO MF_MON(RP_NO,RP_DD,RP_ID,DEP,AMTN,AMTN_ARP,AMTN_REST,FJ_NUM)VALUES(%s,%s,2,'0000',0,%s,%s,0)""",
@@ -577,7 +638,7 @@ class ExpectedPtOperationView(View):
             # RP_DD  时间  datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
             # AMTN_ARP  表身总金额   yf_sum
             # AMTN_REST  本次冲款总金额    sum_ATM
-
+            print(2)
             # 更新预付款单已付金额
             cursor.execute(
                 """UPDATE TF_MON SET AMTN_CLS=ISNULL(AMTN_CLS,0)+%s WHERE RP_NO=%s""",
@@ -585,35 +646,204 @@ class ExpectedPtOperationView(View):
 
             # 获取预付款单的预付金额和已冲金额,进行对比,如果两个值相同,那么就结案掉预付款单
             row_AMTN_MF_MON = cursor.execute(
-                """SELECT ISNULL(AMTN_BC,0) AS AMTN_BC, ISNULL(AMTN_CLS,0) AS AMTN_CLS FROM TF_MON  WHERE IRP_NO=%s""",[IRP_NO]).fetchall()
+                """SELECT ISNULL(AMTN_BC,0) AS AMTN_BC, ISNULL(AMTN_CLS,0) AS AMTN_CLS FROM TF_MON  WHERE RP_NO=%s""",[IRP_NO]).fetchall()
             if float(row_AMTN_MF_MON[0][0]) == float(row_AMTN_MF_MON[0][1]):
                 cursor.execute("""UPDATE TF_MON SET CLS_ID='T' WHERE IRP_NO=%s""",[IRP_NO])
 
-
-            print(222222222222222222)
         a = {
-                "RP_NO": "PT9A220002",
-                "IRP_NO": "PT9A220001",
-                "RP_DD":"2019-10-22",
-                "list": [
-                    {"DH": "AP9A200001",
-                     "ATM": 123.4
-                     },
-                    {"DH": "AP9A200002",
-                     "ATM": 123.0001
-                     }
-                ]
-            }
+            "RP_NO": "PT9A230002",
+            "IRP_NO": "PT9A220001",
+            "RP_DD":"2019-10-22",
+            "CUS_NO":"AQ1111",
+            "USR":"A10000",
+            "list": [
+                {"DH": "LP9A230001",
+                 "ATM": 111
+                 },
+                 {"DH": "LP9A230002",
+                 "ATM": 90
+                 }
+            ]
+        }
         context = {
             'a': 'post预付冲应付作业'
         }
         return http.JsonResponse(context)
+
     def put(self, request):
+        req_data = json.loads(request.body.decode())
+        # # 获取参数
+        RP_NO = req_data.get("RP_NO")  # 付款单号         字符类型
+        IRP_NO = req_data.get("IRP_NO")  # 预付款单号         字符类型
+        RP_DD = req_data.get("RP_DD")  # 付款日期         字符类型
+        list = req_data.get("list")  # 付款单号         字符类型
+        UP_DD = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')  # 修改时间
+        # 判断单号是否存在
+        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO)
+        if row_ZL_NO <= 0:
+            return http.HttpResponseForbidden('单号不存在')
+
+        # 获取总金额
+        sum_ATM = 0  # 冲款金额
+        yf_sum = 0  # 获取单据应付款总金额
+        for i in list:
+            sum_ATM = round(sum_ATM + i["ATM"], 4)
+            with connection.cursor() as cursor:
+                # 获取单据应付款总金额
+                yf_sum = round(
+                    yf_sum + cursor.execute("""SELECT AMTN FROM MF_ARP WHERE BIL_NO=%s""", [i["DH"]]).fetchall()[0][0], 4)
+        if sum_ATM > yf_sum:
+            return http.HttpResponseForbidden('冲款金额大于应付金额')
+
+        with connection.cursor() as cursor:
+            # 修改回冲预付款扣款表
+            cursor.execute("""UPDATE  TF_MON1 SET AMTN_CLS=%s WHERE RP_NO=%s""", [sum_ATM, RP_NO])
+            # RP_NO  付款单号  RP_NO
+            # IRP_NO  预付款单号  IRP_NO
+            # AMTN_CLS  付款总金额  sum_ATM
+
+            # 修改付款单表头
+            cursor.execute("""UPDATE TF_MON SET RP_DD=%s,AMTN_CLS=%s,AMTN_IRP=%s,MODIFY_DD=%s,MODIFY_MAN=USR WHERE RP_NO=%s""",
+                           [datetime.datetime.strptime(RP_DD, '%Y-%m-%d'),sum_ATM,sum_ATM,UP_DD,RP_NO])
+            # RP_NO  付款单  RP_NO
+            # RP_DD  付款日期 datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+            # CUS_NO 付款厂商 CUS_NO
+            # AMTN_CLS  付款金额  sum_ATM
+            # AMTN_IRP   金额 sum_ATM
+            # 修改付款单表身,先遍历出每一项的单号和金额,然后在插入
+            itm = 0  # 定义项次
+            YD_ATY = 0 #原来单总金额
+            for i in list:
+                itm += 1
+                print(i["DH"], i["ATM"])  # 得到单号和金额
+                QTY_TC_MON=cursor.execute("""SELECT ISNULL(AMTN_CLS,0) FROM TC_MON WHERE ARP_NO=%s""", ['AP'+i["DH"][2::]]).fetchall()[0][0]
+                YD_ATY = round((YD_ATY + QTY_TC_MON),4)
+                print(i["ATM"])
+                cursor.execute("""UPDATE TC_MON SET AMTN_CLS=%s WHERE ARP_NO=%s""", [i["ATM"],'AP'+i["DH"][2::]])
+
+                # RP_NO 付款单号  RP_NO
+                # ITM2  项次    itm
+                # ARP_NO 立账单号  'AP'+i["DH"][2::]
+                # AMTN_CLS    冲款金额  i["ATM"]
+                # RP_DD   冲款日期  datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+                # CUS_NO  客户  CUS_NO
+                # PRE_ITM  项次  itm
+
+                # 更新立账金额
+                # 回写立账单的数据
+                # 获取原单已冲金额未冲金额
+                #AMTN_RCV 已经冲金额   AMT未冲金额========================================================================================
+                ATM_MF_ARP=cursor.execute("""SELECT ISNULL(AMTN_RCV,0),ISNULL(AMT,0) FROM MF_ARP  WHERE BIL_NO=%s""",[ i["DH"]]).fetchall()
+                AMTN_RCV=ATM_MF_ARP[0][0]    #原单已经冲金额
+                AMT=ATM_MF_ARP[0][1]    #原单未冲金额
+                cursor.execute(
+                    """UPDATE MF_ARP SET AMTN_RCV=ISNULL(AMTN_RCV,0)+%s-%s,AMT=ISNULL(AMTN,0)-ISNULL(AMTN_RCV,0)-%s-%s WHERE BIL_NO=%s""",
+                    [i["ATM"],AMTN_RCV, i["ATM"],AMT, i["DH"]])
+
+                # AMTN_RCV   已冲金额     ISNULL(AMTN_RCV,0)+%s
+                # AMT   未冲金额  ISNULL(AMT,0)-%s
+                # BIL_NO  开票单号
+
+                # 如果单据已经冲完,结案掉立账单,还有进货的立账结案标识
+                row_AMT = cursor.execute("SELECT CAST(ISNULL(AMT,0) AS INT ),BIL_NO FROM MF_ARP WHERE BIL_NO=%s",
+                                         [i["DH"]]).fetchall()[0][0]
+                if row_AMT <= 0:
+                    cursor.execute("""UPDATE MF_ARP SET CLOSE_ID='F' WHERE BIL_NO=%s""", [i["DH"]])
+                    cursor.execute("""UPDATE MF_PSS SET LZ_CLS_ID='F',CLSLZ='F' WHERE PS_NO=%s""",
+                                   ['PC' + i["DH"][2::]])
+
+            # 更新预付款已冲金额表
+            cursor.execute("""UPDATE MF_MON SET AMTN_REST=%s WHERE RP_NO=%s""",[sum_ATM,RP_NO])
+            # RP_NO  单号  RP_NO
+            # RP_DD  时间  datetime.datetime.strptime(RP_DD, '%Y-%m-%d')
+            # AMTN_ARP  应付总金额   yf_sum
+            # AMTN_REST  本次冲款总金额    sum_ATM
+
+            # 更新预付款单已付金额
+            cursor.execute(
+                """UPDATE TF_MON SET AMTN_CLS=ISNULL(AMTN_CLS,0)+%s-%s WHERE RP_NO=%s""",
+                [sum_ATM,YD_ATY,IRP_NO])
+
+            # 获取预付款单的预付金额和已冲金额,进行对比,如果两个值相同,那么就结案掉预付款单
+            row_AMTN_MF_MON = cursor.execute(
+                """SELECT ISNULL(AMTN_BC,0) AS AMTN_BC, ISNULL(AMTN_CLS,0) AS AMTN_CLS FROM TF_MON  WHERE RP_NO=%s""",
+                [IRP_NO]).fetchall()
+
+            if float(row_AMTN_MF_MON[0][0]) == float(row_AMTN_MF_MON[0][1]):
+                cursor.execute("""UPDATE TF_MON SET CLS_ID='F' WHERE IRP_NO=%s""", [IRP_NO])
+        a = {
+            "RP_NO": "PT9A230002",
+            "IRP_NO": "PT9A220001",
+            "RP_DD": "2019-10-22",
+            "list": [
+                {"DH": "LP9A230001",
+                 "ATM": 111
+                 },
+                {"DH": "LP9A230002",
+                 "ATM": 90
+                 }
+            ]
+        }
+
+
         context = {
             'a': 'put预付冲应付作业'
         }
         return http.JsonResponse(context)
+
     def delete(self, request):
+        req_data = json.loads(request.body.decode())
+        # # 获取参数
+        RP_NO = req_data.get("RP_NO")  # 付款单号         字符类型
+
+        # 判断单号是否存在
+        row_ZL_NO = examine_ood('TF_MON', 'RP_NO', RP_NO)
+        if row_ZL_NO <= 0:
+            return http.HttpResponseForbidden('单号不存在')
+        with connection.cursor() as cursor:
+            # 获取原单付款单总金额用于扣减预付款单总金额
+            sum_TF_MON=cursor.execute("""SELECT ISNULL(AMTN_CLS,0) FROM TF_MON WHERE RP_NO=%s""",[RP_NO]).fetchall()[0][0]
+            print(round(sum_TF_MON, 4))
+            #获取预付款单单号
+            TF_MON_NO = cursor.execute("""SELECT IRP_NO FROM TF_MON1 WHERE RP_NO=%s""", [RP_NO]).fetchall()[0][0]
+            print(TF_MON_NO)
+            # 修改预付款扣款金额
+            cursor.execute("""UPDATE TF_MON SET AMTN_CLS=ISNULL(AMTN_CLS,0)-%s WHERE RP_NO=%s""", [sum_TF_MON,TF_MON_NO])
+            # 以列表的形式获取付款单的立账单号
+            TC_MON_RP_NO = cursor.execute("""SELECT ARP_NO FROM TC_MON WHERE RP_NO=%s""", [RP_NO]).fetchall()
+            # 遍历列表得到每一张立账单号
+            for i in TC_MON_RP_NO:
+                print(i[0])  #立账单号
+                # 更新立账金额
+                # 回写立账单的数据
+                # 获取原单已冲金额未冲金额
+                # AMTN_RCV 已经冲金额   AMT未冲金额===========================================================
+                ATM_MF_ARP = cursor.execute("""SELECT ISNULL(AMTN_RCV,0),ISNULL(AMT,0) FROM MF_ARP  WHERE ARP_NO=%s""",[i[0]]).fetchall()
+                AMTN_RCV = ATM_MF_ARP[0][0]  # 原单已经冲金额
+                AMT = ATM_MF_ARP[0][1]  # 原单未冲金额
+                cursor.execute(
+                    """UPDATE MF_ARP SET AMTN_RCV=ISNULL(AMTN_RCV,0)-%s,AMT=ISNULL(AMTN,0)-ISNULL(AMTN_RCV,0)-%s WHERE ARP_NO=%s""",
+                    [AMTN_RCV,AMT,i[0]])
+
+                # AMTN_RCV   已冲金额     ISNULL(AMTN_RCV,0)+%s
+                # AMT   未冲金额  ISNULL(AMT,0)-%s
+                # BIL_NO  开票单号
+
+                # 如果单据已经冲完,结案掉立账单,还有进货的立账结案标识
+                row_AMT = cursor.execute("SELECT CAST(ISNULL(AMT,0) AS INT ),BIL_NO FROM MF_ARP WHERE ARP_NO=%s",[i[0]]).fetchall()[0][0]
+                if row_AMT <= 0:
+                    cursor.execute("""UPDATE MF_ARP SET CLOSE_ID='F' WHERE ARP_NO=%s""", [i[0]])
+                    cursor.execute("""UPDATE MF_PSS SET LZ_CLS_ID='F',CLSLZ='F' WHERE PS_NO=%s""",['PC' + i[0][2::]])
+            # 删除扣款单
+            cursor.execute("""DELETE FROM TF_MON1 WHERE RP_NO=%s""", [RP_NO])
+            #删除付款单表头
+            cursor.execute("""DELETE FROM TF_MON WHERE RP_NO=%s""", [RP_NO])
+            #删除付款单表身
+            cursor.execute("""DELETE FROM TC_MON WHERE RP_NO=%s""", [RP_NO])
+            #删除付款记录
+            cursor.execute("""DELETE FROM MF_MON WHERE RP_NO=%s""", [RP_NO])
+
+
         context = {
             'a': 'delete预付冲应付作业'
         }

+ 221 - 181
awj/awj/apps/stockinvoice/views.py

@@ -5,7 +5,7 @@ from django.shortcuts import render
 
 # Create your views here.
 from django.views import View
-from django.db import connection
+from django.db import connection, transaction
 import datetime
 
 from utils.cust_data import customer_data
@@ -28,14 +28,15 @@ class InvoiceView(View):
         }
         return http.JsonResponse(context)
 
+    @transaction.atomic
     def post(self,request):
         #1,获取参数
         ZL_NO = request.POST.get("ZL_NO")   #单据号码    1         字符型
         LZ_DD = request.POST.get("LZ_DD")   #单据日期    1         字符型
         INV_NO = request.POST.get("INV_NO")   #发票号码    1       字符型
-        INV_DD = request.POST.get("INV_DD")   #发票日期     1      字符型
+        # INV_DD = request.POST.get("INV_DD")   #发票日期     1      字符型               不需要
         ZHANG_ID = request.POST.get("ZHANG_ID")   #立账方式   1    字符型  2开票 1直接立账
-        TAX_ID = request.POST.get("TAX_ID")   #扣税类别   1         字符型
+        # TAX_ID = request.POST.get("TAX_ID")   #扣税类别   1         字符型                    不需要
         AMT = request.POST.get("AMT")   #开票金额        1          数字型
         CUS_NO = request.POST.get("CUS_NO")   #开票客户编码    1    字符型
         CUS_NO_NAME = request.POST.get("CUS_NO_NAME")   #开票客户名称     1       字符型
@@ -47,23 +48,23 @@ class InvoiceView(View):
         USR_NAME = request.POST.get("USR_NAME")   #制单人姓名   1                字符型
         SAL_ID_CODE = request.POST.get("SAL_ID_CODE")   #开户银行账号   1       字符型
         REM = request.POST.get("REM")   #发票备注   1                            字符型
-        print(SAL_KH_BANK)
-        print(SAL_ID_CODE)
-        print(REM)
-        print(ZL_NO)   #单据号码    1
-        print(LZ_DD)   #单据日期    1
-        print(INV_NO)   #发票号码    1
-        print(INV_DD)    #发票日期     1
-        print(ZHANG_ID)    #立账方式   1
-        print(TAX_ID)     #扣税类别   1
-        print(AMT)       #开票金额        1
-        print(CUS_NO)   #开票客户编码    1
-        print(CUS_NO_NAME)      #开票客户名称     1
-        print(UNI_NO_PAY)      #纳税人识别码   1
-        print(SAL_ADR)     #开票客户地址   1
-        print(SAL_TEL)       #开票客户电话   1
-        print(USR)      #制单人编码   1
-        print(USR_NAME)     #制单人姓名   1
+        # print(SAL_KH_BANK)
+        # print(SAL_ID_CODE)
+        # print(REM)
+        # print(ZL_NO)   #单据号码    1
+        # print(LZ_DD)   #单据日期    1
+        # print(INV_NO)   #发票号码    1
+        # print(INV_DD)    #发票日期     1
+        # print(ZHANG_ID)    #立账方式   1
+        # # print(TAX_ID)     #扣税类别   1
+        # print(AMT)       #开票金额        1
+        # print(CUS_NO)   #开票客户编码    1
+        # print(CUS_NO_NAME)      #开票客户名称     1
+        # print(UNI_NO_PAY)      #纳税人识别码   1
+        # print(SAL_ADR)     #开票客户地址   1
+        # print(SAL_TEL)       #开票客户电话   1
+        # print(USR)      #制单人编码   1
+        # print(USR_NAME)     #制单人姓名   1
 
         # 判断立账方式是否有效
         if int(ZHANG_ID) != 1 and int(ZHANG_ID) != 2:
@@ -77,8 +78,9 @@ class InvoiceView(View):
                 return http.HttpResponseForbidden('进货单号已存在')
 
         # 判断开票单号是否存在
-        if int(ZHANG_ID) > 2:
+        if int(ZHANG_ID) == 2:
             row_ZL_NO = examine_ood('MF_LZ1', 'LZ_NO', ZL_NO,'LZ_ID','LP')
+            print(row_ZL_NO)
             if row_ZL_NO > 0:
                 return http.HttpResponseForbidden('开票单号已存在')
 
@@ -91,63 +93,76 @@ class InvoiceView(View):
         with connection.cursor() as cursor:
             #直接开发票
             if int(ZHANG_ID) == 2:
+                sid = transaction.savepoint()  #开启事物
             # 插入进货单表头-----------------------改CUR_ID  RMB
-                cursor.execute("""INSERT INTO MF_PSS(PS_ID,PS_NO,PS_DD,CUS_NO,ZHANG_ID,USR,CHK_MAN,CLS_DATE,SYS_DATE,AMT,EXC_RTO,LZ_CLS_ID,CLSLZ,TAX_ID)
-                VALUES('PC',%s,%s,%s,3,%s,%s,%s,%s,%s,
-                1,'F','F',1)""", ['PC'+ZL_NO[2::], datetime.datetime.strptime(LZ_DD,'%Y-%m-%d'),CUS_NO,USR,USR,datetime.datetime.strptime(LZ_DD,'%Y-%m-%d'),
-                datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),AMT])
-            #插入进货单表身
-                cursor.execute("""INSERT INTO TF_PSS(PS_ID,PS_NO,PS_DD,WH,PRD_NO,QTY,UP,AMT,AMTN_NET,TAX_RTO,ITM,UNIT,CSTN_SAL,PRE_ITM)
-            VALUES('PC',%s,%s,'0000','AQ001',1,%s,%s,%s,5,1,1,%s,1)""", ['PC'+ZL_NO[2::], datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),AMT,AMT,AMT,AMT])
-
-            #增加发票记录
-                cursor.execute("""INSERT INTO INV_NO (BIL_NO,INV_NO,BIL_ID,INV_ID,CUS_NO,TITLE_BUY,TITLE_PAY,AMT,TAX_ID1,TAX_ID2,INV_TT,INV_DD,UNI_NO_PAY,SAL_ADR,SAL_TEL,SAL_KH_BANK,SAL_ID_CODE,REM)
-                VALUES(%s,%s,'LP',21,%s,%s,%s,%s,1,1,1,%s,%s,%s,%s,%s,%s,%s)""",[ZL_NO, INV_NO, CUS_NO, CUS_NO_NAME, CUS_NO_NAME, AMT,datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),
-                 UNI_NO_PAY,SAL_ADR,SAL_TEL,SAL_KH_BANK,SAL_ID_CODE,REM])
-
-            #插入立账单MF_MRP------------------改CUR_ID   RMB
-                cursor.execute("""INSERT INTO MF_ARP(ARP_ID,OPN_ID,ARP_NO,BIL_NO,CUS_NO,PAY_DD,INV_NO,AMT,AMTN,AMTN_NET,EXC_RTO,CLOSE_ID,BIL_ID,SYS_DATE,BIL_DD)VALUES
-                (2,2,%s,%s,%s,%s,%s,%s,%s,%s,1,'F','LP',%s,%s)""", [ 'AP'+ZL_NO[2::],ZL_NO,CUS_NO,datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),INV_NO
-                ,AMT,AMT,AMT,datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')])
-
-            #插入进货开票表头--------------改CUR_ID    RMB
-                cursor.execute("""INSERT INTO MF_LZ1(LZ_ID,LZ_NO,PAY_DD,LZ_DD,CUS_NO,INV_NO,EXC_RTO,USR,CHK_MAN,AMT,AMTN_NET,ARP_NO,CLS_DATE,TAX_ID,ZHANG_ID,
-            SYS_DATE,VOH_CHK,TURN_ID,INV_BIL_ID,INV_BIL_NO,FLG_VOH)VALUES
-            ('LP',%s,%s,%s,%s,%s,1,%s,%s,
-            %s,%s,%s,%s,'1',3,%s,1,1,'LP',%s,'F')""", [ZL_NO, datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')
-            ,datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),CUS_NO,INV_NO,USR,USR,AMT,AMT,'AP'+ZL_NO[2::],datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')
-            ,ZL_NO])
-
-            #插入进货开票表身
-                cursor.execute("""INSERT INTO TF_LZ1(LZ_ID,LZ_NO,ITM,PS_NO,AMT,AMTN_NET,PRD_NO,EST_ITM,PAY_DD,TAX_RTO,QTY,UP,BIL_ID,UNIT,PRD_NAME,CUS_NO,UP_ZG,PRE_ITM)VALUES
-            ('LP',%s,1,%s,%s,%s,'AQ001',1,%s,5,1,1,'PC',1,'服務費用',%s,1,1)""", [ZL_NO,'PC'+ZL_NO[2::],AMT,AMT,datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),CUS_NO])
-
-            #开完发票回写进货单表身已开金额
-                cursor.execute("""UPDATE TF_PSS SET AMT_FP=%s,AMTN_NET_FP=%s WHERE PS_NO=%s""", [AMT,AMT,'PC'+ZL_NO[2::]])
+                try:
+                    cursor.execute("""INSERT INTO MF_PSS(PS_ID,PS_NO,PS_DD,CUS_NO,ZHANG_ID,USR,CHK_MAN,CLS_DATE,SYS_DATE,AMT,EXC_RTO,LZ_CLS_ID,CLSLZ,TAX_ID)
+                    VALUES('PC',%s,%s,%s,3,%s,%s,%s,%s,%s,
+                    1,'F','F',1)""", ['PC'+ZL_NO[2::], datetime.datetime.strptime(LZ_DD,'%Y-%m-%d'),CUS_NO,USR,USR,datetime.datetime.strptime(LZ_DD,'%Y-%m-%d'),
+                    datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),AMT])
+                #插入进货单表身
+                    cursor.execute("""INSERT INTO TF_PSS(PS_ID,PS_NO,PS_DD,WH,PRD_NO,QTY,UP,AMT,AMTN_NET,TAX_RTO,ITM,UNIT,CSTN_SAL,PRE_ITM)
+                VALUES('PC',%s,%s,'0000','AQ001',1,%s,%s,%s,5,1,1,%s,1)""", ['PC'+ZL_NO[2::], datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),AMT,AMT,AMT,AMT])
+
+                #增加发票记录
+                    cursor.execute("""INSERT INTO INV_NO (BIL_NO,INV_NO,BIL_ID,INV_ID,CUS_NO,TITLE_BUY,TITLE_PAY,AMT,TAX_ID1,TAX_ID2,INV_TT,INV_DD,UNI_NO_PAY,SAL_ADR,SAL_TEL,SAL_KH_BANK,SAL_ID_CODE,REM)
+                    VALUES(%s,%s,'LP',21,%s,%s,%s,%s,1,1,1,%s,%s,%s,%s,%s,%s,%s)""",[ZL_NO, INV_NO, CUS_NO, CUS_NO_NAME, CUS_NO_NAME, AMT,datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),
+                     UNI_NO_PAY,SAL_ADR,SAL_TEL,SAL_KH_BANK,SAL_ID_CODE,REM])
+
+                #插入立账单MF_MRP------------------改CUR_ID   RMB
+                    cursor.execute("""INSERT INTO MF_ARP(ARP_ID,OPN_ID,ARP_NO,BIL_NO,CUS_NO,PAY_DD,INV_NO,AMT,AMTN,AMTN_NET,EXC_RTO,CLOSE_ID,BIL_ID,SYS_DATE,BIL_DD)VALUES
+                    (2,2,%s,%s,%s,%s,%s,%s,%s,%s,1,'F','LP',%s,%s)""", [ 'AP'+ZL_NO[2::],ZL_NO,CUS_NO,datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),INV_NO
+                    ,AMT,AMT,AMT,datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')])
+
+                #插入进货开票表头--------------改CUR_ID    RMB
+                    cursor.execute("""INSERT INTO MF_LZ1(LZ_ID,LZ_NO,PAY_DD,LZ_DD,CUS_NO,INV_NO,EXC_RTO,USR,CHK_MAN,AMT,AMTN_NET,ARP_NO,CLS_DATE,TAX_ID,ZHANG_ID,
+                SYS_DATE,VOH_CHK,TURN_ID,INV_BIL_ID,INV_BIL_NO,FLG_VOH)VALUES
+                ('LP',%s,%s,%s,%s,%s,1,%s,%s,
+                %s,%s,%s,%s,'1',3,%s,1,1,'LP',%s,'F')""", [ZL_NO, datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')
+                ,datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),CUS_NO,INV_NO,USR,USR,AMT,AMT,'AP'+ZL_NO[2::],datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')
+                ,ZL_NO])
+
+                #插入进货开票表身
+                    cursor.execute("""INSERT INTO TF_LZ1(LZ_ID,LZ_NO,ITM,PS_NO,AMT,AMTN_NET,PRD_NO,EST_ITM,PAY_DD,TAX_RTO,QTY,UP,BIL_ID,UNIT,PRD_NAME,CUS_NO,UP_ZG,PRE_ITM)VALUES
+                ('LP',%s,1,%s,%s,%s,'AQ001',1,%s,5,1,1,'PC',1,'服務費用',%s,1,1)""", [ZL_NO,'PC'+ZL_NO[2::],AMT,AMT,datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),CUS_NO])
+
+                #开完发票回写进货单表身已开金额
+                    cursor.execute("""UPDATE TF_PSS SET AMT_FP=%s,AMTN_NET_FP=%s WHERE PS_NO=%s""", [AMT,AMT,'PC'+ZL_NO[2::]])
+                except Exception:
+                    transaction.savepoint_rollback(sid)   #语句执行错误,进行语句回滚
+                    return http.HttpResponseForbidden("增加开票作业sql语句执行错误")
+                transaction.savepoint_commit(sid)   #提交事物
             # 不开发票直接产生进货单,并且立账
             if int(ZHANG_ID) == 1:
-                # 插入进货单表头-----------------------改CUR_ID  RMB
-                cursor.execute("""INSERT INTO MF_PSS(PS_ID,PS_NO,PS_DD,CUS_NO,ZHANG_ID,USR,CHK_MAN,CLS_DATE,SYS_DATE,AMT,EXC_RTO,LZ_CLS_ID,CLSLZ,TAX_ID,ARP_NO)
-                    VALUES('PC',%s,%s,%s,1,%s,%s,%s,%s,%s,
-                    1,'F','F',1,%s)""",
-                               ['PC' + ZL_NO[2::], datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), CUS_NO, USR, USR,
-                                datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),
-                                datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), AMT,'AP' + ZL_NO[2::]])
-                # 插入进货单表身
-                cursor.execute("""INSERT INTO TF_PSS(PS_ID,PS_NO,PS_DD,WH,PRD_NO,QTY,UP,AMT,AMTN_NET,TAX_RTO,ITM,UNIT,CSTN_SAL,PRE_ITM)
-                VALUES('PC',%s,%s,'0000','AQ001',1,%s,%s,%s,5,1,1,%s,1)""",
-                               ['PC' + ZL_NO[2::], datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), AMT, AMT, AMT, AMT])
-                # 插入立账单MF_MRP------------------改CUR_ID   RMB
-                cursor.execute("""INSERT INTO MF_ARP(ARP_ID,OPN_ID,ARP_NO,BIL_NO,CUS_NO,PAY_DD,INV_NO,AMT,AMTN,AMTN_NET,EXC_RTO,CLOSE_ID,BIL_ID,SYS_DATE,BIL_DD)VALUES
-                                (2,2,%s,%s,%s,%s,%s,%s,%s,%s,1,'F','PC',%s,%s)""",
-                               ['AP' + ZL_NO[2::], 'PC' + ZL_NO[2::], CUS_NO, datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), INV_NO
-                                   , AMT, AMT, AMT, datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),
-                                datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')])
+                sid = transaction.savepoint()  #开启事物
+                try:
+                    # 插入进货单表头-----------------------改CUR_ID  RMB
+                    cursor.execute("""INSERT INTO MF_PSS(PS_ID,PS_NO,PS_DD,CUS_NO,ZHANG_ID,USR,CHK_MAN,CLS_DATE,SYS_DATE,AMT,EXC_RTO,LZ_CLS_ID,CLSLZ,TAX_ID,ARP_NO)
+                        VALUES('PC',%s,%s,%s,1,%s,%s,%s,%s,%s,
+                        1,'F','F',1,%s)""",
+                                   ['PC' + ZL_NO[2::], datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), CUS_NO, USR, USR,
+                                    datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),
+                                    datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), AMT,'AP' + ZL_NO[2::]])
+                    # 插入进货单表身
+                    cursor.execute("""INSERT INTO TF_PSS(PS_ID,PS_NO,PS_DD,WH,PRD_NO,QTY,UP,AMT,AMTN_NET,TAX_RTO,ITM,UNIT,CSTN_SAL,PRE_ITM)
+                    VALUES('PC',%s,%s,'0000','AQ001',1,%s,%s,%s,5,1,1,%s,1)""",
+                                   ['PC' + ZL_NO[2::], datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), AMT, AMT, AMT, AMT])
+                    # 插入立账单MF_MRP------------------改CUR_ID   RMB
+                    cursor.execute("""INSERT INTO MF_ARP(ARP_ID,OPN_ID,ARP_NO,BIL_NO,CUS_NO,PAY_DD,INV_NO,AMT,AMTN,AMTN_NET,EXC_RTO,CLOSE_ID,BIL_ID,SYS_DATE,BIL_DD)VALUES
+                                    (2,2,%s,%s,%s,%s,%s,%s,%s,%s,1,'F','PC',%s,%s)""",
+                                   ['AP' + ZL_NO[2::], 'PC' + ZL_NO[2::], CUS_NO, datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), INV_NO
+                                       , AMT, AMT, AMT, datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),
+                                    datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')])
+                except Exception:
+                    transaction.savepoint_rollback(sid)   #语句执行错误,进行语句回滚
+                    return http.HttpResponseForbidden("新增进货单sql语句执行错误")
+                transaction.savepoint_commit(sid)   #提交事物
         context = {
             'a': 'POST提交'
         }
         return http.JsonResponse(context)
 
+    @transaction.atomic
     def put(self, request):
 
         # 获取传过来的参数
@@ -158,8 +173,8 @@ class InvoiceView(View):
         INV_NO = req_data.get("INV_NO")  # 发票号码    1
         INV_DD = req_data.get("INV_DD")  # 发票日期     1
         ZHANG_ID = req_data.get("ZHANG_ID")  # 立账方式   1
-        TAX_ID = req_data.get("TAX_ID")  # 扣税类别   1
-        AMT = req_data.get("AMT")  # 开票金额        1
+        # TAX_ID = req_data.get("TAX_ID")  # 扣税类别   1
+        AMT = req_data.get("AMT")  # 开票金额        1                  不需要
         CUS_NO = req_data.get("CUS_NO")  # 开票客户编码    1
         CUS_NO_NAME = req_data.get("CUS_NO_NAME")  # 开票客户名称     1
         UNI_NO_PAY = req_data.get("UNI_NO_PAY")  # 纳税人识别码   1
@@ -213,94 +228,107 @@ class InvoiceView(View):
         with connection.cursor() as cursor:
             #进货开票
             if int(ZHANG_ID)==2:
-                # 修改进货单表头
-                # PS_NO  单号  单号不能改用作条件
-                # PS_DD  日期
-                # CUS_NO 客户编码
-                # ZHANG_ID  立账方式
-                # USR  用户
-                # CHK_MAN   审核人
-                # AMT  金额
-                cursor.execute("""UPDATE MF_PSS SET PS_DD=%s,CUS_NO=%s,ZHANG_ID=%s,AMT=%s,MODIFY_DD=%s,MODIFY_MAN=USR WHERE PS_NO=%s AND PS_ID='PC'""", [datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), CUS_NO,ZHANG_ID,AMT,UP_DD,'PC'+ZL_NO[2::]])
-
-                # 修改进货单表身
-                # PS_DD日期
-                #UP  单价
-                #AMT 金额
-                # AMTN_NET  金额
-                # CSTN_SAL  立账金额
-                cursor.execute("""UPDATE TF_PSS SET PS_DD=%s,UP=%s,AMTN_NET=%s,CSTN_SAL=%s,AMT=%s,AMT_FP=%s,AMTN_NET_FP=%s WHERE PS_NO=%s AND PS_ID='PC'""", [datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), AMT,AMT,AMT,AMT,AMT, AMT,'PC'+ZL_NO[2::]])
-
-                # 修改发票记录
-                cursor.execute("""UPDATE INV_NO SET INV_NO=%s,CUS_NO=%s,TITLE_BUY=%s,TITLE_PAY=%s,AMT=%s,INV_DD=%s,UNI_NO_PAY=%s,SAL_ADR=%s,SAL_TEL=%s,SAL_KH_BANK=%s,SAL_ID_CODE=%s,REM=%s WHERE BIL_NO=%s AND BIL_ID='LP'""",
-                               [INV_NO, CUS_NO,CUS_NO_NAME,CUS_NO_NAME,AMT,INV_DD,UNI_NO_PAY,SAL_ADR,SAL_TEL,SAL_KH_BANK,SAL_ID_CODE,REM,ZL_NO])
-                # BIL_NO   开票单号  ZL_NO
-                # INV_NO 开票号码       INV_NO
-                #  CUS_NO 开票客户编码   CUS_NO
-                # TITLE_BUY  客户名   CUS_NO_NAME
-                # TITLE_PAY  客户名   CUS_NO_NAME
-                # AMT    金额    AMT
-                #INV_DD  日期  INV_DD
-                #UNI_NO_PAY       UNI_NO_PAY
-                # SAL_ADR      SAL_ADR
-                #SAL_TEL       SAL_TEL
-                # SAL_KH_BANK      SAL_KH_BANK
-                # SAL_ID_CODE       SAL_ID_CODE
-                #REM        REM
-
-                # 修改立账单MF_MRP
-                cursor.execute("""UPDATE MF_ARP SET CUS_NO=%s,PAY_DD=%s,INV_NO=%s,AMT=%s,AMTN=%s,AMTN_NET=%s,SYS_DATE=%s WHERE ARP_NO=%s AND BIL_ID='LP'""", [CUS_NO, datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),
-                  INV_NO,AMT,AMT,AMT,datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),'AP' + ZL_NO[2::]])
-                # ARP_NO  立账单号  'AP' + ZL_NO[2::]
-                # CUS_NO   客户代号  CUS_NO
-                # PAY_DD  立账日期   datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')
-                #INV_NO   发票号码   INV_NO
-                # AMT  金额   AMT
-                # AMTN        AMT
-                # AMTN_NET    AMT
-                # SYS_DATE   录入日期  datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')
-
-                # 插入进货开票表头
-                cursor.execute("""UPDATE MF_LZ1 SET PAY_DD=%s,LZ_DD=%s,CUS_NO=%s,INV_NO=%s,AMT=%s,AMTN_NET=%s,MODIFY_DD=%s,MODIFY_MAN=USR WHERE LZ_NO=%s AND LZ_ID='LP'""",
-                               [datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),CUS_NO,INV_NO,AMT,AMT,UP_DD,ZL_NO])
-                # LZ_NO  单号  ZL_NO
-                # PAY_DD 日期    datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')
-                # LZ_DD  日期    datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')
-                # CUS_NO  客户编码    CUS_NO
-                # INV_NO  发票号码    INV_NO
-                # AMT 金额  AMT
-                # AMTN_NET    金额  AMT
-
-                # 修改进货开票表身
-                cursor.execute("""UPDATE TF_LZ1 SET AMT=%s,AMTN_NET=%s,CUS_NO=%s WHERE LZ_NO=%s AND LZ_ID='LP'""", [AMT, AMT,CUS_NO,ZL_NO])
-                # LZ_NO  单号  ZL_NO
-                # AMT    金额   AMT
-                # AMTN_NET    金额  AMT
-                # CUS_NO  客户  CUS_NO
-                # # 修改开完发票回写进货单表身已开金额
-                # cursor.execute("""UPDATE TF_PSS SET AMT_FP=%s,AMTN_NET_FP=%s WHERE PS_NO=%s""",[AMT, AMT, 'PC' + ZL_NO[2::]])
+                sid = transaction.savepoint()  # 开启事物
+                try:
+                    # 修改进货单表头
+                    # PS_NO  单号  单号不能改用作条件
+                    # PS_DD  日期
+                    # CUS_NO 客户编码
+                    # ZHANG_ID  立账方式
+                    # USR  用户
+                    # CHK_MAN   审核人
+                    # AMT  金额
+                    cursor.execute("""UPDATE MF_PSS SET PS_DD=%s,CUS_NO=%s,ZHANG_ID=%s,AMT=%s,MODIFY_DD=%s,MODIFY_MAN=USR WHERE PS_NO=%s AND PS_ID='PC'""", [datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), CUS_NO,ZHANG_ID,AMT,UP_DD,'PC'+ZL_NO[2::]])
+
+                    # 修改进货单表身
+                    # PS_DD日期
+                    #UP  单价
+                    #AMT 金额
+                    # AMTN_NET  金额
+                    # CSTN_SAL  立账金额
+                    cursor.execute("""UPDATE TF_PSS SET PS_DD=%s,UP=%s,AMTN_NET=%s,CSTN_SAL=%s,AMT=%s,AMT_FP=%s,AMTN_NET_FP=%s WHERE PS_NO=%s AND PS_ID='PC'""", [datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), AMT,AMT,AMT,AMT,AMT, AMT,'PC'+ZL_NO[2::]])
+
+                    # 修改发票记录
+                    cursor.execute("""UPDATE INV_NO SET INV_NO=%s,CUS_NO=%s,TITLE_BUY=%s,TITLE_PAY=%s,AMT=%s,INV_DD=%s,UNI_NO_PAY=%s,SAL_ADR=%s,SAL_TEL=%s,SAL_KH_BANK=%s,SAL_ID_CODE=%s,REM=%s WHERE BIL_NO=%s AND BIL_ID='LP'""",
+                                   [INV_NO, CUS_NO,CUS_NO_NAME,CUS_NO_NAME,AMT,INV_DD,UNI_NO_PAY,SAL_ADR,SAL_TEL,SAL_KH_BANK,SAL_ID_CODE,REM,ZL_NO])
+                    # BIL_NO   开票单号  ZL_NO
+                    # INV_NO 开票号码       INV_NO
+                    #  CUS_NO 开票客户编码   CUS_NO
+                    # TITLE_BUY  客户名   CUS_NO_NAME
+                    # TITLE_PAY  客户名   CUS_NO_NAME
+                    # AMT    金额    AMT
+                    #INV_DD  日期  INV_DD
+                    #UNI_NO_PAY       UNI_NO_PAY
+                    # SAL_ADR      SAL_ADR
+                    #SAL_TEL       SAL_TEL
+                    # SAL_KH_BANK      SAL_KH_BANK
+                    # SAL_ID_CODE       SAL_ID_CODE
+                    #REM        REM
+
+                    # 修改立账单MF_MRP
+                    cursor.execute("""UPDATE MF_ARP SET CUS_NO=%s,PAY_DD=%s,INV_NO=%s,AMT=%s,AMTN=%s,AMTN_NET=%s,SYS_DATE=%s WHERE ARP_NO=%s AND BIL_ID='LP'""", [CUS_NO, datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),
+                      INV_NO,AMT,AMT,AMT,datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),'AP' + ZL_NO[2::]])
+                    # ARP_NO  立账单号  'AP' + ZL_NO[2::]
+                    # CUS_NO   客户代号  CUS_NO
+                    # PAY_DD  立账日期   datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')
+                    #INV_NO   发票号码   INV_NO
+                    # AMT  金额   AMT
+                    # AMTN        AMT
+                    # AMTN_NET    AMT
+                    # SYS_DATE   录入日期  datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')
+
+                    # 插入进货开票表头
+                    cursor.execute("""UPDATE MF_LZ1 SET PAY_DD=%s,LZ_DD=%s,CUS_NO=%s,INV_NO=%s,AMT=%s,AMTN_NET=%s,MODIFY_DD=%s,MODIFY_MAN=USR WHERE LZ_NO=%s AND LZ_ID='LP'""",
+                                   [datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'),CUS_NO,INV_NO,AMT,AMT,UP_DD,ZL_NO])
+                    # LZ_NO  单号  ZL_NO
+                    # PAY_DD 日期    datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')
+                    # LZ_DD  日期    datetime.datetime.strptime(LZ_DD, '%Y-%m-%d')
+                    # CUS_NO  客户编码    CUS_NO
+                    # INV_NO  发票号码    INV_NO
+                    # AMT 金额  AMT
+                    # AMTN_NET    金额  AMT
+
+                    # 修改进货开票表身
+                    cursor.execute("""UPDATE TF_LZ1 SET AMT=%s,AMTN_NET=%s,CUS_NO=%s WHERE LZ_NO=%s AND LZ_ID='LP'""", [AMT, AMT,CUS_NO,ZL_NO])
+                    # LZ_NO  单号  ZL_NO
+                    # AMT    金额   AMT
+                    # AMTN_NET    金额  AMT
+                    # CUS_NO  客户  CUS_NO
+                    # # 修改开完发票回写进货单表身已开金额
+                    # cursor.execute("""UPDATE TF_PSS SET AMT_FP=%s,AMTN_NET_FP=%s WHERE PS_NO=%s""",[AMT, AMT, 'PC' + ZL_NO[2::]])
+                except Exception:
+                    transaction.savepoint_rollback(sid)  # 语句执行错误,进行语句回滚
+                    return http.HttpResponseForbidden("修改进货开票sql语句执行错误")
+                transaction.savepoint_commit(sid)  # 提交事物
             # 不开票
             if int(ZHANG_ID)==1:
-                cursor.execute(
-                    """UPDATE MF_PSS SET PS_DD=%s,CUS_NO=%s,ZHANG_ID=%s,AMT=%s,MODIFY_DD=%s,MODIFY_MAN=USR WHERE PS_NO=%s AND PS_ID='PC'""",
-                    [datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), CUS_NO, ZHANG_ID, AMT, UP_DD, 'PC' + ZL_NO[2::]])
-                # 修改进货单表身
-                # PS_DD日期
-                # UP  单价
-                # AMT 金额
-                # AMTN_NET  金额
-                # CSTN_SAL  立账金额
-                cursor.execute("""UPDATE TF_PSS SET PS_DD=%s,UP=%s,AMTN_NET=%s,CSTN_SAL=%s,AMT=%s WHERE PS_NO=%s AND PS_ID='PC'""",
-                               [datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), AMT, AMT, AMT, AMT, 'PC' + ZL_NO[2::]])
-                # 修改立账金额
-                cursor.execute(
-                    """UPDATE MF_ARP SET CUS_NO=%s,PAY_DD=%s,AMT=%s,AMTN=%s,AMTN_NET=%s,SYS_DATE=%s WHERE ARP_NO=%s AND BIL_ID='PC'""",
-                    [CUS_NO, datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), AMT, AMT, AMT, datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), 'AP' + ZL_NO[2::]])
+                sid = transaction.savepoint()  # 开启事物
+                try:
+                    cursor.execute(
+                        """UPDATE MF_PSS SET PS_DD=%s,CUS_NO=%s,ZHANG_ID=%s,AMT=%s,MODIFY_DD=%s,MODIFY_MAN=USR WHERE PS_NO=%s AND PS_ID='PC'""",
+                        [datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), CUS_NO, ZHANG_ID, AMT, UP_DD, 'PC' + ZL_NO[2::]])
+                    # 修改进货单表身
+                    # PS_DD日期
+                    # UP  单价
+                    # AMT 金额
+                    # AMTN_NET  金额
+                    # CSTN_SAL  立账金额
+                    cursor.execute("""UPDATE TF_PSS SET PS_DD=%s,UP=%s,AMTN_NET=%s,CSTN_SAL=%s,AMT=%s WHERE PS_NO=%s AND PS_ID='PC'""",
+                                   [datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), AMT, AMT, AMT, AMT, 'PC' + ZL_NO[2::]])
+                    # 修改立账金额
+                    cursor.execute(
+                        """UPDATE MF_ARP SET CUS_NO=%s,PAY_DD=%s,AMT=%s,AMTN=%s,AMTN_NET=%s,SYS_DATE=%s WHERE ARP_NO=%s AND BIL_ID='PC'""",
+                        [CUS_NO, datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), AMT, AMT, AMT, datetime.datetime.strptime(LZ_DD, '%Y-%m-%d'), 'AP' + ZL_NO[2::]])
+                except Exception:
+                    transaction.savepoint_rollback(sid)  # 语句执行错误,进行语句回滚
+                    return http.HttpResponseForbidden("修改进货单sql语句执行错误")
+                transaction.savepoint_commit(sid)  # 提交事物
         context = {
             'a': 'PUT提交'
         }
         return http.JsonResponse(context)
 
+    @transaction.atomic
     def delete(self, request):
 
         # 获取传过来的参数
@@ -317,7 +345,7 @@ class InvoiceView(View):
             if row_ZL_NO <= 0:
                 return http.HttpResponseForbidden('进货单号不存在')
         # 判断开票单号是否存在
-        if int(ZHANG_ID) > 2:
+        if int(ZHANG_ID) == 2:
             row_ZL_NO = examine_ood('MF_LZ1', 'LZ_NO', ZL_NO,'LZ_ID','LP')
             if row_ZL_NO <= 0:
                 return http.HttpResponseForbidden('开票单号不存在')
@@ -325,35 +353,47 @@ class InvoiceView(View):
         with connection.cursor() as cursor:
 
             if int(ZHANG_ID)==2:
-                # 判断是否产生后续单据
-                sql = "SELECT COUNT(*) FROM %s WHERE %s='%s'" % ('TC_MON', 'ARP_NO', 'AP' + ZL_NO[2::])
-                row_ZL_NO = cursor.execute(sql).fetchall()[0][0]
-                if row_ZL_NO > 0:
-                    return http.HttpResponseForbidden('开票单或进货单已产生后续收付款单')
-                # 删除进货来票作业表头
-                cursor.execute("""DELETE FROM TF_LZ1 WHERE  LZ_NO=%s AND LZ_ID='LP'""", [ZL_NO])
-                # 删除进货开票作业表身
-                cursor.execute("""DELETE FROM MF_LZ1 WHERE  LZ_NO=%s AND LZ_ID='LP'""", [ZL_NO])
-                #删除立账金额单据
-                cursor.execute("""DELETE FROM MF_ARP WHERE BIL_NO=%s AND BIL_ID='LP'""", [ZL_NO])
-                #删除发票单据
-                cursor.execute("""DELETE FROM INV_NO  WHERE BIL_NO=%s AND BIL_ID='LP'""", [ZL_NO])
-                # 删除进货单表身单据
-                cursor.execute("""DELETE FROM MF_PSS WHERE PS_NO=%s AND PS_ID='PC'""", ['PC' + ZL_NO[2::]])
-                # 删除进货单表头单据
-                cursor.execute("""DELETE FROM TF_PSS WHERE PS_NO=%s AND PS_ID='PC'""", ['PC' + ZL_NO[2::]])
+                sid = transaction.savepoint()  # 开启事物
+                try:
+                    # 判断是否产生后续单据
+                    sql = "SELECT COUNT(*) FROM %s WHERE %s='%s'" % ('TC_MON', 'ARP_NO', 'AP' + ZL_NO[2::])
+                    row_ZL_NO = cursor.execute(sql).fetchall()[0][0]
+                    if row_ZL_NO > 0:
+                        return http.HttpResponseForbidden('开票单或进货单已产生后续收付款单')
+                    # 删除进货来票作业表头
+                    cursor.execute("""DELETE FROM TF_LZ1 WHERE  LZ_NO=%s AND LZ_ID='LP'""", [ZL_NO])
+                    # 删除进货开票作业表身
+                    cursor.execute("""DELETE FROM MF_LZ1 WHERE  LZ_NO=%s AND LZ_ID='LP'""", [ZL_NO])
+                    #删除立账金额单据
+                    cursor.execute("""DELETE FROM MF_ARP WHERE BIL_NO=%s AND BIL_ID='LP'""", [ZL_NO])
+                    #删除发票单据
+                    cursor.execute("""DELETE FROM INV_NO  WHERE BIL_NO=%s AND BIL_ID='LP'""", [ZL_NO])
+                    # 删除进货单表身单据
+                    cursor.execute("""DELETE FROM MF_PSS WHERE PS_NO=%s AND PS_ID='PC'""", ['PC' + ZL_NO[2::]])
+                    # 删除进货单表头单据
+                    cursor.execute("""DELETE FROM TF_PSS WHERE PS_NO=%s AND PS_ID='PC'""", ['PC' + ZL_NO[2::]])
+                except Exception:
+                    transaction.savepoint_rollback(sid)  # 语句执行错误,进行语句回滚
+                    return http.HttpResponseForbidden("删除开票作业sql语句执行错误")
+                transaction.savepoint_commit(sid)  # 提交事物
             if int(ZHANG_ID)==1:
 
-                # 判断是否产生后续单据
-                sql = "SELECT COUNT(*) FROM %s WHERE %s='%s'" % ('TC_MON', 'ARP_NO', 'AP' + ZL_NO[2::])
-                row_ZL_NO = cursor.execute(sql).fetchall()[0][0]
-                if row_ZL_NO > 0:
-                    return http.HttpResponseForbidden('开票单或进货单已产生后续收付款单')
-                cursor.execute("""DELETE FROM MF_PSS WHERE PS_NO=%s AND PS_ID='PC'""", ['PC' + ZL_NO[2::]])
-                # 删除进货单表头单据
-                cursor.execute("""DELETE FROM TF_PSS WHERE PS_NO=%s AND PS_ID='PC'""", ['PC' + ZL_NO[2::]])
-                # 删除进货单表身单据
-                cursor.execute("""DELETE FROM MF_ARP WHERE BIL_NO=%s AND BIL_ID='PC'""", ['PC' + ZL_NO[2::]])
+                sid = transaction.savepoint()  # 开启事物
+                try:
+                    # 判断是否产生后续单据
+                    sql = "SELECT COUNT(*) FROM %s WHERE %s='%s'" % ('TC_MON', 'ARP_NO', 'AP' + ZL_NO[2::])
+                    row_ZL_NO = cursor.execute(sql).fetchall()[0][0]
+                    if row_ZL_NO > 0:
+                        return http.HttpResponseForbidden('开票单或进货单已产生后续收付款单')
+                    cursor.execute("""DELETE FROM MF_PSS WHERE PS_NO=%s AND PS_ID='PC'""", ['PC' + ZL_NO[2::]])
+                    # 删除进货单表头单据
+                    cursor.execute("""DELETE FROM TF_PSS WHERE PS_NO=%s AND PS_ID='PC'""", ['PC' + ZL_NO[2::]])
+                    # 删除进货单表身单据
+                    cursor.execute("""DELETE FROM MF_ARP WHERE BIL_NO=%s AND BIL_ID='PC'""", ['PC' + ZL_NO[2::]])
+                except Exception:
+                    transaction.savepoint_rollback(sid)  # 语句执行错误,进行语句回滚
+                    return http.HttpResponseForbidden("删除进货单sql语句执行错误")
+                transaction.savepoint_commit(sid)  # 提交事物
         context = {
             'a': 'DELETE提交'
         }

+ 3 - 23
awj/awj/apps/stockinvoice/xixi.py

@@ -1,24 +1,4 @@
-from pip._vendor.pytoml.writer import long
-
-a = {
-    "RP_NO":"PT9A220002",
-    "IRP_NO":"PT9A220001",
-    "list":[
-        {"1":"AP9A200001",
-            "金额":123.4
-         },
-        {"2": "AP9A170002",
-            "金额": 123.2
-         },
-        {"3":"AP9A200002",
-            "金额":123.1
-         },
-    ]
-}
-b = a["list"]
-sum1 = int(0)
-print(sum1)
-for i in b:
-    sum1 += i["金额"]
-print(type(round( sum1,6)))
 
+a=1
+if int(1)!=2:
+    print(3)

+ 4 - 3
awj/awj/utils/examine_ood.py

@@ -2,16 +2,17 @@ from django.db import connection
 
 # 用于判断进货开票单是否存在
 
-def examine_ood(TB,TYPE,OOD):
+def examine_ood(TB,TYPE,OOD,BIL_NO,BLI_NAME):
     """
 
     :param TB: 表名
     :param TYPE: 字段名
     :param OOD: 单号
+    :param BIL_NO: 单据id
+    :param BLI_NAME: 单据id值
     :return:
     """
-    sql = "SELECT COUNT(*) FROM %s WHERE %s='%s' " % (TB, TYPE, OOD)
-    print(sql)
+    sql = "SELECT COUNT(*) FROM %s WHERE %s='%s' AND %s='%s'" % (TB, TYPE, OOD,BIL_NO,BLI_NAME)
     with connection.cursor() as cursor:
         # 判断单号是否重复
         row_ZL_NO = cursor.execute(sql).fetchall()[0][0]

+ 23 - 0
awj/awj/utils/invoice_dh.py

@@ -0,0 +1,23 @@
+from django import http
+
+from utils.examine_ood import examine_ood
+
+
+def invoice_dh(ZHANG_ID,ZL_NO,BIL_NO,BLI_NAME):
+    if int(ZHANG_ID) != 1 and int(ZHANG_ID) != 2:
+        return http.HttpResponseForbidden('立账方式只能是1(开票),2(不开票)')
+    # 判断进货单号是存在
+    if int(ZHANG_ID) == 1:
+        row_ZL_NO = examine_ood('MF_PSS', 'PS_NO', 'PC' + ZL_NO[2::],BIL_NO,BLI_NAME)
+        if row_ZL_NO == 0:
+            return http.HttpResponseForbidden('进货单号不存在')
+    # 判断开票单号是否存在
+    if int(ZHANG_ID) == 2:
+        row_ZL_NO = examine_ood('MF_LZ1', 'LZ_NO', ZL_NO,BIL_NO,BLI_NAME)
+        if row_ZL_NO == 0:
+            return http.HttpResponseForbidden('开票单号不存在')
+    # 判断是否产生后续单据
+    row_ZL_NO = examine_ood('TC_MON', 'ARP_NO', 'AP'+ZL_NO[2::],BIL_NO,BLI_NAME)
+    print(row_ZL_NO)
+    if row_ZL_NO > 0:
+        return http.HttpResponseForbidden('开票单或进货单已产生后续收付款单')

+ 101 - 0
awj/awj/utils/response_code.py

@@ -0,0 +1,101 @@
+# coding:utf-8
+
+class RET:
+    OK                  = "0"
+    DBERR               = "4001"
+    NODATA              = "4002"
+    DATAEXIST           = "4003"
+    DATAERR             = "4004"
+    SESSIONERR          = "4101"
+    LOGINERR            = "4102"
+    PARAMERR            = "4103"
+    USERERR             = "4104"
+    ROLEERR             = "4105"
+    PWDERR              = "4106"
+    CPWDERR             = "4107"
+    MOBILEERR           = "4108"
+    REQERR              = "4201"
+    IPERR               = "4202"
+    THIRDERR            = "4301"
+    IOERR               = "4302"
+    SERVERERR           = "4500"
+    UNKOWNERR           = "4501"
+    NECESSARYPARAMERR   = "4502"
+    SMSCODERR           = "4503"
+    ALLOWERR            = "4504"
+
+
+error_map = {
+    RET.OK                    : u"成功",
+    RET.DBERR                 : u"数据库查询错误",
+    RET.NODATA                : u"无数据",
+    RET.DATAEXIST             : u"数据已存在",
+    RET.DATAERR               : u"数据错误",
+    RET.SESSIONERR            : u"用户未登录",
+    RET.LOGINERR              : u"用户登录失败",
+    RET.PARAMERR              : u"参数错误",
+    RET.USERERR               : u"用户不存在或未激活",
+    RET.ROLEERR               : u"用户身份错误",
+    RET.PWDERR                : u"密码错误",
+    RET.CPWDERR               : u"密码不一致",
+    RET.MOBILEERR             : u"手机号错误",
+    RET.REQERR                : u"非法请求或请求次数受限",
+    RET.IPERR                 : u"IP受限",
+    RET.THIRDERR              : u"第三方系统错误",
+    RET.IOERR                 : u"文件读写错误",
+    RET.SERVERERR             : u"内部错误",
+    RET.UNKOWNERR             : u"未知错误",
+    RET.NECESSARYPARAMERR     : u"缺少必传参数",
+    RET.SMSCODERR             : u"短信验证码有误",
+    RET.ALLOWERR              : u"未勾选协议",
+}
+
+
+
+# coding:utf-8
+
+class RETCODE:
+    OK                  = "0"
+    IMAGECODEERR        = "4001"
+    THROTTLINGERR       = "4002"
+    NECESSARYPARAMERR   = "4003"
+    USERERR             = "4004"
+    PWDERR              = "4005"
+    CPWDERR             = "4006"
+    MOBILEERR           = "4007"
+    SMSCODERR           = "4008"
+    ALLOWERR            = "4009"
+    SESSIONERR          = "4101"
+    DBERR               = "5000"
+    EMAILERR            = "5001"
+    TELERR              = "5002"
+    NODATAERR           = "5003"
+    NEWPWDERR           = "5004"
+
+
+    REQERR              = "4201"
+    IPERR               = "4202"
+    THIRDERR            = "4301"
+    IOERR               = "4302"
+    SERVERERR           = "4500"
+    UNKOWNERR           = "4501"
+
+
+err_msg = {
+    RETCODE.OK                 : u"成功",
+    RETCODE.IMAGECODEERR       : u"图形验证码错误",
+    RETCODE.THROTTLINGERR      : u"访问过于频繁",
+    RETCODE.NECESSARYPARAMERR  : u"缺少必传参数",
+    RETCODE.USERERR            : u"用户名错误",
+    RETCODE.PWDERR             : u"密码错误",
+    RETCODE.CPWDERR            : u"密码不一致",
+    RETCODE.MOBILEERR          : u"手机号错误",
+    RETCODE.SMSCODERR          : u"短信验证码有误",
+    RETCODE.ALLOWERR           : u"未勾选协议",
+    RETCODE.SESSIONERR         : u"用户未登录",
+    RETCODE.DBERR              : u"数据错误",
+    RETCODE.EMAILERR           : u"邮箱错误",
+    RETCODE.TELERR             : u"固定电话错误",
+    RETCODE.NODATAERR          : u"无数据",
+    RETCODE.NEWPWDERR          : u"新密码数据",
+}