07月份相关代码

---------------------------------------------------------------------------------------

1.每次导入使用连续的序列号 ;比如batch=1 bsd 1..100 ;NCD 1..65 ; 如果分组为 IBO0+NCD1+BSD0 分别是。。。。

--TYPE  STR_SPLIT

CREATE OR REPLACE TYPE STR_SPLIT    IS TABLE OF VARCHAR2 (4000);

 

--- FUNCTION STR_SPLIT() ;

CREATE OR REPLACE FUNCTION splitstr(p_string IN VARCHAR2, p_delimiter IN VARCHAR2)
   /**********************************************************************
    * 通用方法:按列返回分割字符串后的内容
    * 入参:p_string 分割字符串
    *       p_delimiter 分割符
    * 返回:每个分割出来的字符串
    **********************************************************************/
    RETURN str_split
    PIPELINED
AS
    v_length   NUMBER := LENGTH(p_string);
    v_start    NUMBER := 1;
    v_index    NUMBER;
BEGIN
    WHILE(v_start <= v_length)
    LOOP
        v_index := INSTR(p_string, p_delimiter, v_start);

        IF v_index = 0
        THEN
            PIPE ROW(SUBSTR(p_string, v_start));
            v_start := v_length + 1;
        ELSE
            PIPE ROW(SUBSTR(p_string, v_start, v_index - v_start));
            v_start := v_index + 1;
        END IF;
    END LOOP;

    RETURN;
END splitstr;


---

 1 CREATE OR REPLACE PACKAGE TAX_CONTROL_SENDS IS
 2 
 3   -- Public type declarations  type <TypeName> is <Datatype>;
 4   TYPE IDX_TABL_GROUP IS TABLE OF VARCHAR2(4000) INDEX BY PLS_INTEGER;
 5   TYPE IDX_TABLE_HEAD IS TABLE OF INVOICEDATAMAIN%ROWTYPE INDEX BY PLS_INTEGER;
 6   TYPE IDX_TABLE_LINE IS TABLE OF INVOICEDATADETAIL%ROWTYPE INDEX BY PLS_INTEGER;
 7   TYPE ARR_LIST_BOOLEAN IS VARRAY(17) OF INTEGER;
 8 
 9   IDX_HEAD IDX_TABLE_HEAD;
