Ejemplo n.º 1
0
        public EntityReference <Customer> CreateOrUpdateCustomer(Models.Data.Customer customer)
        {
            var sageCustomer = FindSageCustomer(customer) ?? CompanyContext.Factories.CustomerFactory.Create();

            sageCustomer.PopulateFromModel(CompanyContext, customer);
            sageCustomer.Save();

            return(sageCustomer.Key);
        }
Ejemplo n.º 2
0
        public static void PopulateFromModel(this Customer sageCustomer, Company companyContext, Models.Data.Customer customer)
        {
            if (customer == null)
            {
                return;
            }

            sageCustomer.ID                      = customer.ExternalId;
            sageCustomer.Name                    = customer.Name;
            sageCustomer.IsInactive              = customer.IsInactive;
            sageCustomer.IsProspect              = customer.IsProspect;
            sageCustomer.AccountNumber           = customer.AccountNumber;
            sageCustomer.OpenPurchaseOrderNumber = customer.OpenPurchaseOrderNumber;
            sageCustomer.PriceLevel              = customer.PriceLevel;
            sageCustomer.ReplaceInventoryItemIDWithPartNumber = customer.ReplaceInventoryItemIDWithPartNumber;
            sageCustomer.ReplaceInventoryItemIDWithUPC        = customer.ReplaceInventoryItemIDWithUPC;
            sageCustomer.ResaleNumber           = customer.ResaleNumber;
            sageCustomer.ShipVia                = customer.ShipVia;
            sageCustomer.WebSiteURL             = customer.WebSiteURL;
            sageCustomer.UseEmailToDeliverForms = customer.UseEmailToDeliverForms;
            sageCustomer.Email         = customer.Email;
            sageCustomer.Category      = customer.Category;
            sageCustomer.CreditStatus  = customer.CreditStatus.ToEnum <CustomerCreditStatus>();
            sageCustomer.CustomerSince = customer.CustomerSince;

            sageCustomer.ShipToContact.PopulateFromModel(customer.ShipToContact);
            sageCustomer.BillToContact.PopulateFromModel(customer.BillToContact);

            sageCustomer.PhoneNumbers.PopulateFromModel(customer.PhoneNumbers);

            sageCustomer.CashAccountReference       = sageCustomer.CashAccountReference.PopulateFromModel(customer.CashAccount, companyContext);
            sageCustomer.UsualSalesAccountReference = sageCustomer.UsualSalesAccountReference.PopulateFromModel(customer.UsualSalesAccount, companyContext);
        }
Ejemplo n.º 3
0
        private Customer FindSageCustomer(Models.Data.Customer customer)
        {
            var customerKey   = customer.GlobalKey(actionSource);
            var sageCustomers = CustomersList();

            // find mapping for customer globalKey
            var sageCustomerId = localDbApi.GetCustomerIdByKey(customerKey);

            if (sageCustomerId != null)
            {
                return(sageCustomers.SingleOrDefault(sageCustomerId));
            }

            // find by GlobalKey in Sage50
            var sageCustomer = sageCustomers.SingleOrDefault(customerKey);

            if (sageCustomer != null)
            {
                localDbApi.StoreCustomerId(customerKey, sageCustomer.ID);
                return(sageCustomer);
            }

            // if no mapping found and by extrnalId, find customer by email, phone or name and store mapping to localDb
            if (string.IsNullOrEmpty(customer.Email) && !customer.PhoneNumbers.IsPhonesAbsent())
            {
                // if email empty and phone present, load whole customers list and find first by phone
                sageCustomers.Load();
                sageCustomer = sageCustomers.FirstOrDefault(c => c.PhoneNumbers.ContainsOneOf(customer.PhoneNumbers));
            }
            if (sageCustomer == null)
            {
                FilterExpression expression;
                if (!string.IsNullOrEmpty(customer.Email))
                {
                    expression = FilterExpression.Equal(FilterExpression.Property("Customer.Email"),
                                                        FilterExpression.Constant(customer.Email));
                }
                else if (!string.IsNullOrEmpty(customer.Name))
                {
                    expression = FilterExpression.Equal(FilterExpression.Property("Customer.Name"),
                                                        FilterExpression.Constant(customer.Name));
                }
                else
                {
                    throw new AbortException("Can not search customer because name and email is null");
                }

                var modifier = LoadModifiers.Create();
                modifier.Filters = expression;
                sageCustomers.Load(modifier);

                if (sageCustomers.Count == 0)
                {
                    return(null);
                }

                if (sageCustomers.Count > 1)
                {
                    throw new AbortException(
                              $"Found more that one customer with name: '{customer.Name}' or phones or email: '{customer.Email}'");
                }

                sageCustomer = sageCustomers.First();
            }

            if (sageCustomer == null)
            {
                return(null);
            }

            sageCustomer = sageCustomers.First();
            localDbApi.StoreCustomerId(customerKey, sageCustomer.ID);
            return(sageCustomer);
        }