/// <summary>
            /// Convert amount in one currency to another.
            /// </summary>
            /// <param name="fromCurrencyCode">From currency code.</param>
            /// <param name="toCurrencyCode">To currency code.</param>
            /// <param name="amountToConvert">Amount to convert.</param>
            /// <returns>Converted amount in new currency.</returns>
            public decimal ConvertCurrency(string fromCurrencyCode, string toCurrencyCode, decimal amountToConvert)
            {
                GetCurrencyValueServiceRequest  currencyRequest  = new GetCurrencyValueServiceRequest(fromCurrencyCode, toCurrencyCode, amountToConvert);
                GetCurrencyValueServiceResponse currencyResponse = this.context.Execute <GetCurrencyValueServiceResponse>(currencyRequest);

                return(currencyResponse.ConvertedAmount);
            }
Beispiel #2
0
            /// <summary>
            /// Converts amount from one currency to another.
            /// </summary>
            /// <param name="context">The context.</param>
            /// <param name="companyCurrency">The company currency.</param>
            /// <param name="storeCurrency">The store currency.</param>
            /// <param name="amount">The amount.</param>
            /// <returns>Returns the converted amount in the new currency.</returns>
            internal static decimal CurrencyToCurrency(RequestContext context, string companyCurrency, string storeCurrency, decimal amount)
            {
                var currencyRequest  = new GetCurrencyValueServiceRequest(companyCurrency, storeCurrency, amount);
                var currencyResponse = context.Execute <GetCurrencyValueServiceResponse>(currencyRequest);

                return(currencyResponse.ConvertedAmount);
            }
            /// <summary>
            /// Convert amount from one currency to another.
            /// </summary>
            /// <param name="amountInCurrency">Amount to convert.</param>
            /// <param name="fromCurrencyCode">Currency to convert from.</param>
            /// <param name="toCurrencyCode">Currency to convert to.</param>
            /// <param name="context">Request context.</param>
            /// <returns>Response that contains converted amount along with exchange rate.</returns>
            private static GetCurrencyValueServiceResponse ConvertCurrencyAmount(decimal amountInCurrency, string fromCurrencyCode, string toCurrencyCode, RequestContext context)
            {
                var request = new GetCurrencyValueServiceRequest(
                    fromCurrencyCode,
                    toCurrencyCode,
                    amountInCurrency);

                GetCurrencyValueServiceResponse response = context.Execute <GetCurrencyValueServiceResponse>(request);

                return(response);
            }
            /// <summary>
            /// Converts a value from one currency to another.
            /// </summary>
            /// <param name="request">Currency request specifying retail runtime, source and destination currencies, and amount to convert.</param>
            /// <returns>
            /// The value as it is after conversion in the destination currency, rounded according to the destination currency's rounding setup.
            /// </returns>
            private static GetCurrencyValueServiceResponse CurrencyToCurrency(GetCurrencyValueServiceRequest request)
            {
                GetCurrencyValueServiceResponse response = GetCurrencyConversion(request.RequestContext, request);

                if (response.ExchangeRate <= 0)
                {
                    throw new DataValidationException(
                              DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_CurrencyConversionFailed,
                              string.Format("Exchange rate from currency '{0}' to currency '{1}' is not supported or misconfigured. Calculated exchange rate: {2}", response.FromCurrencyCode, response.ToCurrencyCode, response.ExchangeRate));
                }

                return(response);
            }
            /// <summary>
            /// If any of the charge lines aren't expressed in the channel currency,
            ///     convert their calculated amount to the channel currency.
            /// </summary>
            /// <param name="context">The request context.</param>
            /// <param name="chargeLines">The charge lines which will have amounts calculated.</param>
            /// <param name="channelCurrency">The currency of the channel, compared against charge line to convert.</param>
            private static void ChargeAmountsToStoreCurrency(RequestContext context, IEnumerable <ChargeLine> chargeLines, string channelCurrency)
            {
                foreach (var chargeLine in chargeLines)
                {
                    // if charge and channel don't have same currency, convert to channel currency
                    if (!chargeLine.CurrencyCode.Equals(channelCurrency, StringComparison.OrdinalIgnoreCase))
                    {
                        var currencyRequest = new GetCurrencyValueServiceRequest(chargeLine.CurrencyCode, channelCurrency, chargeLine.CalculatedAmount);

                        var currencyResponse = context.Execute <GetCurrencyValueServiceResponse>(currencyRequest);
                        chargeLine.CalculatedAmount = currencyResponse.ConvertedAmount;
                    }
                }
            }
            /// <summary>
            /// Gets the amount in company currency and exchange rate between the foreign currency and company currency.
            /// </summary>
            /// <param name="context">The request context.</param>
            /// <param name="foreignCurrencyAmount">The foreign currency amount to be converted into company currency.</param>
            /// <param name="foreignCurrency">The foreign currency code.</param>
            /// <returns>The Item1 is the company amount and the Item2 is the exchange rate from foreign currency and company currency.</returns>
            private static Tuple <decimal, decimal> GetCompanyCurrencyValues(RequestContext context, decimal foreignCurrencyAmount, string foreignCurrency)
            {
                decimal companyAmount       = foreignCurrencyAmount;
                decimal companyExchangeRate = 1.00M;

                if (!foreignCurrency.Equals(context.GetChannelConfiguration().CompanyCurrency, StringComparison.OrdinalIgnoreCase))
                {
                    var currencyRequest  = new GetCurrencyValueServiceRequest(foreignCurrency, context.GetChannelConfiguration().CompanyCurrency, foreignCurrencyAmount);
                    var currencyResponse = context.Execute <GetCurrencyValueServiceResponse>(currencyRequest);
                    companyAmount       = currencyResponse.RoundedConvertedAmount;
                    companyExchangeRate = currencyResponse.ExchangeRate;   // exchange rate from foreign to company currency
                }

                return(new Tuple <decimal, decimal>(companyAmount, companyExchangeRate));
            }
            /// <summary>
            /// Calculate the total currency amount after converting to channel currency for the given list of currencies.
            /// </summary>
            /// <param name="request">The list of currencies that requires to be summed.</param>
            /// <returns>Returns the response that contains the sum of currencies amount.</returns>
            private static CalculateTotalAmountServiceResponse CalculateTotalCurrencyAmount(CalculateTotalAmountServiceRequest request)
            {
                var     channelConfiguration = request.RequestContext.GetChannelConfiguration();
                decimal totalAmount          = 0m;

                foreach (var currency in request.CurrenciesToConvert)
                {
                    var getCurrencyValueRequest = new GetCurrencyValueServiceRequest(currency.CurrencyCode, channelConfiguration.Currency, currency.AmountToConvert);
                    GetCurrencyValueServiceResponse response = request.RequestContext.Execute <GetCurrencyValueServiceResponse>(getCurrencyValueRequest);
                    totalAmount += response.RoundedConvertedAmount;
                }

                return(new CalculateTotalAmountServiceResponse(new CurrencyAmount {
                    RoundedConvertedAmount = totalAmount, CurrencyCode = channelConfiguration.Currency
                }));
            }
            /// <summary>
            /// Gets all supported channel currency value and exchange rate for the given amount.
            /// </summary>
            /// <param name="request">Request contains the amount to be converted.</param>
            /// <returns>The converted amount with exchange rates.</returns>
            private static GetChannelCurrencyServiceResponse GetSupportedChannelCurrencies(GetChannelCurrencyServiceRequest request)
            {
                string fromCurrencyCode = request.CurrencyCode;

                GetChannelCurrenciesDataRequest     dataRequest            = new GetChannelCurrenciesDataRequest(request.QueryResultSettings ?? QueryResultSettings.AllRecords);
                PagedResult <CurrencyAmount>        pagedChannelCurrencies = request.RequestContext.Execute <EntityDataServiceResponse <CurrencyAmount> >(dataRequest).PagedEntityCollection;
                ReadOnlyCollection <CurrencyAmount> channelCurrencies      = pagedChannelCurrencies.Results;

                if (channelCurrencies == null || !channelCurrencies.Any())
                {
                    NetTracer.Warning("Cannot find channel currencies");
                    return(new GetChannelCurrencyServiceResponse());
                }

                var currencyList = channelCurrencies.ToList();

                foreach (var toCurrency in currencyList)
                {
                    var getCurrencyValueRequest = new GetCurrencyValueServiceRequest(fromCurrencyCode, toCurrency.CurrencyCode, request.Amount);
                    GetCurrencyValueServiceResponse serviceResponse = GetCurrencyConversion(request.RequestContext, getCurrencyValueRequest);
                    toCurrency.ExchangeRate           = serviceResponse.ExchangeRate;
                    toCurrency.ConvertedAmount        = serviceResponse.ConvertedAmount;
                    toCurrency.RoundedConvertedAmount = serviceResponse.RoundedConvertedAmount;
                }

                var storeCurrencyList = currencyList.Where(currency => currency.ExchangeRate > 0M).ToList();

                // If the from currency does not exists add to the list.
                if (storeCurrencyList.All(currency => string.CompareOrdinal(currency.CurrencyCode, fromCurrencyCode) != 0))
                {
                    CurrencyAmount conversionResult = GetFromCurrencyAmount(request.RequestContext, request.Amount, fromCurrencyCode);
                    storeCurrencyList.Add(conversionResult);
                }

                pagedChannelCurrencies.Results = storeCurrencyList.AsReadOnly();

                return(new GetChannelCurrencyServiceResponse(pagedChannelCurrencies));
            }
