// Bericht Aufmassliste (nachbearbeietet)
using craftsReport.Interfaces;

namespace craftsReport.modules
{
	public class rp0453_xbs : IReportModule
	{
		public IReportHost HostObject { set; get; }

		public rp0453_xbs()
		{
		}

		// Aufmassliste
		public bool Execute()
		{
			HostObject.DefineVariable2("Seite", "s,2,0");
			HostObject.DefineVariable2("Zeile", "s,2,0");
			HostObject.DefineVariable2("Heute", "D,10,0");
			HostObject.DefineVariable2("mSumme", "d,8,2");
			HostObject.DefineVariable2("bNeedHdr", "L,1,0");
			HostObject.DefineVariable2("bNeedTHdr", "L,1,0");
			HostObject.DefineVariable2("cTmp", "s,1,0");
			HostObject.DefineVariable2("nCnt", "d,8,0");
			HostObject.DefineVariable2("bDrkZube", "L,1,0");

			if (HostObject.IsVar("cyZeile"))
				goto _cyZeile;
			HostObject.DefineVariable2("cyZeile", "N,8,4");
			HostObject.Set("cyZeile", @" 2,54/6", true);
			_cyZeile:

			if (HostObject.IsVar("ZlMax"))
				goto _ZlMax;
			HostObject.DefineVariable2("ZlMax", "N,2,0");
			HostObject.Set("ZlMax", @" 63", true);
			_ZlMax:

			if (HostObject.IsVar("RandX"))
				goto _RandX;
			HostObject.DefineVariable2("RandX", "N,5,2");
			HostObject.Set("RandX", @" 1,80", true);
			_RandX:

			if (HostObject.IsVar("RandY"))
				goto _RandY;
			HostObject.DefineVariable2("RandY", "N,5,2");
			HostObject.Set("RandY", @" 0,80", true);
			_RandY:

			if (HostObject.IsVar("SizeX"))
				goto _SizeX;
			HostObject.DefineVariable2("SizeX", "N,5,2");
			HostObject.Set("SizeX", @" 18,40", true);
			_SizeX:

			HostObject.Set("Seite", @"      1", true);
			HostObject.Set("Zeile", @"      0", true);
			HostObject.Set("Heute", @"H", true);
			HostObject.Set("bNeedHdr", @"1", true);
			HostObject.Set("bNeedTHdr", @"1", true);
			HostObject.Set("bDrkZube", @"1", true);

			if (!HostObject.IsVar("bZubehoer"))
				HostObject.DefineVariable2("bZubehoer", "L,1,0");
			if (!HostObject.IsVar("bUnterpos"))
				HostObject.DefineVariable2("bUnterpos", "L,1,0");
			if (!HostObject.IsVar("mBauleiter"))
				HostObject.DefineVariable2("mBauleiter", "L,1,0");
			if (!HostObject.IsVar("mBauleiter"))
				HostObject.DefineVariable2("mBauleiter", "L,1,0");
			if (!HostObject.IsVar("bAufmass"))
				HostObject.DefineVariable2("bAufmass", "L,1,0");
			if (!HostObject.IsVar("mBA"))
				HostObject.DefineVariable2("mBA", "L,1,0");
			if (!HostObject.IsVar("mKurz"))
				HostObject.DefineVariable2("mKurz", "L,1,0");
			if (!HostObject.IsVar("mDatum"))
				HostObject.DefineVariable2("mDatum", "L,1,0");
			if (!HostObject.IsVar("mSpalte"))
				HostObject.DefineVariable2("mSpalte", "L,1,0");
			if (!HostObject.IsVar("mMassDet"))
				HostObject.DefineVariable2("mMassDet", "L,1,0");
			if (!HostObject.IsVar("mNummer"))
				HostObject.DefineVariable2("mNummer", "L,1,0");
			if (!HostObject.IsVar("mMengen"))
				HostObject.DefineVariable2("mMengen", "L,1,0");
			if (!HostObject.IsVar("mRaum"))
				HostObject.DefineVariable2("mRaum", "L,1,0");
			if (!HostObject.IsVar("bBAweise"))
				HostObject.DefineVariable2("bBAweise", "L,1,0");
			if (!HostObject.IsVar("mAufmass"))
				HostObject.DefineVariable2("mAufmass", "d,4,0");
			if (!HostObject.IsVar("bAlle"))
				HostObject.DefineVariable2("bAlle", "L,1,0");

			HostObject.Set("mBauleiter", @"0", true);
			HostObject.Set("bAufmass", @"1", true);
			HostObject.Set("mMassDet", @"0", true);
			if (HostObject.EvalBool("$[mBA]"))
				HostObject.Set("mMassDet", @"1", true);
			if (HostObject.EvalBool("$[mDatum]"))
				HostObject.Set("mMassDet", @"1", true);
			if (HostObject.EvalBool("$[mSpalte]"))
				HostObject.Set("mMassDet", @"1", true);
			if (HostObject.EvalBool("$[mNummer]"))
				HostObject.Set("mMassDet", @"1", true);
			if (HostObject.EvalBool("$[mRaum]"))
				HostObject.Set("mMassDet", @"1", true);

			//  Datensatz BELEGVERZEICHNIS

			HostObject.DefineRecord("recPOS", "TMPKALKLIST", 0, 18, HostObject.EvalString(@"$[FltKey<18]"));
			HostObject.DefineRecord("recMWST", "BELEGPOSITIONEN", 0, 18, HostObject.EvalString(@"$[FltKey<18]"));
			HostObject.DefineRecord("recBVZ", "BELEGVERZEICHNIS", 0, 18, HostObject.EvalString(@"$[FltKey<18]"));
			HostObject.DefineRecord("recEXT", "BELEGKOPF_ERWEITERUNG", 0, 18, HostObject.EvalString(@"$[FltKey<18]"));
			HostObject.DefineRecord("recWKZ", "WÄHRUNGEN", 1, 6, HostObject.EvalString(@"$[recBVZ.WAEHRUNG<6]"));
			HostObject.DefineRecord("recSTEURNR", "OPTIONEN", 0, 5, HostObject.EvalString(@" 6110"));
			HostObject.DefineRecord("recUSTIDNR", "OPTIONEN", 0, 5, HostObject.EvalString(@" 6112"));
			HostObject.DefineRecord("recBA", "BELBAUABSCHNITTE", 0, 18, HostObject.EvalString(@"$[FltKey<18]"));

			//  eigene Anschrift aus Belegvorlauf holen
			HostObject.DefineVariable2("mADRNR", "u,2,0");
			HostObject.Set("mADRNR", @"$[recBVZ.EIGANSCH]", true);
			if (HostObject.EvalBool("$[mADRNR] < 10"))
				HostObject.Set("mADRNR", @"0$[mADRNR>1]", true);
			HostObject.DefineRecord("recEIG", "EIGENE_ANSCHRIFTEN", 0, 2, HostObject.EvalString(@"$[mADRNR>2]"));

			if (HostObject.EvalBool("$[mBauleiter]"))
				HostObject.DefineRecord("recADR1", "BELEGADRESSEN", 0, 19, HostObject.EvalString(@"$[FltKey<18]1"));
			else
				HostObject.DefineRecord("recADR1", "BELEGADRESSEN", 0, 19, HostObject.EvalString(@"$[FltKey<18]0"));
			HostObject.DefineRecord("recADR2", "BELEGADRESSEN", 0, 19, HostObject.EvalString(@"$[FltKey<18]0"));
			HostObject.DefineVariable2("adrZeile1", "C,81,0");
			HostObject.DefineVariable2("adrZeile2", "C,81,0");
			HostObject.DefineVariable2("adrZeile3", "C,81,0");
			HostObject.DefineVariable2("adrZeile4", "C,81,0");
			HostObject.DefineVariable2("adrZeile5", "C,81,0");
			HostObject.DefineVariable2("adrStrasse", "C,40,0");
			HostObject.DefineVariable2("adrPlzOrt", "C,50,0");
			HostObject.Set("adrZeile1", @"$[recADR1.Anrede]", true);
			HostObject.Set("adrZeile2", @"$[recADR1.Name1]", true);
			HostObject.Set("adrZeile3", @"$[recADR1.Name2]", true);
			HostObject.Set("adrZeile4", @"$[recADR1.Branche]", true);
			HostObject.Set("adrZeile5", @"$[recADR1.zuHaenden]", true);
			HostObject.Set("adrStrasse", @"$[recADR1.Strasse]", true);
			HostObject.Set("adrPlzOrt", @"$[recADR1.Plz] $[recADR1.Ort]", true);

			if (!HostObject.IsEmpty("recADR1.Vorname"))
				HostObject.Set("adrZeile2", @"$[recADR1.Vorname] $[adrZeile2]", true);
			SUB_subFmtAdr1();

			HostObject.DefineVariable2("cWKZ", "C,6,0");
			if (HostObject.IsEmpty("recWKZ.Zeichen"))
				HostObject.Set("cWKZ", @"$[recBVZ.WAEHRUNG]", true);
			else
				HostObject.Set("cWKZ", @"$[recWKZ.Zeichen]", true);

			// --------------------------------------------------
			//  Erste Seite initialisieren und PFD Angaben senden

			//  PDF Options Header
			HostObject.DefineRecord("recPDF", "OPTIONEN", 0, 5, HostObject.EvalString(@"20003"));
			HostObject.DefineVariable2("mJobHead", "C,250,0");
			HostObject.Set("mJobHead", @"@saPS2PDF-c$[recPDF.OptDataC]@ @saPS2PDF-p$[recBVZ.PRJNR]@ @saPS2PDF-a$[recBVZ.KDNR]@", true);
			HostObject.Set("mJobHead", @"@saPS2PDF-dKurfassung $[recBVZ.PRJNR]/$[recBVZ.BELEGART]/$[recBVZ.BELEGNR]@ $[mJobHead]", true);

			HostObject.WriteLine(HostObject.EvalString(@"INITPAGESIZE"));
			HostObject.WriteLine(HostObject.EvalString(@"STARTJOB $[mJobHead]"));
			HostObject.WriteLine(HostObject.EvalString(@"TRANSPARENT"));
			SUB_Kopf();

			if (HostObject.EvalBool("$[bBAweise]")) // nach Bauabschnitten
				while (!HostObject.TestEof("recBA"))
				{
					SUB_loopBA();
					HostObject.SkipRecord("recBA");
				}
			else // nach Positionen
				while(!HostObject.TestEof("recPOS"))
				{
					SUB_loopBELEG();
					HostObject.SkipRecord("recPOS");
				}

			// --------------------------------------------------
			//  letzte Seite abschliessen
			SUB_Fuss();
			HostObject.WriteLine(HostObject.EvalString(@"ENDJOB"));

			return true;
		}

