<?php
session_start();

// memanggil library FPDF
require('../library/fpdf185/fpdf.php');

include '../koneksi.php';

// intance object dan memberikan pengaturan halaman PDF
// asline // $pdf=new FPDF('L','mm', array(200,104));
// $pdf=new FPDF('P','mm', 'A6');

//$pdf=new FPDF('P','mm','A4');
$pdf = new FPDF('P', 'mm', array(215,330));
 
// asline //$pdf->SetMargins(7,8,0,0);
$pdf->SetMargins(20,15,10,10);
$pdf->AddPage();

function penyebut($nilai) {
  $nilai = abs($nilai);
  $huruf = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
  $temp = "";
  if ($nilai < 12) {
    $temp = "". $huruf[$nilai];
  } else if ($nilai <20) {
    $temp = penyebut($nilai - 10). " belas";
  } else if ($nilai < 100) {
    $temp = penyebut($nilai/10)." puluh ". penyebut($nilai % 10);
  } else if ($nilai < 200) {
    $temp = " seratus " . penyebut($nilai - 100);
  } else if ($nilai < 1000) {
    $temp = penyebut($nilai/100) . " ratus " . penyebut($nilai % 100);
  } else if ($nilai < 2000) {
    $temp = " seribu " . penyebut($nilai - 1000);
  } else if ($nilai < 1000000) {
    $temp = penyebut($nilai/1000) . " ribu " . penyebut($nilai % 1000);
  } else if ($nilai < 1000000000) {
    $temp = penyebut($nilai/1000000) . " juta " . penyebut($nilai % 1000000);
  } else if ($nilai < 1000000000000) {
    $temp = penyebut($nilai/1000000000) . " milyar " . penyebut(fmod($nilai,1000000000));
  } else if ($nilai < 1000000000000000) {
    $temp = penyebut($nilai/1000000000000) . " trilyun " . penyebut(fmod($nilai,1000000000000));
  }     
  return $temp;
}

            //////////////////////////////////////////////////////////////////////////// 
        $kdc   =$_SESSION['kdc'];    
        $oleh = $_GET['toleh'];
        $tabel_tujuan = "nawal_".$oleh;
        $tabel_asal = "nawal";

        $tgl_d = $_GET['xtgl_d'];
        $tgl_s = $_GET['xtgl_s'];

        $tgl_ke_1 = date('2025-01-01');
        $tgl_p = strtotime("$tgl_d");
        $tgl_lalu = date('Y-m-d', strtotime("-1 day", $tgl_p));

        $kanca = $_SESSION['kanca'];
        $now = date('Y-m-d H:i:s');
            //////////////////////////////////////////////////////////////////////////// 

            //////////////////////////////////////////////////////////////////////////// 
$kdkanca = mysqli_query($koneksi,"select * from kdkanca where kd='$kdc'");
$kc = mysqli_fetch_assoc($kdkanca);
$logo_kop=$kc['gambar'] ;


$gambar='../gambar/'.$logo_kop.'';

$pdf->Image(‘foto/’ . $gambar,20,10,120);

//$pdf->Image($gambar, 20, 5, 90, 20); // atur ukuran dan posisi gambar secara manual

$pdf->SetFont('Times','B',15);
$pdf->Cell(10,10,' ',0,1);

$pdf->Cell(10,12,'','0',1,'C');
$pdf->Cell(180,8,'LAPORAN ARUS KAS',0,1,'C');

$pdf->SetFont('Arial','B',12);
$pdf->Cell(180,5,'Dari Tgl : '.date('d-m-Y', strtotime($tgl_s)).' s/d '.date('d-m-Y', strtotime($tgl_lalu)).'',0,0,'C');
$pdf->Cell(2,5,'',0,0,'C');
$pdf->Cell(10,5,'',0,1);

// Atur warna latar belakang sel
$pdf->SetFillColor(220, 220, 220); // Warna abu-abu

// Atur warna teks
$pdf->SetTextColor(0, 0, 0); // Warna hitam

$pdf->SetFont('Arial','B',11);
$pdf->Cell(15,9,'No.',1,0,'C', true);
$pdf->Cell(120,9,'Keterangan',1,0,'C', true);
$pdf->Cell(40,9,'Jumlah',1,0,'C', true);
$pdf->Cell(5,9,'',0,1);
$pdf->SetFont('Arial','',8);
$pdf->SetTextColor(0, 0, 0); // Warna hitam

                $sqlx = mysqli_query($koneksi, "SELECT nama FROM n_arus_kas WHERE kd='10' limit 1 ");
                list($nama) = mysqli_fetch_array($sqlx);

