<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cXML SYSTEM
  "http://xml.cxml.org/schemas/cXML/1.2.066/cXML.dtd">
<cXML version="1.2.066"
      payloadID="{d.payloadId}"
      timestamp="{d.createdAt:formatD('YYYY-MM-DDTHH:mm:ssZ')}"
      xml:lang="en-US">{o.lang=en-us}{o.timezone=America/New_York}
  <Header>
    <From>
      <Credential domain="NetworkID">
        <Identity>{d.buyer.networkId}</Identity>
      </Credential>
    </From>
    <To>
      <Credential domain="DUNS">
        <Identity>{d.supplier.duns}</Identity>
      </Credential>
    </To>
    <Sender>
      <Credential domain="NetworkID">
        <Identity>{d.buyer.networkId}</Identity>
        <SharedSecret>REPLACE_WITH_SHARED_SECRET</SharedSecret>
      </Credential>
      <UserAgent>{d.buyer.userAgent}</UserAgent>
    </Sender>
  </Header>
  <Request deploymentMode="production">
    <OrderRequest>
      <OrderRequestHeader orderID="{d.order.id}"
                          orderDate="{d.createdAt:formatD('YYYY-MM-DDTHH:mm:ssZ')}"
                          type="new">{d.lines[].qty:mul(.price):aggSum:round(2):set(c.sub)}{c.sub:mul(d.order.taxRate):div(100):round(2):set(c.tax)}
        <Total>
          <Money currency="{d.order.currency}">{c.sub:formatN(2):replace(',', '')}</Money>
        </Total>
        <ShipTo>
          <Address isoCountryCode="US" addressID="{d.order.shipTo.id}">
            <Name xml:lang="en-US">{d.order.shipTo.name}</Name>
            <PostalAddress>
              <DeliverTo>{d.order.shipTo.deliverTo}</DeliverTo>
              <Street>{d.order.shipTo.street}</Street>
              <City>{d.order.shipTo.city}</City>
              <State isoStateCode="{d.order.shipTo.state}">{d.order.shipTo.state}</State>
              <PostalCode>{d.order.shipTo.postalCode}</PostalCode>
              <Country isoCountryCode="US">United States</Country>
            </PostalAddress>
          </Address>
        </ShipTo>
        <BillTo>
          <Address isoCountryCode="US" addressID="{d.order.billTo.id}">
            <Name xml:lang="en-US">{d.order.billTo.name}</Name>
            <PostalAddress>
              <Street>{d.order.billTo.street}</Street>
              <City>{d.order.billTo.city}</City>
              <State isoStateCode="{d.order.billTo.state}">{d.order.billTo.state}</State>
              <PostalCode>{d.order.billTo.postalCode}</PostalCode>
              <Country isoCountryCode="US">United States</Country>
            </PostalAddress>
          </Address>
        </BillTo>
        <Shipping>
          <Money currency="{d.order.currency}">{d.order.shipping:formatN(2):replace(',', '')}</Money>
          <Description xml:lang="en-US">Ground freight, prepaid</Description>
        </Shipping>
        <Tax>
          <Money currency="{d.order.currency}">{c.tax:formatN(2):replace(',', '')}</Money>
          <Description xml:lang="en-US">Sales tax</Description>
          <TaxDetail category="sales" percentageRate="{d.order.taxRate}">
            <TaxableAmount>
              <Money currency="{d.order.currency}">{c.sub:formatN(2):replace(',', '')}</Money>
            </TaxableAmount>
            <TaxAmount>
              <Money currency="{d.order.currency}">{c.tax:formatN(2):replace(',', '')}</Money>
            </TaxAmount>
            <TaxLocation xml:lang="en-US">{d.order.taxLocation}</TaxLocation>
          </TaxDetail>
        </Tax>
        <PaymentTerm payInNumberOfDays="{d.order.paymentDays}"/>
        <Contact role="buyer">
          <Name xml:lang="en-US">{d.order.buyerContact.name}</Name>
          <Email>{d.order.buyerContact.email}</Email>
          <Phone>
            <TelephoneNumber>
              <CountryCode isoCountryCode="US">1</CountryCode>
              <AreaOrCityCode>{d.order.buyerContact.area}</AreaOrCityCode>
              <Number>{d.order.buyerContact.phone}</Number>
            </TelephoneNumber>
          </Phone>
        </Contact>
        <Comments xml:lang="en-US">{d.order.comments}</Comments>
      </OrderRequestHeader>{d.lines[i]}
      <ItemOut quantity="{d.lines[i].qty}"
               lineNumber="{d.lines[i].number}"
               requestedDeliveryDate="{d.lines[i].delivery:formatD('YYYY-MM-DD')}">
        <ItemID>
          <SupplierPartID>{d.lines[i].partId}</SupplierPartID>
          <SupplierPartAuxiliaryID>{d.lines[i].aux}</SupplierPartAuxiliaryID>
          <BuyerPartID>{d.lines[i].buyerPart}</BuyerPartID>
        </ItemID>
        <ItemDetail>
          <UnitPrice>
            <Money currency="{d.order.currency}">{d.lines[i].price:formatN(2):replace(',', '')}</Money>
          </UnitPrice>
          <Description xml:lang="en-US">{d.lines[i].description}</Description>
          <UnitOfMeasure>{d.lines[i].unit}</UnitOfMeasure>
          <Classification domain="UNSPSC">{d.lines[i].unspsc}</Classification>
          <ManufacturerPartID>{d.lines[i].mfrPart}</ManufacturerPartID>
          <ManufacturerName xml:lang="en-US">{d.lines[i].mfr}</ManufacturerName>
        </ItemDetail>
        <Distribution>
          <Accounting name="DistributionCharge">
            <AccountingSegment id="{d.lines[i].costCenter}">
              <Name xml:lang="en-US">Cost Center</Name>
              <Description xml:lang="en-US">{d.lines[i].costCenterName}</Description>
            </AccountingSegment>
            <AccountingSegment id="{d.lines[i].gl}">
              <Name xml:lang="en-US">GL Account</Name>
              <Description xml:lang="en-US">{d.lines[i].glName}</Description>
            </AccountingSegment>
          </Accounting>
          <Charge>
            <Money currency="{d.order.currency}">{d.lines[i].qty:mul(.price):round(2):formatN(2):replace(',', '')}</Money>
          </Charge>
        </Distribution>
      </ItemOut>{d.lines[i+1]}
    </OrderRequest>
  </Request>
</cXML>
