<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/">
		<html>
		<head>
			<title>Purchase Order</title>
			<style>
				body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; margin: 16px; color: #1a1a1a; background: #fff; font-size: 14px; }
				.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; margin: 6px 0; }
				.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 6px 0; }
				.box { border: 1px solid #ccc; border-radius: 4px; padding: 6px 10px; }
				h1 { font-size: 18px; margin: 0 0 8px 0; color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 6px; }
				h2 { font-size: 13px; margin: 0 0 3px 0; color: #3498db; text-transform: uppercase; letter-spacing: 0.5px; }
				table { border-collapse: collapse; width: 100%; }
				td { padding: 2px 6px; vertical-align: top; }
				.lbl { color: #888; white-space: nowrap; width: 1%; }
				.val { color: #1a1a1a; }
				.g2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
				.ext { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 2px 20px; }
				.ext-item { white-space: nowrap; }
				.ext-item .lbl { font-size: 14px; }
				.ext-item .val { font-weight: 600; }
				.lines { width: 100%; border-collapse: collapse; margin: 8px 0; }
				.lines th { background: #2c3e50; color: #fff; padding: 6px 8px; font-size: 13px; text-align: left; white-space: nowrap; }
				.lines td { padding: 5px 8px; border-bottom: 1px solid #e8e8e8; font-size: 14px; }
				.lines tr:nth-child(even) td { background: #f8f9fa; }
				.r { text-align: right; }
				.foot { border-top: 2px solid #2c3e50; margin-top: 8px; padding-top: 6px; }
				.foot td { padding: 4px 8px; }
				.foot .total { font-weight: bold; font-size: 15px; background: #eaf2f8; }
				.dim { color: #999; font-size: 13px; }
				.ctl { font-size: 13px; color: #666; }
				.sep { border-top: 1px solid #eee; }
				.badge { display: inline-block; padding: 2px 8px; border-radius: 3px; font-size: 13px; font-weight: 600; }
				.badge-new { background: #d4edda; color: #155724; }
				.badge-update { background: #fff3cd; color: #856404; }
				.badge-accept { background: #d4edda; color: #155724; }
				.badge-reject { background: #f8d7da; color: #721c24; }
				.badge-backordered { background: #fff3cd; color: #856404; }
				pre { margin: 4px 0; font-size: 14px; white-space: pre-wrap; }
			</style>
		</head>
		<body>
		<xsl:for-each select="cXML">
			<h1>Purchase Order <xsl:value-of select="Request/OrderRequest/OrderRequestHeader/@orderID"/></h1>

			<div class="grid3">
				<div class="box">
					<h2>Order</h2>
					<div class="g2">
						<span class="ext-item"><span class="lbl">ID:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/@orderID"/></span></span>
						<span class="ext-item"><span class="lbl">Date:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/@orderDate"/></span></span>
						<span class="ext-item"><span class="lbl">Type:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/@orderType"/></span></span>
						<span class="ext-item"><span class="lbl">Action:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/@type"/></span></span>
						<span class="ext-item"><span class="lbl">Version:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/@orderVersion"/></span></span>
						<span class="ext-item"><span class="lbl">Mode:</span>&#160;<span class="val"><xsl:value-of select="Request/@deploymentMode"/></span></span>
						<span class="ext-item"><span class="lbl">cXML:</span>&#160;<span class="val"><xsl:value-of select="@version"/></span></span>
					</div>
				</div>
				<div class="box">
					<h2>Ship To</h2>
					<xsl:for-each select="Request/OrderRequest/OrderRequestHeader/ShipTo">
						<span class="ext-item"><span class="lbl">Code:</span>&#160;<span class="val"><xsl:value-of select="Address/@addressID"/></span></span><br/>
						<span class="ext-item"><span class="lbl">Name:</span>&#160;<span class="val"><xsl:value-of select="Address/Name"/></span></span>
						<br/>
						<span class="ext-item"><span class="lbl">Address:</span>&#160;<span class="val"><xsl:value-of select="Address/PostalAddress/Street"/>, <xsl:value-of select="Address/PostalAddress/PostalCode"/> <xsl:value-of select="Address/PostalAddress/City"/>, <xsl:value-of select="Address/@isoCountryCode"/></span></span>
						<div class="g2">
							<xsl:for-each select="IdReference">
								<span class="ext-item"><span class="lbl"><xsl:call-template name="domainLabel"><xsl:with-param name="name" select="@domain"/></xsl:call-template>:</span>&#160;<span class="val"><xsl:value-of select="@identifier"/></span></span>
							</xsl:for-each>
						</div>
					</xsl:for-each>
				</div>
				<div class="box">
					<h2>Bill To</h2>
					<xsl:for-each select="Request/OrderRequest/OrderRequestHeader/BillTo">
						<span class="ext-item"><span class="lbl">ID:</span>&#160;<span class="val"><xsl:value-of select="Address/@addressID"/></span></span><br/>
						<span class="ext-item"><span class="lbl">Name:</span>&#160;<span class="val"><xsl:value-of select="Address/Name"/></span></span>
						<br/>
						<span class="ext-item"><span class="lbl">Address:</span>&#160;<span class="val"><xsl:value-of select="Address/PostalAddress/Street"/>, <xsl:value-of select="Address/PostalAddress/PostalCode"/> <xsl:value-of select="Address/PostalAddress/City"/>, <xsl:value-of select="Address/@isoCountryCode"/></span></span>
						<div class="g2">
							<xsl:for-each select="Address/IdReference">
								<span class="ext-item"><span class="lbl"><xsl:call-template name="domainLabel"><xsl:with-param name="name" select="@domain"/></xsl:call-template>:</span>&#160;<span class="val"><xsl:value-of select="@identifier"/></span></span>
							</xsl:for-each>
						</div>
					</xsl:for-each>
				</div>
			</div>

			<div class="grid3">
				<div class="box">
					<h2>Network</h2>
					<div class="g2">
						<span class="ext-item"><span class="lbl">From:</span>&#160;<span class="val"><xsl:value-of select="Header/From/Credential[@domain='NetworkID']/Identity"/></span></span>
						<span class="ext-item"><span class="lbl">System ID:</span>&#160;<span class="val"><xsl:value-of select="Header/From/Credential[@domain='SystemID']/Identity"/></span></span>
						<span class="ext-item"><span class="lbl">To:</span>&#160;<span class="val"><xsl:value-of select="Header/To/Credential[@domain='NetworkID']/Identity"/></span></span>
						<span class="ext-item"><span class="lbl">Sender:</span>&#160;<span class="val"><xsl:value-of select="Header/Sender/Credential/Identity"/></span></span>
						<span class="ext-item"><span class="lbl">User Agent:</span>&#160;<span class="val"><xsl:value-of select="Header/Sender/UserAgent"/></span></span>
					</div>
				</div>
				<div class="box">
					<h2>From (Customer)</h2>
					<xsl:for-each select="Request/OrderRequest/OrderRequestHeader/BusinessPartner[@role='soldTo']">
						<div class="g2">
							<span class="ext-item"><span class="lbl">Name:</span>&#160;<span class="val"><xsl:value-of select="Address/Name"/></span></span>
							<span class="ext-item"><span class="lbl">Account:</span>&#160;<span class="val"><xsl:value-of select="Address/@addressID"/></span></span>
							<span class="ext-item"><span class="lbl">Country:</span>&#160;<span class="val"><xsl:value-of select="Address/@isoCountryCode"/></span></span>
							<xsl:for-each select="Address/IdReference">
								<span class="ext-item"><span class="lbl"><xsl:call-template name="domainLabel"><xsl:with-param name="name" select="@domain"/></xsl:call-template>:</span>&#160;<span class="val"><xsl:value-of select="@identifier"/></span></span>
							</xsl:for-each>
						</div>
					</xsl:for-each>
				</div>
				<div class="box">
					<h2>To (Supplier)</h2>
					<xsl:for-each select="Request/OrderRequest/OrderRequestHeader/Contact[@role='supplierCorporate']">
						<div class="g2">
							<span class="ext-item"><span class="lbl">Name:</span>&#160;<span class="val"><xsl:value-of select="Name"/></span></span>
							<span class="ext-item"><span class="lbl">Buyer ID:</span>&#160;<span class="val"><xsl:value-of select="@addressID"/></span></span>
							<span class="ext-item"><span class="lbl">Country:</span>&#160;<span class="val"><xsl:value-of select="PostalAddress/Country/@isoCountryCode"/></span></span>
							<span class="ext-item"><span class="lbl">Email:</span>&#160;<span class="val"><xsl:value-of select="Email/@name"/></span></span>
							<xsl:for-each select="IdReference">
								<span class="ext-item"><span class="lbl"><xsl:call-template name="domainLabel"><xsl:with-param name="name" select="@domain"/></xsl:call-template>:</span>&#160;<span class="val"><xsl:value-of select="@identifier"/></span></span>
							</xsl:for-each>
						</div>
					</xsl:for-each>
				</div>
			</div>

			<div class="grid3">
				<div class="box">
					<h2>Organization</h2>
					<span class="ext-item"><span class="lbl">Company:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/LegalEntity/IdReference/@identifier"/> — <xsl:value-of select="Request/OrderRequest/OrderRequestHeader/LegalEntity/IdReference/Description"/></span></span>
					<div class="g2">
						<xsl:for-each select="Request/OrderRequest/OrderRequestHeader/OrganizationalUnit[IdReference/@domain!='PurchasingGroup']">
							<span class="ext-item"><span class="lbl"><xsl:call-template name="domainLabel"><xsl:with-param name="name" select="IdReference/@domain"/></xsl:call-template>:</span>&#160;<span class="val"><xsl:value-of select="IdReference/@identifier"/> — <xsl:value-of select="IdReference/Description"/></span></span>
						</xsl:for-each>
					</div>
				</div>
				<div class="box">
					<h2>Agent</h2>
					<xsl:for-each select="Request/OrderRequest/OrderRequestHeader/Contact[@role='purchasingAgent']">
						<div class="g2">
							<span class="ext-item"><span class="lbl">Name:</span>&#160;<span class="val"><xsl:value-of select="Name"/></span></span>
							<span class="ext-item"><span class="lbl">Email:</span>&#160;<span class="val"><xsl:value-of select="Email"/></span></span>
						</div>
					</xsl:for-each>
					<xsl:for-each select="Request/OrderRequest/OrderRequestHeader/OrganizationalUnit[IdReference/@domain='PurchasingGroup']">
						<span class="ext-item"><span class="lbl"><xsl:call-template name="domainLabel"><xsl:with-param name="name" select="IdReference/@domain"/></xsl:call-template>:</span>&#160;<span class="val"><xsl:value-of select="IdReference/@identifier"/> — <xsl:value-of select="IdReference/Description"/></span></span>
					</xsl:for-each>
				</div>
				<div class="box">
					<h2>Delivery</h2>
					<div class="g2">
						<span class="ext-item"><span class="lbl">Code:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/TermsOfDelivery/TermsOfDeliveryCode/@value"/></span></span>
						<span class="ext-item"><span class="lbl">Shipping:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/TermsOfDelivery/ShippingPaymentMethod/@value"/></span></span>
						<span class="ext-item"><span class="lbl">Terms:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/TermsOfDelivery/TransportTerms/@value"/> — <xsl:value-of select="Request/OrderRequest/OrderRequestHeader/TermsOfDelivery/TransportTerms"/></span></span>
						<span class="ext-item"><span class="lbl">From:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/TermsOfDelivery/Address/Name"/></span></span>
					</div>
				</div>
			</div>

			<div class="grid2" style="grid-template-columns: 2fr 3fr">
				<div class="box" style="padding:5px 10px">
					<h2>Extra</h2>
					<div class="ext">
						<span class="ext-item"><span class="lbl">Payment:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/PaymentTerm/@payInNumberOfDays"/> days</span></span>
						<span class="ext-item"><span class="lbl">Discount:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/PaymentTerm/Discount/DiscountPercent/@percent"/>%</span></span>
						<span class="ext-item"><span class="lbl">Payment terms:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/Extrinsic[@name='AribaNetwork.PaymentTermsExplanation']"/></span></span>
						<span class="ext-item"><span class="lbl">Customer VAT:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/Extrinsic[@name='buyerVatID']"/></span></span>
						<span class="ext-item"><span class="lbl">Supplier VAT:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/Extrinsic[@name='supplierVatID']"/></span></span>
						<span class="ext-item"><span class="lbl">Invoicing:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/Extrinsic[@name='Ariba.invoicingAllowed']"/></span></span>
						<span class="ext-item"><span class="lbl">Available:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/Extrinsic[@name='Ariba.availableAmount']"/></span></span>
						<span class="ext-item"><span class="lbl">Requester:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/Extrinsic[@name='Requester']"/></span></span>
						<span class="ext-item"><span class="lbl">Party ID:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/Extrinsic[@name='partyAdditionalID']"/></span></span>
						<span class="ext-item"><span class="lbl">Document:</span>&#160;<span class="val"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/OrderRequestHeaderIndustry/ExternalDocumentType/@documentType"/> — <xsl:value-of select="Request/OrderRequest/OrderRequestHeader/OrderRequestHeaderIndustry/ExternalDocumentType/Description"/></span></span>
					</div>
				</div>
				<xsl:if test="Request/OrderRequest/OrderRequestHeader/Comments">
					<div class="box" style="padding:5px 10px">
						<h2>Comments</h2>
						<pre><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/Comments"/></pre>
					</div>
				</xsl:if>
			</div>

			<table class="lines">
				<tr>
					<th>Line</th>
					<th>Ext Line</th>
					<th>Buyer Part</th>
					<th>Part</th>
					<th>Description</th>
					<th class="r">Qty</th>
					<th>Unit</th>
					<th>Delivery</th>
					<th class="r">Unit Price</th>
					<th class="r">Line Total</th>
					<th>Tax</th>
					<th>Control Keys</th>
					<th>Commodity</th>
					<th>Info</th>
					<th>Schedule</th>
				</tr>
				<xsl:for-each select="Request/OrderRequest/ItemOut">
					<tr>
						<td><xsl:value-of select="@lineNumber"/></td>
						<td><xsl:value-of select="ItemDetail/Extrinsic[@name='extLineNumber']"/></td>
						<td><xsl:value-of select="ItemID/BuyerPartID"/></td>
						<td><xsl:value-of select="ItemID/SupplierPartID"/></td>
						<td><xsl:value-of select="ItemDetail/Description"/></td>
						<td class="r"><xsl:value-of select="@quantity"/></td>
						<td><xsl:value-of select="ItemDetail/UnitOfMeasure"/></td>
						<td style="white-space:nowrap"><xsl:value-of select="substring(@requestedDeliveryDate, 1, 10)"/></td>
						<td class="r"><xsl:value-of select="ItemDetail/UnitPrice/Money"/></td>
						<td class="r"><xsl:value-of select="format-number(@quantity * ItemDetail/UnitPrice/Money, '#.00')"/></td>
						<td><xsl:value-of select="Tax/TaxDetail/TaxAmount/Money"/>&#160;<xsl:choose>
							<xsl:when test="Tax/TaxDetail/@category='NN'">NN&#160;<span class="dim">(zero)</span></xsl:when>
							<xsl:when test="Tax/TaxDetail/@category='UH'">UH&#160;<span class="dim">(std VAT</span><xsl:if test="Tax/TaxDetail/@percentageRate != ''">&#160;<xsl:value-of select="Tax/TaxDetail/@percentageRate"/>%</xsl:if><span class="dim">)</span></xsl:when>
							<xsl:otherwise><xsl:value-of select="Tax/TaxDetail/@category"/><xsl:if test="Tax/TaxDetail/@percentageRate != ''">&#160;<span class="dim"><xsl:value-of select="Tax/TaxDetail/@percentageRate"/>%</span></xsl:if></xsl:otherwise>
						</xsl:choose></td>
						<td class="ctl">OC: <xsl:choose><xsl:when test="ControlKeys/OCInstruction/@value='allowed'">yes</xsl:when><xsl:otherwise><xsl:value-of select="ControlKeys/OCInstruction/@value"/></xsl:otherwise></xsl:choose>&#160;| ASN: <xsl:choose><xsl:when test="ControlKeys/ASNInstruction/@value='notAllowed'">no</xsl:when><xsl:otherwise><xsl:value-of select="ControlKeys/ASNInstruction/@value"/></xsl:otherwise></xsl:choose>&#160;| Inv: <xsl:choose><xsl:when test="ControlKeys/InvoiceInstruction/@value='isNotERS'">standalone</xsl:when><xsl:when test="ControlKeys/InvoiceInstruction/@value='isERS'">ERS</xsl:when><xsl:when test="ControlKeys/InvoiceInstruction/@value='notAllowed'">no</xsl:when><xsl:otherwise><xsl:value-of select="ControlKeys/InvoiceInstruction/@value"/></xsl:otherwise></xsl:choose>&#160;| Batch: <xsl:value-of select="ItemOutIndustry/BatchInfo/@requiresBatch"/></td>
						<td><xsl:value-of select="ItemDetail/Classification[@domain='ERPCommodityCode']"/>&#160;<span class="dim"><xsl:value-of select="ItemDetail/Classification[@domain='ERPCommodityCodeDescription']"/></span></td>
						<td class="ctl">Planner: <xsl:value-of select="Contact[@role='BuyerPlannerCode']/IdReference[@domain='BuyerPlannerCode']/@identifier"/>&#160;(<xsl:value-of select="Contact[@role='BuyerPlannerCode']/Name"/>)&#160;| Conversion: <xsl:value-of select="ItemDetail/PriceBasisQuantity/@conversionFactor"/>&#160;<xsl:value-of select="ItemDetail/PriceBasisQuantity/UnitOfMeasure"/>&#160;| Inspection days: <xsl:value-of select="ItemDetail/PlannedAcceptanceDays"/>&#160;| Receiving type: <xsl:value-of select="ItemDetail/Extrinsic[@name='ReceivingType']"/></td>
						<td><xsl:value-of select="ScheduleLine/@lineNumber"/>/<xsl:value-of select="ScheduleLine/@quantity"/></td>
					</tr>
				</xsl:for-each>
			</table>

			<div class="foot">
				<table style="width:auto; float:right">
					<tr>
						<td class="lbl">Subtotal</td>
						<td class="r total"><xsl:value-of select="Request/OrderRequest/OrderRequestHeader/Total/Money"/>&#160;<xsl:value-of select="Request/OrderRequest/OrderRequestHeader/Total/Money/@currency"/></td>
					</tr>
					<tr>
						<td class="lbl">Tax</td>
						<td class="r"><xsl:value-of select="format-number(sum(Request/OrderRequest/ItemOut/Tax/TaxDetail/TaxAmount/Money), '#.00')"/>&#160;<xsl:value-of select="Request/OrderRequest/OrderRequestHeader/Total/Money/@currency"/></td>
					</tr>
					<tr>
						<td class="lbl">Grand Total</td>
						<td class="r total"><xsl:value-of select="format-number(Request/OrderRequest/OrderRequestHeader/Total/Money + sum(Request/OrderRequest/ItemOut/Tax/TaxDetail/TaxAmount/Money), '#.00')"/>&#160;<xsl:value-of select="Request/OrderRequest/OrderRequestHeader/Total/Money/@currency"/></td>
					</tr>
				</table>
				<div style="clear:both"/>
				<span class="dim">
					Payload: <xsl:value-of select="@payloadID"/>
					&#160;&#160;|&#160;&#160; <xsl:value-of select="@timestamp"/>
				</span>
			</div>
		</xsl:for-each>
		</body>
		</html>
	</xsl:template>

	<xsl:template name="domainLabel">
		<xsl:param name="name"/>
		<xsl:choose>
			<xsl:when test="$name='buyerLocationID'">Buyer Location</xsl:when>
			<xsl:when test="$name='stockLocationID'">Storage Location</xsl:when>
			<xsl:when test="$name='storageLocationID'">Storage Location</xsl:when>
			<xsl:when test="$name='buyerAccountID'">Buyer Account</xsl:when>
			<xsl:when test="$name='buyerID'">Buyer ID</xsl:when>
			<xsl:when test="$name='PurchasingOrganization'">Purchasing Org.</xsl:when>
			<xsl:when test="$name='PurchasingGroup'">Purchasing Group</xsl:when>
			<xsl:when test="$name='BuyerPlannerCode'">Planner</xsl:when>
			<xsl:otherwise><xsl:value-of select="$name"/></xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
