<?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>
            <cac:TaxScheme>
               <cbc:ID>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['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] );
		$preuni=$_POST['preuni'][$indice];
		$valuni=$_POST['valuni'][$indice];
		$valtot=$_POST['valtot'][$indice];
		$uniabr=$_POST['notdet_uni'][$indice];
		$igvvaltot = $valtot * 0.18;
		
		
		
$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($valtot)).'</cbc:LineExtensionAmount>
      <cac:PricingReference>
         <cac:AlternativeConditionPrice>
            <cbc:PriceAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($preuni)).'</cbc:PriceAmount>
            <cbc:PriceTypeCode>01</cbc:PriceTypeCode>
         </cac:AlternativeConditionPrice>
      </cac:PricingReference>

	<cac:TaxTotal>
	<cbc:TaxAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($igvvaltot)).'</cbc:TaxAmount>
	<cac:TaxSubtotal>
	<cbc:TaxableAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($valtot)).'</cbc:TaxableAmount>
	<cbc:TaxAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($igvvaltot)).'</cbc:TaxAmount>
	<cac:TaxCategory>
	<cbc:Percent>18</cbc:Percent>
	<cbc:TaxExemptionReasonCode>10</cbc:TaxExemptionReasonCode>
	<cac:TaxScheme>
	<cbc:ID>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_id.'</cbc:ID>
         </cac:SellersItemIdentification>
    </cac:Item>
    <cac:Price>
         <cbc:PriceAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($valuni)).'</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"] = "../../docgeneracion/firmar.php";
$data["arc"] = $archivo;
$data["tipcom"] = '07';

echo json_encode($data);

?>