<?PHP 
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
$com_id=$_GET['id'];

    $sumador_total=0;
    $neto=0;
    $total_retencion=0;
    $total_cargo_adicional=0;
    $sql=mysqli_query($con, "SELECT * from compra_detalle where compras_com_id='$com_id' and empresa='$empresa'");
    while ($rew=mysqli_fetch_array($sql))
    {
        $comd_cantidad=$rew['comd_cantidad'];
        $comd_precio=$rew['comd_precio'];
        $comd_iva=$rew['comd_iva'];
        $comd_flete=$rew['comd_flete'];
        $cargo_adicional=$comd_flete*$comd_cantidad;
        $precio_total=$comd_precio*$comd_cantidad;
        $iva=($precio_total*$comd_iva)/100;
        $neto+=$precio_total;//Sumador
        $sumador_total+=$precio_total+$iva;//Sumador
        $total_cargo_adicional+=$cargo_adicional;
    }

    $ret=mysqli_query($con,"SELECT * FROM retencion_compra where empresa='$empresa' and compras_com_id='$com_id'") or die(mysqli_error($con)); 
    $rets=mysqli_fetch_array($ret);
    if (isset($rets['ret_porcentaje'])) 
    {
       $total_retencion=($ret_porcentaje=$rets['ret_porcentaje']/100)*$neto;
    }

    $total_real=$sumador_total+$total_cargo_adicional-$total_retencion;

    $sql=mysqli_query($con, "SELECT sum(coab_abono) as coab_abono from compra_abonos where compras_com_id='$com_id' and empresa='$empresa'");
    $sql1=mysqli_fetch_array($sql);
    $coab_abono=$sql1['coab_abono'];
    $saldo=$total_real-$coab_abono;
