Delimiter //
/**
    In batchnum Int : num du lot
*/

Drop Procedure if exists LOADER_SUMMARIES_SUMMARIES_UPDATE//

Create Procedure LOADER_SUMMARIES_SUMMARIES_UPDATE (
    In batchnum int
)
Begin

-- 	obtention du membre batch
	Set @lssi_idAdminParams = GET_LOADER_INFO('ID_ADMIN_PARAMS', batchnum);
	Set @lssi_IdMEmber  = (Select Min(ID_MEMBERS) From MEMBERS Where UNIVERS_ID_UNIVERS = @lssi_idAdminParams And MEMBERS_ADMIN_RIGHTS = 1 and MEMBERS_LOGIN like '%batch%' );
	if @lssi_IdMEmber Is Null Then
		(Select Min(ID_MEMBERS) From MEMBERS Where UNIVERS_ID_UNIVERS = @lssi_idAdminParams And MEMBERS_ADMIN_RIGHTS = 1 );
	End if;

	Update SUMMARIES SMS
		Inner Join LOADER_SUMMARIES LDS
			On SMS.ID_SUMMARIES = LDS.ID_SUMMARIES
		Inner Join LOADER_STATUS LST
			On LDS.LOADER_STATUS_ID_LOADER_STATUS = LST.ID_LOADER_STATUS
	Set
		SMS.SUMMARIES_DATE = Coalesce(LDS.SUMMARIES_DATE, now())
		, SMS.SUMMARIES_SUBJECT = Coalesce(LDS.SUMMARIES_SUBJECT, SMS.SUMMARIES_SUBJECT)
		, SMS.SUMMARIES_COMMENTS = Coalesce(LDS.SUMMARIES_COMMENTS, SMS.SUMMARIES_COMMENTS)
		, SMS.SUMMARIES_OUTSTANDING = Coalesce(LDS.SUMMARIES_OUTSTANDING, SMS.SUMMARIES_OUTSTANDING)
		, SMS.SUMMARIES_OVERDUE = Coalesce(LDS.SUMMARIES_OVERDUE, SMS.SUMMARIES_OVERDUE)
		, SMS.SUMMARIES_MEMBER_AUTHOR = @lssi_IdMEmber
	Where
		LST.LOADER_STATUS_BATCH_NUM = batchnum
		And LST.LOADER_STATUS_ERRCOUNT = 0
	;
End //

Delimiter ;
