<?php

//ini_set("display_errors",1);

session_start();

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

require_once ("../compra/cComprapago.php");

$oComprapago = new cComprapago();

require_once ("../egreso/cEgreso.php");

$oEgreso = new cEgreso();

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'];

$caja = $_POST['cmb_caj_id'];

//echo $modopago;exit;

$dts1=$oComprapago->filtrar_modulo_comprapago($fec1,$fec2,$formapago,$caja);

$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_compra").tablesorter({

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

    headers: {

     0: {sorter: 'shortDate' },

     10: { sorter: false}

   },

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

		sortList: [[0,0]]

  });

 }); 

</script>





<section class="content" id="table" style="min-height:0px;padding-bottom:0px;margin-bottom:-30px;">

  <div class="row">

    <div class="col-xs-12">

      <div class="box">

        <div class="box-body">

          <table id="tabla_compra" class="table table-bordered table-striped table-responsive">



                <thead>

                  <tr>

                    <th>FECHA</th>

                    <th>DOCUMENTO</th>                    

                    <th>PROVEEDOR</th>

                    <th>RUC/DNI</th>

                    <th>ALMACEN</th>

                    <th>FORMA</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_compra_valven'];

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

                   $total_per+=$dt1['tb_compra_per'];

                   $total_compras+=$dt1['tb_compra_tot'];

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



                   $dts2=$oComprapago->filtrar($dt1['tb_compra_id']);

                   while($dt2 = mysql_fetch_array($dts2))

                   {

                    $monto = $dt2['tb_comprapago_mon']; 

                    $abonos=0;



                    $mod_id=4;

                    $est=1;

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

                    while($dt3 = mysql_fetch_array($dts3))

                    {

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

                    } 

                    mysql_free_result($dts3);



                    $saldo = ($monto - $abonos);



                    $total_abono+=$abonos;

                    $total_saldo+=$saldo;

                  }



                  ?>

                  <tr>

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

                    <td><?php echo $dt1['tb_documento_abr'].' - '.$dt1['tb_compra_numdoc']?></td>

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

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

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

                    <!-- ------------------ -->

                    <td><?php 

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

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

                      ?>

                    </td>

                    <td><?php if($dt1['tb_formapago_id']==2)

                    echo 'A '.$dt1['tb_comprapago_numdia'].' días | Vencimiento: '.mostrarFecha($dt1['tb_comprapago_fecven']);

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

                     <!-- ------------------ -->

                    <td align="right"><?php echo formato_money($dt1['tb_compra_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="compra_form('editar','<?php echo $dt1['tb_compra_id']?>','<?php echo $dt1['saldo'] ?>')">Detalle</a> 

                    

                    </td>

                  </tr>

                  <?php

                }

                mysql_free_result($dts1);

                ?>

              </tbody>

              <?php

              }

              ?>

              <tr class="even">

                <td colspan="7">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="11"><?php echo $num_rows.' registros'?></td>

              </tr>

              </table>

          </div>

        </div>

      </div>

    </div>

  </section>



