Improved "Copy order number from PurchaseOrders group" rule

Created by Jeremy Burgess, Modified on Thu, 23 Jul, 2020 at 10:09 PM by Jeremy Burgess

The FlexiCapture rule "Copy order number from PurchaseOrders group" under the LineItems\OrderNumber field is responsible for copying detected PO number values down to each line item for the purpose of line item matching to PO.


The standard rule (below) is suited to when multiple PO's may be found on the document and the algorithm will ensure that the 'closest' PO number to each line will be selected.


The algorithm DOES NOT copy PO numbers to line items if the PO number has no region on the page - for instance it was typed in manually.


// Script is executed for each Line Item

if (Context.Field("OrderNumberLI").IsVerified || Context.Field("OrderNumberLI").IsMatched)
    return;
    
if (Context.Field("LineItems").Regions.Count == 0)
    return;

int nearest_po_index = -1;
int nearest_po_li_distance = 0;
for(int pageIndex=0; pageIndex<Context.Document.Pages.Count; pageIndex++)
    nearest_po_li_distance += Context.Document.Pages[pageIndex].Rect.Bottom;

for(int i=0; i<Context.Field("OrderNumber").Items.Count; i++)
{
    if (Context.Field("OrderNumber").Items[i].Regions.Count>0)
    {
    
        // look for POs inside Line Item
        if ((Context.Field("OrderNumber").Items[i].Regions[0].PageIndex == Context.Field("LineItems").Regions[0].PageIndex)
        && (Context.Field("OrderNumber").Items[i].Regions[0].SurroundingRect.Top >= Context.Field("LineItems").Regions[0].SurroundingRect.Top)
        && (Context.Field("OrderNumber").Items[i].Regions[0].SurroundingRect.Bottom <= Context.Field("LineItems").Regions[0].SurroundingRect.Bottom))
        {
            Context.Field("OrderNumberLI").Text = Context.Field("OrderNumber").Items[i].Text;
            return;
        }
        
        // look for the nearest PO at the top
        
        /* Recalculating the coordinates to consider multipage invoices
        Each page has y=0 at the top and y=height at the bottom
        The coordinate of the field region at the page N = the height of previous N-1 pages + the coordinate of the region at N-th page.
        So, the top of the very first page has y=0, the end of the document has y =  the sum of height of all pages
        */
        int lineItem_top = Context.Field("LineItems").Regions[0].SurroundingRect.Top;
        for(int pageIndex=0; pageIndex<Context.Field("LineItems").Regions[0].PageIndex; pageIndex++)
            lineItem_top += Context.Document.Pages[pageIndex].Rect.Bottom;
        int orderNumber_top = Context.Field("OrderNumber").Items[i].Regions[0].SurroundingRect.Top;
        for(int pageIndex=0; pageIndex<Context.Field("OrderNumber").Items[i].Regions[0].PageIndex; pageIndex++)
            orderNumber_top += Context.Document.Pages[pageIndex].Rect.Bottom;  
        
        int po_li_distance = lineItem_top - orderNumber_top; // the distance between top of Line Item and the top of Purchase Order    
        if (po_li_distance > 0) // if the top margin of Line Item is located below than Purchase Order in the document
        {   
            if (po_li_distance<nearest_po_li_distance) 
            {
                nearest_po_li_distance = po_li_distance;
                nearest_po_index = i;
            }
        }
    }
}
if (nearest_po_index>-1) 
{
    Context.Field("OrderNumberLI").Text = Context.Field("OrderNumber").Items[nearest_po_index].Text;
    return;
}


Alternative solution

If your objective is simply to copy the first PO number to all lines then use the following script. If will copy the value to any line that has not had explicit verification of the order number field.


if (Context.Field("OrderNumber").Items.Count > 0 && Context.Field("OrderNumberLI").IsVerified == false)
{
    // Copy the value from the first PO number item into the invoice line item
    Context.Field("OrderNumberLI").Text = Context.Field("OrderNumber").Items[0].Text;
}


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article