<?php 
require_once ("../app/formatos/formato.php");
$_POST['cliNom'] = str_ireplace('&','y',$_POST['cliNom']);
$_POST['emp_razsoc'] = str_ireplace('&','y',$_POST['emp_razsoc']);
$_POST['guia_lle'] = substr($_POST['guia_lle'],0,100);
$_POST['guia_par'] = substr($_POST['guia_par'],0,100);
$xml  = '<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<DespatchAdvice 
	xmlns="urn:oasis:names:specification:ubl:schema:xsd:DespatchAdvice-2" 
	xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
	xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
	xmlns:ccts="urn:un:unece:uncefact:documentation:2" 
	xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
	xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" 
	xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" 
	xmlns:sac="urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1" 
	xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  
  <ext:UBLExtensions>
    <ext:UBLExtension>
      <ext:ExtensionContent>  
	  </ext:ExtensionContent>
    </ext:UBLExtension>
  </ext:UBLExtensions>
  
<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
<cbc:CustomizationID>1.0</cbc:CustomizationID>
<cbc:ID>'.$_POST['guia_numdoc'].'</cbc:ID>
<cbc:IssueDate>'.$_POST['guia_fec'].'</cbc:IssueDate>
<cbc:DespatchAdviceTypeCode>09</cbc:DespatchAdviceTypeCode>
<cbc:Note>'.$_POST['ven_numdoc'].'</cbc:Note>
 
  <cac:Signature>
    <cbc:ID>'.$_POST['emp_ruc'].'</cbc:ID>
    <cac:SignatoryParty>
      <cac:PartyIdentification>
        <cbc:ID>'.$_POST['emp_ruc'].'</cbc:ID>
      </cac:PartyIdentification>
      <cac:PartyName>
        <cbc:Name>'.$_POST['emp_nomcom'].'</cbc:Name>
      </cac:PartyName>
    </cac:SignatoryParty>
    <cac:DigitalSignatureAttachment>
      <cac:ExternalReference>
        <cbc:URI>SIGN</cbc:URI>
      </cac:ExternalReference>
    </cac:DigitalSignatureAttachment>
  </cac:Signature>
  
  <cac:DespatchSupplierParty>
    <cbc:CustomerAssignedAccountID schemeID="6">'.$_POST['emp_ruc'].'</cbc:CustomerAssignedAccountID>
    <cac:Party>
      <cac:PartyLegalEntity>
        <cbc:RegistrationName>'.$_POST['emp_razsoc'].'</cbc:RegistrationName>
      </cac:PartyLegalEntity>
    </cac:Party>
  </cac:DespatchSupplierParty>
  
  <cac:DeliveryCustomerParty>
    <cbc:CustomerAssignedAccountID schemeID="'.$_POST['tipdoc'].'">'.$_POST['cliDoc'].'</cbc:CustomerAssignedAccountID>
    <cac:Party>
      <cac:PartyLegalEntity>
        <cbc:RegistrationName>'.$_POST['cliNom'].'</cbc:RegistrationName>
      </cac:PartyLegalEntity>
    </cac:Party>
  </cac:DeliveryCustomerParty>
  
  <cac:Shipment>
    <cbc:ID>1</cbc:ID>
    <cbc:HandlingCode>'.$_POST['guia_motid'].'</cbc:HandlingCode>
    <cbc:Information>'.$_POST['guia_mot'].'</cbc:Information>
	 <cbc:GrossWeightMeasure unitCode="KGM">'.moneda_mysql(formato_money($_POST['guia_can'])).'</cbc:GrossWeightMeasure>
    <cbc:SplitConsignmentIndicator>true</cbc:SplitConsignmentIndicator>
    
	<cac:ShipmentStage>
	
      <cbc:TransportModeCode>01</cbc:TransportModeCode>
      <cac:TransitPeriod>
        <cbc:StartDate>'.$_POST['guia_fec'].'</cbc:StartDate>
      </cac:TransitPeriod>
	  
    </cac:ShipmentStage>
	
    <cac:Delivery>
      <cac:DeliveryAddress>
		<cbc:ID>200601</cbc:ID>
        <cbc:StreetName>'.$_POST['guia_lle'].'</cbc:StreetName>
      </cac:DeliveryAddress>
    </cac:Delivery>
	
    <cac:OriginAddress>
		<cbc:ID>200601</cbc:ID>
	  <cbc:StreetName>'.$_POST['guia_par'].'</cbc:StreetName>
    </cac:OriginAddress>
	
  </cac:Shipment>
  
 ';
	
$i=1;
foreach($_POST['guiadet_can'] as $indice=>$can)
{
	$item_nom=$_POST['guiadet_pronom'][$indice];
		$item_mar=$_POST['guiadet_marnom'][$indice];
		$item_proid=$_POST['guiadet_pro_id'][$indice];
		$item_uni=$_POST['guiadet_uninom'][$indice];
		$item_pes=$_POST['guiadet_pes'][$indice];
$xml .='
<cac:DespatchLine>
<cbc:ID>'.$i.'</cbc:ID>
<cbc:DeliveredQuantity unitCode="MIU">'.$can.'</cbc:DeliveredQuantity>
<cac:OrderLineReference>
      <cbc:LineID>'.$i.'</cbc:LineID>
</cac:OrderLineReference>
<cac:Item>
<cbc:Name>'.$item_nom.'</cbc:Name>
<cac:SellersItemIdentification>
<cbc:ID>'.$item_proid.'</cbc:ID>
</cac:SellersItemIdentification>
</cac:Item>
</cac:DespatchLine>
';
$i++;
}
$xml .='
</DespatchAdvice>';
$fac = new DOMDocument;
$fac->standalone         = false;
$fac -> preserveWhiteSpace = true;
$fac->loadXML($xml);
//Save XML as a file
$nom_xml = $_POST['emp_ruc'].'-'.$_POST['tip_com'].'-'.$_POST['guia_numdoc'].'.xml';
$archivo = '../docsinfirma/'.$nom_xml;
$fac->save($archivo);
$data["url"] = dirname()."../../docgeneracion/firmar.php";
$data["arc"] = $archivo;
$data["tipcom"] = $_POST['tip_com'];
$data["ruc"] = $_POST['emp_ruc'];
echo json_encode($data);
?>