/* Date: 03/16/2015
   By: Veena Veeravalli
   This SQL will import the consolidated accrual items for each consolidated transaction into EIM_LOY_ACRLIT table */

DECLARE
   TYPE T_MN IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.MEM_NUM%TYPE;

   TYPE T_RM IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.SUMMARY_NUM_POINTS%TYPE;

   TYPE T_SNP IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.SUMMARY_NUM_POINTS%TYPE;

   TYPE T_SUV IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.SUMMARY_USED_VALUE%TYPE;

   TYPE T_TN IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.TXN_NUM%TYPE;

   TYPE T_IN IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.ITEM_NUM%TYPE;

   ARR_MN               T_MN;
   ARR_RM               T_RM;
   ARR_SNP              T_SNP;
   ARR_SUV              T_SUV;
   ARR_TN               T_TN;
   ARR_IN               T_IN;

   V_ROWID_DATE         VARCHAR2 (7 CHAR);
   V_ACTION_ROWID       VARCHAR2 (15 CHAR);
   V_POINT_BLOCKID      VARCHAR2 (15 CHAR);
   V_EXP_DATE           DATE;
   V_PRODUCT_NAME       VARCHAR2 (100 CHAR);
   V_IF_ROW_BATCH_NUM   NUMBER := 6001;
   LOG_ROW_ID           NUMBER := 0;
   ROW_COUNT            NUMBER := 0;

   CURSOR C1
   IS
      SELECT A.MEM_NUM,
             ROWNUM,
             A.SUMMARY_NUM_POINTS,
             A.SUMMARY_USED_VALUE,
             A.TXN_NUM,
             A.ITEM_NUM
        FROM SIEBEL_PURGE.PURGE_MEM_TXN A
       WHERE A.PURGE_TXN_COUNT > 0 AND A.ITEM_NUM IS NOT NULL;
BEGIN
   SELECT CASE WHEN MAX (ROW_ID) IS NOT NULL THEN MAX (ROW_ID) + 1 ELSE 1 END
     INTO LOG_ROW_ID
     FROM SIEBEL_PURGE.PURGE_LOG;

   INSERT INTO SIEBEL_PURGE.PURGE_LOG (ROW_ID, PROCESS_NAME, START_TIME)
      SELECT LOG_ROW_ID, 'Load EIM_LOY_ACRLIT', SYSDATE FROM DUAL;

   COMMIT;

   SELECT ADD_MONTHS (PURGE_DATE, 1200), PRODUCT_NAME
     INTO V_EXP_DATE, V_PRODUCT_NAME
     FROM SIEBEL_PURGE.PURGE_CONFIG
    WHERE ROW_ID = 1;

   SELECT ROW_ID
     INTO V_ACTION_ROWID
     FROM SIEBEL.S_LOY_ACTCRTR
    WHERE     NAME = 'Give Points'
          AND PROMO_RULE_ID =
                 (SELECT ROW_ID
                    FROM SIEBEL.S_LOY_PROMO_RL
                   WHERE     NAME = 'Check Points'
                         AND PROMO_ID = (SELECT ROW_ID
                                           FROM SIEBEL.S_LOY_PROMO
                                          WHERE NAME = 'Admin - Accruals'));

   SELECT ROW_ID
     INTO V_POINT_BLOCKID
     FROM SIEBEL.S_LOY_PT_BLCK
    WHERE NAME = 'AS Accrual - Net';

   SELECT TO_CHAR (SYSDATE, 'Y') || TO_CHAR (SYSDATE, 'HH24MISS')
     INTO V_ROWID_DATE
     FROM DUAL;

   OPEN C1;

   LOOP
      FETCH C1
         BULK COLLECT INTO ARR_MN,
              ARR_RM,
              ARR_SNP,
              ARR_SUV,
              ARR_TN,
              ARR_IN
         --LIMIT 10000;
		 LIMIT 50;

      FORALL ROW IN ARR_IN.FIRST .. ARR_IN.LAST
         INSERT INTO SIEBEL.EIM_LOY_ACRLIT (ROW_ID,
                                            CREATED_BY,
                                            LAST_UPD_BY,
                                            IF_ROW_BATCH_NUM,
                                            IF_ROW_STAT,
                                            ITEM_NUM,
                                            ACTION_ROW_ID,
                                            ACCRUALED_VALUE,
                                            COMMENTS,
                                            EXPIRATION_DT,
                                            PRTNR_PT_DEBT_QTY,
                                            USED_VALUE,
                                            MEMBER_MEM_NUM,
                                            POINT_BLCK_ROW_ID,
                                            TXN_NUM)
              VALUES (V_ROWID_DATE || LPAD (TO_CHAR (ARR_RM (ROW)), 8, 0),
                      '0-1',
                      '0-1',
                      V_IF_ROW_BATCH_NUM,
                      'FOR_IMPORT',
                      ARR_IN (ROW),
                      V_ACTION_ROWID,
                      ARR_SNP (ROW),
                      V_PRODUCT_NAME,
                      V_EXP_DATE,
                      ARR_SNP (ROW),
                      ARR_SUV (ROW),
                      ARR_MN (ROW),
                      V_POINT_BLOCKID,
                      ARR_TN (ROW));

      ROW_COUNT := ROW_COUNT + SQL%ROWCOUNT;

      UPDATE SIEBEL_PURGE.PURGE_LOG
         SET PROCESSED_ROWS = ROW_COUNT
       WHERE ROW_ID = LOG_ROW_ID;

      COMMIT;

      IF V_IF_ROW_BATCH_NUM = 7000
      THEN
         V_IF_ROW_BATCH_NUM := 6001;
      ELSE
         V_IF_ROW_BATCH_NUM := V_IF_ROW_BATCH_NUM + 1;
      END IF;

      EXIT WHEN C1%NOTFOUND;
   END LOOP;

   COMMIT;

   CLOSE C1;

   EXECUTE IMMEDIATE 'CREATE INDEX SIEBEL.EIM_LOY_ACRLIT_150819D
   ON SIEBEL.EIM_LOY_ACRLIT (ITEM_NUM)
   PARALLEL 5';

   EXECUTE IMMEDIATE 'ALTER INDEX SIEBEL.EIM_LOY_ACRLIT_150819D
   NOPARALLEL';

   UPDATE SIEBEL_PURGE.PURGE_LOG
      SET END_TIME = SYSDATE, PROCESSED_ROWS = ROW_COUNT
    WHERE ROW_ID = LOG_ROW_ID;

   COMMIT;
END;
/

QUIT