using craftsReport.Interfaces;

namespace craftsReport.modules
{
	public class rp0762_xbs : IReportModule, IReportModuleEvents
	{
		#region Members and Contructor

		public rp0762_xbs()
		{
		}

		#endregion

		#region IReportModule

		public IReportHost HostObject { set; get; }

		// Baustellenspiegel
		public bool Execute()
		{
			if (!HostObject.IsVar("mVersion2"))
				HostObject.DefineVariable2("mVersion2", "L,1,0");

			if (!HostObject.IsVar("mRepTitle"))
				HostObject.DefineVariable2("mRepTitle", "c,250,0");
			if (HostObject.IsEmpty("mRepTitle"))
				HostObject.Set("mRepTitle", "Baustellenspiegel", false);

			HostObject.DefineRecord("recMAIN", "PROJEKTAUFTRAEGE", 0, 0, "");

			HostObject.LoadAdditionalResources("/r");
			HostObject.PaperDefinition("PaperDefinition");
			HostObject.DokumentStart();
			HostObject.SeitenKopf();

			while(!HostObject.TestEof("recMAIN"))
			{
				SUB_loopMAIN();
				HostObject.SkipRecord("recMAIN");
			}

			HostObject.SeitenFuss();
			HostObject.DokumentEnde();
			return true;
		}

		#endregion

		#region IReportModuleEvents

		public void OnAfterPageStart()
		{
			if (HostObject.EvalBool("$[mVersion2]"))
				HostObject.ProcessResource("KopfDetail2");
			else
				HostObject.ProcessResource("KopfDetail");
		}

		public void OnBeforePageClose()
		{
		}

		public void OnAfterExecute(string szOutFileName)
		{
		}

		#endregion

		#region Internals

		bool SUB_loopMAIN()
		{
			HostObject.VariablesPush();

			HostObject.DefineRecord("recBUCH", "PROJEKTGEBUCHT", 0, 8, HostObject.EvalString(@"$[recMAIN.PrjNr<8]"));
			HostObject.DefineRecord("recAUFTR", "BELEGVERZEICHNIS", 0, 10, HostObject.EvalString(@"$[recMAIN.PrjNr<8] 7"));
			HostObject.DefineVariable2("mEkMatS", "N,12,2");
			HostObject.DefineVariable2("mEkSonS", "N,12,2");
			HostObject.DefineVariable2("mEkSonI", "N,12,2");
			HostObject.DefineVariable2("mBrutto", "N,12,2");
			HostObject.DefineVariable2("mNettoI", "N,12,2");
			HostObject.DefineVariable2("mGewinnI", "N,12,2");
			HostObject.DefineVariable2("mGewinnS", "N,12,2");
			HostObject.DefineVariable2("mEkS", "N,12,2");
			HostObject.DefineVariable2("mEkI", "N,12,2");

			HostObject.Set("mEkMatS", @"$[recMAIN.EkMat]   - $[recMAIN.EkFremd]", true);
			HostObject.Set("mEkSonS", @"$[recMAIN.EkFremd] + $[recMAIN.EkGemein]", true);
			HostObject.Set("mEkSonI", @"$[recBUCH.EkFremd] + $[recBUCH.EkSonst]", true);
			HostObject.Set("mNettoI", @"$[recBUCH.VkLohn] + $[recBUCH.VkMat] + $[recBUCH.VkSonst]", true);
			HostObject.Set("mBrutto", @"$[mNettoI] + $[recBUCH.VkUst]", true);
			HostObject.Set("mEkS", @"$[recMAIN.EkLohn] + $[mEkMatS]       + $[mEkSonS]", true);
			HostObject.Set("mEkI", @"$[recBUCH.EkLohn] + $[recBUCH.EkMat] + $[mEkSonI]", true);
			HostObject.Set("mGewinnS", @"$[recMAIN.Netto] - $[mEkS]", true);
			HostObject.Set("mGewinnI", @"$[mNettoI] - $[mEkI]", true);

			HostObject.CheckPageSpace(3);
			if (HostObject.EvalBool("$[mVersion2]"))
				HostObject.ProcessResource("Detail2");
			else
				HostObject.ProcessResource("Detail");

			return HostObject.VariablesPop();
		}

		#endregion
	}
}