		bool SUB_CheckPage()
		{
			HostObject.VariablesPush();

			//  Fussbereich ausgeben, wenn letzte Zeile erreicht
			if (HostObject.EvalBool("$[Zeile] >= $[ZlMax]"))
				SUB_Fuss();
			//  Kopfbereich ausgeben, wenn erste Zeile erreicht
			if (HostObject.EvalBool("$[Zeile] = 0"))
				SUB_Kopf();

			return HostObject.VariablesPop();
		}

		bool SUB_Fuss()
		{
			HostObject.VariablesPush();

			// ========================= Fusszeile drucken ?
			if (HostObject.IsVar("mDrkFuss"))
				goto _mDrkFuss;
			HostObject.DefineVariable2("mDrkFuss", "N,2,0");
			HostObject.Set("mDrkFuss", @"$[ZlMax]", true);
			_mDrkFuss:

			HostObject.Set("Zeile", @"$[mDrkFuss]", true);
			if (HostObject.EvalBool("$[mDrkFuss] > 0"))
				HostObject.ProcessResource("FussBericht");
			HostObject.WriteLine(HostObject.EvalString(@"ENDPAGE"));

			HostObject.Set("Seite", @"$[Seite]+1", true);
			HostObject.Set("Zeile", @"0", true);

			return HostObject.VariablesPop();
		}