Beispiel #9
0
            public static SalesOrder GetSalesOrderFromInfo(CustomerOrderInfo orderInfo, ChannelConfiguration channelConfiguration, RequestContext context)
            {
                // Stores the local copy. There is high probability of having the same shipping/delivery address on all the lines.
                Dictionary <long, Address> shippingAddressDictionary = new Dictionary <long, Address>();

                decimal shippingChargeAmount;

                ColumnSet columnSet  = new ColumnSet();
                var       salesOrder = new SalesOrder
                {
                    SalesId              = orderInfo.Id,
                    TransactionType      = SalesTransactionType.CustomerOrder,
                    CustomerOrderMode    = CustomerOrderMode.OrderRecalled,
                    CartType             = CartType.CustomerOrder,
                    CustomerOrderType    = orderInfo.OrderType,
                    StoreId              = orderInfo.StoreId,
                    IsTaxIncludedInPrice = Convert.ToBoolean(orderInfo.IsTaxIncludedInPrice)
                };

                switch (orderInfo.OrderType)
                {
                case CustomerOrderType.Quote:
                    salesOrder.Status = Utilities.GetSalesStatus((SalesQuotationStatus)orderInfo.Status);
                    break;

                case CustomerOrderType.SalesOrder:
                    salesOrder.Status = Utilities.GetSalesStatus((SalesOrderStatus)orderInfo.Status, (DocumentStatus)orderInfo.DocumentStatus);
                    break;

                default:
                    salesOrder.Status = SalesStatus.Unknown;
                    break;
                }

                DateTimeOffset currentChannelDate = context.GetNowInChannelTimeZone();

                salesOrder.RequestedDeliveryDate = Utilities.ParseDateStringAsDateTimeOffset(orderInfo.RequestedDeliveryDateString, currentChannelDate.Date, currentChannelDate.Offset);
                salesOrder.QuotationExpiryDate   = Utilities.ParseDateStringAsDateTimeOffset(orderInfo.ExpiryDateString, currentChannelDate.Date, currentChannelDate.Offset);

                // CreationDate is stored in UTC. It needs to be converted to local time zone where order is accessed.
                salesOrder.BeginDateTime = Utilities.ParseDateString(orderInfo.CreationDateString, currentChannelDate.ToUniversalTime().DateTime, DateTimeStyles.AssumeUniversal);

                salesOrder.Comment = orderInfo.Comment;

                // Header delivery
                salesOrder.InventoryLocationId = orderInfo.WarehouseId;
                salesOrder.DeliveryMode        = orderInfo.DeliveryMode;

                foreach (var discountCode in orderInfo.DiscountCodes)
                {
                    salesOrder.DiscountCodes.Add(discountCode);
                }

                // Customer info
                salesOrder.CustomerId = orderInfo.CustomerAccount;
                long addressRecordIdLong = 0;

                if (long.TryParse(orderInfo.AddressRecordId, out addressRecordIdLong))
                {
                    var dataServiceRequest = new GetAddressDataRequest(addressRecordIdLong, columnSet);
                    SingleEntityDataServiceResponse <Address> dataServiceResponse = context.Execute <SingleEntityDataServiceResponse <Address> >(dataServiceRequest);

                    if (dataServiceResponse.Entity == null)
                    {
                        Utilities.DownloadCustomerData(context, salesOrder.CustomerId);
                        dataServiceResponse = context.Execute <SingleEntityDataServiceResponse <Address> >(dataServiceRequest);
                    }

                    if (dataServiceResponse.Entity != null)
                    {
                        salesOrder.ShippingAddress = dataServiceResponse.Entity;
                        shippingAddressDictionary.Add(salesOrder.ShippingAddress.RecordId, salesOrder.ShippingAddress);
                    }
                }

                if (!string.IsNullOrEmpty(orderInfo.SalespersonStaffId))
                {
                    // Sets the sales person id and name according to AX values
                    // This is done because we do not know whether the sales person information is available on this store
                    salesOrder.StaffId = orderInfo.SalespersonStaffId;
                }

                salesOrder.ChannelReferenceId = orderInfo.ChannelReferenceId;
                salesOrder.LoyaltyCardId      = orderInfo.LoyaltyCardId;

                salesOrder.ReceiptEmail = orderInfo.Email;

                string shippingChargeCode = channelConfiguration.ShippingChargeCode;

                // Items
                int lineId = 0;

                foreach (ItemInfo item in orderInfo.Items)
                {
                    lineId++;
                    var lineItem = new SalesLine
                    {
                        LineId                  = lineId.ToString(CultureInfo.InvariantCulture),
                        Found                   = true,
                        RecordId                = item.RecId,
                        ItemId                  = item.ItemId,
                        Comment                 = item.Comment,
                        Quantity                = item.Quantity,
                        ReturnQuantity          = item.Quantity,
                        SalesOrderUnitOfMeasure = item.Unit,
                        UnitOfMeasureSymbol     = item.Unit,
                        Price                   = item.Price,
                        NetAmount               = item.NetAmount,
                        QuantityOrdered         = item.Quantity,
                        QuantityInvoiced        = item.QuantityPicked,
                        DeliveryMode            = item.DeliveryMode,
                        RequestedDeliveryDate   = Utilities.ParseDateStringAsDateTimeOffset(item.RequestedDeliveryDateString, currentChannelDate.Date, currentChannelDate.Offset),
                        FulfillmentStoreId      = item.FulfillmentStoreId,
                        InventoryLocationId     = item.WarehouseId,
                        SerialNumber            = item.SerialId,
                        BatchId                 = item.BatchId,
                        Status                  = TransactionStatus.Normal,
                        SalesStatus             = Utilities.GetSalesStatus((SalesOrderStatus)item.Status)
                    };

                    // Copy charges to line and calculates total shipping charge amount
                    lineItem.ChargeLines.AddRange(SalesOrderHelper.CreateChargeLines(item.Charges, shippingChargeCode, salesOrder.BeginDateTime, out shippingChargeAmount));
                    lineItem.DeliveryModeChargeAmount = shippingChargeAmount;

                    // Line level discount amounts
                    lineItem.LineDiscount                 = item.LineDscAmount;
                    lineItem.PeriodicDiscount             = item.PeriodicDiscount;
                    lineItem.PeriodicPercentageDiscount   = item.PeriodicPercentageDiscount;
                    lineItem.LineManualDiscountAmount     = item.LineManualDiscountAmount;
                    lineItem.LineManualDiscountPercentage = item.LineManualDiscountPercentage;
                    lineItem.TotalDiscount                = item.TotalDiscount;
                    lineItem.TotalPercentageDiscount      = item.TotalPctDiscount;

                    // Copy discounts to line
                    lineItem.DiscountLines.AddRange(SalesOrderHelper.CreateDiscountLines(item.Discounts));

                    long itemAddressRecordIdLong;
                    if (long.TryParse(item.AddressRecordId, out itemAddressRecordIdLong))
                    {
                        Address lineLevelshippingAddress = new Address();

                        if (!shippingAddressDictionary.TryGetValue(itemAddressRecordIdLong, out lineLevelshippingAddress))
                        {
                            var dataServiceRequest = new GetAddressDataRequest(itemAddressRecordIdLong, columnSet);
                            SingleEntityDataServiceResponse <Address> dataServiceResponse = context.Execute <SingleEntityDataServiceResponse <Address> >(dataServiceRequest);

                            // If address not found download and get.
                            if (dataServiceResponse.Entity == null)
                            {
                                Utilities.DownloadCustomerData(context, salesOrder.CustomerId);
                                dataServiceResponse = context.Execute <SingleEntityDataServiceResponse <Address> >(dataServiceRequest);
                            }

                            if (dataServiceResponse.Entity != null)
                            {
                                lineItem.ShippingAddress = dataServiceResponse.Entity;
                                shippingAddressDictionary.Add(lineItem.ShippingAddress.RecordId, lineItem.ShippingAddress);
                            }
                        }
                        else
                        {
                            lineItem.ShippingAddress = lineLevelshippingAddress;
                        }
                    }

                    Utilities.SetUpVariantAndProduct(context, item.InventDimensionId, lineItem.ItemId, lineItem);

                    lineItem.DiscountAmount = item.Discount;

                    // Set tax info after defaults, as it may have been overridden.
                    lineItem.SalesTaxGroupId = item.SalesTaxGroup ?? string.Empty;
                    lineItem.ItemTaxGroupId  = item.ItemTaxGroup ?? string.Empty;

                    // Add it to the transaction
                    salesOrder.SalesLines.Add(lineItem);
                }

                // Charges for the header
                salesOrder.ChargeLines.AddRange(SalesOrderHelper.CreateChargeLines(orderInfo.Charges, shippingChargeCode, salesOrder.BeginDateTime, out shippingChargeAmount));
                salesOrder.DeliveryModeChargeAmount = shippingChargeAmount;

                // Payments
                // - total up amounts
                // - add history entries
                decimal nonPrepayments       = decimal.Zero;
                decimal prepaymentAmountPaid = decimal.Zero;

                int tenderLineId = 0;

                foreach (PaymentInfo payment in orderInfo.Payments)
                {
                    if (salesOrder.TenderLines == null)
                    {
                        salesOrder.TenderLines = new Collection <TenderLine>();
                    }

                    decimal amount = 0M;
                    if (string.IsNullOrWhiteSpace(payment.Currency) || payment.Currency.Equals(channelConfiguration.Currency, StringComparison.OrdinalIgnoreCase))
                    {
                        amount = payment.Amount;
                    }
                    else
                    {
                        GetCurrencyValueServiceRequest  currencyValueRequest  = new GetCurrencyValueServiceRequest(payment.Currency, channelConfiguration.Currency, payment.Amount);
                        GetCurrencyValueServiceResponse currencyValueResponse = context.Execute <GetCurrencyValueServiceResponse>(currencyValueRequest);
                        amount = currencyValueResponse.RoundedConvertedAmount;
                    }

                    if (payment.Prepayment)
                    {
                        // Sum prepayments to track total deposits paid
                        prepaymentAmountPaid += amount;
                    }
                    else
                    {
                        // Sum non-prepayments as base for calculating deposits applied to pickups
                        nonPrepayments += amount;
                    }

                    tenderLineId++;
                    var tenderLine = new TenderLine
                    {
                        TenderLineId = tenderLineId.ToString(CultureInfo.InvariantCulture),
                        Amount       = payment.Amount,
                        Currency     = payment.Currency,
                        CardTypeId   = string.Empty,
                        Status       = TenderLineStatus.Historical,
                        IsVoidable   = false,
                        TenderDate   =
                            Utilities.ParseDateString(
                                payment.DateString,
                                currentChannelDate.Date,
                                DateTimeStyles.None)         // On channel timezone
                    };

                    salesOrder.TenderLines.Add(tenderLine);
                }

                if (orderInfo.Affiliations != null && orderInfo.Affiliations.Any())
                {
                    salesOrder.AffiliationLoyaltyTierLines.Clear();
                    salesOrder.AffiliationLoyaltyTierLines.AddRange(orderInfo.Affiliations.Select(line =>
                                                                                                  new SalesAffiliationLoyaltyTier
                    {
                        AffiliationId   = line.AffiliationRecordId,
                        LoyaltyTierId   = line.LoyaltyTierRecordId,
                        AffiliationType = line.AffiliationType
                    }));
                }

                // Prepayment/Deposit override info
                if (orderInfo.PrepaymentAmountOverridden)
                {
                    salesOrder.OverriddenDepositAmount = prepaymentAmountPaid;
                }

                salesOrder.PrepaymentAmountPaid = prepaymentAmountPaid;

                // Portion of the prepayment that has been applied to invoices
                // (total amount invoiced less payments, difference is the deposit applied)
                salesOrder.PrepaymentAmountInvoiced = orderInfo.PreviouslyInvoicedAmount - nonPrepayments;

                // if the prepayment invoiced is greater than the total paid as deposit, there is no credit left
                salesOrder.AvailableDepositAmount = Math.Max(decimal.Zero, salesOrder.PrepaymentAmountPaid - salesOrder.PrepaymentAmountInvoiced);

                salesOrder.HasLoyaltyPayment = orderInfo.HasLoyaltyPayment;
                salesOrder.CurrencyCode      = orderInfo.CurrencyCode;

                return(salesOrder);
            }
            /// <summary>
            /// Converts a value from one currency to another.
            /// </summary>
            /// <param name="context">The request context.</param>
            /// <param name="request">Currency request specifying retail runtime, source and destination currencies, and amount to convert.</param>
            /// <returns>
            /// The value as it is after conversion in the destination currency, rounded according to the destination currency's rounding setup.
            /// </returns>
            private static GetCurrencyValueServiceResponse GetCurrencyConversion(RequestContext context, GetCurrencyValueServiceRequest request)
            {
                string  fromCurrencyCode = request.FromCurrencyCode;
                string  toCurrencyCode   = request.ToCurrencyCode;
                decimal amountToConvert  = request.AmountToConvert;

                // If from and to currency is the same one return the original value
                if (fromCurrencyCode.Trim() == toCurrencyCode.Trim())
                {
                    decimal roundedAmount = RoundAmount(context, amountToConvert, toCurrencyCode);
                    return(new GetCurrencyValueServiceResponse(fromCurrencyCode, toCurrencyCode, amountToConvert, amountToConvert, roundedAmount));
                }

                // If the value to be converted is 0 then just return 0
                if (amountToConvert == 0M)
                {
                    return(new GetCurrencyValueServiceResponse(fromCurrencyCode, toCurrencyCode, amountToConvert, amountToConvert, amountToConvert));
                }

                decimal exchangeRate           = CalculateExchangeRate(context, request.FromCurrencyCode, request.ToCurrencyCode);
                decimal convertedAmount        = 0m;
                decimal roundedConvertedAmount = 0m;

                if (exchangeRate > 0)
                {
                    convertedAmount        = amountToConvert * exchangeRate;
                    roundedConvertedAmount = RoundAmount(context, convertedAmount, toCurrencyCode);
                }

                return(new GetCurrencyValueServiceResponse(fromCurrencyCode, toCurrencyCode, amountToConvert, convertedAmount, roundedConvertedAmount, exchangeRate));
            }