$pdf->SetFont('Arial','BI',11);
$pdf->Cell(15,8,'',1,0,'C');
$pdf->Cell(120,8,$nama,1,0,'L');
$pdf->Cell(40,8,'',1,0,'C');
$pdf->Cell(5,8,'',0,1);

            $no=1;
            $nol=0;
            //
            $data = mysqli_query($koneksi,"SELECT * FROM n_arus_kas WHERE kd>'10' and kd<'49' order by kd ASC ");
            while($z = mysqli_fetch_array($data)){
            	$kdz=$z['kd'];
				$sql_11 = mysqli_query($koneksi, "SELECT sum(mutasik-mutasid) AS tmutasi_11 FROM $tabel_tujuan WHERE ry='11' ");
                	list($tmutasi_11) = mysqli_fetch_array($sql_11);
				$sql_12 = mysqli_query($koneksi, "SELECT sum(mutasik-mutasid) AS tmutasi_12 FROM $tabel_tujuan WHERE ry='12' ");
                	list($tmutasi_12) = mysqli_fetch_array($sql_12);
				$sql_13 = mysqli_query($koneksi, "SELECT sum(mutasik-mutasid) AS tmutasi_13 FROM $tabel_tujuan WHERE ry='13' ");
                	list($tmutasi_13) = mysqli_fetch_array($sql_13);  
				$sql_14 = mysqli_query($koneksi, "SELECT sum(mutasik-mutasid) AS tmutasi_14 FROM $tabel_tujuan WHERE ry='14' ");
                	list($tmutasi_14) = mysqli_fetch_array($sql_14);
				$sql_15 = mysqli_query($koneksi, "SELECT sum(mutasik-mutasid) AS tmutasi_15 FROM $tabel_tujuan WHERE ry='15' ");
                	list($tmutasi_15) = mysqli_fetch_array($sql_15);
                $sql_16 = mysqli_query($koneksi, "SELECT sum(mutasik-mutasid) AS tmutasi_16 FROM $tabel_tujuan WHERE ry='16' ");
                          list($tmutasi_16) = mysqli_fetch_array($sql_16);
                $sql_17 = mysqli_query($koneksi, "SELECT sum(mutasik-mutasid) AS tmutasi_17 FROM $tabel_tujuan WHERE ry='17' ");
                          list($tmutasi_17) = mysqli_fetch_array($sql_17);
                $sql_18 = mysqli_query($koneksi, "SELECT sum(mutasik-mutasid) AS tmutasi_18 FROM $tabel_tujuan WHERE ry='18' ");
                          list($tmutasi_18) = mysqli_fetch_array($sql_18);
                $sql_19 = mysqli_query($koneksi, "SELECT sum(mutasik-mutasid) AS tmutasi_19 FROM $tabel_tujuan WHERE ry='19' ");
                          list($tmutasi_19) = mysqli_fetch_array($sql_19);
                $sql_20 = mysqli_query($koneksi, "SELECT sum(mutasik-mutasid) AS tmutasi_20 FROM $tabel_tujuan WHERE ry='20' ");
                          list($tmutasi_20) = mysqli_fetch_array($sql_20);
                $sql_21 = mysqli_query($koneksi, "SELECT sum(mutasik-mutasid) AS tmutasi_21 FROM $tabel_tujuan WHERE ry='21' ");
                          list($tmutasi_21) = mysqli_fetch_array($sql_21);
                $sql_22 = mysqli_query($koneksi, "SELECT sum(mutasik-mutasid) AS tmutasi_22 FROM $tabel_tujuan WHERE ry='22' ");
                          list($tmutasi_22) = mysqli_fetch_array($sql_22);
                                            
                	if ($kdz=='11')	{
                		$jumlah=$tmutasi_11;		
                	} else if ($kdz=='12')	{
                		$jumlah=$tmutasi_12;
                	} else if ($kdz=='13')	{
                		$jumlah=$tmutasi_13 ;
                	} else if ($kdz=='14')	{
                		$jumlah=$tmutasi_14 ;
                	} else if ($kdz=='15')	{
                		$jumlah=$tmutasi_15 ;
                	} else if ($kdz=='16')	{
                		$jumlah=$tmutasi_16 ;
                	} else if ($kdz=='17')	{
                		$jumlah=$tmutasi_17 ;
                	} else if ($kdz=='18')	{
                		$jumlah=$tmutasi_18 ;
                  } else if ($kdz=='19')  {
                    $jumlah=$tmutasi_19 ;
                  } else if ($kdz=='20')  {
                    $jumlah=$tmutasi_20 ;
                  } else if ($kdz=='21')  {
                    $jumlah=$tmutasi_21 ;
                  } else if ($kdz=='22')  {
                    $jumlah=$tmutasi_22 ;                		                		
                	} else {
                		$jumlah=0 ;
                	}
                  $total_01=$tmutasi_11+($tmutasi_12)+$tmutasi_13+$tmutasi_14+($tmutasi_15)+$tmutasi_16+($tmutasi_17)+$tmutasi_18+$tmutasi_19+($tmutasi_20)+$tmutasi_21+$tmutasi_22 ; 

                $nilai=$jumlah ;
                if ($nilai < 0) {
                    $xnilai= '(' . number_format($nilai*-1).')';
                } else {
                    $xnilai= '' . number_format($nilai).'';
                }

                $nilai_a=$total_01;
                if ($nilai_a < 0) {
                    $xnilai_a= '(' . number_format($nilai_a*-1).')';
                } else {
                    $xnilai_a= '' . number_format($nilai_a).'';
                }                          

$pdf->SetFont('Arial','',11);
$pdf->Cell(15,6,$no++,1,0,'C');
$pdf->Cell(120,6,$z['nama'],1,0,'L');
$pdf->Cell(40,6,$xnilai,1,0,'R');
$pdf->Cell(5,6,'',0,1);

}

