<page backcolor="#FEFEFE" backimg="./res/bas_page.png" backimgx="center" backimgy="bottom" backimgw="100%" backtop="0" backbottom="30mm" footer="date;heure;page" style="font-size: 12pt">
    <bookmark title="Lettre" level="0" ></bookmark>
   
    <table cellspacing="0" border="0"   style="width: 95%; text-align: center; font-size: 14px">
    <tr>
    
        <Td valign="top"  style="width: 40%;" align="LEFT">
           <img src="../../_documentos/28c8edde3d61a0411511d3b1866f0636/8a6dbf554746d73e62de6e21509b6ee2/empresa/cesoft.jpeg" style="width:100px" >
            <strong  style="font-weight: bold;font-size:20px;color:grey;"  > <?php   $XML_Hacienda->electronica_nombre  ;   ?></strong>
            <p><b style='color:red;'>CESOFT </b> CARLOS ENRIQUE DE JESUS ESPINOZA CHAVARRIA</p>
        </Td>
    
     
        <td valign="top"  align="right" style="width: 50%; color: #444444;">
           <span style="font-weight: bold;font-size:20px;color:black;"><?php echo "Nota de ".$nota->tipo." ".$nota->referencia; ?></span>
           <br>
           <?php
            if($nota->tipo == 'credito'){
                $numero = "03";
            }
            
            if($nota->tipo == 'debito'){
                $numero = "02";
            }
            
            $sql = "SELECT concat(consecutivo_establecimiento,consecutivo_punto_venta,consecutivo_tipo_documento,consecutivo_consecutivo) as numConse 
                    FROM `electronica_digital_electronica` WHERE fk_entidad = ".$nota->entidad." and fk_factura = ".$id." and consecutivo_tipo_documento = '".$numero."'";
            $con = $dbh->prepare($sql);
            $con->execute();
            $consecutivo = $con->fetch(PDO::FETCH_OBJ);
            
            
            $sql = "SELECT concat(consecutivo_establecimiento,consecutivo_punto_venta,consecutivo_tipo_documento,consecutivo_consecutivo) as numConse 
                    FROM `electronica_digital_electronica` WHERE fk_entidad = ".$nota->entidad." and fk_factura = ".$nota->fk_documento_modifica." and consecutivo_tipo_documento = '01'";
            $con = $dbh->prepare($sql);
            $con->execute();
            $FacturaModifica = $con->fetch(PDO::FETCH_OBJ);
            
            
            
            
           ?>
           <strong> Clave <?php echo $consecutivo->numConse ?></strong>
         </td>
    </tr>
        
    </table>


   
    <table cellspacing="0"   style="width: 95%; text-align: center; font-size: 14px">
        <tr>
            <td valign="top"    style="width: 40%;" align="LEFT">
 <?php echo  $XML_Hacienda->electronica_nombre_comercial;  ?><br>          
<?php echo  $XML_Hacienda->direccion_PDF;  ?>   <br>
 Email: <?php echo $XML_Hacienda->electronico_correo; ?>  <br>
Cédula: <?php echo $XML_Hacienda->electronica_identificacion_numero  ; ?> <br>
Teléfono: (506) <?php echo $XML_Hacienda->electronica_telefono; ?>  <br>
             </td>
            <td style="width: 20%;"> </td>
            <td valign="top"  align="right" style="width: 40%; color: #444444;">
            <?php if ($nota2->estado==3){ echo "<b style='color:red'>Nota de Crédito <b><ins> NULA </ins></b>";}  ?>

          
            
            
<?php
 if ($nota->estado==0){$estado='<span class="label label-warning">Borrador</span>';}
   if  ($nota->electronica_enviada==1){ $estado='<span class="label label-success">Enviada </span>';}
      if  ($nota->electronica_resultado==1 and $nota->electronica_error== 1 ){ $estado='<span style="color:red;">Rechazada</span>';}
          if  ($nota->electronica_resultado==1 and $nota->electronica_error== 0  ){ $estado='<span class="label label-success">Aceptada</span>';}
                if ($nota->estado==1 and $nota->electronica_enviada==0) {  $estado='<span class="label label-success">Enviando ...</span>';}
                
                
     ?>       
            
            <?php 
            echo "Fecha            :". date('d-m-Y',strtotime($nota->fecha))."<br>";
            echo "Fecha Vencimiento:". date('d-m-Y',strtotime($nota->fecha_vencimiento))."<br>";
            echo "Estado           :". $estado."<br>";


            $cliente->fetch($nota->tercero);
            echo $cliente->nombre;
            echo $cliente->apellidos;
            echo "<br>".$cliente->cedula;
            echo "<br>".$cliente->email;



            ?>
            </td>
        </tr>
    </table>
    <br>
    <br>
 <?php
    $sql="Select * from fi_notas_credito_detalle where fk_nota = ? ";
                 $db=$dbh->prepare($sql);
                 $db->bindValue(1,$id  ,PDO::PARAM_INT);
                 $db->execute();

                  //   print_r($dbh->errorInfo());           
                $contado=0;
                $tr="";

