using System;
using System.Linq;
using System.Text;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using BeWo.Service.ServiceImplementations;
using BS.Shared;

namespace BeWo.WegweiserBetreuungsdienstReports
{
    public partial class Forderungen : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<QueryRO>
    {

        public Forderungen()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(QueryRO pRO)
        {

            this.bindingSource1.DataSource = CreateDataSource(pRO);
        }

        private QueryRO CreateDataSource(QueryRO ro)
        {
            QueryRO newQueryRo = new QueryRO();
			//DateTime? appointmentDate = null;
			//if (ro.Rows != null)
			//{
			//	DateTime dt;
			//	if (DateTime.TryParse(ro.Rows[0].Field1.ToString(), out dt))
			//	{
			//		dt = new DateTime(dt.Year, dt.Month, dt.Day);
			//		dt = dt.AddMonths(1).AddDays(-1).Date;
			//		appointmentDate = dt;

			//		xrLabel13.Text = String.Format("Forderungen/Verbindlichkeiten aus lfd. Hilfeplänen vom {0:dd.MM.yyyy}", dt);
			//	}
			//}

			

	        AnalysisServiceImp ai = new AnalysisServiceImp();
            //OperationsServiceImp oi = new OperationsServiceImp();
            //IList<SupportConcept> list = DAOFactory.GenericDAO.GetAllActive<SupportConcept>();
	        var forderungen = ai.GetOutstandingReceivables(true, false);

            
            decimal totalEntgelt = 0;
            decimal totalBezahlt = 0;
            decimal totalDiff = 0;

            foreach (var f in forderungen)
            {
	            if (f.SupportConcept != null && f.SupportConcept.ActivationType == ActivationTypeId.Active && f.OpenAmount > 0)
	            {
		            QueryRO.Row row = new QueryRO.Row();

		            row.Field1 = String.Format("{0}\r\n{1}", f.CustomerName, f.SupportConceptTimeSpanString);
		            row.Field2 = f.CostBearerName;
		            row.Field5 = f.TotalAmountString;
		            row.Field6 = f.PaidAmountString;
		            row.Field7 = f.OpenAmountString;

		            newQueryRo.Rows.Add(row);
	            }
            }

            //lblGesamtBewilligtFLS.Text = String.Format("{0:#,##0.00}", totalBewilligt);
            //lblGesamtGeleistetFLS.Text = String.Format("{0:#,##0.00}", totalGeleistet);
            //lblGesamtEntgeltFLS.Text = String.Format("{0:c}", totalEntgelt);
            //lblGesamtBezahl.Text = String.Format("{0:c}", totalBezahlt);
            //lblGesamtDiff.Text = String.Format("{0:c}", totalDiff);

	        lblGesamtEntgeltFLS.Text = "";
			lblGesamtBezahl.Text = "";
			lblGesamtDiff.Text = "";

            newQueryRo.Rows.Sort((a, b) => a.Field1.ToString().CompareTo(b.Field1));
            
            return newQueryRo;
        }


    }

}
