using craftsReport.Interfaces;

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

		public rp0120_xbs()
		{
		}


		// Auskunft_nach_DSGVO
		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.Set("Seite", @"1", true);
			HostObject.Set("Zeile", @"0", true);
			HostObject.Set("Heute", @"H", true);

			//  Anzahl Zeilen, die CheckPage prüft
			HostObject.DefineVariable2("SpaceToChk", "N,2,0");
			HostObject.Set("SpaceToChk", @"1", true);
			HostObject.DefineVariable2("HeaderType", "N,2,0");
			HostObject.Set("HeaderType", @"0", true);
			HostObject.DefineVariable2("LastHeader", "N,2,0");
			HostObject.Set("LastHeader", @"-1", true);

			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.DefineRecord("recEIG", "EIGENE_ANSCHRIFTEN", 0, 2, HostObject.EvalString(@"01"));
			HostObject.DefineRecord("recSTEURNR", "OPTIONEN", 0, 5, HostObject.EvalString(@" 6110"));
			HostObject.DefineRecord("recUSTIDNR", "OPTIONEN", 0, 5, HostObject.EvalString(@" 6112"));
			HostObject.DefineRecord("recWKZ", "WÄHRUNGEN", 0, 6, HostObject.EvalString(@"EUR "));
			HostObject.DefineRecord("recIBAN1", "OPTIONEN", 0, 5, HostObject.EvalString(@" 5221  "));
			HostObject.DefineRecord("recIBAN2", "OPTIONEN", 0, 5, HostObject.EvalString(@" 5223  "));
			//  Geschäftsführer, Behörde, Register-Nr.
			HostObject.DefineRecord("recGF5215", "OPTIONEN", 0, 5, HostObject.EvalString(@" 5215  "));
			HostObject.DefineRecord("recAG5217", "OPTIONEN", 0, 5, HostObject.EvalString(@" 5217  "));
			HostObject.DefineRecord("recHR5216", "OPTIONEN", 0, 5, HostObject.EvalString(@" 5216  "));

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

			HostObject.DefineRecord("recDest", "ADRESSSTAMM", 0, 15, HostObject.EvalString(@"$[mNo<15]"));
			HostObject.DefineVariable2("mAdrZeile1", "C,50,0");
			HostObject.DefineVariable2("mAdrZeile2", "C,50,0");
			HostObject.DefineVariable2("mAdrZeile3", "C,50,0");
			HostObject.DefineVariable2("mAdrZeile4", "C,50,0");
			HostObject.DefineVariable2("mAdrZeile5", "C,50,0");
			HostObject.DefineVariable2("mAdrZeile6", "C,50,0");
			HostObject.DefineVariable2("mAdrZeile7", "C,50,0");
			SUB_subAdressFmt();

			// --------------------------------------------------
			//  Erste Seite initialisieren

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

			//  -------------------------------------------------
			//  Details
			HostObject.ProcessResource("outBetreff");
			if (!HostObject.IsEmpty("recDest.BrAnrede"))
				HostObject.ProcessResource("outBrAnrede");
			HostObject.ProcessResource("outVortext");

			//  -------------------------------------------------
			//  Ansprechpartner
			SUB_subPartner();

			//  -------------------------------------------------
			//  Anlagendaten
			SUB_subAnlagen();

			//  -------------------------------------------------
			//  Standardbelege
			SUB_subBelege();

			//  -------------------------------------------------
			//  Wartungsbelege
			SUB_subWartung();

			//  -------------------------------------------------
			//  Monteurberichte
			SUB_subMBerichte();

			// --------------------------------------------------
			//  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] + (1 - $[SpaceToChk]))"))
				SUB_Fuss();
			//  Kopfbereich ausgeben, wenn erste Zeile erreicht
			if (HostObject.EvalBool("$[Zeile] = 0"))
				SUB_Kopf();

			//  reset space
			HostObject.Set("SpaceToChk", @"1", true);

			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);
			HostObject.Set("LastHeader", @"-1", true);

			return HostObject.VariablesPop();
		}


		bool SUB_subKopf1()
		{
			HostObject.VariablesPush();

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

			// ========================= Logo Drucken ?
			if (!HostObject.IsVar("bLogo1"))
				goto _mDrkLogoEnd;
			if (!HostObject.EvalBool("$[bLogo1]"))
				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);
			// ***************** OUT ##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);


			return HostObject.VariablesPop();
		}

		bool SUB_subKopf2()
		{
			HostObject.VariablesPush();

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

			// ========================= Logo Drucken ?
			if (!HostObject.IsVar("bLogo2"))
				goto _mDrkLogoEnd;
			if (!HostObject.EvalBool("$[bLogo2]"))
				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);
			// ********************** OUT ##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);
			// ******************************** OUT ##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.ProcessResource("outBetreff");

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

			//  Unterprogramm von ReportMain/Kopf

			if (HostObject.EvalBool("($[HeaderType]) = (0)"))
				return HostObject.VariablesPop();

			HostObject.Set("SpaceToChk", @"7", true);
			SUB_CheckPage();
			if (HostObject.EvalBool("($[HeaderType]) = ($[LastHeader])"))
				return HostObject.VariablesPop();

			if (HostObject.EvalBool("($[HeaderType]) = (1)"))
				HostObject.ProcessResource("outHeader1");
			if (HostObject.EvalBool("($[HeaderType]) = (2)"))
				HostObject.ProcessResource("outHeader2");
			if (HostObject.EvalBool("($[HeaderType]) = (3)"))
				HostObject.ProcessResource("outHeader3");
			if (HostObject.EvalBool("($[HeaderType]) = (4)"))
				HostObject.ProcessResource("outHeader4");
			if (HostObject.EvalBool("($[HeaderType]) = (5)"))
				HostObject.ProcessResource("outHeader5");

			HostObject.Set("LastHeader", @"$[HeaderType]", true);
			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();

			SUB_subHeader();

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

			//  Leere Zeilen zusammenrücken

			if (!HostObject.IsEmpty("mAdrZeile1"))
				goto X1;
			HostObject.Set("mAdrZeile1", @"$[mAdrZeile2]", true);
			HostObject.Set("mAdrZeile2", @"$[mAdrZeile3]", true);
			HostObject.Set("mAdrZeile3", @"$[mAdrZeile4]", true);
			HostObject.Set("mAdrZeile4", @"$[mAdrZeile5]", true);
			HostObject.Set("mAdrZeile5", @"$[mAdrZeile6]", true);
			HostObject.Set("mAdrZeile6", @"$[mAdrZeile7]", true);
			HostObject.Set("mAdrZeile7", @"", true);
			X1:

			if (!HostObject.IsEmpty("mAdrZeile2"))
				goto X2;
			HostObject.Set("mAdrZeile2", @"$[mAdrZeile3]", true);
			HostObject.Set("mAdrZeile3", @"$[mAdrZeile4]", true);
			HostObject.Set("mAdrZeile4", @"$[mAdrZeile5]", true);
			HostObject.Set("mAdrZeile5", @"$[mAdrZeile6]", true);
			HostObject.Set("mAdrZeile6", @"$[mAdrZeile7]", true);
			HostObject.Set("mAdrZeile7", @"", true);
			X2:

			if (!HostObject.IsEmpty("mAdrZeile3"))
				goto X3;
			HostObject.Set("mAdrZeile3", @"$[mAdrZeile4]", true);
			HostObject.Set("mAdrZeile4", @"$[mAdrZeile5]", true);
			HostObject.Set("mAdrZeile5", @"$[mAdrZeile6]", true);
			HostObject.Set("mAdrZeile6", @"$[mAdrZeile7]", true);
			HostObject.Set("mAdrZeile7", @"", true);
			X3:

			if (!HostObject.IsEmpty("mAdrZeile4"))
				goto X4;
			HostObject.Set("mAdrZeile4", @"$[mAdrZeile5]", true);
			HostObject.Set("mAdrZeile5", @"$[mAdrZeile6]", true);
			HostObject.Set("mAdrZeile6", @"$[mAdrZeile7]", true);
			HostObject.Set("mAdrZeile7", @"", true);
			X4:

			if (!HostObject.IsEmpty("mAdrZeile5"))
				goto X5;
			HostObject.Set("mAdrZeile5", @"$[mAdrZeile6]", true);
			HostObject.Set("mAdrZeile6", @"$[mAdrZeile7]", true);
			HostObject.Set("mAdrZeile7", @"", true);
			X5:

			if (!HostObject.IsEmpty("mAdrZeile6"))
				goto X6;
			HostObject.Set("mAdrZeile6", @"$[mAdrZeile7]", true);
			HostObject.Set("mAdrZeile7", @"", true);
			X6:

			return HostObject.VariablesPop();
		}

		bool SUB_subAdressFmt()
		{
			HostObject.VariablesPush();

			//  subAdressFmt
			//  Unterprogramm von ReportMain

			//  letzte zwei Stellen der Postleitzahl abschneiden
			HostObject.DefineVariable2("mPlz", "C,8,0");
			HostObject.Set("mPlz", @"$[recDest.Plz]", true);
			HostObject.DefineVariable2("mVorname", "C,50,0");
			HostObject.Set("mVorname", @"$[recDest.VorName]", true);

			HostObject.Set("mAdrZeile1", @"$[recDest.Name1]", true);
			HostObject.Set("mAdrZeile2", @"$[recDest.Name2]", true);
			HostObject.Set("mAdrZeile3", @"$[recDest.Branche]", true);
			HostObject.Set("mAdrZeile4", @"$[recDest.zuHaenden]", true);
			HostObject.Set("mAdrZeile5", @"$[recDest.Strasse]", true);
			HostObject.Set("mAdrZeile6", @"$[mPlz] $[recDest.Ort]", true);
			HostObject.Set("mAdrZeile7", @"$[recDest.LandName]", true);

			if (!HostObject.IsEmpty("recDest.VorName"))
				HostObject.Set("mAdrZeile1", @"$[recDest.VorName] $[mAdrZeile1]", true);

			SUB_subAdrVerdich();
			SUB_subAdrVerdich();
			SUB_subAdrVerdich();
			SUB_subAdrVerdich();

			//  Anrede hinzufügen ?
			if (!HostObject.IsEmpty("mAdrZeile7"))
				goto X7;
			if (HostObject.IsEmpty("recDest.Anrede"))
				goto X7;

			HostObject.Set("mAdrZeile7", @"$[mAdrZeile6]", true);
			HostObject.Set("mAdrZeile6", @"$[mAdrZeile5]", true);
			HostObject.Set("mAdrZeile5", @"$[mAdrZeile4]", true);
			HostObject.Set("mAdrZeile4", @"$[mAdrZeile3]", true);
			HostObject.Set("mAdrZeile3", @"$[mAdrZeile2]", true);
			HostObject.Set("mAdrZeile2", @"$[mAdrZeile1]", true);
			HostObject.Set("mAdrZeile1", @"$[recDest.Anrede]", true);
			X7:

			return HostObject.VariablesPop();
		}

		bool SUB_loopBelege()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/subBelege
			//  Datensatzschleife recBelege
			if (HostObject.EvalBool("($[recBelege.BELEGART]) < (0)"))
				return HostObject.VariablesPop();
			if (HostObject.EvalBool("($[recBelege.BELEGART]) > (40)"))
				return HostObject.VariablesPop();
			if (HostObject.EvalBool("($[recBelege.BELEGART]) = (20)"))
				return HostObject.VariablesPop();
			if (HostObject.IsEmpty("recBelege.BELEGNR"))
				return HostObject.VariablesPop();
			if (HostObject.IsEmpty("recBelege.TAG"))
				return HostObject.VariablesPop();
			if (HostObject.IsEmpty("recBelege.MONAT"))
				return HostObject.VariablesPop();
			if (HostObject.IsEmpty("recBelege.JAHR"))
				return HostObject.VariablesPop();

			HostObject.DefineRecord("recBelArt", "BELEGARTEN", 0, 2, HostObject.EvalString(@"$[recBelege.BELEGART>2]"));

			if (!HostObject.EvalBool("$[bHideOld]"))
				goto EndHideOld;
			if (HostObject.EvalBool("($[recBelege.JAHR]) < ($[Heute>4] - 10)"))
				return HostObject.VariablesPop();
			if (HostObject.TestEof("recBelArt"))
				goto EndHideOld;
			if (HostObject.EvalBool("$[recBelArt.Rechnung]"))
				goto EndHideOld;
			if (HostObject.EvalBool("($[recBelege.JAHR]) < ($[Heute>4] - 6)"))
				return HostObject.VariablesPop();
			EndHideOld:

			SUB_CheckPage();
			HostObject.ProcessResource("outBelegKopf");
			return HostObject.VariablesPop();
		}

		bool SUB_subBelege()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			//  Ausgabe der Standardbelege
			if (!HostObject.EvalBool("$[bBelege]"))
				return HostObject.VariablesPop();

			HostObject.DefineRecord("recBelege", "BELEGVERZEICHNIS", 5, 15, HostObject.EvalString(@"$[recDest.DESTDEBNR<15]"));
			if (HostObject.TestEof("recBelege"))
				return HostObject.VariablesPop();

			HostObject.Set("HeaderType", @"1", true);
			if (HostObject.EvalBool("$[Zeile] > 0"))
				SUB_subHeader();

			while(!HostObject.TestEof("recBelege"))
			{
				SUB_loopBelege();
				HostObject.SkipRecord("recBelege");
			}

			return HostObject.VariablesPop();
		}

		bool SUB_loopWartung()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/subWartung
			//  Datensatzschleife recWartung

			//  Aufbewahrungpflicht prüfen
			if (!HostObject.EvalBool("$[bHideOld]"))
				goto EndHideOld;
			if (HostObject.EvalBool("($[recWartung.Datum>4]) < ($[Heute>4] - 6)"))
				return HostObject.VariablesPop();
			EndHideOld:

			HostObject.DefineVariable2("mAuftrArt", "C,40,0");
			HostObject.Set("mAuftrArt", @"Reparaturauftrag", true);
			if (HostObject.EvalBool("($[recWartung.Art]) = (0)"))
				HostObject.Set("mAuftrArt", @"Wartung (klein)", true);
			if (HostObject.EvalBool("($[recWartung.Art]) = (1)"))
				HostObject.Set("mAuftrArt", @"Wartung (groß)", true);

			HostObject.DefineVariable2("mExtern", "C,40,0");
			HostObject.Set("mExtern", @"nein", true);
			if (HostObject.EvalBool("($[recWartung.MfrStatus]) > (0)"))
				HostObject.Set("mExtern", @"MFR-Portal", true);

			SUB_CheckPage();
			HostObject.ProcessResource("outWartung");

			return HostObject.VariablesPop();
		}

		bool SUB_subWartung()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			//  Ausgabe der Wartungsbelege

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

			HostObject.DefineRecord("recWartung", "WARTUNGSAUFTRAEGE", 12, 15, HostObject.EvalString(@"$[recDest.DESTDEBNR<15]"));
			if (HostObject.TestEof("recWartung"))
				return HostObject.VariablesPop();

			HostObject.Set("HeaderType", @"2", true);
			if (HostObject.EvalBool("$[Zeile] > 0"))
				SUB_subHeader();

			while(!HostObject.TestEof("recWartung"))
			{
				SUB_loopWartung();
				HostObject.SkipRecord("recWartung");
			}

			return HostObject.VariablesPop();
		}

		bool SUB_loopAnlagen()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/subAnlagen/loopObjekte
			//  Datensatzschleife recAnlage
			if (HostObject.IsEmpty("recAnlage.Nr"))
				return HostObject.VariablesPop();

			SUB_CheckPage();
			HostObject.ProcessResource("outAnlage");

			return HostObject.VariablesPop();
		}


		bool SUB_loopObjekte()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/subAnlagen
			//  Datensatzschleife recObjekt
			if (HostObject.IsEmpty("recObjekt.Nr"))
				return HostObject.VariablesPop();
			HostObject.DefineRecord("recStandort", "ADRESSSTAMM", 0, 15, HostObject.EvalString(@"$[recObjekt.Standort<15]"));
			HostObject.DefineRecord("recAnlage", "WARTUNGSANLAGEN", 2, 8, HostObject.EvalString(@"$[recObjekt.Ident>8]"));
			while(!HostObject.TestEof("recAnlage"))
			{
				SUB_loopAnlagen();
				HostObject.SkipRecord("recAnlage");
			}
			return HostObject.VariablesPop();
		}

		bool SUB_subAnlagen()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			//  Ausgabe der Wartungsanlagen

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

			HostObject.DefineRecord("recObjekt", "WARTUNGSOBJEKTE", 2, 15, HostObject.EvalString(@"$[recDest.DESTDEBNR<15]"));
			if (HostObject.TestEof("recObjekt"))
				return HostObject.VariablesPop();

			HostObject.Set("HeaderType", @"3", true);
			if (HostObject.EvalBool("$[Zeile] > 0"))
				SUB_subHeader();

			while(!HostObject.TestEof("recObjekt"))
			{
				SUB_loopObjekte();
				HostObject.SkipRecord("recObjekt");
			}

			return HostObject.VariablesPop();
		}

		bool SUB_loopMTB()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/subMBerichte
			//  Datensatzschleife recEIG
			HostObject.DefineVariable2("mTagzahl", "C,20,0");
			HostObject.Set("mTagzahl", @"$[recMTB.DATATEXT<20]", true);
			HostObject.DefineRecord("recTagzahl", "VIRTUELLES_DATUM", 0, 5, HostObject.EvalString(@"$[mTagzahl>5]"));

			//  Aufbewahrungpflicht prüfen
			if (!HostObject.EvalBool("$[bHideOld]"))
				goto EndHideOld;
			if (HostObject.EvalBool("($[recTagzahl.Jahr]) < ($[Heute>4] - 6)"))
				return HostObject.VariablesPop();
			EndHideOld:

			HostObject.DefineRecord("recStatus", "MONTEURBERICHTE", 1, 10, HostObject.EvalString(@"11$[recMTB.BERINR<8]"));
			HostObject.DefineRecord("recBetreff", "MONTEURBERICHTE", 1, 10, HostObject.EvalString(@"30$[recMTB.BERINR<8]"));
			SUB_CheckPage();
			HostObject.ProcessResource("outMTB");

			return HostObject.VariablesPop();
		}


		bool SUB_subMBerichte()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			//  Ausgabe der Monteurberichte

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

			HostObject.DefineRecord("recMTB", "MONTEURBERICHTE", 3, 17, HostObject.EvalString(@"10$[recDest.DESTDEBNR<15]"));
			if (HostObject.TestEof("recMTB"))
				return HostObject.VariablesPop();

			HostObject.Set("HeaderType", @"4", true);
			if (HostObject.EvalBool("$[Zeile] > 0"))
				SUB_subHeader();

			while(!HostObject.TestEof("recMTB"))
			{
				SUB_loopMTB();
				HostObject.SkipRecord("recMTB");
			}

			return HostObject.VariablesPop();
		}

		bool SUB_loopPartner()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/subPartner
			//  Datensatzschleife recPartner

			SUB_CheckPage();
			HostObject.ProcessResource("outPartner");

			return HostObject.VariablesPop();
		}

		bool SUB_subPartner()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			//  Ausgabe der Ansprechpartner

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

			HostObject.DefineRecord("recPartner", "ADRESSSTAMM", 12, 17, HostObject.EvalString(@"$[recDest.DESTDEBNR<15]21"));
			if (HostObject.TestEof("recPartner"))
				return HostObject.VariablesPop();

			HostObject.Set("HeaderType", @"5", true);
			if (HostObject.EvalBool("$[Zeile] > 0"))
				SUB_subHeader();

			while(!HostObject.TestEof("recPartner"))
			{
				SUB_loopPartner();
				HostObject.SkipRecord("recPartner");
			}

			return HostObject.VariablesPop();
		}
	}
}
