using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using BeWo.Data.Access;
using BeWo.Data.Entities;

using BS.Shared.DataContracts;
using BS.Shared.Extensions;
using BS.Shared;
using BeWo.Service.Invoicing;
using BeWo.Data;

namespace BeWo.Report.ReportObjects
{
	public class SettlementRO : AbstractBeWoReportObject<SettlementRO>
	{
		public long CostBearer2SupportConceptOid { get; set; }

		public string Abrechnungstext1 { get; set; }

		public string Abrechnungstext2 { get; set; }

		public string Abrechnungstext3 { get; set; }

		public DateTime? AbsenceEndDate1 { get; set; }

		public DateTime? AbsenceEndDate2 { get; set; }

		public DateTime? AbsenceEndDate3 { get; set; }

		public DateTime? AbsenceEndDate4 { get; set; }

		public string AbsenceReason1 { get; set; }

		public string AbsenceReason2 { get; set; }

		public string AbsenceReason3 { get; set; }

		public string AbsenceReason4 { get; set; }

		public DateTime? AbsenceStartDate1 { get; set; }

		public DateTime? AbsenceStartDate2 { get; set; }

		public DateTime? AbsenceStartDate3 { get; set; }

		public DateTime? AbsenceStartDate4 { get; set; }

		public string AbsenceTimes { get; set; }

		public string AccountingEndDateString { get; set; }

		public DateTime AccountingEndDate { get; set; }

		public string AccountingPeriod { get; set; }

		public string AccountingStartDateString { get; set; }

		public DateTime AccountingStartDate { get; set; }

		public string AmountAdvancedPayments { get; set; }

		public decimal AmountRecordedFLS { get; set; }

		public string AmountRecordedFLSString { get; set; }

		public decimal AmountTotalAdvancedPaidWithHourlyRate3 { get; set; }

		public decimal? AmountTotalAdvancedPaidWithNewHourlyRate { get; set; }

		public decimal? AmountTotalAdvancedPaidWithOldHourlyRate { get; set; }

		public decimal AmountTotalWithHourlyRate3 { get; set; }

		public decimal? AmountTotalWithNewHourlyRate { get; set; }

		public decimal? AmountTotalWithOldHourlyRate { get; set; }

		public DateTime ApprovalNoticeDate { get; set; }

		public string ApprovalNoticeDateString { get; set; }

		public string ApprovedFLS { get; set; }

		public decimal ApprovedFLSWeekly { get; set; }

		public string ApprovedFLSWeeklyString { get; set; }

		public decimal? ApprovedFLSWithActualHourlyRate { get; set; }

		public string ApprovedFLSWithFactor { get; set; }

		public decimal ApprovedFLSWithHourlyRate3 { get; set; }

		public decimal? ApprovedFLSWithOldHourlyRate { get; set; }

		public string Betrag1 { get; set; }

		public string Betrag2 { get; set; }

		public string Betrag3 { get; set; }

		public string Claim { get; set; }

		public decimal ClaimWithEigenbeteiligung { get; set; }

		public string CreatorName { get; set; }

		public DateTime? CustomerBirthday { get; set; }

		public string CustomerBirthdayString { get; set; }

		public string CustomerFirstName { get; set; }

		public string CustomerLastName { get; set; }

		public string CustomerName { get; set; }

		public string CustomerReferenceNumber { get; set; }

		public string CustomerReferenceNumberAndDate { get; set; }

		public string CustomerStreet { get; set; }
		
		public string CustomerPostalCode { get; set; }

		public string CustomerTown { get; set; }

		public int DifferentHourlyRateCount { get; set; }

		public string DebitorNumber { get; set; }

		public string CostCenter { get; set; }

		public decimal? Eigenbeteiligung { get; set; }

		public string FinalSentence { get; set; }

		public decimal? GeleisteteFLSMitStundensatz3 { get; set; }

		public decimal? GeleisteteFLSMitStundensatzAktuell { get; set; }

