Delimiter //
/**
 * If the account ref exists, give its id to the contacts
 * Else, notify an error
 * 
 * @author Jean-François
 * @ticket #3926
 */ 
Drop Procedure If Exists LOADER_CONTACTS_NORMALIZE_MAP_ACCOUNT_REF_PROVIDED//
Create Procedure LOADER_CONTACTS_NORMALIZE_MAP_ACCOUNT_REF_PROVIDED(
    In batchnum Int
)
Begin
    set @entitie = GET_LOADER_INFO('ID_ENTITIES', batchnum);
    Set @loader_type = GET_LOADER_INFO('LOADER_TYPE', batchnum);
--     If  @loader_type = 'CONTACTS' Then    

--         Update
--             LOADER_CONTACTS C
--             Inner Join LOADER_STATUS S
--                 On S.ID_LOADER_STATUS = C.LOADER_STATUS_ID_LOADER_STATUS
--             Left Join ACCOUNTS A
--                 On A.ACCOUNTS_REF = C.ACCOUNTS_REF
--                 And A.ENTITIES_ID_ENTITIES = @entitie
--         Set
--             C.ACCOUNTS_ID_ACCOUNTS = A.ID_ACCOUNTS
--             , S.LOADER_STATUS_ERRCOUNT = 
--                 (CASE WHEN A.ID_ACCOUNTS IS NULL
--                     THEN S.LOADER_STATUS_ERRCOUNT + 1
--                     ELSE S.LOADER_STATUS_ERRCOUNT
--                 END)
--             , S.LOADER_STATUS_ERRMSGS = 
--                 (CASE WHEN A.ID_ACCOUNTS IS NULL
--                     THEN LOADER_CONCAT_ERR(S.LOADER_STATUS_ERRMSGS, Concat('Référence de compte inexistante :', C.ACCOUNTS_REF ) )
--                     ELSE S.LOADER_STATUS_ERRMSGS
--                 END)
--         Where
--             Coalesce(C.ACCOUNTS_REF, '') != ''
--             And S.LOADER_STATUS_BATCH_NUM = batchnum
--         ;
        -- preparation si on doit rejouter le batch
        Update
            LOADER_CONTACTS C
            Inner Join LOADER_STATUS S
                On S.ID_LOADER_STATUS = C.LOADER_STATUS_ID_LOADER_STATUS
        Set 
            C.ACCOUNTS_ID_ACCOUNTS = Null
        Where
            S.LOADER_STATUS_BATCH_NUM = batchnum
        ;
        
        -- maj id contact
        Update
            LOADER_CONTACTS C
            Inner Join LOADER_STATUS S
                On S.ID_LOADER_STATUS = C.LOADER_STATUS_ID_LOADER_STATUS

            Inner Join ACCOUNTS A
                On A.ACCOUNTS_REF = C.ACCOUNTS_REF
                And A.ENTITIES_ID_ENTITIES = @entitie
        Set
            C.ACCOUNTS_ID_ACCOUNTS = A.ID_ACCOUNTS
        Where
            S.LOADER_STATUS_BATCH_NUM = batchnum
        ;
        -- maj des erreurs
        Update
            LOADER_CONTACTS C
            Inner Join LOADER_STATUS S
                On S.ID_LOADER_STATUS = C.LOADER_STATUS_ID_LOADER_STATUS
        Set 
            S.LOADER_STATUS_ERRCOUNT = S.LOADER_STATUS_ERRCOUNT + 1
                
            , S.LOADER_STATUS_ERRMSGS = LOADER_CONCAT_ERR(S.LOADER_STATUS_ERRMSGS, Concat('Référence de compte inexistante :', C.ACCOUNTS_REF )) 
                
        Where
            S.LOADER_STATUS_BATCH_NUM = batchnum
            And Coalesce(C.ACCOUNTS_REF, '') != ''
            And C.ACCOUNTS_ID_ACCOUNTS Is Null
        ;

--     End If;

End//
Delimiter ;