<?php

session_start();

require_once ("../../config/Cado.php");

require_once ("../venta/cVenta.php");

$oVenta = new cVenta();

require_once ("../venta/cVentapago.php");

$oVentapago = new cVentapago();

require_once ("../ingreso/cIngreso.php");

$oIngreso = new cIngreso();

require_once ("../formatos/formato.php");





$fec1 = fecha_mysql($_POST['txt_fil_ven_fec1']);

$fec2 = fecha_mysql($_POST['txt_fil_ven_fec2']);

$formapago = $_POST['cmb_fil_forpag_id'];

$modopago = $_POST['cmb_fil_modpag_id'];

$tarjeta = $_POST['cmb_fil_tar_id'];

$cuenta = $_POST['cmb_fil_cuedep_id'];

//echo $modopago;exit;

$dts1=$oVentapago->filtrar_modulo_ventapago($fec1,$fec2,$formapago,$modopago,$tarjeta,$cuenta);



$num_rows= mysql_num_rows($dts1);

?>



<script type="text/javascript">

  $(function() {	



   $('.btn_editar').button({

    icons: {primary: "ui-icon-pencil"},

    text: true

  });

   

   $('.btn_eliminar').button({

    icons: {primary: "ui-icon-trash"},

    text: false

  });

   

   $('.btn_anular').button({

    icons: {primary: "ui-icon-cancel"},

    text: false

  });



   $("#tabla_venta").tablesorter({

    widgets: ['zebra', 'zebraHover'],

    headers: {

     0: {sorter: 'shortDate' },

     10: { sorter: false}

   },

		//sortForce: [[0,0]],

		sortList: [[0,0],[1,0]]

  });

   

 }); 

</script>

<table cellspacing="1" id="tabla_venta" class="tablesorter">

  <thead>

    <tr>

      <th>FECHA</th>

      <th>DOCUMENTO</th>

      <th>CLIENTE</th>

      <th>RUC/DNI</th>

      <th>VENDEDOR</th>

      <th>FORMA</th>

      <th>MODO</th>

      <th>DETALLE</th>

      <th align="right">TOTAL</th>

      <th align="right">PAGOS</th>

      <th align="right">SALDO</th>

      <th>&nbsp;</th>

    </tr>

  </thead>

  <?php

  if($num_rows>0){

   ?>

   <tbody>

    <?php

    while($dt1 = mysql_fetch_array($dts1))

    {



      $total_valven+=$dt1['tb_venta_valven'];

      $total_igv+=$dt1['tb_venta_igv'];      

      $total_ventas +=$dt1['tb_venta_tot'];

      $total_pagar += $dt1['tb_venta_tot'];

      $dts2=$oVentapago->filtrar($dt1['tb_venta_id']);

      while($dt2 = mysql_fetch_array($dts2))

      {

        $monto = $dt2['tb_ventapago_mon']; 

        $abonos=0;



        $mod_id=3;

        $est=1;

        $dts3 = $oIngreso->mostrar_por_modulo($mod_id,$dt2['tb_ventapago_id'],$est);

        while($dt3 = mysql_fetch_array($dts3))

        {

          $abonos+= $dt3['tb_ingreso_imp'];

        } 

        mysql_free_result($dts3);



        $saldo = ($monto - $abonos);



        $total_abono+=$abonos;

        $total_saldo+=$saldo;

      }



      $filas=0;

      $rw1=$oVenta->mostrar_venta_detalle($dt1['tb_venta_id']);

      $filas= mysql_num_rows($rw1);

      $detalle="";



      if(!$filas>0)

      {

        $detalle="Vacío";

      }



      ?>

      <tr>

        <td nowrap="nowrap"><?php echo mostrarFecha($dt1['tb_venta_fec'])?></td>

        <td nowrap="nowrap"><?php echo $dt1['tb_documento_abr'].' '.$dt1['tb_venta_numdoc']?></td>

        <td><?php echo $dt1['tb_cliente_nom'];?></td>

        <td><?php echo $dt1['tb_cliente_doc'];?></td>

        <td align="right"><?php echo $dt1['tb_vendedor_nom']?></td>

        <td><?php

         if($dt1['tb_formapago_id']==1)echo 'CONTADO';

         if($dt1['tb_formapago_id']==2)echo 'CREDITO';

         ?>

       </td>



       <td><?php 



         if($dt1['tb_modopago_id']==1)echo 'EFECTIVO';

          if($dt1['tb_modopago_id']==2)echo 'TARJETA';

          if($dt1['tb_modopago_id']==3)echo 'DEPOSITO';



         ?></td>

         <td>

          <?php

          //CREDITO         

          if($dt1['tb_formapago_id']==2)echo 'A '.$dt1['tb_ventapago_numdia'].' días | Vencimiento: '.mostrarFecha($dt1['tb_ventapago_fecven']);          

          //TARJETA

          if($dt1['tb_modopago_id']==2)echo $dt1['tb_tarjeta_nom'].' N° Operación: '.$dt1['tb_ventapago_numope'];



          //DEPOSITO

          if($dt1['tb_modopago_id']==3)echo $dt1['tb_cuentadeposito_nom'].' N° Operación: '.$dt1['tb_ventapago_numope'];  

          ?>

        </td>

        

        <td align="right"><?php echo formato_money($dt1['tb_venta_tot'])?></td>

        <td align="right"><?php echo formato_money($abonos);?></td>

        <td align="right"><?php echo formato_money($saldo);?></td>

       <td align="left" nowrap="nowrap">



         <a class="btn_editar" href="#update" onClick="venta_form('editar','<?php echo $dt1['tb_venta_id']?>')">Detalle</a> 

                

         

       </td>

      </tr>

      <?php

    }

    mysql_free_result($dts1);

    ?>

  </tbody>

  <?php

}

?>

<tr class="even">

  <td colspan="8">TOTAL</td>

  <td align="right"><strong><?php echo formato_money($total_pagar)?></strong></td>

  <td align="right"><strong><?php echo formato_money($total_abono)?></strong></td>

  <td align="right"><strong><?php echo formato_money($total_saldo)?></strong></td>

 <td>&nbsp;</td>

</tr>

<tr class="even">

  <td colspan="12"><?php echo $num_rows.' registros'?></td>

</tr>

</table>