Delimiter //
/*
** Normalization des contacts
*/

Drop Procedure if exists LOADER_CONTACTS_NORMALIZE//

Create Procedure LOADER_CONTACTS_NORMALIZE (
    In batchnum int
)
MAIN_CONTACTS_NORMALIZE: Begin

    -- Mandatory checks
	Set @loader_type = GET_LOADER_INFO('LOADER_TYPE', batchnum);

    -- initialise dependents tables on init batch (0)
    if batchnum = 0 Then
        call LOADER_CREATE_TABLE_CONTACTS_ACCOUNTS;
        call LOADER_CREATE_TABLE_CONTACTS_EMAILS;
        call LOADER_CREATE_TABLE_CONTACTS_PHONES;

        if @loader_type <> 'MEMBERS' Then
            -- pour éviter qu'un précédant import de membre lie les contacts aux membres
            call LOADER_CREATE_TABLE_MEMBERS();
        End If;

        leave MAIN_CONTACTS_NORMALIZE;
    End if;

	if @loader_type <> 'MEMBERS' Then
		Call LOADER_CONTACTS_NORMALIZE_ACCOUNT_REF_NOT_NULL(batchnum);
		Call LOADER_CONTACTS_NORMALIZE_CHECK_NOT_ACCOUNT_CONTACTS(batchnum);
	End If;

    Call LOADER_CONTACTS_NORMALIZE_CONTACTS_REF_NOT_NULL(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_CONTACT_REF_IS_UNIQUE(batchnum);

    -- Other checks
    Call LOADER_CONTACTS_NORMALIZE_CONTACTS_CHECK_EMAIL_VALID(batchnum);
    -- #5518 ne plus supprimer les contacts si CONTACTS_INDIVIDUAL_LAST_NAME est vide
--     Call LOADER_CONTACTS_NORMALIZE_CONTACTS_CHECK_LAST_NAME_NOT_NULL(batchnum);
    -- #6704 ne plus vérifier que le nom est en majusule, le transformer
--     Call LOADER_CONTACTS_NORMALIZE_CONTACTS_CHECK_LAST_NAME_UPPERCASE(batchnum);

    -- Mandatory linking
    Call LOADER_CONTACTS_NORMALIZE_MAP_ACCOUNT_REF_PROVIDED(batchnum);

    -- Create or update ?
    Call LOADER_CONTACTS_NORMALIZE_MAP_CONTACT_CREATE_OR_UPDATE(batchnum);

    -- Mapping and linking
    Call LOADER_CONTACTS_NORMALIZE_MAP_ADDRESS_CHECK_STATE_SET(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_LAST_NAME_PROVIDED(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_COUNTRY_PROVIDED(batchnum);
--     Call LOADER_CONTACTS_NORMALIZE_MAP_COUNTRY_NOT_PROVIDED(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_EMAIL_PROVIDED(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_FUNCTION_NOT_PROVIDED(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_FUNCTION_PROVIDED(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_LANGUAGE_NOT_PROVIDED(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_LANGUAGE_PROVIDED(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_LAST_UPDATE_SET(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_MAIN_PROVIDED(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_PHONE_PROVIDED(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_SAND_STATE_SET(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_TITLE_PROVIDED(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_SHORTFORM_PROVIDED(batchnum);
    Call LOADER_CONTACTS_NORMALIZE_MAP_TRASHED_SET(batchnum);

    -- Last actions
    Set @max_batch = GET_LOADER_INFO('MAX_BATCH', batchnum);

    if @loader_type = 'ACCOUNTS' Then
        Call LOADER_CONTACTS_NORMALIZE_MAP_MAIN_FROMACCOUNT_NOTPROVIDED(batchnum);
    elseif batchnum = @max_batch and @loader_type = 'CONTACTS'  Then
        Call LOADER_CONTACTS_NORMALIZE_MAP_MAIN_FROMCONTACT_NOTPROVIDED();
    End if;

End //

Delimiter ;