Delimiter //
/**
 * Author:  armen
 * Created: 26 févr. 2018
 * synchroniser si besoin (en fonction des modifs de champsà avec BUSINESS_FI_ANA_INSERT_NEW_ACCOUNTS
 * @see BUSINESS_UPDATE_FIANA
 * @see BUSINESS_FI_ANA_INSERT_NEW_ACCOUNTS
 * @see BUSINESS_BACKUP_FIANA
 * @see LOADER_ACCOUNTS_COPY_FIANA_BACKUP
 */
Drop Procedure if exists LOADER_ACCOUNTS_COPY_FIANA_INSERT//
Create Procedure LOADER_ACCOUNTS_COPY_FIANA_INSERT (
    In batchnum int
    , In lastId Int
)
Begin
    Set @idAdminParams = GET_LOADER_INFO('ID_ADMIN_PARAMS', batchnum);

    Insert Into FI_ANA (
            ACCOUNTS_ID_ACCOUNTS
            , ADMIN_PARAMS_ID_ADMIN_PARAMS
            , FI_ANA_NOT_OVERDUE
            , FI_ANA_AGE_PORTFOLIO_PERIOD1
            , FI_ANA_AGE_PORTFOLIO_PERIOD2
            , FI_ANA_AGE_PORTFOLIO_PERIOD3
            , FI_ANA_AGE_PORTFOLIO_PERIOD4
            , FI_ANA_AGE_PORTFOLIO_PERIOD5
            , FI_ANA_AGE_PORTFOLIO_PERIOD6
            , FI_ANA_AGE_PORTFOLIO_PERIOD7
            , FI_ANA_AGE_PORTFOLIO_PERIOD8
            , FI_ANA_OUTSTANDING
            , FI_ANA_OVERDUE
            , FI_ANA_FROZEN_AMOUNT
            , FI_ANA_DUE
            , FI_ANA_AGE_ACCOUNT_PERIOD1
            , FI_ANA_AGE_ACCOUNT_PERIOD2
            , FI_ANA_AGE_ACCOUNT_PERIOD3
            , FI_ANA_AGE_ACCOUNT_PERIOD4
            , FI_ANA_ACD
            , FI_ANA_ACD_LASTUPDAT
            , FI_ANA_BPDSO
            , FI_ANA_BPDSO_LASTUPDAT

            , FI_ANA_CASH_ASSESSMENT 
            , FI_ANA_CASH_ASSSSMNTLASTUPDAT 

            , FI_ANA_INTERNAL_SCORE_RATING 
            , FI_ANA_INTRSCORRATLASTUPDAT 

            , FI_ANA_EXTERNAL_SCORE_RATING 
            , FI_ANA_EXTSCORRATLASTUPDAT 
        ) 
    Select 
        src.ID_ACCOUNTS_NEW + lastId ID_ACCOUNTS
        , @idAdminParams ADMIN_ID_ADMIN
        , 0 FI_ANA_NOT_OVERDUE
        , 0 FI_ANA_AGE_PORTFOLIO_PERIOD1
        , 0 FI_ANA_AGE_PORTFOLIO_PERIOD2
        , 0 FI_ANA_AGE_PORTFOLIO_PERIOD3
        , 0 FI_ANA_AGE_PORTFOLIO_PERIOD4
        , 0 FI_ANA_AGE_PORTFOLIO_PERIOD5
        , 0 FI_ANA_AGE_PORTFOLIO_PERIOD6
        , 0 FI_ANA_AGE_PORTFOLIO_PERIOD7
        , 0 FI_ANA_AGE_PORTFOLIO_PERIOD8
        , 0 FI_ANA_OUTSTANDING
        , 0 FI_ANA_OVERDUE
        , 0 FI_ANA_FROZEN_AMOUNT
        , 0 FI_ANA_DUE
        , 0 FI_ANA_AGE_ACCOUNT_PERIOD1
        , 0 FI_ANA_AGE_ACCOUNT_PERIOD2
        , 0 FI_ANA_AGE_ACCOUNT_PERIOD3
        , 0 FI_ANA_AGE_ACCOUNT_PERIOD4
        , 0 FI_ANA_ACD
        , 0 FI_ANA_ACD_LASTUPDAT
        , 0 FI_ANA_BPDSO
        , 0 FI_ANA_BPDSO_LASTUPDAT

        , src.FI_ANA_CASH_ASSESSMENT 
        , src.FI_ANA_CASH_ASSSSMNTLASTUPDAT
#         , COALESCE(src.FI_ANA_CASH_ASSSSMNTLASTUPDAT, curdate())
        , src.FI_ANA_INTERNAL_SCORE_RATING 
        , src.FI_ANA_INTRSCORRATLASTUPDAT 

        , src.FI_ANA_EXTERNAL_SCORE_RATING 
        , src.FI_ANA_EXTSCORRATLASTUPDAT 
    From  
        LOADER_ACCOUNTS src  
        Inner Join LOADER_STATUS lst  
            On src.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS  
    Where 1 = 1  
        And lst.LOADER_STATUS_ERRCOUNT = 0  
        And lst.LOADER_STATUS_BATCH_NUM =  batchnum
        and Coalesce(src.ID_ACCOUNTS_NEW, 0) <> 0 
    ;
End //

Delimiter ;