		public decimal? GeleisteteFLSMitStundensatzAlt { get; set; }

		public decimal HourlyRate3 { get; set; }

		public string HourlyRate3EndDateString { get; set; }

		public string HourlyRate3StartDateString { get; set; }

		public string HourlyRate3String { get; set; }

		public DateTime? HourlyRateEndDate3 { get; set; }

		public decimal? HourlyRateNew { get; set; }

		public DateTime? HourlyRateNewStartDate { get; set; }

		public string HourlyRateNewStartDateString { get; set; }

		public string HourlyRateNewString { get; set; }

		public decimal? HourlyRateOld { get; set; }

		public DateTime? HourlyRateOldEndDate { get; set; }

		public string HourlyRateOldEndDateString { get; set; }

		public DateTime? HourlyRateOldStartDate { get; set; }

		public string HourlyRateOldStartDateString { get; set; }

		public string HourlyRateOldString { get; set; }

		public DateTime? InvoiceDateTime { get; set; }

		public string InvoiceDate { get; set; }

		public string InvoiceNumber { get; set; }

		public string Notice { get; set; }

		public decimal RateFactor { get; set; }

		public string RateFactorAmount { get; set; }

		public string RateFactorText { get; set; }

		public string RateFactorText2 { get; set; }

		public string RecipientContactPerson { get; set; }

		public string RecipientDivision { get; set; }

		public string RecipientOrganisation { get; set; }

		public string RecipientPostCodeAndTown { get; set; }

		public string RecipientStreet { get; set; }

		public string RecipientPhone { get; set; }

		public string RecipientFax { get; set; }

		public string RecordedFLS { get; set; }

		public double RecordedFLSDouble { get; set; }

		public double RecordedFLSTotal { get; set; }

		public string RecordedFLSWithHourlyRate3String { get; set; }

		public string RecordedFLSWithHourlyRate3StringUntilMax { get; set; }

		public string RecordedFLSWithNewHourlyRateString { get; set; }

		public string RecordedFLSWithNewHourlyRateStringUntilMax { get; set; }

		public string RecordedFLSWithOldHourlyRateString { get; set; }

		public string RecordedFLSWithOldHourlyRateStringUntilMax { get; set; }

		public string Salutation1 { get; set; }

		public string Salutation2 { get; set; }

		public string SenderContactPersonLastname { get; set; }

		public string SenderContactPersonTitle { get; set; }

		public string SenderContactPerson { get; set; }

		public string SenderContactPersonDivision { get; set; }

		public string SenderContactPersonFax { get; set; }

		public string SenderContactPersonMail { get; set; }

		public string SenderContactPersonPhone { get; set; }

		public decimal TotalApprovedFLSWithHourlyRate3 { get; set; }

		public string TotalApprovedFLSWithHourlyRate3String { get; set; }

		public decimal TotalApprovedFLSWithNewHourlyRate { get; set; }

		public string TotalApprovedFLSWithNewHourlyRateString { get; set; }

		public decimal TotalApprovedFLSWithOldHourlyRate { get; set; }

		public string TotalApprovedFLSWithOldHourlyRateString { get; set; }

		public decimal? WeeksInAccountingPeriod { get; set; }

		public decimal WeeksWithHourlyRate3 { get; set; }

		public string WeeksWithHourlyRate3String { get; set; }

		public decimal WeeksWithNewHourlyRate { get; set; }

		public string WeeksWithNewHourlyRateString { get; set; }

		public decimal WeeksWithOldHourlyRate { get; set; }

		public string WeeksWithOldHourlyRateString { get; set; }

		public DateTime? TerminationDate { get; set; }

		public String TerminationReason { get; set; }

		public IList<ApprovalPeriodRO> Approvals { get; set; }

		public IList<ApprovalPeriod> CostbearerApprovalPeriods { get; set; }

		public IList<InvoiceItemDC> InvoiceItems { get; set; }



