-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2025, All rights reserved by Steffen Anders, Elsteraue - Germany
-- Visit also https://www.it-crafts.de.
--
-- !!!!!!!!!!!!!!!!!!! Diese Datei wird momentan noch ueberarbeitet !!!!!!!!!!!!!!!!!!
------------------------------------------------------------------------------------------
-- Bilanzauswertung für 12 Monate 
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'hwp_12MonBil')
  DROP PROCEDURE [dbo].[hwp_12MonBil]
GO
/*
CREATE PROCEDURE [dbo].[hwp_12MonBil] 
    @mLstNr CHAR(4)
   ,@m01 CHAR(6),@m02 CHAR(6),@m03 CHAR(6),@m04 CHAR(6),@m05 CHAR(6),@m06 CHAR(6)
   ,@m07 CHAR(6),@m08 CHAR(6),@m09 CHAR(6),@m10 CHAR(6),@m11 CHAR(6),@m12 CHAR(6)
AS
	-- 1. Salden der Konto ermitteln
	CREATE TABLE tmpSaldo
	(
	   [Typ]     char(1) DEFAULT ' '
	  ,[Konto]   char(8) DEFAULT ' '
	  ,[KerKz]   char(1) DEFAULT ' '
	  ,[ASaldo01] float DEFAULT 0
	  ,[NSaldo01] float DEFAULT 0
	  ,[ASaldo02] float DEFAULT 0
	  ,[NSaldo02] float DEFAULT 0
	  ,[ASaldo03] float DEFAULT 0
	  ,[NSaldo03] float DEFAULT 0
	  ,[ASaldo04] float DEFAULT 0
	  ,[NSaldo04] float DEFAULT 0
	  ,[ASaldo05] float DEFAULT 0
	  ,[NSaldo05] float DEFAULT 0
	  ,[ASaldo06] float DEFAULT 0
	  ,[NSaldo06] float DEFAULT 0
	  ,[ASaldo07] float DEFAULT 0
	  ,[NSaldo07] float DEFAULT 0
	  ,[ASaldo08] float DEFAULT 0
	  ,[NSaldo08] float DEFAULT 0
	  ,[ASaldo09] float DEFAULT 0
	  ,[NSaldo09] float DEFAULT 0
	  ,[ASaldo10] float DEFAULT 0
	  ,[NSaldo10] float DEFAULT 0
	  ,[ASaldo11] float DEFAULT 0
	  ,[NSaldo11] float DEFAULT 0
	  ,[ASaldo12] float DEFAULT 0
	  ,[NSaldo12] float DEFAULT 0
	)
	CREATE INDEX Ik_TypKonto ON tmpSaldo (Typ,Konto)
	----------------------------------------------------------------------------------------
	INSERT tmpSaldo (Typ,Konto,KerKz)
		 (SELECT Typ, Konto, SUBSTRING(BilZeile,1,1) FROM [dbo].[ut_Kontenplan] K WHERE (K.Typ = 'P' OR K.Typ = 'S') 
			AND 0 < (SELECT COUNT(*) FROM [dbo].[ut_Buchungszeilen] Z
					 WHERE Z.Typ = K.Typ AND Z.Konto = K.Konto AND (Z.Jahr = SUBSTRING(@m12, 1, 4) OR Z.Jahr = SUBSTRING(@m01, 1, 4))
					)
		 )
	----------------------------------------------------------------------------------------
	UPDATE tmpSaldo SET
	  ASaldo01 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m01, 1, 4), SUBSTRING(@m01, 5, 2), '01')
	 ,NSaldo01 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m01, 1, 4), SUBSTRING(@m01, 5, 2), '32')
	 ,ASaldo02 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m02, 1, 4), SUBSTRING(@m02, 5, 2), '01')
	 ,NSaldo02 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m02, 1, 4), SUBSTRING(@m02, 5, 2), '32')
	 ,ASaldo03 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m03, 1, 4), SUBSTRING(@m03, 5, 2), '01')
	 ,NSaldo03 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m03, 1, 4), SUBSTRING(@m03, 5, 2), '32')
	 ,ASaldo04 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m04, 1, 4), SUBSTRING(@m04, 5, 2), '01')
	 ,NSaldo04 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m04, 1, 4), SUBSTRING(@m04, 5, 2), '32')
	 ,ASaldo05 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m05, 1, 4), SUBSTRING(@m05, 5, 2), '01')
	 ,NSaldo05 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m05, 1, 4), SUBSTRING(@m05, 5, 2), '32')
	 ,ASaldo06 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m06, 1, 4), SUBSTRING(@m06, 5, 2), '01')
	 ,NSaldo06 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m06, 1, 4), SUBSTRING(@m06, 5, 2), '32')
	 ,ASaldo07 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m07, 1, 4), SUBSTRING(@m07, 5, 2), '01')
	 ,NSaldo07 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m07, 1, 4), SUBSTRING(@m07, 5, 2), '32')
	 ,ASaldo08 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m08, 1, 4), SUBSTRING(@m08, 5, 2), '01')
	 ,NSaldo08 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m08, 1, 4), SUBSTRING(@m08, 5, 2), '32')
	 ,ASaldo09 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m09, 1, 4), SUBSTRING(@m09, 5, 2), '01')
	 ,NSaldo09 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m09, 1, 4), SUBSTRING(@m09, 5, 2), '32')
	 ,ASaldo10 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m10, 1, 4), SUBSTRING(@m10, 5, 2), '01')
	 ,NSaldo10 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m10, 1, 4), SUBSTRING(@m10, 5, 2), '32')
	 ,ASaldo11 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m11, 1, 4), SUBSTRING(@m11, 5, 2), '01')
	 ,NSaldo11 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m11, 1, 4), SUBSTRING(@m11, 5, 2), '32')
	 ,ASaldo12 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m12, 1, 4), SUBSTRING(@m12, 5, 2), '01')
	 ,NSaldo12 = dbo.itcfunc_SaldoVor(Typ, Konto, SUBSTRING(@m12, 1, 4), SUBSTRING(@m12, 5, 2), '32')
	----------------------------------------------------------------------------------------
	DELETE FROM tmpSaldo WHERE ASaldo01 = 0 AND NSaldo01 = 0 AND ASaldo02 = 0 AND NSaldo02 = 0 
						   AND ASaldo03 = 0 AND NSaldo03 = 0 AND ASaldo04 = 0 AND NSaldo04 = 0
						   AND ASaldo05 = 0 AND NSaldo05 = 0 AND ASaldo06 = 0 AND NSaldo06 = 0
						   AND ASaldo07 = 0 AND NSaldo07 = 0 AND ASaldo08 = 0 AND NSaldo08 = 0
						   AND ASaldo09 = 0 AND NSaldo09 = 0 AND ASaldo10 = 0 AND NSaldo10 = 0
						   AND ASaldo11 = 0 AND NSaldo11 = 0 AND ASaldo12 = 0 AND NSaldo12 = 0
						   AND Typ = 'P'
	----------------------------------------------------------------------------------------
	-- 2. Salden auf Bilanzschema übertragen
	CREATE TABLE tmpBilZ
	(
	   [LfdNr] integer DEFAULT 0
	  ,[ParentNr] integer DEFAULT 0
	  ,[SummNr] integer DEFAULT 0
	  ,[PosNr] char(20) DEFAULT ' '
	  ,[Text] char(50) DEFAULT ' '
	  ,[Tiefe] integer DEFAULT 0
	  ,[PosTyp] integer DEFAULT 0
	  ,[KtoTyp] char(1) DEFAULT ' '
	  ,[KtoVon] char(8) DEFAULT ' '
	  ,[KtoBis] char(8) DEFAULT ' '
	  ,[VzDruck] tinyint DEFAULT 0
	  ,[VzSumme] tinyint DEFAULT 0
	  ,[VzSaldo] tinyint DEFAULT 0
	  ,[Still] tinyint DEFAULT 0
	  ,[Fett] tinyint DEFAULT 0
	  ,[AbKerKz] char(1) DEFAULT ' '
	  ,[Value01] float DEFAULT 0 -- Druckwert 01
	  ,[Value02] float DEFAULT 0
	  ,[Value03] float DEFAULT 0
	  ,[Value04] float DEFAULT 0
	  ,[Value05] float DEFAULT 0
	  ,[Value06] float DEFAULT 0
	  ,[Value07] float DEFAULT 0
	  ,[Value08] float DEFAULT 0
	  ,[Value09] float DEFAULT 0
	  ,[Value10] float DEFAULT 0
	  ,[Value11] float DEFAULT 0
	  ,[Value12] float DEFAULT 0
	  ,[SValue01] float DEFAULT 0 -- Summenwert 01
	  ,[SValue02] float DEFAULT 0
	  ,[SValue03] float DEFAULT 0
	  ,[SValue04] float DEFAULT 0
	  ,[SValue05] float DEFAULT 0
	  ,[SValue06] float DEFAULT 0
	  ,[SValue07] float DEFAULT 0
	  ,[SValue08] float DEFAULT 0
	  ,[SValue09] float DEFAULT 0
	  ,[SValue10] float DEFAULT 0
	  ,[SValue11] float DEFAULT 0
	  ,[SValue12] float DEFAULT 0
	  ,[ASaldo01] float DEFAULT 0 -- Alter Saldo 01
	  ,[NSaldo01] float DEFAULT 0 -- Neuer Saldo 01
	  ,[ASaldo02] float DEFAULT 0
	  ,[NSaldo02] float DEFAULT 0
	  ,[ASaldo03] float DEFAULT 0
	  ,[NSaldo03] float DEFAULT 0
	  ,[ASaldo04] float DEFAULT 0
	  ,[NSaldo04] float DEFAULT 0
	  ,[ASaldo05] float DEFAULT 0
	  ,[NSaldo05] float DEFAULT 0
	  ,[ASaldo06] float DEFAULT 0
	  ,[NSaldo06] float DEFAULT 0
	  ,[ASaldo07] float DEFAULT 0
	  ,[NSaldo07] float DEFAULT 0
	  ,[ASaldo08] float DEFAULT 0
	  ,[NSaldo08] float DEFAULT 0
	  ,[ASaldo09] float DEFAULT 0
	  ,[NSaldo09] float DEFAULT 0
	  ,[ASaldo10] float DEFAULT 0
	  ,[NSaldo10] float DEFAULT 0
	  ,[ASaldo11] float DEFAULT 0
	  ,[NSaldo11] float DEFAULT 0
	  ,[ASaldo12] float DEFAULT 0
	  ,[NSaldo12] float DEFAULT 0
	)
	CREATE INDEX Ik_LfdNr  ON tmpBilZ (LfdNr)
	CREATE INDEX Ik_Tiefe  ON tmpBilZ (Tiefe,LfdNr)
	CREATE INDEX Ik_SummNr ON tmpBilZ (SummNr)
	CREATE INDEX Ik_PosTyp ON tmpBilZ (PosTyp,Tiefe)
	----------------------------------------------------------------------------------------
	INSERT tmpBilZ (LfdNr,PosNr,[Text],Tiefe,PosTyp,KtoTyp,KtoVon,KtoBis,VzDruck,VzSumme,VzSaldo,Still,Fett,AbKerKz)
		 (SELECT LfdNr,PosNr,[Text],Tiefe,PosTyp,KtoTyp,KtoVon,KtoBis,VzDruck,VzSumme,VzSaldo,Still,Fett,AbKerKz
		  FROM [dbo].[ut_Bilanzzeilen] WHERE LstNr = @mLstNr)
	UPDATE tmpBilZ SET
	  ParentNr = (SELECT MAX(Z2.LfdNr) FROM tmpBilZ Z2 WHERE Z2.Tiefe = tmpBilZ.Tiefe - 1 AND Z2.LfdNr < tmpBilZ.LfdNr)
	WHERE Tiefe > 0
	UPDATE tmpBilZ SET
	  SummNr = (SELECT MIN(Z2.LfdNr) FROM tmpBilZ Z2 WHERE Z2.Tiefe = tmpBilZ.Tiefe - 1 AND Z2.LfdNr > tmpBilZ.LfdNr AND Z2.PosTyp = 3)
	WHERE Tiefe > 0 AND PosTyp > 1
	DELETE FROM tmpBilZ WHERE PosTyp = 2 AND KtoTyp <> 'P' AND
		0 = (SELECT COUNT(*) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis)
	----------------------------------------------------------------------------------------
	UPDATE tmpBilZ SET 
	  ASaldo01 = ISNULL((SELECT SUM(ASaldo01) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,NSaldo01 = ISNULL((SELECT SUM(NSaldo01) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,ASaldo02 = ISNULL((SELECT SUM(ASaldo02) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,NSaldo02 = ISNULL((SELECT SUM(NSaldo02) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,ASaldo03 = ISNULL((SELECT SUM(ASaldo03) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,NSaldo03 = ISNULL((SELECT SUM(NSaldo03) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,ASaldo04 = ISNULL((SELECT SUM(ASaldo04) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,NSaldo04 = ISNULL((SELECT SUM(NSaldo04) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,ASaldo05 = ISNULL((SELECT SUM(ASaldo05) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,NSaldo05 = ISNULL((SELECT SUM(NSaldo05) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,ASaldo06 = ISNULL((SELECT SUM(ASaldo06) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,NSaldo06 = ISNULL((SELECT SUM(NSaldo06) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,ASaldo07 = ISNULL((SELECT SUM(ASaldo07) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,NSaldo07 = ISNULL((SELECT SUM(NSaldo07) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,ASaldo08 = ISNULL((SELECT SUM(ASaldo08) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,NSaldo08 = ISNULL((SELECT SUM(NSaldo08) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,ASaldo09 = ISNULL((SELECT SUM(ASaldo09) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,NSaldo09 = ISNULL((SELECT SUM(NSaldo09) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,ASaldo10 = ISNULL((SELECT SUM(ASaldo10) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,NSaldo10 = ISNULL((SELECT SUM(NSaldo10) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,ASaldo11 = ISNULL((SELECT SUM(ASaldo11) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,NSaldo11 = ISNULL((SELECT SUM(NSaldo11) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,ASaldo12 = ISNULL((SELECT SUM(ASaldo12) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	 ,NSaldo12 = ISNULL((SELECT SUM(NSaldo12) FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis),0.00)
	WHERE PosTyp = 2
	----------------------------------------------------------------------------------------
	-- 3. Bilanzkennung ermitteln und Kontenpositionen berechnen
	UPDATE tmpBilZ SET 
		  AbKerKz = 'B' 
	WHERE PosTyp = 2 AND KtoTyp = 'P'
	UPDATE tmpBilZ SET 
		  AbKerKz = (SELECT TOP 1 KerKz FROM tmpSaldo WHERE Typ = KtoTyp AND Konto >= KtoVon AND Konto <= KtoBis)
	WHERE PosTyp = 2 AND KtoTyp = 'S' AND AbKerKz = ' '
	----------------------------------------------------------------------------------------
	UPDATE tmpBilZ SET
	  Value01 = Round( NSaldo01 - ASaldo01, 2 )
	 ,Value02 = Round( NSaldo02 - ASaldo02, 2 )
	 ,Value03 = Round( NSaldo03 - ASaldo03, 2 )
	 ,Value04 = Round( NSaldo04 - ASaldo04, 2 )
	 ,Value05 = Round( NSaldo05 - ASaldo05, 2 )
	 ,Value06 = Round( NSaldo06 - ASaldo06, 2 )
	 ,Value07 = Round( NSaldo07 - ASaldo07, 2 )
	 ,Value08 = Round( NSaldo08 - ASaldo08, 2 )
	 ,Value09 = Round( NSaldo09 - ASaldo09, 2 )
	 ,Value10 = Round( NSaldo10 - ASaldo10, 2 )
	 ,Value11 = Round( NSaldo11 - ASaldo11, 2 )
	 ,Value12 = Round( NSaldo12 - ASaldo12, 2 )
	 WHERE PosTyp = 2 --AND AbKerKz = 'G'
	----------------------------------------------------------------------------------------
	UPDATE tmpBilZ SET
	  SValue01 = Value01
	 ,SValue02 = Value02
	 ,SValue03 = Value03
	 ,SValue04 = Value04
	 ,SValue05 = Value05
	 ,SValue06 = Value06
	 ,SValue07 = Value07
	 ,SValue08 = Value08
	 ,SValue09 = Value09
	 ,SValue10 = Value10
	 ,SValue11 = Value11
	 ,SValue12 = Value12
	 WHERE PosTyp = 2
	----------------------------------------------------------------------------------------
	UPDATE tmpBilZ SET
	  Value01 = 0.00 - Value01
	 ,Value02 = 0.00 - Value02
	 ,Value03 = 0.00 - Value03
	 ,Value04 = 0.00 - Value04
	 ,Value05 = 0.00 - Value05
	 ,Value06 = 0.00 - Value06
	 ,Value07 = 0.00 - Value07
	 ,Value08 = 0.00 - Value08
	 ,Value09 = 0.00 - Value09
	 ,Value10 = 0.00 - Value10
	 ,Value11 = 0.00 - Value11
	 ,Value12 = 0.00 - Value12
	 WHERE PosTyp = 2 AND VzDruck = 1
	----------------------------------------------------------------------------------------
	UPDATE tmpBilZ SET
	  SValue01 = 0.00 - SValue01
	 ,SValue02 = 0.00 - SValue02
	 ,SValue03 = 0.00 - SValue03
	 ,SValue04 = 0.00 - SValue04
	 ,SValue05 = 0.00 - SValue05
	 ,SValue06 = 0.00 - SValue06
	 ,SValue07 = 0.00 - SValue07
	 ,SValue08 = 0.00 - SValue08
	 ,SValue09 = 0.00 - SValue09
	 ,SValue10 = 0.00 - SValue10
	 ,SValue11 = 0.00 - SValue11
	 ,SValue12 = 0.00 - SValue12
	 WHERE PosTyp = 2 AND VzSumme = 1
	----------------------------------------------------------------------------------------
	UPDATE tmpBilZ SET 
	  ASaldo01 = 0.00 - ASaldo01
	 ,NSaldo01 = 0.00 - NSaldo01
	 ,ASaldo02 = 0.00 - ASaldo02
	 ,NSaldo02 = 0.00 - NSaldo02
	 ,ASaldo03 = 0.00 - ASaldo03
	 ,NSaldo03 = 0.00 - NSaldo03
	 ,ASaldo04 = 0.00 - ASaldo04
	 ,NSaldo04 = 0.00 - NSaldo04
	 ,ASaldo05 = 0.00 - ASaldo05
	 ,NSaldo05 = 0.00 - NSaldo05
	 ,ASaldo06 = 0.00 - ASaldo06
	 ,NSaldo06 = 0.00 - NSaldo06
	 ,ASaldo07 = 0.00 - ASaldo07
	 ,NSaldo07 = 0.00 - NSaldo07
	 ,ASaldo08 = 0.00 - ASaldo08
	 ,NSaldo08 = 0.00 - NSaldo08
	 ,ASaldo09 = 0.00 - ASaldo09
	 ,NSaldo09 = 0.00 - NSaldo09
	 ,ASaldo10 = 0.00 - ASaldo10
	 ,NSaldo10 = 0.00 - NSaldo10
	 ,ASaldo11 = 0.00 - ASaldo11
	 ,NSaldo11 = 0.00 - NSaldo11
	 ,ASaldo12 = 0.00 - ASaldo12
	 ,NSaldo12 = 0.00 - NSaldo12
	WHERE PosTyp = 2 AND VzSaldo = 1
	----------------------------------------------------------------------------------------
	-- 4. Summen Ermitteln
	DECLARE  @mTiefe integer
	SET @mTiefe = 8
	WHILE @mTiefe >= 0
	BEGIN
	   UPDATE tmpBilZ SET
		 Value01 = ISNULL((SELECT SUM(Round(SValue01,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,Value02 = ISNULL((SELECT SUM(Round(SValue02,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,Value03 = ISNULL((SELECT SUM(Round(SValue03,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,Value04 = ISNULL((SELECT SUM(Round(SValue04,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,Value05 = ISNULL((SELECT SUM(Round(SValue05,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,Value06 = ISNULL((SELECT SUM(Round(SValue06,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,Value07 = ISNULL((SELECT SUM(Round(SValue07,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,Value08 = ISNULL((SELECT SUM(Round(SValue08,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,Value09 = ISNULL((SELECT SUM(Round(SValue09,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,Value10 = ISNULL((SELECT SUM(Round(SValue10,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,Value11 = ISNULL((SELECT SUM(Round(SValue11,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,Value12 = ISNULL((SELECT SUM(Round(SValue12,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,ASaldo01 = ISNULL((SELECT SUM(Round(ASaldo01,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,ASaldo02 = ISNULL((SELECT SUM(Round(ASaldo02,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,ASaldo03 = ISNULL((SELECT SUM(Round(ASaldo03,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,ASaldo04 = ISNULL((SELECT SUM(Round(ASaldo04,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,ASaldo05 = ISNULL((SELECT SUM(Round(ASaldo05,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,ASaldo06 = ISNULL((SELECT SUM(Round(ASaldo06,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,ASaldo07 = ISNULL((SELECT SUM(Round(ASaldo07,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,ASaldo08 = ISNULL((SELECT SUM(Round(ASaldo08,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,ASaldo09 = ISNULL((SELECT SUM(Round(ASaldo09,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,ASaldo10 = ISNULL((SELECT SUM(Round(ASaldo10,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,ASaldo11 = ISNULL((SELECT SUM(Round(ASaldo11,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,ASaldo12 = ISNULL((SELECT SUM(Round(ASaldo12,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,NSaldo01 = ISNULL((SELECT SUM(Round(NSaldo01,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,NSaldo02 = ISNULL((SELECT SUM(Round(NSaldo02,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,NSaldo03 = ISNULL((SELECT SUM(Round(NSaldo03,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,NSaldo04 = ISNULL((SELECT SUM(Round(NSaldo04,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,NSaldo05 = ISNULL((SELECT SUM(Round(NSaldo05,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,NSaldo06 = ISNULL((SELECT SUM(Round(NSaldo06,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,NSaldo07 = ISNULL((SELECT SUM(Round(NSaldo07,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,NSaldo08 = ISNULL((SELECT SUM(Round(NSaldo08,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,NSaldo09 = ISNULL((SELECT SUM(Round(NSaldo09,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,NSaldo10 = ISNULL((SELECT SUM(Round(NSaldo10,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,NSaldo11 = ISNULL((SELECT SUM(Round(NSaldo11,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		,NSaldo12 = ISNULL((SELECT SUM(Round(NSaldo12,2)) FROM tmpBilZ C WHERE C.SummNr = tmpBilZ.LfdNr),0.00)
		WHERE PosTyp = 3 AND Tiefe = @mTiefe

	   UPDATE tmpBilZ SET
		 SValue01 = Value01
		,SValue02 = Value02
		,SValue03 = Value03
		,SValue04 = Value04
		,SValue05 = Value05
		,SValue06 = Value06
		,SValue07 = Value07
		,SValue08 = Value08
		,SValue09 = Value09
		,SValue10 = Value10
		,SValue11 = Value11
		,SValue12 = Value12
		WHERE PosTyp = 3 AND Tiefe = @mTiefe

	   UPDATE tmpBilZ SET
		 Value01 = 0.00 - Value01
		,Value02 = 0.00 - Value02
		,Value03 = 0.00 - Value03
		,Value04 = 0.00 - Value04
		,Value05 = 0.00 - Value05
		,Value06 = 0.00 - Value06
		,Value07 = 0.00 - Value07
		,Value08 = 0.00 - Value08
		,Value09 = 0.00 - Value09
		,Value10 = 0.00 - Value10
		,Value11 = 0.00 - Value11
		,Value12 = 0.00 - Value12
		WHERE PosTyp = 3 AND Tiefe = @mTiefe AND VzDruck = 1

	   UPDATE tmpBilZ SET
		 SValue01 = 0.00 - SValue01
		,SValue02 = 0.00 - SValue02
		,SValue03 = 0.00 - SValue03
		,SValue04 = 0.00 - SValue04
		,SValue05 = 0.00 - SValue05
		,SValue06 = 0.00 - SValue06
		,SValue07 = 0.00 - SValue07
		,SValue08 = 0.00 - SValue08
		,SValue09 = 0.00 - SValue09
		,SValue10 = 0.00 - SValue10
		,SValue11 = 0.00 - SValue11
		,SValue12 = 0.00 - SValue12
		WHERE PosTyp = 3 AND Tiefe = @mTiefe AND VzSumme = 1

	   SET @mTiefe = @mTiefe - 1
	END
	----------------------------------------------------------------------------------------
	SELECT @mLstNr AS LstNr, * FROM tmpBilZ ORDER BY LfdNr
	DROP TABLE tmpSaldo
	DROP TABLE tmpBilZ
*/
--GO