		bool SUB_subKopf1()
		{
			HostObject.VariablesPush();

			// ========================= Kopf Seite 1
			HostObject.Set("Zeile", @"0", true);

			// ========================= Logo Drucken ?
			if (!HostObject.IsVar("mDrkLogo1"))
				goto _mDrkLogoEnd;
			if (!HostObject.EvalBool("$[mDrkLogo1]"))
				goto _mDrkLogoEnd;
			SUB_subOutLogo();
			_mDrkLogoEnd:

			// ========================= Seitennummer drucken ?
			if (HostObject.IsVar("mDrkPgNo1"))
				goto _mDrkPgNo1;
			HostObject.DefineVariable2("mDrkPgNo1", "N,2,0");
			HostObject.Set("mDrkPgNo1", @"-1", true);
			_mDrkPgNo1:
			if (HostObject.EvalBool("$[mDrkPgNo1] < 0"))
				goto _xDrkPgNo1;
			HostObject.Set("Zeile", @"$[mDrkPgNo1]", true);
			HostObject.ProcessResource("outPgNo");
			_xDrkPgNo1:

			// ========================= Kopf drucken ?
			if (HostObject.IsVar("mDrkHdr1"))
				goto _mDrkHdr1;
			HostObject.DefineVariable2("mDrkHdr1", "N,2,0");
			HostObject.Set("mDrkHdr1", @"0", true);
			_mDrkHdr1:
			if (HostObject.EvalBool("$[mDrkHdr1] < 0"))
				goto _xDrkHdr1;
			HostObject.Set("Zeile", @"$[mDrkHdr1]", true);
			HostObject.ProcessResource("KopfBericht");
			_xDrkHdr1:

			// ========================= Absender drucken ?
			if (HostObject.IsVar("mDrkAbs1"))
				goto _mDrkAbs1;
			HostObject.DefineVariable2("mDrkAbs1", "N,2,0");
			HostObject.Set("mDrkAbs1", @"10", true);
			_mDrkAbs1:
			if (HostObject.EvalBool("$[mDrkAbs1] < 0"))
				goto _xDrkAbs1;
			HostObject.Set("Zeile", @"$[mDrkAbs1]", true);
			HostObject.ProcessResource("outAbsender");
			_xDrkAbs1:

			// ========================= Merkmale drucken ?
			if (HostObject.IsVar("mDrkMrk1"))
				goto _mDrkMrk1;
			HostObject.DefineVariable2("mDrkMrk1", "N,2,0");
			HostObject.Set("mDrkMrk1", @"11", true);
			_mDrkMrk1:
			if (HostObject.EvalBool("$[mDrkMrk1] < 0"))
				goto _xDrkMrk1;
			HostObject.Set("Zeile", @"$[mDrkMrk1]", true);
			HostObject.ProcessResource("outMerkmale1");
			_xDrkMrk1:

			// ========================= Anschrift1 drucken ?
			if (HostObject.IsVar("mDrkAdr1"))
				goto _mDrkAdr1;
			HostObject.DefineVariable2("mDrkAdr1", "N,2,0");
			HostObject.Set("mDrkAdr1", @"11", true);
			_mDrkAdr1:
			if (HostObject.EvalBool("$[mDrkAdr1] < 0"))
				goto _xDrkAdr1;
			HostObject.Set("Zeile", @"$[mDrkAdr1]", true);
			HostObject.ProcessResource("outAdresse1");
			_xDrkAdr1:

			// ========================= Detailbeginn Seite 1
			if (HostObject.IsVar("mZlStart1"))
				goto _mZlStart1;
			HostObject.DefineVariable2("mZlStart1", "N,2,0");
			HostObject.Set("mZlStart1", @"20", true);
			_mZlStart1:
			HostObject.Set("Zeile", @"$[mZlStart1]", true);


			HostObject.Set("adrZeile1", @"$[recADR2.Anrede]", true);
			HostObject.Set("adrZeile2", @"$[recADR2.Name1]", true);
			HostObject.Set("adrZeile3", @"$[recADR2.Name2]", true);
			HostObject.Set("adrZeile4", @"$[recADR2.Branche]", true);
			HostObject.Set("adrZeile5", @"$[recADR2.zuHaenden]", true);
			HostObject.Set("adrStrasse", @"$[recADR2.Strasse]", true);
			HostObject.Set("adrPlzOrt", @"$[recADR2.Plz] $[recADR2.Ort]", true);
			if (!HostObject.IsEmpty("recADR2.Vorname"))
				HostObject.Set("adrZeile2", @"$[recADR2.Vorname] $[adrZeile2]", true);
			SUB_subFmtAdr1();


			if (HostObject.EvalBool("$[mBauleiter]"))
				HostObject.ProcessResource("outAdresse2");

			return HostObject.VariablesPop();
		}

