private Guid? CreateContract(Budget2DataContext context, Guid budgetVersionId, API.Interface.DataContracts.Contract contract, List<Contract> caList, List<Currency> currencyList, Guid? parentContractUid)
        {
            var currContract =
                (from curr in caList where curr.ExternalId == contract.Id && (!parentContractUid.HasValue || curr.ParentId == parentContractUid.Value) select curr).FirstOrDefault();

            if (currContract != null && currContract.IsProtected)
                return null;

            if (currContract == null)
            {
                #region Если контрагент не найден, то создаём его в системе

                currContract = new Contract
                                   {
                                       Id = Guid.NewGuid(),
                                       Number = contract.Prefix,
                                       NumberId = contract.Number,
                                       IsDeleted = false,
                                       BudgetVersionId = budgetVersionId,
                                       FilialId = DefaultFilialKey,
                                       AuthorId = Guid.Empty,
                                       CreateDate = DateTime.Now
                                       
                                   };
                context.Contracts.InsertOnSubmit(currContract);

                #endregion

                Logger.Log.DebugFormat("Контракт {0} {1} добавлен. ID={2}.", currContract.Number, currContract.NumberId, currContract.Id);
            }
            currContract.ChangedByID = Guid.Empty;
            currContract.ChangeDate = DateTime.Now;
            currContract.Number = contract.Prefix;
            currContract.NumberId = contract.Number;
            currContract.Date = contract.Date;
            currContract.DateFrom = contract.DateFrom;
            currContract.DateTo = contract.DateTo;
            currContract.DistributionDate = contract.CompletionDate;
            currContract.Subject = contract.Comment;
            currContract.CurrencySum = contract.Amount;
            currContract.ConditionPayment = contract.PaymentTerms;
            currContract.CurrencyRateCBPercents = (double?)(contract.RateCBRPercents.HasValue ? contract.RateCBRPercents.Value / 100 : (decimal?)null);
            currContract.CurrencyRate = (double?)contract.Rate;
            currContract.StatusId = (byte)0;
            currContract.ExternalId = contract.Id;
            currContract.ExternalCustomerId = contract.CustomerId;
            currContract.ExternalExecutorId = contract.ExecutorId;
            currContract.ExternalCustomerDetails = contract.CustomerDetails;
            currContract.ExternalExecutorDetails = contract.ExecutorDetails;
            currContract.ExternalManager = contract.Manager;
            if (parentContractUid.HasValue)
                currContract.ParentId = parentContractUid.Value;

            currContract.ExecutorCounteragentId =
                (from c in context.Counteragents
                 where c.Number == contract.ExecutorId && c.BudgetVersionId == budgetVersionId && !c.IsDeleted && !c.IsPotential
                 select new Nullable<Guid>(c.Id)).FirstOrDefault();

            currContract.CustomerCounteragentId =
                (from c in context.Counteragents
                 where c.Number == contract.CustomerId && c.BudgetVersionId == budgetVersionId && !c.IsDeleted && !c.IsPotential
                 select new Nullable<Guid>(c.Id)).FirstOrDefault();

            currContract.CurrencyId =
                (from c in currencyList
                 where c.Code == contract.Currency && !c.IsDeleted
                 select new Nullable<Guid>(c.Id)).FirstOrDefault();

            Logger.Log.DebugFormat("Контракт {0} обновлен (Id={1}).", currContract.Name, currContract.Id);

            return currContract.Id;
        }
        private Guid?CreateContract(Budget2DataContext context, Guid budgetVersionId, API.Interface.DataContracts.Contract contract, List <Contract> caList, List <Currency> currencyList, Guid?parentContractUid)
        {
            var currContract =
                (from curr in caList where curr.ExternalId == contract.Id && (!parentContractUid.HasValue || curr.ParentId == parentContractUid.Value) select curr).FirstOrDefault();

            if (currContract != null && currContract.IsProtected)
            {
                return(null);
            }

            if (currContract == null)
            {
                #region Если контрагент не найден, то создаём его в системе

                currContract = new Contract
                {
                    Id              = Guid.NewGuid(),
                    Number          = contract.Prefix,
                    NumberId        = contract.Number,
                    IsDeleted       = false,
                    BudgetVersionId = budgetVersionId,
                    FilialId        = DefaultFilialKey,
                    AuthorId        = Guid.Empty,
                    CreateDate      = DateTime.Now
                };
                context.Contracts.InsertOnSubmit(currContract);

                #endregion

                Logger.Log.DebugFormat("Контракт {0} {1} добавлен. ID={2}.", currContract.Number, currContract.NumberId, currContract.Id);
            }
            currContract.ChangedByID             = Guid.Empty;
            currContract.ChangeDate              = DateTime.Now;
            currContract.Number                  = contract.Prefix;
            currContract.NumberId                = contract.Number;
            currContract.Date                    = contract.Date;
            currContract.DateFrom                = contract.DateFrom;
            currContract.DateTo                  = contract.DateTo;
            currContract.DistributionDate        = contract.CompletionDate;
            currContract.Subject                 = contract.Comment;
            currContract.CurrencySum             = contract.Amount;
            currContract.ConditionPayment        = contract.PaymentTerms;
            currContract.CurrencyRateCBPercents  = (double?)(contract.RateCBRPercents.HasValue ? contract.RateCBRPercents.Value / 100 : (decimal?)null);
            currContract.CurrencyRate            = (double?)contract.Rate;
            currContract.StatusId                = (byte)0;
            currContract.ExternalId              = contract.Id;
            currContract.ExternalCustomerId      = contract.CustomerId;
            currContract.ExternalExecutorId      = contract.ExecutorId;
            currContract.ExternalCustomerDetails = contract.CustomerDetails;
            currContract.ExternalExecutorDetails = contract.ExecutorDetails;
            currContract.ExternalManager         = contract.Manager;
            if (parentContractUid.HasValue)
            {
                currContract.ParentId = parentContractUid.Value;
            }

            currContract.ExecutorCounteragentId =
                (from c in context.Counteragents
                 where c.Number == contract.ExecutorId && c.BudgetVersionId == budgetVersionId && !c.IsDeleted && !c.IsPotential
                 select new Nullable <Guid>(c.Id)).FirstOrDefault();

            currContract.CustomerCounteragentId =
                (from c in context.Counteragents
                 where c.Number == contract.CustomerId && c.BudgetVersionId == budgetVersionId && !c.IsDeleted && !c.IsPotential
                 select new Nullable <Guid>(c.Id)).FirstOrDefault();

            currContract.CurrencyId =
                (from c in currencyList
                 where c.Code == contract.Currency && !c.IsDeleted
                 select new Nullable <Guid>(c.Id)).FirstOrDefault();

            Logger.Log.DebugFormat("Контракт {0} обновлен (Id={1}).", currContract.Name, currContract.Id);

            return(currContract.Id);
        }