alter table nastroiki add  pr_rasch_itog int
਍最漀ഀഀ
update nastroiki set pr_rasch_itog =0
਍最漀ഀഀ

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

਍䌀刀䔀䄀吀䔀 倀刀伀䌀䔀䐀唀刀䔀 搀戀漀⸀甀瀀搀愀琀攀开䐀漀挀䤀琀漀最ഀഀ
@iddoc int
਍ഀഀ
as
਍搀攀挀氀愀爀攀 䀀稀渀 椀渀琀Ⰰ䀀瀀爀开爀愀猀挀栀开椀琀漀最 椀渀琀ഀഀ
set @zn=(select Zn_cenom from nastroiki)
਍猀攀琀 䀀瀀爀开爀愀猀挀栀开椀琀漀最 㴀⠀猀攀氀攀挀琀 瀀爀开爀愀猀挀栀开椀琀漀最  昀爀漀洀 渀愀猀琀爀漀椀欀椀⤀ഀഀ

਍   ഀഀ
if (@iddoc>0) and   (@pr_rasch_itog=0) 
਍ഀഀ
update DocMaloc set 
਍䤀琀漀最漀䴀㴀挀愀猀攀 眀栀攀渀  ⠀猀攀氀攀挀琀 瀀爀椀稀渀漀欀 昀爀漀洀 琀戀最爀漀瀀攀爀 眀栀攀爀攀 䐀漀挀䴀愀氀漀挀⸀椀搀最爀漀瀀攀爀洀㴀椀搀最爀漀瀀攀爀 ⤀㴀✀ἀ䀄㠄䔄㸄㐄✄ ഀഀ
           then (select isnull(sum(Round( CenoM*KolfM ,@zn )),0) 
਍             昀爀漀洀 匀琀爀椀渀最䴀愀氀漀挀 眀栀攀爀攀 匀琀爀椀渀最䴀愀氀漀挀⸀䤀搀䐀漀挀䴀㴀䐀漀挀䴀愀氀漀挀⸀䤀搀搀漀挀䴀⤀ഀഀ
       else (select isnull(sum(Round(case when (idiznos<8) then CenoM*KolfM   else 0 end,@zn)),0) 
਍             昀爀漀洀 匀琀爀椀渀最䴀愀氀漀挀 眀栀攀爀攀 匀琀爀椀渀最䴀愀氀漀挀⸀䤀搀䐀漀挀䴀㴀䐀漀挀䴀愀氀漀挀⸀䤀搀搀漀挀䴀⤀ ഀഀ
       end
਍眀栀攀爀攀 䀀椀搀搀漀挀㴀椀搀搀漀挀洀ഀഀ

਍攀氀猀攀 椀昀 ⠀䀀椀搀搀漀挀㴀　⤀ 愀渀搀  ⠀䀀瀀爀开爀愀猀挀栀开椀琀漀最㴀　⤀ ഀഀ

਍甀瀀搀愀琀攀 䐀漀挀䴀愀氀漀挀 猀攀琀 ഀഀ
ItogoM=case when  (select priznok from tbgroper where DocMaloc.idgroperm=idgroper )='Приход' 
਍           琀栀攀渀 ⠀猀攀氀攀挀琀 椀猀渀甀氀氀⠀猀甀洀⠀刀漀甀渀搀⠀ 䌀攀渀漀䴀⨀䬀漀氀昀䴀 Ⰰ䀀稀渀 ⤀⤀Ⰰ　⤀ ഀഀ
             from StringMaloc where StringMaloc.IdDocM=DocMaloc.IddocM)
਍       攀氀猀攀 ⠀猀攀氀攀挀琀 椀猀渀甀氀氀⠀猀甀洀⠀刀漀甀渀搀⠀挀愀猀攀 眀栀攀渀 ⠀椀搀椀稀渀漀猀㰀㠀⤀ 琀栀攀渀 䌀攀渀漀䴀⨀䬀漀氀昀䴀   攀氀猀攀 　 攀渀搀Ⰰ䀀稀渀⤀⤀Ⰰ　⤀ ഀഀ
             from StringMaloc where StringMaloc.IdDocM=DocMaloc.IddocM) end
