Delimiter //
/**
 * Author:  armen
 * Created: 25 oct. 2018
 * creation de la table TMP_ENTRIES_has_ACCOUNTS_TO_KEEP
 * populé pars les écriture liés à conserver base sur le filtres de comptes liées
 * la table TMP_ENTRIES_has_ACCOUNTS  doit avoir été créer au préalable
 * => voir procedure BUSINESS_LINKED_ACCOUNTS_CREATES_SELECTED_ENT_HAS_ACC_TBL
 */
Drop Procedure if Exists BUSINESS_LINKED_ACCOUNTS_FIND_ENTRIES_TO_KEEP //
Create Procedure BUSINESS_LINKED_ACCOUNTS_FIND_ENTRIES_TO_KEEP(
            In idaccounts Int unsigned
            , In maxIeration Int 
        )
Begin
    Set @blafetk_maxIeration = Least(Greatest(Coalesce(maxIeration, 5), 3), 15); 
    -- 3 <= @blafetk_maxIeration <= 15
--     Set @blafetk_accoutFilter = coalesce(idaccounts, '%');

    Drop Temporary Table If Exists TMP_ENTRIES_has_ACCOUNTS_TO_KEEP;
    Create Temporary Table TMP_ENTRIES_has_ACCOUNTS_TO_KEEP (
                ID_ENTRIES_has_ACCOUNTS Int Unsigned Not Null
                , ENTRIES_ID_ENTRIES Int Unsigned Not Null
                , ACCOUNTS_ID_ACCOUNTS Int Unsigned Not Null
                , Index IX_TMP_ENTRIES_has_ACCOUNTS_TO_KEEP_0(ID_ENTRIES_has_ACCOUNTS)
                , Index IX_TMP_ENTRIES_has_ACCOUNTS_TO_KEEP_1(ENTRIES_ID_ENTRIES)
                , Index IX_TMP_ENTRIES_has_ACCOUNTS_TO_KEEP_2(ACCOUNTS_ID_ACCOUNTS)
            )
        ;

    -- on a pas besoin de tester ACCOUNTS_ID_ACCOUNTS_0 
    Set @blafetk_it=1;
    Set @blafetk_colCounts= @blafetk_maxIeration + 1;

    inesertDataToKeepLoop: Loop
        if @blafetk_it > @blafetk_colCounts then
            leave inesertDataToKeepLoop;
        End if;
        Set @blafetk_insertQuery = Concat(''
                        , ' Insert Into TMP_ENTRIES_has_ACCOUNTS_TO_KEEP( '
                        , '  ID_ENTRIES_has_ACCOUNTS, ACCOUNTS_ID_ACCOUNTS, ENTRIES_ID_ENTRIES '
                        , ' ) '
                        , ' Select '
                        , '   tar.ID_ENTRIES_has_ACCOUNTS, tar.ACCOUNTS_ID_ACCOUNTS, tar.ENTRIES_ID_ENTRIES '
                        , ' From TMP_ENTRIES_has_ACCOUNTS tar '
                        , '   Inner Join ENTRIES_has_ACCOUNTS eha '
                        , '    On tar.ENTRIES_ID_ENTRIES = eha.ENTRIES_ID_ENTRIES '
                        , '    And tar.ENTRIES_PERMANENT_ATTACHEMENT = 0 ' -- pas util en principe
                        , '    And eha.ENTRIES_PERMANENT_ATTACHEMENT = 1 '
--                         , 'Where Exists( '
                        , ' Where eha.ACCOUNTS_ID_ACCOUNTS In ( '
                        -- test si dans les comptes rattaché on trouve le compte de l'ecriture
--                         , '   Select * '
                        , '   Select src.ACCOUNTS_ID_ACCOUNTS_', @blafetk_it, ' '
                        , '   From TMP_ACC_HAS_ACC src '
                        , '   Where eha.ACCOUNTS_ID_ACCOUNTS = src.ACCOUNTS_ID_ACCOUNTS_', @blafetk_it, ' '
--                         , '  And eha.ENTRIES_PERMANENT_ATTACHEMENT = 1 '
--                         , '   Limit 1 '
                        , ' ) '
                
                )
            ;
--         select @blafetk_insertQuery;
        PREPARE InsertKeepDataStmt FROM @blafetk_insertQuery; 
        execute InsertKeepDataStmt ;

        Deallocate prepare InsertKeepDataStmt;

        set @blafetk_it = @blafetk_it + 1;

    End loop inesertDataToKeepLoop;
end //
Delimiter ;