$subtotal=0;
$impuesto=0;
$total=0;
$eliminar="";
$conteo=0;
while($obj=$db->fetch(PDO::FETCH_ASSOC)){ 
$conteo++;

if ($obj['tipo_impuesto'] > 0)
 { $obj['impuestot']='<span class="label label-warning">Con Impuesto '.$obj['tipo_impuesto'].'</span>';}
                   else { $obj['impuestot']='<span class="label label-warning">Sin Impuesto</span>';}

//-------------------
$subtotal +=($obj['subtotal']*$obj['cantidad']);
$impuesto +=$obj['impuesto'];
$total    +=$obj['total'];
$descuento_aplicado+=$obj['descuento_valor_final']*$obj['cantidad'];
 

if ($obj['fk_producto']>0){   $simbolo=''.$obj['label'].'';'  ';}
 else {$simbolo=''.$obj['label'].''; } 


 if ($obj['descuento_aplicado'] > 0){

      if ($obj['descuento_tipo'] === "porcentual"  ){  $descuento_txt= "(". $obj['descuento_aplicado']."%) ";   }
         else {   $descuento_txt= "";  }

      $descuento="  <td style='width: 15%; text-align: center'> ".($nota->moneda_simbolo)."  ". numero_simple(($obj['descuento_valor_final']), 2, ',', ' ') ." $descuento_txt  </td>";
     
 } else {
     
     $descuento= "<td  style='width:15%;'  ></td>";
     
 }
 

 $tr.="<tr    >
  <td style='width: 30%; text-align: left' >".$simbolo."</td> 
  <td style='width: 5%; text-align: center' > ".$obj['cantidad']."</td> 
   
 
  <td style='width: 15%; text-align: center'> ".($nota->moneda_simbolo)."  ". numero_simple(($obj['precio_original']), 2, ',', ' ') ."</td>
  
   $descuento


   <td style='width: 15%; text-align: center'>  ".($nota->moneda_simbolo)." ".numero_simple(($obj['impuesto']), 2, ',', ' ')."</td>
  <td  style='width: 20%; text-align: right'>   ".($nota->moneda_simbolo)."  ".numero_simple(($obj['total']), 2, ',', ' ')." </td>
  </tr>";
                                   
       
                                    
                                   
                                   
                 
                                    }// fin del While---

$total_tr=45;
if ($conf->configuracion['mostrar_forma_pago_ticket'] and ($nota->pagado > 0 )){$total_tr=40; }


$conteo=$total_tr-$conteo;

for ($i = 1; $i <= $conteo; $i++) {
    $tr.="<tr><td colspan='5'></td></tr>";
}

?>

<?php if ($nota->moneda==2){ $moneda='Tipo Cambio  &cent; '.numero_simple($nota->moneda_tipo_cambio); } else { $moneda=""; } ?> 
 
