<?php 

require_once ("../app/formatos/formato.php");



$_POST['cli_nom'] = str_ireplace('&','y',$_POST['cli_nom']);

$_POST['emp_razsoc'] = str_ireplace('&','y',$_POST['emp_razsoc']);



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

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-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: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>2.0</cbc:CustomizationID>

	



  <cbc:ProfileID schemeName="SUNAT:Identificador de Tipo de Operacion" schemeAgencyName="PE:SUNAT" schemeURI="urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo17">0104</cbc:ProfileID>

  <cbc:ID>'.$_POST['ven_numdoc'].'</cbc:ID>

 



 <cbc:IssueDate>'.$_POST['ven_fec'].'</cbc:IssueDate>

  <cbc:InvoiceTypeCode listID="0101" listAgencyName="PE:SUNAT" listName="SUNAT:Identificador de Tipo de Documento" listURI="urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo01">'.$_POST['tip_com'].'</cbc:InvoiceTypeCode>

  <cbc:Note languageLocaleID="1000">'.$_POST['ven_lettot'].'</cbc:Note>   

  <cbc:DocumentCurrencyCode listID="ISO 4217 Alpha" listName="Currency" listAgencyName="United Nations Economic Commission for Europe">'.$_POST['mon_let'].'</cbc:DocumentCurrencyCode>

  <cbc:LineCountNumeric>1</cbc:LineCountNumeric>







 <cac:DespatchDocumentReference>

        <cbc:ID>'.$_POST['guia_numdoc'].'</cbc:ID>

        <cbc:DocumentTypeCode listAgencyName="PE:SUNAT" listName="SUNAT:Identificador de Tipo de Documento" listURI="urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo01">09</cbc:DocumentTypeCode>

   </cac:DespatchDocumentReference>









<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_razsoc'].'</cbc:Name>

      </cac:PartyName>

    </cac:SignatoryParty>

    <cac:DigitalSignatureAttachment>

      <cac:ExternalReference>

        <cbc:URI>#SignatureKG</cbc:URI>

      </cac:ExternalReference>

    </cac:DigitalSignatureAttachment>

  </cac:Signature>

  



  

  <cac:AccountingSupplierParty>

        <cac:Party>

            <cac:PartyIdentification>

                <cbc:ID schemeID="6" schemeName="SUNAT:Identificador de Documento de Identidad" schemeAgencyName="PE:SUNAT" schemeURI="urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo06">'.$_POST['emp_ruc'].'</cbc:ID>

            </cac:PartyIdentification>

            <cac:PartyName>

                <cbc:Name>'.$_POST['emp_rep'].'</cbc:Name>

            </cac:PartyName>

            <cac:PartyLegalEntity>

                <cbc:RegistrationName>'.$_POST['emp_razsoc'].'</cbc:RegistrationName>

                <cac:RegistrationAddress>

                    <cbc:AddressTypeCode>0001</cbc:AddressTypeCode>

                </cac:RegistrationAddress>

            </cac:PartyLegalEntity>

        </cac:Party>

    </cac:AccountingSupplierParty>

	

  

  

   <cac:AccountingCustomerParty>

        <cac:Party>

            <cac:PartyIdentification>

                <cbc:ID schemeID="'.$_POST['tipdoc'].'" schemeName="SUNAT:Identificador de Documento de Identidad" schemeAgencyName="PE:SUNAT" schemeURI="urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo06">'.$_POST['cli_numdoc'].'</cbc:ID>

            </cac:PartyIdentification>

            <cac:PartyLegalEntity>

                <cbc:RegistrationName>'.$_POST['cli_nom'].'</cbc:RegistrationName>

            </cac:PartyLegalEntity>

        </cac:Party>

    </cac:AccountingCustomerParty>

   

 <cac:TaxTotal>

        <cbc:TaxAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($_POST['ven_igv'])).'</cbc:TaxAmount>

        <cac:TaxSubtotal>

            <cbc:TaxableAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($_POST['ven_valven'])).'</cbc:TaxableAmount>

            <cbc:TaxAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($_POST['ven_igv'])).'</cbc:TaxAmount>

            <cac:TaxCategory>

                <cbc:ID schemeID="UN/ECE 5305" schemeName="Tax Category Identifier" schemeAgencyName="United Nations Economic Commission for Europe">S</cbc:ID>

                <cac:TaxScheme>

                    <cbc:ID schemeID="UN/ECE 5153" schemeAgencyID="6">1000</cbc:ID>

                    <cbc:Name>IGV</cbc:Name>

                    <cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>

                </cac:TaxScheme>

            </cac:TaxCategory>

        </cac:TaxSubtotal>

    </cac:TaxTotal>

    

  <cac:LegalMonetaryTotal>

        <cbc:PayableAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($_POST['ven_tot'])).'</cbc:PayableAmount>

    </cac:LegalMonetaryTotal>

 ';

  

  

