using System;
using System.Text;
using BeWo.Report.ReportObjects;

using DevExpress.XtraReports.UI;

namespace BeWo.Report.DefaultReports
{
    public partial class ServiceInvoiceReport : XtraReport, IBeWoReport<ServiceInvoiceRO>
    {
        public ServiceInvoiceReport()
        {
            this.InitializeComponent();
        }

        public void SetReportDataSource(ServiceInvoiceRO pRO)
        {
			decimal hourlyRate = 0;
			decimal rateFactor = 1;
			decimal hours = 0;

	        StringBuilder sb = new StringBuilder();
			sb.Append(pRO.CustomerSalutation);
			sb.Append(" ");
	        sb.AppendLine(pRO.CustomerName);
			sb.AppendLine(pRO.CustomerStreet);
			sb.Append(pRO.CustomerPostalCode);
			sb.Append(" ");
			sb.AppendLine(pRO.CustomerTown);

	        lblAnschrift.Text = sb.ToString();

			if (pRO.ServiceInvoicePeriods != null && pRO.ServiceInvoicePeriods.Count > 0)
			{
				foreach (var p in pRO.ServiceInvoicePeriods)
				{

					if (p.ServiceInvoiceItems != null && p.ServiceInvoiceItems.Count > 0)
					{

						foreach (var i in p.ServiceInvoiceItems)
						{
							if (i.HourlyRate.HasValue)
							{
								hourlyRate = i.HourlyRate.Value;

								if (!String.IsNullOrEmpty(i.RateFactor))
								{
									var rfStr = i.RateFactor.Replace("%", "").Replace(",00", "").Trim();
									int rfInt = 0;
									Int32.TryParse(rfStr, out rfInt);

									rateFactor = (100m + rfInt) / 100m;
								}
							}

							if (i.Hours.HasValue)
								hours += i.Hours.Value;



						}

					}
				}

			}
			hours *= rateFactor;
			
			lblSatz.Text = String.Format("{0:c}", hourlyRate);
			lblAnzahl.Text = String.Format("{0:0.##}", hours);
			lblBetrag.Text = String.Format("{0}", pRO.NetClaim);

	        if (!String.IsNullOrEmpty(pRO.CustomerSalutation))
	        {
		        if (pRO.CustomerSalutation.ToLower() == "frau")
		        {
			        lblAnrede.Text = String.Format("Sehr geehrte Frau {0}", pRO.CustomerLastName);
		        }
		        else
		        {
			        lblAnrede.Text = String.Format("Sehr geehrter Herr {0}", pRO.CustomerLastName);
		        }
	        }
	        this.bindingSource1.DataSource = pRO;
        }
    }
}