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 = "ABW")]
    public partial class RechnungABW : XtraReport, IBeWoReport<ServiceInvoiceRO>
    {
		public RechnungABW()
        {
            this.InitializeComponent();
        }

        public void SetReportDataSource(ServiceInvoiceRO pRO)
        {
	        if (!String.IsNullOrEmpty(pRO.RecipientOrganisation) && pRO.RecipientOrganisation.IndexOf("ABW pauschal") >= 0)
	        {
		        pRO.RecipientOrganisation = pRO.RecipientOrganisation.Replace("ABW pauschal", "").Trim();
	        }
			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>();

			ServiceInvoiceItemRO pauschaleItem = null;

	        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.ServiceDescription == "Monatliche Pauschale")
					        {
						        if (pauschaleItem == null)
						        {
									pauschaleItem = new ServiceInvoiceItemRO();
									pauschaleItem.UnitCount = 1;
									pauschaleItem.UnitDescription = "1";
									pauschaleItem.NetAmount = 0;
						        }

						        pauschaleItem.NetAmount += i.NetAmount ?? 0;

					        }
					        else 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("{0} {1:MMMM yyyy}", i.ServiceDescription, i.AccountingPeriodStart);
										}
										else
										{
											i.ServiceDescription = String.Format("{0} {1:MMMM yyyy} - {2:MMMM yyyy}", i.ServiceDescription, 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);
					        }
					       
				        }

			        }

		        }
					
	        }

	        if (pauschaleItem != null)
	        {
		        if (startDate.HasValue && endDate.HasValue
		            && startDate.Value.Date.Year == endDate.Value.Date.Year
		            && startDate.Value.Date.Month == endDate.Value.Date.Month)
		        {
					pauschaleItem.ServiceDescription = String.Format("Betreuungskosten {0:MMMM yyyy}", startDate);
		        }
		        else
		        {
					pauschaleItem.ServiceDescription = String.Format("Betreuungskosten {0:MMMM yyyy} - {1:MMMM yyyy}", startDate,
				        endDate);
		        }

				newPeriod.ServiceInvoiceItems.Insert(0, pauschaleItem);
	        }
			
	        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))
	        {
		        sb.AppendLine(pRO.RecipientOrganisation);
	        }
			if (!String.IsNullOrEmpty(pRO.RecipientContactPerson))
	        {
				sb.AppendLine(pRO.RecipientContactPerson);
	        }
			if (!String.IsNullOrEmpty(pRO.RecipientDivision))
	        {
		        sb.AppendLine(pRO.RecipientDivision);
	        }
			if (!String.IsNullOrEmpty(pRO.RecipientStreet))
	        {
		        sb.AppendLine(pRO.RecipientStreet);
	        }
			if (!String.IsNullOrEmpty(pRO.RecipientPoBox))
			{
				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;
        }

		private void PageFooter_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
		{
			e.Cancel = PrintingSystem.PageCount > 0;
		}

    }
}