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

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

        public void SetReportDataSource(ServiceInvoiceRO pRO)
        {
	        if (String.IsNullOrEmpty(pRO.RecipientStreet))
	        {
		        pRO.RecipientStreet = pRO.RecipientPoBox;
	        }
	        if (pRO.ServiceInvoicePeriods != null)
	        {
		        foreach (var sip in pRO.ServiceInvoicePeriods)
		        {
					
			        if (sip.CostBearer2SupportConcept != null)
			        {
				        if (String.IsNullOrEmpty(pRO.OrganisationDebitorNumber))
				        {
					        pRO.OrganisationDebitorNumber = sip.CostBearer2SupportConcept.CostBearer.Organisation.DebitorNumber;
				        }
				        sip.NotBillableString = string.Format("AZ: {0}", sip.CostBearer2SupportConcept.CustomerReferenceNumber);
			        }
			        if (sip.ServiceInvoiceItems != null && sip.ServiceInvoiceItems.Count > 0)
			        {
				        sip.ApprovalPeriod = String.Format("{0:dd.MM.} bis {1:dd.MM.}",
					        sip.ServiceInvoiceItems[0].AccountingPeriodStart, sip.ServiceInvoiceItems[0].AccountingPeriodEnd);

				        sip.ApprovedAmount = String.Format("{0:c}", sip.ServiceInvoiceItems[0].AmountPerUnit);
			        }
			        sip.ApprovedBE = CreateSchluessel(sip);
			        sip.ApprovedHours = 0;
			        if (Leistungsart(sip) == "S")
			        {
				        sip.ApprovedHours = 1;
			        }
		        }
	        }
            this.bindingSource1.DataSource = pRO;
        }

		private string CreateSchluessel(ServiceInvoicePeriodRO sip)
		{
			string schl = "";

			if (sip.SupportConceptApprovalPeriod != null)
			{
				if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.HasValue)
				{
					//K/G Beh.
					if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 358.75m)
					{
						schl = "01:12";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 430.50m)
					{
						schl = "01:10";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 478.33m)
					{
						schl = "01:09";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 538.13m)
					{
						schl = "01:08";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 615.00m)
					{
						schl = "01:07";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 717.50m)
					{
						schl = "01:06";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 861.00m)
					{
						schl = "01:05";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 1076.25m)
					{
						schl = "01:04";
					}
					//seel. Beh.
					if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 458.70m)
					{
						schl = "01:12";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 538.20m)
					{
						schl = "01:10";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 591.20m)
					{
						schl = "01:09";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 657.45m)
					{
						schl = "01:08";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 742.63m)
					{
						schl = "01:07";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 856.20m)
					{
						schl = "01:06";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 1015.20m)
					{
						schl = "01:05";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 1253.70m)
					{
						schl = "01:04";
					}
				}
			}
			return schl;
		}

		private string Leistungsart(ServiceInvoicePeriodRO sip)
		{
			string schl = "GK";

			if (sip.SupportConceptApprovalPeriod != null)
			{
				if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.HasValue)
				{
					//seel. Beh.
					if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 458.70m)
					{
						schl = "S";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 538.20m)
					{
						schl = "S";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 591.20m)
					{
						schl = "S";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 657.45m)
					{
						schl = "S";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 742.63m)
					{
						schl = "S";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 856.20m)
					{
						schl = "S";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 1015.20m)
					{
						schl = "S";
					}
					else if (sip.SupportConceptApprovalPeriod.ApprovedFixedAmount.Value == 1253.70m)
					{
						schl = "S";
					}
				}
			}
			return schl;
		}
    }
}