		bool SUB_subKopf2()
		{
			HostObject.VariablesPush();

			// ========================= Kopf Seite 2
			HostObject.Set("Zeile", @"0", true);

			// ========================= Logo Drucken ?
			if (!HostObject.IsVar("mDrkLogo2"))
				goto _mDrkLogoEnd;
			if (!HostObject.EvalBool("$[mDrkLogo2]"))
				goto _mDrkLogoEnd;
			SUB_subOutLogo();
			_mDrkLogoEnd:

			// ========================= Seitennummer drucken ?
			if (HostObject.IsVar("mDrkPgNo2"))
				goto _mDrkPgNo2;
			HostObject.DefineVariable2("mDrkPgNo2", "N,2,0");
			HostObject.Set("mDrkPgNo2", @"0", true);
			_mDrkPgNo2:
			if (HostObject.EvalBool("$[mDrkPgNo2] < 0"))
				goto _xDrkPgNo2;
			HostObject.Set("Zeile", @"$[mDrkPgNo2]", true);
			HostObject.ProcessResource("outPgNo");
			_xDrkPgNo2:

			// ========================= Kopf drucken ?
			if (HostObject.IsVar("mDrkHdr2"))
				goto _mDrkHdr2;
			HostObject.DefineVariable2("mDrkHdr2", "N,2,0");
			HostObject.Set("mDrkHdr2", @"0", true);
			_mDrkHdr2:
			if (HostObject.EvalBool("$[mDrkHdr2] < 0"))
				goto _xDrkHdr2;
			HostObject.Set("Zeile", @"$[mDrkHdr2]", true);
			HostObject.ProcessResource("KopfSeite");
			_xDrkHdr2:

			// ========================= Absender drucken ?
			if (HostObject.IsVar("mDrkAbs2"))
				goto _mDrkAbs2;
			HostObject.DefineVariable2("mDrkAbs2", "N,2,0");
			HostObject.Set("mDrkAbs2", @"10", true);
			_mDrkAbs2:
			if (HostObject.EvalBool("$[mDrkAbs2] < 0"))
				goto _xDrkAbs2;
			HostObject.Set("Zeile", @"$[mDrkAbs2]", true);
			HostObject.ProcessResource("outAbsender");
			_xDrkAbs2:

			// ========================= Merkmale drucken ?
			if (HostObject.IsVar("mDrkMrk2"))
				goto _mDrkMrk2;
			HostObject.DefineVariable2("mDrkMrk2", "N,2,0");
			HostObject.Set("mDrkMrk2", @"11", true);
			_mDrkMrk2:
			if (HostObject.EvalBool("$[mDrkMrk2] < 0"))
				goto _xDrkMrk2;
			HostObject.Set("Zeile", @"$[mDrkMrk2]", true);
			HostObject.ProcessResource("outMerkmale2");
			_xDrkMrk2:

			// ========================= Anschrift1 drucken ?
			if (HostObject.IsVar("mDrkAdr2"))
				goto _mDrkAdr2;
			HostObject.DefineVariable2("mDrkAdr2", "N,2,0");
			HostObject.Set("mDrkAdr2", @"11", true);
			_mDrkAdr2:
			if (HostObject.EvalBool("$[mDrkAdr2] < 0"))
				goto _xDrkAdr2;
			HostObject.Set("Zeile", @"$[mDrkAdr2]", true);
			HostObject.ProcessResource("outAdresse1");
			_xDrkAdr2:

			// ========================= Detailbeginn Seite 2
			if (HostObject.IsVar("mZlStart2"))
				goto _mZlStart2;
			HostObject.DefineVariable2("mZlStart2", "N,2,0");
			HostObject.Set("mZlStart2", @"20", true);
			_mZlStart2:
			HostObject.Set("Zeile", @"$[mZlStart2]", true);


			HostObject.Set("bNeedHdr", @"1", true);
			if (HostObject.EvalBool("$[mSumme]=0"))
				return HostObject.VariablesPop();
			// OUT ##Uebertrag

			return HostObject.VariablesPop();
		}

		bool SUB_subOutLogo()
		{
			HostObject.VariablesPush();

			//  Logo ausgeben laut Firmenstamm

			HostObject.DefineRecord("recLogo", "OPTIONEN", 0, 5, HostObject.EvalString(@" 7023"));
			if (HostObject.IsEmpty("recLogo.OptDataC"))
				return HostObject.VariablesPop();
			HostObject.DefineRecord("recLogoCX", "OPTIONEN", 0, 5, HostObject.EvalString(@" 7026"));
			if (HostObject.EvalBool("($[recLogoCX.OptDataD]) = (0)"))
				return HostObject.VariablesPop();
			HostObject.DefineRecord("recLogoCY", "OPTIONEN", 0, 5, HostObject.EvalString(@" 7027"));
			if (HostObject.EvalBool("($[recLogoCY.OptDataD]) = (0)"))
				return HostObject.VariablesPop();

			HostObject.DefineVariable2("mX2", "N,8,2");
			HostObject.DefineVariable2("mY2", "N,8,2");
			HostObject.DefineRecord("recLogoX", "OPTIONEN", 0, 5, HostObject.EvalString(@" 7024"));
			HostObject.DefineRecord("recLogoY", "OPTIONEN", 0, 5, HostObject.EvalString(@" 7025"));
			HostObject.Set("mX2", @"$[recLogoX.OptDataD] + $[recLogoCX.OptDataD]", true);
			HostObject.Set("mY2", @"$[recLogoY.OptDataD] + $[recLogoCY.OptDataD]", true);

			HostObject.WriteLine(HostObject.EvalString(@"BITMAP $[recLogoX.OptDataD];$[recLogoY.OptDataD];$[mX2];$[mY2];$[recLogo.OptDataC]"));

			return HostObject.VariablesPop();
		}

		bool SUB_Kopf()
		{
			HostObject.VariablesPush();

			//  naechste Seite anfangen
			HostObject.Set("Zeile", @"0", true);
			HostObject.WriteLine(HostObject.EvalString(@"STARTPAGE"));
			if (HostObject.EvalBool("$[Seite] = 1"))
				HostObject.WriteLine(HostObject.EvalString(@"DEVICE $[mDevice1]"));
			else
				HostObject.WriteLine(HostObject.EvalString(@"DEVICE $[mDevice2]"));
			HostObject.WriteLine(HostObject.EvalString(@"CALCBORDER"));

			if (HostObject.EvalBool("$[Seite] = 1"))
				SUB_subKopf1();
			else
				SUB_subKopf2();

			return HostObject.VariablesPop();
		}

		bool SUB_subFill()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			HostObject.DefineVariable2("mFillStart", "d,8,2");
			HostObject.DefineVariable2("mFillCY", "d,8,2");
			HostObject.Set("mFillStart", @" $[RandY] + ($[cyZeile] * $[Zeile])", true);
			HostObject.Set("mFillCY", @"$[cyZeile]", true);

			HostObject.WriteLine(HostObject.EvalString(@"RGBFILL $[RandX];$[mFillStart];$[SizeX];$[mFillCY];14211288"));
			return HostObject.VariablesPop();
		}

		bool SUB_subFmtAdr1()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			if (!HostObject.IsEmpty("adrZeile5"))
				goto Test4;
			HostObject.Set("adrZeile5", @"$[adrZeile4]", true);
			HostObject.Set("adrZeile4", @"$[adrZeile3]", true);
			HostObject.Set("adrZeile3", @"$[adrZeile2]", true);
			HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
			HostObject.Set("adrZeile1", @"", true);
			if (!HostObject.IsEmpty("adrZeile5"))
				goto Test4;
			HostObject.Set("adrZeile5", @"$[adrZeile4]", true);
			HostObject.Set("adrZeile4", @"$[adrZeile3]", true);
			HostObject.Set("adrZeile3", @"$[adrZeile2]", true);
			HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
			HostObject.Set("adrZeile1", @"", true);
			if (!HostObject.IsEmpty("adrZeile5"))
				goto Test4;
			HostObject.Set("adrZeile5", @"$[adrZeile4]", true);
			HostObject.Set("adrZeile4", @"$[adrZeile3]", true);
			HostObject.Set("adrZeile3", @"$[adrZeile2]", true);
			HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
			HostObject.Set("adrZeile1", @"", true);
			if (!HostObject.IsEmpty("adrZeile5"))
				goto Test4;
			HostObject.Set("adrZeile5", @"$[adrZeile4]", true);
			HostObject.Set("adrZeile4", @"$[adrZeile3]", true);
			HostObject.Set("adrZeile3", @"$[adrZeile2]", true);
			HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
			HostObject.Set("adrZeile1", @"", true);

