using System;
using System.Collections.Generic;
using System.Linq;

using BeWo.Data.Access;
using BeWo.Data.Entities;

using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.Extensions;

namespace BeWo.Service.DCEntityMapper
{
    public class SettlementDC_SettlementInvoice : AbstractIDCEntityMapper<SettlementInvoice, Settlement2DC>
    {
        public Settlement2DC CreateDCFromXMLData(SettlementInvoice pEntity)
        {
            SettlementDC dc = null;
            if (pEntity.InvoiceBase != null && !String.IsNullOrEmpty(pEntity.InvoiceBase.XMLData))
            {
                dc = Utils.XMLDeserializeFromString<SettlementDC>(pEntity.InvoiceBase.XMLData);
            }

            if (dc == null)
            {
                dc = new SettlementDC();
            }
            Settlement2DC dc2 = new Settlement2DC();
            MapDC2DC2(dc, dc2);
            return dc2;
        }

        public override Settlement2DC MapToNewDC(SettlementInvoice pEntity)
        {
            Settlement2DC dc = this.CreateDCFromXMLData(pEntity);
            return this.MergeWithDC(pEntity, dc);
        }

        public override List<Settlement2DC> MapToNewDCs(IEnumerable<SettlementInvoice> pEntitys)
        {
            List<Settlement2DC> lResult = new List<Settlement2DC>();
            foreach (SettlementInvoice iEntity in pEntitys)
            {
                Settlement2DC dc = this.CreateDCFromXMLData(iEntity);
                lResult.Add(this.MergeWithDC(iEntity, dc));
            }

            return lResult;

            
        }

