<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
$ven_id=$_GET['id']; 
$sql=mysqli_query($con, "SELECT * from ventas where ven_id='$ven_id'");
while ($reg=mysqli_fetch_array($sql))
{
    $id=$reg["ven_id"];
    $ven_id=$reg['ven_id'];
    $ven_estado=$reg['ven_estado'];
    $estado_dian=$reg['estado_dian'];
    $clie_id=$reg['Cliente_clie_id'];
    $ven_forma_de_pago=$reg['ven_forma_de_pago'];
?>
    <div class="col-md-12">
        <div class="table-responsive">
            <strong><h2>N° <?php echo $reg['ven_prefijo']; ?>-<?php echo $reg['ven_n_venta']; ?></h2></strong>
            <div class="tabbable">
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#DETALLE<?php echo $ven_id; ?>" data-toggle="tab">DETALLE</a> </li>
                    <li><a href="#ABONOS<?php echo $ven_id; ?>" data-toggle="tab">MEDIO PAGO</a></li> 
                    <li><a href="#MODIFICACIONES<?php echo $reg['ven_id'];?>" data-toggle="tab">MODIFICACIONES</a></li>
                </ul> 
                <div class="tab-content"> 
                    <div class="tab-pane active" id="DETALLE<?php echo $ven_id; ?>">
                        <table class="table table-bordered table-hover table-striped">
                            <thead>
                                <tr>
                                    <th width="10%">REFERENCIA</th>
                                    <th width="5%">CANTIDAD</th>
                                    <th width=""><span class="pull-center">DESCRIPCIÓN</span></th>
                                    <th width="12%"><span class="pull-right">PRECIO U</span></th>
                                    <th width="5%"><span class="pull-right">IMP</span></th>
                                    <th width="5%"><span class="pull-right">DESCUENTO</span></th>
                                    <th width="12%"><span class="pull-right">PRECIO T</span></th>
                                </tr>
                            </thead>
                            <tbody  class="buscar">
                            <?php
                                $total_retencion=0;
                                $ret=mysqli_query($con,"SELECT SUM(ret_importe) as ret_importe FROM retencion_venta where empresa='$empresa' and ventas_ven_id='$ven_id'") or die(mysqli_error($con)); 
                                $rets=mysqli_fetch_array($ret);
                                $total_retencion=$rets['ret_importe'];

                                $subtotal_art=0;
                                $descuento_art=0;
                                $iva_art=0;

                                $sql=mysqli_query($con, "SELECT * from venta_detalle where ventas_ven_id='$ven_id'");
                                while ($row=mysqli_fetch_array($sql))
                                {
                                    $vend_referencia=$row['vend_referencia'];
                                    $vend_nombre=$row['vend_nombre'];
                                    $vend_unidad=$row['vend_unidad'];
                                    $vend_cantidad=$row['vend_cantidad'];
                                    $vend_precio=$row['vend_precio'];
                                    if ($row['vend_iva']>0){ $vend_iva=$row['vend_iva']; } else { $vend_iva='0';}
                                    if ($row['vend_desc']>0){ $vend_descuento=$row['vend_desc']; } else { $vend_descuento='0';}
                                    $iva0=$vend_iva/100; //0.19
                                    $iva1=1+$iva0; //1.19

                                    $precio_total_sin_iva_und=($vend_precio/$iva1); //precio sin iva  840.336 unidad

                                    $precio_total_sin_iva_item=($vend_precio/$iva1)*$vend_cantidad; //precio sin iva  840.336
                                    $descuento_item=($vend_descuento/100)*$precio_total_sin_iva_item; //descuento_item 84.033
                                    $total_iva_item=($precio_total_sin_iva_item-$descuento_item)*$iva0; // total iva  159.336

                                    $subtotal_art+=$precio_total_sin_iva_item;
                                    $descuento_art+=$descuento_item;
                                    $iva_art+=$total_iva_item;
                            ?>
                                <tr>
                                    <td ><?php echo $vend_referencia;?></td>
                                    <td ><?php echo $vend_cantidad;?></td>
                                    <td ><?php echo $vend_nombre;?></td>
                                    <td ><span class="pull-right"><?php echo number_format($vend_precio,0);?></span></td>
                                    <td ><span class="pull-right"><?php echo number_format($vend_iva,0);?></span></td>
                                    <td ><span class="pull-right"><?php echo number_format($vend_descuento,0);?></span></td>
                                    <td style="text-align: right;"><span class="pull-right"><?php echo number_format($vend_precio*$vend_cantidad,0);?></span></td>
                                </tr>
                            <?php
                                   
                                }








                                $subtotal_com=0;
                                $descuento_com=0;
                                $iva_com=0;
                                $sql=mysqli_query($con, "SELECT * from venta_detalle_combo where ven_id='$ven_id'");
                                while ($row1=mysqli_fetch_array($sql))
                                {
                                    $combo_referencia=$row1["combo_referencia"];
                                    $combo_nombre=$row1['combo_nombre'];       
                                    $combo_cantidad=$row1["combo_cantidad"];
                                    $combo_precio=$row1['combo_precio'];
                                    if ($row1['combo_iva']>0){ $combo_iva=$row1['combo_iva']; } else { $combo_iva='0';}
                                    if ($row1['combo_desc']>0){ $combo_desc=$row1['combo_desc']; } else { $combo_desc='0';}
                                    $com_iva0=$combo_iva/100; //0.19
                                    $com_iva1=1+$com_iva0; //1.19

                                    $precio_total_sin_iva_und_com=($combo_precio/$com_iva1); //precio sin iva  840.336 unidad

                                    $precio_total_sin_iva_item_com=($combo_precio/$com_iva1)*$combo_cantidad; //precio sin iva  840.336
                                    $descuento_item_com=($combo_desc/100)*$precio_total_sin_iva_item_com; //descuento_item 84.033
                                    $total_iva_item_com=($precio_total_sin_iva_item_com-$descuento_item_com)*$com_iva0; // total iva  159.336

                                    $subtotal_com+=$precio_total_sin_iva_item_com;
                                    $descuento_com+=$descuento_item_com;
                                    $iva_com+=$total_iva_item_com;
                            ?>
                                <tr>
                                    <td ><?php echo $combo_referencia;?></td>
                                    <td ><?php echo $combo_cantidad;?></td>
                                    <td ><?php echo $combo_nombre;?></td>
                                    <td ><span class="pull-right"><?php echo number_format($combo_precio,0);?></span></td>
                                    <td ><span class="pull-right"><?php echo number_format($combo_iva,0);?></span></td>
                                    <td ><span class="pull-right"><?php echo number_format($combo_desc,0);?></span></td>
                                    <td style="text-align: right;"><span class="pull-right"><?php echo number_format($combo_precio*$combo_cantidad,0);?></span></td>
                                </tr>
                            <?php
                                }

                                $subtotal_venta=$subtotal_art+$subtotal_com;
                                $descuento_venta=$descuento_art+$descuento_com;
                                $iva_venta=$iva_art+$iva_com;
                                $total_venta=($subtotal_venta-$descuento_venta-$total_retencion)+$iva_venta;

                              ?>              
                            <tr>
                                <td colspan="5" border="1" style=" text-align: right;"></td>
                                <td style="width: 5%;  text-align: right;">SUBTOTAL 1 </td>
                                <td style="width: 12%; text-align: right;">&#36; <?php echo number_format($subtotal_venta,0);?></td>
                            </tr>
                            <tr>
                                <td colspan="5" border="1" style=" text-align: right;"></td>
                                <td style="width: 5%;  text-align: right;">DESCUENTO  </td>
                                <td style="width: 12%; text-align: right;">&#36; <?php echo number_format($descuento_venta,0);?></td>
                            </tr>
                            <tr>
                                <td colspan="5" border="1" style=" text-align: right;"></td>
                                <td style="width: 5%;  text-align: right;">SUBTOTAL 2 </td>
                                <td style="width: 12%; text-align: right;">&#36; <?php echo number_format($subtotal_venta+$descuento_venta,0);?></td>
                            </tr>
                            <tr>
                                <td colspan="5" border="1" style=" text-align: right;"></td>
                                <td style="width: 5%; text-align: right;">IMPUESTO  </td>
                                <td style="width: 12%; text-align: right;">&#36; <?php echo number_format($iva_venta,0);?></td>
                            </tr>
                             <tr>
                                <td colspan="5" border="1" style=" text-align: right;"></td>
                                <td style="width: 5%; text-align: right;">RETENCION  </td>
                                <td style="width: 12%; text-align: right;">&#36; <?php echo number_format($total_retencion,0);?></td>
                            </tr>
                            <tr style="border: solid 01px black;  text-align: center; font-size: 10pt;padding:0mm;">
                                <td colspan="5" border="1" style=" text-align: right;"></td>
                                <td style="width: 5%; text-align: right;">TOTAL  </td>
                                <td style="width: 12%; text-align: right;">&#36; <?php echo number_format($total_venta,0);?></td>
                            </tr>
                            </tbody> 
                        </table>
                    </div>

                    <div class="tab-pane" id="MODIFICACIONES<?php echo $reg['ven_id']; ?>">
                        <table class="table table-bordered table-hover table-striped">
                            <tr>
                                <td >Usuario</td>
                                <td >Numero factura Viejo</td>
                                <td >Numero factura Nuevo</td>
                                <td >Fecha Factura Viejo</td>
                                <td >Fecha de Modificacón</td>
                            </tr>
                            <?php
                            
                                $can1=mysqli_query($con,"SELECT * from ventas_historial_electronico where ven_id='$ven_id'") or die(mysqli_error($con)); 
                                while($cons=mysqli_fetch_array($can1)) { ?>
                                <tr>
                                    <td ><?php echo $cons['vent_usuario'];?></td>
                                    <td ><?php echo $cons['vent_num_viejo'];?></td>
                                    <td ><?php echo $cons['vent_num_nuevo'];?></td>
                                    <td ><?php echo $cons['vent_fecha_viejo'];?></td>
                                    <td ><?php echo $cons['vent_fecha_nuevo'];?></td>
                                </tr>
                            <?php } ?>
                        </table>
                    </div>

                    <div class="tab-pane" id="ABONOS<?php echo $reg['ven_id']; ?>">
                        <table class="table table-bordered table-hover table-striped">
                            <thead>
                                <tr>
                                    <th>Forma Pago</th>
                                    <th>Medio Pago</th>
                                    <th><span class="pull-center">Valor</span></th>  
                                </tr>
                            </thead> 
                                <?php  $vea=mysqli_query($con, "SELECT * from venta_abonos where ventas_ven_id='$ven_id'");
                                while ($abo=mysqli_fetch_array($vea))
                                    {
                                        $veab_abono=$abo['veab_abono'];
                                        $forma_pago=$abo['forma_pago'];
                                        $medio_pago=$abo['medio_pago'];
                                ?>
                                <tr>
                                    <td><?php echo ucfirst($forma_pago); ?></td>
                                    <td><?php echo $medio_pago; ?></td>
                                    <td><?php echo number_format($veab_abono); ?></td>
                                </tr>
                            <?php  } ?>
                        </table>
                    </div> 
                </div>
            </div>
        </div> <!-- /.form-group -->
    </div><!-- /.modal-content -->
<?php } ?>