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

using BeWo.Validation;
using BeWo.ViewModel.ListViewModel;

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

namespace BeWo.ViewModel
{
    public class InvoiceBaseVM : AbstractDCMapperVM<InvoiceBaseDC>
    {
        #region Constants and Fields

        public static string PropertyName_AccountingPeriodEnd = "AccountingPeriodEnd";

        public static string PropertyName_AccountingPeriodStart = "AccountingPeriodStart";

        public static string PropertyName_CreatorFirstName = "CreatorFirstName";

        public static string PropertyName_CreatorLastName = "CreatorLastName";

        public static string PropertyName_CustomerReferenceNumber = "CustomerReferenceNumber";

        public static string PropertyName_DueDate = "DueDate";

        public static string PropertyName_DunningLetterCount = "DunningLetterCount";

        public static string PropertyName_InvoiceDate = "InvoiceDate";

        public static string PropertyName_InvoiceItems = "InvoiceItems";

        public static string PropertyName_InvoiceNumber = "InvoiceNumber";

        public static string PropertyName_InvoiceTitle = "InvoiceTitle";

        public static string PropertyName_IsPaid = "IsPaid";

        public static string PropertyName_Notice = "Notice";

        public static string PropertyName_RecipientCostBearerOid = "RecipientCostBearerOid";

        public static string PropertyName_RecipientCustomerOid = "RecipientCustomerOid";

        public static string PropertyName_RecipientDivision = "RecipientDivision";

        public static string PropertyName_RecipientOrganisation = "RecipientOrganisation";

        public static string PropertyName_RecipientOrganisationOid = "RecipientOrganisationOid";

        public static string PropertyName_RecipientPostBox = "RecipientPostBox";

        public static string PropertyName_RecipientPersonFirstName = "RecipientPersonFirstName";

        public static string PropertyName_RecipientPersonLastName = "RecipientPersonLastName";

        public static string PropertyName_RecipientPersonOid = "RecipientPersonOid";

        public static string PropertyName_RecipientPostCode = "RecipientPostCode";

        public static string PropertyName_RecipientStreet = "RecipientStreet";

        public static string PropertyName_RecipientTown = "RecipientTown";

        public static string PropertyName_ReportLink = "ReportLink";

        public static string PropertyName_SenderDivision = "SenderDivision";

        public static string PropertyName_SenderFax = "SenderFax";

        public static string PropertyName_SenderFirstName = "SenderFirstName";

        public static string PropertyName_SenderLastName = "SenderLastName";

        public static string PropertyName_SenderOrganisation = "SenderOrganisation";

        public static string PropertyName_SenderPhone = "SenderPhone";

        public static string PropertyName_SupportConceptOid = "SupportConceptOid";

        public static string PropertyName_TotalAmount = "TotalAmount";

        public static string PropertyName_TypeString = "TypeString";

        private readonly Dictionary<InvoiceType, string> _TypeTranslation = new Dictionary<InvoiceType, string>
        {
            {
                InvoiceType.CustomerEquity, "Eigenanteil"
            },
            {
                InvoiceType.General, "Allgemein"
            },
            {
                InvoiceType.Settlement, "Spitzabrechnung"
            },
            {
                InvoiceType.Service, "Serviceleistung"
            }
        };

        private DateTime? _AccountingPeriodEnd;

        private DateTime? _AccountingPeriodStart;

        private string _CreatorFirstName = string.Empty;

        private string _CreatorLastName = string.Empty;

        private string _CustomerReferenceNumber = string.Empty;

        private DateTime? _DueDate;

        private int? _DunningLetterCount;

        private DateTime? _InvoiceDate;

        private InvoiceItemListVM _InvoiceItems;

        private string _InvoiceNumber = string.Empty;

        private string _InvoiceTitle = string.Empty;
        internal bool _IsPaid;

        private string _Notice = string.Empty;

        private long? _RecipientCostBearerOid;

        private long? _RecipientCustomerOid;