        public override Settlement2DC MergeWithDC(SettlementInvoice pEntity, Settlement2DC pDataContract)
        {
            pDataContract.SettlementIvoiceOid = pEntity.Oid;
            pDataContract.SettlementInvoiceVersion = pEntity.Version;

            pDataContract.Claim = pEntity.Claim;
            pDataContract.AmountAdvancedPayments = pEntity.AmountAdvancedPayments;
            pDataContract.RateFactor = pEntity.AmountRateFactor;
            pDataContract.AmountBillableTotal = pEntity.AmountBillableTotal;
            pDataContract.RecordedBillableFLSNotBillableCustomerAbsent = pEntity.RecordedBillableFLSNotBillableCustomerAbsent ?? 0;
            pDataContract.RecordedBillableFLSNotBillableOutOfApprovedSpan = pEntity.RecordedBillableFLSNotBillableOutOfApprovedSpan ?? 0;
            pDataContract.RecordedBillableFLSNetto = pEntity.RecordedBillableFLSNetto ?? 0;
            pDataContract.ApprovedFLS = pEntity.ApprovedFLS;
            pDataContract.ApprovalNoticeDate = pEntity.ApprovalNoticeDate;
			pDataContract.ErrorNotice = pEntity.ErrorNotice;

            if (pEntity.InvoiceBase != null)
            {
                if (!String.IsNullOrEmpty(pEntity.InvoiceBase.XMLData))
                {
                }

                pDataContract.DunningLetterCount = pEntity.InvoiceBase.DunningLetterCount;
                pDataContract.InvoiceBaseOid = pEntity.InvoiceBase.Oid;
                pDataContract.InvoiceBaseVersion = pEntity.InvoiceBase.Version;
                pDataContract.SupportConceptOid = pEntity.InvoiceBase.SupportConceptOid;
                pDataContract.IsPaid = pEntity.InvoiceBase.IsPaid;

                if (pEntity.InvoiceBase.CostBearer2SupportConcept != null)
                    pDataContract.CostBearer2SupportConceptOid = pEntity.InvoiceBase.CostBearer2SupportConcept.Oid;

                // pDataContract.SenderOrganisation = pEntity.InvoiceBase.SenderOrganisation;
                pDataContract.SenderContactPersonLastName = pEntity.InvoiceBase.SenderLastName;
                pDataContract.SenderContactPersonFirstName = pEntity.InvoiceBase.SenderFirstName;
                pDataContract.SenderContactPersonDivision = pEntity.InvoiceBase.SenderDivision;

                pDataContract.RecipientContactPerson = pEntity.InvoiceBase.RecipientPersonFirstName;

                // pDataContract.RecipientPersonFirstName = pEntity.InvoiceBase.RecipientPersonFirstName;
                pDataContract.RecipientOrganisation = pEntity.InvoiceBase.RecipientOrganisation;
                pDataContract.RecipientDivision = pEntity.InvoiceBase.RecipientDivision;
                pDataContract.InvoiceNumber = pEntity.InvoiceBase.InvoiceNumber;
                pDataContract.InvoiceDate = pEntity.InvoiceBase.InvoiceDate;
                pDataContract.CreatorLastName = pEntity.InvoiceBase.CreatorLastName;
                pDataContract.CreatorFirstName = pEntity.InvoiceBase.CreatorFirstName;
                pDataContract.AccountingPeriodEnd = pEntity.InvoiceBase.AccountingPeriodEnd;
                pDataContract.AccountingPeriodStart = pEntity.InvoiceBase.AccountingPeriodStart;
                pDataContract.CustomerReferenceNumber = pEntity.InvoiceBase.CustomerReferenceNumber;
                pDataContract.Notice = pEntity.InvoiceBase.Notice;

                pDataContract.AmountTotalAdvancedPaidWithNewHourlyRate = pEntity.AmountTotalAdvancedPaidWithNewHourlyRate;
                pDataContract.AmountTotalAdvancedPaidWithNewHourlyRate = pEntity.AmountTotalAdvancedPaidWithNewHourlyRate;
                pDataContract.HourlyRateOld = pEntity.HourlyRateOld;
                pDataContract.AmountTotalAdvancedPaidWithOldHourlyRate = pEntity.AmountTotalAdvancedPaidWithOldHourlyRate;
                pDataContract.HourlyRateNew = pEntity.HourlyRateNew;
                pDataContract.HourlyRateNewStartDate = pEntity.HourlyRateNewStartDate;
                pDataContract.HourlyRateOldEndDate = pEntity.HourlyRateOldEndDate;
                pDataContract.ApprovedFLSWithOldHourlyRate = pEntity.ApprovedFLSWithOldHourlyRate ?? 0;
                pDataContract.ApprovedFLSWithActualHourlyRate = pEntity.ApprovedFLSWithActualHourlyRate ?? 0;
                pDataContract.AmountWithRateFactor = pEntity.AmountWithRateFactor ?? 0;
                pDataContract.RecordedBillableFLSBrutto = pEntity.RecordedBillableFLSBrutto;

                pDataContract.ServiceUnit = pEntity.InvoiceBase.CostBearer2SupportConcept.CostBearer.CostRatePeriods.MapToNewDCs()
                    .GetCostRatePeriodForDate(CostRatePeriodType.MinutesPerServiceUnit, pEntity.InvoiceBase.InvoiceDate.Value);

                if (pDataContract.ServiceUnit == null)
                {
                    pDataContract.ServiceUnit = new CostRatePeriodDC();
                    pDataContract.ServiceUnit.CostRateType = CostRatePeriodType.MinutesPerServiceUnit;
                    pDataContract.ServiceUnit.UnitName = "FLS";
                    pDataContract.ServiceUnit.CostRateValue = 60;

                }

                if (pEntity.InvoiceBase.RecipientAddress != null)
                {
                    pDataContract.RecipientAddressOid = pEntity.InvoiceBase.RecipientAddress.Oid;
                    pDataContract.RecipientStreet = pEntity.InvoiceBase.RecipientAddress.Street;
                    pDataContract.RecipientTown = pEntity.InvoiceBase.RecipientAddress.Town;
                    pDataContract.RecipientPostCode = pEntity.InvoiceBase.RecipientAddress.PostalCode;
                    pDataContract.RecipientAddressVersion = pEntity.InvoiceBase.RecipientAddress.Version;
                }

                // In InvoiceBase.SenderContacts
                //pDataContract.RecipientPostBox = pEntity.InvoiceBase.RecipientPostBox;

                foreach (var iContact in pEntity.InvoiceBase.SenderContacts)
                {
                    if (iContact.Type == ContactType.business_Fax)
                    {
                        pDataContract.SenderContactPersonFax = iContact.Value;
                    }

                    if (iContact.Type == ContactType.business_Phone)
                    {
                        pDataContract.SenderContactPersonPhone = iContact.Value;
                    }

                    if (iContact.Type == ContactType.business_Mail)
                    {
                        pDataContract.SenderContactPersonMail = iContact.Value;
                    }
                }
            }

            if (pEntity.InvoiceBase != null)
            {
                pDataContract.InvoiceItems = MapperFactory.InvoiceItemDC_InvoiceItem.MapToNewDCs(pEntity.InvoiceBase.InvoiceItems);
                pDataContract.InvoiceItems.Sort((x, y) => x.ItemPeriodStart.Value.CompareTo(y.ItemPeriodStart.Value));
            }
            
            return pDataContract;
        }

