Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kasper Skov 66 posts 346 karma points
    Nov 12, 2013 @ 18:26
    Kasper Skov
    0

    Issue with price calculation

    I'm building a relatively advanced procedure which creates new orders from existing orders and finalizes them. I'm having issues figuring out how to use the API correctly. Basically the case is as following. I wan't to create one order for each customer. That order should contain all order lines with a specific property from all orders with a specific status on that customer. My method throws a null ref exception once I try to Save(). From debugging it, it looks like it has something to do with TotalPrices and/or TotalPrice(s) in PaymentInformation. TotalPrices.Count() = 0 and PaymentInformation.TotalPrice = null. I've tried using the Razor-API when adding order lines as I thought the price calculation might be automatic then. But it wasn't flexible enough for other things.

    Here is the code:

                var oService =  OrderService.Instance;
                var orderLines = new List<OrderLine>();
                var currentCulture = CultureInfo.CurrentCulture;
                foreach (var cusOrder in dc.TeaCommerce_Orders.Where(x => x.DateFinalized != null && x.OrderNumber != null && x.TransactionId != null && x.OrderStatusId == 7).GroupBy(x => x.CustomerId))
                {
                    //Create new order for customer (containing only subscriptions)
                    var member = Member.GetMemberFromEmail(cusOrder.First().Email);
                    var tcCusOrder = oService.Get(1, cusOrder.First().Id);
                    var newOrder = new Order(1);
    
                    //Set properties
                    #region orderProps
                    newOrder = tcCusOrder;
                    newOrder.Id = Guid.NewGuid();
                    newOrder.OrderStatusId = 1;
                    newOrder.CopiedFromOrderId = cusOrder.First().Id;
                    newOrder.CartNumber = "SUBCART-" + DateTime.Now.ToShortDateString() + "-" + member.Id;
                    newOrder.OrderNumber = "SUBORDER-" + DateTime.Now.ToShortDateString() + "-" + member.Id;
                    newOrder.DateCreated = DateTime.Now;
                    newOrder.DateFinalized = DateTime.Now;
                    newOrder.DateModified = DateTime.Now;
                    newOrder.OrderLines.Clear();
                    newOrder.LanguageId = tcCusOrder.LanguageId;
                    newOrder.PaymentInformation.TotalPrice = null;
                    newOrder.PaymentInformation.TotalPrices = null;
                    newOrder.SubtotalPrice = new Price(0, tcCusOrder.VatRate, new Currency(1, "DKK", "da-DK")); ;
                    newOrder.SubtotalPrices.Clear();
                    newOrder.TotalPrice = new Price(0, tcCusOrder.VatRate, new Currency(1, "DKK", "da-DK"));
                    newOrder.TotalPrices.Clear();
                    newOrder.OrderLines.Clear();
                    #endregion
    
                    foreach (var order in cusOrder)
                    {
                        //TC order instance
                        var tcOrderLines = oService.Get(1, order.Id).OrderLines;
                        foreach (OrderLine orderLine in tcOrderLines.Where(x => x.Quantity > 0))
                        {
                            //Create new orderline and product snapshot
                            var snapShot = new ProductSnapshot(1, orderLine.ProductIdentifier);
                            var newOrderLine = new OrderLine(snapShot);
                            //set properties
                            #region orderLineProps
                            newOrderLine.Properties = orderLine.Properties;
                            newOrderLine.Quantity = orderLine.Quantity;
                            newOrderLine.TotalPrice = orderLine.TotalPrice;
                            newOrderLine.TotalPrices = orderLine.TotalPrices;
                            newOrderLine.UnitPrice = orderLine.UnitPrice;
                            newOrderLine.UnitPrices = orderLine.UnitPrices;
                            newOrderLine.VatGroupId = orderLine.VatGroupId;
                            newOrderLine.VatRate = orderLine.VatRate;
                            newOrderLine.CopiedFromOrderLineId = orderLine.Id;
                            newOrderLine.LanguageId = orderLine.LanguageId;
                            newOrderLine.Name = orderLine.Name;
                            newOrderLine.OriginalUnitPrices = orderLine.OriginalUnitPrices;
                            #endregion
    
                            //Add price(s)
                            newOrder.OrderLines.Add(newOrderLine);
                            var price = new Price(newOrderLine.UnitPrice.Value, newOrderLine.VatRate, new Currency(1, "DKK", "da-DK"));
                            newOrder.TotalPrices.Add(price);
                        }
                        newOrder.Finalize(newOrder.TransactionInformation.AmountAuthorized.Value, tcCusOrder.TransactionInformation.TransactionId, new PaymentState(), tcCusOrder.PaymentInformation.PaymentMethodId.Value.ToString());
    
                       //Null ref exception
                       newOrder.Save();
    
    
                    }
                }
    

    Any ideas? First thoughts? I know the question is a bit vague and there's a lot of parameters and uncertainties that could cause this. But I'm hoping a quick glance from you could lead me on the right path. Thanks. And let me know if you want it in Danish.

  • Anders Burla 2560 posts 8256 karma points
    Nov 14, 2013 @ 10:31
    Anders Burla
    100

    Hi Kasper

    As you have stated - this is a lot of code to look at :)

    I don't know what might be the problem but what I would do is to comment out almost anything so you just do new Order(storeId) and then order.Save();

    When that works you can move on and add some of the functionality again. At some point you will know exactly what line of code that breaks and that might give a better way of debugging stuff. But generel you should not mess with the prices at all. :)

    Kind regards
    Anders

  • Kasper Skov 66 posts 346 karma points
    Nov 18, 2013 @ 12:16
    Kasper Skov
    0

    Erhm. I know exactly what line of code that breaks. As stated. newOrder.Save() throws a null ref exception. As stated, I think the problem is with the price calculation. TotalPrices prop on the order is empty. Even if I add some as in my code example. I'm not messing with the prices at all. I'm just adding snapshots/order lines to the order. But as you said, I should try and create the orders line by line. I will also try and combine the .NET and Razor API. Hopefully I can create a valid order that way.

  • Anders Burla 2560 posts 8256 karma points
    Nov 19, 2013 @ 10:50
    Anders Burla
    0

    I know that it is in the Save method of the order. But try and make you code more simple. So maybe just new Order() and then save it and see if that works. If that works you can try and add more code and see when the save method fails.

    Kind regards
    Anders

  • Kasper Skov 66 posts 346 karma points
    Nov 19, 2013 @ 10:54
    Kasper Skov
    0

    Yeah I figured out your point after making the answer :) Sorry. The problem was null values in the PaymentInformation props on the order. Thanks for your advice.

Please Sign in or register to post replies

Write your reply to:

Draft