<?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>
   
	 <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=$_POST['art'][$indice];
		$preuni=$_POST['preuni'][$indice];
		$valuni=$_POST['valuni'][$indice];
		$valtot=$_POST['valtot'][$indice];
		$igvvaltot = $valtot * 0.18;
		
		
		
$xml .='
<cac:CreditNoteLine>  
	<cbc:ID>'.$i.'</cbc:ID>
      <cbc:CreditedQuantity unitCode="NIU">'.$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>';
$xml ='
	<DebitNote xmlns="urn:oasis:names:specification:ubl:schema:xsd:DebitNote-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:UBLExtension>
   </ext:UBLExtensions>
   
    <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
   <cbc:CustomizationID>2.0</cbc:CustomizationID>
   <cbc:ID>'.$_POST['notdeb_numdoc'].'</cbc:ID>
   <cbc:IssueDate>'.$_POST['notdeb_fec'].'</cbc:IssueDate>
   <cbc:DocumentCurrencyCode>'.$_POST['mon_let'].'</cbc:DocumentCurrencyCode>
  
   <cac:DiscrepancyResponse>
      <cbc:ReferenceID>'.$_POST['ven_numdoc'].'</cbc:ReferenceID>
      <cbc:ResponseCode>'.$_POST['notdeb_tip'].'</cbc:ResponseCode>
      <cbc:Description>'.$_POST['notdeb_mot'].'</cbc:Description>
   </cac:DiscrepancyResponse>
   
  
   <cac:BillingReference>
      <cac:InvoiceDocumentReference>
         <cbc:ID>'.$_POST['ven_numdoc'].'</cbc:ID>
         <cbc:DocumentTypeCode>'.$_POST['notdeb_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['notdeb_igv'])).'</cbc:TaxAmount>
      <cac:TaxSubtotal>
         <cbc:TaxableAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($_POST['notdeb_valnot'])).'</cbc:TaxableAmount>
         <cbc:TaxAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($_POST['notdeb_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:RequestedMonetaryTotal>
      <cbc:PayableAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($_POST['notdeb_tot'])).'</cbc:PayableAmount>
   </cac:RequestedMonetaryTotal>
  
  ';
  
  $i=1;
foreach($_POST['art'] as $indice=>$art)
{
		$item_nom=$_POST['art'][$indice];
		$valuni=$_POST['valuni'][$indice];
		$can = 1;
		$preuni=$valuni*1.18;
		$valtot=$valuni * $can;
		$igvvaltot = $valtot * 0.18;		
  $xml.='
  <cac:DebitNoteLine>
    	
	<cbc:ID>'.$i.'</cbc:ID>
      <cbc:DebitedQuantity unitCode="NIU">'.$can.'</cbc:DebitedQuantity>
      <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:Item>
	
    <cac:Price>
         <cbc:PriceAmount currencyID="'.$_POST['mon_let'].'">'.moneda_mysql(formato_money($valuni)).'</cbc:PriceAmount>
    </cac:Price>
	
  </cac:DebitNoteLine>
  ';
  $i++;
}
  
  $xml.=' </DebitNote> ';
$fac = new DOMDocument;
$fac->standalone         = false;
$fac -> preserveWhiteSpace = true;
$fac->loadXML($xml);
//Save XML as a file
$nom_xml = $_POST['emp_ruc'].'-08-'.$_POST['notdeb_numdoc'].'.xml';
$archivo = '../docsinfirma/'.$nom_xml;
$fac->save($archivo);
$data["url"] = dirname()."../../docgeneracion/firmar.php";
$data["arc"] = $archivo;
$data["tipcom"] = '08';
echo json_encode($data);
?>