$pdf->SetFont('Arial','BI',11);
$pdf->Cell(15,7,'',1,0,'C');
$pdf->Cell(120,7,'Jumlah Arus Kas dari '.$nama.'',1,0,'L');
$pdf->Cell(40,7,$xnilai_a,1,0,'R');
$pdf->Cell(5,7,'',0,1);





                $sqlx = mysqli_query($koneksi, "SELECT nama FROM n_arus_kas WHERE kd='50' limit 1 ");
                list($nama) = mysqli_fetch_array($sqlx);

$pdf->SetFont('Arial','BI',11);
$pdf->Cell(15,8,'',1,0,'C');
$pdf->Cell(120,8,$nama,1,0,'L');
$pdf->Cell(40,8,'',1,0,'C');
$pdf->Cell(5,8,'',0,1);

            $no=1;
            $nol=0;
            //
            $data = mysqli_query($koneksi,"SELECT * FROM n_arus_kas WHERE kd>'50' and kd<'69' order by kd ASC ");
            while($z = mysqli_fetch_array($data)){
              $kdz=$z['kd'];
        $sql_51 = mysqli_query($koneksi, "SELECT sum(mutasid) AS tmutasi_51 FROM $tabel_tujuan WHERE ry='51' ");
                  list($tmutasi_51) = mysqli_fetch_array($sql_51);
        $sql_52 = mysqli_query($koneksi, "SELECT sum(mutasik) AS tmutasi_52 FROM $tabel_tujuan WHERE ry='51' ");
                  list($tmutasi_52) = mysqli_fetch_array($sql_52);
        $sql_53 = mysqli_query($koneksi, "SELECT sum(mutasid) AS tmutasi_53 FROM $tabel_tujuan WHERE ry='52' ");
                  list($tmutasi_53) = mysqli_fetch_array($sql_53);  
        $sql_54 = mysqli_query($koneksi, "SELECT sum(mutasik) AS tmutasi_54 FROM $tabel_tujuan WHERE ry='52' ");
                  list($tmutasi_54) = mysqli_fetch_array($sql_54);
        $sql_55 = mysqli_query($koneksi, "SELECT sum(mutasid) AS tmutasi_55 FROM $tabel_tujuan WHERE ry='55' ");
                  list($tmutasi_55) = mysqli_fetch_array($sql_55);

                                            
                  if ($kdz=='51') {
                    $jumlah=$tmutasi_51*-1;    
                  } else if ($kdz=='52')  {
                    $jumlah=$tmutasi_52*-1;
                  } else if ($kdz=='53')  {
                    $jumlah=$tmutasi_53 ;
                  } else if ($kdz=='54')  {
                    $jumlah=$tmutasi_54 ;
                  } else if ($kdz=='55')  {
                    $jumlah=$tmutasi_55*-1 ;
                  } else {
                    $jumlah=0 ;
                  }
                  $total_51=($tmutasi_51*-1)+($tmutasi_52*-1)+$tmutasi_53+$tmutasi_54+($tmutasi_55*-1) ;

                $nilai=$jumlah ;
                if ($nilai < 0) {
                    $xnilai= '(' . number_format($nilai*-1).')';
                } else {
                    $xnilai= '' . number_format($nilai).'';
                }

                $nilai_a=$total_51;
                if ($nilai_a < 0) {
                    $xnilai_a= '(' . number_format($nilai_a*-1).')';
                } else {
                    $xnilai_a= '' . number_format($nilai_a).'';
                }                          

$pdf->SetFont('Arial','',11);
$pdf->Cell(15,6,$no++,1,0,'C');
$pdf->Cell(120,6,$z['nama'],1,0,'L');
$pdf->Cell(40,6,$xnilai,1,0,'R');
$pdf->Cell(5,6,'',0,1);

}

