// Zählliste ab Version 4.1
using craftsReport.Interfaces;

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

		public rp0263_xbs()
		{
		}

		// Inventur
		public bool Execute()
		{

			HostObject.DefineVariable2("mSummeS", "N,14,2");
			HostObject.DefineVariable2("mSummeI", "N,14,2");
			HostObject.DefineVariable2("mSumme", "N,14,2");
			if (!HostObject.IsVar("mInvDatum"))
				HostObject.DefineVariable2("mInvDatum", "D,10,0");
			HostObject.DefineVariable2("mKeyFilter", "C,30,0");
			SUB_subCalcFilter();


			HostObject.DefineVariable2("Seite", "s,2,0");
			HostObject.DefineVariable2("Zeile", "s,2,0");
			HostObject.DefineVariable2("Heute", "D,10,0");

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

			if (!HostObject.IsVar("mLagerort"))
				HostObject.DefineVariable2("mLagerort", "C,15,0");
			HostObject.DefineVariable2("mDspLagerort", "C,15,0");
			HostObject.Set("mDspLagerort", @"$[mLagerort]", true);
			if (HostObject.IsEmpty("mDspLagerort"))
				HostObject.Set("mDspLagerort", @"(alle)", true);


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

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

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

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

			//  -------------------------------------------------
			//  Details
			//  Lagerposten sortiert nach Artikelnummer
			if (HostObject.IsEmpty("mLagerort"))
				HostObject.DefineRecord("recPosten", "LAGERBEWEGUNGEN", 5, 9, HostObject.EvalString(@"$[mKeyFilter<9]"));
			else
				HostObject.DefineRecord("recPosten", "LAGERBEWEGUNGEN", 5, 24, HostObject.EvalString(@"$[mKeyFilter<24]"));

			while(!HostObject.TestEof("recPosten"))
			{
				SUB_loopPosten();
				HostObject.SkipRecord("recPosten");
			}
			SUB_CheckPage();
			HostObject.ProcessResource("outEnde");

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

			return true;
		}

		bool SUB_subCalcFilter()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von OnPreReport
			HostObject.DefineVariable2("mTemp", "C,5,0");
			HostObject.Set("mTemp", @"$[mInvDatum<5]", true);
			HostObject.Set("mKeyFilter", @"3$[mInvDatum>4]$[mTemp>2]$[mTemp<2]$[mLagerort<15]", true);
			return HostObject.VariablesPop();
		}

		bool SUB_CheckPage()
		{
			HostObject.VariablesPush();

			//  Fussbereich ausgeben, wenn letzte Zeile erreicht
			if (HostObject.EvalBool("$[Zeile] >= ($[ZlMax] - 1)"))
				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("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);
			// ******************** OUT ##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.ProcessResource("KopfDetail");


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

			// IF BOOL(($[mSumme]=0) * ($[mSummeI]=0) * ($[mSummeS]=0))
			//    RETURN
			// ************************** OUT ##outUebertrag

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

			//  Unterprogramm von ReportMain
			//  Datensatzschleife recPosten
			HostObject.DefineRecord("recTeil", "STAMMPOSITIONEN", 0, 20, HostObject.EvalString(@"$[recPosten.ARTNR<20]"));
			// REC recEinheit(MENGENEINHEITEN,0,4)$[recTeil.Einheit>4]

			HostObject.DefineVariable2("mDiffMenge", "N,12,3");
			HostObject.DefineVariable2("mDiffWert", "N,12,2");
			HostObject.Set("mDiffMenge", @"$[recPosten.MENGE]-$[recPosten.SollBest]", true);
			HostObject.Set("mDiffWert", @"$[recPosten.Wert]-$[recPosten.SollWert]", true);

			HostObject.DefineVariable2("mGebinde", "C,12,0");
			if (HostObject.EvalBool("($[recPosten.GEBINDE]) <> (1,000)"))
				HostObject.Set("mGebinde", @"$[recPosten.GEBINDE]", true);

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

			HostObject.Set("mSummeS", @"$[mSummeS]+$[recPosten.SollWert]", true);
			HostObject.Set("mSummeI", @"$[mSummeI]+$[recPosten.Wert]", true);
			HostObject.Set("mSumme", @"$[mSumme]+$[mDiffWert]", true);

			return HostObject.VariablesPop();
		}
	}
}
