Delimiter //
/**
 * Backup the FI_ANA_HISTO rows at a given date to FI_ANA_MEMBERS_HISTO
 *
 * @author Benoit
 * @see BUSINESS_UPDATE_FIANA
 * @see BUSINESS_UPDATE_FIANA_MEMBERS
 * @see BUSINESS_BACKUP_FIANA
 * @see BUSINESS_BACKUP_FIANA_MEMBERS
 */ 
Drop Procedure if Exists BUSINESS_BACKUP_FIANA_MEMBERS_FROM_HISTO//
 
Create Procedure BUSINESS_BACKUP_FIANA_MEMBERS_FROM_HISTO(In histodate Date, In idmembers Int unsigned, In usetransaction Int)
Begin
    main: Begin

        Declare dateFilter Date Default Ifnull(histodate, AMO_BAL_REFDATE());
--         Declare memberFilter Varchar(10) Default Ifnull(idmembers, '%');
        Declare transactionStarted Int Default 0 ;

        Declare Exit Handler For Sqlexception
        Begin
            If transactionStarted  <> 0 Then
                Rollback;
                Set transactionStarted  = 0;
            End If;
            Resignal;
        End ;

        if idmembers Is null Then
            Set @bbfamfh_mbrMinVal = 0;
            Set @bbfamfh_mbrMaxVal = 4e5;
        Else
            Set @bbfamfh_mbrMinVal = idaccounts;
            Set @bbfamfh_mbrMaxVal = idaccounts;
        End if;

		If Coalesce(usetransaction, 0) <> 0 Then
            Start Transaction;
            Set transactionStarted  = 1;
		End If;

		If Coalesce(usetransaction, 0) <> 0 Then
            -- nettoyage historique du mois
            Delete HISTO
            From 
                FI_ANA_MEMBERS FI
                Inner Join FI_ANA_MEMBERS_HISTO HISTO
                    On FI.MEMBERS_ID_MEMBERS = HISTO.MEMBERS_ID_MEMBERS
                Inner Join DF_DATES
                    On HISTO.FI_ANA_HISTO_DATE = DF_DATES.DF_DATES_FULL
            Where
                (
                    DF_DATES.DF_DATES_YEAR = Extract(Year From AMO_BAL_REFDATE()) 
                    And DF_DATES.DF_DATES_MONTH = Extract(Month From AMO_BAL_REFDATE())
                )
                And HISTO.MEMBERS_ID_MEMBERS Between @bbfamfh_mbrMinVal And @bbfamfh_mbrMaxVal
