XSLT v1-为pdf计算发票布局的折扣总和-NaN错误

问题描述 投票:0回答:1

我有三个自定义折扣,我正在尝试获取折扣的总和,以便可以在pdf布局上显示。

我需要在XSL v1.0中对此进行编程,因为这是CRM用来生成pdf模板的内容。完整的脚本是所有文档(报价,订单,合同和发票)的组合文档布局,因此我只显示了摘录。

在我的测试数据中,有三个产品线,并且每个产品线至少有一个折扣。

LINE 1 Band Relief 302.5 Regional Discount 50% Price Discount 0%
LINE 2 Band Relief 10 Regional Discount 0% Price Discount 0%
LINE 3 Band Relief 0 Regional Discount 0% Price Discount 5%

我尝试如下修改Michael Hor的代码(XSL v1.0 and XPath to Traverse Deep Nesting and Recursively Sum all Lines):

<?xml version="1.0" encoding="UTF-8"?>

<xslt:stylesheet xmlns:date="http://exslt.org/dates-and-times" xmlns:str="http://exslt.org/strings" xmlns:xslt="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xf="http://www.ecrion.com/xf/1.0" xmlns:xc="http://www.ecrion.com/2008/xc" xmlns:xfd="http://www.ecrion.com/xfd/1.0" xmlns:svg="http://www.w3.org/2000/svg" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" extension-element-prefixes="date str">
  <xslt:output indent="yes" encoding="utf-8"/>


<!-- ==== SUM NODES TEMPLATE ==== -->

  <xsl:template name="sum-nodes" >
    <xsl:param name="nodes"/>
    <xsl:param name="sum" select="0"/>
    <xsl:param name="newSum" select="$sum + translate($nodes[1], '$,', '')"/>
    <xsl:choose>
        <xsl:when test="count($nodes) > 1">
             <!-- recursive call --> 
            <xsl:call-template name="sum-nodes" >
                <xsl:with-param name="nodes" select="$nodes[position() > 1]" />
                <xsl:with-param name="sum" select="$newSum" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="format-number($newSum, '#,##0.00')"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

以及在主模板中:

<xslt:template = "/">

<!-- === TESTING VARIABLES === -->

          <fo:block padding-top="0.45cm" font-size="9pt" padding-bottom="0cm">
            <xsl:for-each select = "/hash/order_line_items/order_line_item">
                <fo:block padding-top="0.45cm" font-size="9pt" padding-bottom="0cm">
                    <fo:inline>
                        Band Relief <xsl:value-of select="sum(cf_customer_invoice_line_item_band_relief/@amount)"/>
                        Regional Discount <xsl:value-of select="sum(additional_discount_1_percent)"/>
                        Price Discount <xsl:value-of select="sum(discount_percent)"/>
                    </fo:inline>
                </fo:block>
            </xsl:for-each> 
          </fo:block>

          <fo:block padding-top="0.45cm" font-size="9pt" padding-bottom="0cm">
            <fo:inline>

                <fo:inline> Band Total </fo:inline>
                <xsl:call-template name="sum-nodes" >
                    <xsl:with-param name="nodes" select="cf_customer_invoice_line_item_band_relief[@amount &gt; 0]" />
                </xsl:call-template>

                <fo:inline> Regional Total </fo:inline>
                <xsl:call-template name="sum-nodes" >
                    <xsl:with-param name="nodes" select="additional_discount_1_percent/@amount[@amount &gt; 0]" />
                </xsl:call-template>

                <fo:inline> Discount Total </fo:inline>
                <xsl:call-template name="sum-nodes" >
                    Discount Total <xsl:with-param name="nodes" select="discount_percent/@amount[@amount &gt; 0]" />
                </xsl:call-template>

        <!--        $regional_discount_1 <xsl:value-of select="$regional_discount_1"/> -->
        <!--        $price_discount <xsl:value-of select="$price_discount"/> -->
            </fo:inline>
          </fo:block>


</xslt:template>

但是总和只会产生NaN结果。

折扣字段是货币或百分比(或为空)。

输出:

Band Relief 302.5 Regional Discount 50 Price Discount 0
Band Relief 10 Regional Discount 0 Price Discount 0
Band Relief 0 Regional Discount 0 Price Discount 5
Band Total NaN Regional Total NaN Discount Total NaN

预期结果:

Band Total £312.50 Regional Total 50% Discount Total 5%

为了让它正常工作,我已经转了好几天了!任何帮助将不胜感激!

更新:

我无法提取完整的XML结构作为其内置到数据库中,但是对于发票行项目表,它是指:

                <xsl:for-each select="/hash/order_line_items/order_line_item[contains(./_preload_product_id, 'SHIPPING')=false()]">
              <fo:table-row>

                <fo:table-cell border-width="1pt" border-style="solid" border-color="rgb(0,0,0)" padding="2pt" text-align="right">
                  <xsl:attribute name="border-color">
                    <xsl:value-of select="/hash/text/Table_Border_Colour"/>
                  </xsl:attribute>
                  <fo:block font-size="8pt">
                    <fo:inline>
                      <xsl:value-of select="unit_quantity"/>
                    </fo:inline>
                  </fo:block>
                </fo:table-cell>

等,所以我想它是/ hash / order_line_items / order_line_item /

更新2

由于数据已嵌入到CRM中,因此我无法将数据提取为XML文件。

我所能做的就是将数据库中的样本数据作为报告导出:如果没有乐队减免,则不会发布任何值。范围缓解设置为“货币”(如果未应用折扣,则为空白),而其他两个折扣则设置为百分比。

csv of invoice data

似乎无法将csv作为文本字段导入,因此已作为图像上传。

希望这会有所帮助!

xml xslt sum pdf-generation xslt-1.0
1个回答
0
投票

如果您显示XML结构但我想您想使用的话,对我们会有所帮助

<xsl:value-of select="format-number(sum(/hash/order_line_items/order_line_item/cf_customer_invoice_line_item_band_relief[@amount &gt; 0]), '#,##0.00')"/>
© www.soinside.com 2019 - 2024. All rights reserved.