        private string _RecipientDivision = string.Empty;

        private string _RecipientOrganisation = string.Empty;

        private long? _RecipientOrganisationOid;

        private String _RecipientPostBox;

        private string _RecipientPersonFirstName = string.Empty;

        private string _RecipientPersonLastName = string.Empty;

        private long? _RecipientPersonOid;

        private string _RecipientPostCode = string.Empty;

        private string _RecipientStreet = string.Empty;

        private string _RecipientTown = string.Empty;

        private string _SenderDivision = string.Empty;

        private string _SenderFax = string.Empty;

        private string _SenderFirstName = string.Empty;

        private string _SenderLastName = string.Empty;

        private string _SenderOrganisation = string.Empty;

        private string _SenderPhone = string.Empty;

        private long? _SupportConceptOid;

        private decimal? _TotalAmount;

        #endregion

        #region Constructors and Destructors

        public InvoiceBaseVM(InvoiceBaseDC pDC)
            : base(pDC, pDC.InvoiceBaseOid == null)
        {
        }

        #endregion

        #region Properties

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public DateTime? AccountingPeriodEnd
        {
            get { return _AccountingPeriodEnd; }

            set
            {
                if (AreDifferent(_AccountingPeriodEnd, value))
                {
                    _AccountingPeriodEnd = value;
                    StoreDirtyInformation(AreDifferent(DataContract.AccountingPeriodEnd, value), PropertyName_AccountingPeriodEnd);
                    FirePropertyChanged(PropertyName_AccountingPeriodEnd);
                }
            }
        }

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public DateTime? AccountingPeriodStart
        {
            get { return _AccountingPeriodStart; }

            set
            {
                if (AreDifferent(_AccountingPeriodStart, value))
                {
                    _AccountingPeriodStart = value;
                    StoreDirtyInformation(AreDifferent(DataContract.AccountingPeriodStart, value), PropertyName_AccountingPeriodStart);
                    FirePropertyChanged(PropertyName_AccountingPeriodStart);
                }
            }
        }

        public string AccountingPeriodString
        {
            get
            {
                return _AccountingPeriodEnd.HasValue && _AccountingPeriodStart.HasValue
                    ? _AccountingPeriodStart.Value.ToShortDateString() + " - " + _AccountingPeriodEnd.Value.ToShortDateString()
                    : string.Empty;
            }

            set { }
        }

        public string CreatorFirstName
        {
            get { return _CreatorFirstName; }

            set
            {
                if (AreDifferent(_CreatorFirstName, value))
                {
                    _CreatorFirstName = value;
                    StoreDirtyInformation(AreDifferent(DataContract.CreatorFirstName, value), PropertyName_CreatorFirstName);
                    FirePropertyChanged(PropertyName_CreatorFirstName);
                }
            }
        }

        public string CreatorLastName
        {
            get { return _CreatorLastName; }

            set
            {
                if (AreDifferent(_CreatorLastName, value))
                {
                    _CreatorLastName = value;
                    StoreDirtyInformation(AreDifferent(DataContract.CreatorLastName, value), PropertyName_CreatorLastName);
                    FirePropertyChanged(PropertyName_CreatorLastName);
                }
            }
        }

        public string CustomerName
        {
            get
            {
                string result = string.Empty;
                if (!String.IsNullOrEmpty(DataContract.CustomerLastName))
                {
                    result = DataContract.CustomerLastName;
                }

                if (!String.IsNullOrEmpty(DataContract.CustomerFirstName))
                {
                    if (result.Length > 0)
                    {
                        result += ", ";
                    }

                    result += DataContract.CustomerFirstName;
                }

                return result;
            }

            set { }
        }

        public string CustomerReferenceNumber
        {
            get { return _CustomerReferenceNumber; }

            set
            {
                if (AreDifferent(_CustomerReferenceNumber, value))
                {
                    _CustomerReferenceNumber = value;
                    StoreDirtyInformation(AreDifferent(DataContract.CustomerReferenceNumber, value), PropertyName_CustomerReferenceNumber);
                    FirePropertyChanged(PropertyName_CustomerReferenceNumber);
                }
            }
        }

