using craftsReport.Interfaces;

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

		public rsp223_xbs()
		{
		}

		// Lagerfachkarte
		public bool Execute()
		{

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

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

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

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


			//  Haupprogrammcode 
			// --------------------------------------------------

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

			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("mPrsFak", "d,8,3");
			HostObject.DefineVariable2("mEkNe100", "d,8,2");
			HostObject.DefineVariable2("mCuZahl", "d,8,2");

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

			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.Set("Zeile", @"0", true);
			HostObject.WriteLine(HostObject.EvalString(@"STARTPAGE"));
			HostObject.WriteLine(HostObject.EvalString(@"DEVICE Etiketten"));
			HostObject.ProcessResource("BasisDaten");
			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();
		}
	}
}