--                 And HISTO.MEMBERS_ID_MEMBERS Like memberFilter
            ;
		End If;

        Drop Temporary Table If Exists TMP_FIANA_MBRS_BACKUP;
        Create Temporary Table TMP_FIANA_MBRS_BACKUP As 
            Select
                FIMBR.MEMBERS_ID_MEMBERS
                , Count(FIHISTO.ACCOUNTS_ID_ACCOUNTS) FI_ANA_ACCOUNTS_COUNT
                , Md5(sum(FIHISTO.ACCOUNTS_ID_ACCOUNTS)) FI_ANA_ACCOUNTS_HASH
                   , Sum(Coalesce(FIHISTO.FI_ANA_OUTSTANDING, 0)) FI_ANA_OUTSTANDING
                   , Sum(Coalesce(FIHISTO.FI_ANA_OVERDUE, 0)) FI_ANA_OVERDUE
                   , Sum(Coalesce(FIHISTO.FI_ANA_NOT_OVERDUE, 0)) FI_ANA_NOT_OVERDUE
                   , Sum(Coalesce(FIHISTO.FI_ANA_DUE, 0)) FI_ANA_DUE
                   , Sum(Coalesce(FIHISTO.FI_ANA_FROZEN_AMOUNT, 0)) FI_ANA_FROZEN_AMOUNT
                   , Sum(Coalesce(FIHISTO.FI_ANA_BLOCKED_ALL, 0)) FI_ANA_BLOCKED_ALL
                   , Sum(Coalesce(FIHISTO.FI_ANA_BLOCKED_NOTOVERDUE, 0)) FI_ANA_BLOCKED_NOTOVERDUE
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD1, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD1
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD2, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD2
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD3, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD3
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD4, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD4
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD5, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD5
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD6, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD6
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD7, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD7
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD8, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD8
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_ACCOUNT_PERIOD1, 0)) FI_ANA_AGE_ACCOUNT_PERIOD1
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_ACCOUNT_PERIOD2, 0)) FI_ANA_AGE_ACCOUNT_PERIOD2
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_ACCOUNT_PERIOD3, 0)) FI_ANA_AGE_ACCOUNT_PERIOD3
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_ACCOUNT_PERIOD4, 0)) FI_ANA_AGE_ACCOUNT_PERIOD4
                   , Sum(Coalesce(FIHISTO.FI_ANA_CURRENT_ORDER_AMOUNT, 0)) FI_ANA_CURRENT_ORDER_AMOUNT
                   , Sum(Coalesce(FIHISTO.FI_ANA_BLOCKED_ORDER_AMOUNT, 0)) FI_ANA_BLOCKED_ORDER_AMOUNT
                   , Sum(Coalesce(FIHISTO.FI_ANA_DELIVERY_TOBEINVOICED, 0)) FI_ANA_DELIVERY_TOBEINVOICED
                   , Sum(Coalesce(FIHISTO.FI_ANA_BLOCKED_DELIVERY_TOBEINVOICED, 0)) FI_ANA_BLOCKED_DELIVERY_TOBEINVOICED
                   , Sum(Coalesce(FIHISTO.FI_ANA_BANK_DRAFT, 0)) FI_ANA_BANK_DRAFT
                   , Sum(Coalesce(FIHISTO.FI_ANA_SALES, 0)) FI_ANA_SALES
                   , Sum(Coalesce(FIHISTO.FI_ANA_PAYMENT, 0)) FI_ANA_PAYMENT
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_OUTSTANDING, 0)) FI_ANA_UNBOUND_OUTSTANDING
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_OVERDUE, 0)) FI_ANA_UNBOUND_OVERDUE
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_NOT_OVERDUE, 0)) FI_ANA_UNBOUND_NOT_OVERDUE
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_DUE, 0)) FI_ANA_UNBOUND_DUE
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_FROZEN_AMOUNT, 0)) FI_ANA_UNBOUND_FROZEN_AMOUNT
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_BLOCKED_ALL, 0)) FI_ANA_UNBOUND_BLOCKED_ALL
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_BLOCKED_NOTOVERDUE, 0)) FI_ANA_UNBOUND_BLOCKED_NOTOVERDUE
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_CURRENT_ORDER_AMOUNT, 0)) FI_ANA_UNBOUND_CURRENT_ORDER_AMOUNT
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_BLOCKED_ORDER_AMOUNT, 0)) FI_ANA_UNBOUND_BLOCKED_ORDER_AMOUNT
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_DELIVERY_TOBEINVOICED, 0)) FI_ANA_UNBOUND_DELIVERY_TOBEINVOICED
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_BLOCKED_DELIVERY_TOBEINVOICED, 0)) FI_ANA_UNBOUND_BLOCKED_DELIVERY_TOBEINVOICED
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_BANK_DRAFT, 0)) FI_ANA_UNBOUND_BANK_DRAFT
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_SALES, 0)) FI_ANA_UNBOUND_SALES
                   , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_PAYMENT, 0)) FI_ANA_UNBOUND_PAYMENT
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_CUSTMMDE_PERIOD1, 0)) FI_ANA_AGE_CUSTMMDE_PERIOD1
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_CUSTMMDE_PERIOD2, 0)) FI_ANA_AGE_CUSTMMDE_PERIOD2
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_CUSTMMDE_PERIOD3, 0)) FI_ANA_AGE_CUSTMMDE_PERIOD3
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_CUSTMMDE_PERIOD4, 0)) FI_ANA_AGE_CUSTMMDE_PERIOD4
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_CUSTMMDE_DEBIT, 0)) FI_ANA_AGE_CUSTMMDE_DEBIT
                   , Sum(Coalesce(FIHISTO.FI_ANA_AGE_CUSTMMDE_CREDIT, 0)) FI_ANA_AGE_CUSTMMDE_CREDIT
            From
                FI_ANA_MEMBERS FIMBR
                Inner Join TMP_ACCOUNTS_CREDENTIALS_MEMBER__ALL CRED
                    On CRED.MEMBERS_ID_MEMBERS = FIMBR.MEMBERS_ID_MEMBERS
                Inner Join FI_ANA_HISTO FIHISTO
                    On FIHISTO.ACCOUNTS_ID_ACCOUNTS = CRED.ACCOUNTS_ID_ACCOUNTS
            Where 
                FIMBR.MEMBERS_ID_MEMBERS  Between @bbfamfh_mbrMinVal And @bbfamfh_mbrMaxVal
