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

   Author: Vera Wiest
      Filename:     ship.xsl
   Supporting Files: ship.xml, ship.xsd
   Requirements: Computational- sums price, sums quantity, total cost of all items  
   One choose conditional statement to determine zone shipping price
   One choose conditional statement to total costs when
   One if conditional statement to format date
   five templates- customer, orders, items, item and total cost
  four Computations are sum of price, sum of quantity, quantity times price, total cost
-->

	<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	
	<xsl:output method="html" version="4.0" />
	<xsl:decimal-format decimal-separator="." />
	
	<xsl:template match="/">
	<html>
	<head>
		<title>Shipping Rate Calculations</title>
		<link href="columns.css" rel="stylesheet" type="text/css" />
	</head>
	
	
	<body>
		
	<div id="meat">
	<h1><img src="header2a.jpg" alt="Header" style="margin-left:-5px"/></h1>
	
	<p class="menu"><a href="home.htm">Home</a> | <a href="columncovers.xml">Products</a> |  <a href="colors.xml">Finishes</a>  
	| <a href="portfolio.htm">Portfolio</a>  
		| <a href="ship.xml">Shipping</a> | <a href="contact.xml">Contact Us</a></p>
		
	<h2 class="subtitle">Shipping Rates </h2>
	
   <xsl:comment>This ship page has a customer template that is comprised of a template orders that has a template called items.
   	The item template sums the price and qty rows and calls the template for total cost. The item template spans the row for the date 
   	and chooses which shipping price to add to the item depending on its zone.
	</xsl:comment> 
  
   <xsl:apply-templates select="customers/customer" />   
	
	
	<h4 class="support">support@pittconindustries.com  301.927.1000 </h4>
<p class="support"></p>	
	<p class="footer"><i>Manufactured in Maryland, Arizona, Georgia and California</i></p>	

</div>	
 
	</body>
	</html>	
	
	</xsl:template>
	
	<xsl:template match="customer">
    <table >
    <tr>
       <th>Customer</th>
       <td><xsl:value-of select="cName" />
       </td> 
      
       <th>State</th>
       <td> 
           <xsl:value-of select="state" /> 
       </td>
   
       <th>Zone</th>
       <td> 
           <xsl:value-of select="zone" /> 
       </td>
    </tr>
    
    </table>

    <xsl:apply-templates select="orders" />
</xsl:template>


<xsl:template match="orders">
   <table class="orderinfo" border="10" cellpadding="2">
   	<tr>
    
      <td class="ship">Date</td>
      <td class="ship">Item</td>      
      <td class="ship">Shipping</td>
      <td class="ship">Qty</td>
      <td class="ship">Total</td>
   </tr>

   <xsl:apply-templates select="order/items" />

  
   </table>
   
   <br />  <br />  <br />
</xsl:template>


<xsl:template match="items">
   <xsl:apply-templates select="item" />
<!-- sum columns-->
   <tr>
      <td class="ship" colspan="2">Totals</td>
        <td class="ship"><xsl:value-of select="format-number(sum(item/@price), '$#,#00.00')" /></td>
      <td class="ship"><xsl:value-of select=" sum(item/@qty)" /></td>
      <td class="ship">  <xsl:call-template name="totalcost" >
      <xsl:with-param name="list" select="item" />
      </xsl:call-template></td>
   </tr>
</xsl:template>

<!-- If conditional statement for column span for same dates -->
<xsl:template match="item">
   <tr>
   	<xsl:if test="position()=1">
   	<td  rowspan="{count(../../items/item)}">   		
      <xsl:value-of select="../../@date" />
      </td> 
      </xsl:if>
      
      <!-- Choose conditional statement to determine zone price-->
      <td class="tdtext"><xsl:value-of select="." /></td>
      <!-- Determine shipping rate-->
       <td><xsl:choose>
       <xsl:when test="@series=1500 and ../../@zone=1">
       <p>20.00</p>
       </xsl:when>
       <xsl:when test="@series= 2000 and ../../@zone=1">
       <p>25.00</p>
       </xsl:when>
       <xsl:when test="@series= 9000 and ../../@zone=1">
       <p>30.00</p>
       </xsl:when>
       <xsl:when test="@series= 1500 and ../../@zone=2">
       <p>25.00</p>
       </xsl:when>
       <xsl:when test="@series= 2000 and ../../@zone=2">
       <p>30.00</p>
       </xsl:when>
       <xsl:when test="@series= 9000 and ../../@zone=2">
       <p>35.00</p>
       </xsl:when>
       <xsl:when test="@series= 1500 and ../../@zone=3">
       <p>35.00</p>
       </xsl:when>
       <xsl:when test="@series= 2000 and ../../@zone=3">
       <p>40.00</p>
       </xsl:when>
       <xsl:when test="@series= 9000 and ../../@zone=3">
       <p>45.00</p>
       </xsl:when>
       </xsl:choose></td>
       
       <td><xsl:value-of select="@qty" /></td>
      <!-- Multiply quantity times price-->
        <td> <xsl:value-of select="format-number(@qty * @price, '#,00.00')" /></td>
          
   </tr>
</xsl:template> 

 
<xsl:template name="totalcost">
	<xsl:param name="list" />
	<xsl:param name="total" select="0" />
	<!-- Choose conditional statement to Calculate the total item cost -->
		<xsl:choose>
	<xsl:when test="$list">
		<!--Calculate the first item cost -->
		<xsl:variable name="first" select="$list[1]/@qty * $list[1]/@price" />
		
		<!-- Call the totalcost template-->
		<xsl:call-template name="totalcost" >
			<xsl:with-param name="list" select="$list[position() > 1]" />
			<xsl:with-param name="total" select="$first + $total" />
			</xsl:call-template>
		</xsl:when>
	
		<xsl:otherwise>
			<!-- Display the total cost-->
			<xsl:value-of select="format-number($total, '$#,#00.00')" />
		</xsl:otherwise>
	</xsl:choose>
</xsl:template> 

          
</xsl:stylesheet>