Page 1 of 1
Order Shipment Status Displayed
Posted: Tue May 22, 2012 11:34 am
by vashts1980
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?
Re: Order Shipment Status Displayed
Posted: Tue May 22, 2012 1:36 pm
by david-ebt
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.
Re: Order Shipment Status Displayed
Posted: Tue May 22, 2012 1:41 pm
by vashts1980
Ah. Ok, that makes perfect sense. I should be able to remedy the situation with just a couple lines of code, then. Thank you.
Re: Order Shipment Status Displayed
Posted: Wed May 23, 2012 5:00 pm
by vashts1980
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.
Re: Order Shipment Status Displayed
Posted: Thu May 24, 2012 8:58 am
by david-ebt
Have you tried setting the shipment status for the order itself?
Code: Select all
_Order.ShipmentStatus = OrderShipmentStatus.Shipped;
_Order.Save();
Re: Order Shipment Status Displayed
Posted: Thu May 24, 2012 9:09 am
by vashts1980
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);
Re: Order Shipment Status Displayed
Posted: Thu May 24, 2012 9:32 am
by david-ebt
That sure seems like it should work. The Admin\Orders pages don't seem to do any more.
Re: Order Shipment Status Displayed
Posted: Thu May 24, 2012 10:32 am
by vashts1980
Should I post the entire page of code? It's just an ASHX page with 127 lines.
Re: Order Shipment Status Displayed
Posted: Thu May 24, 2012 10:55 am
by david-ebt
Sure.
Re: Order Shipment Status Displayed
Posted: Thu May 24, 2012 11:01 am
by vashts1980
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");
}
Re: Order Shipment Status Displayed
Posted: Tue May 29, 2012 11:02 am
by vashts1980
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.