/// <summary>
        /// Map data
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="oldObject"></param>
        /// <param name="serviceArea"></param>
        /// <param name="systemId"></param>
        private static void CopyToInstance(DbAppContext dbContext, ImportModels.ServiceArea oldObject,
                                           ref HetServiceArea serviceArea, string systemId)
        {
            try
            {
                if (serviceArea == null)
                {
                    serviceArea = new HetServiceArea();
                }

                if (oldObject.Service_Area_Id <= 0)
                {
                    return;
                }

                serviceArea.ServiceAreaId         = oldObject.Service_Area_Id;
                serviceArea.MinistryServiceAreaId = oldObject.Service_Area_Id;
                serviceArea.Name = oldObject.Service_Area_Desc.Trim();

                // remove " CA" from Service Area Names
                if (serviceArea.Name.EndsWith(" CA"))
                {
                    serviceArea.Name = serviceArea.Name.Replace(" CA", "");
                }

                // service area number
                if (oldObject.Service_Area_Cd != null)
                {
                    serviceArea.AreaNumber = int.Parse(oldObject.Service_Area_Cd);
                }

                // get the district for this service area
                int tempServiceAreaId = GetServiceAreaId(serviceArea.Name);

                if (tempServiceAreaId > 0)
                {
                    HetDistrict district = dbContext.HetDistrict.AsNoTracking()
                                           .FirstOrDefault(x => x.MinistryDistrictId == tempServiceAreaId);

                    if (district != null)
                    {
                        serviceArea.DistrictId = district.DistrictId;
                    }
                }

                if (oldObject.FiscalStart != null)
                {
                    serviceArea.FiscalStartDate = DateTime.ParseExact(oldObject.FiscalStart.Trim().Substring(0, 10), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
                }

                if (oldObject.FiscalEnd != null)
                {
                    serviceArea.FiscalEndDate = DateTime.ParseExact(oldObject.FiscalEnd.Trim().Substring(0, 10), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
                }

                // Address, Phone, etc.
                serviceArea.Address = oldObject.Address.Trim();

                if (serviceArea.Address.Length > 255)
                {
                    serviceArea.Address = serviceArea.Address.Substring(1, 255);
                }

                serviceArea.Phone = oldObject.Phone.Trim();

                if (serviceArea.Phone.Length > 50)
                {
                    serviceArea.Phone = serviceArea.Phone.Substring(1, 50);
                }

                serviceArea.Phone = ImportUtility.FormatPhone(serviceArea.Phone);

                serviceArea.Fax = oldObject.Fax.Trim();

                if (serviceArea.Fax.Length > 50)
                {
                    serviceArea.Fax = serviceArea.Fax.Substring(1, 50);
                }

                serviceArea.Fax = ImportUtility.FormatPhone(serviceArea.Fax);

                serviceArea.SupportingDocuments = oldObject.Sup_Docs.Trim();

                if (serviceArea.SupportingDocuments.Length > 500)
                {
                    serviceArea.SupportingDocuments = serviceArea.SupportingDocuments.Substring(1, 500);
                }

                serviceArea.AppCreateUserid        = systemId;
                serviceArea.AppCreateTimestamp     = DateTime.UtcNow;
                serviceArea.AppLastUpdateUserid    = systemId;
                serviceArea.AppLastUpdateTimestamp = DateTime.UtcNow;

                dbContext.HetServiceArea.Add(serviceArea);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("***Error*** - Service Area Id: " + oldObject.Service_Area_Id);
                Debug.WriteLine(ex.Message);
                throw;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Map data
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="oldObject"></param>
        /// <param name="owner"></param>
        /// <param name="systemId"></param>
        /// <param name="maxOwnerIndex"></param>
        private static void CopyToInstance(DbAppContext dbContext, ImportModels.Owner oldObject,
                                           ref HetOwner owner, string systemId, ref int maxOwnerIndex)
        {
            try
            {
                if (owner != null)
                {
                    return;
                }

                owner = new HetOwner {
                    OwnerId = ++maxOwnerIndex
                };

                // ***********************************************
                // set owner code
                // ***********************************************
                string tempOwnerCode = ImportUtility.CleanString(oldObject.Owner_Cd).ToUpper();

                if (!string.IsNullOrEmpty(tempOwnerCode))
                {
                    owner.OwnerCode = tempOwnerCode;
                }
                else
                {
                    // must have an owner code: HETS-817
                    return;
                }

                // ***********************************************
                // set company name
                // ***********************************************
                string tempCompanyName = ImportUtility.CleanString(oldObject.Company_Name);

                if (!string.IsNullOrEmpty(tempCompanyName))
                {
                    tempCompanyName = ImportUtility.GetCapitalCase(tempCompanyName);

                    // fix some capital case names
                    tempCompanyName = tempCompanyName.Replace("Bc", "BC");
                    tempCompanyName = tempCompanyName.Replace("Rv", "RV");
                    tempCompanyName = tempCompanyName.Replace(", & ", " & ");

                    owner.OrganizationName = tempCompanyName;
                }

                // ***********************************************
                // DBA Name
                // ***********************************************
                string tempDbaName = ImportUtility.CleanString(oldObject.Operating_AS);

                if (!string.IsNullOrEmpty(tempDbaName))
                {
                    tempDbaName           = ImportUtility.GetCapitalCase(tempDbaName);
                    owner.DoingBusinessAs = tempDbaName;
                }

                // ***********************************************
                // maintenance contractor
                // ***********************************************
                if (oldObject.Maintenance_Contractor != null)
                {
                    owner.IsMaintenanceContractor = (oldObject.Maintenance_Contractor.Trim() == "Y");
                }

                // ***********************************************
                // residency
                // ***********************************************
                if (oldObject.Local_To_Area != null)
                {
                    owner.MeetsResidency = (oldObject.Local_To_Area.Trim() == "Y");
                }

                // ***********************************************
                // set local area
                // ***********************************************
                HetLocalArea localArea = dbContext.HetLocalArea.FirstOrDefault(x => x.LocalAreaId == oldObject.Area_Id);

                if (localArea != null)
                {
                    owner.LocalAreaId = localArea.LocalAreaId;
                }

                // ***********************************************
                // set other attributes
                // ***********************************************
                owner.WorkSafeBcexpiryDate = ImportUtility.CleanDate(oldObject.CGL_End_Dt);
                owner.CglendDate           = ImportUtility.CleanDate(oldObject.CGL_End_Dt);

                owner.WorkSafeBcpolicyNumber = ImportUtility.CleanString(oldObject.WCB_Num);

                if (owner.WorkSafeBcpolicyNumber == "0")
                {
                    owner.WorkSafeBcpolicyNumber = null;
                }

                string tempCglCompanyName = ImportUtility.CleanString(oldObject.CGL_Company);
                tempCglCompanyName = ImportUtility.GetCapitalCase(tempCglCompanyName);

                if (!string.IsNullOrEmpty(tempCglCompanyName))
                {
                    owner.CglCompanyName = tempCglCompanyName;
                }

                owner.CglPolicyNumber = ImportUtility.CleanString(oldObject.CGL_Policy);

                if (owner.CglPolicyNumber == "")
                {
                    owner.CglPolicyNumber = null;
                }

                // ***********************************************
                // manage archive and owner status
                // ***********************************************
                string tempArchive = oldObject.Archive_Cd;
                string tempStatus  = oldObject.Status_Cd.Trim();

                if (tempArchive == "Y")
                {
                    int?statusId = StatusHelper.GetStatusId("Archived", "ownerStatus", dbContext);

                    if (statusId == null)
                    {
                        throw new DataException(string.Format("Status Id cannot be null (OwnerIndex: {0}", maxOwnerIndex));
                    }

                    // archived!
                    owner.ArchiveCode       = "Y";
                    owner.ArchiveDate       = DateTime.UtcNow;
                    owner.ArchiveReason     = "Imported from BC Bid";
                    owner.OwnerStatusTypeId = (int)statusId;

                    string tempArchiveReason = ImportUtility.CleanString(oldObject.Archive_Reason);

                    if (!string.IsNullOrEmpty(tempArchiveReason))
                    {
                        owner.ArchiveReason = ImportUtility.GetUppercaseFirst(tempArchiveReason);
                    }
                }
                else
                {
                    int?statusId = StatusHelper.GetStatusId("Approved", "ownerStatus", dbContext);

                    if (statusId == null)
                    {
                        throw new DataException(string.Format("Status Id cannot be null (OwnerIndex: {0}", maxOwnerIndex));
                    }

                    int?statusIdUnapproved = StatusHelper.GetStatusId("Unapproved", "ownerStatus", dbContext);

                    if (statusIdUnapproved == null)
                    {
                        throw new DataException(string.Format("Status Id cannot be null (OwnerIndex: {0}", maxOwnerIndex));
                    }

                    owner.ArchiveCode       = "N";
                    owner.ArchiveDate       = null;
                    owner.ArchiveReason     = null;
                    owner.OwnerStatusTypeId = tempStatus == "A" ? (int)statusId : (int)statusIdUnapproved;
                    owner.StatusComment     = string.Format("Imported from BC Bid ({0})", tempStatus);
                }

                // ***********************************************
                // manage owner information
                // ***********************************************
                string tempOwnerFirstName = ImportUtility.CleanString(oldObject.Owner_First_Name);
                tempOwnerFirstName = ImportUtility.GetCapitalCase(tempOwnerFirstName);

                string tempOwnerLastName = ImportUtility.CleanString(oldObject.Owner_Last_Name);
                tempOwnerLastName = ImportUtility.GetCapitalCase(tempOwnerLastName);

                // some fields have duplicates in the name
                if (tempOwnerLastName == tempOwnerFirstName)
                {
                    tempOwnerFirstName = "";
                }

                if (!string.IsNullOrEmpty(tempOwnerLastName))
                {
                    owner.Surname = tempOwnerLastName;
                }

                if (!string.IsNullOrEmpty(tempOwnerFirstName))
                {
                    owner.GivenName = tempOwnerFirstName;
                }

                // if there is no company name - create one
                string tempName = "";

                if (string.IsNullOrEmpty(tempCompanyName) &&
                    !string.IsNullOrEmpty(owner.OwnerCode) &&
                    string.IsNullOrEmpty(tempOwnerLastName))
                {
                    owner.OrganizationName = owner.OwnerCode;
                    tempName = owner.OrganizationName;
                }
                else if (string.IsNullOrEmpty(tempCompanyName) && !string.IsNullOrEmpty(owner.OwnerCode))
                {
                    owner.OrganizationName = string.Format("{0} - {1} {2}", owner.OwnerCode, tempOwnerFirstName, tempOwnerLastName);
                    tempName = owner.OrganizationName;
                }
                else if (string.IsNullOrEmpty(tempCompanyName))
                {
                    owner.OrganizationName = string.Format("{0} {1}", tempOwnerFirstName, tempOwnerLastName);
                    tempName = owner.OrganizationName;
                }

                // check if the organization name is actually a "Ltd" or other company name
                // (in BC Bid the names are sometimes used to store the org)
                if (!string.IsNullOrEmpty(tempName) &&
                    tempName.IndexOf(" Ltd", StringComparison.Ordinal) > -1 ||
                    tempName.IndexOf(" Resort", StringComparison.Ordinal) > -1 ||
                    tempName.IndexOf(" Oilfield", StringComparison.Ordinal) > -1 ||
                    tempName.IndexOf(" Nation", StringComparison.Ordinal) > -1 ||
                    tempName.IndexOf(" Ventures", StringComparison.Ordinal) > -1 ||
                    tempName.IndexOf(" Indian Band", StringComparison.Ordinal) > -1)
                {
                    owner.Surname   = null;
                    owner.GivenName = null;

                    if (!string.IsNullOrEmpty(owner.OwnerCode))
                    {
                        owner.OrganizationName = string.Format("{0} {1}", tempOwnerFirstName, tempOwnerLastName);
                    }
                }

                // ***********************************************
                // format company names
                // ***********************************************
                if (owner.OrganizationName.EndsWith(" Ltd") ||
                    owner.OrganizationName.EndsWith(" Ulc") ||
                    owner.OrganizationName.EndsWith(" Inc") ||
                    owner.OrganizationName.EndsWith(" Co"))
                {
                    owner.OrganizationName = owner.OrganizationName + ".";
                }

                // ***********************************************
                // manage contacts
                // ***********************************************
                bool contactExists = false;

                if (owner.HetContact != null)
                {
                    foreach (HetContact contactItem in owner.HetContact)
                    {
                        if (!string.Equals(contactItem.GivenName, tempOwnerFirstName, StringComparison.InvariantCultureIgnoreCase) &&
                            !string.Equals(contactItem.Surname, tempOwnerLastName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            contactExists = true;
                        }
                    }
                }

                string tempPhone  = ImportUtility.FormatPhone(oldObject.Ph_Country_Code, oldObject.Ph_Area_Code, oldObject.Ph_Number, oldObject.Ph_Extension);
                string tempFax    = ImportUtility.FormatPhone(oldObject.Fax_Country_Code, oldObject.Fax_Area_Code, oldObject.Fax_Number, oldObject.Fax_Extension);
                string tempMobile = ImportUtility.FormatPhone(oldObject.Cell_Country_Code, oldObject.Cell_Area_Code, oldObject.Cell_Number, oldObject.Cell_Extension);

                // only add if they don't already exist
                if (!contactExists && !string.IsNullOrEmpty(tempOwnerLastName))
                {
                    HetContact ownerContact = new HetContact
                    {
                        Role                   = "Owner",
                        Province               = "BC",
                        Notes                  = "",
                        WorkPhoneNumber        = tempPhone,
                        MobilePhoneNumber      = tempMobile,
                        FaxPhoneNumber         = tempFax,
                        AppCreateUserid        = systemId,
                        AppCreateTimestamp     = DateTime.UtcNow,
                        AppLastUpdateUserid    = systemId,
                        AppLastUpdateTimestamp = DateTime.UtcNow
                    };

                    if (!string.IsNullOrEmpty(tempOwnerLastName))
                    {
                        ownerContact.Surname = tempOwnerLastName;
                    }

                    if (!string.IsNullOrEmpty(tempOwnerFirstName))
                    {
                        ownerContact.GivenName = tempOwnerFirstName;
                    }

                    if (owner.HetContact == null)
                    {
                        owner.HetContact = new List <HetContact>();
                    }

                    owner.HetContact.Add(ownerContact);
                }

                // is the BC Bid contact the same as the owner?
                string tempContactPerson = ImportUtility.CleanString(oldObject.Contact_Person);
                tempContactPerson = ImportUtility.GetCapitalCase(tempContactPerson);

                if (!string.IsNullOrEmpty(tempContactPerson))
                {
                    string tempContactPhone = ImportUtility.FormatPhone(oldObject.Contact_Country_Code, oldObject.Contact_Area_Code, oldObject.Contact_Number, oldObject.Contact_Extension);

                    // split the name
                    string tempContactFirstName;
                    string tempContactLastName;

                    if (tempContactPerson.IndexOf(" Or ", StringComparison.Ordinal) > -1)
                    {
                        tempContactFirstName = tempContactPerson;
                        tempContactLastName  = "";
                    }
                    else
                    {
                        tempContactFirstName = ImportUtility.GetNamePart(tempContactPerson, 1);
                        tempContactLastName  = ImportUtility.GetNamePart(tempContactPerson, 2);
                    }

                    // check if the name is unique
                    if ((!string.Equals(tempContactFirstName, tempOwnerFirstName, StringComparison.InvariantCultureIgnoreCase) &&
                         !string.Equals(tempContactLastName, tempOwnerLastName, StringComparison.InvariantCultureIgnoreCase)) ||
                        (!string.Equals(tempContactFirstName, tempOwnerFirstName, StringComparison.InvariantCultureIgnoreCase) &&
                         !string.Equals(tempContactFirstName, tempOwnerLastName, StringComparison.InvariantCultureIgnoreCase) &&
                         !string.IsNullOrEmpty(tempContactLastName)))
                    {
                        // check if the name(s) already exist
                        contactExists = false;

                        if (owner.HetContact != null)
                        {
                            foreach (HetContact contactItem in owner.HetContact)
                            {
                                if (string.Equals(contactItem.GivenName, tempContactFirstName, StringComparison.InvariantCultureIgnoreCase) &&
                                    string.Equals(contactItem.Surname, tempContactLastName, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    contactExists = true;
                                }
                            }
                        }

                        if (!contactExists)
                        {
                            string tempPhoneNumber = "";

                            if (tempPhone != tempContactPhone && !string.IsNullOrEmpty(tempContactPhone))
                            {
                                tempPhoneNumber = tempContactPhone;
                            }

                            HetContact primaryContact = new HetContact
                            {
                                Role                   = "Primary Contact",
                                Province               = "BC",
                                WorkPhoneNumber        = tempPhone,
                                MobilePhoneNumber      = tempMobile,
                                FaxPhoneNumber         = tempFax,
                                Notes                  = tempPhoneNumber,
                                AppCreateUserid        = systemId,
                                AppCreateTimestamp     = DateTime.UtcNow,
                                AppLastUpdateUserid    = systemId,
                                AppLastUpdateTimestamp = DateTime.UtcNow
                            };

                            if (!string.IsNullOrEmpty(tempContactLastName))
                            {
                                primaryContact.Surname = tempContactLastName;
                            }

                            if (!string.IsNullOrEmpty(tempContactFirstName))
                            {
                                primaryContact.GivenName = tempContactFirstName;
                            }

                            string tempComment = ImportUtility.CleanString(oldObject.Comment);

                            if (!string.IsNullOrEmpty(tempComment))
                            {
                                tempComment          = ImportUtility.GetCapitalCase(tempComment);
                                primaryContact.Notes = tempComment;
                            }

                            owner.PrimaryContact = primaryContact; // since this was the default in the file - make it primary
                        }
                    }
                }

                // ensure the contact is valid
                if (owner.HetContact != null)
                {
                    for (int i = owner.HetContact.Count - 1; i >= 0; i--)
                    {
                        if (string.IsNullOrEmpty(owner.HetContact.ElementAt(i).GivenName) &&
                            string.IsNullOrEmpty(owner.HetContact.ElementAt(i).Surname))
                        {
                            owner.HetContact.Remove(owner.HetContact.ElementAt(i));
                        }
                    }

                    // update primary
                    if (owner.HetContact.Count > 0 &&
                        owner.PrimaryContact == null)
                    {
                        owner.PrimaryContact = owner.HetContact.ElementAt(0);
                        owner.HetContact.Remove(owner.HetContact.ElementAt(0));
                    }
                }

                // ***********************************************
                // create owner
                // ***********************************************
                owner.AppCreateUserid        = systemId;
                owner.AppCreateTimestamp     = DateTime.UtcNow;
                owner.AppLastUpdateUserid    = systemId;
                owner.AppLastUpdateTimestamp = DateTime.UtcNow;

                dbContext.HetOwner.Add(owner);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("***Error*** - Owner Code: " + owner.OwnerCode);
                Debug.WriteLine("***Error*** - Master Owner Index: " + maxOwnerIndex);
                Debug.WriteLine(ex.Message);
                throw;
            }
        }