<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
$com_id=$_GET['com_id'];
?>
<div class="table-responsive">
    <table class="table table-bordered table-hover table-striped">
        <tr>
            <th width="10%">CODIGO</th>
            <th width="10%">CANTIDAD</th>
            <th>DESCRIPCION</th>
            <th width="10%"><span class="pull-right">PRECIO UNIT</span></th>
            <th width="10%"><span class="pull-right">PRECIO UNIT IMP</span></th>
            <th width="8%"><span class="pull-right">DESC. DESP. DE IMP</span></th>
            <th width="10%"><span class="pull-right">PRECIO TOTAL</span></th>
            <th width="10%"></th>
        </tr>
            <?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_unit=($comd_precio*$comd_iva)/100;
              $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($comd_precio+$iva_unit,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>
            <td>  
                <a data-id="<?php echo $id;?>" class="btn btn-edit btn-xs btn-info"> <i class="btn-icon-only icon-edit"></i></a>
                  <?php if ($negocio_neg_id==1){?>
                    <a href="#" data-id="<?php echo $id;?>" class="btn btn-xs btn-default btn-serial"><i class="btn-icon-only icon-barcode"></i></a>
                   <?php } ?>
                <a href="#" id="del-<?php echo $row["comd_id"];?>" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
                <script>
                    $("#del-"+<?php echo $row["comd_id"];?>).click(function(e){
                    e.preventDefault();
                    p = confirm("Estas seguro?");
                    if(p){
                    $.get("./php_compras/eliminar_mod.php","comd_id="+<?php echo $row["comd_id"];?>,function(data){
                    loadtabla();
                        });
                        }
                    });
                </script>
            </td>
        </tr>   
            <?php } ?>
        <tr>
            <td></td>
            <td colspan=5><span class="pull-right"><strong>NETO $</strong></span></td>
            <td><span class="pull-right"><strong><?php echo number_format($sumador_total,0);?></strong></span></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td colspan=5><span class="pull-right"><strong>IMPUESTO $</strong></span></td>
            <td><span class="pull-right"><strong><?php echo number_format($sumador_iva,0);?></strong></span></td>
            <td></td>
        </tr>
        <tr>
            <td><span class="pull-right"></span></td>
            <td colspan=5><span class="pull-right"><strong>DESC. CONDICIONAL</strong></span></td>
            <td><span class="pull-right"><strong><?php echo number_format($sumador_descuento,0);?></strong></span></td>
            <td></td>
        </tr>
                 <tr>
            <td><span class="pull-right"></span></td>
            <td colspan="5"><span class="pull-right"><strong>RETENCION</strong></span></td>
            <td><span class="pull-right"><strong>
                <?php
                    $retencion=0;
                    $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'])) 
                    {
                        $retencion=($ret_porcentaje=$rets['ret_porcentaje'])/100*$sumador_total;
                        echo number_format($retencion,0);
                    }
                ?>
            </strong></span></td>
            <td>
                <?php
                    $ret=mysqli_query($con,"SELECT * FROM retencion_compra where empresa='$empresa' and compras_com_id='$com_id'") or die(mysqli_error($con)); 
                    while ($rets=mysqli_fetch_array($ret))
                    {
                        $ret_porcentaje=$rets['ret_porcentaje'];
                    ?>
                    <h5> (<?php echo $ret_porcentaje;?>%)<a href="#" id="re-<?php echo $rets["ret_id"];?>"><i class="btn-icon-only icon-remove"></i></a></h5>
                    <script>
                    $("#re-"+<?php echo $rets["ret_id"];?>).click(function(e){
                      e.preventDefault();
                      p = confirm("Estas seguro?");
                      if(p){
                        $.get("./php_compras/eliminar_retencion_mod.php","ret_id="+<?php echo $rets["ret_id"];?>,function(data){
                        });
                         loadtabla();
                      }
                      loadtabla();
                    });
                    </script>
                <?php }?>
            </td>
        </tr>
        <tr>
            <td></td>
            <td colspan=5><span class="pull-right"><strong>CARGO ADICIONAL $</strong></span></td>
            <td><span class="pull-right"><strong><?php echo number_format($total_cargo_adicional,0);?></strong></span></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td colspan=5><span class="pull-right"><strong>TOTAL $</strong></span></td>
            <td><span class="pull-right"><strong><?php echo number_format(($sumador_total+$sumador_iva+$total_cargo_adicional)-$retencion-$sumador_descuento,0);?></strong></span></td>
            <td></td>
        </tr>
    </table>
</div> <!-- /.table-responsive -->
<script>
    $(".btn-edit").click(function(){
        id = $(this).data("id");
        $.get("./php_compras/form_modificar_m.php","id="+id,function(data){
            $("#form-edit").html(data);
        });
        $('#editModal').modal('show');
    });
</script>
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">ACTUALIZAR</h4>
            </div>
            <div class="modal-body">
                <div id="form-edit"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script>
    $(".btn-serial").click(function(){
        id = $(this).data("id");
        $.get("php_compras/form_seriales.php","id="+id,function(data){
            $("#form-serial").html(data);
        });
        $('#seriale').modal('show');
    });
</script>
  
 <div class="modal fade" id="seriale" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">MODIFICAR SERIALES</h4>
            </div>
            <div class="modal-body">
                <div id="form-serial"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script>
//retencion
$( "#form_retencion" ).submit(function( event ) {
$('#btn_ret').attr("disabled", true);
var parametros=$(this).serialize();
   $.ajax({
      type: "POST",
      url: "php_compras/registrar_retencion_mod.php",
      data: parametros,
        beforeSend: function(objeto){
         $('#tabla').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
      success: function(datos){
      $("#tabla").html(datos);
      $('#btn_ret').attr("disabled", false);
      loadtabla();
      }
  });
  event.preventDefault();
})
</script>

    <div class="col-md-9" style="padding:0px;"></div>
    <div class="col-md-3" style="padding:0px;">
        <form action="" method="post" id="form_retencion" name="form_retencion" class="">
            <div class="input-group">
                <select class="form-control input-sm" name="retencion" required>
                    <?php
                        $result_1=mysqli_query($con,"SELECT * FROM retencion WHERE empresa='$empresa'");  
                        while ($row_1=mysqli_fetch_array($result_1)) 
                        {   ?>
                            <option value='<?php echo $row_1['ret_id']; ?>' ><?php echo $row_1['ret_nombre']; ?> (<?php echo $row_1['ret_porcentaje']; ?> %)</option> 
                    <?php } ?>
                </select>
                <input type="hidden" name="compra_id" value="<?php echo $com_id ?>">
                <span class="input-group-btn">
                <button name="btn_ret" id="btn_ret" type="submit" class="btn btn-warning  btn-sm" >Retención</button></span>
            </div>
        </form>
    </div></br></br>
