NetSuite高级PDF按自定义客户列分组项目的两类问题求助
NetSuite发票PDF自定义字段分组问题修复
我在NetSuite中需要按**自定义交易列字段(客户选择列表)**对发票PDF里的项目进行分组。该字段可以留空(此时关联开票客户/主体),也可以选择开票客户的子客户。
示例数据
| 项目名称 | 金额 | 关联实体 |
|---|---|---|
| Setup Item | 500 | Customer (Bill To) |
| Software Fee | 1000 | Customer (Bill To) |
| Software Fee | 1000 | Child Customer#1 |
| Software Fee | 1000 | Child Customer#2 |
| Variable Fee | 3515 | Child Customer#1 |
| Variable Fee | 1525 | Child Customer#2 |
期望与当前问题
期望PDF分三个部分展示对应客户的项目及小计,但目前遇到两个问题:
- Customer (Bill To)部分仅显示表头和小计,项目行缺失且小计为$0;
- 需要将子客户的表格排在Customer (Bill To)之前。
现有FreeMarker代码
<#assign entitiesProcessed = []> <#list record.item?sort_by('custcol_associated_entity') as item> <#assign entityName = item.custcol_associated_entity> <#if entityName == ""> <#assign entityName = record.entity> </#if> <#if entitiesProcessed?seq_contains(entityName)> <#else> <#assign entitiesProcessed = entitiesProcessed +[entityName] > <#assign entityTotal=0> <table style="width: 100%;"> <tr><td>${entityName}</td></tr> <#list record.item?sort_by('custcol_associated_entity') as item2> <#if entityName == item2.custcol_associated_entity> <#assign entityTotal = entityTotal + item2.amount> <tr><td> <table class="itemtable" style="width: 100%;"><tr> <td>${item2.item}</td> <td align="center" style="width: 100px;">${item2.quantity}</td> <td align="right" style="width: 100px;">${item2.rate}</td> <td align="right" style="width: 100px;">${item2.amount}</td> </tr></table> </td></tr> </#if></#list> <tr><td align="right">${entityName} Total: $${entityTotal?string(",##0.00")}</td></tr> </table> </#if> </#list>
问题原因
- 主客户项目匹配失败:当自定义字段为空时,外层循环把
entityName设为record.entity,但内层循环判断时,item2.custcol_associated_entity是空字符串,和entityName(主客户名称)不匹配,导致主客户的项目被过滤掉,小计自然为0。 - 排序逻辑错误:原代码按自定义字段排序,空字符串会排在子客户名称前面,所以主客户部分会先显示。
修复后的代码
<#-- 提前处理所有项目,统一关联实体名称并标记主实体 --> <#assign processedItems = []> <#list record.item as item> <#assign entity = item.custcol_associated_entity!> <#assign entityName = entity?trim == "" ? record.entity : entity> <#assign processedItems = processedItems + [{ "item": item.item, "quantity": item.quantity, "rate": item.rate, "amount": item.amount, "entityName": entityName, "isMainEntity": entity?trim == "" }]> </#list> <#-- 排序:子客户在前,主客户在后;子客户按名称排序 --> <#assign sortedItems = processedItems?sort_by(["isMainEntity", "entityName"])> <#-- 提取去重后的实体列表,保持排序顺序 --> <#assign uniqueEntities = []> <#list sortedItems as item> <#if !uniqueEntities?seq_contains(item.entityName)> <#assign uniqueEntities = uniqueEntities + [item.entityName]> </#if> </#list> <#-- 按实体分组渲染表格 --> <#list uniqueEntities as entityName> <table style="width: 100%; margin-bottom: 15px;"> <tr><td style="font-weight: bold; font-size: 1.1em;">${entityName}</td></tr> <#assign entityTotal = 0> <tr> <td> <table class="itemtable" style="width: 100%; border-collapse: collapse;"> <#-- 项目表头 --> <tr style="border-bottom: 1px solid #ccc;"> <td style="font-weight: bold;">项目</td> <td align="center" style="width: 100px; font-weight: bold;">数量</td> <td align="right" style="width: 100px; font-weight: bold;">单价</td> <td align="right" style="width: 100px; font-weight: bold;">金额</td> </tr> <#-- 渲染当前实体的所有项目 --> <#list sortedItems as item> <#if item.entityName == entityName> <#assign entityTotal = entityTotal + item.amount> <tr style="border-bottom: 1px solid #eee;"> <td>${item.item}</td> <td align="center" style="width: 100px;">${item.quantity}</td> <td align="right" style="width: 100px;">${item.rate}</td> <td align="right" style="width: 100px;">${item.amount}</td> </tr> </#if> </#list> </table> </td> </tr> <#-- 小计行 --> <tr> <td align="right" style="font-weight: bold; margin-top: 5px;"> ${entityName} 小计: $${entityTotal?string(",##0.00")} </td> </tr> </table> </#list>
修复说明
- 统一实体匹配逻辑:提前遍历所有项目,把空字段的实体名称直接替换成主客户名称,同时标记是否为主实体,确保后续筛选时能精准匹配。
- 调整排序规则:通过
isMainEntity标记让子客户排在前面,主客户排在后面,子客户内部按名称排序,满足需求。 - 优化代码结构:把项目预处理、排序、去重分开写,逻辑更清晰,后续修改也方便。
- 增强视觉效果:添加了表头、边框和间距,让PDF里的表格更易读。
内容的提问来源于stack exchange,提问作者user30795227
相关产品推荐
相关产品推荐