$pdf->SetFont('Arial','BI',11);
$pdf->Cell(15,7,'',1,0,'C');
$pdf->Cell(120,7,'Jumlah Arus Kas dari '.$nama.'',1,0,'L');
$pdf->Cell(40,7,$xnilai_a,1,0,'R');
$pdf->Cell(5,7,'',0,1);






                $sqlx = mysqli_query($koneksi, "SELECT nama FROM n_arus_kas WHERE kd='70' limit 1 ");
                list($nama) = mysqli_fetch_array($sqlx);

$pdf->SetFont('Arial','BI',11);
$pdf->Cell(15,8,'',1,0,'C');
$pdf->Cell(120,8,$nama,1,0,'L');
$pdf->Cell(40,8,'',1,0,'C');
$pdf->Cell(5,8,'',0,1);

            $no=1;
            $nol=0;
            //
            $data = mysqli_query($koneksi,"SELECT * FROM n_arus_kas WHERE kd>'70' and kd<'89' order by kd ASC ");
            while($z = mysqli_fetch_array($data)){
              $kdz=$z['kd'];
        $sql_71 = mysqli_query($koneksi, "SELECT sum(mutasik) AS tmutasi_71 FROM $tabel_tujuan WHERE ry='71' ");
                  list($tmutasi_71) = mysqli_fetch_array($sql_71);
        $sql_72 = mysqli_query($koneksi, "SELECT sum(mutasid) AS tmutasi_72 FROM $tabel_tujuan WHERE ry='71' ");
                  list($tmutasi_72) = mysqli_fetch_array($sql_72);
        $sql_73 = mysqli_query($koneksi, "SELECT sum(mutasik) AS tmutasi_73 FROM $tabel_tujuan WHERE ry='73' ");
                  list($tmutasi_73) = mysqli_fetch_array($sql_73);  
        $sql_74 = mysqli_query($koneksi, "SELECT sum(mutasid) AS tmutasi_74 FROM $tabel_tujuan WHERE ry='73' ");
                  list($tmutasi_74) = mysqli_fetch_array($sql_74);
        $sql_75 = mysqli_query($koneksi, "SELECT sum(mutasik-mutasid) AS tmutasi_75 FROM $tabel_tujuan WHERE ry='75' ");
                  list($tmutasi_75) = mysqli_fetch_array($sql_75);

                  if ($kdz=='71') {
                    $jumlah=$tmutasi_71;    
                  } else if ($kdz=='72')  {
                    $jumlah=$tmutasi_72*-1;
                  } else if ($kdz=='73')  {
                    $jumlah=$tmutasi_73 ;
                  } else if ($kdz=='74')  {
                    $jumlah=$tmutasi_74*-1 ;
                  } else if ($kdz=='75')  {
                    $jumlah=$tmutasi_75 ;
                  } else {
                    $jumlah=0 ;
                  }
                  $total_71=$tmutasi_71+($tmutasi_72*-1)+$tmutasi_73+($tmutasi_74*-1)+$tmutasi_75 ;

                $nilai=$jumlah ;
                if ($nilai < 0) {
                    $xnilai= '(' . number_format($nilai*-1).')';
                } else {
                    $xnilai= '' . number_format($nilai).'';
                }

                $nilai_a=$total_71;
                if ($nilai_a < 0) {
                    $xnilai_a= '(' . number_format($nilai_a*-1).')';
                } else {
                    $xnilai_a= '' . number_format($nilai_a).'';
                }                    

$pdf->SetFont('Arial','',11);
$pdf->Cell(15,6,$no++,1,0,'C');
$pdf->Cell(120,6,$z['nama'],1,0,'L');
$pdf->Cell(40,6,$xnilai,1,0,'R');
$pdf->Cell(5,6,'',0,1);

}

