/// <summary> /// Gets a single address that meets the supplied criteria. /// </summary> /// <param name="request">The data service request.</param> /// <returns>The data service response.</returns> private static SingleEntityDataServiceResponse <Address> GetAddress(GetAddressDataRequest request) { CustomerDataManager dataManager = GetDataManagerInstance(request.RequestContext); Address address = dataManager.GetAddress(request.RecordId, request.CustomerRecordId, request.ColumnSet); return(new SingleEntityDataServiceResponse <Address>(address)); }
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); }