Order Shipment Status Displayed

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
vashts1980
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 98
Joined: Fri Apr 29, 2011 2:56 pm

Order Shipment Status Displayed

Post by vashts1980 » Tue May 22, 2012 11:34 am

I had recently had some success in being able to set the shipment status of orders to shipped from custom code. However, I have discovered a new issue. WHen orders are shipped in the manner, and someone goes to review orders in the Order Manager", they still display as "unshipped" until someone actually opens the order to view the details. Is there something that can be done to update the views on the main listing?

User avatar
david-ebt
Captain (CAPT)
Captain (CAPT)
Posts: 253
Joined: Fri Dec 31, 2010 10:12 am

Re: Order Shipment Status Displayed

Post by david-ebt » Tue May 22, 2012 1:36 pm

The main Order Manager display uses the ac_Orders.ShipmentStatusId - it doesn't look at each shipment in an order. Options for this field are:

Unspecified = 0
Unshipped = 1
Shipped = 2
NonShippable = 3

If you have multiple shipments on an order and if your custom code is marking each ac_OrderShipments record as shipped, then you may have to manually set the ShipmentStatusId field in the order to "2" and then save the order. I've found that the Able code that processes the order after you mark each shipment as shipped can sometimes miss that all shipments were shipped and so the order isn't marked as shipped. In my case I think it is because the updates to the ac_TrackingNumbers and ac_OrderShipments records are automatically done in a single transaction by the database context. This means the Able code that would normally update the ac_Orders.ShipmentStatusId doesn't see all the shipments as shipped. But when you later go into the order it does see all of the shipments as shipped and will update the order shipped flag.
David
http://www.ecombuildertoday.com
Enhanced Reporting for AbleCommerce
Image

vashts1980
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 98
Joined: Fri Apr 29, 2011 2:56 pm

Re: Order Shipment Status Displayed

Post by vashts1980 » Tue May 22, 2012 1:41 pm

Ah. Ok, that makes perfect sense. I should be able to remedy the situation with just a couple lines of code, then. Thank you.

vashts1980
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 98
Joined: Fri Apr 29, 2011 2:56 pm

Re: Order Shipment Status Displayed

Post by vashts1980 » Wed May 23, 2012 5:00 pm

Ok, I have the following code...

Code: Select all

NameValueCollection nvc = request.Form;
String shipDate = nvc["shipdate"];

// other code

OrderShipment shipment = OrderShipmentDataSource.Load(shippedOrder.Shipments[0].OrderShipmentId);

// tracking numbers added to shipment here

if (shipDate != null && shipDate != "")
{
    shipment.Ship(DateTime.Parse(shipDate));
}
else
    shipment.Ship();

shippedOrder.ShipmentStatus = OrderShipmentStatus.Shipped;
shippedOrder.ShipmentStatusId = 2;

shippedOrder.Save();
But they still show "Unshipped" until after the details have been viewed.

User avatar
david-ebt
Captain (CAPT)
Captain (CAPT)
Posts: 253
Joined: Fri Dec 31, 2010 10:12 am

Re: Order Shipment Status Displayed

Post by david-ebt » Thu May 24, 2012 8:58 am

Have you tried setting the shipment status for the order itself?

Code: Select all

_Order.ShipmentStatus = OrderShipmentStatus.Shipped;
_Order.Save();
David
http://www.ecombuildertoday.com
Enhanced Reporting for AbleCommerce
Image

vashts1980
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 98
Joined: Fri Apr 29, 2011 2:56 pm

Re: Order Shipment Status Displayed

Post by vashts1980 » Thu May 24, 2012 9:09 am

That's what I'm already doing. I have this code in here to load the desired order...

Code: Select all

String orderNum = nvc["ordernum"];
orderNumber = Convert.ToInt32(orderNum.Substring(2));
int acOrderId = OrderDataSource.LookupOrderId(orderNumber);
Order shippedOrder = new Order();
if (acOrderId != 0)
    shippedOrder = OrderDataSource.Load(acOrderId);

User avatar
david-ebt
Captain (CAPT)
Captain (CAPT)
Posts: 253
Joined: Fri Dec 31, 2010 10:12 am

Re: Order Shipment Status Displayed

Post by david-ebt » Thu May 24, 2012 9:32 am

That sure seems like it should work. The Admin\Orders pages don't seem to do any more.
David
http://www.ecombuildertoday.com
Enhanced Reporting for AbleCommerce
Image

vashts1980
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 98
Joined: Fri Apr 29, 2011 2:56 pm

Re: Order Shipment Status Displayed

Post by vashts1980 » Thu May 24, 2012 10:32 am

Should I post the entire page of code? It's just an ASHX page with 127 lines.

User avatar
david-ebt
Captain (CAPT)
Captain (CAPT)
Posts: 253
Joined: Fri Dec 31, 2010 10:12 am

Re: Order Shipment Status Displayed

Post by david-ebt » Thu May 24, 2012 10:55 am

Sure.
David
http://www.ecombuildertoday.com
Enhanced Reporting for AbleCommerce
Image

vashts1980
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 98
Joined: Fri Apr 29, 2011 2:56 pm

Re: Order Shipment Status Displayed

Post by vashts1980 » Thu May 24, 2012 11:01 am

