using System;
using System.Linq;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using System.Collections.Generic;

namespace BeWo.BeWoNeuss
{
	public partial class Finanzauswertung : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<FinanceRO>
    {
        public Finanzauswertung()
        {
            InitializeComponent();
        }

		public void SetReportDataSource(FinanceRO pRO)
        {
			if (pRO.ReportStartDate.HasValue && pRO.ReportEndDate.HasValue)
			{
				DateTime start = pRO.ReportStartDate.Value;
				DateTime end = pRO.ReportEndDate.Value;

				
				if (start.Year == end.Year && start.Month == end.Month && start.Day == 1 &&
				    end.Day == DateTime.DaysInMonth(end.Year, end.Month))
				{
					lblTitel.Text = String.Format("Finanzauswertung {0:MMMM yyyy}", start);
					cellBewilligtImZeitraumHeader.Text = String.Format("bewilligte FLS {0:MMMM}", start);
					cellGeleistetImZeitraumHeader.Text = String.Format("geleistete Std. {0:MMMM} bewilligt", start);
					
				}
				else
				{
					lblTitel.Text = String.Format("Finanzauswertung {0:dd.MM.yyyy}-{1:dd.MM.yyyy}", start, end);
					cellBewilligtImZeitraumHeader.Text = String.Format("bewilligte FLS {0:dd.MM.yyyy}-{1:dd.MM.yyyy}", start, end);
					cellGeleistetImZeitraumHeader.Text = String.Format("geleistete Std. {0:dd.MM.yyyy}-{1:dd.MM.yyyy} bewilligt", start, end);
					
				}
				if (pRO.RateFactor.HasValue)
				{
					cellRateFactorHeader.Text = String.Format("geleistete Std. x {0:0.##}", pRO.RateFactor);
				}
				else
				{
					cellRateFactorHeader.Visible = false;
					cellRateFactor.Visible = false;
				}
				
			}
			else
			{
				cellBewilligtImZeitraumHeader.Visible = false;
				cellBewilligteFLSImBereich.Visible = false;
				cellGeleistetImBereich.Visible = false;
				cellGeleistetImZeitraumHeader.Visible = false;
				cellRateFactorHeader.Visible = false;
				cellRateFactor.Visible = false;
			}

			if (pRO.SupportConceptFinanceList != null)
			{
				foreach (var scro in pRO.SupportConceptFinanceList)
				{
					scro.Costbearer2Supportconcept.MonthlyPayment = 0;

					if (scro.Costbearer2Supportconcept != null)
					{
						if (!scro.Costbearer2Supportconcept.ApprovedEndDate.HasValue ||
						    !scro.Costbearer2Supportconcept.ApprovedStartDate.HasValue)
						{
							scro.ApprovedHoursInReportTimeSpan = 0;
							scro.Costbearer2Supportconcept.MonthlyPayment = scro.BillableHoursInReportTimeSpan;
							scro.BillableHoursInReportTimeSpan = 0;
							scro.AmountTotal = 0;
							scro.OpenAmount = 0;
							scro.ApprovedHours = 0;
						}

						Customer c =
							DAOFactory.GenericDAO.LoadByID<Customer>(scro.Costbearer2Supportconcept.SupportConcept.Customer.CustomerOid);

						scro.Costbearer2Supportconcept.CustomerReferenceNumber = "";
						foreach (var e2c in c.Employee2CustomerList)
						{
							foreach (var v2o in e2c.ValueList)
							{
								if (v2o.Entry.Value == "Hauptbetreuung Viersen")
								{
									scro.Costbearer2Supportconcept.CustomerReferenceNumber = "V";			
								}
							}

						}
						
					}
				}
			}
			this.bindingSource1.DataSource = pRO;
        }

    }

}