        public DateTime? DueDate
        {
            get { return _DueDate; }

            set
            {
                if (AreDifferent(_DueDate, value))
                {
                    _DueDate = value;
                    StoreDirtyInformation(AreDifferent(DataContract.DueDate, value), PropertyName_DueDate);
                    FirePropertyChanged(PropertyName_DueDate);
                }
            }
        }

        public int? DunningLetterCount
        {
            get { return _DunningLetterCount; }

            set
            {
                if (AreDifferent(_DunningLetterCount, value))
                {
                    _DunningLetterCount = value;
                    StoreDirtyInformation(AreDifferent(DataContract.DunningLetterCount, value), PropertyName_DunningLetterCount);
                    FirePropertyChanged(PropertyName_DunningLetterCount);
                }
            }
        }

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public DateTime? InvoiceDate
        {
            get { return _InvoiceDate; }

            set
            {
                if (AreDifferent(_InvoiceDate, value))
                {
                    _InvoiceDate = value;
                    StoreDirtyInformation(AreDifferent(DataContract.InvoiceDate, value), PropertyName_InvoiceDate);
                    FirePropertyChanged(PropertyName_InvoiceDate);
                }
            }
        }

        public InvoiceItemListVM InvoiceItems
        {
            get { return _InvoiceItems; }

            set
            {
                if (AreDifferent(_InvoiceItems, value))
                {
                    _InvoiceItems = value;
                    FirePropertyChanged(PropertyName_InvoiceItems);
                }
            }
        }

        public string InvoiceNumber
        {
            get { return _InvoiceNumber; }

            set
            {
                if (AreDifferent(_InvoiceNumber, value))
                {
                    _InvoiceNumber = value;
                    StoreDirtyInformation(AreDifferent(DataContract.InvoiceNumber, value), PropertyName_InvoiceNumber);
                    FirePropertyChanged(PropertyName_InvoiceNumber);
                }
            }
        }

        public string InvoiceTitle
        {
            get { return _InvoiceTitle; }

            set
            {
                if (AreDifferent(_InvoiceTitle, value))
                {
                    _InvoiceTitle = value;
                    StoreDirtyInformation(AreDifferent(DataContract.InvoiceTitle, value), PropertyName_InvoiceTitle);
                    FirePropertyChanged(PropertyName_InvoiceTitle);
                }
            }
        }

        public bool IsPaid
        {
            get { return _IsPaid; }

            set
            {
                if (AreDifferent(_IsPaid, value))
                {
                    _IsPaid = value;
                    StoreDirtyInformation(AreDifferent(DataContract.IsPaid, value), PropertyName_IsPaid);
                    FirePropertyChanged(PropertyName_IsPaid);
                }
            }
        }

        public string Notice
        {
            get { return _Notice; }

            set
            {
                if (AreDifferent(_Notice, value))
                {
                    _Notice = value;
                    StoreDirtyInformation(AreDifferent(DataContract.Notice, value), PropertyName_Notice);
                    FirePropertyChanged(PropertyName_Notice);
                }
            }
        }

        public long? RecipientCostBearerOid
        {
            get { return _RecipientCostBearerOid; }

            set
            {
                if (AreDifferent(_RecipientCostBearerOid, value))
                {
                    _RecipientCostBearerOid = value;
                    StoreDirtyInformation(AreDifferent(DataContract.RecipientCostBearerOid, value), PropertyName_RecipientCostBearerOid);
                    FirePropertyChanged(PropertyName_RecipientCostBearerOid);
                }
            }
        }

        public long? RecipientCustomerOid
        {
            get { return _RecipientCustomerOid; }

            set
            {
                if (AreDifferent(_RecipientCustomerOid, value))
                {
                    _RecipientCustomerOid = value;
                    StoreDirtyInformation(AreDifferent(DataContract.RecipientCustomerOid, value), PropertyName_RecipientCustomerOid);
                    FirePropertyChanged(PropertyName_RecipientCustomerOid);
                }
            }
        }

