using System;
using BeWo.Report.ReportObjects;
using BeWo.Report;
using BeWo.Data.Entities;
using BeWo.Data.Access;

namespace BeWo.BetreuWoReports
{
    public partial class Leistungsdokumentation : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<FLSReportRO>
    {
        public Leistungsdokumentation()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(FLSReportRO pRO)
        {
            if (pRO.FLSReportGroups != null)
            {
                foreach (BeWo.Report.ReportObjects.FLSReportRO.FLSReportGroup group in pRO.FLSReportGroups)
                {
                    if (group.ServiceRecords != null)
                    {
                        foreach (var sr in group.ServiceRecords)
                        {
                            if (sr.ServiceDescription == "Freizeit" && sr.ServiceCategory == "Selbstzahler Freizeit" && sr.EmployeeOid.HasValue)
                            {
                                Employee emp = DAOFactory.GenericDAO.LoadByID<Employee>(sr.EmployeeOid.Value);

                                Contract c = emp.GetValidContractForDate(sr.Start);
                                if (c != null && c.HourlyRate.HasValue)
                                {
                                    decimal amount = (sr.RoundedDuration / 60) * c.HourlyRate.Value;
                                    group.EmployeePayment -= amount;
                                }

                                group.TotalFLM -= sr.RoundedDuration;
                                sr.RoundedDuration = 0;
                            }
                        }

                        int h = (int)Math.Floor(group.TotalFLM / 60);
                        int m = (int)group.TotalFLM % 60;

                        group.TotalHourMinuteString = String.Format("{0}:{1:00}", h, m);

                        
                    }

                    if (group.EmploymentTypeOid != null && 
						(group.EmploymentTypeOid == 19 ||
						group.EmploymentTypeOid == 34 ||
						group.EmploymentTypeOid == 47 ||
						group.EmploymentTypeOid == 50 ||
						group.EmploymentTypeOid == 54 ||
						group.EmploymentTypeOid == 57 ||
						group.EmploymentTypeOid == 60 ||
						group.EmploymentTypeOid == 63 ||
						group.EmploymentTypeOid == 66))
                    {
                        if (pRO.ReportStartDate.HasValue && pRO.ReportStartDate.Value >= new DateTime(2013, 1, 1))
                        {
                            if (group.EmployeePayment != null && group.EmployeePayment > 450)
                            {
                                group.EmployeePayment = 450;
                            }
                        }
                        else
                        {
                            if (group.EmployeePayment != null && group.EmployeePayment > 400)
                            {
                                group.EmployeePayment = 400;
                            }
                        }
                    }
					if (group.EmployeePayment != null)
					{
						decimal amount = group.EmployeePayment.Value;
						//amount *= 2;

						//amount = Math.Round(amount);
						//amount /= 2;
						amount = Math.Round(amount, 1, MidpointRounding.AwayFromZero);

						group.EmployeePayment = amount;

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

        

    }
}
