using craftsReport.Interfaces;

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

		public rsp222_xbs()
		{
		}

		// Lagerfachkarte
		public bool Execute()
		{


			//  Haupprogrammcode 
			// --------------------------------------------------
			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,6");

			if (!HostObject.IsVar("VonArtNr"))
				HostObject.DefineVariable2("VonArtNr", "u,20,0");
			if (!HostObject.IsVar("BisArtNr"))
				HostObject.DefineVariable2("BisArtNr", "u,20,0");

			HostObject.Set("ZlMax", @"     61", true);
			HostObject.Set("RandX", @"      1,80", true);
			HostObject.Set("RandY", @"      0,80", true);
			HostObject.Set("SizeX", @"      18,0", true);
			HostObject.Set("Seite", @"      1", true);
			HostObject.Set("Zeile", @"      0", true);
			HostObject.Set("cyZeile", @" 2,54/6", true);
			HostObject.Set("Heute", @"H", true);

			// **************************************************
			//  Datensatzschleife STAMMPOSITIONEN
			HostObject.DefineRecord("recART", "STAMMPOSITIONEN", 0, 0, HostObject.EvalString(@""));
			HostObject.FindRecord("recART", HostObject.EvalString("$[VonArtNr<20]"));
			//EOF_recART:
			// **************************************************
			HostObject.DefineRecord("recOPT", "OPTIONEN", 0, 0, HostObject.EvalString(@""));

			HostObject.DefineVariable2("mLohn", "N,6,2");
			HostObject.FindRecord("recOPT", HostObject.EvalString(" 7006"));
			HostObject.Set("mLohn", @"$[recOPT.OPTDATAD]", true);

			HostObject.WriteLine(HostObject.EvalString(@"INITPAGESIZE"));
			HostObject.WriteLine(HostObject.EvalString(@"STARTJOB Artikelstammblatt"));
			HostObject.WriteLine(HostObject.EvalString(@"TRANSPARENT"));
			while(!HostObject.TestEof("recART"))
			{
				SUB_loopMAIN();
				HostObject.SkipRecord("recART");
			}
			HostObject.WriteLine(HostObject.EvalString(@"ENDJOB"));

			return true;
		}

		bool SUB_loopMAIN()
		{
			HostObject.VariablesPush();

			if (HostObject.GetYieldResult(HostObject.EvalString("$[recART.Lkz]/$[recART.Artnr]")))
				goto NoBreak;
			HostObject.BreakRecord("recART");
			return HostObject.VariablesPop();
			NoBreak:

			//  Wird fuer jeden Satz von recMAIN aufgerufen
			// -------------------------------------------------
			if (HostObject.IsEmpty("BisArtNr"))
				goto DoIt;
			if (!HostObject.CompareString("BisArtNr", "<", HostObject.EvalString(@"$[recART.LKZ<2]$[recART.ARTNR]")))
				goto DoIt;
			HostObject.BreakRecord("recART");
			return HostObject.VariablesPop();
			DoIt:
			// -------------------------------------------------
			HostObject.DefineVariable2("mSaveZl", "s,2,0");
			HostObject.DefineVariable2("mPrsFak", "d,8,3");
			HostObject.DefineVariable2("mEkNe100", "d,8,2");
			HostObject.DefineVariable2("mCuZahl", "d,8,2");
			HostObject.DefineVariable2("mMaxBew", "s,2,0");
			HostObject.DefineVariable2("mBewGP", "d,8,2");
			HostObject.Set("Zeile", @"0", true);
			HostObject.WriteLine(HostObject.EvalString(@"STARTPAGE"));
			HostObject.ProcessResource("KopfSeite");
			// -------------------------------------------------

			// **************************************************
			//  Datensatzschleife MENGENEINHEITEN
			HostObject.DefineRecord("recME", "MENGENEINHEITEN", 0, 4, HostObject.EvalString(@"$[recART.EINHEIT>4]"));
			//EOF_recME:
			// **************************************************
			// **************************************************
			//  Datensatzschleife PREISEINHEITEN
			HostObject.DefineRecord("recPE", "PREISEINHEITEN", 0, 2, HostObject.EvalString(@"$[recART.PE>2]"));
			//EOF_recPE:
			// **************************************************

			HostObject.Set("mPrsFak", @" $[recART.GEBINDE] * $[recPE.FAKTOR]", true);
			HostObject.Set("mEkNe100", @"0", true);
			HostObject.Set("mCuZahl", @" 0", true);

			// **************************************************
			//  Datensatzschleife NEZUSCHLAEGE
			HostObject.DefineRecord("recNEZ", "NEZUSCHLAEGE", 0, 20, HostObject.EvalString(@"$[recART.LKZ<2]$[recART.ARTNR<18]"));
			while(!HostObject.TestEof("recNEZ"))
			{
				SUB_loop_recNEZ();
				HostObject.SkipRecord("recNEZ");
			}
			// **************************************************

			// -------------------------------------------------
			HostObject.ProcessResource("BasisDaten");
			HostObject.ProcessResource("LagerDaten");

			HostObject.Set("mMaxBew", @"(8 * ($[ZlMax] - $[Zeile])) / 6", true);

			HostObject.Set("Zeile", @"($[Zeile] * 8) / 6", true);
			HostObject.Set("cyZeile", @" 2,54/8", true);
			HostObject.Set("mSaveZl", @"$[Zeile]", true);
			HostObject.Set("mMaxBew", @"$[mMaxBew] + $[Zeile]", true);

			//  Schleife für Zugänge
			HostObject.Set("Zeile", @"$[mSaveZl]", true);
			// **************************************************
			//  Datensatzschleife LAGERBEWEGUNGEN
			HostObject.DefineRecord("recZU", "LAGERBEWEGUNGEN", 100, 20, HostObject.EvalString(@"$[recART.LKZ<2]$[recART.ARTNR<18]"));
			HostObject.ProcessResource("Kopf_recZU");
			while(!HostObject.TestEof("recZU"))
			{
				SUB_loop_recZU();
				HostObject.SkipRecord("recZU");
			}
			//EOF_recZU:
			// **************************************************


			//  Schleife für Abgänge
			HostObject.Set("Zeile", @"$[mSaveZl]", true);
			// **************************************************
			//  Datensatzschleife LAGERBEWEGUNGEN
			HostObject.DefineRecord("recAB", "LAGERBEWEGUNGEN", 100, 20, HostObject.EvalString(@"$[recART.LKZ<2]$[recART.ARTNR<18]"));
			HostObject.ProcessResource("Kopf_recAB");
			while(!HostObject.TestEof("recAB"))
			{
				SUB_loop_recAB();
				HostObject.SkipRecord("recAB");
			}
			//EOF_recAB:
			// **************************************************

			// -------------------------------------------------
			HostObject.Set("cyZeile", @" 2,54/6", true);
			HostObject.Set("Zeile", @"$[ZlMax]", true);
			HostObject.ProcessResource("FussSeite");
			HostObject.WriteLine(HostObject.EvalString(@"ENDPAGE"));
			// -------------------------------------------------

			return HostObject.VariablesPop();
		}

		bool SUB_loop_recNEZ()
		{
			HostObject.VariablesPush();

			//  Datensatzschleife NEZUSCHLAEGE
			//  Datensatzname = recNEZ

			// **************************************************
			//  Datensatzschleife NICHTEISENDATEN
			HostObject.DefineRecord("recNEF", "NICHTEISENDATEN", 0, 2, HostObject.EvalString(@"$[recNEZ.NEKZ<2]"));
			if (HostObject.TestEof("recNEF"))
				return HostObject.VariablesPop();
			// **************************************************

			HostObject.DefineVariable2("TagesPrs", "d,8,6");
			HostObject.DefineVariable2("BasisPrs", "d,8,6");
			HostObject.DefineVariable2("NePrs", "d,8,6");

			HostObject.Set("TagesPrs", @"$[recNEF.TAGESPREIS] / $[recNEF.KGJEPREIS]", true);
			HostObject.Set("BasisPrs", @"$[recNEZ.BASISPREIS] / $[recNEZ.KGJEBASIS]", true);
			HostObject.Set("NePrs", @"   ($[recNEZ.GEWICHT] / $[recNEZ.GEBINDE]) * ($[TagesPrs]-$[BasisPrs])", true);
			HostObject.Set("mEkNe100", @"$[mEkNe100] + (100 * $[NePrs])", true);

			if (HostObject.CompareString("recNEZ.NEKZ", "=", HostObject.EvalString(@"CU")))
				HostObject.Set("mCuZahl", @" $[recNEZ.GEWICHT]", true);

			return HostObject.VariablesPop();
		}

		bool SUB_loop_recZU()
		{
			HostObject.VariablesPush();

			//  Datensatzschleife LAGERBEWEGUNGEN
			//  Datensatzname = recZU

			if (HostObject.EvalBool("$[Zeile] < $[mMaxBew]"))
				goto DoIt;
			HostObject.BreakRecord("recZU");
			return HostObject.VariablesPop();

			DoIt:
			if (!HostObject.EvalBool("$[recZU.KZ_ZA] = 1"))
				return HostObject.VariablesPop();
			if (!HostObject.EvalBool("$[recZU.GEBINDE] > 0"))
				return HostObject.VariablesPop();

			HostObject.Set("mBewGP", @" $[recZU.MENGE] * ($[recZU.EKPREIS] / $[recZU.GEBINDE])", true);
			HostObject.ProcessResource("out_recZU");

			return HostObject.VariablesPop();
		}

		bool SUB_loop_recAB()
		{
			HostObject.VariablesPush();

			//  Datensatzschleife LAGERBEWEGUNGEN
			//  Datensatzname = recAB

			if (HostObject.EvalBool("$[Zeile] < $[mMaxBew]"))
				goto DoIt;
			HostObject.BreakRecord("recAB");
			return HostObject.VariablesPop();

			DoIt:
			if (!HostObject.EvalBool("$[recAB.KZ_ZA] = 2"))
				return HostObject.VariablesPop();
			if (!HostObject.EvalBool("$[recAB.GEBINDE] > 0"))
				return HostObject.VariablesPop();

			HostObject.Set("mBewGP", @" $[recAB.MENGE] * ($[recAB.EKPREIS] / $[recAB.GEBINDE])", true);
			HostObject.ProcessResource("out_recAB");

			return HostObject.VariablesPop();
		}
	}
}