$pdf->SetFont('Arial','BI',11);
$pdf->Cell(15,7,'',1,0,'C');
$pdf->Cell(120,7,'Jumlah Arus Kas dari '.$nama.'',1,0,'L');
$pdf->Cell(40,7,$xnilai_a,1,0,'R');
$pdf->Cell(5,7,'',0,1);

$naik_turun=$total_01+$total_51+$total_71 ;

                $nilai_a=$naik_turun;
                if ($nilai_a < 0) {
                    $tnilai_a= '(' . number_format($nilai_a*-1).')';
                } else {
                    $tnilai_a= '' . number_format($nilai_a).'';
                }      

$pdf->SetFont('Arial','B',11);
$pdf->Cell(15,7,'',1,0,'C', true);
$pdf->Cell(120,7,'Kenaikan (Penurunan)',1,0,'L', true);
$pdf->Cell(40,7,$tnilai_a,1,0,'R', true);
$pdf->Cell(5,7,'',0,1);

                  $sql_89 = mysqli_query($koneksi, "SELECT sum(awald) AS t_awald FROM $tabel_tujuan WHERE ry='00' ");
                  list($t_awald) = mysqli_fetch_array($sql_89);

                $nilai_a=$t_awald;
                if ($nilai_a < 0) {
                    $anilai_a= '(' . number_format($nilai_a*-1).')';
                } else {
                    $anilai_a= '' . number_format($nilai_a).'';
                }    

$pdf->SetFont('Arial','B',11);
$pdf->Cell(15,7,'',1,0,'C');
$pdf->Cell(120,7,'Kas & Setara Kas Awal ',1,0,'L');
$pdf->Cell(40,7,$anilai_a,1,0,'R');
$pdf->Cell(5,7,'',0,1);

                  $sql_89 = mysqli_query($koneksi, "SELECT sum(akhird) AS t_akhird FROM $tabel_tujuan WHERE ry='00' ");
                  list($t_akhird) = mysqli_fetch_array($sql_89);

                $nilai_a=$t_akhird;
                if ($nilai_a < 0) {
                    $bnilai_a= '(' . number_format($nilai_a*-1).')';
                } else {
                    $bnilai_a= '' . number_format($nilai_a).'';
                }    

$pdf->SetFont('Arial','B',11);
$pdf->Cell(15,7,'',1,0,'C', true);
$pdf->Cell(120,7,'Kas & Setara Kas Akhir',1,0,'L', true);
$pdf->Cell(40,7,$bnilai_a,1,0,'R', true);
$pdf->Cell(5,7,'',0,1);












date_default_timezone_set('Asia/Jakarta');

$bulan = array(
  1 => 'Januari',
  'Februari',
  'Maret',
  'April',
  'Mei',
  'Juni',
  'Juli',
  'Agustus',
  'September',
  'Oktober',
  'November',
  'Desember'
);

$tgl = date('d');
$bln = date('n');
$thn = date('Y');
//echo $tgl . ' ' . $bulan[$bln] . ' ' . $thn;

$pdf->SetFont('Arial','',9);
$pdf->Cell(60,3,'',0,0,'C');
$pdf->Cell(10,3,'',0,1);  

$pdf->Cell(70,5,'',0,0,'C');
$pdf->Cell(80,5,''.$kc['kota'].',  '.$tgl . ' ' . $bulan[$bln] . ' ' . $thn.'',0,0,'R');
$pdf->Cell(27,5,'',0,0,'C');
$pdf->Cell(10,5,'',0,1);  

$pdf->Cell(90,5,'',0,0,'C');
$pdf->Cell(40,5,$kc['ttd_1'],0,0,'C');
$pdf->Cell(40,5,$kc['ttd_2'],0,0,'C');
$pdf->Cell(27,5,'',0,1,'C');

$pdf->Cell(60,17,'',0,0,'C');
$pdf->Cell(10,17,'',0,1);  

$pdf->Cell(90,5,'',0,0,'C');
$pdf->Cell(40,5,$kc['nama_ttd_1'],0,0,'C');
$pdf->Cell(40,5,$kc['nama_ttd_2'],0,0,'C');
$pdf->Cell(27,5,'',0,1,'C');

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="dokumen.pdf"');
$pdf->Output();
?>