Delimiter //
/**
 * Si la forme juridique abregée est fournie dans le fichier d'importation :
 * - Rechercher la forme juridique abregée de l'environnement administratif (insensible à la casse).
 *   - Si trouve : l’initialiser au contact
 *   - Si ne trouve pas : warning « forme juridique inconnue : [nom agrégé du titre recherché] »
 * 
 * @param int batchnum
 * @ticket #4148
 */ 
Drop Procedure if Exists LOADER_CONTACTS_NORMALIZE_MAP_SHORTFORM_PROVIDED//
 
Create Procedure LOADER_CONTACTS_NORMALIZE_MAP_SHORTFORM_PROVIDED (
    In batchnum int
)
Begin
 
    Set @idAdminParams = GET_LOADER_INFO('ID_ADMIN_PARAMS', batchnum);
    
    UPDATE
        LOADER_CONTACTS LCTC
        INNER JOIN LOADER_STATUS LSTS
            ON LSTS.ID_LOADER_STATUS = LCTC.LOADER_STATUS_ID_LOADER_STATUS
        LEFT JOIN DF_FORMS DFFRM
            ON DFFRM.ADMIN_PARAMS_ID_ADMIN_PARAMS= @idAdminParams
            AND DFFRM.DF_FORMS_SHORT_FORM collate utf8_general_ci = LCTC.DF_FORMS_SHORT_FORM collate utf8_general_ci
    SET
        LCTC.DF_FORMS_ID_DF_FORMS = DFFRM.ID_DF_FORMS
        , LSTS.LOADER_STATUS_WARNCOUNT =
            (CASE WHEN DFFRM.ID_DF_FORMS IS NULL
                THEN LSTS.LOADER_STATUS_WARNCOUNT + 1
                ELSE LSTS.LOADER_STATUS_WARNCOUNT
            END)
        , LSTS.LOADER_STATUS_WARNMSGS =
            (CASE WHEN DFFRM.ID_DF_FORMS IS NULL
                THEN LOADER_CONCAT_ERR(LSTS.LOADER_STATUS_WARNMSGS, CONCAT('forme juridique inconnue : ', LCTC.DF_FORMS_SHORT_FORM) ) 
                ELSE LSTS.LOADER_STATUS_WARNMSGS
            END)

    WHERE 
        LSTS.LOADER_STATUS_BATCH_NUM = batchnum
        AND Coalesce(LCTC.DF_FORMS_SHORT_FORM, '') != ''
    ;
    
End //
Delimiter ;