using System;
using System.Linq;
using BeWo.Report;
using BeWo.Report.ReportObjects;

using DevExpress.XtraReports.UI;

using BS.Shared.Core;

namespace demo_reports
{
    [IDSpecificClass(Identifier = "Sammelrechnung")]
    public partial class Sammelrechnung : XtraReport, IBeWoReport<ServiceInvoiceRO>
    {
        public Sammelrechnung()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(ServiceInvoiceRO pRO)
        {
	        DateTime dtStart = pRO.AccountingPeriodStart;
	        DateTime dtEnd = pRO.AccountingPeriodEnd;
			
			int monate = (int)Math.Ceiling(((decimal)dtEnd.Subtract(dtStart).TotalDays) / (365m / 12m));
			cellMonate.Text = monate.ToString();

	        decimal approvedHours = 0;
	        decimal? hourlyRate = null;
	        decimal providedHours = 0;

	        foreach (var sip in pRO.ServiceInvoicePeriods)
	        {
		        foreach (var sii in sip.ServiceInvoiceItems)
		        {
			        if (!hourlyRate.HasValue && sii.HourlyRate.HasValue)
			        {
				        hourlyRate = sii.HourlyRate;
			        }
			        providedHours += sii.Hours ?? 0;
		        }
		        approvedHours += sip.ApprovedHours ?? 0;
		        
	        }
	        cellHourlyRate.Text = String.Format("{0:c}", hourlyRate);
			cellApprovedHours.Text = String.Format("{0:0.##}", approvedHours);
			cellProvidedHours.Text = String.Format("{0:0.##}", providedHours);
			cellProvidedMin.Text = String.Format("{0:0}", providedHours * 60m);
            bindingSource1.DataSource = pRO;
        }
    }
}