<?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='<CreditNote xmlns="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
   <ext:UBLExtensions>
      <ext:UBLExtension>
         <ext:ExtensionContent />
      </ext:UBLExtension>
   </ext:UBLExtensions>
   
   <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
   <cbc:CustomizationID>2.0</cbc:CustomizationID>
   <cbc:ID>'.$_POST['notcre_numdoc'].'</cbc:ID>
   <cbc:IssueDate>'.$_POST['notcre_fec'].'</cbc:IssueDate>
   <cbc:DocumentCurrencyCode>'.$_POST['mon_let'].'</cbc:DocumentCurrencyCode>
  
   <cac:DiscrepancyResponse>
      <cbc:ReferenceID>'.$_POST['ven_numdoc'].'</cbc:ReferenceID>
      <cbc:ResponseCode>'.$_POST['notcre_tip'].'</cbc:ResponseCode>
      <cbc:Description>'.$_POST['notcre_mot'].'</cbc:Description>
   </cac:DiscrepancyResponse>
  
   <cac:BillingReference>
      <cac:InvoiceDocumentReference>
         <cbc:ID>'.$_POST['ven_numdoc'].'</cbc:ID>
         <cbc:DocumentTypeCode>'.$_POST['notcre_tipcom'].'</cbc:DocumentTypeCode>
      </cac:InvoiceDocumentReference>
   </cac:BillingReference>
  
	<cac:Signature>
      <cbc:ID>'.$_POST['emp_ruc'].'</cbc:ID>
      <cbc:Note>'.$_POST['emp_nomcom'].'</cbc:Note>
      <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>#SIGN-GREEN</cbc:URI>
         </cac:ExternalReference>
      </cac:DigitalSignatureAttachment>
    </cac:Signature>
	
	<cac:AccountingSupplierParty>
      <cac:Party>
         <cac:PartyIdentification> 
            <cbc:ID schemeID="6">'.$_POST['emp_ruc'].'</cbc:ID>
         </cac:PartyIdentification>
         <cac:PartyName>
            <cbc:Name>'.$_POST['emp_razsoc'].'</cbc:Name>
         </cac:PartyName>
         <cac:PartyLegalEntity>
            <cbc:RegistrationName>'.$_POST['emp_razsoc'].'</cbc:RegistrationName>
            <cac:RegistrationAddress>
               <cbc:ID>150101</cbc:ID>
               <cbc:AddressTypeCode>0000</cbc:AddressTypeCode>
               <cbc:CitySubdivisionName>-</cbc:CitySubdivisionName>
               <cbc:CityName>-</cbc:CityName>
               <cbc:CountrySubentity>-</cbc:CountrySubentity>
               <cbc:District>-</cbc:District>
               <cac:AddressLine>
                  <cbc:Line>'.$_POST['emp_dir1'].'</cbc:Line>
               </cac:AddressLine>
               <cac:Country>
                  <cbc:IdentificationCode>PE</cbc:IdentificationCode>
               </cac:Country>
            </cac:RegistrationAddress>
         </cac:PartyLegalEntity>
      </cac:Party>
   </cac:AccountingSupplierParty>	
		
	<cac:AccountingCustomerParty>
      <cac:Party>
         <cac:PartyIdentification>
            <cbc:ID schemeID="'.$_POST['tipcom_cli'].'">'.$_POST['cli_numdoc'].'</cbc:ID>
         </cac:PartyIdentification>
         <cac:PartyLegalEntity>
            <cbc:RegistrationName>'.$_POST['cli_nom'].'</cbc:RegistrationName>
            <cac:RegistrationAddress>
               <cac:AddressLine>
                  <cbc:Line>'.$_POST['cli_dir'].'</cbc:Line>
               </cac:AddressLine>
               <cac:Country>
                  <cbc:IdentificationCode>PE</cbc:IdentificationCode>
               </cac:Country>
            </cac:RegistrationAddress>
         </cac:PartyLegalEntity>
      </cac:Party>
   </cac:AccountingCustomerParty>
	
	 <cac:TaxTotal>
      <cbc:TaxAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($_POST['notcre_igv'])).'</cbc:TaxAmount>
      <cac:TaxSubtotal>
         <cbc:TaxableAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($_POST['notcre_valnot'])).'</cbc:TaxableAmount>
         <cbc:TaxAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($_POST['notcre_igv'])).'</cbc:TaxAmount>
          <cac:TaxCategory>
                <cbc:ID schemeID="UN/ECE 5305" schemeName="Tax Category Identifier" schemeAgencyName="PE:SUNAT">S</cbc:ID>
                <cac:TaxScheme>
                    <cbc:ID schemeID="UN/ECE 5153" schemeAgencyID="6">9997</cbc:ID>
                    <cbc:Name>EXO</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['notcre_tot'])).'</cbc:PayableAmount>
   </cac:LegalMonetaryTotal> ';
	
	$i=1;

