Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT adding tax..How to handle 1

Status
Not open for further replies.

Bilberry

Programmer
Dec 17, 2007
111
NL
Hi all,

I want to add some tax on the unit price. Normally we have the following:

Quantity | Unit price | Net amount
76 | 84.12 | 6393.12

The net amount is calculated with 76*84.12

It is possible to have some taxes which i need to add to the net amount, and i need to change the unit price. Within the XML file there can be TypeOfAllowanceOrChange//MonetaryAmount element. This is the tax, for example 14.14. The XSL should be changed to calculate the following:

Quantity | Unit price | Net amount | Wrap tax
76 | 84.306 | 6407.26 | 14.14

The unit price should be calculated as follows:
84.12 + (14.14/76)

Now my XML files. Normally we can have multiple Lineitemnum's a invoice, one of these item is look like:

Code:
- <LineItemNum>
  <BuyerLineItemNum>1</BuyerLineItemNum> 
  </LineItemNum>
- <ItemIdentifiers>
- <PartNumbers>
- <StandardPartNumber>
- <ProductIdentifierCoded>
  <ProductIdentifierQualifierCoded>EAN2-5-5-1</ProductIdentifierQualifierCoded> 
  <ProductIdentifier>4011100002777</ProductIdentifier> 
  </ProductIdentifierCoded>
  </StandardPartNumber>
  </PartNumbers>
  </ItemIdentifiers>
- <TotalQuantity>
- <Quantity>
  <QuantityValue>1</QuantityValue> 
- <UnitOfMeasurement>
  <UOMCoded>1</UOMCoded> 
  </UnitOfMeasurement>
  </Quantity>
  </TotalQuantity>
  </InvoiceBaseItemDetail>
- <InvoicePricingDetail>
- <ListOfPrice>
- <Price>
- <UnitPrice>
  <UnitPriceValue>12.29</UnitPriceValue> 
  </UnitPrice>
  </Price>
  </ListOfPrice>
- <Tax>
 <TaxFunctionQualifierCoded>Tax</TaxFunctionQualifierCoded> 
  <TaxCategoryCoded>ExemptFromTax</TaxCategoryCoded> 
  <TaxTypeCoded>ValueAddedTax</TaxTypeCoded> 
  <TaxPercent /> 
  <TaxAmount>0</TaxAmount> 
  </Tax>
- <InvoiceCurrencyTotalValue>
- <MonetaryValue>
  <MonetaryAmount>12.29</MonetaryAmount> 
  </MonetaryValue>
  </InvoiceCurrencyTotalValue>
  </InvoicePricingDetail>
  </InvoiceItemDetail>

In my HTML document, i need to fill the information above in a table look like:

Quantity | Unit price | Net amount | Wraptax
1 | 12.29 | 12.29 |

This is not a problem. But sometimes i can have like the following item:

Code:
- <InvoiceItemDetail>
- <InvoiceBaseItemDetail>
- <LineItemNum>
  <BuyerLineItemNum>4</BuyerLineItemNum> 
  </LineItemNum>
- <ItemIdentifiers>
- <PartNumbers>
- <StandardPartNumber>
- <ProductIdentifierCoded>
  <ProductIdentifierQualifierCoded>EAN2-5-5-1</ProductIdentifierQualifierCoded> 
  <ProductIdentifier>5449000000552</ProductIdentifier> 
  </ProductIdentifierCoded>
  </StandardPartNumber>
  </PartNumbers>
  </ItemIdentifiers>
- <TotalQuantity>
- <Quantity>
  <QuantityValue>1</QuantityValue> 
- <UnitOfMeasurement>
  <UOMCoded>1</UOMCoded> 
  </UnitOfMeasurement>
  </Quantity>
  </TotalQuantity>
  </InvoiceBaseItemDetail>
- <InvoicePricingDetail>
- <ListOfPrice>
- <Price>
- <UnitPrice>
  <UnitPriceValue>6.49</UnitPriceValue> 
  </UnitPrice>
  </Price>
  </ListOfPrice>
- <Tax>
 <TaxFunctionQualifierCoded>Tax</TaxFunctionQualifierCoded> 
  <TaxCategoryCoded>ExemptFromTax</TaxCategoryCoded> 
  <TaxTypeCoded>ValueAddedTax</TaxTypeCoded> 
  <TaxPercent /> 
  <TaxAmount>0</TaxAmount> 
  </Tax>
