// Bericht Reperaturauftrag mit Posten
using craftsReport.Interfaces;

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

		public rp2708_xbs()
		{
		}

		// Reparaturauftrag
		public bool Execute()
		{
			HostObject.DefineVariable2("Zeile", "s,2,0");
			HostObject.DefineVariable2("ZlMax", "s,2,0");
			HostObject.DefineVariable2("Seite", "s,2,0");
			HostObject.DefineVariable2("RandX", "d,8,2");
			HostObject.DefineVariable2("RandY", "d,8,2");
			HostObject.DefineVariable2("SizeX", "d,8,2");
			HostObject.DefineVariable2("Heute", "D,10,0");
			HostObject.DefineVariable2("cyZeile", "d,8,3");
			HostObject.DefineVariable2("mLPI", "s,2,0");
			HostObject.DefineVariable2("cntMessW", "s,2,0");
			HostObject.DefineVariable2("cntTeile", "s,2,0");
			HostObject.DefineVariable2("cntListe", "s,2,0");
			HostObject.DefineVariable2("maxDetails", "s,2,0");
			HostObject.DefineVariable2("LfdNr", "s,2,0");
			HostObject.DefineVariable2("mHeader", "C,80,0");
			HostObject.DefineVariable2("fntLITT", "C,40,0");
			HostObject.DefineVariable2("fntHDR", "C,40,0");
			HostObject.DefineVariable2("fntTXT", "C,40,0");
			HostObject.DefineVariable2("cntTexte", "s,2,0");
			HostObject.DefineVariable2("maxMessW", "s,2,0");
			HostObject.DefineVariable2("maxTeile", "s,2,0");
			HostObject.DefineVariable2("maxListe", "s,2,0");
			HostObject.DefineVariable2("startMessW", "s,2,0");
			HostObject.DefineVariable2("startTeile", "s,2,0");
			HostObject.DefineVariable2("startListe", "s,2,0");
			HostObject.DefineVariable2("endLines", "s,2,0");

			HostObject.Set("endLines", @"  36 ", true);
			HostObject.Set("startListe", @"43", true);
			HostObject.Set("maxListe", @"   6", true);
			HostObject.Set("mLPI", @"       6", true);

			if (!HostObject.IsVar("mNadel"))
				HostObject.DefineVariable2("mNadel", "L,1,0");
			if (HostObject.EvalBool("$[mNadel]"))
				SUB_subNadel();
			else
				SUB_subLaser();

			if (!HostObject.IsVar("bDrkBez2"))
				HostObject.DefineVariable2("bDrkBez2", "L,1,0");

			HostObject.Set("RandX", @"      1,80", true);
			HostObject.Set("RandY", @"      0,80", true);
			HostObject.Set("SizeX", @"      17,50", true);
			HostObject.Set("Seite", @"      1", true);
			HostObject.Set("Zeile", @"      0", true);
			HostObject.Set("cyZeile", @"    2.54/$[mLPI]", true);
			HostObject.Set("ZlMax", @"     (27.4/$[cyZeile]) - 5", true);

			HostObject.Set("Heute", @"H", true);
			HostObject.Set("maxDetails", @"$[maxMessW]", true);
			if (HostObject.EvalBool("($[maxDetails]) < ($[maxTeile] + $[maxListe] + 1)"))
				HostObject.Set("maxDetails", @"$[maxTeile] + $[maxListe] +  1", true);

			if (!HostObject.IsVar("pIdent"))
				HostObject.DefineVariable2("pIdent", "N,8,0");

			if (!HostObject.IsVar("mDevice"))
				HostObject.DefineVariable2("mDevice", "C,20,0");
			if (HostObject.IsEmpty("mDevice"))
				HostObject.Set("mDevice", @"Service", true);

			//  Datensätze zum Auftrag
			HostObject.DefineRecord("recAuftrag", "WARTUNGSAUFTRAEGE", 0, 8, HostObject.EvalString(@"$[pIdent>8]"));
			HostObject.DefineRecord("recObject", "WARTUNGSOBJEKTE", 0, 8, HostObject.EvalString(@"$[recAuftrag.ObjektId>8]"));
			HostObject.DefineRecord("recAnlage", "WARTUNGSANLAGEN", 0, 8, HostObject.EvalString(@"$[recAuftrag.AnlagenId>8]"));
			HostObject.DefineRecord("recMonteur", "ADRESSSTAMM", 6, 6, HostObject.EvalString(@" 4$[recAuftrag.Monteur<4]"));
			HostObject.DefineRecord("recKunde", "ADRESSSTAMM", 0, 15, HostObject.EvalString(@"$[recObject.KdNr<15]"));
			HostObject.DefineRecord("recStando", "ADRESSSTAMM", 0, 15, HostObject.EvalString(@"$[recObject.Standort<15]"));

			//  Datensatz EIGENE_ANSCHRIFTEN
			HostObject.DefineRecord("recEIGA", "EIGENE_ANSCHRIFTEN", 0, 2, HostObject.EvalString(@"01"));

			//  -------------------------------------------------
			if (HostObject.EvalBool("$[recAuftrag.Art] = 2"))
				HostObject.Set("mHeader", @"Auftrag", true);
			else
				HostObject.Set("mHeader", @"Wartungsauftrag", true);
			//  -------------------------------------------------

			//  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$[recAuftrag.PrjNr]@ @saPS2PDF-a$[recKunde.DESTDEBNR]@", true);
			HostObject.Set("mJobHead", @"@saPS2PDF-d$[mHeader] $[recAuftrag.Nr]@ $[mJobHead]", true);

			//  -------------------------------------------------
			HostObject.DefineVariable2("cTermin", "C,8,0");
			HostObject.DefineVariable2("cBezeich", "C,50,0");
			HostObject.Set("cTermin", @"$[recAuftrag.Termin<6]$[recAuftrag.Termin>2]", true);
			if (HostObject.EvalBool("$[bDrkBez2]"))
				HostObject.Set("cBezeich", @"$[recAnlage.Bez1] $[recAnlage.Bez2]", true);
			else
				HostObject.Set("cBezeich", @"$[recAnlage.Bez1]", true);
			if (HostObject.IsEmpty("cBezeich"))
				HostObject.Set("cBezeich", @"Anlagen-Nr", true);

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

			HostObject.WriteLine(HostObject.EvalString(@"INITPAGESIZE"));
			HostObject.WriteLine(HostObject.EvalString(@"STARTJOB $[mJobHead]"));
			HostObject.WriteLine(HostObject.EvalString(@"TRANSPARENT"));
			HostObject.WriteLine(HostObject.EvalString(@"STARTPAGE"));
			HostObject.WriteLine(HostObject.EvalString(@"DEVICE $[mDevice]"));
			HostObject.Set("Zeile", @"0", true);
			SUB_Kopf();
			SUB_subAufText();

			HostObject.Set("Zeile", @"$[startListe] - 19", true);
			SUB_subLines();

			HostObject.Set("Zeile", @"$[startListe]", true);
			SUB_subDetails();

			// --------------------------------------------------
			//  letzte Seite abschliessen
			if (HostObject.EvalBool("($[Zeile]) <> (0)"))
			{
				HostObject.Set("Zeile", @"$[ZlMax] + 1", true);
				HostObject.ProcessResource("FussSeite");
				HostObject.WriteLine(HostObject.EvalString(@"ENDPAGE"));
			}

			HostObject.WriteLine(HostObject.EvalString(@"ENDJOB"));
			return true;
		}


		bool SUB_subNadel()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von OnPreReport
			//  ------------------ Nadeldrucker OKI 380
			//  fntLITT=Courier 20 cpi,10,N
			//  fntHDR=Courier 15 cpi,10,F
			//  fntTXT=Courier 12 cpi,10,N

			//  ------------------ Nadeldrucker allgemein
			HostObject.Set("fntLITT", @"!Courier New,10,1636,N", true);
			HostObject.Set("fntHDR", @"!Courier New,10,1200,F", true);
			HostObject.Set("fntTXT", @"!Courier New,10,1200,N", true);
			return HostObject.VariablesPop();
		}

		bool SUB_subLaser()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von OnPreReport
			//  ------------------ Nadeldrucker allgemein
			HostObject.Set("fntLITT", @"Arial,9,N", true);
			HostObject.Set("fntHDR", @"Arial,10,F", true);
			HostObject.Set("fntTXT", @"Arial,10,N", true);
			return HostObject.VariablesPop();
		}

		/*
		bool SUB_loopMAIN()
		{
			HostObject.VariablesPush();
			return HostObject.VariablesPop();
		}
		*/

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

			HostObject.Set("Zeile", @"$[ZlMax]", true);
			HostObject.ProcessResource("FussSeite");

			HostObject.WriteLine(HostObject.EvalString(@"ENDPAGE"));
			//  naechste Seite anfangen
			HostObject.Set("Seite", @"  $[Seite]+1", true);
			HostObject.Set("Zeile", @"0", true);
			HostObject.WriteLine(HostObject.EvalString(@"STARTPAGE"));
			HostObject.WriteLine(HostObject.EvalString(@"DEVICE $[mDevice]"));

			return HostObject.VariablesPop();
		}
		*/

		bool SUB_Kopf()
		{
			HostObject.VariablesPush();

			HostObject.Set("Zeile", @"7", true);
			HostObject.ProcessResource("KopfSeite");
			HostObject.ProcessResource("KopfDetail");

			return HostObject.VariablesPop();
		}

		bool SUB_loopZeilenT()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/subDetails
			//  Datensatzschleife recZeilen

			if (HostObject.EvalBool("($[recZeilen.Tag]) <= ($[cntTeile] + $[cntListe] + 2)"))
				return HostObject.VariablesPop();
			if (HostObject.EvalBool("($[recZeilen.Tag]) > ($[maxDetails] + 1)"))
				return HostObject.VariablesPop();

			HostObject.Set("LfdNr", @"$[LfdNr] + 1", true);
			HostObject.ProcessResource("outZeilenT");

			return HostObject.VariablesPop();
		}

		bool SUB_loopDimWerte()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/subDetails/loopTeile
			//  Datensatzschleife recDim
			HostObject.DefineVariable2("cBez", "u,40,0");
			HostObject.Set("cBez", @"$[recDim.Bez]", true);

			if (HostObject.CompareString("cBez", "=", HostObject.EvalString(@"$[cDimName1]")))
				HostObject.Set("cDimWert1", @"$[recDim.Wert]", true);
			if (HostObject.CompareString("cBez", "=", HostObject.EvalString(@"$[cDimName2]")))
				HostObject.Set("cDimWert2", @"$[recDim.Wert]", true);
			if (HostObject.CompareString("cBez", "=", HostObject.EvalString(@"$[cDimName3]")))
				HostObject.Set("cDimWert3", @"$[recDim.Wert]", true);
			if (HostObject.CompareString("cBez", "=", HostObject.EvalString(@"$[cDimName4]")))
				HostObject.Set("cDimWert4", @"$[recDim.Wert]", true);

			return HostObject.VariablesPop();
		}

		/*
		bool SUB_loopTeile()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/subDetails
			//  Datensatzschleife recTeile

			if (!HostObject.EvalBool("($[maxTeile]) > ($[cntTeile])"))
				return HostObject.VariablesPop();

			HostObject.DefineRecord("recDim", "WARTUNGSDIMENSIONEN", 0, 12, HostObject.EvalString(@"$[recTeile.AnlagenId>8]$[recTeile.PosNr>4]"));
			HostObject.DefineVariable2("cDimWert1", "C,40,0");
			HostObject.DefineVariable2("cDimWert2", "C,40,0");
			HostObject.DefineVariable2("cDimWert3", "C,40,0");
			HostObject.DefineVariable2("cDimWert4", "C,40,0");
			while(!HostObject.TestEof("recDim"))
			{
				SUB_loopDimWerte();
				HostObject.SkipRecord("recDim");
			}

			HostObject.Set("cntTeile", @" $[cntTeile] + 1", true);
			HostObject.ProcessResource("outTeile");

			return HostObject.VariablesPop();
		}
		*/

		bool SUB_loopTodo()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/subDetails
			//  Datensatzschleife recTodo

			// IFNOT BOOL($[recTodo.Check])
			//   RETURN
			if (!HostObject.EvalBool("($[maxListe]) > ($[cntListe])"))
				return HostObject.VariablesPop();

			HostObject.Set("cntListe", @" $[cntListe] + 1", true);
			HostObject.Set("LfdNr", @"$[LfdNr] + 1", true);
			HostObject.ProcessResource("outTodo");

			return HostObject.VariablesPop();
		}

		bool SUB_subDetails()
		{
			HostObject.VariablesPush();

			//  Ausgabe linke Seite
			HostObject.DefineVariable2("SaveZeile", "s,2,0");
			HostObject.DefineRecord("recTeile", "WARTUNGSPOSITIONEN", 0, 8, HostObject.EvalString(@"$[recAnlage.Ident>8]"));
			HostObject.DefineRecord("recTodo", "WARTUNGSPOSTEN", 0, 8, HostObject.EvalString(@"$[recAuftrag.Ident>8]"));

			HostObject.Set("SaveZeile", @"$[Zeile]", true);

			// OUT ##outTeilHead
			// LOOP recTeile(#loopTeile)

			HostObject.ProcessResource("outChkHead");
			while(!HostObject.TestEof("recTodo"))
			{
				SUB_loopTodo();
				HostObject.SkipRecord("recTodo");
			}

			HostObject.DefineRecord("recZeilen", "VIRTUELLES_DATUM", 1, 6, HostObject.EvalString(@"200501"));
			while(!HostObject.TestEof("recZeilen"))
			{
				SUB_loopZeilenT();
				HostObject.SkipRecord("recZeilen");
			}

			HostObject.ProcessResource("outVOB");

			// Zeile=$[SaveZeile]
			return HostObject.VariablesPop();
		}

		bool SUB_loopZeilenE()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/subLines
			//  Datensatzschleife recZeilen
			if (HostObject.EvalBool("($[Zeile]) > ($[endLines] - 2)"))
				HostObject.BreakRecord("recZeilen");
			if (HostObject.TestEof("recZeilen"))
				return HostObject.VariablesPop();
			HostObject.Set("LfdNr", @"$[LfdNr] + 1", true);
			HostObject.ProcessResource("outZeilenE");

			return HostObject.VariablesPop();
		}

		bool SUB_subLines()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			HostObject.ProcessResource("outHdLines");
			HostObject.DefineRecord("recZeilen", "VIRTUELLES_DATUM", 1, 4, HostObject.EvalString(@"2005"));
			while(!HostObject.TestEof("recZeilen"))
			{
				SUB_loopZeilenE();
				HostObject.SkipRecord("recZeilen");
			}

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

			HostObject.ProcessResource("outPunkte");
			return HostObject.VariablesPop();
		}

		bool SUB_loopText()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/subAufText
			//  Datensatzschleife recText
			HostObject.Set("mLine", @"$[mLine]$[recText.Daten]", true);
			if (HostObject.EvalBool("$[recText.Abschluss]"))
				HostObject.BreakRecord("recText");
			if (!HostObject.EvalBool("$[recText.SoftBreak] | $[recText.LineFeeds]"))
				return HostObject.VariablesPop();

			HostObject.Set("cntTexte", @"$[cntTexte] + 1", true);
			SUB_subCalcTPmpt();
			HostObject.ProcessResource("outTextZeile");
			HostObject.Set("mLine", @"", true);

			return HostObject.VariablesPop();
		}

		bool SUB_subCalcTPmpt()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/subAufText
			HostObject.Set("mTxtPmpt", @"", true);

			// ==== :Kond_67BD Bedingungscode
			if (HostObject.EvalBool("($[cntTexte]) <> (1)"))
				goto Nicht1;
			HostObject.Set("mTxtPmpt", @"BvH/Objekt:", true);
			return HostObject.VariablesPop();

			Nicht1:
			if (HostObject.EvalBool("($[cntTexte]) <> (3)"))
				goto Nicht3;
			HostObject.Set("mTxtPmpt", @"Leistung  :", true);
			return HostObject.VariablesPop();

			Nicht3:
			HostObject.Set("mTxtPmpt", @"", true);
			return HostObject.VariablesPop();
		}

		bool SUB_subAufText()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			HostObject.DefineRecord("recText", "LANGTEXTZEILEN", 0, 22, HostObject.EvalString(@"25AUAUF$[recAuftrag.Ident>15]"));
			if (HostObject.TestEof("recText"))
				return HostObject.VariablesPop();

			HostObject.DefineVariable2("mTxtPmpt", "C,20,0");
			HostObject.DefineVariable2("mLine", "C,250,0");
			while(!HostObject.TestEof("recText"))
			{
				SUB_loopText();
				HostObject.SkipRecord("recText");
			}

			if (HostObject.IsEmpty("mLine"))
				goto EmpLine;

			HostObject.Set("cntTexte", @"$[cntTexte] + 1", true);
			SUB_subCalcTPmpt();
			HostObject.ProcessResource("outTextZeile");

			EmpLine:
			// OUT ##outTextLine
			return HostObject.VariablesPop();
		}
	}
}