Code: Select all

<%@ WebHandler Language="C#" Class="OrderShipUpdate" %>

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Web;
using CommerceBuilder.Common;
using CommerceBuilder.Orders;
using CommerceBuilder.Shipping;
using CommerceBuilder.Stores;

public class OrderShipUpdate : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        Store _Store = StoreDataSource.Load(1);
        Token.Instance.InitStoreContext(_Store);

        HttpRequest request = context.Request;
        HttpResponse response = context.Response;

        NameValueCollection nvc = request.Form;
        String headerID = nvc["ID"];
        String trackingNums = nvc["trackingnums"];
        String shipDate = nvc["shipdate"];
        String orderNum = nvc["ordernum"];
        String orderStatus = nvc["status"];

        response.ContentType = "text/xml";
        response.ContentEncoding = Encoding.UTF8;

        Int32 orderNumber = 0;
        String ConvertError = "";
        try
        {
            orderNumber = Convert.ToInt32(orderNum.Substring(2));
        }
        catch (Exception err)
        {
            ConvertError = err.Message + "(" + orderNum + ")";
        }

        int acOrderId = OrderDataSource.LookupOrderId(orderNumber);
        Order shippedOrder = new Order();
        if (acOrderId != 0)
        {
            shippedOrder = OrderDataSource.Load(acOrderId);
            if (shippedOrder != null)
            {
                List<String> tn = new List<String>();

                if (trackingNums.Trim() != "")
                {
                    String[] TrackingNumbers = trackingNums.Split('/');
                    for (int nt = 0; nt < TrackingNumbers.Length; nt++)
                    {
                        String[] subTrackNums = TrackingNumbers[nt].Split(';');
                        for (int sub = 0; sub < subTrackNums.Length; sub++)
                            tn.Add(subTrackNums[sub].Replace("USPF", "UPSF").Trim());
                    }
                }

                for (int ship = 0; ship < shippedOrder.Shipments.Count; ship++)
                {
                    OrderShipment shipment = OrderShipmentDataSource.Load(shippedOrder.Shipments[ship].OrderShipmentId);

                    if (tn.Count > 0 && tn.Count != shipment.TrackingNumbers.Count)
                    {
                        if (shipment.TrackingNumbers.Count > 0)
                            shipment.TrackingNumbers.DeleteAll();

                        foreach (String trackNum in tn)
                        {
                            TrackingNumber trackingNum = new TrackingNumber();
                            trackingNum.TrackingNumberData = trackNum;
                            trackingNum.OrderShipmentId = shipment.OrderShipmentId;
                            trackingNum.Save();
                            shipment.TrackingNumbers.Add(trackingNum);
                        }

                        shipment.Save();

                        if (orderStatus == "complete")
                        {
                            if (shipDate != null && shipDate != "")
                            {
                                shipment.Ship(DateTime.Parse(shipDate));
                            }
                            else
                                shipment.Ship();
                        }

                        shipment.Save();
                        shippedOrder.Save();
                    }
                    else
                    {
                        TrackingNumber trackingNum = new TrackingNumber();
                        trackingNum.TrackingNumberData = "Customer Pick Up";
                        trackingNum.OrderShipmentId = shipment.OrderShipmentId;
                        trackingNum.Save();
                        shipment.TrackingNumbers.Add(trackingNum);
                        shipment.Save();
                        shippedOrder.Save();
                    }
                }

                shippedOrder = OrderDataSource.Load(shippedOrder.OrderId);
                if (orderStatus == "complete")
                {
                    shippedOrder.ShipmentStatus = OrderShipmentStatus.Shipped;
                    shippedOrder.ShipmentStatusId = 2;
                }   
                shippedOrder.OrderStatusId = 3;
                shippedOrder.Save();

                SendResponse(response, "SUCCESS");
            }
            else
                SendResponse(response, "FAILED");
        }
        else
            SendResponse(response, "FAILED");
    }

vashts1980
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 98
Joined: Fri Apr 29, 2011 2:56 pm

Re: Order Shipment Status Displayed

Post by vashts1980 » Tue May 29, 2012 11:02 am

Ok, I added the following code after the last save of the order record and it seems to be doing the trick...

Code: Select all

shippedOrder = OrderDataSource.Load(shippedOrder.OrderId);
if (shippedOrder.ShipmentStatusId != 2)
{
    try
    {
        CommerceBuilder.Data.Database database = Token.Instance.Database;
        StringBuilder sql = new StringBuilder();
        sql.Append("UPDATE ac_Orders SET ac_Orders.ShipmentStatusId=2 WHERE ac_Orders.OrderId=" + acOrderId.ToString());
        System.Data.Common.DbCommand selectCommand = database.GetSqlStringCommand(sql.ToString());
        database.AddInParameter(selectCommand, "@storeId", System.Data.DbType.Int32, Token.Instance.StoreId);
        System.Data.IDataReader dbResults = database.ExecuteReader(selectCommand);
        dbResults.Close();
        dbResults.Dispose();
        selectCommand.Connection.Close();
    }
    catch (Exception)
    {
        SendResponse(response, "FAILED");
        return;
    }
}
I'd rather not have to write directly to the database, but if the value isn't being saved as it should, I don't see much other option.

Post Reply