public bool AddCustomerFields(OrderInfo order, Dictionary<string, string> fields, CustomerDatatypes customerDataType, bool ingnoreNotAllowed = false)
		{
			if (ChangeOrderToIncompleteAndReturnTrueIfNotAllowed(order) && ingnoreNotAllowed == false)
			{
				return false;
			}

			//var xDoc = OrderUpdatingService.CreateXDocumentBasedOnCMSDocumentType(fields, documentAlias);

			var xDoc = new XDocument(new XElement(customerDataType.ToString()));

			if (customerDataType == CustomerDatatypes.Customer)
			{
				if (order.CustomerInfo.customerInformation != null) xDoc = order.CustomerInfo.customerInformation;
			}

			if (customerDataType == CustomerDatatypes.Shipping)
			{
				if (order.CustomerInfo.shippingInformation != null) xDoc = order.CustomerInfo.shippingInformation;
			}

			if (customerDataType == CustomerDatatypes.Extra)
			{
				if (order.CustomerInfo.extraInformation != null) xDoc = order.CustomerInfo.extraInformation;
			}

			foreach (var field in fields.Where(field => xDoc.Root != null && xDoc.Root.Descendants(field.Key).Any()))
			{
				if (xDoc.Root != null) xDoc.Root.Descendants(field.Key).Remove();
			}

			AddFieldsToXDocumentBasedOnCMSDocumentType(xDoc, fields, Order.NodeAlias);

			var documentType = IO.Container.Resolve<ICMSDocumentTypeService>().GetByAlias(Order.NodeAlias);

			// special fields that also work when field is not created on order document type
			foreach (var field in fields)
			{
				if (field.Key.ToLower() == "customercountry")
				{
					if (field.Value != "0")
					{
						order.CustomerInfo.CountryCode = field.Value;

						if (field.Value.Length > 4)
						{
							Log.Instance.LogDebug(
								string.Format("customerCountry Length == {0} Value: {1}, Possible added Country.Name instead of Country.Code?",
									field.Key.Length, field.Value));
						}
					}
				}

				if (field.Key.ToLower() == "shippingcountry")
				{
					if (field.Value != "0")
					{
						order.CustomerInfo.ShippingCountryCode = field.Value;

						if (field.Value.Length > 4)
						{
							Log.Instance.LogDebug(
								string.Format("shippingcountry Length == {0} Value: {1}, Possible added Country.Name instead of Country.Code?",
									field.Key.Length, field.Value));
						}
					}
				}

				if (field.Key.ToLower() == "customervatnumber")
				{
					order.SetVATNumber(field.Value);
				}

				if (field.Key.ToLower() == "customerregion")
				{
					order.CustomerInfo.RegionCode = field.Value;
					order._regionalVatInCents = null;

					if (field.Value.Length > 4)
					{
						Log.Instance.LogDebug(string.Format("customerregion Length == {0} Value: {1}, Possible added Country.Name instead of Country.Code?", field.Key.Length, field.Value));
					}
				}

				if (field.Key.ToLower() == "acceptsmarketing" || field.Key.ToLower() == "customeracceptsmarketing")
				{
					if (field.Value == "1" || field.Value == "true" || field.Value == "on" || field.Value == "acceptsmarketing" || field.Value == "customeracceptsmarketing")
					{
						order.CustomerInfo.AcceptsMarketing = true;
					}
					else
					{
						order.CustomerInfo.AcceptsMarketing = false;
					}
				}

				// 'hack' because if you an empty checkbox is not send to the browser, by supporting this option the developer can add a hidden input 'false' field and make it checked using javascript.
				if (field.Key.ToLower() == "acceptsmarketingfalse" || field.Key.ToLower() == "customeracceptsmarketingfalse")
				{
					if (field.Value == "1" || field.Value == "true" || field.Value == "on" || field.Value == "acceptsmarketingfalse" || field.Value == "customeracceptsmarketingfalse")
					{
						order.CustomerInfo.AcceptsMarketing = false;
					}
				}
			}

			if (customerDataType == CustomerDatatypes.Customer)
			{
				order.CustomerInfo.customerInformation = xDoc;
				order.CustomerValidationFailed = _orderService.ValidateCustomer(order).Any();
				return true;
			}

			if (customerDataType == CustomerDatatypes.Shipping)
			{
				order.CustomerInfo.shippingInformation = xDoc;
				order.CustomerValidationFailed = _orderService.ValidateCustomer(order).Any();
				return true;
			}

			if (customerDataType == CustomerDatatypes.Extra)
			{
				order.CustomerInfo.extraInformation = xDoc;
				order.CustomerValidationFailed = _orderService.ValidateCustomer(order).Any();
				return true;
			}

			return false;
		}
