<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: 50%;" align="LEFT">
            <?php
            if($_GET['ad']==1){
                $textoImagen = ENLACE_SERVIDOR."_documentos/logos/".$entidadGeneral."/logo.png";
            }else{
                $textoImagen = ENLACE_SERVIDOR."_documentos/logos/".$entidadGeneral."/logo.png";
            }
            
            
            if(file_exists($textoImagen)){ ?>
                <img src="<?php echo $textoImagen ?>" style="width:150px" ><br>
                 <strong  style="font-weight: bold;font-size:20px;color:grey;"  ><?php  echo $XML_Hacienda->electronica_nombre  ;   ?></strong>
                <?php
            }else{
                ?>
                <strong  style="font-weight: bold;font-size:20px;color:grey;"  > <?php echo $XML_Hacienda->electronica_nombre  ;   ?></strong><br>
                <?php
            }   
                ?>
        
        
        
           
        </Td>

      
        <td valign="top"  align="right" style="width: 55%; color: #444444;">
           <span style="font-weight: bold;font-size:20px;color:black;"><?php echo  $tipo.' '.$factura->referencia; ?></span>
           <br>
         </td>
    </tr>
        
    </table>


   
    <table cellspacing="0"   style="width: 95%; text-align: center; font-size: 14px">
        <tr>
            <td valign="top"    style="width: 45%;" align="LEFT">
       
        

            
 <?php echo  $XML_Hacienda->electronica_nombre_comercial;  ?><br>          
<?php echo  $XML_Hacienda->direccion_PDF;  ?>   <br>
Cédula: <?php echo $XML_Hacienda->electronica_identificacion_numero  ; ?> <br>
Teléfono: (506) <?php echo $XML_Hacienda->electronica_telefono; ?>  <br>
Email: <?php echo $XML_Hacienda->electronico_correo; ?>  <br>
             </td>
            <td style="width: 20%;"> </td>
            <td valign="top"  align="right" style="width: 40%; color: #444444;">
            <?php if ($factura2->estado==3){ echo "<b style='color:red'>Factura <b><ins> NULA </ins></b>";}  ?>
            <hr>

          
            
            
<?php
 if ($factura->estado==0){$estado='<span class="label label-warning">Validada</span>';}
   if  ($factura->electronica_enviada==1){ $estado='<span class="label label-success">Enviada </span>';}
      if  ($factura->electronica_resultado==1 and $factura->electronica_error== 1 ){ $estado='<span style="color:red;">Rechazada</span>';}
          if  ($factura->electronica_resultado==1 and $factura->electronica_error== 0  ){ $estado='<span class="label label-success">Validada</span>';}
                if ($factura->estado==1 and $factura->electronica_enviada==0) {  $estado='<span class="label label-success">Enviando ...</span>';}
                
                
     ?>       
            
            <?php 
            //echo "<b>Fecha Emisión:</b> <br>". date('d-m-Y h:i A',strtotime($factura->fecha_creacion_server))."<br>";
            echo "Fecha Vencimiento:". date('d-m-Y',strtotime($factura->fecha_vencimiento))."<br>";
            ///echo "<b>Estado</b>           :". $estado."<br>";


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



            ?>
            <hr>
            <?php if($factura->tipo != 0){ ?>
            <!--Tipo Factura: --> 
            
            <?php
                switch($factura->tipo){
                    case 0 : //echo "Contado";
                    break;
                    case 1 : //echo "Crédito";
                    break;
                }
                
                /*
                $sql = "SELECT * FROM `diccionario_condiciones_venta` where rowid = :rowid";
                $db3 = $dbh->prepare($sql);
                $db3->bindValue('rowid', $factura->tipo, PDO::PARAM_INT);
                $db3->execute();
                $tipoFactura = $db3->fetch(PDO::FETCH_OBJ);
                echo $tipoFactura->etiqueta;
                */
            ?>
            <?php } ?>
            
            </td>
        </tr>
    </table>
    <br>
    <br>
 <?php
 
 $cantidadExoneraciones = 0;
    $sql="Select * from fi_facturas_detalle where fk_factura = ? ";
                 $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($entidadGeneral == 89){
    $simbolo=''.$obj['label']." - ".$obj['ref'].'';
}

 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'> ".($factura->moneda_simbolo)."  ". numero_simple(($obj['descuento_valor_final']), 2, ',', ' ') ." $descuento_txt  </td>";
     
 } else {
     
     $descuento= "<td  style='width:15%;'  ></td>";
     
 }
 
    if($obj['exoneracion'] == 1){
        $textoExoneracion = " * ";
        $cantidadExoneraciones += 1;
    }else{
        $textoExoneracion = "";
    }
 $tr.="<tr    >
  <td style='width: 30%; text-align: left; word-wrap: break-all;' >".$simbolo."<br><i>".$obj['label_extra']."</i></td> 
  <td style='width: 5%; text-align: center' > ".$obj['cantidad']."</td> 
   
 
  <td style='width: 15%; text-align: center'> ".($factura->moneda_simbolo)."  ". numero_simple(($obj['precio_original']), 2, ',', ' ') ."</td>
  
   $descuento
    
    

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

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


$conteo=$total_tr-$conteo;

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

?>

<?php if ($factura->moneda==2){ $moneda='Tipo Cambio  &cent; '.numero_simple($factura->moneda_tipo_cambio); } else { $moneda=""; } ?> 
<br><span style='font-size: 10pt;'><?php echo "Factura emitida en ".$factura->moneda_text." ".$moneda; ?><?php echo ($factura->moneda == 1)? '(CRC)':'(USD)' ?></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; ?>

   </table>

    

 
 <page_footer>
    <b>NOTA:</b>
    
    <?php
     if($cantidadExoneraciones > 0){
        $sql = "Select * from fi_terceros_exonerar where fk_tercero = ".$factura->tercero ;
        $db = $dbh->prepare($sql);
        $db->execute();
        $infoExo = $db->fetch(PDO::FETCH_OBJ);
        echo "[*] productos exonerados por: Documento (".$infoExo->numeroDocumento.") emitida a esta entidad.<br>" ;  
        }
     ?>
    
    <?php echo $factura->nota ?><br>
    <!--
     <a href="<?php // echo ENLACE_WEB."comprobante/".md5($factura->id)."/".$clave; ?>" style='color:grey'>Descargar Comprobante</a><br>
     <a href="<?php // echo ENLACE_WEB."factura/".md5($factura->id)."/".$clave; ?>" style='color:grey' >Descargar Factura</a><br>
     -->

    <table style="width:100%">
        <tr>
            <td>    <qrcode value="<?php // echo ENLACE_WEB."factura/".md5($factura->id)."/".$clave; ?>" style="width: 30mm;"></qrcode> 
                    <qrcode value="<?php echo ENLACE_WEB ?>" 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 ($factura->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 ($factura->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 ($factura->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 ($factura->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 ($factura->moneda_simbolo)." ". number_format($total, 2, ',', ' '); ?>  </th>
        </tr>

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




<?php   

if ($conf->configuracion['mostrar_forma_pago_ticket'] and ($factura->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 = ".$factura->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;">
RESOLUCIÓN 11-97 DE LA D.G.T.D. GACETA N° 171 DEL 5-09-97</span> <br> 

</page_footer>
        
</page>