using craftsReport.Interfaces;

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

		bool m_bFilter = false;
		string m_cFilter = "";

		public rp1020_xbs()
		{
		}

		#endregion

		#region IReportModule

		public IReportHost HostObject { set; get; }

		// Projektbuchblatt
		public bool Execute()
		{
			m_bFilter = HostObject.IsVar("bFilter") && HostObject.EvalBool("$[bFilter]");
			if ((m_bFilter) && HostObject.IsVar("cFilter"))
				m_cFilter = HostObject.EvalString("$[cFilter]").PadRight(6, '0');

			if (!HostObject.IsVar("mAlias"))
				HostObject.DefineVariable2("mAlias", "C,30,0");
			if (HostObject.IsEmpty("mAlias"))
				HostObject.Set("mAlias", "PrjBucket_TAGLOHN", false);

			HostObject.DefineRecord("recBucket", HostObject.EvalString("$[mAlias]"), 0, 0, "");
			HostObject.DefineRecord("recProjekt", "BELEGVERZEICHNIS", 0, 10, HostObject.EvalString(@"$[recBucket.PrjNr<8] 0"));

			if (!HostObject.IsVar("mRepTitle"))
				HostObject.DefineVariable2("mRepTitle", "c,250,0");
			if (HostObject.IsEmpty("mRepTitle"))
				HostObject.Set("mRepTitle", "Projektbuchungen $[recProjekt.PRJNR]", true);

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

			HostObject.DefineVariable2("mTmpCnt", "l,4,0");   // Zaehler fuer die bearbeiteten Zeilen je Konto
			HostObject.DefineVariable2("mDistKto", "u,8,0");  // aktuelles Konto
			HostObject.DefineVariable2("sumSoll", "N,14,2");  // Summe Soll fuer Konto
			HostObject.DefineVariable2("sumHaben", "N,14,2"); // Summe Haben fuer Konto
			while (!HostObject.TestEof("recBucket"))
			{
				SUB_loopBucket();
				HostObject.SkipRecord("recBucket");
			}

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

		#endregion

		#region IReportModuleEvents

		public void OnAfterPageStart()
		{
			HostObject.ProcessResource("KopfDetail");
		}

		public void OnBeforePageClose()
		{
		}

		public void OnAfterExecute(string szOutFileName)
		{
		}

		#endregion

		#region Internals

		bool SUB_loopBucket()
		{
			HostObject.VariablesPush();

			HostObject.DefineRecord("recKonto", "KONTENPLAN", 0, 9, HostObject.EvalString(@"$[recBucket.Typ<1]$[recBucket.Konto<8]"));
			HostObject.DefineRecord("recZeile", "BUCHUNGSZEILEN", 13, 23, HostObject.EvalString(@"$[recBucket.PrjNr<8]$[recBucket.Typ<1]$[recBucket.Konto<8]$[recBucket.Jahr<4]$[recBucket.Monat<2]"));
			HostObject.DefineVariable2("mcSoll", "C,12,0");
			HostObject.DefineVariable2("mcHaben", "C,12,0");
			HostObject.DefineVariable2("mcSumme", "C,20,0");
			HostObject.DefineVariable2("mnSumme", "N,14,2");

			// ******************************************** Konto hat gewechselt
			if (! HostObject.CompareString("mDistKto", "=", HostObject.EvalString(@"$[recBucket.Konto]")))
			{
				HostObject.CheckPageSpace(2);
				HostObject.ProcessResource("outBuckHead");

				HostObject.Set("mDistKto", "$[recBucket.Konto]", true);
				HostObject.Set("mTmpCnt", "0", true);
				HostObject.Set("sumSoll", "0", true);
				HostObject.Set("sumHaben", "0", true);
			}

			// ******************************************** Zeilen ausgeben
			HostObject.Set("mTmpCnt", @"$[mTmpCnt] + 1", true);
			while (!HostObject.TestEof("recZeile"))
			{
				SUB_loopZeile();
				HostObject.SkipRecord("recZeile");
			}


			// ******************************************** Bucketwerte ausgeben
			bool bDoBucket = true;
			if (m_bFilter)
			{
				if (HostObject.EvalBool("$[recBucket.Jahr] <> $[cFilter<4]"))
					bDoBucket = false;
				if (m_cFilter.Substring(4, 2) != "00" && HostObject.EvalBool("$[recBucket.Monat] <> $[cFilter> 2]"))
					bDoBucket = false;
			}

			if (bDoBucket)
			{
				HostObject.Set("mcSoll", "", true);
				HostObject.Set("mcHaben", "", true);
				if (HostObject.EvalBool("($[recBucket.Soll]) <> (0)"))
					HostObject.Set("mcSoll", "$[recBucket.Soll]", true);
				if (HostObject.EvalBool("($[recBucket.Haben]) <> (0)"))
					HostObject.Set("mcHaben", "$[recBucket.Haben]", true);
				HostObject.Set("mnSumme", "$[recBucket.Haben] - $[recBucket.Soll]", true);
				HostObject.Set("mcSumme", "$[mnSumme] H", true);

				if (!HostObject.EvalBool("$[mnSumme] > 0"))
				{
					HostObject.Set("mnSumme", "$[recBucket.Soll] - $[recBucket.Haben]", true);
					HostObject.Set("mcSumme", "$[mnSumme] S", true);
				}
				HostObject.CheckPageSpace(1);
				HostObject.ProcessResource("outBuckData");
				HostObject.Set("sumSoll", "$[sumSoll] + $[recBucket.Soll]", true);
				HostObject.Set("sumHaben", "$[sumHaben] + $[recBucket.Haben]", true);
			}

			// ******************************************** Gesamtwerte ausgeben
			if (!HostObject.EvalBool("($[mTmpCnt]) < ($[recBucket.Anzahl])"))
			{
				HostObject.Set("mcSoll", "", true);
				HostObject.Set("mcHaben", "", true);
				if (HostObject.EvalBool("($[sumSoll]) <> (0)"))
					HostObject.Set("mcSoll", "$[sumSoll]", true);
				if (HostObject.EvalBool("($[sumHaben]) <> (0)"))
					HostObject.Set("mcHaben", "$[sumHaben]", true);
				HostObject.Set("mnSumme", "$[sumHaben] - $[sumSoll]", true);
				HostObject.Set("mcSumme", "$[mnSumme] H", true);
				if (!HostObject.EvalBool("$[mnSumme] > 0"))
				{
					HostObject.Set("mnSumme", "0 - $[mnSumme]", true);
					HostObject.Set("mcSumme", "$[mnSumme] S", true);
				}

				HostObject.CheckPageSpace(2);
				if (HostObject.EvalBool("$[mnSumme]"))
					HostObject.ProcessResource("outBuckSumm");
				else
					HostObject.LineFeed(1);
			}

			return HostObject.VariablesPop();
		}

		bool SUB_loopZeile()
		{
			if (HostObject.EvalBool("($[recZeile.Soll] = 0) & ($[recZeile.Haben] = 0)"))
				return true;

			if (m_bFilter)
            {
				if (HostObject.EvalBool("$[recZeile.Jahr] <> $[cFilter<4]"))
					return true;
				if (m_cFilter.Substring(4,2) != "00" && HostObject.EvalBool("$[recZeile.Monat] <> $[cFilter> 2]"))
					return true;
			}


			HostObject.VariablesPush();

			HostObject.Set("mcSoll", @"", true);
			HostObject.Set("mcHaben", @"", true);
			if (HostObject.EvalBool("($[recZeile.Soll]) <> (0)"))
				HostObject.Set("mcSoll", @"$[recZeile.Soll]", true);
			if (HostObject.EvalBool("($[recZeile.Haben]) <> (0)"))
				HostObject.Set("mcHaben", @"$[recZeile.Haben]", true);
			HostObject.Set("mnSumme", @"$[recZeile.Haben] - $[recZeile.Soll]", true);
			HostObject.Set("mcSumme", @"$[mnSumme] H", true);
			if (!HostObject.EvalBool("$[mnSumme] > 0"))
			{
				HostObject.Set("mnSumme", @"$[recZeile.Soll] - $[recZeile.Haben]", true);
				HostObject.Set("mcSumme", @"$[mnSumme] S", true);
			}

			HostObject.CheckPageSpace(1); 
			HostObject.ProcessResource("outZeile");
			return HostObject.VariablesPop();
		}

        #endregion
    }
}