        public override SettlementInvoice MergeWithEntity(Settlement2DC pDataContract, SettlementInvoice pEntity)
        {
            this.ConcurrencyCheck(pDataContract.SettlementInvoiceVersion, pEntity);

            pEntity.Oid                                             = pDataContract.SettlementIvoiceOid;
            pEntity.Claim                                           = pDataContract.Claim;
            pEntity.AmountAdvancedPayments                          = pDataContract.AmountAdvancedPayments;
            pEntity.AmountRateFactor                                = pDataContract.RateFactor;
            pEntity.AmountBillableTotal                             = pDataContract.AmountBillableTotal;
            pEntity.RecordedBillableFLSNotBillableCustomerAbsent    = pDataContract.RecordedBillableFLSNotBillableCustomerAbsent;
            pEntity.RecordedBillableFLSNotBillableOutOfApprovedSpan = pDataContract.RecordedBillableFLSNotBillableOutOfApprovedSpan;
            pEntity.RecordedBillableFLSNetto                        = pDataContract.RecordedBillableFLSNetto;
            pEntity.ApprovedFLS                                     = pDataContract.ApprovedFLS;
            pEntity.ApprovalNoticeDate                              = pDataContract.ApprovalNoticeDate;

            pEntity.AmountTotalAdvancedPaidWithNewHourlyRate = pDataContract.AmountTotalAdvancedPaidWithNewHourlyRate;
            pEntity.AmountTotalAdvancedPaidWithNewHourlyRate = pDataContract.AmountTotalAdvancedPaidWithNewHourlyRate;
            pEntity.HourlyRateOld                            = pDataContract.HourlyRateOld;
            pEntity.AmountTotalAdvancedPaidWithOldHourlyRate = pDataContract.AmountTotalAdvancedPaidWithOldHourlyRate;
            pEntity.HourlyRateNew                            = pDataContract.HourlyRateNew;
            pEntity.HourlyRateNewStartDate                   = pDataContract.HourlyRateNewStartDate;
            pEntity.HourlyRateOldEndDate                     = pDataContract.HourlyRateOldEndDate;
            pEntity.ApprovedFLSWithOldHourlyRate             = pDataContract.ApprovedFLSWithOldHourlyRate;
            pEntity.ApprovedFLSWithActualHourlyRate          = pDataContract.ApprovedFLSWithActualHourlyRate;
            pEntity.AmountWithRateFactor                     = pDataContract.AmountWithRateFactor;
            pEntity.RecordedBillableFLSBrutto                = pDataContract.RecordedBillableFLSBrutto;
			pEntity.ErrorNotice = pDataContract.ErrorNotice;

            if (pEntity.InvoiceBase == null)
            {
                pEntity.InvoiceBase = new InvoiceBase {Type = InvoiceType.Settlement};
            }

            ConcurrencyCheck(pDataContract.InvoiceBaseVersion, pEntity.InvoiceBase);

            pEntity.InvoiceBase.Oid                = pDataContract.InvoiceBaseOid;
            pEntity.InvoiceBase.DunningLetterCount = pDataContract.DunningLetterCount;

            if (pDataContract.CostBearer2SupportConceptOid.HasValue)
            {
                pEntity.InvoiceBase.CostBearer2SupportConcept = DAOFactory.GenericDAO.LoadByID<CostBearer2SupportConcept>(pDataContract.CostBearer2SupportConceptOid.Value);
            }

            pEntity.InvoiceBase.SenderLastName           = pDataContract.SenderContactPersonLastName;
            pEntity.InvoiceBase.SenderFirstName          = pDataContract.SenderContactPersonFirstName;
            pEntity.InvoiceBase.SenderDivision           = pDataContract.SenderContactPersonDivision;
            pEntity.InvoiceBase.IsPaid                   = pDataContract.IsPaid;
            pEntity.InvoiceBase.RecipientPersonFirstName = pDataContract.RecipientContactPerson;

            // pDataContract.RecipientrePersonLastName  = pEntity.InvoiceBase.RecipientPersonLastName;
            // pDataContract.RecipientPersonFirstName   = pEntity.InvoiceBase.RecipientPersonFirstName;
            pEntity.InvoiceBase.SupportConceptOid       = pDataContract.SupportConceptOid;
            pEntity.InvoiceBase.RecipientOrganisation   = pDataContract.RecipientOrganisation;
            //pEntity.InvoiceBase.RecipientPostBox        = pDataContract.RecipientPostBox;
            if (!String.IsNullOrEmpty(pDataContract.RecipientPostBox))
            {
                var pobox = pEntity.InvoiceBase.RecipientContacts.FirstOrDefault(f => f.Type.Equals(ContactType.business_POBox));

                if (pobox != null)
                {
                    pobox.Value = pDataContract.RecipientPostBox;
                }
                else
                {
                    pEntity.InvoiceBase.RecipientContacts.Add(new Contact {Type = ContactType.business_POBox, Value = pDataContract.RecipientPostBox});
                }
            }

            pEntity.InvoiceBase.RecipientDivision       = pDataContract.RecipientDivision;
            pEntity.InvoiceBase.InvoiceNumber           = pDataContract.InvoiceNumber;
            pEntity.InvoiceBase.InvoiceDate             = pDataContract.InvoiceDate;
            pEntity.InvoiceBase.CreatorLastName         = pDataContract.CreatorLastName;
            pEntity.InvoiceBase.CreatorFirstName        = pDataContract.CreatorFirstName;
            pEntity.InvoiceBase.AccountingPeriodEnd     = pDataContract.AccountingPeriodEnd;
            pEntity.InvoiceBase.AccountingPeriodStart   = pDataContract.AccountingPeriodStart;
            pEntity.InvoiceBase.CustomerReferenceNumber = pDataContract.CustomerReferenceNumber;
            pEntity.InvoiceBase.Notice                  = pDataContract.Notice;

            if (pDataContract.ContainsRecipientAddressData())
            {
                if (pEntity.InvoiceBase.RecipientAddress == null)
                {
                    pEntity.InvoiceBase.RecipientAddress = new Address();
                }

                ConcurrencyCheck(pDataContract.RecipientAddressVersion, pEntity.InvoiceBase.RecipientAddress);

                pEntity.InvoiceBase.RecipientAddress.Oid        = pDataContract.RecipientAddressOid;
                pEntity.InvoiceBase.RecipientAddress.Street     = pDataContract.RecipientStreet;
                pEntity.InvoiceBase.RecipientAddress.Town       = pDataContract.RecipientTown;
                pEntity.InvoiceBase.RecipientAddress.PostalCode = pDataContract.RecipientPostCode;
            }
            else if (pEntity.InvoiceBase.RecipientAddress != null)
            {
                // all-delete-orphan wird von NHibernate (noch) nicht unterstützt für many-to-one
                var lInvoiceAddress = pEntity.InvoiceBase.RecipientAddress;
                pEntity.InvoiceBase.RecipientAddress = null;
                DAOFactory.GenericDAO.Delete(lInvoiceAddress);
            }

            CommitSenderContact(pDataContract.SenderContactPersonFax, ContactType.business_Fax, pDataContract, pEntity);
            CommitSenderContact(pDataContract.SenderContactPersonPhone, ContactType.business_Phone, pDataContract, pEntity);
            CommitSenderContact(pDataContract.SenderContactPersonMail, ContactType.business_Mail, pDataContract, pEntity);

            var dcToSerialize = new SettlementDC();
            MapDC22DC(pDataContract, dcToSerialize);
            pEntity.InvoiceBase.XMLData = Utils.XMLSerializeToString(dcToSerialize);

            MapperFactory.InvoiceItemDC_InvoiceItem.MergeWithEntitys(pDataContract.InvoiceItems, pEntity.InvoiceBase.InvoiceItems);

            return pEntity;
        }