			Test4:
			if (!HostObject.IsEmpty("adrZeile4"))
				goto Test3;
			HostObject.Set("adrZeile4", @"$[adrZeile3]", true);
			HostObject.Set("adrZeile3", @"$[adrZeile2]", true);
			HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
			HostObject.Set("adrZeile1", @"", true);
			if (!HostObject.IsEmpty("adrZeile4"))
				goto Test3;
			HostObject.Set("adrZeile4", @"$[adrZeile3]", true);
			HostObject.Set("adrZeile3", @"$[adrZeile2]", true);
			HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
			HostObject.Set("adrZeile1", @"", true);
			if (!HostObject.IsEmpty("adrZeile4"))
				goto Test3;
			HostObject.Set("adrZeile4", @"$[adrZeile3]", true);
			HostObject.Set("adrZeile3", @"$[adrZeile2]", true);
			HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
			HostObject.Set("adrZeile1", @"", true);

			Test3:
			if (!HostObject.IsEmpty("adrZeile3"))
				goto Test2;
			HostObject.Set("adrZeile3", @"$[adrZeile2]", true);
			HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
			HostObject.Set("adrZeile1", @"", true);
			if (!HostObject.IsEmpty("adrZeile3"))
				goto Test2;
			HostObject.Set("adrZeile3", @"$[adrZeile2]", true);
			HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
			HostObject.Set("adrZeile1", @"", true);

			Test2:
			if (!HostObject.IsEmpty("adrZeile2"))
				goto Test1;
			HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
			HostObject.Set("adrZeile1", @"", true);

