Delimiter $$
/**
 * compute BUSINESS_FI_ANA_INSERT_NEW_ACCOUNTS
 * les nouveau comptes dans la table FI_ANA pour la mise à jour des données
 * @see BUSINESS_UPDATE_FIANA
 */ 
Drop Procedure if Exists BUSINESS_FI_ANA_INSERT_NEW_ACCOUNTS $$
Create Procedure BUSINESS_FI_ANA_INSERT_NEW_ACCOUNTS(In idaccounts Int unsigned, In identities Int unsigned)
Begin

    if idaccounts Is null Then
        Set @bfina_accMinVal = 0;
        Set @bfina_accMaxVal = 4e7;
    Else
        Set @bfina_accMinVal = idaccounts;
        Set @bfina_accMaxVal = idaccounts;
    End if;

    if identities Is null Then
        Set @bfina_enttMinVal = 0;
        Set @bfina_enttMaxVal = 4e7;
    Else
        Set @bfina_enttMinVal = identities;
        Set @bfina_enttMaxVal = identities;
    End if;

    fi_ana_insert_new_accounts: begin
        -- temporary table for insert accounts without entries
		Drop Temporary table if exists TMP_FIANA_NEW_ACCOUNTS;
		Create Temporary Table TMP_FIANA_NEW_ACCOUNTS AS 
		Select 
			ACC.ID_ACCOUNTS, ENT.ADMIN_ID_ADMIN
		From
			ACCOUNTS ACC
			Inner Join ENTITIES ENT
				On ACC.ENTITIES_ID_ENTITIES = ENT.ID_ENTITIES
			Left Join FI_ANA
				On ACC.ID_ACCOUNTS = FI_ANA.ACCOUNTS_ID_ACCOUNTS
		Where 
            FI_ANA.ACCOUNTS_ID_ACCOUNTS Is Null
            And ACC.ID_ACCOUNTS Between @bfina_accMinVal And @bfina_accMaxVal
            And ACC.ENTITIES_ID_ENTITIES Between @bfina_enttMinVal And @bfina_enttMaxVal
-- 			And ACC.ID_ACCOUNTS like @accFilter
		;

		-- insert accounts without entries
		Insert Into 
			FI_ANA (
				ACCOUNTS_ID_ACCOUNTS
				, ADMIN_PARAMS_ID_ADMIN_PARAMS
				, 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 
			ID_ACCOUNTS, ADMIN_ID_ADMIN
                , 0 FI_ANA_OUTSTANDING
                , 0 FI_ANA_OVERDUE
                , 0 FI_ANA_NOT_OVERDUE
                , 0 FI_ANA_DUE
                , 0 FI_ANA_FROZEN_AMOUNT
                , 0 FI_ANA_BLOCKED_ALL
                , 0 FI_ANA_BLOCKED_NOTOVERDUE
                , 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_AGE_ACCOUNT_PERIOD1
                , 0 FI_ANA_AGE_ACCOUNT_PERIOD2
                , 0 FI_ANA_AGE_ACCOUNT_PERIOD3
                , 0 FI_ANA_AGE_ACCOUNT_PERIOD4
                , 0 FI_ANA_CURRENT_ORDER_AMOUNT
                , 0 FI_ANA_BLOCKED_ORDER_AMOUNT
                , 0 FI_ANA_DELIVERY_TOBEINVOICED
                , 0 FI_ANA_BLOCKED_DELIVERY_TOBEINVOICED
                , 0 FI_ANA_BANK_DRAFT
                , 0 FI_ANA_SALES
                , 0 FI_ANA_PAYMENT
                , 0 FI_ANA_UNBOUND_OUTSTANDING
                , 0 FI_ANA_UNBOUND_OVERDUE
                , 0 FI_ANA_UNBOUND_NOT_OVERDUE
                , 0 FI_ANA_UNBOUND_DUE
                , 0 FI_ANA_UNBOUND_FROZEN_AMOUNT
                , 0 FI_ANA_UNBOUND_BLOCKED_ALL
                , 0 FI_ANA_UNBOUND_BLOCKED_NOTOVERDUE
                , 0 FI_ANA_UNBOUND_CURRENT_ORDER_AMOUNT
                , 0 FI_ANA_UNBOUND_BLOCKED_ORDER_AMOUNT
                , 0 FI_ANA_UNBOUND_DELIVERY_TOBEINVOICED
                , 0 FI_ANA_UNBOUND_BLOCKED_DELIVERY_TOBEINVOICED
                , 0 FI_ANA_UNBOUND_BANK_DRAFT
                , 0 FI_ANA_UNBOUND_SALES
                , 0 FI_ANA_UNBOUND_PAYMENT
                , 0 FI_ANA_AGE_CUSTMMDE_PERIOD1
                , 0 FI_ANA_AGE_CUSTMMDE_PERIOD2
                , 0 FI_ANA_AGE_CUSTMMDE_PERIOD3
                , 0 FI_ANA_AGE_CUSTMMDE_PERIOD4
                , 0 FI_ANA_AGE_CUSTMMDE_DEBIT
                , 0 FI_ANA_AGE_CUSTMMDE_CREDIT
		From TMP_FIANA_NEW_ACCOUNTS;
-- 		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_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 
-- 			ID_ACCOUNTS, 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
-- 			, null FI_ANA_ACD_LASTUPDAT
-- 			, 0 FI_ANA_BPDSO
-- 			, null FI_ANA_BPDSO_LASTUPDAT
--             , 0 FI_ANA_AGE_CUSTMMDE_PERIOD1
--             , 0 FI_ANA_AGE_CUSTMMDE_PERIOD2
--             , 0 FI_ANA_AGE_CUSTMMDE_PERIOD3
--             , 0 FI_ANA_AGE_CUSTMMDE_PERIOD4
--             , 0 FI_ANA_AGE_CUSTMMDE_DEBIT
--             , 0 FI_ANA_AGE_CUSTMMDE_CREDIT
-- 		From TMP_FIANA_NEW_ACCOUNTS;

		Drop Temporary table if exists TMP_FIANA_NEW_ACCOUNTS;
    end;
end $$
Delimiter ;