public string ProcessOrders(NopCommerceDataReader dataReader, NopImportContext nopImportContext) { HashSet<OrderData> orders = dataReader.GetOrderData(); HashSet<OrderLineData> orderLines = dataReader.GetOrderLineData(); using (EventContext.Instance.Disable<GenerateGiftCards>()) { _session.Transact(session => { foreach (OrderData data in orders) { Guid guid = data.Guid; if (session.QueryOver<Order>().Where(o => o.Guid == guid).Any()) continue; var billingAddress = nopImportContext.FindNew<Address>(data.BillingAddressId); var shippingAddress = nopImportContext.FindNew<Address>(data.ShippingAddressId.GetValueOrDefault()); var order = new Order { BillingAddress = billingAddress.ToAddressData(), ShippingAddress = shippingAddress != null ? shippingAddress.ToAddressData() : null, CustomerIP = data.CustomerIp, DiscountAmount = data.OrderDiscount, Id = data.Id, OrderEmail = data.Email, PaidDate = data.PaidDate, PaymentStatus = GetPaymentStatus(data.PaymentStatus), ShippingStatus = GetShippingStatus(data.ShippingStatus), ShippingMethodName = data.ShippingMethodName, ShippingSubtotal = data.OrderShippingExclTax, ShippingTax = data.OrderShippingInclTax - data.OrderShippingExclTax, ShippingTotal = data.OrderShippingInclTax, Subtotal = data.OrderSubtotalInclTax, Tax = data.OrderTax, Total = data.OrderTotal, TotalPaid = data.OrderTotal, User = nopImportContext.FindNew<User>(data.CustomerId), SalesChannel = EcommerceApp.NopCommerceSalesChannel, PaymentMethod = data.PaymentMethod, OrderDate = data.OrderDate }; order.SetGuid(data.Guid); session.Save(order); if (order.OrderNotes == null) order.OrderNotes = new List<OrderNote>(); data.Notes.Add(new OrderNoteData { Note = "Previous order id: " + data.Id, ShowToCustomer = false }); foreach (OrderNoteData note in data.Notes) { var orderNote = new OrderNote { ShowToClient = note.ShowToCustomer, Note = note.Note, Order = order }; if (note.Date.HasValue) orderNote.CreatedOn = note.Date.Value; order.OrderNotes.Add(orderNote); session.Save(orderNote); } int orderId = data.Id; HashSet<OrderLineData> lineDatas = orderLines.FindAll(x => x.OrderId == orderId); foreach (OrderLineData lineData in lineDatas) { var orderLine = new OrderLine { Discount = lineData.DiscountAmountInclTax, Id = lineData.Id, Order = order, Quantity = lineData.Quantity, Name = lineData.ProductName, Price = lineData.PriceInclTax, UnitPrice = lineData.UnitPriceInclTax, UnitPricePreTax = lineData.DiscountAmountExclTax, PricePreTax = lineData.PriceExclTax, Tax = lineData.PriceInclTax - lineData.PriceExclTax, Weight = lineData.ItemWeight.GetValueOrDefault(), SKU = lineData.SKU, RequiresShipping = lineData.RequiresShipping }; order.OrderLines.Add(orderLine); session.Save(orderLine); } nopImportContext.AddEntry(data.Id, order); } }); } return string.Format("{0} orders processed", orders.Count); }
public Order PlaceOrder(CartModel cartModel, Action<Order> postCreationActions) { // this code is here to try and take into account a 3rd party posting back more than once for whatever reason (see bug #2754). // If the order with the guid has already been placed, we'll just return that placed order var existingOrders = _session.QueryOver<Order>().Where(order => order.Guid == cartModel.CartGuid).List(); if (existingOrders.Any()) { return existingOrders.First(); } var placedOrder = _session.Transact(session => { var order = new Order { ShippingAddress = cartModel.RequiresShipping ? cartModel.ShippingAddress.ToAddressData() : null, BillingAddress = cartModel.BillingAddress.ToAddressData(), ShippingMethodName = cartModel.RequiresShipping ? cartModel.ShippingMethod.Name : "No shipping required", Subtotal = cartModel.Subtotal, DiscountAmount = cartModel.OrderTotalDiscount, ShippingDiscountAmount = cartModel.ShippingDiscount, //DiscountCode = cartModel.DiscountCodes, RewardPointsAppliedAmount = cartModel.AppliedRewardPointsAmount, Tax = cartModel.Tax, Total = cartModel.Total, TotalPaid = cartModel.TotalToPay, ShippingSubtotal = cartModel.ShippingTotalPreDiscount, ShippingTotal = cartModel.ShippingTotal, ShippingTax = cartModel.ShippingTax, RequestedShippingDate = cartModel.RequestedShippingDate, User = cartModel.User, Weight = cartModel.Weight, OrderEmail = cartModel.OrderEmail, GiftMessage = cartModel.GiftMessage, CustomerIP = RequestHelper.GetIP(), HttpData = RequestHelper.GetRawHttpData(), PaymentMethod = cartModel.PaymentMethodSystemName, ShippingStatus = cartModel.RequiresShipping ? ShippingStatus.Pending : ShippingStatus.ShippingNotRequired, ShippingTaxPercentage = cartModel.ShippingTaxPercentage, SalesChannel = EcommerceApp.DefaultSalesChannel, OrderDate = CurrentRequestData.Now }; order.SetGuid(cartModel.CartGuid); session.Save(order); foreach (var discount in cartModel.Discounts) { var discountUsage = new DiscountUsage { Discount = discount.Discount, Order = order }; session.Save(discountUsage); order.DiscountUsages.Add(discountUsage); } foreach (var orderLine in cartModel.Items.Select(item => _orderLineCreator.GetOrderLine(item))) { orderLine.Order = order; session.Save(orderLine); order.OrderLines.Add(orderLine); } order = _giftCardApplier.Apply(cartModel.AppliedGiftCards, order); if (postCreationActions != null) postCreationActions(order); // Similarly, we check again just before we save - we should be fine as we are inside of a transaction // but we will err on the side of catching duplicates existingOrders = _session.QueryOver<Order>().Where(o => o.Guid == cartModel.CartGuid).List(); if (existingOrders.Any()) { return existingOrders.First(); } session.Update(order); return order; }); EventContext.Instance.Publish<IOnOrderPlaced, OrderPlacedArgs>(new OrderPlacedArgs { Order = placedOrder }); return placedOrder; }