<br><span style='font-size: 10pt;'><?php echo "Esta Nota de ".$nota->tipo.", afecta a la Factura <strong>".$FacturaModifica->numConse."</strong> con fecha ".date('d-m-Y', strtotime($nota->factura_fecha))." "; ?> </span>
    <table cellspacing="1pt" border="0" style="width: 100%; border: solid 1px black; background: #E7E7E7; text-align: center; font-size: 10pt;">
   <tr>
           <th style="width: 30%;text-align: left">Descripción Item</th>
           <th style="width: 5%">Cant.</th>
            <th style="width: 15%">P.U</th>
            <th style="width: 15%">Descuento</th>
            <th style="width: 15%">Impuesto</th>
            <th style="width: 20%;text-align: right "  >Total</th>
         </tr>
    </table>
    

    <table  border="0"   cellspacing="5pt" style="width: 100%; border: solid 1px black; background: #F7F7F7; text-align: center; font-size: 10pt;">
        

   <?php echo $tr; ?>

    <?php
            if($nota->tipo == 'credito'){
                $tipoNota = "3";
            }
            
            if($nota->tipo == 'debito'){
                $tipoNota = "2";
            }
    ?>


   </table>



 <page_footer>
     
     <a href="<?php echo ENLACE_WEB."comprobante_nota/".md5($id)."/".$clave."/".$tipoNota; ?>" style='color:grey'>Descargar Comprobante</a><br>
     <a href="<?php echo ENLACE_WEB."nota_xml/".md5($id)."/".$clave; ?>" style='color:grey' >Descargar Nota <?php echo $nota->tipo ?></a><br>
     

    <table style="width:100%">
        <tr>
            <td>    <qrcode value="<?php echo ENLACE_WEB."nota_xml/".md5($id)."/".$clave; ?>" style="width: 30mm;"></qrcode> </td>
            
            <td>
                 
    <table cellspacing="3" style="width:170mm; border: solid 1px black; background: #E7E7E7; text-align: center; font-size: 11pt;">
       <?php if ($descuento_aplicado> 0 ){ ?>
        <tr>
            <th style="width: 50%; text-align: right;">SubTotal : </th>
            <th style="width: 50%; text-align: right;"><?php echo ($nota->moneda_simbolo)." ". number_format($subtotal + $descuento_aplicado  , 2, ',', ' '); ?></th>
        </tr>


        
        <tr>
            <th style="width: 50%; text-align: right;">Descuento : </th>
            <th style="width: 50%; text-align: right;"> <?php echo ($nota->moneda_simbolo)." ".number_format($descuento_aplicado , 2, ',', ' '); ?></th>
        </tr>
    <?php }  ?> 
    
   
        <tr>
            <th style="width: 50%; text-align: right;">SubTotal : </th>
            <th style="width: 50%; text-align: right;"><?php echo ($nota->moneda_simbolo)." ".number_format($subtotal, 2, ',', ' '); ?></th>
        </tr>


         <tr>
            <th style="width: 50%; text-align: right;">Impuesto : </th>
            <th style="width: 50%; text-align: right;"><?php echo ($nota->moneda_simbolo)." ".number_format($impuesto, 2, ',', ' '); ?></th>
        </tr>


  <tr style="font-size: 12pt;">
            <th style="width: 50%; text-align: right;">Total : </th>
            <th style="width: 50%; text-align: right;"><?php echo ($nota->moneda_simbolo)." ". number_format($total, 2, ',', ' '); ?>  </th>
        </tr>

     </table>
 
 </td></tr>
        
    </table>
    
<br>
 




<?php   

if ($conf->configuracion['mostrar_forma_pago_ticket'] and ($nota->pagado > 0 )){ 

              $sql="select 
              dp.label as forma ,
              p.monto 
              from fi_facturas_pagos p  
              left join diccionario_formas_pago dp on dp.rowid = p.forma_pago
           
              where p.fk_factura = ".$nota->id." ";

              $db=$dbh->prepare($sql);
              $db->execute();
              echo '
                <table cellspacing="1pt" border="0" style="width: 50%; border: solid 1px black; background: #E7E7E7; text-align: center; font-size: 10pt;">

              <tr><td style="width:100%;text-align:left;"  align="left" colspan="2">Pago(s) Recibido(s)</td></tr>';       

              while($pagos=$db->fetch(PDO::FETCH_ASSOC)){

              echo '<tr><td  style="width:50%;text-align:left;" >'.$pagos['forma'].'</td><td style="width:50%"  align="right" >'.numero_simple($pagos['monto'], 2, ',', ' ').'</td></tr>';       

              }

echo '</table>';
 } ?>
         <br>

        <span style="font-size:8pt;text-align: center;">
Emitido conforme lo establece la Resolución de Facturación Electr&oacute;nica ,<a href="http://facturacionpymes.com/files/DGT-R-48-2016.pdf">Número 0048-2016 </a> del 7 Octubre del 2016   <br>  segun  la dirección General de
Tributación Directa</span>        
</page_footer>
        
</page>