private ContractLoad GetContract(tblContractDTO oldContract)
        {
            var key             = _contractKeyReservationHelper.ParseAndRegisterKey(oldContract.SerializedKey);
            var requiresKeySync = key == null;

            _loadCount.AddRead(EntityType.Contract);

            var timestamp = oldContract.EntryDate.ConvertLocalToUTC() ?? oldContract.KDate ?? oldContract.BegDate;

            if (timestamp == null)
            {
                Log(new CallbackParameters(CallbackReason.UnderterminedTimestamp)
                {
                    OldContract = oldContract
                });
                return(null);
            }

            if (oldContract.KDate == null)
            {
                Log(new CallbackParameters(CallbackReason.NullContractDate)
                {
                    OldContract = oldContract
                });
                return(null);
            }

            var employeeId = _newContextHelper.DefaultEmployee.EmployeeId;

            if (oldContract.EmployeeId == null)
            {
                Log(new CallbackParameters(CallbackReason.DefaultEmployee)
                {
                    OldContract = oldContract
                });
            }
            else
            {
                employeeId = oldContract.EmployeeId.Value;
            }

            var customer = _newContextHelper.GetCompany(oldContract.Company_IA, CompanyType.Customer);

            if (customer == null)
            {
                Log(new CallbackParameters(CallbackReason.CustomerNotLoaded)
                {
                    OldContract = oldContract
                });
                return(null);
            }

            var broker = _newContextHelper.GetCompany(oldContract.Broker, CompanyType.Broker);

            if (broker == null)
            {
                Log(new CallbackParameters(CallbackReason.BrokerNotLoaded)
                {
                    OldContract = oldContract
                });
                return(null);
            }

            Facility facility = null;

            if (oldContract.WHID == null)
            {
                facility = _newContextHelper.Facilities.Values.FirstOrDefault(f => f.Name == "Rincon");
                Log(new CallbackParameters(CallbackReason.DefaultRinconWarehouse)
                {
                    OldContract = oldContract
                });
            }
            else
            {
                facility = _newContextHelper.GetFacility(oldContract.WHID);
            }

            if (facility == null)
            {
                Log(new CallbackParameters(CallbackReason.WarehouseNotLoaded)
                {
                    OldContract = oldContract
                });
                return(null);
            }

            var contractType = GetContractType(oldContract.KType);

            if (contractType == null)
            {
                Log(new CallbackParameters(CallbackReason.UndeterminedContractType)
                {
                    OldContract = oldContract
                });
                return(null);
            }

            var contractStatus = GetContractStatus(oldContract.KStatus);

            if (contractStatus == null)
            {
                Log((new CallbackParameters(CallbackReason.UndeterminedContractStatus)
                {
                    OldContract = oldContract
                }));
                return(null);
            }

            var commentsNotebook = _notebookFactory.BirthNext(timestamp.Value, employeeId, oldContract.Comments);

            key = key ?? _contractKeyReservationHelper.GetNextAndRegisterKey(new ContractKeyReservationHelper.Key
            {
                ContractKey_Year     = oldContract.KDate.Value.Year,
                ContractKey_Sequence = 0
            });

            var newContract = new ContractLoad
            {
                RequiresKeySync = requiresKeySync,
                EmployeeId      = oldContract.EmployeeId ?? _newContextHelper.DefaultEmployee.EmployeeId,
                TimeStamp       = timestamp.Value,

                ContractYear     = key.ContractKey_Year,
                ContractSequence = key.ContractKey_Sequence,

                ContractId  = oldContract.ContractID,
                CustomerId  = customer.Id,
                ContactName = oldContract.Contact_IA,
                FOB         = oldContract.FOB,
                BrokerId    = broker.Id,
                DefaultPickFromWarehouseId = facility.Id,

                ContractType   = contractType.Value,
                ContractStatus = contractStatus.Value,
                ContractDate   = oldContract.KDate.Value,
                TimeCreated    = timestamp.Value.Date,
                TermBegin      = oldContract.BegDate.GetDate(),
                TermEnd        = oldContract.EndDate.GetDate(),

                PaymentTerms          = oldContract.PmtTerms,
                NotesToPrint          = oldContract.Notes2Print,
                CustomerPurchaseOrder = oldContract.PONum,
                ContactAddress        = new Address
                {
                    AddressLine1 = oldContract.Address1_IA,
                    AddressLine2 = oldContract.Address2_IA,
                    AddressLine3 = oldContract.Address3_IA,
                    City         = oldContract.City_IA,
                    State        = oldContract.State_IA,
                    PostalCode   = oldContract.Zip_IA,
                    Country      = oldContract.Country_IA
                },

                CommentsDate     = commentsNotebook.Date,
                CommentsSequence = commentsNotebook.Sequence,
                Comments         = commentsNotebook
            };

            SerializableContract.DeserializeIntoContract(newContract, oldContract.Serialized);

            GetContractItems(newContract, oldContract);

            return(newContract);
        }
        private void GetContractItems(Contract newContract, tblContractDTO oldContract)
        {
            var sequence = 0;
            var items    = new List <ContractItem>();

            foreach (var oldDetail in oldContract.Details)
            {
                _loadCount.AddRead(EntityType.ContractItem);

                var chileProduct = _newContextHelper.GetChileProduct(oldDetail.ProdID);
                if (chileProduct == null)
                {
                    Log(new CallbackParameters(CallbackReason.ChileProductNotLoaded)
                    {
                        Detail = oldDetail
                    });
                    continue;
                }

                var packaging = _newContextHelper.GetPackagingProduct(oldDetail.PkgID);
                if (packaging == null)
                {
                    Log(new CallbackParameters(CallbackReason.PackagingProductNotLoaded)
                    {
                        Detail = oldDetail
                    });
                    continue;
                }

                var treatment = _newContextHelper.NoTreatment;
                if (oldDetail.TrtmtID != null)
                {
                    treatment = _newContextHelper.GetInventoryTreatment(oldDetail.TrtmtID.Value);
                }

                if (treatment == null)
                {
                    Log(new CallbackParameters(CallbackReason.TreatmentNotLoaded)
                    {
                        Detail = oldDetail
                    });
                    continue;
                }

                items.Add(new ContractItem
                {
                    KDetailID            = oldDetail.KDetailID,
                    ContractYear         = newContract.ContractYear,
                    ContractSequence     = newContract.ContractSequence,
                    ContractItemSequence = sequence++,

                    ChileProductId     = chileProduct.Id,
                    PackagingProductId = packaging.Id,
                    TreatmentId        = treatment.Id,
                    UseCustomerSpec    = (oldDetail.Spec ?? "").Replace(" ", "").ToUpper() == "CUST",

                    CustomerProductCode = oldDetail.CustProductCode,
                    Quantity            = (int)(oldDetail.Quantity ?? 0),
                    PriceBase           = (double)(oldDetail.Price ?? 0),
                    PriceFreight        = (double)(oldDetail.FreightP ?? 0),
                    PriceTreatment      = (double)(oldDetail.TrtnmntP ?? 0),
                    PriceWarehouse      = (double)(oldDetail.WHCostP ?? 0),
                    PriceRebate         = (double)(oldDetail.Rebate ?? 0)
                });
            }

            newContract.ContractItems = items;
        }