SET NOCOUNT ON
BEGIN TRY  
	Begin TRAN
    /* sporzadzenie listy dokumentow*/
    declare @itemid uniqueidentifier ='c6af5494-dd3e-eb11-a98b-060c7c3ef0b7'
    /*Usuniecie wszystkich tempow*/
    delete from document.documentline where status=-1 and itemid = @itemid

    if exists(select 1 from document.documentline where iscorrectiveline =1 and itemid = @itemid and status>-1)
    Begin
			select * from document.documentline where iscorrectiveline =1 and itemid = @itemid and status>-1;
			THROW 50101 ,'NA tym indeksie sa korekty, Korekty nie sa obsługiwane',  1;
    END
	update  document.documentline set status = 0 ,itemquantityleft= itemquantity, itemValueLeft = itemValue ,sysitemvalueleft =sysitemvalue 
	where itemid=@itemid and status>0

	declare @documentlineid uniqueidentifier
	declare @sign int
	declare @data TABLE(
		row int,
		documentlineid uniqueidentifier,
        sign int
        )
	insert INTO @data(row,documentlineid,sign) 
	select row_number()  over (ORDER BY 	
		d.documentdate,
		d.outcomeShiftid, --chodzi o posortowanie mm- zeby byly pierwsze one maja null
		dl.sign desc ,dl.adddate asc),  dl.documentlineid, dl.sign
	from document.documentline dl inner join document.document d on d.documentid =dl.documentid
	where itemid = @itemid and dl.status=0
	order by 1

	declare @current_row int = 1 
	declare @total_rows int = (select count(*) from @data)
	WHILE(@current_row <= @total_rows)
	BEGIN
		 select  @documentlineid =documentlineid ,@sign =sign from @data where row =@current_row
		if @sign =-1
			exec [erp].[documentlineOUT_Commit] @username='sys',@documentLineId=@documentLineId
		else 
			exec [erp].[documentlineIN_Commit] @username='sys',@documentLineId=@documentLineId
		
		select @current_row +=1
	end
	
		select dl.documentlineid,dl.sign,*
			from document.documentline dl inner join document.document d on d.documentid =dl.documentid
		where itemid =@itemid and dl.status>-1
		order by 
			d.documentdate,
			d.outcomeShiftid, --chodiz o posortowanie mm- zeby byly pierwsze one maja null
			dl.sign desc ,dl.adddate asc
	commit TRAN;
END TRY  
BEGIN CATCH  


		select dl.documentlineid,dl.sign,d.documentfullnr,dl.itemquantity,dl.itemquantityleft,dl.sysItemValueLeft,dl.itemvalueleft,dl.sourceDocumentLineId
			from document.documentline dl inner join document.document d on d.documentid =dl.documentid
		where dl.itemid =@itemid  and dl.status>-1
		order by 
			d.documentdate,
			d.outcomeShiftid, --chodiz o posortowanie mm- zeby byly pierwsze one maja null
			dl.sign desc ,dl.adddate asc;

 THROW;

        ROLLBACK TRAN;

END CATCH
 



 /*
		select dl.documentlineid,dl.sign,d.documentfullnr,dl.itemquantity,dl.itemquantityleft
			from document.documentline dl inner join document.document d on d.documentid =dl.documentid
		where dl.itemid ='7deccbcd-200b-475e-8a52-5cfd6a6b7ea8'  and dl.status>-1
		order by 
			d.documentdate,
			d.outcomeShiftid, --chodiz o posortowanie mm- zeby byly pierwsze one maja null
			dl.sign desc ,dl.adddate asc;
			*/
		--	select * from document.documentline where documentlineid in ('8EFA0FBE-6797-EC11-930B-002590D9752E','AB15B6ED-204E-49C9-A058-2E71750EF254')

/*
srs/416 > check 44 > seria 0410Q059113
6909119b-6272-e911-a984-060c7c3ef0b7 mamy w 44 -400 na 	0410Q059113
select * from document.documentline where documentid = 'f1ad1771-0a54-ec11-82e7-0690a1809de2'
delete from document.documentLine where documentlineid ='8C2E7D3F-1454-EC11-930B-002590D9752E'

srs/416 > check 44 >79972496-8572-e911-a984-060c7c3ef0b7  > 020079695
Są korekty

	
srs/416 > check 44 > c6af5494-dd3e-eb11-a98b-060c7c3ef0b7 >	 5654114
declare @documentlineid uniqueidentifier ='DDE942F8-7F53-EC11-930B-002590D9752E'
delete from document.documentLine where documentlineid =@documentlineid
--idealny przypadek, malo dokuemntow

*/

--select * from document.documentline where documentid = '72592d58-5053-ec11-82e7-0690a1809de2'