using craftsReport.Interfaces;

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

		public rp0456_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
			if (!HostObject.IsVar("FltKey"))
				HostObject.DefineVariable2("FltKey", "u,18,0");
			if (!HostObject.IsVar("cFltRaum"))
				HostObject.DefineVariable2("cFltRaum", "u,6,0");
			if (!HostObject.IsVar("nVonMass"))
				HostObject.DefineVariable2("nVonMass", "N,4,0");
			if (!HostObject.IsVar("nBisMass"))
				HostObject.DefineVariable2("nBisMass", "N,4,0");
			if (!HostObject.IsVar("bMitPos"))
				HostObject.DefineVariable2("bMitPos", "L,1,0");
			if (!HostObject.IsVar("bFormFeed"))
				HostObject.DefineVariable2("bFormFeed", "L,1,0");

			HostObject.DefineVariable2("cNoRaum", "u,6,0");
			if (HostObject.IsEmpty("cFltRaum"))
				HostObject.DefineRecord("recRaum", "BELRAUM", 1, 24, HostObject.EvalString(@"$[FltKey<18]$[cNoRaum<6]"));
			else
				HostObject.DefineRecord("recRaum", "BELRAUM", 1, 30, HostObject.EvalString(@"$[FltKey<18]$[cNoRaum<6]$[cFltRaum<6]"));

			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"));

			//  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-dRaumbuch $[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

			while(!HostObject.TestEof("recRaum"))
			{
				SUB_loopRaum();
				HostObject.SkipRecord("recRaum");
			}

			// --------------------------------------------------
			//  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();
		}

		// Adresse nach unten verschieben
		bool SUB_subFmtAdr1()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain

			for (int i5 = 0; i5 < 4; i5++)
			{
				if (HostObject.IsEmpty("adrZeile5"))
				{
					HostObject.Set("adrZeile5", @"$[adrZeile4]", true);
					HostObject.Set("adrZeile4", @"$[adrZeile3]", true);
					HostObject.Set("adrZeile3", @"$[adrZeile2]", true);
					HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
					HostObject.Set("adrZeile1", @"", true);
				}
			}
			for (int i4 = 0; i4 < 3; i4++)
			{
				if (HostObject.IsEmpty("adrZeile4"))
				{
					HostObject.Set("adrZeile4", @"$[adrZeile3]", true);
					HostObject.Set("adrZeile3", @"$[adrZeile2]", true);
					HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
					HostObject.Set("adrZeile1", @"", true);
				}
			}
			for (int i3 = 0; i3 < 2; i3++)
			{
				if (HostObject.IsEmpty("adrZeile3"))
				{
					HostObject.Set("adrZeile3", @"$[adrZeile2]", true);
					HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
					HostObject.Set("adrZeile1", @"", true);
				}
			}
			if (HostObject.IsEmpty("adrZeile2"))
			{
				HostObject.Set("adrZeile2", @"$[adrZeile1]", true);
				HostObject.Set("adrZeile1", @"", true);
			}

			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_loopTestMass()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopRaum
			//  Datensatzschleife recTestMass
			//  Teste, ob Aufmaßnummer stimmt
			if (HostObject.EvalBool("($[recTestMass.AmNr]) < ($[nVonMass])"))
				return HostObject.VariablesPop();
			if (HostObject.EvalBool("($[recTestMass.AmNr]) > ($[nBisMass])"))
				return HostObject.VariablesPop();
			HostObject.Set("mHasData", @"1", true);
			HostObject.BreakRecord("recTestMass");

			return HostObject.VariablesPop();
		}

		bool SUB_loopMassText()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopRaum/loopMasskette
			//  Datensatzschleife recMassText

			if (HostObject.EvalBool("$[recMassText.HASVALUE]"))
				HostObject.Set("cMassGehalt", @"$[recMassText.VALUE]", true);
			else
				HostObject.Set("cMassGehalt", @"", true);
			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outloopMassTex");

			return HostObject.VariablesPop();
		}

		bool SUB_loopPosRef()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopRaum/loopMasskette
			//  Datensatzschleife recPosRef
			HostObject.DefineRecord("recBelPos", "TMPBELTREE", 3, 24, HostObject.EvalString(@"$[FltKey<18]$[recPosRef.PosRefKey]"));
			HostObject.DefineRecord("recBauAb", "BELBAUABSCHNITTE", 0, 38, HostObject.EvalString(@"$[FltKey<18]$[recPosRef.BaRef<20]"));
			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outloopPosRef");

			return HostObject.VariablesPop();
		}

		bool SUB_loopMasskette()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopRaum
			//  Datensatzschleife recMass
			if (HostObject.EvalBool("($[recMass.AmNr]) < ($[nVonMass])"))
				return HostObject.VariablesPop();
			if (HostObject.EvalBool("($[recMass.AmNr]) > ($[nBisMass])"))
				return HostObject.VariablesPop();

			HostObject.DefineRecord("recBauteil", "BELRAUM", 0, 30, HostObject.EvalString(@"$[FltKey<18]$[recMass.RaumNr<6]$[recMass.Bauteil<6]"));
			HostObject.DefineRecord("recMassText", "TMPBELTXTLNS", 0, 25, HostObject.EvalString(@"$[FltKey<18]9$[recMass.AmRefKey<6]"));
			HostObject.DefineVariable2("cMassGehalt", "C,12,0");
			if (HostObject.EvalBool("$[recMassText.HASVALUE]"))
				HostObject.Set("cMassGehalt", @"$[recMassText.VALUE]", true);
			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outloopMassket");
			HostObject.SkipRecord("recMassText");
			while(!HostObject.TestEof("recMassText"))
			{
				SUB_loopMassText();
				HostObject.SkipRecord("recMassText");
			}

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

			HostObject.DefineRecord("recPosRef", "AUFMASSREFERENZ", 1, 24, HostObject.EvalString(@"$[FltKey<18]$[recMass.AmRefKey<6]"));
			while(!HostObject.TestEof("recPosRef"))
			{
				SUB_loopPosRef();
				HostObject.SkipRecord("recPosRef");
			}

			return HostObject.VariablesPop();
		}

		bool SUB_loopRaum()
		{
			HostObject.VariablesPush();

			//  Datensatzschleife recRaum
			//  Test, ob im Raum aufgemessen wurde
			HostObject.DefineVariable2("mHasData", "L,1,0");
			HostObject.DefineRecord("recTestMass", "BELEGMASSKETTENREFERENZ", 3, 24, HostObject.EvalString(@"$[FltKey<18]$[recRaum.RaumNr<6]"));
			while(!HostObject.TestEof("recTestMass"))
			{
				SUB_loopTestMass();
				HostObject.SkipRecord("recTestMass");
			}
			if (!HostObject.EvalBool("$[mHasData]"))
				return HostObject.VariablesPop();

			HostObject.DefineVariable2("cRaumBez", "C,200,0");
			if (HostObject.IsEmpty("recRaum.Bez1"))
				HostObject.Set("cRaumBez", @"(keine Bezeichnung)", true);
			else
				HostObject.Set("cRaumBez", @"$[recRaum.Bez1]", true);

			if (!HostObject.IsEmpty("recRaum.RaumNr"))
				HostObject.Set("cRaumBez", @"$[recRaum.RaumNr] $[cRaumBez]", true);

			if (HostObject.EvalBool("$[bFormFeed] & ($[bNeedHdr] = 0)"))
				SUB_Fuss();
			SUB_CheckPage();
			SUB_subChkHdr();
			HostObject.ProcessResource("outloopRaum");
			HostObject.DefineRecord("recMass", "BELEGMASSKETTENREFERENZ", 3, 24, HostObject.EvalString(@"$[FltKey<18]$[recRaum.RaumNr<6]"));
			while(!HostObject.TestEof("recMass"))
			{
				SUB_loopMasskette();
				HostObject.SkipRecord("recMass");
			}

			return HostObject.VariablesPop();
		}
	}
}
