<?php
include '../../config/conn.php';
$query = mysqli_query($conn, "SELECT * FROM jenis_transaksi WHERE id_jenis='$_GET[type]' ORDER BY id_jenis");
while ($row = mysqli_fetch_array($query)) {
  $jenis = $row['nama_jenis'];
}

$hariini = date('Y-m-d');
$hariini_title = date('d-m-Y');
$hariini_title2 = date('Ymd');

header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=TRANSAKSI HARIAN $hariini_title -  $hariini_title.xls");


$debit = 0;
$kredit = 0;
$saldo = 0;



//untuk membuat total debit, kredit

  $query_saldo = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE tanggal='$hariini'");

$row_saldo = mysqli_fetch_array($query_saldo);
$saldo_keseluruhan = $row_saldo['jumlah_debit'] - $row_saldo['jumlah_kredit'];


?>

<h3 align="center">LAPORAN KEUANGAN <?php 
                                      echo "TANGGAL " . date('d') . " BULAN " . date('m') . " TAHUN " . date('Y');
                                  
                                    ?><small></small></h3>
<table border="1">
  <thead>
    <tr>
      <th>NO</th>
      <th>Tanggal</th>
      <th>Sumber Kas Transaksi</th>
      <th>Jenis Transaksi</th>
      <th>keterangan</th>
      <th>Jenis Kas</th>
      <th>Debit</th>
      <th>Kredit</th>
      <th width="110">Saldo</th>
    </tr>
  </thead>
  <tbody>
    <?php
    $no = 0;
  
      $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE tanggal='$hariini' order by tanggal  ASC ");
    

    $count = 2;



    while ($row = mysqli_fetch_array($query)) {
      $keri = mysqli_query($conn, "SELECT * FROM santri WHERE id_santri='$row[id_santri]'");
      $r = mysqli_fetch_array($keri);

      $don1 = mysqli_query($conn, "SELECT * FROM donatur WHERE id_donatur='$row[id_donatur]'");
      $don = mysqli_fetch_array($don1);
      $no++;
    ?>

      <tr>
        <td style="background: <?php if ($row['kredit'] != 0) { ?>
                                #feeeea;
                                <?php } ?>"><?php echo $no; ?></td>
        <td style="background: <?php if ($row['kredit'] != 0) { ?>
                                #feeeea;
                                <?php } ?>"><?php echo "'".date('Y-m-d', strtotime($row['tanggal'])); ?></td>
        <td style="background: <?php if ($row['kredit'] != 0) { ?>
                                #feeeea;
                                <?php } ?>"><?php
                                  $jtran = mysqli_query($conn, "SELECT * FROM jenis_transaksi WHERE id_jenis='$row[id_jenis_sumber]'");
                                  $jtran2 = mysqli_fetch_array($jtran);
                                  echo $jtran2['nama_jenis'];
                                  ?></td> 
                                  <!-- sumber kas transaksi -->

<td style="background: <?php if ($row['kredit'] != 0) { ?>
                                #feeeea;
                                <?php } ?>"><?php
                                  $jtran = mysqli_query($conn, "SELECT * FROM jenis_transaksi WHERE id_jenis='$row[id_jenis]'");
                                  $jtran2 = mysqli_fetch_array($jtran);
                                  echo $jtran2['nama_jenis'];
                                  ?></td>



        <td style="background: <?php if ($row['kredit'] != 0) { ?>
                                #feeeea;
                                <?php } ?>"><?php
                                            if ($row['id_jenis'] == '5' or $row['id_jenis'] == '6' or $row['id_jenis'] == '14' or $row['id_jenis'] == '17') {
                                              echo "id/nama : $row[id_santri]/$r[nama] |";
                                            }
                                            if ($row['id_jenis'] == '5' ) {
                                              echo "SPP $row[spp_bulan]-$row[spp_tahun] |";
                                            }
                                            if ($row['id_jenis'] == '17' ) {
                                              echo "DUT $row[dut_tahun] |";
                                            }

                                            if ($row['id_jenis'] == '10') {
                                              //jenis donatur
                                              $jn1 = mysqli_query($conn, "SELECT * FROM jenis_donatur WHERE id='$row[jenis_donatur]'");
                                              $jn = mysqli_fetch_array($jn1);
                                              $d = $row['id_donatur'];
                                              if ($d == "") {
                                                echo "id/nama : tdk tetap/$row[nama_donatur_tidak_tetap] | $jn[nama_jenis] |";
                                              } else {
                                                echo "id/nama : $d/$don[nama_donatur] | $jn[nama_jenis] |";
                                              }

                                              // echo "id/nama : $d/$don[nama_donatur] |";
                                            }

                                            if ($row['id_jenis'] == '11' or $row['id_jenis'] == '12') {
                                              //jenis donatur
                                              $jn1 = mysqli_query($conn, "SELECT * FROM santri_rt WHERE id_santri='$row[id_santri]'");
                                              $jn = mysqli_fetch_array($jn1);
                                              $namart1 = mysqli_query($conn, "SELECT * FROM jenis_rt WHERE id='$jn[jenis_rt]'");
                                              $namart = mysqli_fetch_array($namart1);
                                              $d = $row['id_santri'];

                                              echo "id/nama : $d/$jn[nama] | $namart[nama_jenis] |";

                                              // echo "id/nama : $d/$don[nama_donatur] |";
                                            }
                                            if ($row['id_jenis_rt'] != "") {
                                              $jenrt1 = mysqli_query($conn, "SELECT nama_jenis FROM jenis_rt WHERE id=$row[id_jenis_rt]");
                                              $jnrt = mysqli_fetch_array($jenrt1);
                                              $jen = $jnrt['nama_jenis'] . " | ";
                                            } else {
                                              $jen = "";
                                            }

                                            echo " $jen $row[keterangan]"; ?></td>
        <td style="background: <?php if ($row['kredit'] != 0) { ?>
                                #feeeea;
                                <?php } ?>">
          <?php
          $jeniskas = mysqli_query($conn, "SELECT * FROM jenis_kas WHERE id='$row[jenis_kas]'");
          $jekas = mysqli_fetch_array($jeniskas);
          echo $jekas['nama'];
          ?>
        </td>

        <?php
        if ($row['debit'] != 0) {
        ?>
          <td><?php echo $row['debit']; ?></td>
          <td><?php echo $row['kredit']; ?></td>
          <td>
            <?php
            $debit = $debit + $row['debit'];
            $saldo = $saldo + $row['debit'];
            echo $saldo;
            ?>
          </td>


        <?php } else { ?>
          <td style="background: <?php if ($row['kredit'] != 0) { ?>
                                #feeeea;
                                <?php } ?>"><?php echo $row['debit']; ?></td>
          <td style="background: <?php if ($row['kredit'] != 0) { ?>
                                #feeeea;
                                <?php } ?>"><?php echo $row['kredit']; ?></td>
          <td style="background: <?php if ($row['kredit'] != 0) { ?>
                                #feeeea;
                                <?php } ?>">
          <?php
          $kredit = $kredit + $row['kredit'];
          $saldo = $saldo - $row['kredit'];
          echo $saldo;
        }
          ?>
          </td>

      </tr>


    <?php } ?>
    <tr>
      <td align="center" colspan="6"><b>TOTAL</b></td>
      <td><?php echo $row_saldo['jumlah_debit']; ?></td>
      <td><?php echo $row_saldo['jumlah_kredit']; ?></td>
    </tr>
  </tbody>
</table>