Delimiter //
/*
** Normalization des membres
*/

Drop Procedure if exists LOADER_MEMBERS_NORMALIZE//

Create Procedure LOADER_MEMBERS_NORMALIZE (
    In batchnum int
)
MAIN_MEMBERS_NORMALIZE: Begin
	
	if batchnum = 0 Then
		call LOADER_MEMBERS_NORMALIZE_MAP_LOGIN_UNIQUE();
        -- il faut purger la table LOADER_ACCOUNTS_REPLICATION;
        -- car la maj des contacts peut créer une liaison entres les contacts du membre 
        -- et les comptes du précédant import
        call LOADER_CREATE_TABLE_ACCOUNTS_REPLICATION();
        call LOADER_CREATE_TABLE_CONTACTS_ACCOUNTS();
        call LOADER_CREATE_TABLE_ACCOUNTS();
	End if;

	call LOADER_MEMBERS_NORMALIZE_MAP_MEMBER_TYPE(batchnum);
	call LOADER_MEMBERS_NORMALIZE_MAP_ADMIN_RIGHTS(batchnum);

	call LOADER_MEMBERS_NORMALIZE_CONTACT_REF_NOT_NULL(batchnum);

	call LOADER_MEMBERS_NORMALIZE_CONTACT_REF_IS_UNIQUE(batchnum);

-- 	call LOADER_MEMBERS_NORMALIZE_CHECK_NOT_ACCOUNT_CONTACT(batchnum);
	call LOADER_MEMBERS_NORMALIZE_CHECK_NOT_MEMBER_CONTACT(batchnum);

	call LOADER_MEMBERS_NORMALIZE_MAP_CREATE_OR_UPDATE(batchnum);

	call LOADER_MEMBERS_NORMALIZE_MAP_LOGIN_USED(batchnum);
	


End //

Delimiter ;