- <ItemAllowancesOrCharges>
- <ListOfAllowOrCharge>
- <AllowOrCharge>
  <IndicatorCoded>Charge</IndicatorCoded> 
  <MethodOfHandlingCoded>Other</MethodOfHandlingCoded> 
- <AllowanceOrChargeDescription>
- <AllowOrChgDesc>
- <ListOfDescription>
- <Description>
  <DescriptionText>RAD</DescriptionText> 
- <Language>
  <LanguageCoded>en</LanguageCoded> 
  </Language>
  </Description>
  </ListOfDescription>
  <ServiceCoded>Deposit</ServiceCoded> 
  </AllowOrChgDesc>
  </AllowanceOrChargeDescription>
- <TypeOfAllowanceOrCharge>
- <MonetaryValue>
  <MonetaryAmount>5</MonetaryAmount> 
  </MonetaryValue>
  </TypeOfAllowanceOrCharge>
  </AllowOrCharge>
  </ListOfAllowOrCharge>
  </ItemAllowancesOrCharges>
- <InvoiceCurrencyTotalValue>
- <MonetaryValue>
  <MonetaryAmount>11.49</MonetaryAmount> 
  </MonetaryValue>
  </InvoiceCurrencyTotalValue>
  </InvoicePricingDetail>
  </InvoiceItemDetail>

As you can see we have an addtional element, called AllowOrCharge (which is not always available, in this example we need to add the 5 euro on the unit price, like:


Quantity | Unit price | Net amount | Wrap tax
1 | 11.49 | 11.49 | 5

The unit price should be calculated in the stylesheet as follows:

6.49 + (5/1) = 11.49

I have the following lines in my stysheet to put this information into a table:
Code:
<xsl:for-each select="//InvoiceItemDetail"> 
..
..
<td><span><xsl:value-of select="InvoiceBaseItemDetail//QuantityValue"/></span></td>

<!-- The calculation of the unit price -->

<td align="right"><span><xsl:value-of select="format-number(InvoicePricingDetail//UnitPriceValue + concat('0', InvoicePricingDetail//AllowOrCharge[IndicatorCoded='Charge']/../TypeOfAllowanceOrCharge/MonetaryValue/MonetaryAmount) div //QuantityValue, '0.00')"/></span></td>

But the unit price calculation does not calculate the unit price correctly, does somebody know what im doing wrong?

A big golden star for the solution...

TIA



 
[1] You can devise the template to match InvoiceItemDetail like this.
[tt]
<xsl:template match="InvoiceItemDetail">
<xsl:variable name="qty" select="number(InvoiceBaseItemDetail/TotalQuantity/Quantity/QuantityValue)" />
<xsl:variable name="unitprice" select="number(InvoicePricingDetail//UnitPriceValue[1])" />
<xsl:variable name="taxadj">
<xsl:choose>
<xsl:when test="count(.//ItemAllowancesOrCharges) != 0">
<xsl:value-of select="number(.//ItemAllowancesOrCharges[1]//MonetaryAmount[1])" />
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="0" />
</xsl:eek:therwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="unitpriceadj" select="format-number(($unitprice * $qty + $taxadj) div $qty, '#.##')" />
<xsl:variable name="netamount" select="format-number($unitpriceadj * $qty, '#.##')" />
<tr>
<td><xsl:value-of select="$qty" /></td>
<td><xsl:value-of select="$unitpriceadj" /></td>
<td><xsl:value-of select="$netamount" /></td>
<td>
<xsl:choose>
<xsl:when test="$taxadj = 0">
<xsl:value-of select="'&#xa0;'" />
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="$taxadj" />
</xsl:eek:therwise>
</xsl:choose>
</td>
</tr>
</xsl:template>
[/tt]
[2] It can be called like this.
[tt]
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>Quantity</th>
<th>Unit Price</th>
<th>Net amount</th>
<th>Wrap tax</th>
</tr>
<xsl:apply-templates select="//InvoiceItemDetail" />
</table>
</body>
</html>
</xsl:template>
[/tt]
[3] You can consider make the names of variables more easy to associate the meaning of them and make the path to the nodes more precise rather than using //. I use the latter to shorten the path for convenience only.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top