using craftsReport.Interfaces;

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

		private bool m_IsLastPage = false;

		public rp0507_xbs()
		{
		}

		#endregion

		#region IReportModule

		public IReportHost HostObject { set; get; }

		public bool Execute()
		{
			if (!HostObject.IsVar("mRepTitle"))
				HostObject.DefineVariable2("mRepTitle", "c,250,0");
			if (HostObject.IsVar("mHeader"))
				HostObject.Set("mRepTitle", "$[mHeader]", true);
			if (HostObject.IsEmpty("mRepTitle"))
				HostObject.Set("mRepTitle", "Bestellung", true);

			HostObject.DefineRecord("recKOP", "BESTELLKOPF", 0, 15, HostObject.EvalString(@"$[mNRHW<15]"));
			HostObject.DefineRecord("recPOS", "BESTELLPOSITIONEN", 0, 15, HostObject.EvalString(@"$[mNRHW<15]"));
			HostObject.DefineRecord("recADR1", "ADRESSSTAMM", 5, 4, HostObject.EvalString(@" 3$[recKOP.LKZ<2]"));
			HostObject.DefineRecord("recADR2", "ADRESSSTAMM", 0, 15, HostObject.EvalString(@"$[recKOP.LADRNR<15]"));
			HostObject.DefineRecord("recWKZ", "WAEHRUNGEN", 1, 6, HostObject.EvalString(@"$[recKOP.WAEHR<6]"));
			HostObject.DefineRecord("recPRJ", "BELEGVERZEICHNIS", 0, 8, HostObject.EvalString(@"$[recKOP.PRJNR<8] 0"));
			//HostObject.DefineRecord("recEINH", "MENGENEINHEITEN", 0, 0, HostObject.EvalString(@""));

			if (!HostObject.IsVar("bLaddr"))
				HostObject.DefineVariable2("bLaddr", "L,1,0");
			if (HostObject.IsEmpty("recKOP.LADRNR"))
				HostObject.Set("bLaddr", @"0", true);

			if (!HostObject.IsVar("mBelegNr"))
				HostObject.DefineVariable2("mBelegNr", "c,20,0");
			if (HostObject.IsEmpty("mBelegNr"))
				HostObject.Set("mBelegNr", "$[recKOP.PRJNR]/$[mNRHW]", true);

			HostObject.DefineVariable2("mUpperUZ", "u,4,0");
			HostObject.Set("mUpperUZ", "$[recKOP.BEARB]", true);
			HostObject.DefineRecord("recUZName", "ZUSATZTEXTE", 0, 12, HostObject.EvalString("SYSNAME4$[mUpperUZ<4]"));
			HostObject.DefineRecord("recUZMail", "ZUSATZTEXTE", 0, 12, HostObject.EvalString("SYSMAIL4$[mUpperUZ<4]"));
			HostObject.DefineRecord("recUZFon",  "ZUSATZTEXTE", 0, 12, HostObject.EvalString("SYSFON4$[mUpperUZ<4]"));

			HostObject.DefineRecord("recABRUF", "BESTELLABRUF", 0, 23, HostObject.EvalString(@"$[mNRHW<15]$[mAbruf>8]"));
			if (!HostObject.TestEof("recABRUF"))
			{
				// TODO
				//HostObject.Set("mHeader", @"$[mHeader] - Teil $[recABRUF.LfdNr]", true);
				//HostObject.Set("mTermin", @"$[recABRUF.Datum]", true);
			}
			/*
			HostObject.DefineVariable2("mTermin", "D,10,0");
			HostObject.Set("mTermin", @"$[recKOP.TERMIN]", true);

			if (HostObject.EvalBool("$[mNoPrs]"))
				HostObject.Set("mDrkUeber", @"0", true);

			//  eigene Anschrift aus Belegvorlauf holen
			HostObject.DefineVariable2("mADRNR", "u,2,0");
			HostObject.Set("mADRNR", @"$[mAnschrift>2]", true);
			if (HostObject.EvalBool("$[mADRNR] < 10"))
				HostObject.Set("mADRNR", @"0$[mADRNR>1]", true);
			HostObject.DefineRecord("recEIG", "EIGENE_ANSCHRIFTEN", 0, 2, HostObject.EvalString(@"$[mADRNR>2]"));
			*/
			HostObject.DefineVariable2("mSumme", "N,14,2");
			HostObject.DefineVariable2("mTextZeile", "c,250,0");

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

			while (!HostObject.TestEof("recPOS"))
			{
				Handle_recPOS();
				HostObject.SkipRecord("recPOS");
			}
			EndSumme();
			Nachtext();
			//HostObject.FormFeed();

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

			return true;
		}

		#endregion

		#region IReportModuleEvents

		public void OnAfterPageStart()
		{
			if (HostObject.EvalInt("$[Seite]") == 1)
			{
				HostObject.Set("VorlaufLabel1", "Kundennummer", true);
				HostObject.Set("VorlaufValue1", "$[recKOP.KDNRHW]", true);
				HostObject.Set("VorlaufLabel2", "Datum", true);
				HostObject.Set("VorlaufValue2", "$[recKOP.Datum]", true);
				HostObject.Set("VorlaufLabel3", "Termin", true);
				HostObject.Set("VorlaufValue3", "$[recKOP.TERMIN]", true);
				HostObject.Set("VorlaufLabel4", "Bearbeiter", true);
				HostObject.Set("VorlaufValue4", "$[recUZName.Text42]", true);
				HostObject.Set("VorlaufLabel5", "Durchwahl", true);
				HostObject.Set("VorlaufValue5", "$[recUZFon.Text42]", true);
				//HostObject.Set("VorlaufLabel6", "EMail", true);
				HostObject.Set("VorlaufValue6", "$[recUZMail.Text42]", true);

				HostObject.ProcessDestAddress("recADR1");
				HostObject.ProcessResource("Adresse");
				HostObject.ProcessResource("Vorlauf");
				HostObject.ProcessResource("Betreff");
				LieferAdresse();
				Vortext();

				// Kopf nur Drucken, wenn sich der Text nicht auf der Folgeseite fortgesetzt wurde.
				if (HostObject.EvalInt("$[Seite]") == 1)
					HostObject.ProcessResource("KopfDetail");
			}
			else
			{
				HostObject.ProcessResource("KopfDetail");

				if (!HostObject.EvalBool("$[mNoPrs]"))
				{
					HostObject.ProcessResource("UebertragVon");
				}
			}
		}

		public void OnBeforePageClose()
		{
			if (m_IsLastPage)
			{
				//HostObject.ProcessResource("Sterne");
			}
			else
			{
				if (!HostObject.EvalBool("$[mNoPrs]"))
				{
					int nextPageNo = HostObject.EvalInt("$[Seite]") + 1;
					HostObject.VariablesPush();
					HostObject.DefineVariable2("NextPageNo", "N,3,0");
					HostObject.Set("NextPageNo", nextPageNo.ToString(), false);
					HostObject.Set("Zeile", "$[ZlMax] - 1", true);
					HostObject.ProcessResource("UebertragAuf");
					HostObject.VariablesPop();
				}
			}
		}

		public void OnAfterExecute(string szOutFileName)
		{
		}

		#endregion

		#region Internals

		void LieferAdresse()
        {
			if (HostObject.EvalBool("$[bLaddr]"))
            {
				HostObject.ProcessDestAddress("recADR2");
				HostObject.ProcessResource("LieferAdresse");
			}
		}

		void Vortext()
		{
			if (HostObject.EvalBool("$[mDrkVT]"))
			{
				string bsTextNr = HostObject.EvalString("$[recKOP.NRHW]");
				string bsText = HostObject.ReadMasterDataTextBlob(26, "VT", bsTextNr).Replace("\r", "").TrimEnd('\n');
				if ((bsText != null) && (bsText != ""))
				{
					string[] textLines = bsText.Split('\n');
					foreach (string textLine in textLines)
					{
						HostObject.CheckPageSpace(3);
						HostObject.ProcessLTextLine(textLine, false);
					}

					HostObject.LineFeed(1);
				}
			}
		}

		void Nachtext()
		{
			if (HostObject.EvalBool("$[mDrkNT]"))
			{
				string bsTextNr = HostObject.EvalString("$[recKOP.NRHW]");
				string bsText = HostObject.ReadMasterDataTextBlob(26, "NT", bsTextNr).Replace("\r", "").TrimEnd('\n');
				if ((bsText != null) && (bsText != ""))
				{
					HostObject.LineFeed(1);

					string[] textLines = bsText.Split('\n');
					foreach (string textLine in textLines)
					{
						HostObject.CheckPageSpace(3);
						HostObject.ProcessLTextLine(textLine, false);
					}
				}
			}
		}

		void Handle_recPOS()
		{
			HostObject.VariablesPush();

			//  Detail der Positionsschleife
			HostObject.DefineVariable2("mMenge", "N,12,3");
			HostObject.DefineVariable2("cMenge", "C,12,0");
			HostObject.DefineVariable2("mNetto", "N,14,2");
			HostObject.Set("mMenge", @"$[recPOS.MENGE]", true);
			HostObject.Set("mNetto", @"$[recPOS.NETTO]", true);

			HostObject.DefineVariable2("mPosNr", "C,20,0");
			HostObject.DefineVariable2("mPrsEH", "N,5,0");
			HostObject.Set("mPrsEH", @"1", true);
			if (HostObject.EvalBool("$[recPOS.PE]=1"))
				HostObject.Set("mPrsEH", @"10", true);
			if (HostObject.EvalBool("$[recPOS.PE]=2"))
				HostObject.Set("mPrsEH", @"100", true);
			if (HostObject.EvalBool("$[recPOS.PE]=3"))
				HostObject.Set("mPrsEH", @"1000", true);
			if (HostObject.EvalBool("$[recPOS.PE]=4"))
				HostObject.Set("mPrsEH", @"10000", true);

			// HostObject.FindRecord("recEINH", HostObject.EvalString("$[recPOS.ME>4]"));
			HostObject.DefineRecord("recEinheit", "MENGENEINHEITEN", 0, 4, HostObject.EvalInt("$[recPOS.ME]").ToString().PadLeft(4));
			HostObject.DefineRecord("recMENGE", "BESTELLABRUFMENGEN", 1, 33, HostObject.EvalString(@"$[recKOP.NRHW<15]$[recPOS.POSNRHW<10]$[recABRUF.LfdNr>8]"));

			if (!HostObject.TestEof("recABRUF"))
			{
				if (HostObject.TestEof("recMENGE"))
				{
					HostObject.VariablesPop();
					return;
				}

				HostObject.Set("mMenge", @"$[recMENGE.MENGE]", true);
				HostObject.Set("mNetto", @"($[mMenge] * $[recPOS.BRUTTO]) * (1.00 - ($[recPOS.RAB1]/100.00))", true);
				HostObject.Set("mNetto", @"($[mNetto]) * (1.00 - ($[recPOS.RAB2]/100.00))", true);
				HostObject.Set("mNetto", @"$[mNetto] / $[mPrsEH]", true);
			}

			if (HostObject.EvalBool("$[mLVPOSNR]"))
				HostObject.Set("mPosNr", @"$[recPOS.LVNR]", true);
			else
				HostObject.Set("mPosNr", @"$[recPOS.POSNRHW]", true);

			// Druckmenge
			string bsMenge = HostObject.EvalString("$[mMenge]").TrimEnd('0').TrimEnd(',');
			HostObject.Set("cMenge", bsMenge, false);

			// ========================== keine Textposition ?
			if (!HostObject.CompareString("recPOS.PosTyp", "=", HostObject.EvalString(@"T")))
			{
				HostObject.CheckPageSpace(4);

				if (HostObject.EvalBool("$[mNoPrs]"))
					HostObject.ProcessResource("DetailOhnePreis");
				else
					HostObject.ProcessResource("DetailMitPreis");

				// =========================== NEP ?
				if (!HostObject.EvalBool("$[recPOS.Alternativ]"))
					HostObject.Set("mSumme", @"$[mSumme] + $[mNetto]", true);
			}

			//  Positionstext
			if (!HostObject.IsEmpty("recPOS.TEXT1"))
			{
				HostObject.Set("mTextZeile", @"$[recPOS.TEXT1]", true);
				HostObject.CheckPageSpace(3);
				HostObject.ProcessResource("DetailTextZeile");
			}
			if (!HostObject.IsEmpty("recPOS.TEXT2"))
			{
				HostObject.Set("mTextZeile", @"$[recPOS.TEXT2]", true);
				HostObject.CheckPageSpace(3);
				HostObject.ProcessResource("DetailTextZeile");
			}

			//  Datensatzschleife BESTELLTEXTERGÄNZUNG
			HostObject.DefineRecord("recPST", "BESTELLTEXTERGÄNZUNG", 0, 25, HostObject.EvalString(@"$[mNRHW<15]$[recPOS.POSNRHW<10]"));
			while (!HostObject.TestEof("recPST"))
			{
				HostObject.Set("mTextZeile", @"$[recPST.TEXT]", true);
				HostObject.CheckPageSpace(3);
				HostObject.ProcessResource("DetailTextZeile");
				HostObject.SkipRecord("recPST");
			}

			HostObject.VariablesPop();
		}

		void EndSumme()
        {
			if (!HostObject.EvalBool("$[mNoPrs]"))
			{
				HostObject.CheckPageSpace(5);
				HostObject.ProcessResource("EndSumme");
				HostObject.Set("mNoPrs", "1", false);  // kein neuer Uebertrag mehr
			}
		}

		#endregion
	}
}
