How to source invoice record's line item details on customer payment advanced pdf template?

28 Views Asked by At

I am trying to show invoice line level items on payment receipt's advanced pdf template. Is it possible? Thanks in advance!

1

There are 1 best solutions below

2
bknights On

Yes, This can be done.

A User Event Script of type beforeLoad can use the script context's type field to run for 'print' events.

You can then look up custom data and add it to your record as custom fields and lists.

Your PDF template can check for the existence of those fields and lists and display them appropriately.

Some Typescript to get you started. Note this was doing the opposite of what you asked - it lists payments on a printed invoice.

export function beforeLoad(ctx: EntryPoints.UserEvent.beforeLoadContext){

    if(ctx.type == ctx.UserEventType.PRINT && ctx.newRecord && ctx.newRecord.id){

const payments = search.create(...).run().getRange({start:0, end:1000}).map...);

var paymentsList = form.addSublist({
        id:'custpage_links',
        type: ui.SublistType.STATICLIST,
        label:'Applied'
    });

    paymentsList.addField({
        id:'doc',
        type:ui.FieldType.TEXT,
        label:'Document'
    })

    paymentsList.addField({
        id:'type',
        type:ui.FieldType.TEXT,
        label:'Type'
    });

    paymentsList.addField({
        id:'total',
        type:ui.FieldType.CURRENCY,
        label:'total'
    });

    payments.forEach((pmnt, idx)=>{
        paymentsList.setSublistValue({
            id:'doc',
            line:idx,
            value:pmnt.doc
        })
        paymentsList.setSublistValue({
            id:'type',
            line:idx,
            value:mapAppliedType(pmnt.type, pmnt.method)
        });
         paymentsList.setSublistValue({
            id:'total',
            line:idx,
            value:''+ pmnt.total
        });
    });

and then on the transaction pdf template side:

    <#assign appliedLines = []>
    <#assign hasPayment = false>
    <#if record.custpage_links?has_content>
        <#list record.custpage_links as link>
            <#if link.type?has_content && link.type != 'Total'>
                <#if appliedLines?seq_contains(link.type)>
                <#else>
                    <#assign hasPayment = true>
                    <#assign appliedLines = appliedLines + [link.type]>
                    <#assign typeApplied = 0>
                    <#list record.custpage_links as pymnt>
                        <#if pymnt.type == link.type>
                            <#assign typeApplied = typeApplied + pymnt.total>
                        </#if>
                    </#list>
                    <tr>
                        <td colspan="5" align="right"><b>${link.type}</b></td>
                        <td align="right"><i>(${typeApplied?string.currency})</i></td>
                    </tr>
                </#if>
            </#if>
        </#list>
    </#if>
    <#if hasPayment == false>
        <tr>
            <td colspan="5" align="right"><b>${record.amountpaid@label}</b></td>
            <td align="right">${record.amountpaid}</td>
        </tr>
    </#if>