--                 FIMBR.MEMBERS_ID_MEMBERS Like memberFilter
                And Extract(YEAR_MONTH From FIHISTO.FI_ANA_HISTO_DATE) = Extract(YEAR_MONTH From histodate)
            Group By 
                FIMBR.MEMBERS_ID_MEMBERS
--         Create Temporary Table TMP_FIANA_MBRS_BACKUP As 
--             Select
--                 FIMBR.MEMBERS_ID_MEMBERS
--                 , Sum(Coalesce(FIHISTO.FI_ANA_OUTSTANDING, 0)) FI_ANA_OUTSTANDING
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_OUTSTANDING, 0)) FI_ANA_UNBOUND_OUTSTANDING
--                 , Sum(Coalesce(FIHISTO.FI_ANA_OVERDUE, 0)) FI_ANA_OVERDUE
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_OVERDUE, 0)) FI_ANA_UNBOUND_OVERDUE
--                 , Sum(Coalesce(FIHISTO.FI_ANA_NOT_OVERDUE, 0)) FI_ANA_NOT_OVERDUE
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_NOT_OVERDUE, 0)) FI_ANA_UNBOUND_NOT_OVERDUE
--                 , Sum(Coalesce(FIHISTO.FI_ANA_DUE, 0)) FI_ANA_DUE
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_DUE, 0)) FI_ANA_UNBOUND_DUE
--                 , Sum(Coalesce(FIHISTO.FI_ANA_FROZEN_AMOUNT, 0)) FI_ANA_FROZEN_AMOUNT
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD1, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD1
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD2, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD2
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD3, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD3
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD4, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD4
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD5, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD5
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD6, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD6
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD7, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD7
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_PORTFOLIO_PERIOD8, 0)) FI_ANA_AGE_PORTFOLIO_PERIOD8
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_ACCOUNT_PERIOD1, 0)) FI_ANA_AGE_ACCOUNT_PERIOD1
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_ACCOUNT_PERIOD2, 0)) FI_ANA_AGE_ACCOUNT_PERIOD2
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_ACCOUNT_PERIOD3, 0)) FI_ANA_AGE_ACCOUNT_PERIOD3
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_ACCOUNT_PERIOD4, 0)) FI_ANA_AGE_ACCOUNT_PERIOD4
--                 , Sum(Coalesce(FIHISTO.FI_ANA_CURRENT_ORDER_AMOUNT, 0)) FI_ANA_CURRENT_ORDER_AMOUNT
--                 , Sum(Coalesce(FIHISTO.FI_ANA_DELIVERY_TOBEINVOICED, 0)) FI_ANA_DELIVERY_TOBEINVOICED
--                 , Sum(Coalesce(FIHISTO.FI_ANA_BANK_DRAFT, 0)) FI_ANA_BANK_DRAFT
--                 , Sum(Coalesce(FIHISTO.FI_ANA_SALES, 0)) FI_ANA_SALES
--                 , Sum(Coalesce(FIHISTO.FI_ANA_PAYMENT, 0)) FI_ANA_PAYMENT
--                 , Count(FIHISTO.ACCOUNTS_ID_ACCOUNTS) FI_ANA_ACCOUNTS_COUNT
-- --                 , Md5(Group_Concat(FIHISTO.ACCOUNTS_ID_ACCOUNTS ORDER BY FIHISTO.ACCOUNTS_ID_ACCOUNTS DESC SEPARATOR '-')) FI_ANA_ACCOUNTS_HASH
--                 , Md5(sum(FIHISTO.ACCOUNTS_ID_ACCOUNTS)) FI_ANA_ACCOUNTS_HASH
-- 				, Sum(Coalesce(FIHISTO.FI_ANA_BLOCKED_ALL, 0)) FI_ANA_BLOCKED_ALL
--                 , Sum(Coalesce(FIHISTO.FI_ANA_BLOCKED_NOTOVERDUE, 0)) FI_ANA_BLOCKED_NOTOVERDUE
--                 , Sum(Coalesce(FIHISTO.FI_ANA_BLOCKED_ORDER_AMOUNT, 0)) FI_ANA_BLOCKED_ORDER_AMOUNT
--                 , Sum(Coalesce(FIHISTO.FI_ANA_BLOCKED_DELIVERY_TOBEINVOICED, 0)) FI_ANA_BLOCKED_DELIVERY_TOBEINVOICED
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_CUSTMMDE_PERIOD1, 0)) FI_ANA_AGE_CUSTMMDE_PERIOD1
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_CUSTMMDE_PERIOD2, 0)) FI_ANA_AGE_CUSTMMDE_PERIOD2
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_CUSTMMDE_PERIOD3, 0)) FI_ANA_AGE_CUSTMMDE_PERIOD3
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_CUSTMMDE_PERIOD4, 0)) FI_ANA_AGE_CUSTMMDE_PERIOD4
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_CUSTMMDE_DEBIT, 0)) FI_ANA_AGE_CUSTMMDE_DEBIT
--                 , Sum(Coalesce(FIHISTO.FI_ANA_AGE_CUSTMMDE_CREDIT, 0)) FI_ANA_AGE_CUSTMMDE_CREDIT
-- 
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_FROZEN_AMOUNT, 0)) FI_ANA_UNBOUND_FROZEN_AMOUNT
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_BLOCKED_ALL, 0)) FI_ANA_UNBOUND_BLOCKED_ALL
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_BLOCKED_NOTOVERDUE, 0)) FI_ANA_UNBOUND_BLOCKED_NOTOVERDUE
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_CURRENT_ORDER_AMOUNT, 0)) FI_ANA_UNBOUND_CURRENT_ORDER_AMOUNT
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_BLOCKED_ORDER_AMOUNT, 0)) FI_ANA_UNBOUND_BLOCKED_ORDER_AMOUNT
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_DELIVERY_TOBEINVOICED, 0)) FI_ANA_UNBOUND_DELIVERY_TOBEINVOICED
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_BLOCKED_DELIVERY_TOBEINVOICED, 0)) FI_ANA_UNBOUND_BLOCKED_DELIVERY_TOBEINVOICED
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_BANK_DRAFT, 0)) FI_ANA_UNBOUND_BANK_DRAFT
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_SALES, 0)) FI_ANA_UNBOUND_SALES
--                 , Sum(Coalesce(FIHISTO.FI_ANA_UNBOUND_PAYMENT, 0)) FI_ANA_UNBOUND_PAYMENT
-- 
--             From
--                 FI_ANA_MEMBERS FIMBR
--                 Inner Join TMP_ACCOUNTS_CREDENTIALS_MEMBER__ALL CRED
--                     On CRED.MEMBERS_ID_MEMBERS = FIMBR.MEMBERS_ID_MEMBERS
--                 Inner Join FI_ANA_HISTO FIHISTO
--                     On FIHISTO.ACCOUNTS_ID_ACCOUNTS = CRED.ACCOUNTS_ID_ACCOUNTS
--             Where 
--                 FIMBR.MEMBERS_ID_MEMBERS Like memberFilter
--                 And Extract(YEAR_MONTH From FIHISTO.FI_ANA_HISTO_DATE) = Extract(YEAR_MONTH From histodate)
--             Group By 
--                 FIMBR.MEMBERS_ID_MEMBERS
        ;

        -- Insert rows
        Insert Into 
            FI_ANA_MEMBERS_HISTO (
                MEMBERS_ID_MEMBERS
                , ADMIN_PARAMS_ID_ADMIN_PARAMS
                , FI_ANA_ACCOUNTS_COUNT
                , FI_ANA_ACCOUNTS_HASH
                , FI_ANA_HISTO_DATE
                   , FI_ANA_OUTSTANDING
                   , FI_ANA_OVERDUE
                   , FI_ANA_NOT_OVERDUE
                   , FI_ANA_DUE
                   , FI_ANA_FROZEN_AMOUNT
                   , FI_ANA_BLOCKED_ALL
                   , FI_ANA_BLOCKED_NOTOVERDUE
                   , 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_AGE_ACCOUNT_PERIOD1
                   , FI_ANA_AGE_ACCOUNT_PERIOD2
                   , FI_ANA_AGE_ACCOUNT_PERIOD3
                   , FI_ANA_AGE_ACCOUNT_PERIOD4
                   , FI_ANA_CURRENT_ORDER_AMOUNT
                   , FI_ANA_BLOCKED_ORDER_AMOUNT
                   , FI_ANA_DELIVERY_TOBEINVOICED
                   , FI_ANA_BLOCKED_DELIVERY_TOBEINVOICED
                   , FI_ANA_BANK_DRAFT
                   , FI_ANA_SALES
                   , FI_ANA_PAYMENT
                   , FI_ANA_UNBOUND_OUTSTANDING
                   , FI_ANA_UNBOUND_OVERDUE
                   , FI_ANA_UNBOUND_NOT_OVERDUE
                   , FI_ANA_UNBOUND_DUE
                   , FI_ANA_UNBOUND_FROZEN_AMOUNT
                   , FI_ANA_UNBOUND_BLOCKED_ALL
                   , FI_ANA_UNBOUND_BLOCKED_NOTOVERDUE
                   , FI_ANA_UNBOUND_CURRENT_ORDER_AMOUNT
                   , FI_ANA_UNBOUND_BLOCKED_ORDER_AMOUNT
                   , FI_ANA_UNBOUND_DELIVERY_TOBEINVOICED
                   , FI_ANA_UNBOUND_BLOCKED_DELIVERY_TOBEINVOICED
                   , FI_ANA_UNBOUND_BANK_DRAFT
                   , FI_ANA_UNBOUND_SALES
                   , FI_ANA_UNBOUND_PAYMENT
                   , FI_ANA_AGE_CUSTMMDE_PERIOD1
                   , FI_ANA_AGE_CUSTMMDE_PERIOD2
                   , FI_ANA_AGE_CUSTMMDE_PERIOD3
                   , FI_ANA_AGE_CUSTMMDE_PERIOD4
                   , FI_ANA_AGE_CUSTMMDE_DEBIT
                   , FI_ANA_AGE_CUSTMMDE_CREDIT
            ) 
            Select
                FITMP.MEMBERS_ID_MEMBERS
                , PARAM.ADMIN_PARAMS_ID_ADMIN_PARAMS
                , FITMP.FI_ANA_ACCOUNTS_COUNT
                , FITMP.FI_ANA_ACCOUNTS_HASH
                , histodate
                   , FITMP.FI_ANA_OUTSTANDING FI_ANA_OUTSTANDING
                   , FITMP.FI_ANA_OVERDUE FI_ANA_OVERDUE
                   , FITMP.FI_ANA_NOT_OVERDUE FI_ANA_NOT_OVERDUE
                   , FITMP.FI_ANA_DUE FI_ANA_DUE
                   , FITMP.FI_ANA_FROZEN_AMOUNT FI_ANA_FROZEN_AMOUNT
                   , FITMP.FI_ANA_BLOCKED_ALL FI_ANA_BLOCKED_ALL
                   , FITMP.FI_ANA_BLOCKED_NOTOVERDUE FI_ANA_BLOCKED_NOTOVERDUE
                   , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD1 FI_ANA_AGE_PORTFOLIO_PERIOD1
                   , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD2 FI_ANA_AGE_PORTFOLIO_PERIOD2
                   , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD3 FI_ANA_AGE_PORTFOLIO_PERIOD3
                   , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD4 FI_ANA_AGE_PORTFOLIO_PERIOD4
                   , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD5 FI_ANA_AGE_PORTFOLIO_PERIOD5
                   , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD6 FI_ANA_AGE_PORTFOLIO_PERIOD6
                   , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD7 FI_ANA_AGE_PORTFOLIO_PERIOD7
                   , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD8 FI_ANA_AGE_PORTFOLIO_PERIOD8
                   , FITMP.FI_ANA_AGE_ACCOUNT_PERIOD1 FI_ANA_AGE_ACCOUNT_PERIOD1
                   , FITMP.FI_ANA_AGE_ACCOUNT_PERIOD2 FI_ANA_AGE_ACCOUNT_PERIOD2
                   , FITMP.FI_ANA_AGE_ACCOUNT_PERIOD3 FI_ANA_AGE_ACCOUNT_PERIOD3
                   , FITMP.FI_ANA_AGE_ACCOUNT_PERIOD4 FI_ANA_AGE_ACCOUNT_PERIOD4
                   , FITMP.FI_ANA_CURRENT_ORDER_AMOUNT FI_ANA_CURRENT_ORDER_AMOUNT
                   , FITMP.FI_ANA_BLOCKED_ORDER_AMOUNT FI_ANA_BLOCKED_ORDER_AMOUNT
                   , FITMP.FI_ANA_DELIVERY_TOBEINVOICED FI_ANA_DELIVERY_TOBEINVOICED
                   , FITMP.FI_ANA_BLOCKED_DELIVERY_TOBEINVOICED FI_ANA_BLOCKED_DELIVERY_TOBEINVOICED
                   , FITMP.FI_ANA_BANK_DRAFT FI_ANA_BANK_DRAFT
                   , FITMP.FI_ANA_SALES FI_ANA_SALES
                   , FITMP.FI_ANA_PAYMENT FI_ANA_PAYMENT
                   , FITMP.FI_ANA_UNBOUND_OUTSTANDING FI_ANA_UNBOUND_OUTSTANDING
                   , FITMP.FI_ANA_UNBOUND_OVERDUE FI_ANA_UNBOUND_OVERDUE
                   , FITMP.FI_ANA_UNBOUND_NOT_OVERDUE FI_ANA_UNBOUND_NOT_OVERDUE
                   , FITMP.FI_ANA_UNBOUND_DUE FI_ANA_UNBOUND_DUE
                   , FITMP.FI_ANA_UNBOUND_FROZEN_AMOUNT FI_ANA_UNBOUND_FROZEN_AMOUNT
                   , FITMP.FI_ANA_UNBOUND_BLOCKED_ALL FI_ANA_UNBOUND_BLOCKED_ALL
                   , FITMP.FI_ANA_UNBOUND_BLOCKED_NOTOVERDUE FI_ANA_UNBOUND_BLOCKED_NOTOVERDUE
                   , FITMP.FI_ANA_UNBOUND_CURRENT_ORDER_AMOUNT FI_ANA_UNBOUND_CURRENT_ORDER_AMOUNT
                   , FITMP.FI_ANA_UNBOUND_BLOCKED_ORDER_AMOUNT FI_ANA_UNBOUND_BLOCKED_ORDER_AMOUNT
                   , FITMP.FI_ANA_UNBOUND_DELIVERY_TOBEINVOICED FI_ANA_UNBOUND_DELIVERY_TOBEINVOICED
                   , FITMP.FI_ANA_UNBOUND_BLOCKED_DELIVERY_TOBEINVOICED FI_ANA_UNBOUND_BLOCKED_DELIVERY_TOBEINVOICED
                   , FITMP.FI_ANA_UNBOUND_BANK_DRAFT FI_ANA_UNBOUND_BANK_DRAFT
                   , FITMP.FI_ANA_UNBOUND_SALES FI_ANA_UNBOUND_SALES
                   , FITMP.FI_ANA_UNBOUND_PAYMENT FI_ANA_UNBOUND_PAYMENT
                   , FITMP.FI_ANA_AGE_CUSTMMDE_PERIOD1 FI_ANA_AGE_CUSTMMDE_PERIOD1
                   , FITMP.FI_ANA_AGE_CUSTMMDE_PERIOD2 FI_ANA_AGE_CUSTMMDE_PERIOD2
                   , FITMP.FI_ANA_AGE_CUSTMMDE_PERIOD3 FI_ANA_AGE_CUSTMMDE_PERIOD3
                   , FITMP.FI_ANA_AGE_CUSTMMDE_PERIOD4 FI_ANA_AGE_CUSTMMDE_PERIOD4
                   , FITMP.FI_ANA_AGE_CUSTMMDE_DEBIT FI_ANA_AGE_CUSTMMDE_DEBIT
                   , FITMP.FI_ANA_AGE_CUSTMMDE_CREDIT FI_ANA_AGE_CUSTMMDE_CREDIT
                   
            From 
                TMP_FIANA_MBRS_BACKUP FITMP
                Inner Join ADMIN_PARAMS_has_MEMBERS PARAM
                    On PARAM.MEMBERS_ID_MEMBERS = FITMP.MEMBERS_ID_MEMBERS
            Where 
                FITMP.MEMBERS_ID_MEMBERS  Between @bbfamfh_mbrMinVal And @bbfamfh_mbrMaxVal
