using System;
using BeWo.Report.ReportObjects;
using BeWo.Report;
using DevExpress.XtraReports.UI;
using System.Data;

namespace BeWo.SchoengeistReports
{
    public partial class Monatsabrechnung : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<QueryRO>
    {
        public Monatsabrechnung()
        {
            InitializeComponent();

        }

        public void SetReportDataSource(QueryRO pRO)
        {
            InitTable(pRO);
            this.bindingSource1.DataSource = pRO;
        }

        private void InitTable(QueryRO ro)
        {
            decimal gesamt = 0;

            DateTime monat = DateTime.MinValue;
            foreach (BeWo.Report.ReportObjects.QueryRO.Row row in ro.Rows)
            {
                if (monat == DateTime.MinValue)
                {
                    monat = DateTime.Parse(row.Field8.ToString());
                    lblHeader.Text = String.Format("Abrechnung {0:MMMM yyyy}", monat);
                }

                

                if (row.Field4 != null)
                    row.Field4 = String.Format("{0:0.00}", row.Field4.ToString());
                if (row.Field5 != null)
                    row.Field5 = String.Format("{0:0.00}", row.Field5.ToString());
                if (row.Field6 != null)
                    row.Field6 = String.Format("{0:0.00}", row.Field6.ToString());

                //if (row.Field3 == null)
                //    row.Field3 = String.Empty;
                //if (row.Field4 != null)
                //{
                //    if (row.Field3 != null && row.Field3.ToString().Length > 0)
                //        row.Field3 = row.Field3.ToString() + " ";
                //    row.Field3 = row.Field3.ToString() + row.Field4.ToString();
                //}

                //row.Field5 = String.Format("{0:dd.MM.yy} - {1:dd.MM.yy}", row.Field5, row.Field6);

                decimal betrag = 0;
                if (row.Field7 != null && Decimal.TryParse(row.Field7.ToString(), out betrag))
                {
                    gesamt += betrag;
                    row.Field7 = String.Format("{0:c}", betrag);
                }
                
            }

            lblGesamt.Text = String.Format("Gesamt: {0:c}", gesamt);

        }

    }
}
