using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.Invoicing;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.Services;

namespace BeWoRueckenwind.Invoicing
{

	public class BeWoRueckenwindDefaultInvoiceCreation : InvoiceCreation
    {
		public override void SetInvoiceId(ServiceInvoice invoice)
		{
			invoice.InvoiceBase.InvoiceId = "Default";
		}

		public override InvoiceItem CreateInvoiceItem(ServiceRecordDC iServiceRecord, SupportConceptApprovalPeriod scap, Calculations calc)
		{
			var item = base.CreateInvoiceItem(iServiceRecord, scap, calc);

			return item;
		}

		public override IList<InvoiceItem> CreateSummaryInvoiceItems(IList<InvoiceItem> itemList, DateTimeSpan invoicePeriod, SupportConceptApprovalPeriod scap,
			Calculations calc)
		{
			var items = base.CreateSummaryInvoiceItems(itemList, invoicePeriod, scap, calc);

			foreach (var ii in items)
			{
				if (ii.UnitCount.HasValue && ii.RateFactor.HasValue)
				{
					ii.UnitCount = Math.Round(ii.UnitCount.Value* ((100 + ii.RateFactor.Value) /100), 2, MidpointRounding.AwayFromZero);

					if (ii.AmountPerUnit.HasValue)
					{
						ii.GrossAmountTotal =
							ii.AmountTotal = Math.Round(ii.UnitCount.Value*ii.AmountPerUnit.Value, 2, MidpointRounding.AwayFromZero);
					}
				}
			}

			return items;
		}
    }
}
