Whats the best way to check if all the rows have been returned on a sales order? I’m using the sales return management to create RMA’s and when/if all items on the original sales order have been returned I want to excecute some custom logic.
You can try to create the RMA and catch the invalidDataException like this :
try
{
generatedRma = _rmaFactory.Create(new RmaCreateArgs {SalesOrderSystemId = args.Rma.SalesOrderSystemId.Value});
}
catch (InvalidDataException)
{
// InvalidDataException will be thrown if the order has fully returned
// Implement custom logic here
}
Thank you for your input. I ended up doing like this:
private bool IsOrderFullyReturned(Order salesOrder)
{
var ovfi = new OrderViewFilteringInfo();
ovfi.StartDate = new DateTime(2022, 9, 1); ;
ovfi.EndDate = DateTime.Now;
ovfi.OrderStatus = (short)OrderState.ReturnCompleted;
ovfi.OrderType = OrderType.SalesReturnOrder;
var pi = new PagingInfo(1, 100);
var ovsi = new OrderViewSortingInfo(SortOrder.ASC, OrderViewSortingInfo.OrderBy.ExternalOrderID);
var ai = new AdditionalInfo();
var qi = new QueryInfo(ai, ovfi, pi, ovsi);
var orderViewRows = ModuleECommerce.Instance.Orders
.GetOrderViewRows(qi, ModuleECommerce.Instance.AdminToken);
var salesReturnOrders = ModuleECommerce.Instance.Orders
.GetOrders(orderViewRows
.Select(x => x.OrderID), ModuleECommerce.Instance.AdminToken)
.Where(o => o.SalesOrderSystemId == salesOrder.ID);
var returnedItems = new Dictionary<string, decimal>();
foreach (var sro in salesReturnOrders)
{
foreach (var or in sro.OrderRows)
{
if (!returnedItems.ContainsKey(or.ArticleNumber))
returnedItems.Add(or.ArticleNumber, or.Quantity);
else
returnedItems[or.ArticleNumber] += or.Quantity;
}
}
var orderedItems = new Dictionary<string, decimal>();
foreach (var or in salesOrder.OrderRows)
orderedItems.Add(or.ArticleNumber, or.Quantity);
foreach (var or in salesOrder.OrderRows)
{
if (returnedItems.ContainsKey(or.ArticleNumber))
orderedItems[or.ArticleNumber] += returnedItems[or.ArticleNumber];
}
return !orderedItems.Any(i => i.Value > 0);
}