<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php'); 

if (isset($_POST['art_id'])){  $art_id=$_POST['art_id'];}
if (isset($_POST['art_referencia'])){ $art_referencia=$_POST['art_referencia'];}
if (isset($_POST['cantidad'])){ $cantidad=$_POST['cantidad'];}
if (isset($_POST['precio_venta'])){ $precio_venta=$_POST['precio_venta'];}
if (isset($_POST['art_iva'])){$art_iva=$_POST['art_iva'];}
if (isset($_POST['descuento'])){ $descuento=$_POST['descuento'];}
if (isset($_POST['nombre'])){$nombre=$_POST['nombre'];}

  
if (!empty($cantidad) )
{
$sqll=mysqli_query($con,"SELECT facd_art_id FROM fac_detalle_temp WHERE facd_art_id='$art_id' and empresa='$empresa' and usuario='$usu_id' ") ;
if($roww=mysqli_fetch_array($sqll))
    { 
    $can7=mysqli_query($con,"SELECT * FROM fac_detalle_temp WHERE facd_art_id='$art_id' and empresa='$empresa' and usuario='$usu_id' ") or die(mysqli_error($con));  
    while($dato=mysqli_fetch_array($can7))
    {
        $c=$dato['facd_cantidad'];
        $suma=$c+$cantidad;
    }
    $registros=mysqli_query($con,"UPDATE fac_detalle_temp Set facd_cantidad='$suma',facd_precio='$precio_venta' 
        Where facd_art_id='$art_id' and empresa='$empresa' and usuario='$usu_id' ") or die(mysqli_error($con));
    }
    else
    {   
        $insert_tmp=mysqli_query($con, "INSERT INTO fac_detalle_temp (facd_art_id,facd_referencia,facd_nombre,facd_cantidad,facd_iva,facd_desc,facd_precio,usuario,empresa) 
        VALUES ('$art_id','$art_referencia','$nombre','$cantidad','$art_iva','$descuento','$precio_venta','$usu_id','$empresa')");
    }

} 
?>

<div class="table-responsive">
<table class="table table-striped table-bordered" style="font-size: 9pt;">
<tr>
  <th width="7%">CODIGO</th>
  <th width="7%">CANT.</th>
  <th><span class="pull-center">DESCRIPCIÓN</span></th>
  <th width="10%"><span class="pull-right">PRECIO U.</span></th>

  <th width="7%"><span class="pull-right">DESC.%</span></th>
  <th width="10%"><span class="pull-right">PRECIO T</span></th>
  <th width="10%"></th>
</tr>
<?php

  $precio_total_sin_iva=0;
  $descuentof=0;
  $sumador_total=0;
  $total1=0;
  $iva_total=0;
  $sql=mysqli_query($con, "select * from  fac_detalle_temp where empresa='$empresa' and usuario='$usu_id'");
  while ($row=mysqli_fetch_array($sql))
  {
  $id=$row["facd_id"];
  $facd_id=$row["facd_id"];
  $facd_referencia=$row['facd_referencia'];
  $facd_cantidad=$row['facd_cantidad'];
  $facd_nombre=$row['facd_nombre'];
  $facd_precio=$row['facd_precio'];
  $facd_iva=$row['facd_iva'];
  $facd_descuento=$row['facd_desc'];

  $ivaf=$facd_iva/100;
  $precio_total=$facd_precio*$facd_cantidad;
  $iva=$facd_precio*$ivaf;

  $sin_iva=$facd_precio-$iva;
  $descuento=($facd_descuento/100)*$precio_total;
  $total_sin_iva=$sin_iva*$facd_cantidad;

  $descuentof+=$descuento;
  $total1+=$precio_total;
  $precio_total_sin_iva+=$total_sin_iva;

  $iva_total=$total1*$ivaf;
  $sumador_total=($precio_total_sin_iva+$iva_total)-$descuentof;

  ?>
    <tr>
      <td><?php echo $facd_referencia;?></td>
      <td><?php echo $facd_cantidad;?></td>
      <td><?php echo $facd_nombre;?></td>
      
      
      <td><span class="pull-right"><?php echo number_format($sin_iva,0);?></span></td>
      <td><span class="pull-right"><?php echo $facd_descuento;?></span></td>
      <td><span class="pull-right"><?php echo number_format($total_sin_iva,0);?></span></td>
      <td >
      <a data-id="<?php echo $id;?>" class="btn btn-edit btn-xs btn-warning"> <i class="btn-icon-only icon-edit"></i></a>
      <a href="#" id="del-<?php echo $row["facd_id"];?>" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
      
    <script>
    $("#del-"+<?php echo $row["facd_id"];?>).click(function(e){
      e.preventDefault();
      p = confirm("Estas seguro?");
      if(p){
        $.get("./php_facturacion_periodica/eliminar_articulo.php","facd_id="+<?php echo $row["facd_id"];?>,function(data){
          loadTabla();
        });
      }
    });
    </script></td>
    </tr>   
    <?php
  }
?>
<tr>
  <td colspan="5"><span class="pull-right"><strong>NETO $</strong></span></td>
  <td><span class="pull-right"><strong><?php echo number_format($precio_total_sin_iva,0);?></strong></span></td>
  <td></td>
</tr>
<tr>
  <td colspan="5"><span class="pull-right"><strong>DESCUENTO %</strong></span></td>
  <td><span class="pull-right"><strong><?php echo number_format($descuentof,0);?></strong></span></td>
  <td></td>
</tr>
<tr>
  <td colspan="5"><span class="pull-right"><strong>IVA %</strong></span></td>
  <td><span class="pull-right"><strong><?php echo number_format($iva_total,0);?></strong></span></td>
  <td></td>
</tr>
<tr>
  <td colspan="5"><span class="pull-right"><strong>TOTAL $</strong></span></td>
  <td><span class="pull-right"><strong style="font-size:15px"><?php echo number_format($sumador_total,0);?></strong></span></td>
  <td></td>
</tr>
</table>
</div>
<?php
if($sumador_total>0)
{
?>
<div class="form-group pull-right">
    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#guardar">
        <span class="btn-icon-only icon-ok"></span> Guardar
    </button>
</div> 
<?php } ?>


<script>
    $(".btn-edit").click(function(){
      id = $(this).data("id");
      $.get("./php_facturacion_periodica/form_articulo.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 -->
