NetSuite高级HTML模板:如何实现相同物料分组并汇总数量与金额?
解决NetSuite高级HTML模板中物料行分组汇总问题
问题场景
你有一个从多个Sales Order提取行数据的自定义记录,当前使用的NetSuite高级HTML模板会重复显示相同物料的行,导致打印内容冗余。你找到的分组示例代码仅实现了去重,无法完成数量(quantity)和金额(amount)的汇总,需要调整代码实现需求。
现有模板代码
<#if record.item?has_content> <table class="itemtable" style="margin-top:10px;width:100%;"><!-- start items --><#list record.item as item><#if item_index==0> <thead> <tr> <th align="left" colspan="9">Product Description</th> <th align="center" colspan="4">Packaging Type</th> <th align="center" colspan="3">${item.quantity@label} /<br />Weight (T)</th> <th align="center" colspan="3">${item.rate@label} (${record.currency})</th> <th align="right" colspan="4">${item.amount@label} (${record.currency})</th> </tr> </thead> </#if><tr> <td colspan="9" line-height="150%"><span class="itemname"><!--${item.item}--></span>${item.description}</td> <td align="center" colspan="4">${item.custcol_mss_bag_type}</td> <td align="center" colspan="3">${item.quantity}</td> <td align="center" colspan="3">${item.rate?string['#,##0.00']}</td> <td align="right" colspan="4">${item.amount?string['#,##0.00']}</td> </tr> </#list><!-- end items --></table> <hr />
你找到的分组示例代码(仅去重,无汇总)
<#assign item_group = {}/> <#List record item as item> <#assign item_key = item.item/> <#assign item_group = item_group + {item.item: item}> </#list> <#list item_group?values as item> <td align="center" colspan="4">${item.item}</td> <td align="center" colspan="4">${item.description}</td> </#list>
完整解决方案代码
替换原有模板中的表格部分,以下代码会按物料ID分组,自动汇总数量和金额,同时保留物料描述、包装类型等信息:
<#if record.item?has_content> <#-- 1. 初始化分组对象,存储每个物料的汇总数据 --> <#assign item_group = {}> <#-- 2. 遍历所有物料行,按物料ID分组并汇总 --> <#list record.item as item> <#assign item_key = item.item?string> <#-- 判断当前物料是否已在分组中 --> <#if item_group[item_key]??> <#-- 已存在:累加数量和金额 --> <#assign existing_item = item_group[item_key]> <#assign updated_quantity = existing_item.total_quantity + item.quantity> <#assign updated_amount = existing_item.total_amount + item.amount> <#assign item_group = item_group + { item_key: { "item": item.item, "description": item.description, "custcol_mss_bag_type": item.custcol_mss_bag_type, "total_quantity": updated_quantity, "total_amount": updated_amount, "rate": item.rate <!-- 假设相同物料单价一致,取最后一行的单价 --> } }> <#else> <#-- 不存在:新增分组条目 --> <#assign item_group = item_group + { item_key: { "item": item.item, "description": item.description, "custcol_mss_bag_type": item.custcol_mss_bag_type, "total_quantity": item.quantity, "total_amount": item.amount, "rate": item.rate } }> </#if> </#list> <#-- 3. 生成汇总后的表格 --> <table class="itemtable" style="margin-top:10px;width:100%;"> <thead> <tr> <th align="left" colspan="9">Product Description</th> <th align="center" colspan="4">Packaging Type</th> <th align="center" colspan="3">${record.item[0].quantity@label} /<br />Weight (T)</th> <th align="center" colspan="3">${record.item[0].rate@label} (${record.currency})</th> <th align="right" colspan="4">${record.item[0].amount@label} (${record.currency})</th> </tr> </thead> <tbody> <#list item_group?values as grouped_item> <tr> <td colspan="9" style="line-height:150%;"> <span class="itemname"></span>${grouped_item.description} </td> <td align="center" colspan="4">${grouped_item.custcol_mss_bag_type}</td> <td align="center" colspan="3">${grouped_item.total_quantity}</td> <td align="center" colspan="3">${grouped_item.rate?string['#,##0.00']}</td> <td align="right" colspan="4">${grouped_item.total_amount?string['#,##0.00']}</td> </tr> </#list> </tbody> </table> <hr /> </#if>
关键说明
- 分组逻辑:以物料ID(
item.item)作为分组键,确保相同物料被归为一组 - 汇总规则:对同一物料的
quantity和amount进行累加,单价(rate)默认取该物料最后一行的数值(如果同一物料单价可能不同,可根据需求调整为取平均或其他逻辑) - 表头处理:通过
record.item[0]获取表头标签,避免原模板中依赖第一个循环条目的问题
内容的提问来源于stack exchange,提问作者Zarah
相关产品推荐
相关产品推荐

