using System;
using System.Text;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using BS.Shared;
using BS.Shared.Core;
using DevExpress.XtraReports.UI;

namespace BeWo.LebenshilfeDuisburgReports
{
	[IDSpecificClass(Identifier = "LH-BeWo")]
    public partial class ServiceInvoiceReportBeWo : XtraReport, IBeWoReport<ServiceInvoiceRO>
    {
		public ServiceInvoiceReportBeWo()
        {
            this.InitializeComponent();
        }

        public void SetReportDataSource(ServiceInvoiceRO pRO)
        {
			pRO.Notice = "";

			lblSachbearbeiter.Text = pRO.CustomerReferenceNumber;
			if (pRO.InvoiceDateTime.HasValue)
				pRO.Notice = String.Format("{0:dd.MM.yyyy}", pRO.InvoiceDateTime.Value.AddDays(30));

	        if (pRO.ServiceInvoicePeriods != null)
	        {
		        foreach (var sip in pRO.ServiceInvoicePeriods)
		        {
			        if (sip.ServiceInvoiceItems != null)
			        {
				        foreach (var ii in sip.ServiceInvoiceItems)
				        {
					        ii.ServiceDescription = String.Format("{0}\n{1}", ii.ServiceDescription, ii.Notice);

					        ii.CustomerFirstname = String.Format("{0:c}", ii.AmountPerUnit);
					        if (!String.IsNullOrEmpty(ii.RateFactor))
					        {
								
								var rfStr = ii.RateFactor.Replace("%", "").Replace(",00", "").Trim();
								int rfInt = 0;
								Int32.TryParse(rfStr, out rfInt);

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

								if (rateFactor > 1)
								{
									ii.CustomerFirstname = String.Format("{0} x {1:0.##}", ii.CustomerFirstname, rateFactor);
								}
								else
								{
									ii.CustomerFirstname = String.Format("{0}", ii.CustomerFirstname);
								}
					        }
						        
				        }
			        }
		        }

		        if (pRO.ServiceInvoicePeriods.Count > 0)
		        {
			        var c2s = pRO.ServiceInvoicePeriods[0].CostBearer2SupportConcept;
			        if (c2s.CostBearerContactPerson != null)
			        {
				        StringBuilder sb = new StringBuilder();
				        if (c2s.CostBearerContactPerson.Sex.HasValue)
				        {
					        sb.AppendLine(c2s.CostBearerContactPerson.Sex.Value == Sex.Male ? "Herr" : "Frau");
				        }
				        sb.AppendLine(c2s.CostBearerContactPerson.FirstNameLastName);
				        sb.Append(pRO.CustomerReferenceNumber);

				        lblSachbearbeiter.Text = sb.ToString();
			        }
		        }
	        }
	        
			
			
            this.bindingSource1.DataSource = pRO;
        }
    }
}