if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[BSO_OstNaDate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
਍搀爀漀瀀 瀀爀漀挀攀搀甀爀攀 嬀搀戀漀崀⸀嬀䈀匀伀开伀猀琀一愀䐀愀琀攀崀ഀഀ
GO
਍ഀഀ
SET QUOTED_IDENTIFIER ON 
਍䜀伀ഀഀ
SET ANSI_NULLS ON 
਍䜀伀ഀഀ

਍ഀഀ
CREATE PROCEDURE dbo.BSO_OstNaDate
਍䀀䐀愀琀攀 搀愀琀攀琀椀洀攀Ⰰഀഀ
@anal int
਍䄀匀ഀഀ

਍䐀䔀䌀䰀䄀刀䔀 䀀搀愀琀 䐀䄀吀䔀琀椀洀攀㬀ഀഀ
set @dat=@date;
਍ഀഀ
delete dbo.bso;
਍ഀഀ
declare @BSO_spis table(kodBSO_S int identity(1,1), dataDocm_S datetime, KodSklM_S int, pntm_S int, 
਍ऀऀऀ挀攀渀漀洀开匀 搀攀挀椀洀愀氀⠀㌀　Ⰰ㐀⤀Ⰰ 匀攀爀椀愀开匀 瘀愀爀挀栀愀爀⠀㈀⤀Ⰰ 一漀洀攀爀一开匀 椀渀琀Ⰰ 一漀洀攀爀䬀开匀 椀渀琀Ⰰ 瀀爀 椀渀琀Ⰰ匀挀栀攀琀开猀 瘀愀爀挀栀愀爀⠀㄀　⤀Ⰰ 椀搀愀渀愀氀 椀渀琀Ⰰ 搀愀琀攀嘀嘀 搀愀琀攀琀椀洀攀⤀ഀഀ

਍椀渀猀攀爀琀 椀渀琀漀 䀀䈀匀伀开猀瀀椀猀⠀搀愀琀愀䐀漀挀洀开匀Ⰰ 䬀漀搀匀欀氀䴀开匀Ⰰ 瀀渀琀洀开匀Ⰰ 挀攀渀漀洀开匀Ⰰ 匀攀爀椀愀开匀Ⰰ 一漀洀攀爀一开匀Ⰰ 一漀洀攀爀䬀开匀Ⰰ 瀀爀Ⰰ匀挀栀攀琀开猀Ⰰ 椀搀愀渀愀氀Ⰰ 搀愀琀攀嘀嘀⤀ ഀഀ
		select s.dateDocm, KodSklM , pntm , cenom , s.Seria, NomerN, NomerK, 0 as pr , CASE WHEN pr_zab = 0 OR  pr_zab IS NULL THEN (CONVERT(varchar(2), KodSchetKT) + '.' + CONVERT(varchar(3), KodSubSchetKT)) ELSE '00' + CONVERT(varchar(1), KodSchetKT) + '.' + CONVERT(varchar(3), 
਍                      䌀伀䄀䰀䔀匀䌀䔀 ⠀䬀漀搀匀甀戀匀挀栀攀琀䬀吀Ⰰ ✀✀⤀⤀ 䔀一䐀Ⰰ 䌀伀䄀䰀䔀匀䌀䔀 ⠀䤀搀猀挀栀攀琀猀甀戀猀栀攀琀䬀琀䴀Ⰰ　⤀Ⰰ 搀愀琀攀嘀嘀 搀愀琀攀琀椀洀攀  ⴀⴀ ⠀䌀伀一嘀䔀刀吀⠀瘀愀爀挀栀愀爀⠀㈀⤀Ⰰ䬀漀搀匀挀栀攀琀䬀吀⤀ ⬀✀⸀✀⬀䌀伀一嘀䔀刀吀⠀瘀愀爀挀栀愀爀⠀㌀⤀Ⰰ䬀漀搀匀甀戀匀挀栀攀琀䬀吀⤀⤀ ഀഀ
		from stringmaloc s, docmaloc d, tbgroper g 
਍ऀऀ眀栀攀爀攀 嘀椀搀搀漀挀䴀㴀㈀ 愀渀搀 猀⸀椀搀搀漀挀洀㴀搀⸀椀搀搀漀挀洀 愀渀搀 猀⸀搀愀琀攀搀漀挀洀㰀㴀䀀搀愀琀 愀渀搀 猀⸀猀攀爀椀愀 椀猀 渀漀琀 渀甀氀氀 愀渀搀 搀⸀椀搀最爀漀瀀攀爀洀㴀最⸀椀搀最爀漀瀀攀爀 愀渀搀  䌀伀䄀䰀䔀匀䌀䔀 ⠀䤀搀猀挀栀攀琀猀甀戀猀栀攀琀䬀琀䴀Ⰰ　⤀㴀䀀愀渀愀氀ഀഀ

਍椀渀猀攀爀琀 椀渀琀漀 搀戀漀⸀戀猀漀 猀攀氀攀挀琀 猀⸀搀愀琀攀䐀漀挀洀Ⰰ 䬀漀搀匀欀氀䴀 Ⰰ 瀀渀琀洀 Ⰰ 挀攀渀漀洀 Ⰰ 猀⸀匀攀爀椀愀Ⰰ 一漀洀攀爀一Ⰰ 一漀洀攀爀䬀Ⰰ䌀䄀匀䔀 圀䠀䔀一 瀀爀开稀愀戀 㴀 　 伀刀 瀀爀开稀愀戀 䤀匀 一唀䰀䰀 吀䠀䔀一 ⠀䌀伀一嘀䔀刀吀⠀瘀愀爀挀栀愀爀⠀㈀⤀Ⰰ 䬀漀搀匀挀栀攀琀䐀吀⤀ ⬀ ✀⸀✀ ⬀ 䌀伀一嘀䔀刀吀⠀瘀愀爀挀栀愀爀⠀㌀⤀Ⰰ 䬀漀搀匀甀戀匀挀栀攀琀䐀吀⤀⤀ 䔀䰀匀䔀 ✀　　✀ ⬀ 䌀伀一嘀䔀刀吀⠀瘀愀爀挀栀愀爀⠀㄀⤀Ⰰ 䬀漀搀匀挀栀攀琀䐀吀⤀ ⬀ ✀⸀✀ ⬀ 䌀伀一嘀䔀刀吀⠀瘀愀爀挀栀愀爀⠀㌀⤀Ⰰ ഀഀ
                      COALESCE (KodSubSchetDT, '')) END, COALESCE (IdschetsubshetDtM,0) , dateVV datetime-- (CONVERT(varchar(2),KodSchetDT) +'.'+CONVERT(varchar(3),KodSubSchetDT) )
਍ऀऀ    昀爀漀洀 猀琀爀椀渀最洀愀氀漀挀 猀Ⰰ 搀漀挀洀愀氀漀挀 搀 Ⰰ 琀戀最爀漀瀀攀爀 最 ഀഀ
		    where ViddocM=1 and s.iddocm=d.iddocm and s.datedocm<=@dat and s.seria is not null  and d.idgroperm=g.idgroper  and  COALESCE (IdschetsubshetDtM,0)=@anal
਍ഀഀ
declare @kodBSO_S int, @dataDocm_S datetime, @KodSklM_S int , @pntm_S int , @cenom_S decimal(30,4), @Seria_S varchar(2), @NomerN_S int, @NomerK_S int ,@KK int,@Schet_s varchar(10),@idanal int , @dateVV datetime;
਍⼀⨀搀攀挀氀愀爀攀 䀀䈀匀伀开猀瀀椀猀 琀愀戀氀攀⠀欀漀搀䈀匀伀开匀 椀渀琀 椀搀攀渀琀椀琀礀⠀㄀Ⰰ㄀⤀Ⰰ 搀愀琀愀䐀漀挀洀开匀 搀愀琀攀琀椀洀攀Ⰰ 䬀漀搀匀欀氀䴀开匀 椀渀琀Ⰰ 瀀渀琀洀开匀 椀渀琀Ⰰ ഀഀ
			cenom_S decimal(30,4), Seria_S varchar(2), NomerN_S int, NomerK_S int, pr int)*/
਍䐀䔀䌀䰀䄀刀䔀 䌀刀匀 䌀唀刀匀伀刀 䰀伀䌀䄀䰀 䘀伀刀 猀攀氀攀挀琀 欀漀搀䈀匀伀开匀Ⰰ 搀愀琀愀䐀漀挀洀开匀Ⰰ 䬀漀搀匀欀氀䴀开匀 Ⰰ 瀀渀琀洀开匀 Ⰰ 挀攀渀漀洀开匀 Ⰰ 匀攀爀椀愀开匀Ⰰ 一漀洀攀爀一开匀Ⰰ 一漀洀攀爀䬀开猀Ⰰ匀挀栀攀琀开猀Ⰰ 椀搀愀渀愀氀Ⰰ 搀愀琀攀嘀嘀  昀爀漀洀 䀀䈀匀伀开猀瀀椀猀 漀爀搀攀爀 戀礀  䬀漀搀匀欀氀䴀开匀 Ⰰ 瀀渀琀洀开匀 Ⰰ 挀攀渀漀洀开匀 Ⰰ 匀攀爀椀愀开匀Ⰰ 一漀洀攀爀一开匀Ⰰ 一漀洀攀爀䬀开匀ഀഀ
  OPEN CRS
਍  䘀䔀吀䌀䠀 一䔀堀吀 䘀刀伀䴀 䌀刀匀 䤀一吀伀 䀀欀漀搀䈀匀伀开匀Ⰰ 䀀搀愀琀愀䐀漀挀洀开匀Ⰰ 䀀䬀漀搀匀欀氀䴀开匀 Ⰰ 䀀瀀渀琀洀开匀 Ⰰ 䀀挀攀渀漀洀开匀 Ⰰ 䀀匀攀爀椀愀开匀Ⰰ 䀀一漀洀攀爀一开匀Ⰰ 䀀一漀洀攀爀䬀开匀Ⰰ䀀匀挀栀攀琀开猀Ⰰ 䀀椀搀愀渀愀氀Ⰰ 䀀搀愀琀攀嘀嘀 ഀഀ
  WHILE @@FETCH_STATUS = 0
਍   戀攀最椀渀ഀഀ
	if (select kodBSO from bso where @Schet_s=Schet and @KodSklM_S=KodSklM and @pntm_S=pntm and @cenom_S=cenom and @Seria_S=Seria and NomerN=@NomerN_S and NomerK>@NomerK_S)>=1
਍ऀऀ戀攀最椀渀ഀഀ
		update bso set NomerN=@NomerK_S+1 where kodBSO=(select kodBSO from bso where @Schet_s=Schet and @KodSklM_S=KodSklM and @pntm_S=pntm and @cenom_S=cenom and @Seria_S=Seria and NomerN=@NomerN_S and NomerK>@NomerK_S)  
਍ऀऀ搀攀氀攀琀攀 昀爀漀洀 䀀䈀匀伀开猀瀀椀猀 眀栀攀爀攀 欀漀搀䈀匀伀开匀㴀 䀀欀漀搀䈀匀伀开匀ഀഀ
		end
਍ऀ攀氀猀攀ഀഀ
	if (select kodBSO from bso where @Schet_s=Schet and @KodSklM_S=KodSklM and @pntm_S=pntm and @cenom_S=cenom and @Seria_S=Seria and NomerN<@NomerN_S and NomerK>@NomerK_S)>=1
਍ऀऀ戀攀最椀渀ഀഀ
		set @KK=(select NomerK from bso where @KodSklM_S=KodSklM and @pntm_S=pntm and @cenom_S=cenom and @Seria_S=Seria and NomerN<@NomerN_S and NomerK>@NomerK_S)
਍ऀऀ椀渀猀攀爀琀 椀渀琀漀 戀猀漀 ⠀搀愀琀愀䐀漀挀洀Ⰰ 䬀漀搀匀欀氀䴀Ⰰ 瀀渀琀洀Ⰰ挀攀渀漀洀Ⰰ 匀攀爀椀愀Ⰰ 一漀洀攀爀一Ⰰ 一漀洀攀爀䬀Ⰰ匀挀栀攀琀⤀ 瘀愀氀甀攀猀 ⠀䀀搀愀琀愀䐀漀挀洀开匀Ⰰ 䀀䬀漀搀匀欀氀䴀开匀 Ⰰ 䀀瀀渀琀洀开匀 Ⰰ 䀀挀攀渀漀洀开匀 Ⰰ 䀀匀攀爀椀愀开匀Ⰰ 䀀一漀洀攀爀䬀开匀⬀㄀Ⰰ 䀀䬀䬀Ⰰ䀀匀挀栀攀琀开猀⤀ഀഀ
		update bso set NomerK=@NomerN_S-1 where kodBSO=(select kodBSO from bso where @KodSklM_S=KodSklM and @pntm_S=pntm and @cenom_S=cenom and @Seria_S=Seria and NomerN<@NomerN_S and NomerK>@NomerK_S)  
਍ऀऀഀഀ
		delete from @BSO_spis where kodBSO_S= @kodBSO_S
਍ऀऀ攀渀搀ഀഀ
	else 
਍ऀ椀昀 ⠀猀攀氀攀挀琀 欀漀搀䈀匀伀 昀爀漀洀 戀猀漀 眀栀攀爀攀 䀀䬀漀搀匀欀氀䴀开匀㴀䬀漀搀匀欀氀䴀 愀渀搀 䀀瀀渀琀洀开匀㴀瀀渀琀洀 愀渀搀 䀀挀攀渀漀洀开匀㴀挀攀渀漀洀 愀渀搀 䀀匀攀爀椀愀开匀㴀匀攀爀椀愀 愀渀搀 一漀洀攀爀一㰀䀀一漀洀攀爀一开匀 愀渀搀 一漀洀攀爀䬀㴀䀀一漀洀攀爀䬀开匀⤀㸀㴀㄀ഀഀ
		begin
਍ऀऀ甀瀀搀愀琀攀 戀猀漀 猀攀琀 一漀洀攀爀䬀㴀䀀一漀洀攀爀一开匀ⴀ㄀ 眀栀攀爀攀 欀漀搀䈀匀伀㴀⠀猀攀氀攀挀琀 欀漀搀䈀匀伀 昀爀漀洀 戀猀漀 眀栀攀爀攀 䀀䬀漀搀匀欀氀䴀开匀㴀䬀漀搀匀欀氀䴀 愀渀搀 䀀瀀渀琀洀开匀㴀瀀渀琀洀 愀渀搀 䀀挀攀渀漀洀开匀㴀挀攀渀漀洀 愀渀搀 䀀匀攀爀椀愀开匀㴀匀攀爀椀愀 愀渀搀 一漀洀攀爀一㰀䀀一漀洀攀爀一开匀 愀渀搀 一漀洀攀爀䬀㴀䀀一漀洀攀爀䬀开匀⤀  ഀഀ
	
਍ऀऀ搀攀氀攀琀攀 昀爀漀洀 䀀䈀匀伀开猀瀀椀猀 眀栀攀爀攀 欀漀搀䈀匀伀开匀㴀 䀀欀漀搀䈀匀伀开匀ഀഀ
		end
਍ऀ攀氀猀攀 ഀഀ
	if (select kodBSO from bso where @KodSklM_S=KodSklM and @pntm_S=pntm and @cenom_S=cenom and @Seria_S=Seria and NomerN=@NomerN_S and NomerK=@NomerK_S)>=1
਍ऀऀ戀攀最椀渀ഀഀ
		delete from bso where kodBSO=(select kodBSO from bso where @KodSklM_S=KodSklM and @pntm_S=pntm and @cenom_S=cenom and @Seria_S=Seria and NomerN=@NomerN_S and NomerK=@NomerK_S)  
਍ऀऀ搀攀氀攀琀攀 昀爀漀洀 䀀䈀匀伀开猀瀀椀猀 眀栀攀爀攀 欀漀搀䈀匀伀开匀㴀 䀀欀漀搀䈀匀伀开匀ഀഀ
		end 
਍     䘀䔀吀䌀䠀 一䔀堀吀 䘀刀伀䴀 䌀刀匀 䤀一吀伀 䀀欀漀搀䈀匀伀开匀Ⰰ 䀀搀愀琀愀䐀漀挀洀开匀Ⰰ 䀀䬀漀搀匀欀氀䴀开匀Ⰰ 䀀瀀渀琀洀开匀Ⰰ 䀀挀攀渀漀洀开匀Ⰰ 䀀匀攀爀椀愀开匀Ⰰ 䀀一漀洀攀爀一开匀Ⰰ 䀀一漀洀攀爀䬀开匀Ⰰ䀀匀挀栀攀琀开猀Ⰰ 䀀椀搀愀渀愀氀Ⰰ 䀀搀愀琀攀嘀嘀 ഀഀ
   end
਍䌀䰀伀匀䔀 䌀刀匀ഀഀ

਍ⴀⴀ猀攀氀攀挀琀 ⨀ 昀爀漀洀  戀猀漀ⴀⴀ䀀䈀匀伀开猀瀀椀猀ഀഀ
GO
਍匀䔀吀 儀唀伀吀䔀䐀开䤀䐀䔀一吀䤀䘀䤀䔀刀 伀䘀䘀 ഀഀ
GO
਍匀䔀吀 䄀一匀䤀开一唀䰀䰀匀 伀一 ഀഀ
GO
਍ഀഀ