10   IDX_LINE IDX_TABLE_LINE;
11   IDX_TAB  IDX_TABL_GROUP;
12   LIST     ARR_LIST_BOOLEAN;
13   V_SPLITSTR CONSTANT VARCHAR2(2) := '|';
14   V_TYPE2ISTRUE   INT;
15   V_TYPE3ISTRUE   INT;
16   V_GN_COUNT      INT;
17   V_DETAILCOUNT_1 INT;
18   V_DETAILCOUNT_2 INT;
19   V_DEALMONTH     VARCHAR2(100);
20   V_PTCLIST       VARCHAR2(300);
21   --分组id ;
22   V_GROUPID INTEGER;
23   --会员id ;
24   V_MEMBERID INTEGER;
25   --分组名 
26   V_GROUPNAME TAX_CONTROL_SEND.GROUPNAME%TYPE;
27   --编号;
28   -- V_UNINO VARCHAR2(16);
29   --产品分组;
30   V_PRODUCTGROUPNAME VARCHAR2(1000);
31   --产品CODE;
32   V_PRODUCTCODE VARCHAR2(10);
33   --是否汇总;
34   V_ISSUMMARY VARCHAR2(100);
35   -- 最大的;
36   V_MAXROWNUM INTEGER := 0;
37   ---v_goods
38   V_GOODS TAX_CONTROL_SEND.GOODS%TYPE;
39   ---
40   V_MAXBATCHNO INT;
41   V_ROWID      ROWID;
42   --function insertHeadTab向头表中插入数据,返回值类型为 true/false;
43   FUNCTION INSERTHEADTAB(IN_GROUPID   IN INTEGER,
44                          IN_DEALMONTH IN VARCHAR2,
45                          V_UNINO      IN VARCHAR2) RETURN BOOLEAN;
46   --function insertLineTab()向行表中插入数据,返回值类型为 true/false;
47   FUNCTION INSERTLINETAB(V_UNINO      IN VARCHAR2,
48                          IN_MEMBERID  IN INTEGER,
49                          IN_PCODE     IN VARCHAR2,
50                          IN_DEALMONTH IN VARCHAR2) RETURN BOOLEAN;
51 
52   --function insertsummaryData() 汇总插入 ;
53   FUNCTION INSERTSUMMARYDATA(V_UNINO      IN VARCHAR2,
54                              IN_MEMBERID  IN INTEGER,
55                              IN_PCODE     IN VARCHAR2,
56                              IN_DEALMONTH IN VARCHAR2) RETURN BOOLEAN;
57   --将头表和行表的信息落入本地invoicedataMain 和 invoicedatadetail 表中;
58   FUNCTION INSERT_INVOICE_MAIN_DETAIL(IN_GROUPID IN INTEGER) RETURN BOOLEAN;
59   --将head表的主键关联到 insert 到 send 表headLineTabID;
60   FUNCTION UPDATETTSENDTAB(IN_GROUPID IN INTEGER, V_UNINO IN VARCHAR2)
61     RETURN BOOLEAN;
62   ---自定义的明细insert 到detail 表 ;
63   FUNCTION CUSTOMGROUPNAME_DETAIL(V_UNINO       IN VARCHAR2,
64                                   V_MEMBERID    IN INTEGER,
65                                   V_PRODUCTCODE IN VARCHAR2,
66                                   V_DEALMONTH   IN VARCHAR2) RETURN BOOLEAN;
67   --executeProcedure 
68 
69   ---自定义的summary insert 到detail 表 ;
70   FUNCTION CUSTOMGROUPNAME_SUMMARY_2(V_UNINO       IN VARCHAR2,
71                                      V_MEMBERID    IN INTEGER,
72                                      V_PRODUCTCODE IN VARCHAR2,
73                                      V_DEALMONTH   IN VARCHAR2)
74     RETURN BOOLEAN;
75 
76   FUNCTION CUSTOMGROUPNAME_SUMMARY_3(V_UNINO       IN VARCHAR2,
77                                      V_MEMBERID    IN INTEGER,
78                                      V_PRODUCTCODE IN VARCHAR2,
79                                      V_DEALMONTH   IN VARCHAR2)
80     RETURN BOOLEAN;
81 
82   PROCEDURE EXECUTEPROCEDURE;
83 END TAX_CONTROL_SENDS;
   1 CREATE OR REPLACE PACKAGE BODY TAX_CONTROL_SENDS IS
   2   /**********************************************************************
   3   ***FunctionName: INSERTHEADTAB
   4   ***Used as:根据分组将发票维护的数据通过 insert into select 的形式insert到head表; 
   5   ***Date: 2017/0601
   6   ***Author: Mr.yang
   7   **********************************************************************/
   8   FUNCTION INSERTHEADTAB(IN_GROUPID   IN INTEGER,
   9                          IN_DEALMONTH VARCHAR2,
  10                          V_UNINO      IN VARCHAR2) RETURN BOOLEAN AS
  11   
  12     RESULTFLAG BOOLEAN := FALSE;
  13   BEGIN
  14     V_DEALMONTH := IN_DEALMONTH;
  15     V_GROUPID   := IN_GROUPID;
  16     --将查询结果放入到IDX_HEAD 当中;
  17     INSERT INTO INVOICEDATAMAIN
  18       SELECT V_UNINO,
  19              IM.MEMBERID,
  20              NULL,
  21              IM.CUSTOMERNAME,
  22              IM.RATEPAYERNUMBER,
  23              IM.CUSTOMERADDRESS || IM.PHONE,
  24              IM.CUSTOMERBANKNAME,
  25              TO_DATE(V_DEALMONTH, 'yyyy-mm'),
  26              NULL,
  27              TCGM.INVOICEKIND,
  28              NULL,
  29              NULL,
  30              NULL,
  31              NULL,
  32              NULL,
  33              NULL,
  34              'N',
  35              NULL,
  36              NULL,
  37              0,
  38              SYSTIMESTAMP
  39       --  BULK COLLECT INTO tax_control_sends.IDX_HEAD
  40         FROM INVOICE_INFO_MAINTAIN IM, TAX_CONTROLLED_GROUP_MAINTAIN TCGM
  41        WHERE TCGM.MEMBERID = IM.MEMBERID(+)
  42          AND TCGM.ID = V_GROUPID;
  43     --从IDX_HEAD中取数据批量insert到INVOICEDATAMAIN 表中;
  44     RESULTFLAG := TRUE;
  45     RETURN RESULTFLAG;
  46   EXCEPTION
  47     WHEN OTHERS THEN
  48       DBMS_OUTPUT.PUT_LINE('INSERT_HEAD_TAB触发异常:::' || SQLCODE || SQLERRM);
  49       RETURN RESULTFLAG;
  50   END INSERTHEADTAB;
  51   /**********************************************************************
  52   ***FunctionName: INSERTLINETAB
  53   ***Used as: 根据分组将明细数据通过 insert into select 的形式insert到line表; 
  54   ***Date: 2017/0601
  55   ***Author: Mr.yang
  56   **********************************************************************/
  57   FUNCTION INSERTLINETAB(V_UNINO      IN VARCHAR2,
  58                          IN_MEMBERID  IN INTEGER,
  59                          IN_PCODE     IN VARCHAR2,
  60                          IN_DEALMONTH IN VARCHAR2) RETURN BOOLEAN AS
  61     RESULTFLAG BOOLEAN := FALSE;
  62   
  63   BEGIN
  64     V_DEALMONTH   := IN_DEALMONTH;
  65     V_MEMBERID    := IN_MEMBERID;
  66     V_PRODUCTCODE := IN_PCODE;
  67   
  68     SELECT 编号,
  69            序号,
  70            数量,
  71            单价,
  72            金额,
  73            税率,
  74            税额,
  75            商品名称,
  76            商品税目,
  77            规格型号,
  78            计量单位,
  79            含税标志,
  80            原价,
  81            折扣率,
  82            折扣金额,
  83            折扣税额,
  84            备注,
  85            状态,
  86            CREATETIME,
  87            MODIFYTIME,
  88            编码版本号,
  89            税收分类编码,
  90            优惠政策,
  91            折扣行 BULK COLLECT
  92       INTO IDX_LINE
  93     
  94       FROM (SELECT V_UNINO AS 编号,
  95                    (ROWNUM + V_MAXROWNUM) AS 序号,
  96                    NULL AS 数量,
  97                    NULL AS 单价,
  98                    FBD.EXCLUDEBROKERAGE AS 金额,
  99                    6 AS 税率,
 100                    FBD.TAXPRICE AS 税额,
 101                    V_GOODS AS 商品名称,
 102                    '0101' AS 商品税目,
 103                    V_PRODUCTCODE AS 规格型号,
 104                    NULL AS 计量单位,
 105                    'N' AS 含税标志,
 106                    NULL AS 原价,
 107                    NULL AS 折扣率,
 108                    NULL AS 折扣金额,
 109                    NULL AS 折扣税额,
 110                    NULL AS 备注,
 111                    0 AS 状态,
 112                    SYSTIMESTAMP AS CREATETIME,
 113                    SYSTIMESTAMP AS MODIFYTIME,
 114                    '13.0' AS 编码版本号,
 115                    '30602' AS 税收分类编码,
 116                    0 AS 优惠政策,
 117                    ROWNUM AS 折扣行
 118             
 119               FROM (SELECT FBD.DEALNO,
 120                            FBD.TAXPRICE,
 121                            FBD.INCLUDEBROKERAGE,
 122                            FBD.EXCLUDEBROKERAGE
 123                       FROM FINANCE_BILL_DETAIL FBD
 124                      WHERE FBD.MEMBERID = V_MEMBERID
 125                        AND FBD.BILLDATE = V_DEALMONTH
 126                        AND FBD.PRODUCTCODE = V_PRODUCTCODE
 127                        AND FBD.GROUPNAME = V_GROUPNAME
 128                        AND FBD.BATCHNO = V_MAXBATCHNO
 129                     
 130                     ) FBD,
 131                    (SELECT B.DEALNO,
 132                            SUM(B.DISCOUNTCNY) AS DISCOUNTCNY,
 133                            SUM(B.DISCOUNTAMOUNTCNY) AS DISCOUNTAMOUNTCNY,
 134                            SUM(B.DISCOUNTTAXAFTERCNY) AS DISCOUNTTAXAFTERCNY
 135                       FROM (SELECT M.NO AS DEALNO,
 136                                    DECODE(M.BUYERID, V_MEMBERID, 1, 2) AS BSFLAG
 137                               FROM HIS_PRODUCT_DEAL_MAIN M
 138                              WHERE EXISTS
 139                              (SELECT 1
 140                                       FROM FINANCE_BILL_DETAIL FBD
 141                                      WHERE M.NO = FBD.DEALNO
 142                                        AND FBD.MEMBERID = V_MEMBERID
 143                                        AND FBD.BILLDATE = V_DEALMONTH
 144                                        AND FBD.PRODUCTCODE = V_PRODUCTCODE
 145                                        AND FBD.BATCHNO = V_MAXBATCHNO
 146                                        AND FBD.GROUPNAME = V_GROUPNAME
 147                                     
 148                                     )
 149                                AND M.STATE = 9
 150                             
 151                             ) MA,
 152                            BASE_DEAL_DISCOUNT B
 153                      WHERE MA.DEALNO = B.DEALNO(+)
 154                        AND MA.BSFLAG = B.BSFLAG(+)
 155                        AND B.STATUS = 0
 156                        AND B.ISWRITEOFF = 0
 157                      GROUP BY B.DEALNO) BDD
 158              WHERE FBD.DEALNO = BDD.DEALNO(+)
 159             
 160             UNION ALL
 161             
 162             SELECT MAX(V_UNINO) AS 编号,
 163                    MAX(ROWNUM) + 1 + V_MAXROWNUM AS 序号,
 164                    NULL AS 数量,
 165                    NULL AS 单价,
 166                    SUM(NVL(BDD.DISCOUNTCNY, 0)) * -1 AS 金额,
 167                    6 AS 税率,
 168                    ROUND(SUM(NVL(BDD.DISCOUNTCNY, 0)) * 0.06, 2) * -1 AS 税额,
 169                    V_GOODS 商品名称,
 170                    '0101' AS 商品税目,
 171                    V_PRODUCTCODE AS 规格型号,
 172                    NULL AS 计量单位,
 173                    'N' AS 含税标志,
 174                    NULL AS 原价,
 175                    NULL AS 折扣率,
 176                    NULL AS 折扣金额,
 177                    NULL AS 折扣税额,
 178                    NULL AS 备注,
 179                    0 状态,
 180                    SYSTIMESTAMP AS CREATETIME,
 181                    SYSTIMESTAMP AS MODIFYTIME,
 182                    '13.0' AS 编码版本号,
 183                    '30602' AS 税收分类编码,
 184                    0 AS 优惠政策,
 185                    V_DETAILCOUNT_2 AS 折扣行
 186             
 187               FROM (SELECT FBD.DEALNO,
 188                            FBD.TAXPRICE,
 189                            FBD.INCLUDEBROKERAGE,
 190                            FBD.EXCLUDEBROKERAGE
 191                       FROM FINANCE_BILL_DETAIL FBD
 192                      WHERE FBD.MEMBERID = V_MEMBERID
 193                        AND FBD.BILLDATE = V_DEALMONTH
 194                        AND FBD.PRODUCTCODE = V_PRODUCTCODE
 195                        AND FBD.GROUPNAME = V_GROUPNAME
 196                        AND FBD.BATCHNO = V_MAXBATCHNO) FBD,
 197                    (SELECT B.DEALNO,
 198                            SUM(B.DISCOUNTCNY) AS DISCOUNTCNY,
 199                            SUM(B.DISCOUNTAMOUNTCNY) AS DISCOUNTAMOUNTCNY,
 200                            SUM(B.DISCOUNTTAXAFTERCNY) AS DISCOUNTTAXAFTERCNY
 201                       FROM (SELECT M.NO AS DEALNO,
 202                                    DECODE(M.BUYERID, V_MEMBERID, 1, 2) AS BSFLAG
 203                               FROM HIS_PRODUCT_DEAL_MAIN M
 204                              WHERE EXISTS
 205                              (SELECT 1
 206                                       FROM FINANCE_BILL_DETAIL FBD
 207                                      WHERE M.NO = FBD.DEALNO
 208                                        AND FBD.MEMBERID = V_MEMBERID
 209                                        AND FBD.BILLDATE = V_DEALMONTH
 210                                        AND FBD.PRODUCTCODE = V_PRODUCTCODE
 211                                        AND FBD.BATCHNO = V_MAXBATCHNO
 212                                        AND FBD.GROUPNAME = V_GROUPNAME)
 213                                AND M.STATE = 9) MA,
 214                            BASE_DEAL_DISCOUNT B
 215                      WHERE MA.DEALNO = B.DEALNO(+)
 216                        AND MA.BSFLAG = B.BSFLAG(+)
 217                        AND B.STATUS = 0
 218                        AND B.ISWRITEOFF = 0
 219                      GROUP BY B.DEALNO) BDD
 220              WHERE FBD.DEALNO = BDD.DEALNO(+));
 221   
 222     V_MAXROWNUM := IDX_LINE(IDX_LINE.LAST).SERIAL;
 223   
 224     FORALL J IN IDX_LINE.FIRST .. IDX_LINE.LAST
 225       INSERT INTO INVOICEDATADETAIL
 226       VALUES
 227         (IDX_LINE(J).UNINO,
 228          IDX_LINE(J).SERIAL,
 229          IDX_LINE(J).QTY,
 230          IDX_LINE(J).PRICE,
 231          IDX_LINE(J).SHPAMT,
 232          IDX_LINE(J).TAXRATE,
 233          IDX_LINE(J).TAXATION,
 234          IDX_LINE(J).TRADENAME,
 235          IDX_LINE(J).TRADETAX,
 236          IDX_LINE(J).MODEL,
 237          IDX_LINE(J).UNIT,
 238          IDX_LINE(J).TAXPRICE,
 239          IDX_LINE(J).OLDPRICE,
 240          IDX_LINE(J).DISRATE,
 241          IDX_LINE(J).DISAMT,
 242          IDX_LINE(J).DISTAXAMT,
 243          IDX_LINE(J).REMARK,
 244          IDX_LINE(J).STATE,
 245          IDX_LINE(J).CREATETIME,
 246          IDX_LINE(J).MODIFYTIME,
 247          IDX_LINE(J).BMBBH,
 248          IDX_LINE(J).SSFLBM,
 249          IDX_LINE(J).YHZC,
 250          IDX_LINE(J).DISCOUNTLINE);
 251   
 252     RESULTFLAG := TRUE;
 253     RETURN RESULTFLAG;
 254   EXCEPTION
 255     WHEN OTHERS THEN
 256       DBMS_OUTPUT.PUT_LINE('INSERT_LINE_TAB 触发异常::::' || SQLCODE || '::' ||
 257                            SQLERRM || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
 258       RETURN RESULTFLAG;
 259   END INSERTLINETAB;
 260   /**********************************************************************
 261   ***FunctionName: CUSTOMGROUPNAMEDETAIL
 262   ***Used as:  
 263   ***Date: 2017/0601
 264   ***Author: Mr.yang
 265   **********************************************************************/
 266   FUNCTION CUSTOMGROUPNAME_DETAIL(V_UNINO       IN VARCHAR2,
 267                                   V_MEMBERID    IN INTEGER,
 268                                   V_PRODUCTCODE IN VARCHAR2,
 269                                   V_DEALMONTH   IN VARCHAR2) RETURN BOOLEAN AS
 270     RESULTFLAG BOOLEAN := FALSE;
 271   
 272   BEGIN
 273   
 274     SELECT 编号,
 275            序号,
 276            数量,
 277            单价,
 278            金额,
 279            税率,
 280            税额,
 281            商品名称,
 282            商品税目,
 283            规格型号,
 284            计量单位,
 285            含税标志,
 286            原价,
 287            折扣率,
 288            折扣金额,
 289            折扣税额,
 290            备注,
 291            状态,
 292            CREATETIME,
 293            MODIFYTIME,
 294            编码版本号,
 295            税收分类编码,
 296            优惠政策,
 297            折扣行
 298            
 299            BULK COLLECT
 300       INTO IDX_LINE
 301       FROM (SELECT V_UNINO AS 编号,
 302                    (ROWNUM + V_MAXROWNUM) AS 序号,
 303                    NULL AS 数量,
 304                    NULL AS 单价,
 305                    FBD.EXCLUDEBROKERAGE AS 金额,
 306                    6 AS 税率,
 307                    FBD.TAXPRICE AS 税额,
 308                    V_GOODS AS 商品名称,
 309                    '0101' AS 商品税目,
 310                    V_PRODUCTCODE AS 规格型号,
 311                    NULL AS 计量单位,
 312                    'N' AS 含税标志,
 313                    NULL AS 原价,
 314                    NULL AS 折扣率,
 315                    NULL AS 折扣金额,
 316                    NULL AS 折扣税额,
 317                    NULL AS 备注,
 318                    0 AS 状态,
 319                    SYSTIMESTAMP AS CREATETIME,
 320                    SYSTIMESTAMP AS MODIFYTIME,
 321                    '13.0' AS 编码版本号,
 322                    '30602' AS 税收分类编码,
 323                    0 AS 优惠政策,
 324                    ROWNUM AS 折扣行
 325             
 326               FROM (SELECT DISTINCT (FBD.DEALNO),
 327                                     FBD.TAXPRICE,
 328                                     FBD.INCLUDEBROKERAGE,
 329                                     FBD.EXCLUDEBROKERAGE
 330                       FROM FINANCE_BILL_DETAIL FBD
 331                      WHERE FBD.MEMBERID = V_MEMBERID
 332                        AND FBD.BILLDATE = V_DEALMONTH
 333                        AND FBD.PRODUCTCODE = V_PRODUCTCODE
 334                        AND FBD.BATCHNO = V_MAXBATCHNO) FBD,
 335                    (SELECT B.DEALNO,
 336                            SUM(B.DISCOUNTCNY) AS DISCOUNTCNY,
 337                            SUM(B.DISCOUNTAMOUNTCNY) AS DISCOUNTAMOUNTCNY,
 338                            SUM(B.DISCOUNTTAXAFTERCNY) AS DISCOUNTTAXAFTERCNY
 339                       FROM (SELECT M.NO AS DEALNO,
 340                                    DECODE(M.BUYERID, V_MEMBERID, 1, 2) AS BSFLAG
 341                               FROM HIS_PRODUCT_DEAL_MAIN M
 342                              WHERE EXISTS
 343                              (SELECT 1
 344                                       FROM FINANCE_BILL_DETAIL FBD
 345                                      WHERE M.NO = FBD.DEALNO
 346                                        AND FBD.MEMBERID = V_MEMBERID
 347                                        AND FBD.BILLDATE = V_DEALMONTH
 348                                        AND FBD.PRODUCTCODE = V_PRODUCTCODE
 349                                        AND FBD.BATCHNO = V_MAXBATCHNO)
 350                                AND M.STATE = 9) MA,
 351                            BASE_DEAL_DISCOUNT B
 352                      WHERE MA.DEALNO = B.DEALNO(+)
 353                        AND MA.BSFLAG = B.BSFLAG(+)
 354                        AND B.STATUS = 0
 355                        AND B.ISWRITEOFF = 0
 356                      GROUP BY B.DEALNO) BDD
 357              WHERE FBD.DEALNO = BDD.DEALNO(+)
 358             
 359             UNION ALL
 360             
 361             SELECT MAX(V_UNINO) AS 编号,
 362                    MAX(ROWNUM) + 1 + V_MAXROWNUM AS 序号,
 363                    NULL AS 数量,
 364                    NULL AS 单价,
 365                    SUM(NVL(BDD.DISCOUNTCNY, 0)) * -1 AS 金额,
 366                    6 AS 税率,
 367                    ROUND(SUM(NVL(BDD.DISCOUNTCNY, 0)) * 0.06, 2) * -1 AS 税额,
 368                    V_GOODS 商品名称,
 369                    '0101' AS 商品税目,
 370                    V_PRODUCTCODE AS 规格型号,
 371                    NULL AS 计量单位,
 372                    'N' AS 含税标志,
 373                    NULL AS 原价,
 374                    NULL AS 折扣率,
 375                    NULL AS 折扣金额,
 376                    NULL AS 折扣税额,
 377                    NULL AS 备注,
 378                    0 状态,
 379                    SYSTIMESTAMP AS CREATETIME,
 380                    SYSTIMESTAMP AS MODIFYTIME,
 381                    '13.0' AS 编码版本号,
 382                    '30602' AS 税收分类编码,
 383                    0 AS 优惠政策,
 384                    V_DETAILCOUNT_1 AS 折扣行
 385             
 386               FROM (SELECT DISTINCT (FBD.DEALNO),
 387                                     FBD.TAXPRICE,
 388                                     FBD.INCLUDEBROKERAGE,
 389                                     FBD.EXCLUDEBROKERAGE
 390                       FROM FINANCE_BILL_DETAIL FBD
 391                      WHERE FBD.MEMBERID = V_MEMBERID
 392                        AND FBD.BILLDATE = V_DEALMONTH
 393                        AND FBD.PRODUCTCODE = V_PRODUCTCODE
 394                        AND FBD.BATCHNO = V_MAXBATCHNO) FBD,
 395                    (SELECT B.DEALNO,
 396                            SUM(B.DISCOUNTCNY) AS DISCOUNTCNY,
 397                            SUM(B.DISCOUNTAMOUNTCNY) AS DISCOUNTAMOUNTCNY,
 398                            SUM(B.DISCOUNTTAXAFTERCNY) AS DISCOUNTTAXAFTERCNY
 399                       FROM (SELECT M.NO AS DEALNO,
 400                                    DECODE(M.BUYERID, V_MEMBERID, 1, 2) AS BSFLAG
 401                               FROM HIS_PRODUCT_DEAL_MAIN M
 402                              WHERE EXISTS
 403                              (SELECT 1
 404                                       FROM FINANCE_BILL_DETAIL FBD
 405                                      WHERE M.NO = FBD.DEALNO
 406                                        AND FBD.MEMBERID = V_MEMBERID
 407                                        AND FBD.BILLDATE = V_DEALMONTH
 408                                        AND FBD.PRODUCTCODE = V_PRODUCTCODE
 409                                        AND FBD.BATCHNO = V_MAXBATCHNO)
 410                                AND M.STATE = 9) MA,
 411                            BASE_DEAL_DISCOUNT B
 412                      WHERE MA.DEALNO = B.DEALNO(+)
 413                        AND MA.BSFLAG = B.BSFLAG(+)
 414                        AND B.STATUS = 0
 415                        AND B.ISWRITEOFF = 0
 416                      GROUP BY B.DEALNO) BDD
 417              WHERE FBD.DEALNO = BDD.DEALNO(+));
 418   
 419     V_MAXROWNUM := IDX_LINE(IDX_LINE.LAST).SERIAL;
 420     DBMS_OUTPUT.PUT_LINE('V_MAXROWNUM=' || V_MAXROWNUM);
 421     FORALL J IN IDX_LINE.FIRST .. IDX_LINE.LAST
 422       INSERT INTO INVOICEDATADETAIL
 423       VALUES
 424         (IDX_LINE(J).UNINO,
 425          IDX_LINE(J).SERIAL,
 426          IDX_LINE(J).QTY,
 427          IDX_LINE(J).PRICE,
 428          IDX_LINE(J).SHPAMT,
 429          IDX_LINE(J).TAXRATE,
 430          IDX_LINE(J).TAXATION,
 431          IDX_LINE(J).TRADENAME,
 432          IDX_LINE(J).TRADETAX,
 433          IDX_LINE(J).MODEL,
 434          IDX_LINE(J).UNIT,
 435          IDX_LINE(J).TAXPRICE,
 436          IDX_LINE(J).OLDPRICE,
 437          IDX_LINE(J).DISRATE,
 438          IDX_LINE(J).DISAMT,
 439          IDX_LINE(J).DISTAXAMT,
 440          IDX_LINE(J).REMARK,
 441          IDX_LINE(J).STATE,
 442          IDX_LINE(J).CREATETIME,
 443          IDX_LINE(J).MODIFYTIME,
 444          IDX_LINE(J).BMBBH,
 445          IDX_LINE(J).SSFLBM,
 446          IDX_LINE(J).YHZC,
 447          IDX_LINE(J).DISCOUNTLINE);
 448   
 449     RESULTFLAG := TRUE;
 450     RETURN RESULTFLAG;
 451   
 452   EXCEPTION
 453     WHEN OTHERS THEN
 454       DBMS_OUTPUT.PUT_LINE('CUSTOMGROUPNAMEDETAIL 触发异常::::' || SQLCODE || '::' ||
 455                            SQLERRM || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
 456       RETURN RESULTFLAG;
 457     
 458   END CUSTOMGROUPNAME_DETAIL;
 459 
 460   /**********************************************************************
 461   ***FunctionName: INSERTSUMMARYDATA
 462   ***Used as: 根据分组将汇总数据通过 insert into select 的形式insert到line表; 
 463   ***Date: 2017/0601
 464   ***Author: Mr.yang
 465   **********************************************************************/
 466   FUNCTION INSERTSUMMARYDATA(V_UNINO      IN VARCHAR2,
 467                              IN_MEMBERID  IN INTEGER,
 468                              IN_PCODE     IN VARCHAR2,
 469                              IN_DEALMONTH IN VARCHAR2) RETURN BOOLEAN AS
 470     RESULTFLAG BOOLEAN := FALSE;
 471   
 472   BEGIN
 473     V_DEALMONTH   := IN_DEALMONTH;
 474     V_PRODUCTCODE := IN_PCODE;
 475     V_MEMBERID    := IN_MEMBERID;
 476   
 477     SELECT 编号,
 478            序号,
 479            数量,
 480            单价,
 481            金额,
 482            税率,
 483            税额,
 484            商品名称,
 485            商品税目,
 486            规格型号,
 487            计量单位,
 488            含税标志,
 489            原价,
 490            折扣率,
 491            折扣金额,
 492            折扣税额,
 493            备注,
 494            状态,
 495            CREATETIME,
 496            MODIFYTIME,
 497            编码版本号,
 498            税收分类编码,
 499            优惠政策,
 500            折扣行
 501            
 502            BULK COLLECT
 503       INTO IDX_LINE
 504     
 505       FROM (SELECT TO_CHAR(V_UNINO) AS 编号,
 506                    (ROWNUM + V_MAXROWNUM) AS 序号,
 507                    NULL AS 数量,
 508                    NULL AS 单价,
 509                    S.EXCLUDETAXBROKERAGE AS 金额,
 510                    6 AS 税率,
 511                    S.TAXPRICE AS 税额,
 512                    V_GOODS AS 商品名称,
 513                    '0101' AS 商品税目,
 514                    V_PRODUCTCODE AS 规格型号,
 515                    NULL AS 计量单位,
 516                    'N' AS 含税标志,
 517                    NULL AS 原价,
 518                    NULL AS 折扣率,
 519                    NULL AS 折扣金额,
 520                    NULL AS 折扣税额,
 521                    NULL AS 备注,
 522                    0 状态,
 523                    SYSTIMESTAMP AS CREATETIME,
 524                    SYSTIMESTAMP AS MODIFYTIME,
 525                    '13.0' AS 编码版本号,
 526                    '30602' AS 税收分类编码,
 527                    0 AS 优惠政策,
 528                    ROWNUM AS 折扣行
 529               FROM FINANCE_BILL_SUMMARY S
 530              WHERE S.BILLDATE = V_DEALMONTH
 531                AND S.PRODUCT = V_PRODUCTCODE
 532                AND S.MEMBERID = V_MEMBERID
 533                AND S.GROUPNAME = V_GROUPNAME
 534                AND S.BATCHNO = V_MAXBATCHNO
 535             
 536             UNION ALL
 537             
 538             SELECT TO_CHAR(V_UNINO) AS 编号,
 539                    (ROWNUM + 1 + V_MAXROWNUM) AS 序号,
 540                    NULL AS 数量,
 541                    NULL AS 单价,
 542                    S.DISCOUNT * -1 AS 金额,
 543                    6 AS 税率,
 544                    S.DISCOUNTTAXPRICE * -1 AS 税额,
 545                    V_GOODS AS 商品名称,
 546                    '0101' AS 商品税目,
 547                    V_PRODUCTCODE AS 规格型号,
 548                    NULL AS 计量单位,
 549                    'N' AS 含税标志,
 550                    NULL AS 原价,
 551                    NULL AS 折扣率,
 552                    NULL AS 折扣金额,
 553                    NULL AS 折扣税额,
 554                    NULL AS 备注,
 555                    0 状态,
 556                    SYSTIMESTAMP AS CREATETIME,
 557                    SYSTIMESTAMP AS MODIFYTIME,
 558                    '13.0' AS 编码版本号,
 559                    '30602' AS 税收分类编码,
 560                    0 AS 优惠政策,
 561                    V_GN_COUNT AS 折扣行
 562               FROM FINANCE_BILL_SUMMARY S
 563              WHERE S.BILLDATE = V_DEALMONTH
 564                AND S.PRODUCT = V_PRODUCTCODE
 565                AND S.MEMBERID = V_MEMBERID
 566                AND S.GROUPNAME = V_GROUPNAME
 567                AND S.BATCHNO = V_MAXBATCHNO);
 568   
 569     V_MAXROWNUM := IDX_LINE(IDX_LINE.LAST).SERIAL;
 570     DBMS_OUTPUT.PUT_LINE('V_MAXROWNUM=' || V_MAXROWNUM);
 571     FORALL J IN IDX_LINE.FIRST .. IDX_LINE.LAST
 572       INSERT INTO INVOICEDATADETAIL
 573       VALUES
 574         (IDX_LINE(J).UNINO,
 575          IDX_LINE(J).SERIAL,
 576          IDX_LINE(J).QTY,
 577          IDX_LINE(J).PRICE,
 578          IDX_LINE(J).SHPAMT,
 579          IDX_LINE(J).TAXRATE,
 580          IDX_LINE(J).TAXATION,
 581          IDX_LINE(J).TRADENAME,
 582          IDX_LINE(J).TRADETAX,
 583          IDX_LINE(J).MODEL,
 584          IDX_LINE(J).UNIT,
 585          IDX_LINE(J).TAXPRICE,
 586          IDX_LINE(J).OLDPRICE,
 587          IDX_LINE(J).DISRATE,
 588          IDX_LINE(J).DISAMT,
 589          IDX_LINE(J).DISTAXAMT,
 590          IDX_LINE(J).REMARK,
 591          IDX_LINE(J).STATE,
 592          IDX_LINE(J).CREATETIME,
 593          IDX_LINE(J).MODIFYTIME,
 594          IDX_LINE(J).BMBBH,
 595          IDX_LINE(J).SSFLBM,
 596          IDX_LINE(J).YHZC,
 597          IDX_LINE(J).DISCOUNTLINE);
 598   
 599     RESULTFLAG := TRUE;
 600     RETURN RESULTFLAG;
 601   EXCEPTION
 602     WHEN OTHERS THEN
 603       DBMS_OUTPUT.PUT_LINE('INSERT_SUMMARY_DATA触发异常:::' || SQLCODE ||
 604                            SQLERRM || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
 605       RETURN RESULTFLAG;
 606   END INSERTSUMMARYDATA;
 607 
 608   FUNCTION CUSTOMGROUPNAME_SUMMARY_2(V_UNINO       IN VARCHAR2,
 609                                      V_MEMBERID    IN INTEGER,
 610                                      V_PRODUCTCODE IN VARCHAR2,
 611                                      V_DEALMONTH   IN VARCHAR2)
 612     RETURN BOOLEAN AS
 613     RESULTFLAG BOOLEAN := FALSE;
 614   BEGIN
 615     SELECT 编号,
 616            序号,
 617            数量,
 618            单价,
 619            金额,
 620            税率,
 621            税额,
 622            商品名称,
 623            商品税目,
 624            规格型号,
 625            计量单位,
 626            含税标志,
 627            原价,
 628            折扣率,
 629            折扣金额,
 630            折扣税额,
 631            备注,
 632            状态,
 633            CREATETIME,
 634            MODIFYTIME,
 635            编码版本号,
 636            税收分类编码,
 637            优惠政策,
 638            折扣行 BULK COLLECT
 639       INTO IDX_LINE
 640     
 641       FROM (SELECT TO_CHAR(V_UNINO) AS 编号,
 642                    (ROWNUM + V_MAXROWNUM) AS 序号,
 643                    NULL AS 数量,
 644                    NULL AS 单价,
 645                    S.EXCLUDETAXBROKERAGE AS 金额,
 646                    6 AS 税率,
 647                    S.TAXPRICE AS 税额,
 648                    V_GOODS AS 商品名称,
 649                    '0101' AS 商品税目,
 650                    V_PRODUCTCODE AS 规格型号,
 651                    NULL AS 计量单位,
 652                    'N' AS 含税标志,
 653                    NULL AS 原价,
 654                    NULL AS 折扣率,
 655                    NULL AS 折扣金额,
 656                    NULL AS 折扣税额,
 657                    NULL AS 备注,
 658                    0 状态,
 659                    SYSTIMESTAMP AS CREATETIME,
 660                    SYSTIMESTAMP AS MODIFYTIME,
 661                    '13.0' AS 编码版本号,
 662                    '30602' AS 税收分类编码,
 663                    0 AS 优惠政策,
 664                    ROWNUM AS 折扣行
 665               FROM FINANCE_BILL_SUMMARY S
 666              WHERE S.BILLDATE = V_DEALMONTH
 667                AND S.PRODUCT = V_PRODUCTCODE
 668                AND S.MEMBERID = V_MEMBERID
 669                AND S.BATCHNO = V_MAXBATCHNO
 670                AND S.TYPE = 2
 671             
 672             UNION ALL
 673             
 674             SELECT TO_CHAR(V_UNINO) AS 编号,
 675                    MAX(ROWNUM) + 1 + V_MAXROWNUM AS 序号,
 676                    NULL AS 数量,
 677                    NULL AS 单价,
 678                    SUM(S.DISCOUNT) * -1 AS 金额,
 679                    6 AS 税率,
 680                    SUM(S.DISCOUNTTAXPRICE) * -1 AS 税额,
 681                    V_GOODS AS 商品名称,
 682                    '0101' AS 商品税目,
 683                    V_PRODUCTCODE AS 规格型号,
 684                    NULL AS 计量单位,
 685                    'N' AS 含税标志,
 686                    NULL AS 原价,
 687                    NULL AS 折扣率,
 688                    NULL AS 折扣金额,
 689                    NULL AS 折扣税额,
 690                    NULL AS 备注,
 691                    0 状态,
 692                    SYSTIMESTAMP AS CREATETIME,
 693                    SYSTIMESTAMP AS MODIFYTIME,
 694                    '13.0' AS 编码版本号,
 695                    '30602' AS 税收分类编码,
 696                    0 AS 优惠政策,
 697                    V_TYPE2ISTRUE AS 折扣行
 698               FROM FINANCE_BILL_SUMMARY S
 699              WHERE S.BILLDATE = V_DEALMONTH
 700                AND S.PRODUCT = V_PRODUCTCODE
 701                AND S.MEMBERID = V_MEMBERID
 702                AND S.BATCHNO = V_MAXBATCHNO
 703                AND S.TYPE = 2);
 704   
 705     V_MAXROWNUM := IDX_LINE(IDX_LINE.LAST).SERIAL;
 706     DBMS_OUTPUT.PUT_LINE('V_MAXROWNUM=' || V_MAXROWNUM);
 707     FORALL J IN IDX_LINE.FIRST .. IDX_LINE.LAST
 708       INSERT INTO INVOICEDATADETAIL
 709       VALUES
 710         (IDX_LINE(J).UNINO,
 711          IDX_LINE(J).SERIAL,
 712          IDX_LINE(J).QTY,
 713          IDX_LINE(J).PRICE,
 714          IDX_LINE(J).SHPAMT,
 715          IDX_LINE(J).TAXRATE,
 716          IDX_LINE(J).TAXATION,
 717          IDX_LINE(J).TRADENAME,
 718          IDX_LINE(J).TRADETAX,
 719          IDX_LINE(J).MODEL,
 720          IDX_LINE(J).UNIT,
 721          IDX_LINE(J).TAXPRICE,
 722          IDX_LINE(J).OLDPRICE,
 723          IDX_LINE(J).DISRATE,
 724          IDX_LINE(J).DISAMT,
 725          IDX_LINE(J).DISTAXAMT,
 726          IDX_LINE(J).REMARK,
 727          IDX_LINE(J).STATE,
 728          IDX_LINE(J).CREATETIME,
 729          IDX_LINE(J).MODIFYTIME,
 730          IDX_LINE(J).BMBBH,
 731          IDX_LINE(J).SSFLBM,
 732          IDX_LINE(J).YHZC,
 733          IDX_LINE(J).DISCOUNTLINE);
 734   
 735     RESULTFLAG := TRUE;
 736     RETURN RESULTFLAG;
 737   EXCEPTION
 738     WHEN OTHERS THEN
 739       RETURN RESULTFLAG;
 740   END CUSTOMGROUPNAME_SUMMARY_2;
 741 
 742   /**********************************************************************
 743   ***FunctionName: CUSTOMGROUPNAME_SUMMARY
 744   ***Used as:自定义分组 将汇总数据insert 到 detail表中 ;
 745   ***Date: 2017/0601
 746   ***Author: Mr.yang
 747   **********************************************************************/
 748 
 749   FUNCTION CUSTOMGROUPNAME_SUMMARY_3(V_UNINO       IN VARCHAR2,
 750                                      V_MEMBERID    IN INTEGER,
 751                                      V_PRODUCTCODE IN VARCHAR2,
 752                                      V_DEALMONTH   IN VARCHAR2)
 753     RETURN BOOLEAN AS
 754   
 755     RESULTFLAG BOOLEAN := FALSE;
 756   BEGIN
 757   
 758     SELECT 编号,
 759            序号,
 760            数量,
 761            单价,
 762            金额,
 763            税率,
 764            税额,
 765            商品名称,
 766            商品税目,
 767            规格型号,
 768            计量单位,
 769            含税标志,
 770            原价,
 771            折扣率,
 772            折扣金额,
 773            折扣税额,
 774            备注,
 775            状态,
 776            CREATETIME,
 777            MODIFYTIME,
 778            编码版本号,
 779            税收分类编码,
 780            优惠政策,
 781            折扣行 BULK COLLECT
 782       INTO IDX_LINE
 783     
 784       FROM (SELECT TO_CHAR(V_UNINO) AS 编号,
 785                    MAX(ROWNUM) + V_MAXROWNUM AS 序号,
 786                    NULL AS 数量,
 787                    NULL AS 单价,
 788                    SUM(S.EXCLUDETAXBROKERAGE) AS 金额,
 789                    6 AS 税率,
 790                    SUM(S.TAXPRICE) AS 税额,
 791                    V_GOODS AS 商品名称,
 792                    '0101' AS 商品税目,
 793                    V_PRODUCTCODE AS 规格型号,
 794                    NULL AS 计量单位,
 795                    'N' AS 含税标志,
 796                    NULL AS 原价,
 797                    NULL AS 折扣率,
 798                    NULL AS 折扣金额,
 799                    NULL AS 折扣税额,
 800                    NULL AS 备注,
 801                    0 状态,
 802                    SYSTIMESTAMP AS CREATETIME,
 803                    SYSTIMESTAMP AS MODIFYTIME,
 804                    '13.0' AS 编码版本号,
 805                    '30602' AS 税收分类编码,
 806                    0 AS 优惠政策,
 807                    ROWNUM AS 折扣行
 808               FROM FINANCE_BILL_SUMMARY S
 809              WHERE S.BILLDATE = V_DEALMONTH
 810                AND S.PRODUCT = V_PRODUCTCODE
 811                AND S.MEMBERID = V_MEMBERID
 812                AND S.TYPE = 3
 813                AND S.BATCHNO = V_MAXBATCHNO
 814             
 815             UNION ALL
 816             
 817             SELECT TO_CHAR(V_UNINO) AS 编号,
 818                    MAX(ROWNUM) + 1 + V_MAXROWNUM AS 序号,
 819                    NULL AS 数量,
 820                    NULL AS 单价,
 821                    SUM(S.DISCOUNT) * -1 AS 金额,
 822                    6 AS 税率,
 823                    SUM(S.DISCOUNTTAXPRICE) * -1 AS 税额,
 824                    V_GOODS AS 商品名称,
 825                    '0101' AS 商品税目,
 826                    V_PRODUCTCODE AS 规格型号,
 827                    NULL AS 计量单位,
 828                    'N' AS 含税标志,
 829                    NULL AS 原价,
 830                    NULL AS 折扣率,
 831                    NULL AS 折扣金额,
 832                    NULL AS 折扣税额,
 833                    NULL AS 备注,
 834                    0 状态,
 835                    SYSTIMESTAMP AS CREATETIME,
 836                    SYSTIMESTAMP AS MODIFYTIME,
 837                    '13.0' AS 编码版本号,
 838                    '30602' AS 税收分类编码,
 839                    0 AS 优惠政策,
 840                    V_TYPE3ISTRUE AS 折扣行
 841               FROM FINANCE_BILL_SUMMARY S
 842              WHERE S.BILLDATE = V_DEALMONTH
 843                AND S.PRODUCT = V_PRODUCTCODE
 844                AND S.MEMBERID = V_MEMBERID
 845                AND S.TYPE = 3
 846                AND S.BATCHNO = V_MAXBATCHNO);
 847   
 848     V_MAXROWNUM := IDX_LINE(IDX_LINE.LAST).SERIAL;
 849     DBMS_OUTPUT.PUT_LINE('V_MAXROWNUM=' || V_MAXROWNUM);
 850     FORALL J IN IDX_LINE.FIRST .. IDX_LINE.LAST
 851       INSERT INTO INVOICEDATADETAIL
 852       VALUES
 853         (IDX_LINE(J).UNINO,
 854          IDX_LINE(J).SERIAL,
 855          IDX_LINE(J).QTY,
 856          IDX_LINE(J).PRICE,
 857          IDX_LINE(J).SHPAMT,
 858          IDX_LINE(J).TAXRATE,
 859          IDX_LINE(J).TAXATION,
 860          IDX_LINE(J).TRADENAME,
 861          IDX_LINE(J).TRADETAX,
 862          IDX_LINE(J).MODEL,
 863          IDX_LINE(J).UNIT,
 864          IDX_LINE(J).TAXPRICE,
 865          IDX_LINE(J).OLDPRICE,
 866          IDX_LINE(J).DISRATE,
 867          IDX_LINE(J).DISAMT,
 868          IDX_LINE(J).DISTAXAMT,
 869          IDX_LINE(J).REMARK,
 870          IDX_LINE(J).STATE,
 871          IDX_LINE(J).CREATETIME,
 872          IDX_LINE(J).MODIFYTIME,
 873          IDX_LINE(J).BMBBH,
 874          IDX_LINE(J).SSFLBM,
 875          IDX_LINE(J).YHZC,
 876          IDX_LINE(J).DISCOUNTLINE);
 877   
 878     RESULTFLAG := TRUE;
 879     RETURN RESULTFLAG;
 880   EXCEPTION
 881     WHEN OTHERS THEN
 882       DBMS_OUTPUT.PUT_LINE(SQLCODE || SQLERRM);
 883     
 884   END CUSTOMGROUPNAME_SUMMARY_3;
 885 
 886   /**********************************************************************
 887   ***FunctionName: INSERT_INVOICE_MAIN_DETAIL
 888   ***Used as: 根据分组向 head 和 line 表中insert 数据;
 889   ***Date: 2017/0601
 890   ***Author: Mr.yang
 891   **********************************************************************/
 892   FUNCTION INSERT_INVOICE_MAIN_DETAIL(IN_GROUPID IN INTEGER) RETURN BOOLEAN AS
 893     RESULTFLAG    BOOLEAN := FALSE;
 894     V_UNINO       VARCHAR2(16);
 895     V_CUSTOMVALUE INT := 0;
 896   
 897     EXP_DETAIL EXCEPTION;
 898     EXP_SUMMARY EXCEPTION;
 899     CUSTOMGROUPEXP_DETAIL EXCEPTION;
 900     CUSTOMGROUPEXP_SUMMARY_2 EXCEPTION;
 901     CUSTOMGROUPEXP_SUMMARY_3 EXCEPTION;
 902     EXP_HEAD EXCEPTION;
 903   
 904   BEGIN
 905     V_TYPE2ISTRUE   := 0;
 906     V_TYPE3ISTRUE   := 0;
 907     V_DETAILCOUNT_1 := 0;
 908     V_DETAILCOUNT_2 := 0;
 909     V_MAXBATCHNO    := 0;
 910     V_MAXROWNUM     := 0;
 911     V_GROUPID       := IN_GROUPID;
 912     V_GN_COUNT      := 0;
 913     LIST            := ARR_LIST_BOOLEAN(NULL);
 914     --v_unino 作为INVOICEDATAMAIN 的主键id; 
 915     SELECT NVL(MAX(ROWNUM) + 1, 1) INTO V_UNINO FROM INVOICEDATAMAIN IDM;
 916     --根据V_GROUPID从税控分组维护表中获取产品分组信息和会员机构的ID ;   
 917     IF V_PRODUCTGROUPNAME IS NULL THEN
 918       SELECT TC.PRODUCTGROUP, TC.MEMBERID, TS.BILLDATE
 919         INTO V_PRODUCTGROUPNAME, V_MEMBERID, V_DEALMONTH
 920         FROM TAX_CONTROLLED_GROUP_MAINTAIN TC, TAX_CONTROL_SEND TS
 921        WHERE TC.ID = TS.GROUPID(+)
 922          AND TS.LOCALINSERTFLAG IS NULL
 923          AND TS.SENDSTATE = 0
 924          AND TC.ID = V_GROUPID
 925          AND TS.ROWID = V_ROWID;
 926     
 927     ELSE
 928     
 929       SELECT TC.MEMBERID, TS.BILLDATE
 930         INTO V_MEMBERID, V_DEALMONTH
 931         FROM TAX_CONTROLLED_GROUP_MAINTAIN TC, TAX_CONTROL_SEND TS
 932        WHERE TC.ID = TS.GROUPID(+)
 933          AND TS.LOCALINSERTFLAG IS NULL
 934          AND TS.SENDSTATE = 0
 935          AND TC.ID = V_GROUPID
 936          AND TS.ROWID = V_ROWID;
 937     
 938     END IF;
 939     --使用Function SPLITSTR  对产品分组进行拆分后放入idx_tab中;
 940     SELECT COLUMN_VALUE BULK COLLECT
 941       INTO IDX_TAB
 942       FROM TABLE(SPLITSTR(V_PRODUCTGROUPNAME, V_SPLITSTR));
 943   
 944     --向头表中insert数据 ; 
 945     IF INSERTHEADTAB(V_GROUPID, V_DEALMONTH, V_UNINO) THEN
 946       DBMS_OUTPUT.PUT_LINE('INSERTHEADTAB insert 成功 !');
 947     ELSE
 948       DBMS_OUTPUT.PUT_LINE('INSERTHEADTAB insert 失败 !');
 949       RAISE EXP_HEAD;
 950     END IF;
 951   
 952     --插入头表成功后再获取最大batchNo号;(因为头表insert失败整个方法失败,不需要在它之前进行查询!) 
 953     SELECT MAX(FBD.BATCHNO)
 954       INTO V_MAXBATCHNO
 955       FROM FINANCE_BILL_DETAIL FBD
 956      WHERE FBD.MEMBERID = V_MEMBERID
 957        AND FBD.BILLDATE = V_DEALMONTH;
 958   
 959     --对   tax_control_sends.IDX_TAB 进行循环  
 960     FOR I IN IDX_TAB.FIRST .. IDX_TAB.LAST LOOP
 961       LIST.EXTEND(IDX_TAB.LAST);
 962       --截取字符串 ;
 963       V_PRODUCTCODE := UPPER(SUBSTR(IDX_TAB(I), 0, LENGTH(IDX_TAB(I)) - 1));
 964       V_ISSUMMARY   := SUBSTR(IDX_TAB(I), -1, 1);
 965     
 966       --假如给据 月份+产品+机构id 没有查询到任何数据直接跳出本次循环;
 967       IF V_MAXBATCHNO IS NULL THEN
 968         RETURN RESULTFLAG;
 969       END IF;
 970     
 971       --判断是税控分组名还是财务账单分组名 ;
 972       SELECT COUNT(1)
 973         INTO V_CUSTOMVALUE
 974         FROM FINANCE_BILL_DETAIL FBD
 975        WHERE FBD.MEMBERID = V_MEMBERID
 976          AND FBD.BILLDATE = V_DEALMONTH
 977             -- AND FBD.PRODUCTCODE = V_PRODUCTCODE
 978          AND FBD.GROUPNAME = V_GROUPNAME;
 979     
 980       --V_ISSUMMARY=0 代表汇总 , 
 981       IF V_ISSUMMARY = 0 THEN
 982         --说明是税控维护的分组
 983         IF V_CUSTOMVALUE = 0 THEN
 984         
 985           ---查询判断TYPE=2是否存在如果不存在取type=3 ;
 986           SELECT COUNT(1)
 987             INTO V_TYPE2ISTRUE
 988             FROM FINANCE_BILL_SUMMARY FBD
 989            WHERE FBD.MEMBERID = V_MEMBERID
 990              AND FBD.BILLDATE = V_DEALMONTH
 991              AND FBD.PRODUCT = V_PRODUCTCODE
 992              AND FBD.BATCHNO = V_MAXBATCHNO
 993                 --  AND FBD.GROUPNAME = V_GROUPNAME
 994              AND FBD.TYPE = 2;
 995           IF V_TYPE2ISTRUE <> 0 THEN
 996             IF CUSTOMGROUPNAME_SUMMARY_2(V_UNINO,
 997                                          V_MEMBERID,
 998                                          V_PRODUCTCODE,
 999                                          V_DEALMONTH) THEN
1000               LIST(I) := 1;
1001               DBMS_OUTPUT.PUT_LINE('CUSTOMGROUPNAME_SUMMARY_2 insert 成功 !');
1002             ELSE
1003               --出发 CUSTOMGROUPNAME_SUMMARY_2 异常 ;
1004               RAISE CUSTOMGROUPEXP_SUMMARY_2;
1005             
1006             END IF;
1007           
1008           ELSE
1009             SELECT COUNT(1)
1010               INTO V_TYPE3ISTRUE
1011               FROM FINANCE_BILL_SUMMARY FBD
1012              WHERE FBD.MEMBERID = V_MEMBERID
1013                AND FBD.BILLDATE = V_DEALMONTH
1014                AND FBD.PRODUCT = V_PRODUCTCODE
1015                AND FBD.BATCHNO = V_MAXBATCHNO
1016                   --  AND FBD.GROUPNAME = V_GROUPNAME
1017                AND FBD.TYPE = 3;
1018             IF V_TYPE3ISTRUE = 0 THEN
1019               LIST(I) := 0;
1020               CONTINUE;
1021             ELSE
1022               IF CUSTOMGROUPNAME_SUMMARY_3(V_UNINO,
1023                                            V_MEMBERID,
1024                                            V_PRODUCTCODE,
1025                                            V_DEALMONTH) THEN
1026                 LIST(I) := 1;
1027                 DBMS_OUTPUT.PUT_LINE('想detail 表中插入自定义的汇总数据,insert 成功!');
1028               ELSE
1029                 RAISE CUSTOMGROUPEXP_SUMMARY_3;
1030               
1031               END IF;
1032             END IF;
1033           END IF;
1034         
1035         ELSIF V_CUSTOMVALUE <> 0 THEN
1036         
1037           SELECT COUNT(1)
1038             INTO V_GN_COUNT
1039             FROM FINANCE_BILL_SUMMARY FBD
1040            WHERE FBD.MEMBERID = V_MEMBERID
1041              AND FBD.BILLDATE = V_DEALMONTH
1042              AND FBD.PRODUCT = V_PRODUCTCODE
1043              AND FBD.BATCHNO = V_MAXBATCHNO
1044              AND FBD.GROUPNAME = V_GROUPNAME
1045              AND FBD.BATCHNO = V_MAXBATCHNO;
1046           IF V_GN_COUNT = 0 THEN
1047             LIST(I) := 0;
1048             CONTINUE;
1049           ELSE
1050             --向行表中insert数据;
1051             IF INSERTSUMMARYDATA(V_UNINO,
1052                                  V_MEMBERID,
1053                                  V_PRODUCTCODE,
1054                                  V_DEALMONTH) THEN
1055               LIST(I) := 1;
1056               DBMS_OUTPUT.PUT_LINE(V_PRODUCTCODE ||
1057                                    '按汇总:INSERTSUMMARYDATA insert 成功 !');
1058             ELSE
1059               DBMS_OUTPUT.PUT_LINE(V_PRODUCTCODE ||
1060                                    '按汇总 没有数据:INSERTSUMMARYDATA insert 失败 !');
1061               RAISE EXP_SUMMARY;
1062             
1063             END IF;
1064           END IF;
1065         
1066         END IF;
1067       
1068         --V_ISSUMMARY=1 代表明细; 
1069       ELSIF V_ISSUMMARY = 1 THEN
1070       
1071         IF V_CUSTOMVALUE = 0 THEN
1072           SELECT COUNT(1)
1073             INTO V_DETAILCOUNT_1
1074             FROM FINANCE_BILL_DETAIL FBD
1075            WHERE FBD.MEMBERID = V_MEMBERID
1076              AND FBD.BILLDATE = V_DEALMONTH
1077              AND FBD.PRODUCTCODE = V_PRODUCTCODE
1078              AND FBD.BATCHNO = V_MAXBATCHNO;
1079         
1080           --        DBMS_OUTPUT.PUT_LINE('自定的分组名发送单个市场的');
1081           IF V_DETAILCOUNT_1 = 0 THEN
1082             LIST(I) := 0;
1083             CONTINUE;
1084           ELSE
1085           
1086             IF CUSTOMGROUPNAME_DETAIL(V_UNINO,
1087                                       V_MEMBERID,
1088                                       V_PRODUCTCODE,
1089                                       V_DEALMONTH) THEN
1090               LIST(I) := 1;
1091               DBMS_OUTPUT.PUT_LINE('自定的分组明细insert成功 !');
1092             ELSE
1093             
1094               RAISE CUSTOMGROUPEXP_DETAIL;
1095             END IF;
1096           END IF;
1097         ELSIF V_CUSTOMVALUE <> 0 THEN
1098           SELECT COUNT(1)
1099             INTO V_DETAILCOUNT_2
1100             FROM FINANCE_BILL_DETAIL FBD
1101            WHERE FBD.MEMBERID = V_MEMBERID
1102              AND FBD.BILLDATE = V_DEALMONTH
1103              AND FBD.PRODUCTCODE = V_PRODUCTCODE
1104              AND FBD.BATCHNO = V_MAXBATCHNO
1105              AND FBD.GROUPNAME = V_GROUPNAME;
1106           IF V_DETAILCOUNT_2 = 0 THEN
1107             LIST(I) := 0;
1108             CONTINUE;
1109           ELSE
1110             IF INSERTLINETAB(V_UNINO,
1111                              V_MEMBERID,
1112                              V_PRODUCTCODE,
1113                              V_DEALMONTH) THEN
1114               LIST(I) := 1;
1115               DBMS_OUTPUT.PUT_LINE(V_PRODUCTCODE ||
1116                                    '按明细 ;INSERTLINETAB 执行成功 !');
1117             ELSE
1118             
1119               DBMS_OUTPUT.PUT_LINE(V_PRODUCTCODE ||
1120                                    '按明细 ;INSERTLINETAB 执行 失败 !');
1121               RAISE EXP_DETAIL;
1122             END IF;
1123           END IF;
1124         END IF;
1125       
1126       END IF;
1127     END LOOP;
1128     -- 将head表的主键关联到 TAX_CONTROL_SEND 表headLineTabID;
1129     IF UPDATETTSENDTAB(V_GROUPID, V_UNINO) THEN
1130       DBMS_OUTPUT.PUT_LINE('将head表的主键关联到TAX_CONTROL_SEND 表headLineTabID ,执行成功!');
1131     ELSE
1132       DBMS_OUTPUT.PUT_LINE('将head表的主键关联到TAX_CONTROL_SEND 表headLineTabID ,执行失败!');
1133     END IF;
1134     --整个过程顺利执行完毕后进行commmit ; 
1135     IF LIST.EXISTS(1) THEN
1136       LIST.DELETE;
1137       COMMIT;
1138     ELSE
1139       ROLLBACK;
1140     END IF;
1141     DBMS_OUTPUT.PUT_LINE('-------------commit !-------------------');
1142     RESULTFLAG := TRUE;
1143     RETURN RESULTFLAG;
1144   EXCEPTION
1145     WHEN EXP_HEAD THEN
1146       DBMS_OUTPUT.PUT_LINE('触发 EXP_HEAD 异常,即将对数据 rollback ,并return false!');
1147       ROLLBACK;
1148       RETURN RESULTFLAG;
1149     WHEN EXP_SUMMARY THEN
1150       DBMS_OUTPUT.PUT_LINE('触发 Exp_Summary 异常,即将对数据 rollback ,并return false!');
1151       ROLLBACK;
1152       RETURN RESULTFLAG;
1153     WHEN EXP_DETAIL THEN
1154       DBMS_OUTPUT.PUT_LINE('触发 Exp_Detail 异常,即将对数据 rollback ,并return false!');
1155       ROLLBACK;
1156       RETURN RESULTFLAG;
1157     
1158     WHEN CUSTOMGROUPEXP_DETAIL THEN
1159       DBMS_OUTPUT.PUT_LINE('触发 CUSTOMGROUPEXP_detail 异常,即将对数据 rollback ,并return false!');
1160       ROLLBACK;
1161       RETURN RESULTFLAG;
1162     WHEN CUSTOMGROUPEXP_SUMMARY_3 THEN
1163       DBMS_OUTPUT.PUT_LINE('触发 CUSTOMGROUPEXP_SUMMARY_3 异常,即将对数据 rollback ,并return false!');
1164       ROLLBACK;
1165       RETURN RESULTFLAG;
1166     WHEN CUSTOMGROUPEXP_SUMMARY_2 THEN
1167       DBMS_OUTPUT.PUT_LINE('触发 CUSTOMGROUPEXP_SUMMARY_2 异常,即将对数据 rollback ,并return false!');
1168       ROLLBACK;
1169       RETURN RESULTFLAG;
1170     WHEN OTHERS THEN
1171       DBMS_OUTPUT.PUT_LINE('INSERT_INVOICE_MAIN_DETAIL触发异常:' || SQLCODE ||
1172                            SQLERRM || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
1173     
1174       ROLLBACK;
1175       RETURN RESULTFLAG;
1176     
1177   END INSERT_INVOICE_MAIN_DETAIL;
1178 
1179   /**********************************************************************
1180   ***FunctionName: INSERTTTSENDTAB
1181   ***Used as: 将head表的主键关联到 insert 到 TAX_CONTROL_SEND 表headLineTabID;
1182   ***Date: 2017/0601
1183   ***Author: Mr.yang
1184   **********************************************************************/
1185 
1186   FUNCTION UPDATETTSENDTAB(IN_GROUPID IN INTEGER, V_UNINO IN VARCHAR2)
1187     RETURN BOOLEAN AS
1188     RESULTFLAG BOOLEAN := FALSE;
1189   BEGIN
1190   
1191     UPDATE TAX_CONTROL_SEND S
1192        SET S.HEADLINETABID = V_UNINO
1193      WHERE S.GROUPID = IN_GROUPID
1194        AND ROWID = V_ROWID;
1195   
1196     RESULTFLAG := TRUE;
1197     RETURN RESULTFLAG;
1198   EXCEPTION
1199     WHEN OTHERS THEN
1200       DBMS_OUTPUT.PUT_LINE('INSERTTTSENDTAB:::' || SQLCODE || SQLERRM);
1201       RETURN RESULTFLAG;
1202   END UPDATETTSENDTAB;
1203   /**********************************************************************
1204   ***FunctionName: EXECUTEPROCEDURE
1205   ***Used as:执行INSERT_INVOICE_MAIN_DETAIL 整个过程,并更新send表中的insetLocalFlag 标志位 !;
1206   ***Date: 2017/0601
1207   ***Author: Mr.yang
1208   **********************************************************************/
1209 
1210   PROCEDURE EXECUTEPROCEDURE AS
1211   
1212     V_GROUPID TAX_CONTROL_SEND.GROUPID%TYPE;
1213     EXP_GROUPIDISNULL EXCEPTION;
1214     CURSOR CUR_EXECPRO IS
1215       SELECT S.GROUPID, S.GOODS, S.GROUPNAME, S.PRODUCTLIST, S.ROWID
1216         FROM TAX_CONTROL_SEND S
1217        WHERE S.SENDSTATE = 0
1218          AND S.LOCALINSERTFLAG IS NULL
1219        ORDER BY S.GROUPID;
1220   BEGIN
1221     OPEN CUR_EXECPRO;
1222     LOOP
1223       V_ROWID := NULL;
1224       FETCH CUR_EXECPRO
1225         INTO V_GROUPID, V_GOODS, V_GROUPNAME, V_PRODUCTGROUPNAME, V_ROWID;
1226       IF V_GROUPID IS NULL THEN
1227         DBMS_OUTPUT.PUT_LINE('V_GROUPID IS  NULL 触发EXP_GROUPIDISNULL异常!');
1228         RAISE EXP_GROUPIDISNULL;
1229       END IF;
1230       DBMS_OUTPUT.PUT_LINE('V_GROUPID=========' || V_GROUPID);
1231       EXIT WHEN CUR_EXECPRO%NOTFOUND;
1232       IF INSERT_INVOICE_MAIN_DETAIL(V_GROUPID) THEN
1233       
1234         UPDATE TAX_CONTROL_SEND T
1235            SET T.LOCALINSERTFLAG = 1
1236          WHERE ROWID = V_ROWID;
1237       
1238         DBMS_OUTPUT.PUT_LINE('修改send表的groupid= ' || V_GROUPID ||
1239                              'localinsertflag等于1为成功状态!');
1240       ELSE
1241       
1242         UPDATE TAX_CONTROL_SEND T
1243            SET T.LOCALINSERTFLAG = 0
1244          WHERE ROWID = V_ROWID;
1245       
1246         DBMS_OUTPUT.PUT_LINE('修改send表的groupid= ' || V_GROUPID ||
1247                              ' localinsertflag等于0为失败状态!');
1248       
1249       END IF;
1250       COMMIT;
1251     END LOOP;
1252     --执行commit ;
1253   
1254     CLOSE CUR_EXECPRO;
1255   
1256     RETURN;
1257   EXCEPTION
1258     WHEN EXP_GROUPIDISNULL THEN
1259       CLOSE CUR_EXECPRO;
1260       RETURN;
1261     WHEN OTHERS THEN
1262       DBMS_OUTPUT.PUT_LINE('EXECUTEPROCEDURE 触发异常:' || SQLCODE || SQLERRM ||
1263                            DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
1264       RETURN;
1265   END EXECUTEPROCEDURE;
1266 
1267 END TAX_CONTROL_SENDS;

 

posted @ 2017-08-01 19:25  linbo.yang  阅读(335)  评论(0编辑  收藏  举报