# Check if all rows on an order have been returned (RMA)

**URL:** <https://forum.litium.com/t/check-if-all-rows-on-an-order-have-been-returned-rma/2684>\
**Category:** Questions\
**Tags:** litium-7\
**Created:** [September 21, 2022, 12:48pm UTC](https://forum.litium.com/t/check-if-all-rows-on-an-order-have-been-returned-rma/2684 "2022-09-21T12:48:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![magnus.bolling](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/magnus.bolling/32/222_2.png) [@magnus.bolling](https://forum.litium.com/u/magnus.bolling)\
**Post date:** [September 21, 2022, 12:48pm UTC](https://forum.litium.com/t/check-if-all-rows-on-an-order-have-been-returned-rma/2684/1 "2022-09-21T12:48:43Z")

</div>

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.

Litium version: 7.7.9

---

<div class="post-metadata">

**Author:** ![steve.redstorm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/steve.redstorm/32/14_2.png) [@steve.redstorm](https://forum.litium.com/u/steve.redstorm)\
**Post date:** [September 22, 2022, 8:07am UTC](https://forum.litium.com/t/check-if-all-rows-on-an-order-have-been-returned-rma/2684/2 "2022-09-22T08:07:08Z")

</div>

You could match the quantities in the original Sales-order and the Sales-Return-order and execute your logic.

---

<div class="post-metadata">

**Author:** ![magnus.bolling](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/magnus.bolling/32/222_2.png) [@magnus.bolling](https://forum.litium.com/u/magnus.bolling)\
**Post date:** [September 26, 2022, 9:21am UTC](https://forum.litium.com/t/check-if-all-rows-on-an-order-have-been-returned-rma/2684/3 "2022-09-26T09:21:01Z")

</div>

How can i get all SalesReturnOrders that is connected to a SalesOrder?

---

<div class="post-metadata">

**Author:** ![steve.redstorm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/steve.redstorm/32/14_2.png) [@steve.redstorm](https://forum.litium.com/u/steve.redstorm)\
**Post date:** [September 26, 2022, 9:41am UTC](https://forum.litium.com/t/check-if-all-rows-on-an-order-have-been-returned-rma/2684/4 "2022-09-26T09:41:48Z")

</div>

You can try to create the RMA and catch the invalidDataException like this :

```auto
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
}

```

Litiums own exception that is thrown :

 ![Exception](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/eb59db3c5c9f033fb155ffb1deb9a5cee7ca286e.png)

---

<div class="post-metadata">

**Author:** ![magnus.bolling](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/magnus.bolling/32/222_2.png) [@magnus.bolling](https://forum.litium.com/u/magnus.bolling)\
**Post date:** [September 27, 2022, 1:34pm UTC](https://forum.litium.com/t/check-if-all-rows-on-an-order-have-been-returned-rma/2684/5 "2022-09-27T13:34:25Z")

</div>

Thank you for your input. I ended up doing like this:

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)\
**Post date:** [October 25, 2022, 1:34pm UTC](https://forum.litium.com/t/check-if-all-rows-on-an-order-have-been-returned-rma/2684/6 "2022-10-25T13:34:33Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