        public string RecipientDivision
        {
            get { return _RecipientDivision; }

            set
            {
                if (AreDifferent(_RecipientDivision, value))
                {
                    _RecipientDivision = value;
                    StoreDirtyInformation(AreDifferent(DataContract.RecipientDivision, value), PropertyName_RecipientDivision);
                    FirePropertyChanged(PropertyName_RecipientDivision);
                }
            }
        }

        public string RecipientOrganisation
        {
            get { return _RecipientOrganisation; }

            set
            {
                if (AreDifferent(_RecipientOrganisation, value))
                {
                    _RecipientOrganisation = value;
                    StoreDirtyInformation(AreDifferent(DataContract.RecipientOrganisation, value), PropertyName_RecipientOrganisation);
                    FirePropertyChanged(PropertyName_RecipientOrganisation);
                }
            }
        }

        public long? RecipientOrganisationOid
        {
            get { return _RecipientOrganisationOid; }

            set
            {
                if (AreDifferent(_RecipientOrganisationOid, value))
                {
                    _RecipientOrganisationOid = value;
                    StoreDirtyInformation(AreDifferent(DataContract.RecipientOrganisationOid, value), PropertyName_RecipientOrganisationOid);
                    FirePropertyChanged(PropertyName_RecipientOrganisationOid);
                }
            }
        }

        public String RecipientPostBox
        {
            get { return _RecipientPostBox; }

            set
            {
                if (AreDifferent(_RecipientPostBox, value))
                {
                    _RecipientPostBox = value;


                    StoreDirtyInformation(
                        DataContract.RecipientContactInformations.SingleOrDefault(con => con.ContactType == ContactType.business_POBox && con.ContactValue == value) == null,
                        PropertyName_RecipientPostBox);

                    FirePropertyChanged(PropertyName_RecipientPostBox);
                }
            }
        }

        public string RecipientPersonFirstName
        {
            get { return _RecipientPersonFirstName; }

            set
            {
                if (AreDifferent(_RecipientPersonFirstName, value))
                {
                    _RecipientPersonFirstName = value;
                    StoreDirtyInformation(AreDifferent(DataContract.RecipientPersonFirstName, value), PropertyName_RecipientPersonFirstName);
                    FirePropertyChanged(PropertyName_RecipientPersonFirstName);
                }
            }
        }

        public string RecipientPersonLastName
        {
            get { return _RecipientPersonLastName; }

            set
            {
                if (AreDifferent(_RecipientPersonLastName, value))
                {
                    _RecipientPersonLastName = value;
                    StoreDirtyInformation(AreDifferent(DataContract.RecipientPersonLastName, value), PropertyName_RecipientPersonLastName);
                    FirePropertyChanged(PropertyName_RecipientPersonLastName);
                }
            }
        }

        public long? RecipientPersonOid
        {
            get { return _RecipientPersonOid; }

            set
            {
                if (AreDifferent(_RecipientPersonOid, value))
                {
                    _RecipientPersonOid = value;
                    StoreDirtyInformation(AreDifferent(DataContract.RecipientPersonOid, value), PropertyName_RecipientPersonOid);
                    FirePropertyChanged(PropertyName_RecipientPersonOid);
                }
            }
        }

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public string RecipientPostCode
        {
            get { return _RecipientPostCode; }

            set
            {
                if (AreDifferent(_RecipientPostCode, value))
                {
                    _RecipientPostCode = value;
                    StoreDirtyInformation(AreDifferent(DataContract.RecipientPostCode, value), PropertyName_RecipientPostCode);
                    FirePropertyChanged(PropertyName_RecipientPostCode);
                }
            }
        }

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public string RecipientStreet
        {
            get { return _RecipientStreet; }

            set
            {
                if (AreDifferent(_RecipientStreet, value))
                {
                    _RecipientStreet = value;
                    StoreDirtyInformation(AreDifferent(DataContract.RecipientStreet, value), PropertyName_RecipientStreet);
                    FirePropertyChanged(PropertyName_RecipientStreet);
                }
            }
        }