			Test1:
			return HostObject.VariablesPop();
		}

		bool SUB_subT()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopBELEG
			SUB_CheckPage();
			HostObject.ProcessResource("outsubT");

			return HostObject.VariablesPop();
		}

		bool SUB_subChkPgH()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopBELEG/subH
			if (HostObject.EvalBool("$[Zeile] >= ($[ZlMax] - 5)"))
				SUB_Fuss();
			//  Kopfbereich ausgeben, wenn erste Zeile erreicht
			if (HostObject.EvalBool("$[Zeile] = 0"))
				SUB_Kopf();

			return HostObject.VariablesPop();
		}

		bool SUB_subH()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopBELEG
			SUB_subChkPgH();
			SUB_subChkHdr();
			if (HostObject.EvalBool("($[recPOS.SATZART]) = (1)"))
				return HostObject.VariablesPop();
			SUB_subFill();
			HostObject.ProcessResource("outsubH");

			return HostObject.VariablesPop();
		}

		bool SUB_subJ()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopBELEG
			//  Position oder Jumbo Drucken

			HostObject.Set("bDrkZube", @"0", true);

			if (HostObject.EvalBool("$[bAlle]"))
				goto EndFilter;

			HostObject.DefineRecord("recJMASS", "AUFMASSREFERENZ", 2, 24, HostObject.EvalString(@"$[FltKey<18]$[recPOS.REFKEY<6]"));
			if (HostObject.IsEmpty("recJMASS.AmRefKey"))
				return HostObject.VariablesPop();
			EndFilter:

			HostObject.Set("bDrkZube", @"1", true);

			//  ---------------------- 1. Zeile
			SUB_CheckPage();
			SUB_subChkHdr();
			if (HostObject.EvalBool("$[mMengen]"))
				HostObject.ProcessResource("outsubJEx");
			else
				HostObject.ProcessResource("outsubJ");

			//  ---------------------- 2. Zeile
			if (HostObject.IsEmpty("recPOS.KTXT2"))
				goto EndKtxt2;

			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outsubJ2");
			EndKtxt2:

			//  ---------------------- 3. Zeile
			if (HostObject.IsEmpty("recPOS.KTXT3"))
				goto EndKtxt3;

			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outsubJ3");
			EndKtxt3:


			//  ---------------------- Aufmass
			if (HostObject.EvalBool("$[bBAweise]"))
				goto EndAufmass;
			if (!HostObject.EvalBool("$[bAufmass]"))
				goto EndAufmass;

			SUB_subAufmass();

			EndAufmass:

			return HostObject.VariablesPop();
		}

		bool SUB_subU()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopBELEG
			//  Position oder Jumbo Drucken

			HostObject.Set("bDrkZube", @"0", true);

			if (!HostObject.EvalBool("$[bUnterpos]"))
				return HostObject.VariablesPop();

			if (HostObject.EvalBool("$[bAlle]"))
				goto EndFilter;

			HostObject.DefineRecord("recUMASS", "AUFMASSREFERENZ", 2, 24, HostObject.EvalString(@"$[FltKey<18]$[recPOS.REFKEY<6]"));
			if (HostObject.IsEmpty("recUMASS.AmRefKey"))
				return HostObject.VariablesPop();
			EndFilter:

			HostObject.Set("bDrkZube", @"1", true);

			//  ---------------------- 1. Zeile
			SUB_CheckPage();
			SUB_subChkHdr();
			if (HostObject.EvalBool("$[mMengen]"))
				HostObject.ProcessResource("outsubUEx");
			else
				HostObject.ProcessResource("outsubU");

			//  ---------------------- 2. Zeile
			if (HostObject.IsEmpty("recPOS.KTXT2"))
				goto EndKtxt2;

			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outsubU2");
			EndKtxt2:

			//  ---------------------- 3. Zeile
			if (HostObject.IsEmpty("recPOS.KTXT3"))
				goto EndKtxt3;

			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outsubU3");
			EndKtxt3:

			//  ---------------------- Aufmass
			if (HostObject.EvalBool("$[bAufmass]"))
				SUB_subAufmass();

			return HostObject.VariablesPop();
		}

		bool SUB_subZ()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopBELEG
			//  Position oder Jumbo Drucken

			if (!HostObject.EvalBool("$[bUnterpos]"))
				return HostObject.VariablesPop();
			if (!HostObject.EvalBool("$[bZubehoer]"))
				return HostObject.VariablesPop();
			if (!HostObject.EvalBool("$[bDrkZube]"))
				return HostObject.VariablesPop();

			//  ---------------------- 1. Zeile
			SUB_CheckPage();
			SUB_subChkHdr();
			if (HostObject.EvalBool("$[mMengen]"))
				HostObject.ProcessResource("outsubZEx");
			else
				HostObject.ProcessResource("outsubZ");

			//  ---------------------- 2. Zeile
			if (HostObject.IsEmpty("recPOS.KTXT2"))
				goto EndKtxt2;

			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outsubZ2");
			EndKtxt2:

			//  ---------------------- 3. Zeile
			if (HostObject.IsEmpty("recPOS.KTXT3"))
				goto EndKtxt3;

			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outsubZ3");
			EndKtxt3:

			return HostObject.VariablesPop();
		}

		bool SUB_loopTXT()
		{
			//  Datensatzschleife recTXT
			HostObject.VariablesPush();

			HostObject.DefineVariable2("cWert", "C,12,0");
			// Test, ob die Zeile mit einem = endet
			if (HostObject.EvalString("$[recTXT.DATEN>1]") == "=")
            {
				HostObject.Set("cWert", @"$[recTXT.WERT]", true);
				HostObject.Set("nCnt", @"$[nCnt] + 1", true);
			}
			HostObject.Set("mZeilen", @"$[mZeilen] + 1", true);

			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outMassText");

			return HostObject.VariablesPop();
		}

		bool SUB_loopMASS()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopBELEG/subAufmass
			//  Datensatzschleife recMASS
			HostObject.DefineVariable2("mZeilen", "N,10,0");
			HostObject.DefineVariable2("mTextDetails", "C,100,0");
			HostObject.Set("mZeilen", @"0", true);
			HostObject.Set("mTextDetails", @"", true);

			HostObject.DefineRecord("recREF", "BELEGMASSKETTENREFERENZ", 0, 24, HostObject.EvalString(@"$[FltKey<18]$[recMASS.AMREFKEY<6]"));
			HostObject.DefineRecord("recAUF", "BELAUFMASSE", 0, 22, HostObject.EvalString(@"$[FltKey<18]$[recREF.AmNr>4]"));
			HostObject.DefineRecord("recTXT", "TMPMASSTEXT", 0, 24, HostObject.EvalString(@"$[FltKey<18]$[recMASS.AMREFKEY<6]"));

			if (HostObject.EvalBool("($[mAufmass]) = (0)"))
				goto EndFilter;

			if (!HostObject.EvalBool("($[mAufmass]) = ($[recAUF.AmNr])"))
				return HostObject.VariablesPop();

			EndFilter:


			if (!HostObject.EvalBool("$[mMassDet]"))
				goto EndeDetails;

			if (!HostObject.EvalBool("$[mNummer]"))
				goto EndNummer;
			if (HostObject.IsEmpty("mTextDetails"))
				HostObject.Set("mTextDetails", @"Aufm:", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]  Aufm:", true);
			if (HostObject.IsEmpty("recREF.AmNr"))
				HostObject.Set("mTextDetails", @"$[mTextDetails]-/-", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]$[recREF.AmNr]", true);
			if (!HostObject.IsEmpty("recAUF.Bez1"))
				HostObject.Set("mTextDetails", @"$[mTextDetails]-$[recAUF.Bez1]", true);
			EndNummer:


			if (!HostObject.EvalBool("$[mDatum]"))
				goto EndDatum;
			if (HostObject.IsEmpty("mTextDetails"))
				HostObject.Set("mTextDetails", @"Dat:", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails] /", true);
			if (HostObject.CompareString("recMASS.Datum", "=", HostObject.EvalString(@"00.00.0000")))
				HostObject.Set("mTextDetails", @"$[mTextDetails] $[recAUF.Datum]", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails] $[recMASS.Datum]", true);
			EndDatum:


			if (!HostObject.EvalBool("$[mBA]"))
				goto EndBA;
			if (HostObject.IsEmpty("mTextDetails"))
				HostObject.Set("mTextDetails", @"BA:", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]  BA:", true);
			if (HostObject.IsEmpty("recMASS.BaRef"))
				HostObject.Set("mTextDetails", @"$[mTextDetails]-/-", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]$[recMASS.BaRef]", true);
			EndBA:


			if (!HostObject.EvalBool("$[mRaum]"))
				goto EndRaum;
			if (HostObject.IsEmpty("mTextDetails"))
				HostObject.Set("mTextDetails", @"R:", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]  R:", true);
			if (HostObject.IsEmpty("recREF.RaumNr"))
				HostObject.Set("mTextDetails", @"$[mTextDetails]-/-", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]$[recREF.RaumNr]", true);
			EndRaum:


			if (!HostObject.EvalBool("$[mSpalte]"))
				goto EndSpalte;
			if (HostObject.IsEmpty("mTextDetails"))
				HostObject.Set("mTextDetails", @"B/S:", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]  B/S:", true);
			if (HostObject.IsEmpty("recREF.Blatt"))
				HostObject.Set("mTextDetails", @"$[mTextDetails]-", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]$[recREF.Blatt]", true);
			HostObject.Set("mTextDetails", @"$[mTextDetails]/", true);
			if (HostObject.IsEmpty("recREF.Spalte"))
				HostObject.Set("mTextDetails", @"$[mTextDetails]-", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]$[recREF.Spalte]", true);
			EndSpalte:


			if (!HostObject.EvalBool("$[mREBAddr]"))
				goto EndREBAddr;
			if (HostObject.IsEmpty("recREF.REBStart"))
				goto EndREBAddr;
			if (HostObject.IsEmpty("mTextDetails"))
				HostObject.Set("mTextDetails", @"REB", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]  REB", true);
			HostObject.Set("mTextDetails", @"$[mTextDetails] $[recREF.REBStart]..$[recREF.REBSumme]", true);
			EndREBAddr:


			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outMassBA");
			EndeDetails:

			HostObject.Set("nCnt", @"0", true);

			//  -------------------------Aufmasslangtext
			if (HostObject.EvalBool("$[mKurz]"))
				goto EndeMassText;
			while(!HostObject.TestEof("recTXT"))
			{
				SUB_loopTXT();
				HostObject.SkipRecord("recTXT");
			}

			if (HostObject.EvalBool("$[nCnt] > 1"))
				goto MassMenge1;
			else
				goto MassMenge2;

			MassMenge1:
			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outMassMenge1");
			goto EndeMassMenge;

			MassMenge2:
			HostObject.Set("Zeile", @"$[Zeile] - 1", true);
			HostObject.ProcessResource("outMassMenge");
			goto EndeMassMenge;

			EndeMassText:

			//  -------------------------Aufmassmenge
			if (!HostObject.EvalBool("$[mMassDet]"))
				goto EndeMassMenge;
			HostObject.Set("Zeile", @"$[Zeile] - 1", true);
			HostObject.ProcessResource("outMassMenge");
			EndeMassMenge:

			HostObject.Set("mMassAnzahl", @"$[mMassAnzahl] + 1", true);
			HostObject.Set("mMassSumme", @"$[mMassSumme] + $[recREF.WERT]", true);

			return HostObject.VariablesPop();
		}

		bool SUB_subAufmass()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopBELEG
			HostObject.DefineVariable2("mMassAnzahl", "N,10,0");
			HostObject.DefineVariable2("mMassSumme", "N,10,3");
			HostObject.Set("mMassAnzahl", @"0", true);
			HostObject.Set("mMassSumme", @"0", true);

			HostObject.DefineRecord("recMASS", "AUFMASSREFERENZ", 2, 24, HostObject.EvalString(@"$[FltKey<18]$[recPOS.REFKEY<6]"));
			while(!HostObject.TestEof("recMASS"))
			{
				SUB_loopMASS();
				HostObject.SkipRecord("recMASS");
			}

			if (!HostObject.EvalBool("$[mKurz]"))
				goto EndeMassKurz;
			if (HostObject.EvalBool("$[mMassDet]"))
				goto EndeMassKurz;
			HostObject.Set("Zeile", @"$[Zeile] - 1", true);
			HostObject.ProcessResource("outMassSumme2");
			goto Ende;
			EndeMassKurz:

			if (!HostObject.EvalBool("$[mMassAnzahl] > 1"))
				goto EndeMassSumme;
			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outMassSumme");
			EndeMassSumme:

			Ende:
			return HostObject.VariablesPop();
		}


		bool SUB_loopBELEG()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			//  Datensatzschleife recPOS

			if (HostObject.CompareString("recPOS.VKZ", "=", HostObject.EvalString(@"H")))
				SUB_subH();
			if (HostObject.CompareString("recPOS.VKZ", "=", HostObject.EvalString(@"J")))
				SUB_subJ();
			if (HostObject.CompareString("recPOS.VKZ", "=", HostObject.EvalString(@"P")))
				SUB_subJ();
			if (HostObject.CompareString("recPOS.VKZ", "=", HostObject.EvalString(@"U")))
				SUB_subU();
			if (HostObject.CompareString("recPOS.VKZ", "=", HostObject.EvalString(@"Z")))
				SUB_subZ();

			return HostObject.VariablesPop();
		}

		bool SUB_subChkHdr()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			if (!HostObject.EvalBool("$[bNeedHdr]"))
				return HostObject.VariablesPop();
			if (HostObject.EvalBool("$[Zeile] >= ($[ZlMax] - 10)"))
				SUB_Fuss();
			//  Kopfbereich ausgeben, wenn erste Zeile erreicht
			if (HostObject.EvalBool("$[Zeile] = 0"))
				SUB_Kopf();
			HostObject.ProcessResource("KopfDetail");
			HostObject.Set("bNeedHdr", @" 0", true);

			return HostObject.VariablesPop();
		}

		bool SUB_loopTXT1()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopBA/loopMASS1/loopREF1/loopAMREF
			//  Datensatzschleife recTXT1

			HostObject.DefineVariable2("cWert", "C,10,0");
			HostObject.Set("cWert", @"", true);

			if (!HostObject.CompareString("recTXT.WERT", "=", HostObject.EvalString(@"0.000")))
				HostObject.Set("cWert", @"$[recTXT.WERT]", true);

			HostObject.Set("cTmp", @"$[recTXT.DATEN>1]", true);
			if (HostObject.CompareString("cTmp", "=", HostObject.EvalString(@"=")))
				HostObject.Set("nCnt", @"$[nCnt] + 1", true);

			HostObject.Set("mZeilen", @"$[mZeilen] + 1", true);

			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outMassText");

			return HostObject.VariablesPop();
		}

		bool SUB_loopAMREF()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopBA/loopMASS1/loopREF1
			//  Datensatzschleife recREF

			if (!HostObject.EvalBool("($[recREF.AmNr]) = ($[recAUF.AmNr])"))
				return HostObject.VariablesPop();

			HostObject.DefineRecord("recPOS", "TMPKALKLIST", 3, 24, HostObject.EvalString(@"$[FltKey<18]$[recMASS.PosRefKey<6]"));
			while(!HostObject.TestEof("recPOS"))
			{
				SUB_loopBELEG();
				HostObject.SkipRecord("recPOS");
			}

			HostObject.DefineVariable2("mZeilen", "N,10,0");
			HostObject.DefineVariable2("mTextDetails", "C,100,0");
			HostObject.Set("mZeilen", @"0", true);
			HostObject.Set("mTextDetails", @"", true);

			HostObject.DefineRecord("recTXT", "TMPMASSTEXT", 0, 24, HostObject.EvalString(@"$[FltKey<18]$[recREF.AMREFKEY<6]"));

			if (!HostObject.EvalBool("$[mMassDet]"))
				goto EndeDetails;

			if (!HostObject.EvalBool("$[mNummer]"))
				goto EndNummer;
			if (HostObject.IsEmpty("mTextDetails"))
				HostObject.Set("mTextDetails", @"Aufm:", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]  Aufm:", true);
			if (HostObject.IsEmpty("recREF.AmNr"))
				HostObject.Set("mTextDetails", @"$[mTextDetails]-/-", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]$[recREF.AmNr]", true);
			if (!HostObject.IsEmpty("recAUF.Bez1"))
				HostObject.Set("mTextDetails", @"$[mTextDetails]-$[recAUF.Bez1]", true);
			EndNummer:


			if (!HostObject.EvalBool("$[mDatum]"))
				goto EndDatum;
			if (HostObject.IsEmpty("mTextDetails"))
				HostObject.Set("mTextDetails", @"Dat:", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails] /", true);
			if (HostObject.CompareString("recMASS.Datum", "=", HostObject.EvalString(@"00.00.0000")))
				HostObject.Set("mTextDetails", @"$[mTextDetails] $[recAUF.Datum]", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails] $[recMASS.Datum]", true);
			EndDatum:


			if (!HostObject.EvalBool("$[mBA]"))
				goto EndBA;
			if (HostObject.IsEmpty("mTextDetails"))
				HostObject.Set("mTextDetails", @"BA:", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]  BA:", true);
			if (HostObject.IsEmpty("recMASS.BaRef"))
				HostObject.Set("mTextDetails", @"$[mTextDetails]-/-", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]$[recMASS.BaRef]", true);
			EndBA:


			if (!HostObject.EvalBool("$[mRaum]"))
				goto EndRaum;
			if (HostObject.IsEmpty("mTextDetails"))
				HostObject.Set("mTextDetails", @"R:", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]  R:", true);
			if (HostObject.IsEmpty("recREF.RaumNr"))
				HostObject.Set("mTextDetails", @"$[mTextDetails]-/-", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]$[recREF.RaumNr]", true);
			EndRaum:


			if (!HostObject.EvalBool("$[mSpalte]"))
				goto EndSpalte;
			if (HostObject.IsEmpty("mTextDetails"))
				HostObject.Set("mTextDetails", @"B/S:", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]  B/S:", true);
			if (HostObject.IsEmpty("recREF.Blatt"))
				HostObject.Set("mTextDetails", @"$[mTextDetails]-", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]$[recREF.Blatt]", true);
			HostObject.Set("mTextDetails", @"$[mTextDetails]/", true);
			if (HostObject.IsEmpty("recREF.Spalte"))
				HostObject.Set("mTextDetails", @"$[mTextDetails]-", true);
			else
				HostObject.Set("mTextDetails", @"$[mTextDetails]$[recREF.Spalte]", true);
			EndSpalte:


			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outMassBA");
			EndeDetails:

			HostObject.Set("nCnt", @"0", true);

			//  -------------------------Aufmasslangtext
			if (HostObject.EvalBool("$[mKurz]"))
				goto EndeMassText;
			while(!HostObject.TestEof("recTXT"))
			{
				SUB_loopTXT1();
				HostObject.SkipRecord("recTXT");
			}


			if (HostObject.EvalBool("$[nCnt] > 1"))
				goto MassMenge1;
			else
				goto MassMenge2;

			MassMenge1:
			HostObject.ProcessResource("outMassMenge1");
			goto EndeMassMenge;

			MassMenge2:
			HostObject.Set("Zeile", @"$[Zeile] - 1", true);
			HostObject.ProcessResource("outMassMenge");
			goto EndeMassMenge;

			EndeMassText:

			//  -------------------------Aufmassmenge
			if (!HostObject.EvalBool("$[mMassDet]"))
				goto EndeMassMenge;
			HostObject.Set("Zeile", @"$[Zeile] - 1", true);
			HostObject.ProcessResource("outMassMenge");
			EndeMassMenge:

			HostObject.Set("mMassAnzahl", @"$[mMassAnzahl] + 1", true);
			HostObject.Set("mMassSumme", @"$[mMassSumme] + $[recREF.WERT]", true);

			return HostObject.VariablesPop();
		}

		bool SUB_loopREF1()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopBA/loopMASS1
			//  Datensatzschleife recMASS

			HostObject.DefineRecord("recREF", "BELEGMASSKETTENREFERENZ", 0, 24, HostObject.EvalString(@"$[FltKey<18]$[recMASS.AmRefKey<6]"));
			while(!HostObject.TestEof("recREF"))
			{
				SUB_loopAMREF();
				HostObject.SkipRecord("recREF");
			}

			return HostObject.VariablesPop();
		}

		bool SUB_loopMASS1()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopBA
			//  Datensatzschleife recAUF

			if (HostObject.EvalBool("($[mAufmass]) = (0)"))
				goto EndFilter;

			if (!HostObject.EvalBool("($[mAufmass]) = ($[recAUF.AmNr])"))
				return HostObject.VariablesPop();

			EndFilter:

			HostObject.DefineVariable2("mMassAnzahl", "N,10,0");
			HostObject.DefineVariable2("mMassSumme", "N,10,3");
			HostObject.Set("mMassAnzahl", @"0", true);
			HostObject.Set("mMassSumme", @"0", true);

			HostObject.DefineRecord("recMASS", "AUFMASSREFERENZ", 3, 38, HostObject.EvalString(@"$[FltKey<18]$[recBA.BaRef<20]"));
			while(!HostObject.TestEof("recMASS"))
			{
				SUB_loopREF1();
				HostObject.SkipRecord("recMASS");
			}

			return HostObject.VariablesPop();

			/*
			if (!HostObject.EvalBool("$[mKurz]"))
				goto EndeMassKurz;
			if (HostObject.EvalBool("$[mMassDet]"))
				goto EndeMassKurz;
			HostObject.Set("Zeile", @"$[Zeile] - 1", true);
			HostObject.ProcessResource("outMassSumme2");
			goto Ende;
			EndeMassKurz:

			if (!HostObject.EvalBool("$[mMassAnzahl] > 1"))
				goto EndeMassSumme;
			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outMassSumme");
			EndeMassSumme:

			Ende:

			return HostObject.VariablesPop();
			*/
		}

		bool SUB_loopBA()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			//  Datensatzschleife recBA

			SUB_subChkPgH();
			SUB_subChkHdr();
			SUB_subFill();
			HostObject.ProcessResource("outBA");

			HostObject.DefineRecord("recAUF", "BELAUFMASSE", 0, 18, HostObject.EvalString(@"$[FltKey<18]"));
			while(!HostObject.TestEof("recAUF"))
			{
				SUB_loopMASS1();
				HostObject.SkipRecord("recAUF");
			}

			return HostObject.VariablesPop();
		}
	}
}
