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

namespace LebenshilfeCoburg
{
	
    public partial class AusbildungsassistenzRechnung : XtraReport, IBeWoReport<ServiceInvoiceRO>
    {
		public AusbildungsassistenzRechnung()
        {
            this.InitializeComponent();
        }

        public void SetReportDataSource(ServiceInvoiceRO pRO)
        {
			DateTime start = pRO.AccountingPeriodStart;
			DateTime end = pRO.AccountingPeriodEnd;

	        if (pRO.CustomerSalutation == "Herr")
	        {
		        pRO.CustomerSalutation = "Herrn";
	        }
	

	        decimal approved = 0;
	        decimal approvedFactor = 0;

	        
	        foreach (var sip in pRO.ServiceInvoicePeriods)
	        {
		        approved += sip.ApprovedHours ?? 0;

		        int count = 0;
				ServiceInvoiceItemRO abschlag = null;
		        decimal amount = 0;
		        foreach (var ii in sip.ServiceInvoiceItems)
		        {
			        if (ii.ServiceDescription == "Abschlag")
			        {
				        abschlag = ii;
			        }
			        else
			        {
				        amount += ii.NetAmount ?? 0;
			        }
			        if (count == 0)
			        {
				        if (ii.AccountingPeriodStart.Value.Month == ii.AccountingPeriodEnd.Value.Month &&
				            ii.AccountingPeriodStart.Value.Year == ii.AccountingPeriodEnd.Value.Year)
				        {
					        ii.UnitDescription = String.Format("{0:MMMM yy}", ii.AccountingPeriodStart.Value);
				        }
				        else
				        {
							ii.UnitDescription = String.Format("{0:MMMM yy} - {1:MMMM yy}", ii.AccountingPeriodStart.Value,
						        ii.AccountingPeriodEnd.Value);
				        }
			        }
			       
			        count++;
		        }

		        if (abschlag != null)
		        {
			        ServiceInvoiceItemRO diff = new ServiceInvoiceItemRO();
					diff.ServiceDescription = "Differenz";
					diff.NetAmount = amount + (abschlag.NetAmount ?? 0);
					diff.GrossAmount = String.Format("{0:c}", diff.NetAmount);
			        sip.ServiceInvoiceItems.Add(diff);
		        }
	        }


			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();


			if (pRO.AccountingPeriodStart.Date.Year == pRO.AccountingPeriodEnd.Date.Year
				&& pRO.AccountingPeriodStart.Date.Month == pRO.AccountingPeriodEnd.Date.Month)
			{
				lblMonth.Text = String.Format("{0:MMMM yyyy}", pRO.AccountingPeriodStart);
			}
			else
			{
				lblMonth.Text = String.Format("{0:MMMM yyyy} - {1:MMMM yyyy}", pRO.AccountingPeriodStart,
					pRO.AccountingPeriodEnd);
			}

		


            this.bindingSource1.DataSource = pRO;
        }
    }
}