public Order Build(Order order, bool useDefaultCurrency = false) { order.ShippingOption = _shippingOptionRepository.Return(order.ShippingOptionId); order.LineItemCollection = _lineItemRepository.Table.Where(l => l.OrderId == order.Id).ToList(); order.Country = _countryRepository.Return(order.CountryId); order.USState = _usStateRepository.Return(order.USStateId); order.ShippingUSState = _usStateRepository.Return(order.ShippingUSStateId); order.ShippingCountry = _countryRepository.Return(order.ShippingCountryId); order.OrderComments = _orderCommentRepository.Table.Where(c => c.OrderId == order.Id).OrderByDescending(c => c.DateStamp).ToList(); order.PharmOrder = GetPharmOrderByOrderId(order); order.GrandTotal = _orderCalculator.GetOrderTotal(order, useDefaultCurrency); order.Tax = _orderCalculator.GetVAT(order, useDefaultCurrency); var item = _orderStatusRepository.Table.Where(o => o.StatusCode == order.StatusCode).FirstOrDefault(); if (item != null) { order.OrderStatus = item.Status; } return(order); }
public OrderTotals CalculateOrderTotals(int profileId, bool autoRemovePhoneOrderItems = true, int testOfferRuleId = 0) { var orderTotals = new OrderTotals(); var gas = _genericAttributeService.GetAttributesForEntity(profileId, "Profile"); var gaCurrencyId = gas.FirstOrDefault(ga => ga.Key == SystemCustomerAttributeNames.CurrencyId); if (gaCurrencyId == null) { throw new ApolloException(string.Format("Currency is not selected. Profile ID={{{0}}}", profileId)); } var currencyId = Convert.ToInt32(gaCurrencyId.Value); var currency = _currencyRepository.Return(currencyId); orderTotals.CurrencyCode = currency.CurrencyCode; orderTotals.ExchangeRate = currency.ExchangeRate; var itemCount = GetTotalQuantityCartItemByProfileId(profileId, autoRemovePhoneOrderItems); if (itemCount <= 0) { return(orderTotals); } orderTotals.ItemCount = itemCount; //get country var gaCountryId = gas.FirstOrDefault(ga => ga.Key == SystemCustomerAttributeNames.CountryId); if (gaCountryId == null) { throw new ApolloException(string.Format("Country is not selected. Profile ID={{{0}}}", profileId)); } var countryId = Convert.ToInt32(gaCountryId.Value); var country = _countryRepository.Return(countryId); //subtotal var items = GetCartItemsByProfileId(profileId, autoRemovePhoneOrderItems); orderTotals.Subtotal = items .Where(x => x.CartItemMode != (int)CartItemMode.FreeItem) .Select(x => ((x.CartItemMode == (int)CartItemMode.InitialPrice) ? x.ProductPrice.PriceExclTax : x.ProductPrice.OfferPriceExclTax) * x.Quantity) .DefaultIfEmpty(0).Sum(); //discount var gaDiscountCouponCode = gas.FirstOrDefault(ga => ga.Key == SystemCustomerAttributeNames.DiscountCouponCode); var discountCouponCode = string.Empty; if (gaDiscountCouponCode != null) { discountCouponCode = gaDiscountCouponCode.Value; } var cartOffer = ProcessCartOfferByProfileId(profileId, country.ISO3166Code, testOfferRuleId); if (cartOffer.IsValid) { foreach (var discount in cartOffer.Discounts) { var offer = _offerService.GetOfferRuleById(discount.Key); if (offer != null) { orderTotals.Discounts.Add(offer.Alias, discount.Value); } } orderTotals.Discount = cartOffer.DiscountAmount; orderTotals.AwardedPoints = cartOffer.RewardPoint; } //shipping info var gaSelectedShippingOption = gas.FirstOrDefault(ga => ga.Key == SystemCustomerAttributeNames.SelectedShippingOption); if (gaSelectedShippingOption != null) { var selectedShippingOptionId = Convert.ToInt32(gaSelectedShippingOption.Value); var shippingOption = _shippingOptionRepository.Return(selectedShippingOptionId); orderTotals.ShippingCost = CalculateShippingCost(profileId, selectedShippingOptionId); orderTotals.ShippingMethod = shippingOption.Name; } //allocated points var gaAllocatedPoint = gas.FirstOrDefault(ga => ga.Key == SystemCustomerAttributeNames.AllocatedPoint); var allocatedPoints = 0; if (gaAllocatedPoint != null) { allocatedPoints = Convert.ToInt32(gaAllocatedPoint.Value); } orderTotals.AllocatedPoints = allocatedPoints; var allocatedPointsValue = allocatedPoints / 100M; //tax if (country.IsEC) { orderTotals.Tax = _orderCalculator.GetVAT(items); orderTotals.DisplayTax = country.IsEC; } //total var total = orderTotals.Subtotal + orderTotals.Tax - orderTotals.Discount - allocatedPointsValue + orderTotals.ShippingCost; orderTotals.Total = total; //earned points var earnedPoint = _orderCalculator.CalculateEarnedLoyaltyPointsFromCart( profileId, orderTotals.AllocatedPoints, currency.CurrencyCode, orderTotals.Discount); orderTotals.EarnedPoints = earnedPoint; return(orderTotals); }