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

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

        public void SetReportDataSource(ServiceInvoiceRO pRO)
        {
			StringBuilder sb = new StringBuilder();

			if (!String.IsNullOrEmpty(pRO.RecipientOrganisation))
	        {
		        sb.AppendLine(pRO.RecipientOrganisation);
	        }
			if (!String.IsNullOrEmpty(pRO.RecipientDivision))
	        {
		        sb.AppendLine(pRO.RecipientDivision);
	        }
			if (!String.IsNullOrEmpty(pRO.RecipientStreet))
	        {
		        sb.AppendLine(pRO.RecipientStreet);
	        }
			if (!String.IsNullOrEmpty(pRO.RecipientPostCodeAndTown))
	        {
		        sb.AppendLine(pRO.RecipientPostCodeAndTown);
	        }
	        lblAdresse.Text = sb.ToString();

			pRO.Notice = "";

			lblAZ.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);
								}
					        }
						        
				        }
			        }
		        }

	        }
	        
			SetCustomerInfos(pRO);
			
            this.bindingSource1.DataSource = pRO;
        }

		private void SetCustomerInfos(ServiceInvoiceRO pRO)
		{
			String mnr = String.Empty;
			Customer c = null;
			if (pRO.CustomerOid.HasValue)
			{
				c = DAOFactory.GenericDAO.LoadByID<Customer>(pRO.CustomerOid.Value);
			}
			if (c == null)
			{
				if (pRO.ServiceInvoicePeriods.Count > 0)
				{
					var c2s = pRO.ServiceInvoicePeriods[0].CostBearer2SupportConcept;
					c = c2s.SupportConcept.Customer;
				}
			}

			if (c != null)
			{
				foreach (var vv in c.VarFieldValueList)
				{
					if (vv.VarFieldDefOid.Value == 2)
					{
						var varFieldValue = Utils.XMLDeserializeFromString(vv.SerializedValue, Type.GetType(vv.TypeName));
						if (varFieldValue != null)
						{
							mnr = varFieldValue.ToString();

						}
					}
				}
			}


			txtMittelbindungsnr.Text = mnr;

		}
    }
}