਍眀栀攀爀攀 搀愀琀攀搀漀挀洀㸀㴀䐀䄀吀䔀䄀䐀䐀⠀洀漀渀琀栀Ⰰⴀ㌀Ⰰ最攀琀搀愀琀攀⠀⤀⤀ഀഀ

਍攀氀猀攀 椀昀 ⠀䀀椀搀搀漀挀㸀　⤀ 愀渀搀  ⠀䀀瀀爀开爀愀猀挀栀开椀琀漀最㴀㄀⤀ ഀഀ

਍甀瀀搀愀琀攀 䐀漀挀䴀愀氀漀挀 猀攀琀 ഀഀ
ItogoM=case when  (select priznok from tbgroper where DocMaloc.idgroperm=idgroper )='Приход' 
਍           琀栀攀渀 ⠀猀攀氀攀挀琀 椀猀渀甀氀氀⠀猀甀洀⠀刀漀甀渀搀⠀ 䌀攀渀漀䴀⨀䬀漀氀昀䴀 Ⰰ䀀稀渀 ⤀⤀Ⰰ　⤀ ഀഀ
             from StringMaloc where StringMaloc.IdDocM=DocMaloc.IddocM)
਍       攀氀猀攀 ⠀猀攀氀攀挀琀 椀猀渀甀氀氀⠀猀甀洀⠀刀漀甀渀搀⠀挀愀猀攀 眀栀攀渀 䌀攀渀漀䴀伀琀瀀甀猀欀㴀渀甀氀氀  琀栀攀渀 䌀攀渀漀䴀⨀䬀漀氀昀䴀   攀氀猀攀 䌀攀渀漀䴀伀琀瀀甀猀欀⨀䬀漀氀昀䴀  攀渀搀Ⰰ䀀稀渀⤀⤀Ⰰ　⤀ ഀഀ
             from StringMaloc where StringMaloc.IdDocM=DocMaloc.IddocM) 
਍       攀渀搀ഀഀ
where @iddoc=iddocm
਍ഀഀ
else if (@iddoc=0) and  (@pr_rasch_itog=1) 
਍ഀഀ
update DocMaloc set 
਍䤀琀漀最漀䴀㴀挀愀猀攀 眀栀攀渀  ⠀猀攀氀攀挀琀 瀀爀椀稀渀漀欀 昀爀漀洀 琀戀最爀漀瀀攀爀 眀栀攀爀攀 䐀漀挀䴀愀氀漀挀⸀椀搀最爀漀瀀攀爀洀㴀椀搀最爀漀瀀攀爀 ⤀㴀✀ἀ䀄㠄䔄㸄㐄✄ ഀഀ
           then (select isnull(sum(Round( CenoM*KolfM ,@zn )),0) 
਍             昀爀漀洀 匀琀爀椀渀最䴀愀氀漀挀 眀栀攀爀攀 匀琀爀椀渀最䴀愀氀漀挀⸀䤀搀䐀漀挀䴀㴀䐀漀挀䴀愀氀漀挀⸀䤀搀搀漀挀䴀⤀ഀഀ
       else (select isnull(sum(Round(case when CenoMOtpusk=null  then CenoM*KolfM  else CenoMOtpusk*KolfM end,@zn)),0) 
਍             昀爀漀洀 匀琀爀椀渀最䴀愀氀漀挀 眀栀攀爀攀 匀琀爀椀渀最䴀愀氀漀挀⸀䤀搀䐀漀挀䴀㴀䐀漀挀䴀愀氀漀挀⸀䤀搀搀漀挀䴀⤀ 攀渀搀ഀഀ
where datedocm>=DATEADD(month,-3,getdate())
਍䜀伀ഀഀ
SET QUOTED_IDENTIFIER OFF 
਍䜀伀ഀഀ
SET ANSI_NULLS ON 
਍䜀伀ഀഀ

਍