Пример #1
0
        public static coreModel.QuoteRequest ToCoreModel(this dataModel.QuoteRequestEntity dbEntity)
        {
            if (dbEntity == null)
            {
                throw new ArgumentNullException("dbEntity");
            }

            var retVal = new coreModel.QuoteRequest();

            retVal.InjectFrom(dbEntity);
            if (dbEntity.ShipmentMethodCode != null)
            {
                retVal.ShipmentMethod = new Domain.Quote.Model.ShipmentMethod
                {
                    OptionName         = dbEntity.ShipmentMethodOption,
                    ShipmentMethodCode = dbEntity.ShipmentMethodCode
                };
            }
            retVal.Currency    = dbEntity.Currency;
            retVal.Addresses   = dbEntity.Addresses.Select(x => x.ToCoreModel()).ToList();
            retVal.Attachments = dbEntity.Attachments.Select(x => x.ToCoreModel()).ToList();
            retVal.Items       = dbEntity.Items.Select(x => x.ToCoreModel()).ToList();

            return(retVal);
        }
Пример #2
0
        public static dataModel.QuoteRequestEntity ToDataModel(this coreModel.QuoteRequest quoteRequest, PrimaryKeyResolvingMap pkMap)
        {
            if (quoteRequest == null)
            {
                throw new ArgumentNullException("quoteRequest");
            }

            var retVal = new dataModel.QuoteRequestEntity();

            pkMap.AddPair(quoteRequest, retVal);
            retVal.InjectFrom(quoteRequest);

            if (quoteRequest.ShipmentMethod != null)
            {
                retVal.ShipmentMethodCode   = quoteRequest.ShipmentMethod.ShipmentMethodCode;
                retVal.ShipmentMethodOption = quoteRequest.ShipmentMethod.OptionName;
            }
            retVal.Currency = quoteRequest.Currency.ToString();
            if (quoteRequest.Addresses != null)
            {
                retVal.Addresses = new ObservableCollection <dataModel.AddressEntity>(quoteRequest.Addresses.Select(x => x.ToDataModel()));
            }
            if (quoteRequest.Attachments != null)
            {
                retVal.Attachments = new ObservableCollection <dataModel.AttachmentEntity>(quoteRequest.Attachments.Select(x => x.ToDataModel()));
            }
            if (quoteRequest.Items != null)
            {
                retVal.Items = new ObservableCollection <dataModel.QuoteItemEntity>(quoteRequest.Items.Select(x => x.ToDataModel(pkMap)));
            }
            return(retVal);
        }
		public static coreModel.QuoteRequest ToCoreModel(this webModel.QuoteRequest quoteRequest)
		{
			var retVal = new coreModel.QuoteRequest();
			retVal.InjectFrom(quoteRequest);
            retVal.Currency = quoteRequest.Currency;

            if (quoteRequest.Addresses != null)
            {
                retVal.Addresses = quoteRequest.Addresses.Select(x => x.ToCoreModel()).ToList();
            }
            if (quoteRequest.Attachments != null)
            {
                retVal.Attachments = quoteRequest.Attachments.Select(x => x.ToCoreModel()).ToList();
            }
            if (quoteRequest.Items != null)
            {
                retVal.Items = quoteRequest.Items.Select(x => x.ToCoreModel()).ToList();
            }
            if (quoteRequest.DynamicProperties != null)
            {
                retVal.DynamicProperties = quoteRequest.DynamicProperties;
            }
            if (quoteRequest.Totals != null)
            {
                retVal.Totals = quoteRequest.Totals.ToCoreModel();
            }
            if(quoteRequest.ShipmentMethod != null)
            {
                retVal.ShipmentMethod = new coreModel.ShipmentMethod();
                retVal.ShipmentMethod.InjectFrom(quoteRequest.ShipmentMethod);
                retVal.ShipmentMethod.Currency = quoteRequest.Currency;
            }
            retVal.OperationsLog = quoteRequest.OperationsLog;
            return retVal;
		}
		public IEnumerable<QuoteRequest> SaveChanges(QuoteRequest[] quoteRequests)
		{
			if (quoteRequests == null)
			{
				throw new ArgumentNullException("quoteRequests");
			}
			//Need for primary key resolving
			var domainEntityMap = new List<KeyValuePair<QuoteRequest, QuoteRequestEntity>>();
			//Generate Number
			EnsureThatQuoteHasNumber(quoteRequests);

			using (var repository = _repositoryFactory())
			{
				var ids = quoteRequests.Where(x => x.Id != null).Select(x => x.Id).Distinct().ToArray();

				var origDbQuotes = repository.GetQuoteRequestByIds(ids);
				using (var changeTracker = GetChangeTracker(repository))
				{
					//Update
					foreach (var origDbQuote in origDbQuotes)
					{
						var changedQuote = quoteRequests.First(x => x.Id == origDbQuote.Id);
						// Do business logic on  quote request
						_eventPublisher.Publish(new QuoteRequestChangeEvent(EntryState.Modified, GetByIds(new[] { origDbQuote.Id }).First(), changedQuote));
                        domainEntityMap.Add(new KeyValuePair<QuoteRequest, QuoteRequestEntity>(changedQuote, origDbQuote));

                        var changedDbQuote = changedQuote.ToDataModel();
                        changeTracker.Attach(origDbQuote);
						changedDbQuote.Patch(origDbQuote);
					}

					//Create
					var newQuotes = quoteRequests.Where(x => !origDbQuotes.Any(y => y.Id == x.Id));
					foreach(var newQuote in newQuotes)
					{
                        // Do business logic on  quote request
                        _eventPublisher.Publish(new QuoteRequestChangeEvent(EntryState.Added, newQuote, newQuote));
						var newDbQuote = newQuote.ToDataModel();
						repository.Add(newDbQuote);
                        domainEntityMap.Add(new KeyValuePair<QuoteRequest, QuoteRequestEntity>(newQuote, newDbQuote));
                    }
                    repository.UnitOfWork.Commit();
				}

				//Save dynamic properties
				foreach (var pair in domainEntityMap)
				{
                    //Set key for all objects
                    pair.Key.SetObjectId(pair.Value.Id);
                    _dynamicPropertyService.SaveDynamicPropertyValues(pair.Key);
                }
                return domainEntityMap.Select(x => x.Key);
			}
		}
		public IEnumerable<QuoteRequest> SaveChanges(QuoteRequest[] quoteRequests)
		{
			if (quoteRequests == null)
			{
				throw new ArgumentNullException("quoteRequests");
			}
		
			//Generate Number
			EnsureThatQuoteHasNumber(quoteRequests);
            var pkMap = new PrimaryKeyResolvingMap();
            using (var repository = _repositoryFactory())
			{
				var ids = quoteRequests.Where(x => x.Id != null).Select(x => x.Id).Distinct().ToArray();

				var origDbQuotes = repository.GetQuoteRequestByIds(ids);
				using (var changeTracker = GetChangeTracker(repository))
				{
					//Update
					foreach (var origDbQuote in origDbQuotes)
					{
						var changedQuote = quoteRequests.First(x => x.Id == origDbQuote.Id);
						// Do business logic on  quote request
						_eventPublisher.Publish(new QuoteRequestChangeEvent(EntryState.Modified, GetByIds(new[] { origDbQuote.Id }).First(), changedQuote));
                     
                        var changedDbQuote = changedQuote.ToDataModel(pkMap);
                        changeTracker.Attach(origDbQuote);
						changedDbQuote.Patch(origDbQuote);
					}

					//Create
					var newQuotes = quoteRequests.Where(x => !origDbQuotes.Any(y => y.Id == x.Id));
					foreach(var newQuote in newQuotes)
					{
                        // Do business logic on  quote request
                        _eventPublisher.Publish(new QuoteRequestChangeEvent(EntryState.Added, newQuote, newQuote));
						var newDbQuote = newQuote.ToDataModel(pkMap);
						repository.Add(newDbQuote);
                    
                    }
                    repository.UnitOfWork.Commit();
                    //Copy generated id from dbEntities to model
                    pkMap.ResolvePrimaryKeys();
				}

				//Save dynamic properties
				foreach (var quoteRequest in quoteRequests)
				{
                    _dynamicPropertyService.SaveDynamicPropertyValues(quoteRequest);
                }
                return quoteRequests;
			}
		}
        public QuoteRequestTotals CalculateTotals(QuoteRequest quote)
        {
            var retVal = new QuoteRequestTotals();
            var cartFromQuote = quote.ToCartModel();
            var store = _storeService.GetById(quote.StoreId);

            if (store != null)
            {
                //Calculate shipment total
                //firts try to get manual amount
                retVal.ShippingTotal = quote.ManualShippingTotal;
                if (retVal.ShippingTotal == 0 && quote.ShipmentMethod != null)
                {
                    //calculate total by using shipment gateways
                    var evalContext = new ShippingEvaluationContext(cartFromQuote);

                    var rate = store.ShippingMethods.Where(x => x.IsActive && x.Code == quote.ShipmentMethod.ShipmentMethodCode)
                                                    .SelectMany(x => x.CalculateRates(evalContext))
                                                    .Where(x => quote.ShipmentMethod.OptionName != null ? quote.ShipmentMethod.OptionName == x.OptionName : true)
                                                    .FirstOrDefault();
                    retVal.ShippingTotal = rate != null ? rate.Rate : 0m;
                }
                //Calculate taxes
                var taxProvider = store.TaxProviders.Where(x => x.IsActive).OrderBy(x => x.Priority).FirstOrDefault();
                if (taxProvider != null)
                {
                    var taxRequest = quote.ToTaxRequest();
                    var taxEvalContext = new TaxEvaluationContext(taxRequest);
                    retVal.TaxTotal = taxProvider.CalculateRates(taxEvalContext).Select(x => x.Rate).DefaultIfEmpty(0).Sum(x => x);
                }
            }

            //Calculate subtotal
            var items = quote.Items.Where(x => x.SelectedTierPrice != null);
            if (quote.Items != null)
            {
                retVal.OriginalSubTotalExlTax = items.Sum(x => x.SalePrice * x.SelectedTierPrice.Quantity);
                retVal.SubTotalExlTax = items.Sum(x => x.SelectedTierPrice.Price * x.SelectedTierPrice.Quantity);
                if (quote.ManualSubTotal > 0)
                {
                    retVal.DiscountTotal = retVal.SubTotalExlTax - quote.ManualSubTotal;
                    retVal.SubTotalExlTax = quote.ManualSubTotal;
                }
                else if (quote.ManualRelDiscountAmount > 0)
                {
                    retVal.DiscountTotal = retVal.SubTotalExlTax * quote.ManualRelDiscountAmount * 0.01m;
                }
            }

            return retVal;
        }
		public static coreModel.QuoteRequest ToCoreModel(this dataModel.QuoteRequestEntity dbEntity)
		{
			if (dbEntity == null)
				throw new ArgumentNullException("dbEntity");

			var retVal = new coreModel.QuoteRequest();
			retVal.InjectFrom(dbEntity);
            if (dbEntity.ShipmentMethodCode != null)
            {
                retVal.ShipmentMethod = new Domain.Quote.Model.ShipmentMethod
                {
                    OptionName = dbEntity.ShipmentMethodOption,
                    ShipmentMethodCode = dbEntity.ShipmentMethodCode
                };
            }
            retVal.Currency = (CurrencyCodes)Enum.Parse(typeof(CurrencyCodes), dbEntity.Currency);
            retVal.Addresses = dbEntity.Addresses.Select(x => x.ToCoreModel()).ToList();
			retVal.Attachments = dbEntity.Attachments.Select(x => x.ToCoreModel()).ToList();
			retVal.Items = dbEntity.Items.Select(x => x.ToCoreModel()).ToList();
			
			return retVal;
		}