--                 FITMP.MEMBERS_ID_MEMBERS Like memberFilter
        ;
--         Insert Into 
--             FI_ANA_MEMBERS_HISTO (
--                 MEMBERS_ID_MEMBERS
--                 , ADMIN_PARAMS_ID_ADMIN_PARAMS
--                 , FI_ANA_OUTSTANDING
--                 , FI_ANA_UNBOUND_OUTSTANDING
--                 , FI_ANA_OVERDUE
--                 , FI_ANA_UNBOUND_OVERDUE
--                 , FI_ANA_NOT_OVERDUE
--                 , FI_ANA_UNBOUND_NOT_OVERDUE
--                 , FI_ANA_DUE
--                 , FI_ANA_UNBOUND_DUE
--                 , FI_ANA_FROZEN_AMOUNT
--                 , 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_AGE_ACCOUNT_PERIOD1
--                 , FI_ANA_AGE_ACCOUNT_PERIOD2
--                 , FI_ANA_AGE_ACCOUNT_PERIOD3
--                 , FI_ANA_AGE_ACCOUNT_PERIOD4
--                 , FI_ANA_CURRENT_ORDER_AMOUNT
--                 , FI_ANA_DELIVERY_TOBEINVOICED
--                 , FI_ANA_BANK_DRAFT
--                 , FI_ANA_SALES
--                 , FI_ANA_PAYMENT
--                 , FI_ANA_ACCOUNTS_COUNT
--                 , FI_ANA_ACCOUNTS_HASH
--                 , FI_ANA_HISTO_DATE
--                 , FI_ANA_BLOCKED_ALL
--                 , FI_ANA_BLOCKED_NOTOVERDUE
--                 , FI_ANA_BLOCKED_ORDER_AMOUNT
--                 , FI_ANA_BLOCKED_DELIVERY_TOBEINVOICED
--                 , FI_ANA_AGE_CUSTMMDE_PERIOD1
--                 , FI_ANA_AGE_CUSTMMDE_PERIOD2
--                 , FI_ANA_AGE_CUSTMMDE_PERIOD3
--                 , FI_ANA_AGE_CUSTMMDE_PERIOD4
--                 , FI_ANA_AGE_CUSTMMDE_DEBIT
--                 , FI_ANA_AGE_CUSTMMDE_CREDIT
-- 
--                 ,  FI_ANA_UNBOUND_FROZEN_AMOUNT
--                 ,  FI_ANA_UNBOUND_BLOCKED_ALL
--                 ,  FI_ANA_UNBOUND_BLOCKED_NOTOVERDUE
--                 ,  FI_ANA_UNBOUND_CURRENT_ORDER_AMOUNT
--                 ,  FI_ANA_UNBOUND_BLOCKED_ORDER_AMOUNT
--                 ,  FI_ANA_UNBOUND_DELIVERY_TOBEINVOICED
--                 ,  FI_ANA_UNBOUND_BLOCKED_DELIVERY_TOBEINVOICED
--                 ,  FI_ANA_UNBOUND_BANK_DRAFT
--                 ,  FI_ANA_UNBOUND_SALES
--                 ,  FI_ANA_UNBOUND_PAYMENT
-- 
--             ) 
--             Select
--                 FITMP.MEMBERS_ID_MEMBERS
--                 , PARAM.ADMIN_PARAMS_ID_ADMIN_PARAMS
--                 , FITMP.FI_ANA_OUTSTANDING
--                 , FITMP.FI_ANA_UNBOUND_OUTSTANDING
--                 , FITMP.FI_ANA_OVERDUE
--                 , FITMP.FI_ANA_UNBOUND_OVERDUE
--                 , FITMP.FI_ANA_NOT_OVERDUE
--                 , FITMP.FI_ANA_UNBOUND_NOT_OVERDUE
--                 , FITMP.FI_ANA_DUE
--                 , FITMP.FI_ANA_UNBOUND_DUE
--                 , FITMP.FI_ANA_FROZEN_AMOUNT
--                 , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD1
--                 , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD2
--                 , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD3
--                 , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD4
--                 , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD5
--                 , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD6
--                 , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD7
--                 , FITMP.FI_ANA_AGE_PORTFOLIO_PERIOD8
--                 , FITMP.FI_ANA_AGE_ACCOUNT_PERIOD1
--                 , FITMP.FI_ANA_AGE_ACCOUNT_PERIOD2
--                 , FITMP.FI_ANA_AGE_ACCOUNT_PERIOD3
--                 , FITMP.FI_ANA_AGE_ACCOUNT_PERIOD4
--                 , FITMP.FI_ANA_CURRENT_ORDER_AMOUNT
--                 , FITMP.FI_ANA_DELIVERY_TOBEINVOICED
--                 , FITMP.FI_ANA_BANK_DRAFT
--                 , FITMP.FI_ANA_SALES
--                 , FITMP.FI_ANA_PAYMENT
--                 , FITMP.FI_ANA_ACCOUNTS_COUNT
--                 , FITMP.FI_ANA_ACCOUNTS_HASH
--                 , histodate
-- 				, FITMP.FI_ANA_BLOCKED_ALL
--                 , FITMP.FI_ANA_BLOCKED_NOTOVERDUE
--                 , FITMP.FI_ANA_BLOCKED_ORDER_AMOUNT
--                 , FITMP.FI_ANA_BLOCKED_DELIVERY_TOBEINVOICED
--                 , FITMP.FI_ANA_AGE_CUSTMMDE_PERIOD1
--                 , FITMP.FI_ANA_AGE_CUSTMMDE_PERIOD2
--                 , FITMP.FI_ANA_AGE_CUSTMMDE_PERIOD3
--                 , FITMP.FI_ANA_AGE_CUSTMMDE_PERIOD4
--                 , FITMP.FI_ANA_AGE_CUSTMMDE_DEBIT
--                 , FITMP.FI_ANA_AGE_CUSTMMDE_CREDIT
-- 
--                 ,  FI_ANA_UNBOUND_FROZEN_AMOUNT
--                 ,  FI_ANA_UNBOUND_BLOCKED_ALL
--                 ,  FI_ANA_UNBOUND_BLOCKED_NOTOVERDUE
--                 ,  FI_ANA_UNBOUND_CURRENT_ORDER_AMOUNT
--                 ,  FI_ANA_UNBOUND_BLOCKED_ORDER_AMOUNT
--                 ,  FI_ANA_UNBOUND_DELIVERY_TOBEINVOICED
--                 ,  FI_ANA_UNBOUND_BLOCKED_DELIVERY_TOBEINVOICED
--                 ,  FI_ANA_UNBOUND_BANK_DRAFT
--                 ,  FI_ANA_UNBOUND_SALES
--                 ,  FI_ANA_UNBOUND_PAYMENT
--             From 
--                 TMP_FIANA_MBRS_BACKUP FITMP
--                 Inner Join ADMIN_PARAMS_has_MEMBERS PARAM
--                     On PARAM.MEMBERS_ID_MEMBERS = FITMP.MEMBERS_ID_MEMBERS
--             Where 
--                 FITMP.MEMBERS_ID_MEMBERS Like memberFilter
--         ;

        Drop Temporary Table If Exists TMP_FIANA_MBRS_BACKUP;

        If transactionStarted = 1 Then
            Commit;
            Set transactionStarted  = 0;
        End If;

    End;

End //
Delimiter ;