Ejemplo n.º 2
0
        public bool AddCustomerFields(OrderInfo order, Dictionary <string, string> fields, CustomerDatatypes customerDataType, bool ingnoreNotAllowed = false)
        {
            if (ChangeOrderToIncompleteAndReturnTrueIfNotAllowed(order) && ingnoreNotAllowed == false)
            {
                return(false);
            }

            //var xDoc = OrderUpdatingService.CreateXDocumentBasedOnCMSDocumentType(fields, documentAlias);

            var xDoc = new XDocument(new XElement(customerDataType.ToString()));

            if (customerDataType == CustomerDatatypes.Customer)
            {
                if (order.CustomerInfo.customerInformation != null)
                {
                    xDoc = order.CustomerInfo.customerInformation;
                }
            }

            if (customerDataType == CustomerDatatypes.Shipping)
            {
                if (order.CustomerInfo.shippingInformation != null)
                {
                    xDoc = order.CustomerInfo.shippingInformation;
                }
            }

            if (customerDataType == CustomerDatatypes.Extra)
            {
                if (order.CustomerInfo.extraInformation != null)
                {
                    xDoc = order.CustomerInfo.extraInformation;
                }
            }

            foreach (var field in fields.Where(field => xDoc.Root != null && xDoc.Root.Descendants(field.Key).Any()))
            {
                if (xDoc.Root != null)
                {
                    xDoc.Root.Descendants(field.Key).Remove();
                }
            }

            AddFieldsToXDocumentBasedOnCMSDocumentType(xDoc, fields, Order.NodeAlias);

            var documentType = IO.Container.Resolve <ICMSDocumentTypeService>().GetByAlias(Order.NodeAlias);

            // special fields that also work when field is not created on order document type
            foreach (var field in fields)
            {
                if (field.Key.ToLower() == "customercountry")
                {
                    if (field.Value != "0")
                    {
                        order.CustomerInfo.CountryCode = field.Value;

                        if (field.Value.Length > 4)
                        {
                            Log.Instance.LogDebug(
                                string.Format("customerCountry Length == {0} Value: {1}, Possible added Country.Name instead of Country.Code?",
                                              field.Key.Length, field.Value));
                        }
                    }
                }

                if (field.Key.ToLower() == "shippingcountry")
                {
                    if (field.Value != "0")
                    {
                        order.CustomerInfo.ShippingCountryCode = field.Value;

                        if (field.Value.Length > 4)
                        {
                            Log.Instance.LogDebug(
                                string.Format("shippingcountry Length == {0} Value: {1}, Possible added Country.Name instead of Country.Code?",
                                              field.Key.Length, field.Value));
                        }
                    }
                }

                if (field.Key.ToLower() == "customervatnumber")
                {
                    order.SetVATNumber(field.Value);
                }

                if (field.Key.ToLower() == "customerregion")
                {
                    order.CustomerInfo.RegionCode = field.Value;
                    order._regionalVatInCents     = null;

                    if (field.Value.Length > 4)
                    {
                        Log.Instance.LogDebug(string.Format("customerregion Length == {0} Value: {1}, Possible added Country.Name instead of Country.Code?", field.Key.Length, field.Value));
                    }
                }

                if (field.Key.ToLower() == "acceptsmarketing" || field.Key.ToLower() == "customeracceptsmarketing")
                {
                    if (field.Value == "1" || field.Value == "true" || field.Value == "on" || field.Value == "acceptsmarketing" || field.Value == "customeracceptsmarketing")
                    {
                        order.CustomerInfo.AcceptsMarketing = true;
                    }
                    else
                    {
                        order.CustomerInfo.AcceptsMarketing = false;
                    }
                }

                // 'hack' because if you an empty checkbox is not send to the browser, by supporting this option the developer can add a hidden input 'false' field and make it checked using javascript.
                if (field.Key.ToLower() == "acceptsmarketingfalse" || field.Key.ToLower() == "customeracceptsmarketingfalse")
                {
                    if (field.Value == "1" || field.Value == "true" || field.Value == "on" || field.Value == "acceptsmarketingfalse" || field.Value == "customeracceptsmarketingfalse")
                    {
                        order.CustomerInfo.AcceptsMarketing = false;
                    }
                }
            }

            if (customerDataType == CustomerDatatypes.Customer)
            {
                order.CustomerInfo.customerInformation = xDoc;
                order.CustomerValidationFailed         = _orderService.ValidateCustomer(order).Any();
                return(true);
            }

            if (customerDataType == CustomerDatatypes.Shipping)
            {
                order.CustomerInfo.shippingInformation = xDoc;
                order.CustomerValidationFailed         = _orderService.ValidateCustomer(order).Any();
                return(true);
            }

            if (customerDataType == CustomerDatatypes.Extra)
            {
                order.CustomerInfo.extraInformation = xDoc;
                order.CustomerValidationFailed      = _orderService.ValidateCustomer(order).Any();
                return(true);
            }

            return(false);
        }
		private HandleObject AddCustomerInformation(NameValueCollection requestParameters, IEnumerable<string> queryStringCollection, CustomerDatatypes customerDataType, Uri requestUri)
		{
			var handleObject = new HandleObject {Action = "AddCustomerInformation", Url = requestUri};
			var result = new Dictionary<string, string>();

			var order = GetCreateOrderOrWishList(requestParameters);

			var stringCollection = queryStringCollection as string[] ?? queryStringCollection.ToArray();
			var fields = stringCollection.ToDictionary(s => s, s => requestParameters[s]);

			if (!fields.Any())
			{
				SetBasket(handleObject, order);
				handleObject.Success = false;
				handleObject.Validated = false;
				result.Add(Constants.CustomerInformation, CustomerInformationResult.Failed.ToString());
				Session.Add(Constants.CustomerInformation, CustomerInformationResult.Failed.ToString());
				handleObject.Messages = result;
				return handleObject;
			}
			IO.Container.Resolve<IOrderUpdatingService>().AddCustomerFields(order, fields, customerDataType);

			var customerIsShippingKey = requestParameters.AllKeys.FirstOrDefault(x => x.ToLower() == "customerisshipping"); //requestParameters["customerIsShipping"];

			if (customerIsShippingKey != null)
			{
				var customerIsShippingValue = requestParameters[customerIsShippingKey];

				if (customerIsShippingValue != null && (customerIsShippingValue.ToLower() == "customerisshipping" || customerIsShippingValue.ToLower() == "true" || customerIsShippingValue.ToLower() == "on" || customerIsShippingValue == "1"))
				{
					var shippingFields = stringCollection.ToDictionary(s => s.Replace("customer", "shipping"), s => requestParameters[s]);

					if (!shippingFields.Any()) return handleObject;
					order.AddCustomerFields(shippingFields, CustomerDatatypes.Shipping);
				}
			}

			order.Save();

			SetBasket(handleObject, order);
			handleObject.Success = true;
			handleObject.Validated = true;
			result.Add(Constants.CustomerInformation, CustomerInformationResult.Success.ToString());
			Session.Add(Constants.CustomerInformation, CustomerInformationResult.Success.ToString());
			handleObject.Messages = result;
			return handleObject;
		}