        private static void MapDC22DC(Settlement2DC source, SettlementDC target)
        {
            target.AccountingPeriodEnd                             = source.AccountingPeriodEnd;
            target.AccountingPeriodStart                           = source.AccountingPeriodStart;
            target.AmountAdvancedPayments                          = source.AmountAdvancedPayments;
            target.AmountRecordedBillableFLS                       = source.AmountBillableTotal;
            target.AmountTotalAdvancedPaidWithHourlyRate3          = source.AmountTotalAdvancedPaidWithHourlyRate3;
            target.AmountTotalAdvancedPaidWithNewHourlyRate        = source.AmountTotalAdvancedPaidWithNewHourlyRate;
            target.AmountTotalAdvancedPaidWithOldHourlyRate        = source.AmountTotalAdvancedPaidWithOldHourlyRate;
            target.AmountTotalWithHourlyRate3                      = source.AmountTotalWithHourlyRate3;
            target.AmountWithRateFactor                            = source.AmountWithRateFactor;
            target.ApprovalNoticeDate                              = source.ApprovalNoticeDate;
            target.ApprovedFLS                                     = source.ApprovedFLS;
            target.ApprovedFLSWeekly                               = source.ApprovedFLSWeekly;
            target.ApprovedFLSWithActualHourlyRate                 = source.ApprovedFLSWithActualHourlyRate;
            target.ApprovedFLSWithHourlyRate3                      = source.ApprovedFLSWithHourlyRate3;
            target.ApprovedFLSWithOldHourlyRate                    = source.ApprovedFLSWithOldHourlyRate;
            target.Claim                                           = source.Claim;
            target.CostBearer2SupportConceptOid                    = source.CostBearer2SupportConceptOid;
            target.CreatorFirstName                                = source.CreatorFirstName;
            target.CreatorLastName                                 = source.CreatorLastName;
            target.CustomerReferenceNumber                         = source.CustomerReferenceNumber;
            target.DifferentHourlyRateCount                        = source.DifferentHourlyRateCount;
            target.DunningLetterCount                              = source.DunningLetterCount;
            target.EquityContribution                              = source.EquityContribution;
            target.HourlyRate3                                     = source.HourlyRate3;
            target.HourlyRateEndDate3                              = source.HourlyRateEndDate3;
            target.HourlyRateNew                                   = source.HourlyRateNew;
            target.HourlyRateNewStartDate                          = source.HourlyRateNewStartDate;
            target.HourlyRateOld                                   = source.HourlyRateOld;
            target.HourlyRateOldEndDate                            = source.HourlyRateOldEndDate;
            target.HourlyRateOldStartDate                          = source.HourlyRateOldStartDate;
            target.InvoiceBaseOid                                  = source.InvoiceBaseOid;
            target.InvoiceBaseVersion                              = source.InvoiceBaseVersion;
            target.InvoiceDate                                     = source.InvoiceDate;
            target.InvoiceNumber                                   = source.InvoiceNumber;
            target.IsPaid                                          = source.IsPaid;
            target.MaxAmountAllowed                                = source.MaxAmountAllowed;
            target.Notice                                          = source.Notice;
            target.RateFactor                                      = source.RateFactor;
            target.RecipientAddressOid                             = source.RecipientAddressOid;
            target.RecipientAddressVersion                         = source.RecipientAddressVersion;
            target.RecipientContactPerson                          = source.RecipientContactPerson;
            target.RecipientDivision                               = source.RecipientDivision;
            target.RecipientOrganisation                           = source.RecipientOrganisation;
            target.RecipientPostCode                               = source.RecipientPostCode;
            target.RecipientPostBox                                = source.RecipientPostBox;
            target.RecipientStreet                                 = source.RecipientStreet;
            target.RecipientTown                                   = source.RecipientTown;
            target.RecordedBillableFLSBrutto                       = source.RecordedBillableFLSBrutto;
            target.RecordedBillableFLSNetto                        = source.RecordedBillableFLSNetto;
            target.RecordedBillableFLSNotBillableCustomerAbsent    = source.RecordedBillableFLSNotBillableCustomerAbsent;
            target.RecordedBillableFLSNotBillableOutOfApprovedSpan = source.RecordedBillableFLSNotBillableOutOfApprovedSpan;
            target.SenderContactPersonDivision                     = source.SenderContactPersonDivision;
            target.SenderContactPersonFax                          = source.SenderContactPersonFax;
            target.SenderContactPersonFirstName                    = source.SenderContactPersonFirstName;
            target.SenderContactPersonLastName                     = source.SenderContactPersonLastName;
            target.SenderContactPersonMail                         = source.SenderContactPersonMail;
            target.SenderContactPersonPhone                        = source.SenderContactPersonPhone;
            target.ServiceUnit                                     = source.ServiceUnit;
            target.SettlementInvoiceVersion                        = source.SettlementInvoiceVersion;
            target.SettlementIvoiceOid                             = source.SettlementIvoiceOid;
            target.SupportConceptOid                               = source.SupportConceptOid;
            target.TotalAmount                                     = source.TotalAmount;
            target.WeeksWithHourlyRate3                            = source.WeeksWithHourlyRate3;
            target.WeeksWithNewHourlyRate                          = source.WeeksWithNewHourlyRate;
            target.WeeksWithOldHourlyRate                          = source.WeeksWithOldHourlyRate;
            
        }