Пример #8
0
        public static cartCoreModel.ShoppingCart ToCartModel(this coreModel.QuoteRequest quoteRequest)
        {
            var retVal = new cartCoreModel.ShoppingCart();

            retVal.InjectFrom(quoteRequest);
            retVal.Currency = quoteRequest.Currency;
            if (quoteRequest.Items != null)
            {
                retVal.Items = quoteRequest.Items.Select(x => x.ToCartModel()).ToList();
            }

            if (quoteRequest.ShipmentMethod != null)
            {
                var shipment = new cartCoreModel.Shipment
                {
                    Currency             = quoteRequest.Currency,
                    ShipmentMethodCode   = quoteRequest.ShipmentMethod.ShipmentMethodCode,
                    ShipmentMethodOption = quoteRequest.ShipmentMethod.OptionName
                };
                retVal.Shipments = new List <cartCoreModel.Shipment>(new[] { shipment });
            }

            return(retVal);
        }
Пример #9
0
        public static taxCoreModel.TaxEvaluationContext ToTaxEvalContext(this coreModel.QuoteRequest quoteRequest)
        {
            var retVal = new taxCoreModel.TaxEvaluationContext();

            retVal.Id       = quoteRequest.Id;
            retVal.Code     = quoteRequest.Number;
            retVal.Currency = quoteRequest.Currency;
            retVal.Address  = quoteRequest.Addresses != null?quoteRequest.Addresses.FirstOrDefault() : null;

            retVal.Type = quoteRequest.GetType().Name;
            foreach (var quoteItem in quoteRequest.Items)
            {
                var line = new taxCoreModel.TaxLine
                {
                    Id      = quoteItem.Id,
                    Code    = quoteItem.Sku,
                    Name    = quoteItem.Name,
                    TaxType = quoteItem.TaxType,
                    Amount  = quoteItem.SelectedTierPrice.Price * quoteItem.SelectedTierPrice.Quantity
                };
                retVal.Lines.Add(line);
            }
            return(retVal);
        }
