Delimiter //
/*
* Normalization des accredidation membres
*/
Drop Procedure if exists LOADER_ATTACHEDMEMBERS_NORMALIZE//

Create Procedure LOADER_ATTACHEDMEMBERS_NORMALIZE (
    In batchnum int
)
Begin
    MAIN_ATTACHEDMEMBERS_NORMALIZE: Begin
        Set @lamn_batchnum = batchnum ;

        if @lamn_batchnum  = 0 Then

            -- creation si nécessaire de la table TMP_ACCOUNTS_has_MEMBERS_SAVE
            -- creation / purge de la table TMP_ACCOUNTS_has_MEMBERS_PRCSS
            call LOADER_CREATE_TABLE_TMP_ACC_HAS_MBR_SAVE();

            -- purge de la table au début de l'import
            -- dans le lot 0, il n'y a pas de transaction
            truncate table TMP_ACCOUNTS_has_MEMBERS;
            leave MAIN_ATTACHEDMEMBERS_NORMALIZE;
        Else
            -- on ne peux pas faire de truncate dans les lot > 0 en raison de du commit implicite
            Delete TMP_ACCOUNTS_has_MEMBERS From TMP_ACCOUNTS_has_MEMBERS ;
        End if;

        call LOADER_ATTACHEDMEMBERS_NORMALIZE_CHECK_LOGIN_OR_CTC_REF_PROVIDED(@lamn_batchnum);
        call LOADER_ATTACHEDMEMBERS_NORMALIZE_MAP_MEMBERS_WITH_LOGIN_PROVIDED(@lamn_batchnum);
        call LOADER_ATTACHEDMEMBERS_NORMALIZE_MAP_MBRS_WITH_CTCREF_PROVIDED(@lamn_batchnum);

        call LOADER_ATTACHEDMEMBERS_NORMALIZE_CHECK_MEMBERS_EXISTS(@lamn_batchnum);

        call LOADER_ATTACHEDMEMBERS_NORMALIZE_MAP_PROFILES_PROVIDED(@lamn_batchnum);

        call LOADER_ATTACHEDMEMBERS_NORMALIZE_MAP_ACCOUNTS(@lamn_batchnum);

        call LOADER_ATTACHEDMEMBERS_NORMALIZE_MAP_ATTCHDMBR_REF_NT_PRVD(@lamn_batchnum);

        call LOADER_ATTACHEDMEMBERS_NORMALIZE_MAP_MEMBERS_REFDATA_PROVIDED(@lamn_batchnum, 1, null);
        call LOADER_ATTACHEDMEMBERS_NORMALIZE_MAP_MEMBERS_REFDATA_PROVIDED(@lamn_batchnum, 2, null);
        call LOADER_ATTACHEDMEMBERS_NORMALIZE_MAP_MEMBERS_REFDATA_PROVIDED(@lamn_batchnum, 3, null);
        call LOADER_ATTACHEDMEMBERS_NORMALIZE_MAP_MEMBERS_REFDATA_PROVIDED(@lamn_batchnum, 4, null);
        call LOADER_ATTACHEDMEMBERS_NORMALIZE_MAP_MEMBERS_REFDATA_PROVIDED(@lamn_batchnum, 5, null);

--         call LOADER_TMP_ACCOUNTS_has_MEMBERS_INSERT(@lamn_batchnum);
--         call BUSINESS_ATTACHED_MEMBERS_COMPUTE_PREPARED();

    End;
End //
Delimiter ;