?>
            
           
                <div  class="col-md-12">
                    <div class="tabbable">
                        <ul class="nav nav-tabs">
                            <li class="active"><a href="#DETALLE" data-toggle="tab">DETALLE</a> </li>
                            <?php $sql1=mysqli_query($con, "SELECT count(coab_id) as abonos from  compra_abonos where compras_com_id='$com_id' ");
                            $row1=mysqli_fetch_array($sql1);
                            $abonos=$row1["abonos"]; 
                            if ($abonos>0) { ?>
                            <li><a href="#ABONOS" data-toggle="tab">ABONOS</a></li> 
                            <?php } ?>
                        </ul> 
                        <div class="tab-content"> 
                            <div class="tab-pane active" id="DETALLE">
                                <div class="table-responsive">
                                    <table class="table table-bordered table-hover table-striped">
                                        <thead>
                                          <tr>
                                            <th>CODIGO</th>
                                            <th>CANT.</th>
                                            <th>DESCRIPCION</th>
                                            <th><span class="pull-right">PRECIO UNIT.</span></th>
                                            <th><span class="pull-right">IMP</span></th>
                                            <th><span class="pull-right">DESC</span></th>
                                            <th><span class="pull-right">PRECIO TOTAL</span></th>
                                          </tr>
                                        </thead>
                                        <tbody  class="buscar">
                                    <?php
                                        $sumador_total=0;
                                        $sumador_iva=0;
                                        $sumador_descuento=0;
                                        $total_cargo_adicional=0;
                                        $sql=mysqli_query($con, "SELECT * from  compra_detalle where compras_com_id='$com_id' ");
                                        while ($row=mysqli_fetch_array($sql))
                                          {
                                              $id=$row["comd_id"];
                                              $comd_id=$row["comd_id"];
                                              $comd_referencia=$row['comd_referencia'];
                                              $comd_nombre=$row['comd_nombre'];
                                              $comd_cantidad=$row['comd_cantidad'];
                                              $comd_precio=$row['comd_precio'];
                                              $comd_iva=$row['comd_iva'];
                                              $comd_desc_desp_iva=$row['comd_desc_desp_iva'];
                                              $comd_flete=$row['comd_flete'];
                                              $cargo_adicional=$comd_flete*$comd_cantidad;
                                              $precio_total=$comd_precio*$comd_cantidad;
                                              $iva=($precio_total*$comd_iva)/100;
                                              $sumador_iva+=$iva;//Sumador
                                              $sumador_total+=$precio_total;//Sumador
                                              $total_cargo_adicional+=$cargo_adicional;
                                              $sumador_descuento+=$comd_desc_desp_iva;
                                    ?>
                                        <tr>
                                            <td><?php echo $comd_referencia;?></td>
                                            <td><?php echo $comd_cantidad;?></td>
                                            <td><?php echo $comd_nombre;?></td>
                                            <td><span class="pull-right"><?php echo number_format($comd_precio,0);?></span></td>
                                            <td><span class="pull-right"><?php echo number_format($iva,0);?></span></td>
                                            <td><span class="pull-right"><?php echo number_format($comd_desc_desp_iva,0);?></span></td>
                                            <td><span class="pull-right"><?php echo number_format($precio_total,0);?></span></td>
                                        </tr>  
                                    <?php } ?>
                                        <tr>
                                            <td colspan=6><span class="pull-right">SUB TOTAL $</span></td>
                                            <td><span class="pull-right"><strong><?php echo number_format($sumador_total,0);?></strong></span></td>
                                        </tr>
                                        <tr>
                                            <td colspan=6><span class="pull-right">IMPUESTO $</span></td>
                                            <td><span class="pull-right"><strong><?php echo number_format($sumador_iva,0);?></strong></span></td>
                                        </tr>
                                        <tr>
                                            <td colspan=6><span class="pull-right">DESC. CONDIC. $</span></td>
                                            <td><span class="pull-right"><strong><?php echo number_format($sumador_descuento,0);?></strong></span></td>
                                        </tr>
                                        <tr>
                                            <td colspan=6><span class="pull-right">RETENCION $</span></td>
                                            <td><span class="pull-right"><strong><?php echo number_format($total_retencion,0);?></strong></span></td>
                                        </tr>
                                         <tr>
                                            <td colspan=6><span class="pull-right">CARGO ADICIONAL $</span></td>
                                            <td><span class="pull-right"><strong><?php echo number_format($total_cargo_adicional,0);?></strong></span></td>
                                        </tr>
                                        <tr>
                                            <td colspan=6><span class="pull-right">TOTAL $</span></td>
                                            <td><span class="pull-right"><strong><?php echo number_format($sumador_total+$sumador_iva+$total_cargo_adicional-$total_retencion,0);?></strong></span></td>
                                        </tr>
                                        </tbody> 
                                    </table>
                                </div><!-- /.form-group -->
                                <a href="libraries/php_excel/excel_compra.php?com_id=<?php echo $com_id; ?>" class="btn btn-ms btn-success " > <i class="btn-icon-only icon-download"></i> Descargar Excel</a>
                                
                                <a href="libraries/pdf/pdf_reportes/compras_detalles.php?com_id=<?php echo $com_id; ?>"  class="btn btn-ms btn-danger "  target=”_blank”> <i class="btn-icon-only icon-download"></i> Descargar Pdf</a>
                                <a href="I_etiquetas.php?com_id=<?php echo $com_id; ?>"  class="btn btn-ms btn-info" onclick="imprimir_etiquetas('<?php echo  $com_id; ?>')" > <i class="btn-icon-only icon-print"></i> Imprimir Etiquetas</a>
                            </div>

                          
                            <div class="tab-pane" id="ABONOS">
                                <div id="resultados_abo"></div>  
                                <div id="tabla_abo"> </div>          
                            </div>
                        
                        </div>
                              
                        </div><!-- /.modal-content -->
                    </div>
  <input type="hidden" name="com_id" id="com_id" value="<?php echo $com_id;?>">    

<style type="text/css">
/*Supongamos que nuestro contenedor mide 150px x 150px*/
.contenedor{
    background-color: white;
    width:100px;
    height:30px;
}
/*Aplicamos la propiedad object-fit cover, ajustar su tamaño y no perder la proporcion de nuestra imagen*/
.marca{
    object-fit: scale-down;
    width:100%;
    height:100%;
}

</style>

<script type="text/javascript">

function eliminar_abono (coab_id)
{
    if (confirm("Realmente deseas eliminar el abono")){ 
    $.ajax({
        type: "GET",
        url: "php_compras/eliminar_abono.php",
        data: "coab_id="+coab_id,
        beforeSend: function(objeto){
         $('#resultados_abo').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        $("#resultados_abo").html(datos);
        loadTablaAbono();
        load(1);
        }
        });
    }
}

function imprimir_etiquetas (com_id)
{ 
    $.ajax({
        type: "GET",
        url: "php_compras/imprimir_etiquetas.php",
        data: "com_id="+com_id,
        });
}


function loadTablaAbono(){
     var com_id=$("#com_id").val();
    $.get("php_compras/tabla_abonos.php?com_id="+com_id,function(data){
    $("#tabla_abo").html(data);
    })
}
loadTablaAbono();

</script>