// Bietervergleich
// Parameter FltKey u,18,0
using craftsReport.Interfaces;
using System;

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

		private bool m_IsLastPage = false;
		private IReportRecord m_recVorlauf = null;
		private IReportRecord m_recVorlaufEx = null;
		private IReportRecord m_recTender = null;
		private bool m_DoTabulieren = false;
		private bool m_bAllePositionen = false;

		public rp0457_xbs()
		{
		}

		#endregion

		#region IReportModule

		public IReportHost HostObject { set; get; }

		public bool Execute()
		{
			// Titel festlegen
			if (!HostObject.IsVar("mRepTitle"))
				HostObject.DefineVariable2("mRepTitle", "c,250,0");
			if (HostObject.IsEmpty("mRepTitle"))
				HostObject.Set("mRepTitle", "Bietervergleich", false);

			// Belegvorlaufdaten filtern
			if (!HostObject.IsVar("FltKey"))
				HostObject.DefineVariable2("FltKey", "u,18,0");
			m_recVorlauf = HostObject.DefineRecord("recVorlauf", "BELEGVERZEICHNIS", 0, 18, HostObject.EvalString(@"$[FltKey<18]"));
			m_recVorlaufEx = HostObject.DefineRecord("recVorlaufEx", "BELEGKOPF_ERWEITERUNG", 0, 18, HostObject.EvalString(@"$[FltKey<18]"));
			m_recTender = HostObject.DefineRecord("recTender", "Tender", 1, 18, HostObject.EvalString(@"$[FltKey<18]"));
			HostObject.DefineRecord("recTenderer1", "Tenderer", 0, 22, HostObject.EvalString(@"$[recTender.No<20] 1"));
			HostObject.DefineRecord("recTenderer2", "Tenderer", 0, 22, HostObject.EvalString(@"$[recTender.No<20] 2"));
			HostObject.DefineRecord("recTenderer3", "Tenderer", 0, 22, HostObject.EvalString(@"$[recTender.No<20] 3"));
			HostObject.DefineRecord("recTenderer4", "Tenderer", 0, 22, HostObject.EvalString(@"$[recTender.No<20] 4"));

			if (HostObject.IsVar("mAllePos"))
				m_bAllePositionen = HostObject.EvalBool("$[mAllePos]");

				// Filterbeschreibung
			if (!HostObject.IsVar("mFilterText"))
				HostObject.DefineVariable2("mFilterText", "c,250,0");
			HostObject.Set("mFilterText", "$[recVorlauf.BELARTTEXT] $[recVorlauf.BELEGNR]", true);

			HostObject.DefineVariable2("AngebotsSumme1", "N,14,2");
			HostObject.DefineVariable2("AngebotsSumme2", "N,14,2");
			HostObject.DefineVariable2("AngebotsSumme3", "N,14,2");
			HostObject.DefineVariable2("AngebotsSumme4", "N,14,2");
			HostObject.DefineVariable2("ZuschlagsSumme1", "N,14,2");
			HostObject.DefineVariable2("ZuschlagsSumme2", "N,14,2");
			HostObject.DefineVariable2("ZuschlagsSumme3", "N,14,2");
			HostObject.DefineVariable2("ZuschlagsSumme4", "N,14,2");

			HostObject.LoadAdditionalResources("reportQuer");
			HostObject.PaperDefinition("PaperDefinition");
			HostObject.DokumentStart();
			HostObject.SeitenKopf();

			IReportRecord recRoot = HostObject.DefineRecord("recRoot", "BelegPositionen", 0, 18, HostObject.EvalString(@"$[FltKey<18]"));
			while ((!recRoot.Eof) && (recRoot.Get("SatzArt").ToString() != "1"))
			{
				recRoot.GoNext();
			}
			if (!recRoot.Eof)
            {
				LVBereich(recRoot);
            }
			HostObject.CheckPageSpace(3);
			HostObject.ProcessResource("Summen");

			m_IsLastPage = true;
			HostObject.SeitenFuss();
			HostObject.DokumentEnde();

			return true;
		}

		#endregion

		#region IReportModuleEvents

		public void OnAfterPageStart()
		{
			m_DoTabulieren = false;
			HostObject.ProcessResource("KopfDetail");
			if (HostObject.EvalInt("$[Seite]") == 1)
			{
			}
			else
			{
			}
		}

		public void OnBeforePageClose()
		{
			if (m_IsLastPage)
			{
			}
			else
			{
			}
		}

		public void OnAfterExecute(string szOutFileName)
		{
		}

        #endregion

        #region Internals

		void LVBereich(IReportRecord recParent)
        {
			HostObject.VariablesPush();

			string bsKeyValue = HostObject.EvalString(@"$[FltKey<18]") + recParent.Get("RefKey").ToString();
			IReportRecord recPos = HostObject.DefineRecord("recPos", "BelegPositionen", 5, 24, bsKeyValue);
			while(! recPos.Eof)
            {
				switch(recPos.Get("SatzArt").ToString())
                {
					case "4":
						LVBereich(recPos);
						break;

					case "6":
					case "7":
						LVPosition(recPos);
						break;
				}
				recPos.GoNext();
            }

			HostObject.VariablesPop();
		}

		void LVPosition(IReportRecord recPos)
		{
			HostObject.VariablesPush();

			string bsKeyValue1 = HostObject.EvalString("$[recTenderer1.TenderNo<20]$[recTenderer1.Tenderer>2]$[recPos.RefKey<6]");
			IReportRecord recPrice1 = HostObject.DefineRecord("recPrice1", "TenderPrice", 0, 28, bsKeyValue1);

			string bsKeyValue2 = HostObject.EvalString("$[recTenderer2.TenderNo<20]$[recTenderer2.Tenderer>2]$[recPos.RefKey<6]");
			IReportRecord recPrice2 = HostObject.DefineRecord("recPrice2", "TenderPrice", 0, 28, bsKeyValue2);

			string bsKeyValue3 = HostObject.EvalString("$[recTenderer3.TenderNo<20]$[recTenderer3.Tenderer>2]$[recPos.RefKey<6]");
			IReportRecord recPrice3 = HostObject.DefineRecord("recPrice3", "TenderPrice", 0, 28, bsKeyValue3);

			string bsKeyValue4 = HostObject.EvalString("$[recTenderer4.TenderNo<20]$[recTenderer4.Tenderer>2]$[recPos.RefKey<6]");
			IReportRecord recPrice4 = HostObject.DefineRecord("recPrice4", "TenderPrice", 0, 28, bsKeyValue4);


			if (m_bAllePositionen || (!recPrice1.Eof) || (!recPrice2.Eof) || (!recPrice3.Eof) || (!recPrice4.Eof))
			{
				HostObject.DefineVariable2("AngebotsMenge1", "N,12,3");
				HostObject.DefineVariable2("AngebotsMenge2", "N,12,3");
				HostObject.DefineVariable2("AngebotsMenge3", "N,12,3");
				HostObject.DefineVariable2("AngebotsMenge4", "N,12,3");
				HostObject.DefineVariable2("AngebotsBetrag1", "N,14,2");
				HostObject.DefineVariable2("AngebotsBetrag2", "N,14,2");
				HostObject.DefineVariable2("AngebotsBetrag3", "N,14,2");
				HostObject.DefineVariable2("AngebotsBetrag4", "N,14,2");
				HostObject.DefineVariable2("ZuschlagsBetrag1", "N,14,2");
				HostObject.DefineVariable2("ZuschlagsBetrag2", "N,14,2");
				HostObject.DefineVariable2("ZuschlagsBetrag3", "N,14,2");
				HostObject.DefineVariable2("ZuschlagsBetrag4", "N,14,2");

				// Ressource ohne Zeilenvorschub + Reservezeile
				HostObject.CheckPageSpace(2);
				m_DoTabulieren = !m_DoTabulieren;
				if (m_DoTabulieren) Tabulieren();
				HostObject.ProcessResource("LVPos_Beginn");

				// Details fuer Tenderer 1
				if (!recPrice1.Eof)
				{
					if (HostObject.EvalString("$[recPrice1.UP]") != "0,00")
					{
						HostObject.Set("AngebotsMenge1", "$[recPrice1.Qty]", true);
						if (HostObject.EvalString("$[AngebotsMenge1]") == "0,000")
                        {
							HostObject.Set("AngebotsMenge1", "$[recPos.Menge]", true);
						}
						HostObject.Set("AngebotsBetrag1", "($[AngebotsMenge1] * $[recPrice1.UP]) * (1.00 - ($[recPrice1.DscntPcnt] / 100.00))", true);
						HostObject.Set("ZuschlagsBetrag1", "($[recPrice1.QtyPurch] * $[recPrice1.UP]) * (1.00 - ($[recPrice1.DscntPcnt] / 100.00))", true);
					}
					HostObject.ProcessResource("LVPos_Tenderer_1");
				}

				// Details fuer Tenderer 2
				if (!recPrice2.Eof)
				{
					if (HostObject.EvalString("$[recPrice2.UP]") != "0,00")
					{
						HostObject.Set("AngebotsMenge2", "$[recPrice2.Qty]", true);
						if (HostObject.EvalString("$[AngebotsMenge2]") == "0,000")
						{
							HostObject.Set("AngebotsMenge2", "$[recPos.Menge]", true);
						}
						HostObject.Set("AngebotsBetrag2", "($[AngebotsMenge2] * $[recPrice2.UP]) * (1.00 - ($[recPrice2.DscntPcnt] / 100.00))", true);
						HostObject.Set("ZuschlagsBetrag2", "($[recPrice2.QtyPurch] * $[recPrice2.UP]) * (1.00 - ($[recPrice2.DscntPcnt] / 100.00))", true);
					}
					HostObject.ProcessResource("LVPos_Tenderer_2");
				}

				// Details fuer Tenderer 3
				if (!recPrice3.Eof)
				{
					if (HostObject.EvalString("$[recPrice3.UP]") != "0,00")
					{
						HostObject.Set("AngebotsMenge3", "$[recPrice3.Qty]", true);
						if (HostObject.EvalString("$[AngebotsMenge3]") == "0,000")
						{
							HostObject.Set("AngebotsMenge3", "$[recPos.Menge]", true);
						}
						HostObject.Set("AngebotsBetrag3", "($[AngebotsMenge3] * $[recPrice3.UP]) * (1.00 - ($[recPrice3.DscntPcnt] / 100.00))", true);
						HostObject.Set("ZuschlagsBetrag3", "($[recPrice3.QtyPurch] * $[recPrice3.UP]) * (1.00 - ($[recPrice3.DscntPcnt] / 100.00))", true);
					}
					HostObject.ProcessResource("LVPos_Tenderer_3");
				}

				// Details fuer Tenderer 4
				if (!recPrice4.Eof)
				{
					if (HostObject.EvalString("$[recPrice4.UP]") != "0,00")
					{
						HostObject.Set("AngebotsMenge4", "$[recPrice4.Qty]", true);
						if (HostObject.EvalString("$[AngebotsMenge4]") == "0,000")
						{
							HostObject.Set("AngebotsMenge4", "$[recPos.Menge]", true);
						}
						HostObject.Set("AngebotsBetrag4", "($[AngebotsMenge4] * $[recPrice4.UP]) * (1.00 - ($[recPrice4.DscntPcnt] / 100.00))", true);
						HostObject.Set("ZuschlagsBetrag4", "($[recPrice4.QtyPurch] * $[recPrice4.UP]) * (1.00 - ($[recPrice4.DscntPcnt] / 100.00))", true);
					}
					HostObject.ProcessResource("LVPos_Tenderer_4");
				}

				// Zeilenvorschub nachholen
				HostObject.ProcessResource("LVPos_Ende");

				HostObject.Set("AngebotsSumme1", "$[AngebotsSumme1] + $[AngebotsBetrag1]", true);
				HostObject.Set("AngebotsSumme2", "$[AngebotsSumme2] + $[AngebotsBetrag2]", true);
				HostObject.Set("AngebotsSumme3", "$[AngebotsSumme3] + $[AngebotsBetrag3]", true);
				HostObject.Set("AngebotsSumme4", "$[AngebotsSumme4] + $[AngebotsBetrag4]", true);
				HostObject.Set("ZuschlagsSumme1", "$[ZuschlagsSumme1] + $[ZuschlagsBetrag1]", true);
				HostObject.Set("ZuschlagsSumme2", "$[ZuschlagsSumme2] + $[ZuschlagsBetrag2]", true);
				HostObject.Set("ZuschlagsSumme3", "$[ZuschlagsSumme3] + $[ZuschlagsBetrag3]", true);
				HostObject.Set("ZuschlagsSumme4", "$[ZuschlagsSumme4] + $[ZuschlagsBetrag4]", true);
			}

			HostObject.VariablesPop();
		}

		void Tabulieren()
		{
			HostObject.VariablesPush();
			HostObject.DefineVariable2("mFillStart", "d,8,2");
			HostObject.Set("mFillStart", "$[RandY] + ($[cyZeile] * $[Zeile])", true);
			HostObject.DefineVariable2("mFillHeight", "d,8,2");
			HostObject.Set("mFillHeight", "$[cyZeile] * 2", true);
			HostObject.WriteLine(HostObject.EvalString("RGBFILL $[RandX];$[mFillStart];$[SizeX];$[mFillHeight];15263976"));
			HostObject.VariablesPop();
		}

		#endregion
	}
}