		public class ApprovalPeriodRO
		{
			public DateTime? PeriodStartDate { get; set; }

			public DateTime? PeriodEndDate { get; set; }

			public decimal? AmountPerUnit { get; set; }

			public decimal? AmountTotal { get; set; }

			public decimal? GrossAmountTotal { get; set; }

			public string ItemDescription { get; set; }

			public string DetailDescription { get; set; }

			public decimal? RateFactor { get; set; }

			public decimal? UnitCount { get; set; }

			public string UnitDescription { get; set; }

			public decimal ReceivedAmount { get; set; }

			public decimal? MaxApprovedUnitCount { get; set; }

			public decimal? ApprovedPerUnit { get; set; }

			public AccountingIntervalType? ApprovalUnit { get; set; }

			public decimal? MaxApprovedAmount { get; set; }
		}

		public static SettlementRO Create(Settlement2DC pDC)
		{
			var lResult = new SettlementRO();

			lResult.CostBearer2SupportConceptOid = pDC.CostBearer2SupportConceptOid.Value;
			lResult.InvoiceItems = pDC.InvoiceItems;

			
			CostBearer2SupportConcept lCb2Sc =
				DAOFactory.GenericDAO.LoadByID<CostBearer2SupportConcept>(pDC.CostBearer2SupportConceptOid.Value);
			Customer lCustomer = lCb2Sc.SupportConcept.Customer;

			String tenant = SessionFacade.Tenant;

			var creator = SettlementInvoiceCreation.GetInstance(lCb2Sc.CostBearer.ID, tenant);

			lResult.CostbearerApprovalPeriods = creator.CreateApprovalPeriods(pDC, lCb2Sc);

			if (pDC.ApprovedFLSWeekly != null)
			{
				lResult.ApprovedFLSWeekly = pDC.ApprovedFLSWeekly.Value;
			}
			lResult.Approvals = new List<ApprovalPeriodRO>();
			foreach (var period in lCb2Sc.ApprovalPeriodList)
			{
				ApprovalPeriodRO ap = new ApprovalPeriodRO();
				ap.PeriodStartDate = period.StartDate;
				ap.PeriodEndDate = period.EndDate;
				ap.AmountPerUnit = 0;
				ap.AmountTotal = 0;
				if (period.ApprovedBEInterval.HasValue)
				{
					switch (period.ApprovedBEInterval.Value)
					{
						case SupportConceptApprovalInterval.Daily:
							ap.ApprovalUnit = AccountingIntervalType.Daily;
							break;
						case SupportConceptApprovalInterval.Weekly:
							ap.ApprovalUnit = AccountingIntervalType.Weekly;
							break;
						case SupportConceptApprovalInterval.Fortnightly:
							ap.ApprovalUnit = AccountingIntervalType.Fortnightly;
							break;
						case SupportConceptApprovalInterval.Monthly:
							ap.ApprovalUnit = AccountingIntervalType.Monthly;
							break;
						case SupportConceptApprovalInterval.Quarterly:
							ap.ApprovalUnit = AccountingIntervalType.Quarterly;
							break;
						case SupportConceptApprovalInterval.HalfYearly:
							ap.ApprovalUnit = AccountingIntervalType.HalfYearly;
							break;
						case SupportConceptApprovalInterval.Yearly:
							ap.ApprovalUnit = AccountingIntervalType.Yearly;
							break;
						default:
							throw new ArgumentOutOfRangeException();
					}
				}

				ap.ApprovedPerUnit = period.ApprovedBEPerInterval;

				lResult.Approvals.Add(ap);
			}

			lResult.ApprovedFLSWeekly = Math.Round(lResult.ApprovedFLSWeekly, 2, MidpointRounding.AwayFromZero);

			lResult.ApprovedFLSWeeklyString = string.Format("{0:0.00}", lResult.ApprovedFLSWeekly);

			lResult.ApprovedFLS = string.Format("{0:0.00} FLS", pDC.ApprovedFLS);

			if (pDC.ApprovedFLS != null && pDC.ApprovedFLS < pDC.RecordedBillableFLSNetto)
			{
				lResult.RecordedFLS = string.Format("{0:0.00} FLS", pDC.ApprovedFLS);
				lResult.RecordedFLSDouble = (double) pDC.ApprovedFLS.Value;
			}
			else
			{
				lResult.RecordedFLS = string.Format("{0:0.00} FLS", pDC.RecordedBillableFLSNetto);
				lResult.RecordedFLSDouble = (double) pDC.RecordedBillableFLSNetto;
			}
			lResult.RecordedFLSTotal = (double)pDC.RecordedBillableFLSNetto;
			lResult.AmountAdvancedPayments = string.Format("{0:n} €", pDC.AmountAdvancedPayments);

			if (pDC.AmountBillableTotal != null)
			{
				lResult.AmountRecordedFLS = pDC.AmountBillableTotal.Value;
			}

			lResult.AmountRecordedFLSString = string.Format("{0:n} €", pDC.AmountBillableTotal);

			decimal fact = 1;
			lResult.RateFactor = 1;
			if (pDC.RateFactor != null && pDC.RateFactor.Value > 0)
			{
				// lResult.RateFactorText = string.Format("Inkl. pauschaler Faktor ({0:0.00}%)", pDC.RateFactor);
				// lResult.RateFactorAmount = string.Format("{0:n} €", (pDC.AmountRecordedBillableFLS * pDC.RateFactor) / 100);
				fact = (100 + pDC.RateFactor.Value)/100;

				lResult.ApprovedFLSWithFactor = string.Format("{0:0.00} FLS", pDC.ApprovedFLS*(decimal) fact);
				lResult.RateFactorText = "Inkl. pauschaler Faktor";
				lResult.RateFactorText2 = Math.Round(fact, 2, MidpointRounding.AwayFromZero).ToString();
				lResult.RateFactorAmount = string.Format("{0:n} €", pDC.AmountWithRateFactor);
				lResult.RateFactor = fact;
			}


			lResult.ApprovedFLSWithActualHourlyRate = pDC.ApprovedFLSWithActualHourlyRate*(decimal) fact;
			lResult.ApprovedFLSWithOldHourlyRate = pDC.ApprovedFLSWithOldHourlyRate*(decimal) fact;
			lResult.ApprovedFLSWithHourlyRate3 = pDC.ApprovedFLSWithHourlyRate3*(decimal) fact;

			lResult.RecordedFLSWithNewHourlyRateString = string.Format("{0:0.00}", lResult.ApprovedFLSWithActualHourlyRate);
			lResult.RecordedFLSWithOldHourlyRateString = string.Format("{0:0.00}", lResult.ApprovedFLSWithOldHourlyRate);
			lResult.RecordedFLSWithHourlyRate3String = string.Format("{0:0.00}", lResult.ApprovedFLSWithHourlyRate3);

			lResult.GeleisteteFLSMitStundensatzAktuell = pDC.ApprovedFLSWithActualHourlyRate;
			lResult.GeleisteteFLSMitStundensatzAlt = pDC.ApprovedFLSWithOldHourlyRate;
			lResult.GeleisteteFLSMitStundensatz3 = pDC.ApprovedFLSWithHourlyRate3;

			lResult.HourlyRateOldStartDate = pDC.HourlyRateOldStartDate;
			if (lResult.HourlyRateOldStartDate != null)
			{
				lResult.HourlyRateOldStartDateString = String.Format("{0:dd.MM.yyyy}", pDC.HourlyRateOldStartDate);
			}

			lResult.HourlyRateOldEndDate = pDC.HourlyRateOldEndDate;
			if (lResult.HourlyRateOldEndDate != null)
			{
				lResult.HourlyRateOldEndDateString = String.Format("{0:dd.MM.yyyy}", pDC.HourlyRateOldEndDate);
			}

			lResult.HourlyRateNewStartDate = pDC.HourlyRateNewStartDate;
			if (lResult.HourlyRateNewStartDate != null)
			{
				lResult.HourlyRateNewStartDateString = String.Format("{0:dd.MM.yyyy}", pDC.HourlyRateNewStartDate);
			}

			lResult.HourlyRateNew = pDC.HourlyRateNew;
			lResult.HourlyRateNewString = string.Format("{0:n}", pDC.HourlyRateNew);
			lResult.HourlyRateOld = pDC.HourlyRateOld;
			if (lResult.HourlyRateOld != null)
			{
				lResult.HourlyRateOldString = string.Format("{0:n}", pDC.HourlyRateOld.Value);
			}

			lResult.AmountTotalAdvancedPaidWithNewHourlyRate = pDC.AmountTotalAdvancedPaidWithNewHourlyRate;
			lResult.AmountTotalAdvancedPaidWithOldHourlyRate = pDC.AmountTotalAdvancedPaidWithOldHourlyRate;

			lResult.AmountTotalWithNewHourlyRate = (pDC.HourlyRateNew ?? 0)*(lResult.ApprovedFLSWithActualHourlyRate ?? 0);
			lResult.AmountTotalWithOldHourlyRate = (pDC.HourlyRateOld ?? 0)*(lResult.ApprovedFLSWithOldHourlyRate ?? 0);

			lResult.DifferentHourlyRateCount = pDC.DifferentHourlyRateCount;
			lResult.HourlyRate3 = pDC.HourlyRate3;

			lResult.HourlyRateEndDate3 = pDC.HourlyRateEndDate3;
			if (lResult.HourlyRateEndDate3 != null)
			{
				lResult.HourlyRate3EndDateString = String.Format("{0:dd.MM.yyyy}", lResult.HourlyRateEndDate3.Value);
			}

			lResult.HourlyRate3String = string.Format("{0:n}", pDC.HourlyRate3);
			lResult.DifferentHourlyRateCount = pDC.DifferentHourlyRateCount;
			lResult.AmountTotalAdvancedPaidWithHourlyRate3 = pDC.AmountTotalAdvancedPaidWithHourlyRate3;
			lResult.AmountTotalWithHourlyRate3 = pDC.HourlyRate3*lResult.ApprovedFLSWithHourlyRate3;

			lResult.WeeksWithNewHourlyRate = pDC.WeeksWithNewHourlyRate;
			lResult.WeeksWithOldHourlyRate = pDC.WeeksWithOldHourlyRate;
			lResult.WeeksWithHourlyRate3 = pDC.WeeksWithHourlyRate3;

			lResult.WeeksWithNewHourlyRateString = string.Format("{0:0.00}", pDC.WeeksWithNewHourlyRate);
			lResult.WeeksWithOldHourlyRateString = string.Format("{0:0.00}", pDC.WeeksWithOldHourlyRate);
			lResult.WeeksWithHourlyRate3String = string.Format("{0:0.00}", pDC.WeeksWithHourlyRate3);

			if (lResult.DifferentHourlyRateCount == 1)
			{
				if (pDC.ApprovedFLS != null)
				{
					lResult.TotalApprovedFLSWithNewHourlyRate = pDC.ApprovedFLS.Value;
				}
			}
			else
			{
				lResult.TotalApprovedFLSWithNewHourlyRate = lResult.ApprovedFLSWeekly*lResult.WeeksWithNewHourlyRate;
			}

			lResult.TotalApprovedFLSWithOldHourlyRate = lResult.ApprovedFLSWeekly*lResult.WeeksWithOldHourlyRate;
			lResult.TotalApprovedFLSWithHourlyRate3 = lResult.ApprovedFLSWeekly*lResult.WeeksWithHourlyRate3;

			lResult.TotalApprovedFLSWithNewHourlyRate = Math.Round(lResult.TotalApprovedFLSWithNewHourlyRate, 2,
				MidpointRounding.AwayFromZero);
			lResult.TotalApprovedFLSWithOldHourlyRate = Math.Round(lResult.TotalApprovedFLSWithOldHourlyRate, 2,
				MidpointRounding.AwayFromZero);
			lResult.TotalApprovedFLSWithHourlyRate3 = Math.Round(lResult.TotalApprovedFLSWithHourlyRate3, 2,
				MidpointRounding.AwayFromZero);

			lResult.TotalApprovedFLSWithNewHourlyRateString = string.Format("{0:0.00}", lResult.TotalApprovedFLSWithNewHourlyRate);
			lResult.TotalApprovedFLSWithOldHourlyRateString = string.Format("{0:0.00}", lResult.TotalApprovedFLSWithOldHourlyRate);
			lResult.TotalApprovedFLSWithHourlyRate3String = string.Format("{0:0.00}", lResult.TotalApprovedFLSWithHourlyRate3);

		    lResult.RecordedFLSWithNewHourlyRateStringUntilMax = string.Format("{0:0.00}", lResult.ApprovedFLSWithActualHourlyRate < lResult.TotalApprovedFLSWithNewHourlyRate ? lResult.ApprovedFLSWithActualHourlyRate : lResult.TotalApprovedFLSWithNewHourlyRate);

		    lResult.RecordedFLSWithOldHourlyRateStringUntilMax = string.Format("{0:0.00}", lResult.ApprovedFLSWithOldHourlyRate < lResult.TotalApprovedFLSWithOldHourlyRate ? lResult.ApprovedFLSWithOldHourlyRate : lResult.TotalApprovedFLSWithOldHourlyRate);

		    lResult.RecordedFLSWithHourlyRate3StringUntilMax = lResult.ApprovedFLSWithHourlyRate3 < lResult.TotalApprovedFLSWithHourlyRate3 ? string.Format("{0:0.00}", pDC.ApprovedFLSWithHourlyRate3*fact) : string.Format("{0:0.00}", lResult.TotalApprovedFLSWithHourlyRate3*fact);

			if (pDC.Claim == null)
			{
				pDC.Claim = 0;
			}

			if (pDC.Claim != null)
			{
				lResult.ClaimWithEigenbeteiligung = pDC.Claim.Value;
			}

			lResult.Claim = string.Format("{0:n} €", pDC.Claim);

			if (pDC.Claim > 0)
			{
				lResult.FinalSentence = "Bitte überweisen Sie unsere Restforderung nach Prüfung der Schlussabrechnung auf das unten angegebene Konto.";
			}
			else if (pDC.Claim == 0)
			{
				lResult.FinalSentence = "Alle Ansprüche sind ausgeglichen.";
			}
			else if (pDC.Claim < 0)
			{
				lResult.FinalSentence = "Die geleistete Überzahlung wird in den nächsten Tagen ihrem Konto gutgeschrieben.";
			}

			lResult.AccountingPeriod = String.Format("{0:dd.MM.yyyy}", pDC.AccountingPeriodStart.Value) + " bis " +
			                           String.Format("{0:dd.MM.yyyy}", pDC.AccountingPeriodEnd.Value);

			double days = pDC.AccountingPeriodEnd.Value.Subtract(pDC.AccountingPeriodStart.Value).TotalDays + 1;
			lResult.WeeksInAccountingPeriod = (decimal) (days/7);

			lResult.AccountingStartDate = pDC.AccountingPeriodStart.Value;
			lResult.AccountingEndDate = pDC.AccountingPeriodEnd.Value;

			lResult.AccountingStartDateString = String.Format("{0:dd.MM.yyyy}", pDC.AccountingPeriodStart.Value);
			lResult.AccountingEndDateString = String.Format("{0:dd.MM.yyyy}", pDC.AccountingPeriodEnd.Value);

			lResult.CreatorName = pDC.CreatorFirstName + " " + pDC.CreatorLastName;

			lResult.Salutation1 = lCustomer.Person.Sex == Sex.Male ? "Herr" : "Frau";

			lResult.Salutation2 = lCustomer.Person.Sex == Sex.Male ? "Herrn" : "Frau";

			lResult.CustomerName = lCustomer.Person.FirstName + " " + lCustomer.Person.LastName;
			lResult.Eigenbeteiligung = pDC.EquityContribution;

			lResult.DebitorNumber = lCustomer.DebitorNumber;
			lResult.CostCenter = lCustomer.CostCenter;

			
			lResult.TerminationDate = lCustomer.TerminationDate;
			if (lCustomer.TerminationReason != null)
				lResult.TerminationReason = lCustomer.TerminationReason.Value;

			// if (lResult.Eigenbeteiligung != null)
			// lResult.ClaimWithEigenbeteiligung = pDC.Claim.Value - lResult.Eigenbeteiligung.Value;
			// else
			// lResult.ClaimWithEigenbeteiligung = pDC.Claim.Value;
			lResult.CustomerFirstName = lCustomer.Person.FirstName;
			lResult.CustomerLastName = lCustomer.Person.LastName;
			lResult.CustomerBirthday = lCustomer.Person.DateOfBirth;

			if (lCustomer.Person.Address != null)
			{
				lResult.CustomerStreet = lCustomer.Person.Address.Street;
				lResult.CustomerPostalCode = lCustomer.Person.Address.PostalCode;
				lResult.CustomerTown = lCustomer.Person.Address.Town;
			}

			if (lResult.CustomerBirthday != null)
			{
				lResult.CustomerBirthdayString = String.Format("{0:dd.MM.yyyy}", lResult.CustomerBirthday.Value);
			}

			lResult.CustomerReferenceNumber = pDC.CustomerReferenceNumber;
			lResult.CustomerReferenceNumberAndDate = "Az.: " + pDC.CustomerReferenceNumber;
			if (pDC.ApprovalNoticeDate != null)
			{
				lResult.ApprovalNoticeDate = pDC.ApprovalNoticeDate.Value;
				lResult.ApprovalNoticeDateString = String.Format("{0:dd.MM.yyyy}", pDC.ApprovalNoticeDate);
				lResult.CustomerReferenceNumberAndDate += " - Bewilligungsbescheid vom " +
				                                          String.Format("{0:dd.MM.yyyy}", pDC.ApprovalNoticeDate);
			}

			lResult.InvoiceDateTime = pDC.InvoiceDate;
			lResult.InvoiceDate = String.Format("{0:dd.MM.yyyy}", pDC.InvoiceDate.Value);
			lResult.InvoiceNumber = pDC.InvoiceNumber;
			//lResult.Notice = String.IsNullOrEmpty(pDC.Notice) ? pDC.Notice : pDC.Notice.Replace("\r\n", "\n");
			lResult.Notice = pDC.Notice;


			lResult.RecipientContactPerson = pDC.RecipientContactPerson;
			lResult.RecipientDivision = pDC.RecipientDivision;
			lResult.RecipientOrganisation = pDC.RecipientOrganisation;
			lResult.RecipientPostCodeAndTown = pDC.RecipientPostCode + " " + pDC.RecipientTown;
			lResult.RecipientStreet = pDC.RecipientStreet;

			var org = lCb2Sc.CostBearer.Organisation;
			if (org != null)
			{
				if (org.Contacts != null)
				{
					var contact =
						org.Contacts.FirstOrDefault(con => con.Type == ContactType.business_Fax);

					if (contact != null)
						lResult.RecipientFax = contact.Value;

					contact =
						org.Contacts.FirstOrDefault(con => con.Type == ContactType.business_Phone);

					if (contact != null)
						lResult.RecipientPhone = contact.Value;
				}
			}

			lResult.SenderContactPerson = pDC.SenderContactPersonFirstName + " " + pDC.SenderContactPersonLastName;
			lResult.SenderContactPersonLastname = pDC.SenderContactPersonLastName;
			//lResult.SenderContactPersonTitle = pDC.s
			lResult.SenderContactPersonDivision = pDC.SenderContactPersonDivision;

			if (!pDC.SenderContactPersonFax.IsNullOrEmpty())
			{
				lResult.SenderContactPersonFax = "Fax: " + pDC.SenderContactPersonFax;
			}

			if (!pDC.SenderContactPersonMail.IsNullOrEmpty())
			{
				lResult.SenderContactPersonMail = "E-Mail: " + pDC.SenderContactPersonMail;
			}

			if (!pDC.SenderContactPersonPhone.IsNullOrEmpty())
			{
				lResult.SenderContactPersonPhone = "Tel.: " + pDC.SenderContactPersonPhone;
			}

			lResult.AbsenceTimes = string.Empty;

			if (lCustomer.AbsenceTimes != null)
			{
				var sb = new StringBuilder();

				var count = 1;

				foreach (var at in lCustomer.AbsenceTimes)
				{
					if (at.AbsenceSpan != null)
					{
						bool cont = true;

						if (at.Start != null && at.Start > pDC.AccountingPeriodEnd)
						{
							cont = false;
						}

						if (cont && at.End != null && at.End < pDC.AccountingPeriodStart)
						{
							cont = false;
						}

						if (cont)
						{
							if (sb.Length > 0)
							{
								sb.Append(", ");
							}

							if (at.Start.HasValue)
							{
								if (count == 1)
								{
									lResult.AbsenceStartDate1 = at.Start.Value;
								}

								if (count == 2)
								{
									lResult.AbsenceStartDate2 = at.Start.Value;
								}

								if (count == 3)
								{
									lResult.AbsenceStartDate3 = at.Start.Value;
								}

								if (count == 4)
								{
									lResult.AbsenceStartDate4 = at.Start.Value;
								}

								sb.Append(string.Format("{0:00}", at.Start.Value.Day));
								sb.Append(".");
								sb.Append(string.Format("{0:00}", at.Start.Value.Month));
								sb.Append(".");
								sb.Append(at.Start.Value.Year.ToString().Substring(2));

								if (at.End.HasValue)
								{
									if (count == 1)
									{
										lResult.AbsenceEndDate1 = at.End.Value;
									}

									if (count == 2)
									{
										lResult.AbsenceEndDate2 = at.End.Value;
									}

									if (count == 3)
									{
										lResult.AbsenceEndDate3 = at.End.Value;
									}

									if (count == 4)
									{
										lResult.AbsenceEndDate4 = at.End.Value;
									}

									sb.Append(" - ");
									sb.Append(string.Format("{0:00}", at.End.Value.Day));
									sb.Append(".");
									sb.Append(string.Format("{0:00}", at.End.Value.Month));
									sb.Append(".");
									sb.Append(at.End.Value.Year.ToString().Substring(2));
								}

								if (at.AbsenceReason != null)
								{
									if (count == 1)
									{
										lResult.AbsenceReason1 = at.AbsenceReason.Description;
									}

									if (count == 2)
									{
										lResult.AbsenceReason2 = at.AbsenceReason.Description;
									}

									if (count == 3)
									{
										lResult.AbsenceReason3 = at.AbsenceReason.Description;
									}

									if (count == 4)
									{
										lResult.AbsenceReason4 = at.AbsenceReason.Description;
									}
								}
							}

							sb.Append(" ");
							sb.Append(at.AbsenceReason.Description);

							count++;
						}
					}
				}

				if (sb.Length > 0)
				{
					lResult.AbsenceTimes = "Abwesenheitszeiten: " + sb;
				}
			}

			return lResult;
		}
	}
}
    