using System;
using System.Collections.Generic;
using System.Text;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using BS.Shared.Core;
using DevExpress.XtraReports.UI;

namespace BeWo.GpzBiberachReports
{
	[IDSpecificClass(Identifier = "Miete")]
    public partial class RechnungMiete : XtraReport, IBeWoReport<ServiceInvoiceRO>
    {
		public RechnungMiete()
        {
            this.InitializeComponent();
        }

        public void SetReportDataSource(ServiceInvoiceRO pRO)
        {
	        pRO.InvoiceNumber = String.Format("8235-{0}", pRO.InvoiceNumber);

			decimal gesamtBetrag = 0;

			DateTime? startDate = null;
			DateTime? endDate = null;

	        var newPeriods = new List<ServiceInvoicePeriodRO>();
			var newPeriod = new ServiceInvoicePeriodRO();
			newPeriods.Add(newPeriod);

			newPeriod.ServiceInvoiceItems = new List<ServiceInvoiceItemRO>();


	        if (pRO.ServiceInvoicePeriods != null && pRO.ServiceInvoicePeriods.Count > 0)
	        {
				foreach (var p in pRO.ServiceInvoicePeriods)
		        {
			    
			        if (p.ServiceInvoiceItems != null && p.ServiceInvoiceItems.Count > 0)
			        {
				        foreach (var i in p.ServiceInvoiceItems)
				        {
					        if (i.AccountingPeriodStart.HasValue)
					        {
						        if (startDate == null || startDate > i.AccountingPeriodStart.Value)
						        {
							        startDate = i.AccountingPeriodStart.Value;
						        }
					        }
					        if (i.AccountingPeriodEnd.HasValue)
					        {
						        if (endDate == null || endDate < i.AccountingPeriodEnd.Value)
						        {
							        endDate = i.AccountingPeriodEnd.Value;
						        }
					        }

					        gesamtBetrag += i.NetAmount ?? 0;

					        if (i.NetAmount.HasValue && i.NetAmount.Value != 0)
					        {
						        if (i.UnitCount.HasValue)
						        {
							        if (i.ServiceRecord == null)
							        {
										i.UnitCount = 1;
										i.UnitDescription = "1";

										if (i.AccountingPeriodStart.HasValue && i.AccountingPeriodEnd.HasValue
											&& i.AccountingPeriodStart.Value.Date.Year == i.AccountingPeriodEnd.Value.Date.Year
												&& i.AccountingPeriodStart.Value.Date.Month == i.AccountingPeriodEnd.Value.Date.Month)
										{
											i.ServiceDescription = String.Format("Miete {0:MMMM yyyy}", i.AccountingPeriodStart);
										}
										else
										{
											i.ServiceDescription = String.Format("Miete {0:MMMM yyyy} - {1:MMMM yyyy}", i.AccountingPeriodStart,
												i.AccountingPeriodEnd);
										}

							        }
							        else
							        {
										i.UnitCount = i.UnitCount.Value * 60;
										i.ServiceDescription = String.Format("{0} {1:dd.MM.yyyy}", i.ServiceDescription, i.AccountingPeriodStart);
										i.UnitDescription = String.Format("{0:0} min.", i.UnitCount);    
							        }
							        
						        }

								newPeriod.ServiceInvoiceItems.Add(i);
					        }
					       
				        }

			        }

		        }
					
	        }

	      
	        pRO.ServiceInvoicePeriods = newPeriods;
			//cellMenge4.Text = "%";
			//cellArtikel4.Text = "Anzuwendender Steuersatz: 19 % / 7 %\nbzw. steuerfreie Leistung 0 %";
			//cellArtikel5.Text = "Gesamt";
			//cellBetrag5.Text = String.Format("{0:c}", gesamtBetrag);

			StringBuilder sb = new StringBuilder();

			if (!String.IsNullOrEmpty(pRO.RecipientOrganisation) && !String.IsNullOrEmpty(pRO.RecipientOrganisation.Trim()))
	        {
		        sb.AppendLine(pRO.RecipientOrganisation);
	        }
			if (!String.IsNullOrEmpty(pRO.RecipientContactPerson) && !String.IsNullOrEmpty(pRO.RecipientContactPerson.Trim()))
	        {
				sb.AppendLine(pRO.RecipientContactPerson);
	        }
			if (!String.IsNullOrEmpty(pRO.RecipientDivision) && !String.IsNullOrEmpty(pRO.RecipientDivision.Trim()))
	        {
		        sb.AppendLine(pRO.RecipientDivision);
	        }
			if (!String.IsNullOrEmpty(pRO.RecipientStreet) && !String.IsNullOrEmpty(pRO.RecipientStreet.Trim()))
	        {
		        sb.AppendLine(pRO.RecipientStreet);
	        }
			if (!String.IsNullOrEmpty(pRO.RecipientPoBox) && !String.IsNullOrEmpty(pRO.RecipientPoBox.Trim()))
			{
				sb.Append("Postfach ");
				sb.AppendLine(pRO.RecipientPoBox.Replace("Postfach", "").Trim());
			}
			if (!String.IsNullOrEmpty(pRO.RecipientPostCodeAndTown))
	        {
		        sb.AppendLine(pRO.RecipientPostCodeAndTown);
	        }
	        lblAnschrift.Text = sb.ToString();



	        this.bindingSource1.DataSource = pRO;
        }

    }
}