        public string RecipientString
        {
            get
            {
                string result = _RecipientOrganisation;
                if (!string.IsNullOrEmpty(result) && (!string.IsNullOrEmpty(_RecipientPersonFirstName) || !string.IsNullOrEmpty(_RecipientPersonLastName)))
                {
                    result += " - ";
                }

                result += _RecipientPersonFirstName + " " + _RecipientPersonLastName;
                return result;
            }

            set { }
        }

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public string RecipientTown
        {
            get { return _RecipientTown; }

            set
            {
                if (AreDifferent(_RecipientTown, value))
                {
                    _RecipientTown = value;
                    StoreDirtyInformation(AreDifferent(DataContract.RecipientTown, value), PropertyName_RecipientTown);
                    FirePropertyChanged(PropertyName_RecipientTown);
                }
            }
        }

        public string ReportLink
        {
            get
            {
                string url = string.Empty;

                switch (DataContract.Type)
                {
                    case InvoiceType.CustomerEquity:
                        url = BeWoApp.SiteOfOrigin + "/ReportView.aspx?ioid=" + DataContract.InvoiceBaseOid + "&type=CustomerInvoice";
                        break;
                    case InvoiceType.General:
                        url = BeWoApp.SiteOfOrigin + "/ReportView.aspx?ioid=" + DataContract.InvoiceBaseOid + "&type=CustomerInvoice";
                        break;
                    case InvoiceType.Settlement:
                        url = BeWoApp.SiteOfOrigin + "/ReportView.aspx?ibid=" + DataContract.InvoiceBaseOid + "&type=" + Utils.EnumName(ReportTypes.Settlement);
                        break;
                    case InvoiceType.Service:
                        url = BeWoApp.SiteOfOrigin + "/ReportView.aspx?ibid=" + DataContract.InvoiceBaseOid + "&type=" + Utils.EnumName(ReportTypes.ServiceInvoice);
                        break;
                    default:
                        url = string.Empty;
                        break;
                }

                return url;
            }
        }

        public string SenderDivision
        {
            get { return _SenderDivision; }

            set
            {
                if (AreDifferent(_SenderDivision, value))
                {
                    _SenderDivision = value;
                    StoreDirtyInformation(AreDifferent(DataContract.SenderDivision, value), PropertyName_SenderDivision);
                    FirePropertyChanged(PropertyName_SenderDivision);
                }
            }
        }

        public string SenderFax
        {
            get { return _SenderFax; }

            set
            {
                if (AreDifferent(_SenderFax, value))
                {
                    _SenderFax = value;

                    StoreDirtyInformation(
                        DataContract.SenderContactInformations.SingleOrDefault(con => con.ContactType == ContactType.business_Fax && con.ContactValue == value) == null,
                        PropertyName_SenderFax);

                    FirePropertyChanged(PropertyName_SenderFax);
                }
            }
        }

        public string SenderFirstName
        {
            get { return _SenderFirstName; }

            set
            {
                if (AreDifferent(_SenderFirstName, value))
                {
                    _SenderFirstName = value;
                    StoreDirtyInformation(AreDifferent(DataContract.SenderFirstName, value), PropertyName_SenderFirstName);
                    FirePropertyChanged(PropertyName_SenderFirstName);
                }
            }
        }

        public string SenderLastName
        {
            get { return _SenderLastName; }

            set
            {
                if (AreDifferent(_SenderLastName, value))
                {
                    _SenderLastName = value;
                    StoreDirtyInformation(AreDifferent(DataContract.SenderLastName, value), PropertyName_SenderLastName);
                    FirePropertyChanged(PropertyName_SenderLastName);
                }
            }
        }

