Search code examples
netsuitefreemarker

Group NetSuite Advanced PDF Customer Statement by the Memo field on Invoice


I am trying to group a customer statement by the memo field, and then have different subtotals per Memo.

Attached is an image of where I am trying to get to. Effectively each subtotal will be grouped on the Memo field on NetSuite.

I have scoured the internet to try and find something to help, including the Free Marker documentation but have come up short.

Has anyone done something like this before or has any ideas?

enter image description here


Solution

  • You can loop through the statement lines to create a sequence of memos, then loop through the memos with a nested loop of the lines, compare memos and print the line only if the memos match. Not very well explained I fear, but check the freemarker below for the general idea:

    <#-- Assign an empty sequence to hold the memos (called 'branches' here)-->
    <#assign branches = []>
    
    <#-- Loop through the statement lines to add to the 'branches' sequence -->
    <#list record.lines as line>
      <#if !branches?seq_contains(line.memo)>
        <#assign branches += [line.memo]>
      </#if>
    </#list>
    
    <#-- This just lists the contents of the new sequence -->
    <#list branches as branch>
      ${branch}
    </#list>
    
    <#-- This just lists the contents of the statement lines as in the default statement-->
    <#list record.lines as line>
      ${line.memo} --- ${line.amount}
    </#list>
    
    <#-- Here's where we use the nested list directive to show each element of the new sequence and then each matching element of the statement lines-->
    <#list branches as branch>
      ${branch}
      <#list record.lines as line>
        <#if line.memo == branch>
          ${line.memo} --- ${line.amount}
        </#if>
      </#list>
    </#list>
    

    You may need to create another sequence or hash to store the subtotals, if they're not available in the data. You will also need to check the variable and property names I've used - this is not a copy and paste example, but just to give the idea!

    You can test this out at the Online FreeMarker Template Tester using the above code as the template and the following as the data:

    record = { "lines": [{"memo": "Branch1", "amount": 100}, {"memo": "Branch1", "amount": 50},{"memo": "Branch2", "amount": 200}]}