Пример #10
0
        public static coreModel.QuoteRequest ToCoreModel(this webModel.QuoteRequest quoteRequest)
        {
            var retVal = new coreModel.QuoteRequest();

            retVal.InjectFrom(quoteRequest);
            retVal.Currency = quoteRequest.Currency;

            if (quoteRequest.Addresses != null)
            {
                retVal.Addresses = quoteRequest.Addresses.Select(x => x.ToCoreModel()).ToList();
            }
            if (quoteRequest.Attachments != null)
            {
                retVal.Attachments = quoteRequest.Attachments.Select(x => x.ToCoreModel()).ToList();
            }
            if (quoteRequest.Items != null)
            {
                retVal.Items = quoteRequest.Items.Select(x => x.ToCoreModel()).ToList();
            }
            if (quoteRequest.DynamicProperties != null)
            {
                retVal.DynamicProperties = quoteRequest.DynamicProperties;
            }
            if (quoteRequest.Totals != null)
            {
                retVal.Totals = quoteRequest.Totals.ToCoreModel();
            }
            if (quoteRequest.ShipmentMethod != null)
            {
                retVal.ShipmentMethod = new coreModel.ShipmentMethod();
                retVal.ShipmentMethod.InjectFrom(quoteRequest.ShipmentMethod);
                retVal.ShipmentMethod.Currency = quoteRequest.Currency;
            }
            retVal.OperationsLog = quoteRequest.OperationsLog;
            return(retVal);
        }
		private void EnsureThatQuoteHasNumber(QuoteRequest[] quoteRequests)
		{
            var stores = _storeService.GetByIds(quoteRequests.Select(x => x.StoreId).Distinct().ToArray());
			foreach (var quoteRequest in quoteRequests)
			{
				if (string.IsNullOrEmpty(quoteRequest.Number))
				{
                    var store = stores.FirstOrDefault(x => x.Id == quoteRequest.StoreId);
                    var numberTemplate = "RFQ{0:yyMMdd}-{1:D5}";
                    if (store != null)
                    {
                        numberTemplate = store.Settings.GetSettingValue("Quotes.QuoteRequestNewNumberTemplate", numberTemplate);
                    }
                    quoteRequest.Number = _uniqueNumberGenerator.GenerateNumber(numberTemplate);
                }
            }
		}
		private void EnsureThatQuoteHasNumber(QuoteRequest[] quoteRequests)
		{
			foreach (var quoteRequest in quoteRequests)
			{
				if (string.IsNullOrEmpty(quoteRequest.Number))
				{
                    var numberTemplate = _settingManager.GetValue("Quotes.QuoteRequestNewNumberTemplate", "RFQ{0:yyMMdd}-{1:D5}");
                    quoteRequest.Number = _uniqueNumberGenerator.GenerateNumber(numberTemplate);
				}
			}
		}
		private void EnsureThatQuoteHasNumber(QuoteRequest[] quoteRequests)
		{
			foreach (var quoteRequest in quoteRequests)
			{
				if (String.IsNullOrEmpty(quoteRequest.Number))
				{
					quoteRequest.Number = _uniqueNumberGenerator.GenerateNumber("RFQ{0:yyMMdd}-{1:D5}");
				}
			}
		}
		public QuoteRequestChangeEvent(EntryState state, QuoteRequest origQuote, QuoteRequest modifiedQuote)
		{
			ChangeState = state;
			OrigQuote = origQuote;
			ModifiedQuote = modifiedQuote;
		}