        public string SenderOrganisation
        {
            get { return _SenderOrganisation; }

            set
            {
                if (AreDifferent(_SenderOrganisation, value))
                {
                    _SenderOrganisation = value;
                    StoreDirtyInformation(AreDifferent(DataContract.SenderOrganisation, value), PropertyName_SenderOrganisation);
                    FirePropertyChanged(PropertyName_SenderOrganisation);
                }
            }
        }

        public string SenderPhone
        {
            get { return _SenderPhone; }

            set
            {
                if (AreDifferent(_SenderPhone, value))
                {
                    _SenderPhone = value;

                    StoreDirtyInformation(
                        DataContract.SenderContactInformations.Where(con => con.ContactType == ContactType.business_Phone && con.ContactValue == value).SingleOrDefault() == null,
                        PropertyName_SenderPhone);

                    FirePropertyChanged(PropertyName_SenderPhone);
                }
            }
        }

        public long? SupportConceptOid
        {
            get { return _SupportConceptOid; }

            set
            {
                if (AreDifferent(_SupportConceptOid, value))
                {
                    _SupportConceptOid = value;
                    StoreDirtyInformation(AreDifferent(DataContract.SupportConceptOid, value), PropertyName_SupportConceptOid);
                    FirePropertyChanged(PropertyName_SupportConceptOid);
                }
            }
        }

        public decimal? TotalAmount
        {
            get { return _TotalAmount; }

            set
            {
                if (AreDifferent(_TotalAmount, value))
                {
                    _TotalAmount = value;
                    StoreDirtyInformation(AreDifferent(DataContract.TotalAmount, value), PropertyName_TotalAmount);
                    FirePropertyChanged(PropertyName_TotalAmount);
                }
            }
        }

        public string TypeString
        {
            get
            {
                if (!String.IsNullOrEmpty(DataContract.InvoiceTypeText))
                {
                    return DataContract.InvoiceTypeText;
                }
                return _TypeTranslation[DataContract.Type];
            }
        }

        #endregion

        #region Methods

        protected override void InitByDataContract(InvoiceBaseDC pDataContract)
        {
            _SupportConceptOid        = pDataContract.SupportConceptOid;
            _SenderOrganisation       = pDataContract.SenderOrganisation;
            _SenderLastName           = pDataContract.SenderLastName;
            _SenderFirstName          = pDataContract.SenderFirstName;
            _SenderDivision           = pDataContract.SenderDivision;
            _RecipientPersonOid       = pDataContract.RecipientPersonOid;
            _RecipientPersonLastName  = pDataContract.RecipientPersonLastName;
            _RecipientPersonFirstName = pDataContract.RecipientPersonFirstName;
            _RecipientOrganisationOid = pDataContract.RecipientOrganisationOid;
            _RecipientOrganisation    = pDataContract.RecipientOrganisation;
            _RecipientDivision        = pDataContract.RecipientDivision;
            _RecipientCustomerOid     = pDataContract.RecipientCustomerOid;
            _RecipientCostBearerOid   = pDataContract.RecipientCostBearerOid;
            _RecipientStreet          = pDataContract.RecipientStreet;
            _RecipientTown            = pDataContract.RecipientTown;
            _RecipientPostCode        = pDataContract.RecipientPostCode;
            _IsPaid                   = pDataContract.IsPaid;
            _InvoiceTitle             = pDataContract.InvoiceTitle;
            _InvoiceNumber            = pDataContract.InvoiceNumber;
            _InvoiceDate              = pDataContract.InvoiceDate;
            _DueDate                  = pDataContract.DueDate;
            _CreatorLastName          = pDataContract.CreatorLastName;
            _CreatorFirstName         = pDataContract.CreatorFirstName;
            _AccountingPeriodEnd      = pDataContract.AccountingPeriodEnd;
            _AccountingPeriodStart    = pDataContract.AccountingPeriodStart;
            _Notice                   = pDataContract.Notice;
            _DunningLetterCount       = pDataContract.DunningLetterCount;
            _TotalAmount              = pDataContract.TotalAmount;

            foreach (var iContactDC in pDataContract.SenderContactInformations)
            {
                if (iContactDC.ContactType == ContactType.business_Fax)
                {
                    _SenderFax = iContactDC.ContactValue;
                }

                if (iContactDC.ContactType == ContactType.business_Phone)
                {
                    _SenderPhone = iContactDC.ContactValue;
                }

                if (iContactDC.ContactType == ContactType.business_POBox)
                {
                    _RecipientPostBox = iContactDC.ContactValue;
                }
            }

            _InvoiceItems = new InvoiceItemListVM(pDataContract.InvoiceItems);
        }

