<?php
  include("conn.php");

  $auxCliente = (!isset($_POST['xCliente']))?'':$_POST['xCliente'];
  $auxBase = (!isset($_POST['xBase']))?'-1':$_POST['xBase'];
  $auxInicial = (!isset($_POST['xInicial']))?'':$_POST['xInicial'];
  $auxRazon = (!isset($_POST['xRazon']))?'':$_POST['xRazon'];
  $auxDesde = (!isset($_POST['xDesde']))?'':$_POST['xDesde'];
  $auxHasta = (!isset($_POST['xHasta']))?'':$_POST['xHasta'];

  $sqlBases = "SELECT DISTINCT E.BASE, E.DESCRI, E.NOMBRE_BASE 
               FROM (SELECT XB.BASE, XB.DESCRI, XB.NOMBRE_BASE 
                     FROM BASES xB 
                          INNER JOIN USUARIOSBASES XUB 
                              INNER JOIN USUARIOS XU 
                              ON XUB.ID_USUARIO = XU.ID_USUARIO 
                              AND XU.USUARIO = 'PIXO_WEB' 
                          ON XB.BASE = XUB.BASE) E 
                    INNER JOIN USUARIOSBASES UB 
                        INNER JOIN USUARIOS U 
                        ON UB.ID_USUARIO = U.ID_USUARIO 
                    ON E.BASE = UB.BASE 
               WHERE 1=1 AND E.BASE = ".$auxBase."
               ORDER BY E.DESCRI";

  $RsEmpresas = $DBSegu->Execute($sqlBases);
  if (!$RsEmpresas) {
    $jsondata['success'] = false;
    $jsondata['message'] = 'Ha ocurrido un error durante la consulta. Ref. #V01.02';
    header('Content-type: application/json; charset=utf-8');
    echo json_encode($jsondata);
    die();  
  }

  $sqlBase = $RsEmpresas->fields["DESCRI"];

  $sqlConsulta = "select convert(varchar(10), CC.FECHA, 103) as FECHA, CC.COMPROB, TM.CLAVE, TM.CREDITO,
                         case when TM.CREDITO = 1 then CAST(0 AS MONEY) else CC.IMPORTE end as DEBE,
                         case when TM.CREDITO = 1 then CC.IMPORTE else cast(0 AS MONEY) end as HABER
                  from ".$RsEmpresas->fields["NOMBRE_BASE"].".dbo.ccCTACTE CC 
                       inner join ".$RsEmpresas->fields["NOMBRE_BASE"].".dbo.ccTIPOMOV TM
                       on CC.TIPO_MOV = TM.TIPO_MOV
                  where CC.CLI_PRV = '".$auxCliente."' 
                    and CC.FECHA between '". $auxDesde ."' and '".$auxHasta."' 
                    and CC.IMPORTE <> 0
                  order by CC.FECHA, TM.CLAVE, CC.LETRA, CC.PTO_VENTA, CC.NUMERO";
  $RsConsulta = $DBSegu->Execute($sqlConsulta);
  if (!$RsConsulta) {
    print $DBSegu->ErrorMsg();
  }


  $xSaldo = 0; //este habría que calcular el saldo inicial
?>

            <table id="tablaDetalle" class="display table table-striped table-bordered sticky-header" style="width:100%">
              <thead style="background-color: #db5916; color: aliceblue;">
                <tr>
                  <th class="thalign tituloshead">FECHA</th>
                  <th class="thalign tituloshead">COMPROBANTE</th>
                  <th class="thalign tituloshead">DEBE</th>
                  <th class="thalign tituloshead">HABER</th>
                  <th class="thalign tituloshead">SALDO FINAL</th>
                </tr>
              </thead>
              <tbody>           
                <tr>  
                  <td class="numerales">Saldo Inicial</td>
                  <td class="numerales"></td>
                  <td class="numerales"><?php echo '$'. number_format($auxInicial,2,',','.'); ?></td>
                  <td class="numerales"><?php echo '$'. number_format(0,2,',','.'); ?></td>
                  <td class="numerales"><?php echo '$'. number_format($auxInicial,2,',','.'); ?></td>
                </tr>
<?php
  $xSaldo = $auxInicial;
  while (!$RsConsulta->EOF) {
  	$xSaldo = $xSaldo + $RsConsulta->fields['DEBE'] - $RsConsulta->fields['HABER'];
?>
                <tr>  
                  <td class="numerales"><?php echo $RsConsulta->fields['FECHA']; ?> </td>
                  <td class="numerales"><?php echo $RsConsulta->fields['COMPROB']; ?></td>
                  <td class="numerales"><?php echo '$'. number_format($RsConsulta->fields['DEBE'],2,',','.'); ?></td>
                  <td class="numerales"><?php echo '$'. number_format($RsConsulta->fields['HABER'],2,',','.'); ?></td>
                  <td class="numerales"><?php echo '$'. number_format($xSaldo,2,',','.'); ?></td>
                </tr>
<?php  
    $RsConsulta->Movenext();
  }
?>
              </tbody>
              <tfoot>
              </tfoot>
            </table>
