Delimiter //
/**
 * Author:  armen
 * Created: 26 mai 2016
 * #7326
*/
Drop Procedure if exists LOADER_MATCHINGCODES_RECLAIMS_SUMMARIES_INSERT//
Create Procedure LOADER_MATCHINGCODES_RECLAIMS_SUMMARIES_INSERT (
    In batchnum int
--     , In lastId Int
)
Begin
	Set @idEntities = GET_LOADER_INFO('ID_ENTITIES', batchnum);
	-- obtention du membre batch
	Set @admin = GET_LOADER_INFO('ID_ADMIN_PARAMS', batchnum);
	Set @IdMEmber  = (Select ID_MEMBERS From MEMBERS Where UNIVERS_ID_UNIVERS = @admin And MEMBERS_ADMIN_RIGHTS = 1 and MEMBERS_LOGIN like '%batch%' limit 1);
	if @IdMEmber Is Null Then
		Set @IdMEmber  = (Select ID_MEMBERS From MEMBERS Where UNIVERS_ID_UNIVERS = @admin And MEMBERS_ADMIN_RIGHTS = 1 limit 1);
	End if;

	Insert Into SUMMARIES (
		ACCOUNTS_ID_ACCOUNTS, ENTRIES_ID_ENTRIES
		, DF_SUMMARIES_ID_DF_SUMMR, DF_PRIORITY_ID_DF_PRIORITY
		, SUMMARIES_DATE, SUMMARIES_MEMBER_AUTHOR, SUMMARIES_OUTSTANDING
		, RECLAIMS_ID_RECLAIMS
	)  
	Select 
		LM.ACCOUNTS_ID_ACCOUNTS, src.ENTRIES_ID_ENTRIES
		, DFSM.ID_DF_SUMMARIES, DFSM.DF_PRIORITY_ID_DF_PRIORITY
		, now(), @IdMEmber, src.RECLAIMS_DETAILS_AMOUNT
		, src.RECLAIMS_ID_RECLAIMS
	From  
		LOADER_MATCHINGCODES_NEW_RECLAIMS_DETAILS src
		Inner Join LOADER_STATUS lst  
			On src.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS  
		Inner Join LOADER_MATCHINGCODES LM
			on lst.ID_LOADER_STATUS = LM.LOADER_STATUS_ID_LOADER_STATUS
		Inner Join DF_SUMMARIES DFSM
			On src.DF_SUMMARIES_ID_DF_SUMMR = DFSM.ID_DF_SUMMARIES
		Inner Join ENTRIES ENT
			On src.ENTRIES_ID_ENTRIES = ENT.ID_ENTRIES
		Inner Join DF_MATCHINGCODE_STATE DMS
			On ENT.DF_MATCH_STAT_ID_DF_MATCH_STAT = DMS.ID_DF_MATCHINGCODE_STATE
	Where 
		lst.LOADER_STATUS_ERRCOUNT = 0  
		And DMS.DF_MATCHINGCODE_STATE_CLOSED <> 0
--             And lst.LOADER_STATUS_BATCH_NUM =  batchnum
	;
End //

Delimiter ;
