SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO















ALTER             PROCEDURE dbo.CalcPrintItog
@idDoc int
AS
Declare @ItogoKolF decimal(18,3),
        @ItogoKolm decimal(18,3),
        @ItogoKolTov decimal(18,3),
        @ItogoKolTar decimal(18,3),
        @ItogoRoz decimal(18,3),
        @ItogoMas decimal(18,3),
        @Itogo decimal(18,3), 
        @ItogoOO decimal(18,3),
        @ItogoKolKzp decimal(18,3),
        @ItogoKolOv decimal(18,3),
        @ItogoNaim smallint, 
        @VidDoc tinyint,
        @SumO decimal(18,3),
        @SumRoz decimal(18,3),
        @SumOpl decimal(18,3),
        @SumIncNDS decimal(18,3),
        @NameGrPr varchar(20),
        @IdGrOper smallint,
        @CenRoz decimal(18,3),
        @KodGr tinyint,
        @PrNds bit,
        @Kodskl int,
        @Idpredpr int

   set @CenRoz=0
   set @KodGr=0
   set @NameGrPr=''
   set @ItogoKolF=0 
   set @ItogoKolTov=0 
   set @ItogoKolTar=0 
   set @ItogoKolM=0 
   set @ItogoRoz=0
   set @ItogoOO=0
   set @ItogoKolKzp=0
   set @ItogoKolOv=0
   set @ItogoMas=0
   set @Itogo=0
   set @ItogoNaim=0
   set @SumOpl=0
   set @SumIncNDS=0

   /*DECLARE @idDoc int
   SET @idDoc = 606150
   Exec dbo.CalcPrintItog @idDoc*/   

   Select @VidDoc=VidDoc, @IdGrOper=IdGrOper,@Kodskl=Kodskl,@IdPredpr=IdPredpr from DocCaption where IdDoc=@IdDoc
   set @NameGrPr=(select namegrpredpr from tbgrpredpr where idgrpredpr=(select idgrpredpr from tbpredpr where idpredpr=(select kodskl from doccaption where iddoc=@Iddoc)))
   --if ((select TypeFieldN from tbOsnNastr where NameFieldN='Pr_SkidOv' and idPredpr=(Select Kodskl from Doccaption where iddoc=@Iddoc))=1) 
   if (((select TypeFieldN from tbOsnNastr where NameFieldN='Pr_SkidOv' and idPredpr=(Select Kodskl from Doccaption where iddoc=@Iddoc))=1) and (@VidDoc=1)) 
      Select @ItogoKolF=SUM(KolF-Isnull(KolOv,0)) from PrintString where IdDoc=@IdDoc 
   else
      Select @ItogoKolF=SUM(KolF) from PrintString where IdDoc=@IdDoc 
   Select @ItogoKolM=SUM(KolM),
          @ItogoMas=case
                    when (sum(kolz) is null) then (Sum(dbo.Rounding(0,(Ves*KolF))))
                    else (Sum(dbo.Rounding(0,(Ves*Kolz))))
                    end,
          --,@ItogoMas=Sum(Round((Ves*KolF),2))
          @ItogoKolOv=Sum(Isnull(KolOv,0)),
          @ItogoKolKzp=Sum(Isnull(KolKzp,0))
   from PrintString where IdDoc=@IdDoc 
   if @NameGrPr='Золото'
      set @ItogoRoz=(Select Sum(dbo.Rounding(0,CenRoz*KolF)) from PrintString where IdDoc=@IdDoc) 
   else 
   begin 
      if (((select TypeFieldN from tbOsnNastr where NameFieldN='Pr_SkidOv' and idPredpr=(Select Kodskl from Doccaption where iddoc=@Iddoc))=1) and (@VidDoc=2)) 
         Select @ItogoRoz=Sum(dbo.Rounding(0,CenRoz*(KolF-isnull(KolOv,0)))) from PrintString where IdDoc=@IdDoc -- Изменил Суховаров А.А. 
      else     
         Select @ItogoRoz=Sum(dbo.Rounding(0,CenRoz*KolF)) from PrintString where IdDoc=@IdDoc -- Изменил Суховаров А.А. 
   end
   Select @ItogoKolTar=IsNull(SUM(KolF),0) from PrintString where IdDoc=@IdDoc and (Kodgr=99 or (Kodgr=93 and KodPgr=0))
   set @ItogoKolTov=@ItogoKolF-@ItogoKolTar
   Select @ItogoNaim=Count(*)  from PrintString where IdDoc=@IdDoc
   if @VidDoc=1 or @VidDoc=5
   begin
      if (select trpredpr from tbnastrpredpr)=0 
      begin
         set @Itogo=(Select Sum(dbo.Rounding(0,CenO*KolF)) from DocString where IdDoc=@IdDoc)
      end
      else
      begin
         set @Itogo=(Select ISNULL(Sum(dbo.Rounding(0,dbo.Rounding(0,DocString.ceno*(1+DocString.NalTr/100))*KolF)),0) from DocString  where   DocString.IdDoc=@IdDoc )
      end
      Set @SumIncNDS=IsNull((Select Sum(IsNull(SumNDS,0)) from PrintString where (IdDoc=@IdDoc and (NDS-Floor(NDS))<>0) or (IdDoc=@IdDoc and Nds=10 and IdPrizlog=6 and PrLogBks<>1)),0)