        private static void MapDC2DC2(SettlementDC source, Settlement2DC target)
        {
            target.AccountingPeriodEnd                             = source.AccountingPeriodEnd;
            target.AccountingPeriodStart                           = source.AccountingPeriodStart;
            target.AmountAdvancedPayments                          = source.AmountAdvancedPayments;
            target.AmountBillableTotal                             = source.AmountRecordedBillableFLS;
            target.AmountTotalAdvancedPaidWithHourlyRate3          = source.AmountTotalAdvancedPaidWithHourlyRate3;
            target.AmountTotalAdvancedPaidWithNewHourlyRate        = source.AmountTotalAdvancedPaidWithNewHourlyRate;
            target.AmountTotalAdvancedPaidWithOldHourlyRate        = source.AmountTotalAdvancedPaidWithOldHourlyRate;
            target.AmountTotalWithHourlyRate3                      = source.AmountTotalWithHourlyRate3;
            target.AmountWithRateFactor                            = source.AmountWithRateFactor;
            target.ApprovalNoticeDate                              = source.ApprovalNoticeDate;
            target.ApprovedFLS                                     = source.ApprovedFLS;
            target.ApprovedFLSWeekly                               = source.ApprovedFLSWeekly;
            target.ApprovedFLSWithActualHourlyRate                 = source.ApprovedFLSWithActualHourlyRate;
            target.ApprovedFLSWithHourlyRate3                      = source.ApprovedFLSWithHourlyRate3;
            target.ApprovedFLSWithOldHourlyRate                    = source.ApprovedFLSWithOldHourlyRate;
            target.Claim                                           = source.Claim;
            target.CostBearer2SupportConceptOid                    = source.CostBearer2SupportConceptOid;
            target.CreatorFirstName                                = source.CreatorFirstName;
            target.CreatorLastName                                 = source.CreatorLastName;
            target.CustomerReferenceNumber                         = source.CustomerReferenceNumber;
            target.DifferentHourlyRateCount                        = source.DifferentHourlyRateCount;
            target.DunningLetterCount                              = source.DunningLetterCount;
            target.EquityContribution                              = source.EquityContribution;
            target.HourlyRate3                                     = source.HourlyRate3;
            target.HourlyRateEndDate3                              = source.HourlyRateEndDate3;
            target.HourlyRateNew                                   = source.HourlyRateNew;
            target.HourlyRateNewStartDate                          = source.HourlyRateNewStartDate;
            target.HourlyRateOld                                   = source.HourlyRateOld;
            target.HourlyRateOldEndDate                            = source.HourlyRateOldEndDate;
            target.HourlyRateOldStartDate                          = source.HourlyRateOldStartDate;
            target.InvoiceBaseOid                                  = source.InvoiceBaseOid;
            target.InvoiceBaseVersion                              = source.InvoiceBaseVersion;
            target.InvoiceDate                                     = source.InvoiceDate;
            target.InvoiceNumber                                   = source.InvoiceNumber;
            target.IsPaid                                          = source.IsPaid;
            target.MaxAmountAllowed                                = source.MaxAmountAllowed;
            target.Notice                                          = source.Notice;
            target.RateFactor                                      = source.RateFactor;
            target.RecipientAddressOid                             = source.RecipientAddressOid;
            target.RecipientAddressVersion                         = source.RecipientAddressVersion;
            target.RecipientContactPerson                          = source.RecipientContactPerson;
            target.RecipientDivision                               = source.RecipientDivision;
            target.RecipientOrganisation                           = source.RecipientOrganisation;
            target.RecipientPostBox                                = source.RecipientPostBox;
            target.RecipientPostCode                               = source.RecipientPostCode;
            target.RecipientStreet                                 = source.RecipientStreet;
            target.RecipientTown                                   = source.RecipientTown;
            target.RecordedBillableFLSBrutto                       = source.RecordedBillableFLSBrutto;
            target.RecordedBillableFLSNetto                        = source.RecordedBillableFLSNetto;
            target.RecordedBillableFLSNotBillableCustomerAbsent    = source.RecordedBillableFLSNotBillableCustomerAbsent;
            target.RecordedBillableFLSNotBillableOutOfApprovedSpan = source.RecordedBillableFLSNotBillableOutOfApprovedSpan;
            target.SenderContactPersonDivision                     = source.SenderContactPersonDivision;
            target.SenderContactPersonFax                          = source.SenderContactPersonFax;
            target.SenderContactPersonFirstName                    = source.SenderContactPersonFirstName;
            target.SenderContactPersonLastName                     = source.SenderContactPersonLastName;
            target.SenderContactPersonMail                         = source.SenderContactPersonMail;
            target.SenderContactPersonPhone                        = source.SenderContactPersonPhone;
            target.ServiceUnit                                     = source.ServiceUnit;
            target.SettlementInvoiceVersion                        = source.SettlementInvoiceVersion;
            target.SettlementIvoiceOid                             = source.SettlementIvoiceOid;
            target.SupportConceptOid                               = source.SupportConceptOid;
            target.TotalAmount                                     = source.TotalAmount;
            target.WeeksWithHourlyRate3                            = source.WeeksWithHourlyRate3;
            target.WeeksWithNewHourlyRate                          = source.WeeksWithNewHourlyRate;
            target.WeeksWithOldHourlyRate                          = source.WeeksWithOldHourlyRate;
        }


        protected override bool AreDCAndEntityEqual(Settlement2DC pDC, SettlementInvoice pEntity)
        {
            if (pDC.SettlementIvoiceOid == null)
            {
                return false;
            }

            return pDC.SettlementIvoiceOid == pEntity.Oid;
        }

        private static void CommitSenderContact(string pValue, ContactType pContactType, Settlement2DC pDC, SettlementInvoice pEntity)
        {
            if (pEntity.InvoiceBase.SenderContacts == null)
            {
                pEntity.InvoiceBase.SenderContacts = new List<Contact>();
            }

            var lContact = pEntity.InvoiceBase.SenderContacts.SingleOrDefault(con => con.Type == pContactType);

            if (string.IsNullOrEmpty(pValue) && lContact != null)
            {
                pEntity.InvoiceBase.SenderContacts.Remove(lContact);
            }
            else if (!string.IsNullOrEmpty(pValue))
            {
                if (lContact == null)
                {
                    lContact = new Contact { Type = pContactType };
                    pEntity.InvoiceBase.SenderContacts.Add(lContact);
                }

                lContact.Value = pValue;
            }
        }
    }
}