$i=1;

foreach($_POST['vendet_can'] as $indice=>$can)

{

		

$item_proid=$_POST['pro_id'][$indice];

$item_nom=$_POST['vendet_art'][$indice];

$item_valuni=$_POST['vendet_valuni'][$indice];

$item_subtot=$_POST['vendet_valuni'][$indice] * $can;

$item_pre  =$item_valuni * 1.18;

		

$xml .='

<cac:InvoiceLine>

        <cbc:ID>'.$i.'</cbc:ID>

        <cbc:InvoicedQuantity unitCode="MIU" unitCodeListID="UN/ECE rec 20" unitCodeListAgencyName="United Nations Economic Commission forEurope">'.$can.'</cbc:InvoicedQuantity>

        

		<cbc:LineExtensionAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($item_subtot)).'</cbc:LineExtensionAmount>

        

		<cac:PricingReference>

            <cac:AlternativeConditionPrice>

                <cbc:PriceAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($item_pre)).'</cbc:PriceAmount>

                <cbc:PriceTypeCode listName="SUNAT:Indicador de Tipo de Precio" listAgencyName="PE:SUNAT" listURI="urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo16">01</cbc:PriceTypeCode>

            </cac:AlternativeConditionPrice>

        </cac:PricingReference>

        

		<cac:TaxTotal>

            <cbc:TaxAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($_POST['ven_igv'])).'</cbc:TaxAmount>

            <cac:TaxSubtotal>

                <cbc:TaxableAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($_POST['ven_valven'])).'</cbc:TaxableAmount>

                <cbc:TaxAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($_POST['ven_igv'])).'</cbc:TaxAmount>

                <cac:TaxCategory>

                    <cbc:ID schemeID="UN/ECE 5305" schemeName="Tax Category Identifier" schemeAgencyName="United Nations Economic Commission for Europe">S</cbc:ID>

                    <cbc:Percent>18.00</cbc:Percent>

                    <cbc:TaxExemptionReasonCode listAgencyName="PE:SUNAT" listName="SUNAT:Codigo de Tipo de Afectación del IGV" listURI="urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo07">10</cbc:TaxExemptionReasonCode>

                    <cac:TaxScheme>

                        <cbc:ID schemeID="UN/ECE 5153" schemeName="Tax Scheme Identifier" schemeAgencyName="United Nations Economic Commission for Europe">1000</cbc:ID>

                        <cbc:Name>IGV</cbc:Name>

                        <cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>

                    </cac:TaxScheme>

                </cac:TaxCategory>

            </cac:TaxSubtotal>

        </cac:TaxTotal>

        <cac:Item>

            <cbc:Description>'.$item_nom.'</cbc:Description>

            <cac:SellersItemIdentification>

                <cbc:ID>'.$item_proid.'</cbc:ID>

            </cac:SellersItemIdentification>

            <cac:CommodityClassification>

                <cbc:ItemClassificationCode listID="UNSPSC" listAgencyName="GS1 US" listName="Item Classification">10000000</cbc:ItemClassificationCode>

            </cac:CommodityClassification>

        </cac:Item>

        <cac:Price>

            <cbc:PriceAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($item_valuni)).'</cbc:PriceAmount>

        </cac:Price>

    </cac:InvoiceLine>';	

$i++;

}

  

 $xml.='</Invoice>';





$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['ven_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);



?>