AR 收款导入及收款核销程序

CREATE OR REPLACE PACKAGE MEW_AR_RECEIPT_IMPORT2 IS
  PROCEDURE INTERFACE_IMPORT(P_METHOD                 VARCHAR2
                            ,P_BANK_NUM               VARCHAR2
                            ,P_FACTOR_DISCOUNT_AMOUNT NUMBER
                            ,P_CUSTOMER_NUMBER        VARCHAR2
                            ,P_CUSTOMER_SITE_USE      VARCHAR2
                            ,P_RECEIPT_NUMBER         VARCHAR2
                            ,P_DOC_SEQUENCE_VALUE     VARCHAR2
                            ,P_RECEIPT_DATE           DATE
                            ,P_USR_CURRENCY_CODE      VARCHAR2
                            ,P_AMOUNT                 NUMBER
                            ,P_USR_EXCHANGE_RATE_TYPE VARCHAR2);
  PROCEDURE INTERFACE_IMPORT_APPLY(P_RECEIPT_NUMBER   VARCHAR2
                                  ,P_TRX_NUMBER       VARCHAR2
                                  ,P_AMOUNT_APPLIED   NUMBER
                                  ,P_AMOUNT_APPLIED_F NUMBER);

END MEW_AR_RECEIPT_IMPORT2;
/
CREATE OR REPLACE PACKAGE BODY MEW_AR_RECEIPT_IMPORT2 IS
  -------------create by yuxunping 2009-10-12------------

  PROCEDURE INTERFACE_IMPORT(P_METHOD                 VARCHAR2
                            ,P_BANK_NUM               VARCHAR2
                            ,P_FACTOR_DISCOUNT_AMOUNT NUMBER
                            ,P_CUSTOMER_NUMBER        VARCHAR2
                            ,P_CUSTOMER_SITE_USE      VARCHAR2
                            ,P_RECEIPT_NUMBER         VARCHAR2
                            ,P_DOC_SEQUENCE_VALUE     VARCHAR2
                            ,P_RECEIPT_DATE           DATE
                            ,
                             
                             P_USR_CURRENCY_CODE      VARCHAR2
                            ,P_AMOUNT                 NUMBER
                            ,P_USR_EXCHANGE_RATE_TYPE VARCHAR2) IS
  
    GPR_RETURN_STATUS VARCHAR2(1) := NULL;
    GPR_MSG_COUNT     NUMBER := 0;
    GPR_MSG_DATA      VARCHAR2(2000);
  
    L_MSG         VARCHAR2(2000);
    I             NUMBER;
    L_FLAG        VARCHAR2(2);
    L_CUSTOMER_ID NUMBER;
    RECEIPT EXCEPTION; ------收款编号重复例外
  BEGIN
    BEGIN
      SELECT 1
        INTO L_FLAG
        FROM AR.AR_RECEIPT_METHODS T
       WHERE T.NAME = P_METHOD
         AND ROWNUM = 1;
    EXCEPTION
      WHEN OTHERS THEN
        APP_EXCEPTION.RAISE_EXCEPTION('MEW'
                                     ,204
                                     ,'收款方法有错误');
    END;
  
    /*  
    begin
      select count(1)
        into l_flag
        from ar_cash_receipts_all t
       where t.receipt_number = p_receipt_number
         and rownum = 1;
      if l_flag > 0
      then
        raise receipt;
      end if;
    exception
      when receipt then
        app_exception.raise_exception('MEW',
                                      204,
                                      '收款编号重复');
      when others then
        null;
    end;*/
    --delete by zhuhailong
  
    BEGIN
      SELECT S.CUSTOMER_ID
        INTO L_CUSTOMER_ID
        FROM AR_CUSTOMERS S
       WHERE S.CUSTOMER_NUMBER = P_CUSTOMER_NUMBER
         AND ROWNUM = 1;
    EXCEPTION
      WHEN OTHERS THEN
        APP_EXCEPTION.RAISE_EXCEPTION('MEW'
                                     ,204
                                     ,'客户编码有错误');
    END;
    IF P_CUSTOMER_SITE_USE IS NOT NULL
    THEN
      BEGIN
        SELECT 1
          INTO L_FLAG
          FROM HZ_CUST_SITE_USES  SITE_USE
              ,HZ_CUST_ACCT_SITES ACCT_SITE
         WHERE ACCT_SITE.CUST_ACCOUNT_ID = L_CUSTOMER_ID
           AND ACCT_SITE.STATUS = 'A'
           AND SITE_USE.CUST_ACCT_SITE_ID = ACCT_SITE.CUST_ACCT_SITE_ID
           AND SITE_USE.STATUS = 'A'
           AND SITE_USE.LOCATION = P_CUSTOMER_SITE_USE
           AND ROWNUM = 1;
      EXCEPTION
        WHEN OTHERS THEN
          APP_EXCEPTION.RAISE_EXCEPTION('MEW'
                                       ,204
                                       ,'客户地点有错误');
      END;
    END IF;
    BEGIN
      SELECT 1
        INTO L_FLAG
        FROM FND_CURRENCIES_VL S
       WHERE S.CURRENCY_CODE = P_USR_CURRENCY_CODE
         AND ROWNUM = 1;
    EXCEPTION
      WHEN OTHERS THEN
        APP_EXCEPTION.RAISE_EXCEPTION('MEW'
                                     ,204
                                     ,'输入币种有错误');
    END;
    IF P_DOC_SEQUENCE_VALUE IS NULL
    THEN
    
      IF P_USR_EXCHANGE_RATE_TYPE IS NULL
      THEN
        AR_RECEIPT_API_PUB.CREATE_CASH(P_API_VERSION                  => 1
                                      ,P_INIT_MSG_LIST                => FND_API.G_TRUE
                                      ,P_COMMIT                       => FND_API.G_FALSE
                                      ,P_VALIDATION_LEVEL             => FND_API.G_VALID_LEVEL_FULL
                                      ,X_RETURN_STATUS                => GPR_RETURN_STATUS
                                      ,X_MSG_COUNT                    => GPR_MSG_COUNT
                                      ,X_MSG_DATA                     => GPR_MSG_DATA
                                      ,P_CURRENCY_CODE                => P_USR_CURRENCY_CODE
                                      ,P_AMOUNT                       => P_AMOUNT
                                      ,P_RECEIPT_DATE                 => P_RECEIPT_DATE
                                      ,P_REMITTANCE_BANK_ACCOUNT_NAME => P_BANK_NUM
                                      ,P_RECEIPT_NUMBER               => P_RECEIPT_NUMBER
                                      ,P_CUSTOMER_NUMBER              => P_CUSTOMER_NUMBER
                                      ,P_LOCATION                     => P_CUSTOMER_SITE_USE
                                      ,P_FACTOR_DISCOUNT_AMOUNT       => P_FACTOR_DISCOUNT_AMOUNT
                                      ,P_RECEIPT_METHOD_NAME          => P_METHOD
                                      ,P_CR_ID                        => I);
      ELSE
      
        BEGIN
          SELECT 1
            INTO L_FLAG
            FROM GL_DAILY_CONVERSION_TYPES S
           WHERE S.USER_CONVERSION_TYPE = P_USR_EXCHANGE_RATE_TYPE
             AND ROWNUM = 1;
        EXCEPTION
          WHEN OTHERS THEN
            APP_EXCEPTION.RAISE_EXCEPTION('MEW'
                                         ,204
                                         ,'输入汇率类型有错误');
        END;
        --------没有核销的发票号,则只建立收款,不核销.
        AR_RECEIPT_API_PUB.CREATE_CASH(P_API_VERSION                  => 1
                                      ,P_INIT_MSG_LIST                => FND_API.G_TRUE
                                      ,P_COMMIT                       => FND_API.G_FALSE
                                      ,P_VALIDATION_LEVEL             => FND_API.G_VALID_LEVEL_FULL
                                      ,X_RETURN_STATUS                => GPR_RETURN_STATUS
                                      ,X_MSG_COUNT                    => GPR_MSG_COUNT
                                      ,X_MSG_DATA                     => GPR_MSG_DATA
                                      ,P_CURRENCY_CODE                => P_USR_CURRENCY_CODE
                                      ,P_USR_EXCHANGE_RATE_TYPE       => P_USR_EXCHANGE_RATE_TYPE
                                      ,P_AMOUNT                       => P_AMOUNT
                                      ,P_REMITTANCE_BANK_ACCOUNT_NAME => P_BANK_NUM
                                      ,P_RECEIPT_DATE                 => P_RECEIPT_DATE
                                      ,P_RECEIPT_NUMBER               => P_RECEIPT_NUMBER
                                      ,P_CUSTOMER_NUMBER              => P_CUSTOMER_NUMBER
                                      ,P_LOCATION                     => P_CUSTOMER_SITE_USE
                                      ,P_FACTOR_DISCOUNT_AMOUNT       => P_FACTOR_DISCOUNT_AMOUNT
                                      ,P_RECEIPT_METHOD_NAME          => P_METHOD
                                      ,P_CR_ID                        => I);
      END IF;
    ELSE
    
      IF P_USR_EXCHANGE_RATE_TYPE IS NULL
      THEN
      
        AR_RECEIPT_API_PUB.CREATE_CASH(P_API_VERSION                  => 1
                                      ,P_INIT_MSG_LIST                => FND_API.G_TRUE
                                      ,P_COMMIT                       => FND_API.G_FALSE
                                      ,P_VALIDATION_LEVEL             => FND_API.G_VALID_LEVEL_FULL
                                      ,X_RETURN_STATUS                => GPR_RETURN_STATUS
                                      ,X_MSG_COUNT                    => GPR_MSG_COUNT
                                      ,X_MSG_DATA                     => GPR_MSG_DATA
                                      ,P_CURRENCY_CODE                => P_USR_CURRENCY_CODE
                                      ,P_AMOUNT                       => P_AMOUNT
                                      ,P_REMITTANCE_BANK_ACCOUNT_NAME => P_BANK_NUM
                                      ,P_RECEIPT_DATE                 => P_RECEIPT_DATE
                                      ,P_RECEIPT_NUMBER               => P_RECEIPT_NUMBER
                                      ,P_CUSTOMER_NUMBER              => P_CUSTOMER_NUMBER
                                      ,P_LOCATION                     => P_CUSTOMER_SITE_USE
                                      ,P_DOC_SEQUENCE_VALUE           => P_DOC_SEQUENCE_VALUE
                                      ,P_FACTOR_DISCOUNT_AMOUNT       => P_FACTOR_DISCOUNT_AMOUNT
                                      ,P_RECEIPT_METHOD_NAME          => P_METHOD
                                      ,P_CR_ID                        => I);
      ELSE
      
        BEGIN
          SELECT 1
            INTO L_FLAG
            FROM GL_DAILY_CONVERSION_TYPES S
           WHERE S.USER_CONVERSION_TYPE = P_USR_EXCHANGE_RATE_TYPE
             AND ROWNUM = 1;
        EXCEPTION
          WHEN OTHERS THEN
            APP_EXCEPTION.RAISE_EXCEPTION('MEW'
                                         ,204
                                         ,'输入汇率类型有错误');
        END;
      
        AR_RECEIPT_API_PUB.CREATE_CASH(P_API_VERSION                  => 1
                                      ,P_INIT_MSG_LIST                => FND_API.G_TRUE
                                      ,P_COMMIT                       => FND_API.G_FALSE
                                      ,P_VALIDATION_LEVEL             => FND_API.G_VALID_LEVEL_FULL
                                      ,X_RETURN_STATUS                => GPR_RETURN_STATUS
                                      ,X_MSG_COUNT                    => GPR_MSG_COUNT
                                      ,X_MSG_DATA                     => GPR_MSG_DATA
                                      ,P_CURRENCY_CODE                => P_USR_CURRENCY_CODE
                                      ,P_USR_EXCHANGE_RATE_TYPE       => P_USR_EXCHANGE_RATE_TYPE
                                      ,P_AMOUNT                       => P_AMOUNT
                                      ,P_REMITTANCE_BANK_ACCOUNT_NAME => P_BANK_NUM
                                      ,P_RECEIPT_DATE                 => P_RECEIPT_DATE
                                      ,P_RECEIPT_NUMBER               => P_RECEIPT_NUMBER
                                      ,P_CUSTOMER_NUMBER              => P_CUSTOMER_NUMBER
                                      ,P_LOCATION                     => P_CUSTOMER_SITE_USE
                                      ,P_DOC_SEQUENCE_VALUE           => P_DOC_SEQUENCE_VALUE
                                      ,P_FACTOR_DISCOUNT_AMOUNT       => P_FACTOR_DISCOUNT_AMOUNT
                                      ,P_RECEIPT_METHOD_NAME          => P_METHOD
                                      ,P_CR_ID                        => I);
      END IF;
    END IF;
  
    IF GPR_RETURN_STATUS <> FND_API.G_RET_STS_SUCCESS
    THEN
      IF GPR_MSG_COUNT = 1
      THEN
      
        L_MSG := GPR_MSG_DATA;
      ELSIF GPR_MSG_COUNT > 1
      THEN
        LOOP
          IF NVL(I
                ,0) < GPR_MSG_COUNT
          THEN
            I     := NVL(I
                        ,0) + 1;
            L_MSG := L_MSG || FND_MSG_PUB.GET(FND_MSG_PUB.G_NEXT
                                             ,FND_API.G_FALSE);
          ELSE
            EXIT;
          END IF;
        END LOOP;
      END IF;
    
      APP_EXCEPTION.RAISE_EXCEPTION('MEW'
                                   ,20004
                                   ,L_MSG);
    END IF;
  
  EXCEPTION
    WHEN OTHERS THEN
      APP_EXCEPTION.RAISE_EXCEPTION('MEW'
                                   ,20003
                                   ,SQLERRM);
  END;

  PROCEDURE INTERFACE_IMPORT_APPLY(P_RECEIPT_NUMBER   VARCHAR2
                                  ,P_TRX_NUMBER       VARCHAR2
                                  , --核销发票编号
                                   P_AMOUNT_APPLIED   NUMBER
                                  ,P_AMOUNT_APPLIED_F NUMBER) IS
  
    GPR_RETURN_STATUS VARCHAR2(1) := NULL;
    GPR_MSG_COUNT     NUMBER := 0;
    GPR_MSG_DATA      VARCHAR2(2000);
  
    V_ID  NUMBER; -- add by zhuhailong
    V_STR VARCHAR2(50); -- add by zhuhailong
  
    L_MSG VARCHAR2(2000);
    I     NUMBER;
  
  BEGIN
    V_ID := TO_NUMBER(SUBSTR(P_RECEIPT_NUMBER
                            ,1
                            ,INSTR(P_RECEIPT_NUMBER
                                  ,',') - 1)); -- add by zhuhailong
    -- add by zhuhailong
    IF V_ID IS NULL
    THEN
      V_STR := SUBSTR(P_RECEIPT_NUMBER
                     ,2);
      IF P_AMOUNT_APPLIED_F IS NULL
      THEN
      
        AR_RECEIPT_API_PUB.APPLY(P_API_VERSION      => 1
                                ,P_INIT_MSG_LIST    => FND_API.G_TRUE
                                ,P_COMMIT           => FND_API.G_FALSE
                                ,P_VALIDATION_LEVEL => FND_API.G_VALID_LEVEL_FULL
                                ,X_RETURN_STATUS    => GPR_RETURN_STATUS
                                ,X_MSG_COUNT        => GPR_MSG_COUNT
                                ,X_MSG_DATA         => GPR_MSG_DATA
                                ,P_RECEIPT_NUMBER   => V_STR
                                ,P_TRX_NUMBER       => P_TRX_NUMBER
                                ,P_AMOUNT_APPLIED   => P_AMOUNT_APPLIED /*
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ,P_AMOUNT_APPLIED_FROM => P_AMOUNT_APPLIED_F*/);
      ELSE
      
        AR_RECEIPT_API_PUB.APPLY(P_API_VERSION         => 1
                                ,P_INIT_MSG_LIST       => FND_API.G_TRUE
                                ,P_COMMIT              => FND_API.G_FALSE
                                ,P_VALIDATION_LEVEL    => FND_API.G_VALID_LEVEL_FULL
                                ,X_RETURN_STATUS       => GPR_RETURN_STATUS
                                ,X_MSG_COUNT           => GPR_MSG_COUNT
                                ,X_MSG_DATA            => GPR_MSG_DATA
                                ,P_RECEIPT_NUMBER      => V_STR
                                ,P_TRX_NUMBER          => P_TRX_NUMBER
                                ,P_AMOUNT_APPLIED      => P_AMOUNT_APPLIED
                                ,P_AMOUNT_APPLIED_FROM => P_AMOUNT_APPLIED_F);
      END IF;
    ELSE
      IF P_AMOUNT_APPLIED_F IS NULL
      THEN
        AR_RECEIPT_API_PUB.APPLY(P_API_VERSION      => 1
                                ,P_INIT_MSG_LIST    => FND_API.G_TRUE
                                ,P_COMMIT           => FND_API.G_FALSE
                                ,P_VALIDATION_LEVEL => FND_API.G_VALID_LEVEL_FULL
                                ,X_RETURN_STATUS    => GPR_RETURN_STATUS
                                ,X_MSG_COUNT        => GPR_MSG_COUNT
                                ,X_MSG_DATA         => GPR_MSG_DATA
                                ,P_CASH_RECEIPT_ID  => V_ID
                                ,P_TRX_NUMBER       => P_TRX_NUMBER
                                ,P_AMOUNT_APPLIED   => P_AMOUNT_APPLIED /*
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ,P_AMOUNT_APPLIED_FROM => P_AMOUNT_APPLIED_F*/);
      ELSE
        AR_RECEIPT_API_PUB.APPLY(P_API_VERSION         => 1
                                ,P_INIT_MSG_LIST       => FND_API.G_TRUE
                                ,P_COMMIT              => FND_API.G_FALSE
                                ,P_VALIDATION_LEVEL    => FND_API.G_VALID_LEVEL_FULL
                                ,X_RETURN_STATUS       => GPR_RETURN_STATUS
                                ,X_MSG_COUNT           => GPR_MSG_COUNT
                                ,X_MSG_DATA            => GPR_MSG_DATA
                                ,P_CASH_RECEIPT_ID     => V_ID
                                ,P_TRX_NUMBER          => P_TRX_NUMBER
                                ,P_AMOUNT_APPLIED      => P_AMOUNT_APPLIED
                                ,P_AMOUNT_APPLIED_FROM => P_AMOUNT_APPLIED_F);
      END IF;
    END IF;
    -- end add by zhuhailong
    -- 暂时删除
    /*      if P_AMOUNT_APPLIED_F is null then
    
      AR_RECEIPT_API_PUB.APPLY(P_API_VERSION      => 1
                              ,P_INIT_MSG_LIST    => FND_API.G_TRUE
                              ,P_COMMIT           => FND_API.G_FALSE
                              ,P_VALIDATION_LEVEL => FND_API.G_VALID_LEVEL_FULL
                              ,X_RETURN_STATUS    => GPR_RETURN_STATUS
                              ,X_MSG_COUNT        => GPR_MSG_COUNT
                              ,X_MSG_DATA         => GPR_MSG_DATA
                              ,P_RECEIPT_NUMBER   => P_RECEIPT_NUMBER
                              ,P_TRX_NUMBER       => P_TRX_NUMBER
                              ,P_AMOUNT_APPLIED   => P_AMOUNT_APPLIED \*
                                                                                                                                                                                                                                                                                                                                                                                 ,P_AMOUNT_APPLIED_FROM => P_AMOUNT_APPLIED_F*\);
    else
    
      AR_RECEIPT_API_PUB.APPLY(P_API_VERSION         => 1
                              ,P_INIT_MSG_LIST       => FND_API.G_TRUE
                              ,P_COMMIT              => FND_API.G_FALSE
                              ,P_VALIDATION_LEVEL    => FND_API.G_VALID_LEVEL_FULL
                              ,X_RETURN_STATUS       => GPR_RETURN_STATUS
                              ,X_MSG_COUNT           => GPR_MSG_COUNT
                              ,X_MSG_DATA            => GPR_MSG_DATA
                              ,P_RECEIPT_NUMBER      => P_RECEIPT_NUMBER
                              ,P_TRX_NUMBER          => P_TRX_NUMBER
                              ,P_AMOUNT_APPLIED      => P_AMOUNT_APPLIED
                              ,P_AMOUNT_APPLIED_FROM => P_AMOUNT_APPLIED_F);
    end if;*/
  
    IF GPR_RETURN_STATUS <> FND_API.G_RET_STS_SUCCESS
    THEN
      IF GPR_MSG_COUNT = 1
      THEN
      
        L_MSG := GPR_MSG_DATA;
      ELSIF GPR_MSG_COUNT > 1
      THEN
        LOOP
          IF NVL(I
                ,0) < GPR_MSG_COUNT
          THEN
            I     := NVL(I
                        ,0) + 1;
            L_MSG := L_MSG || FND_MSG_PUB.GET(FND_MSG_PUB.G_NEXT
                                             ,FND_API.G_FALSE);
          ELSE
            EXIT;
          END IF;
        END LOOP;
      END IF;
    
      APP_EXCEPTION.RAISE_EXCEPTION('MEW'
                                   ,20004
                                   ,L_MSG);
    END IF;
  
  EXCEPTION
    WHEN OTHERS THEN
      APP_EXCEPTION.RAISE_EXCEPTION('MEW'
                                   ,20003
                                   ,SQLERRM);
  END;

END MEW_AR_RECEIPT_IMPORT2;
/

你可能感兴趣的:(导入)