Delimiter //
/**
 * a faire aprs LOADER_RECLAIMS_NORMALIZE_MAP_RECLAIMS_CREATE_OR_UPDATE
 */
Drop Procedure If Exists LOADER_RECLAIMS_NORMALIZE_MAP_SUMMARIES//
Create Procedure LOADER_RECLAIMS_NORMALIZE_MAP_SUMMARIES(
    In batchnum Int
)
Begin
	Set @admin = GET_LOADER_INFO('ID_ADMIN_PARAMS', batchnum);
	Set @sumarieIdReclaimCreated = (
				Select ID_DF_SUMMARIES 
				from DF_SUMMARIES 
				where ADMIN_ID_ADMIN = @admin 
					and DF_SUMMARIES_CODE = 'RECLAIMS-C'
			)
		;
	Set @sumarieIdReclaimUpdated = (
				Select ID_DF_SUMMARIES 
				from DF_SUMMARIES 
				where ADMIN_ID_ADMIN = @admin 
					and DF_SUMMARIES_CODE = 'RECLA-ST-UPD'
			)
		;

    Update
        LOADER_STATUS STS
        Inner Join LOADER_RECLAIMS LRD
            On LRD.LOADER_STATUS_ID_LOADER_STATUS = STS.ID_LOADER_STATUS
    Set
        LRD.SUMMARIES_ID_SUMMARIES = Case When LRD.NEW_ID_RECLAIMS is not Null Then
										@sumarieIdReclaimCreated
									else
										@sumarieIdReclaimUpdated
									End 
    Where
        STS.LOADER_STATUS_BATCH_NUM = batchnum
		And STS.LOADER_STATUS_ERRCOUNT = 0
		And Coalesce(LRD.NEW_ID_RECLAIMS, LRD.ID_RECLAIMS) Is Not Null
    ;


End//
Delimiter ;