        protected override InvoiceBaseDC MapToDataContract(InvoiceBaseDC pDataContract, bool doCommit)
        {
            pDataContract.SupportConceptOid        = _SupportConceptOid;
            pDataContract.SenderOrganisation       = _SenderOrganisation;
            pDataContract.SenderLastName           = _SenderLastName;
            pDataContract.SenderFirstName          = _SenderFirstName;
            pDataContract.SenderDivision           = _SenderDivision;
            pDataContract.RecipientPersonOid       = _RecipientPersonOid;
            pDataContract.RecipientPersonLastName  = _RecipientPersonLastName;
            pDataContract.RecipientPersonFirstName = _RecipientPersonFirstName;
            pDataContract.RecipientOrganisationOid = _RecipientOrganisationOid;
            pDataContract.RecipientOrganisation    = _RecipientOrganisation;
            pDataContract.RecipientDivision        = _RecipientDivision;
            pDataContract.RecipientCustomerOid     = _RecipientCustomerOid;
            pDataContract.RecipientCostBearerOid   = _RecipientCostBearerOid;
            pDataContract.RecipientPostCode        = _RecipientPostCode;
            pDataContract.RecipientStreet          = _RecipientStreet;
            pDataContract.RecipientTown            = _RecipientTown;
            pDataContract.IsPaid                   = _IsPaid;
            pDataContract.InvoiceTitle             = _InvoiceTitle;
            pDataContract.InvoiceNumber            = _InvoiceNumber;
            pDataContract.InvoiceDate              = _InvoiceDate;
            pDataContract.DueDate                  = _DueDate;
            pDataContract.CreatorLastName          = _CreatorLastName;
            pDataContract.CreatorFirstName         = _CreatorFirstName;
            pDataContract.AccountingPeriodEnd      = _AccountingPeriodEnd;
            pDataContract.AccountingPeriodStart    = _AccountingPeriodStart;
            pDataContract.Notice                   = _Notice;
            pDataContract.DunningLetterCount       = _DunningLetterCount;
            pDataContract.TotalAmount              = _TotalAmount;

            CommitSenderContact(_SenderFax, ContactType.business_Fax);
            CommitSenderContact(_SenderPhone, ContactType.business_Phone);
            CommitSenderContact(_RecipientPostBox, ContactType.business_POBox);

            pDataContract.InvoiceItems = _InvoiceItems.CopyToDCList(doCommit);

            return pDataContract;
        }

        private void CommitSenderContact(string pValue, ContactType pContactType)
        {
            var lOldContactDCs = DataContract.SenderContactInformations != null ? DataContract.SenderContactInformations.ToList() : new List<ContactDC>();
            var lContactDC     = lOldContactDCs.SingleOrDefault(con => con.ContactType == pContactType);

            if (string.IsNullOrEmpty(pValue) && lContactDC != null)
            {
                lOldContactDCs.Remove(lContactDC);
            }
            else if (!string.IsNullOrEmpty(pValue))
            {
                if (lContactDC == null)
                {
                    lContactDC = new ContactDC
                    {
                        ContactType = pContactType
                    };

                    lOldContactDCs.Add(lContactDC);
                }

                lContactDC.ContactValue = pValue;
            }

            DataContract.SenderContactInformations = lOldContactDCs;
        }

        #endregion
    }
}