foreach($_POST['can'] as $indice=>$can)
{
		
		$item_id=$_POST['pro_id'][$indice];
		$item_nom=  str_ireplace('&','y',$_POST['art'][$indice] );
		$uniabr=$_POST['notdet_uni'][$indice];
		
$item_valuni=moneda_mysql($_POST['valuni'][$indice]);
$item_preuni=moneda_mysql($_POST['preuni'][$indice]);
$item_igv = 0;

$item_pretot=$item_preuni * $can;
$item_igvtot=$item_igv * $can;

		
		
$xml .='
<cac:CreditNoteLine>  
	<cbc:ID>'.$i.'</cbc:ID>
      <cbc:CreditedQuantity unitCode="'.$uniabr.'">'.$can.'</cbc:CreditedQuantity>
      <cbc:LineExtensionAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($item_pretot)).'</cbc:LineExtensionAmount>
      <cac:PricingReference>
         <cac:AlternativeConditionPrice>
            <cbc:PriceAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money4($item_preuni)).'</cbc:PriceAmount>
            <cbc:PriceTypeCode>01</cbc:PriceTypeCode>
         </cac:AlternativeConditionPrice>
      </cac:PricingReference>

	<cac:TaxTotal>
	<cbc:TaxAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($item_igvtot)).'</cbc:TaxAmount>
	<cac:TaxSubtotal>
	<cbc:TaxableAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($item_pretot)).'</cbc:TaxableAmount>
	<cbc:TaxAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($item_igvtot)).'</cbc:TaxAmount>
	<cac:TaxCategory>
                    <cbc:Percent>0</cbc:Percent>
                    <cbc:TaxExemptionReasonCode listAgencyName="PE:SUNAT" listName="Afectacion del IGV"
					listURI="urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo07">20</cbc:TaxExemptionReasonCode>
                    <cac:TaxScheme>
                        <cbc:ID schemeID="UN/ECE 5153" schemeName="Codigo de tributos" schemeAgencyName="PE:SUNAT">9997</cbc:ID>
                        <cbc:Name>EXO</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_id.'</cbc:ID>
         </cac:SellersItemIdentification>
    </cac:Item>
    <cac:Price>
         <cbc:PriceAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money4($item_preuni)).'</cbc:PriceAmount>
    </cac:Price>

</cac:CreditNoteLine>';	
	$i++;
	}
$xml .='</CreditNote>';

$fac = new DOMDocument;
$fac->standalone         = false;
$fac -> preserveWhiteSpace = true;
$fac->loadXML($xml);
//Save XML as a file
$nom_xml = $_POST['emp_ruc'].'-07-'.$_POST['notcre_numdoc'].'.xml';
$archivo = '../docsinfirma/'.$nom_xml;
$fac->save($archivo);

$data["url"] = dirname()."../../docgeneracion/firmar.php";
$data["arc"] = $archivo;
$data["tipcom"] = '07';

echo json_encode($data);

?>