--print cast(@SumIncNDS as varchar(18))+'приход'
   end
   else
   begin
      
      --if (Select NameGrOper from PrintCaption where IdDoc=@IdDoc)='Возврат посреднику'
      if (SELECT NameGrOper FROM tbGrOper WHERE idGrOper = (SELECT idGrOper FROM DocCaption WHERE idDoc = @idDoc))='Возврат посреднику'
         Select @Itogo=Sum(dbo.Rounding(0,dbo.Rounding(0,CenO*(1+NacPosr/100))*KolF))  from PrintString where IdDoc=@IdDoc  --Изменено 2016.02.29 Суховаров А.А.
      else
      begin
         if (select trpredpr from tbnastrpredpr)=0 
         begin
            --Set @Itogo=IsNull((Select Sum(Round(CenO*KolF*(1+Naltr/100)*(1+NacOpt/100),0))  from PrintString where IdDoc=@IdDoc and skidPokup=0),0)
            Set @Itogo=IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenO*(1+Naltr/100)*(1+NacOpt/100))*KolF))  from PrintString where IdDoc=@IdDoc and skidPokup=0 and SkidOv=0),0)
        +IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenO*(1+Naltr/100)*(1+NacOpt/100))*(KolF-KolOv)))  from PrintString where IdDoc=@IdDoc and skidPokup=0 and SkidOv<>0),0)     -- добавлено
        +IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenO*(1-SkidPokup/100))*KolF)) from PrintString where IdDoc=@IdDoc and skidPokup<>0 and PrLogBKS=0),0)
        +IsNull((Select Sum(dbo.Rounding(0,(CenO*(1-SkidPokup/100))-(CenO*(1-SkidPokup/100)*Nds/(100+Nds)))*KolF) from PrintString where IdDoc=@IdDoc and skidPokup<>0 and PrLogBKS=1),0)

            Set @ItogoOO=IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenO*(1+Naltr/100)*(1+NacOpt/100))*KolF))  from PrintString where IdDoc=@IdDoc  and CenPriobr=0 and skidPokup=0),0)
            +IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenO*(1-SkidPokup/100))*KolF)) from PrintString where IdDoc=@IdDoc and skidPokup<>0 and CenPriobr=0),0)
            +IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenPriobr*(1+Naltr/100)*(1+NacOpt/100))*KolF)) from PrintString where IdDoc=@IdDoc and CenPriobr<>0 and skidpokup=0),0)
            +IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenPriobr*(1-SkidPokup/100))*KolF)) from PrintString where IdDoc=@IdDoc and skidPokup<>0 and CenPriobr<>0),0)
         end
         else
         begin
            Set @Itogo=IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenO*(1+Naltr/100)*(1+NacOpt/100))*KolF))  from PrintString where IdDoc=@IdDoc and skidPokup=0 and SkidOv=0),0)
            +IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenO*(1+Naltr/100)*(1+NacOpt/100))*(KolF-KolOv)))  from PrintString where IdDoc=@IdDoc and skidPokup=0 and SkidOv<>0),0)
            +IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenO*(1-SkidPokup/100))*KolF)) from PrintString where IdDoc=@IdDoc and skidPokup<>0 and PrLogBKS=0),0)
            +IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenO*(1-SkidPokup/100)-CenO*(1-SkidPokup/100))*kolf*Nds/(100+Nds))) from PrintString where IdDoc=@IdDoc and skidPokup<>0 and PrLogBKS=1),0)
            --+IsNull((Select Sum(Round((CenO*KolF*(1-SkidPokup/100))-(CenO*KolF*(1-SkidPokup/100)*Nds/(100+Nds)),0)) from PrintString where IdDoc=@IdDoc and skidPokup<>0 and PrLogBKS=1),0)

            Set @ItogoOO=IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenO*(1+Naltr/100)*(1+NacOpt/100))*KolF))  from PrintString where IdDoc=@IdDoc  and CenPriobr=0 and skidPokup=0),0)
            +IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenO*(1-SkidPokup/100))*KolF)) from PrintString where IdDoc=@IdDoc and skidPokup<>0 and CenPriobr=0),0)
            +IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenPriobr*(1+Naltr/100)*(1+NacOpt/100))*KolF)) from PrintString where IdDoc=@IdDoc and CenPriobr<>0 and skidpokup=0),0)
            +IsNull((Select Sum(dbo.Rounding(0,dbo.Rounding(0,CenPriobr*(1-SkidPokup/100))*KolF)) from PrintString where IdDoc=@IdDoc and skidPokup<>0 and CenPriobr<>0),0)
         end
         --изменения для Молодечно 02.03.2010 
         --print @KodSkl
         --Print @IdPredpr
         Set @prNds=(select typefieldp from tbPrintNastr where idpredpr=@KodSkl and idpriznpartn=(select isnull(idpriznpartn,0) from tbpredpr where idpredpr=@idPredpr) and NameFieldp='Pr_Rnds')
         --print @PrNds 
         if @PrNds=1
            Set @SumIncNDS=IsNull((Select Sum(IsNull(SumNDSR,0)) from PrintString where (IdDoc=@IdDoc and (NDS-Floor(NDS))<>0) or (IdDoc=@IdDoc and IdPrizlog=41) or (IdDoc=@IdDoc and Nds=10 and IdPrizlog=6 and PrLogBks<>1)),0)
         else
            --изменения для Чашников по расчёту НДС 18.06.2007 
            Set @SumIncNDS=IsNull((Select Sum(IsNull(SumNDS,0)) from PrintString where (IdDoc=@IdDoc and (NDS-Floor(NDS))<>0) or (IdDoc=@IdDoc and IdPrizlog=41) or (IdDoc=@IdDoc and Nds=10 and IdPrizlog=6 and PrLogBks<>1)),0)
            --Set @SumIncNDS=IsNull((Select Sum(IsNull(SumNDSR,0)) from PrintString where (IdDoc=@IdDoc and (NDS-Floor(NDS))<>0)  or (IdDoc=@IdDoc and IdPrizlog=41) or (IdDoc=@IdDoc and Nds=24 and IdPrizlog=6)),0)
      end
   end
   Exec ItogRas @IdDoc,@IdGrOper,@SumO output,@SumRoz output,@SumOpl output,@ItogoMas output
   if (select typefieldp from tbPrintNastr where idpredpr=@KodSkl and idpriznpartn=(select isnull(idpriznpartn,0) from tbpredpr where idpredpr=@idPredpr) and NameFieldp='Pr_Rnds')=1
      set @SumIncNds=0
   Update  PrintCaption 
   set    ItogoKolF=@ItogoKolF,
          ItogoKolM=@ItogoKolM,
          ItogoNaim =@ItogoNaim,
          ItogoKolTov=@ItogoKolTov,
          ItogoMas=@ItogoMas,
          ItogoRoz=@ItogoRoz,
          ItogoOO=@ItogoOO,
          ItogoKolKzp=@ItogoKolKzp,
          ItogoKolOv=@ItogoKolOv,
          Itogo=@Itogo,
          SumOpl=@SumOpl,
          SumIncNDS=@SumIncNDS
   where   IdDoc=@IdDoc












GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

