using System;
using BeWo.Report;
using BeWo.Report.ReportObjects;

using DevExpress.XtraReports.UI;

namespace BeWo.DiakonieKKKleve
{
    public partial class ServiceInvoiceReport : XtraReport, IBeWoReport<ServiceInvoiceRO>
    {
        public ServiceInvoiceReport()
        {
            this.InitializeComponent();
        }

        public void SetReportDataSource(ServiceInvoiceRO pRO)
        {

			lblAddress.Text = "";
			if (!String.IsNullOrEmpty(pRO.RecipientOrganisation) && !String.IsNullOrEmpty(pRO.RecipientOrganisation.Trim()))
	        {
		        lblAddress.Text += pRO.RecipientOrganisation + "\n";
	        }
			if (!String.IsNullOrEmpty(pRO.RecipientContactPerson) && !String.IsNullOrEmpty(pRO.RecipientContactPerson.Trim()))
			{
				lblAddress.Text += pRO.RecipientContactPerson + "\n";
			}
			if (!String.IsNullOrEmpty(pRO.RecipientDivision) && !String.IsNullOrEmpty(pRO.RecipientDivision.Trim()))
			{
				lblAddress.Text += pRO.RecipientDivision + "\n";
			}
			if (!String.IsNullOrEmpty(pRO.RecipientStreet) && !String.IsNullOrEmpty(pRO.RecipientStreet.Trim()))
			{
				lblAddress.Text += pRO.RecipientStreet + "\n";
			}
			if (!String.IsNullOrEmpty(pRO.RecipientPostCodeAndTown) && !String.IsNullOrEmpty(pRO.RecipientPostCodeAndTown.Trim()))
			{
				lblAddress.Text += pRO.RecipientPostCodeAndTown;
			}


			DateTime start = pRO.AccountingPeriodStart;
			DateTime end = pRO.AccountingPeriodEnd;


			if (start.Month == end.Month && start.Year == end.Year)
			{
				lblAbrechnungszeitraum.Text = String.Format("hiermit berechnen wir für den Monat {0:MMMM yyyy} folgende erbrachte Tätigkeiten:", start);
			}
			else
			{
				lblAbrechnungszeitraum.Text = String.Format("hiermit berechnen wir für den Zeitraum {0:MMMM yyyy} - {1:MMMM yyyy} folgende erbrachte Tätigkeiten:", start, end);
			}
			
	        decimal rateFactor = 1;
	        decimal hourlyRate = 0;
	        decimal minutenGesamt = 0;
			if (pRO.ServiceInvoicePeriods != null)
			{
				foreach (var p in pRO.ServiceInvoicePeriods)
				{
					if (p.ServiceInvoiceItems != null)
					{
						foreach (var i in p.ServiceInvoiceItems)
						{
							if (!String.IsNullOrEmpty(i.ServiceDescription) && i.ServiceDescription.StartsWith("Assistenz"))
							{
								i.ServiceDescription = "Assistenz";
							}
							else
							{
								i.ServiceDescription = "BEWO";
							}
							if (i.UnitCount.HasValue)
							{
								minutenGesamt += i.UnitCount.Value*60;
							}
							if (!String.IsNullOrEmpty(i.RateFactor))
							{
								var rfStr = i.RateFactor.Replace("%", "").Replace(",00", "").Trim();
								int rfInt = 0;
								Int32.TryParse(rfStr, out rfInt);

								rateFactor = (100m + rfInt)/100m;
							}
							if (i.HourlyRate.HasValue)
							{
								hourlyRate = i.HourlyRate.Value;
							}
						}
					}
				}
			}
	        minutenGesamt = Math.Round(minutenGesamt, 0, MidpointRounding.AwayFromZero);

	        if (rateFactor == 1)
	        {
		        lblFaktorMinuten.Visible = false;
		        lblRatefactorLabel.Visible = false;
	        }
	        
			decimal faktorMinuten = (minutenGesamt * rateFactor) - minutenGesamt;
			lblGesamtMinuten.Text = String.Format("{0:0} Minuten", minutenGesamt);
	        lblFaktorMinuten.Text = String.Format("{0:0} Minuten", faktorMinuten);
			lblGesamtUndFaktorMinuten.Text = String.Format("{0:0} Minuten", faktorMinuten + minutenGesamt);
			lblGesamtUndFaktorStunden.Text = String.Format("{0:0.##} Stunden", (faktorMinuten + minutenGesamt) / 60);
	        lblGesamtBetrag.Text = pRO.GrossClaim.Replace("€", "EUR");
	        lblGesamtBetrag2.Text = lblGesamtBetrag.Text;

			lblXMinutenEntsprechen.Text = String.Format("{0:0} Minuten entsprechen", faktorMinuten + minutenGesamt);

			lblRechnungsbetragText.Text = String.Format("Rechnungsbetrag für {0:0.##} Stunden zu {1:0.00} EUR/Std.", (faktorMinuten + minutenGesamt) / 60, hourlyRate);
			lblRechnungsbetragText2.Text = String.Format("Von Ihnen zu zahlender Betrag für\r\n{0:0.##} Stunden zu {1:0.00} EUR/Std.", (faktorMinuten + minutenGesamt) / 60, hourlyRate);

            this.bindingSource1.DataSource = pRO;
        }
    }
}