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

session_start();
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');
 

// asline //$pdf->SetMargins(7,8,0,0);
$pdf->SetMargins(19,10,0,0);
$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;
}

function terbilang($nilai) {
  if($nilai<0) {
    $hasil = "minus ". trim(penyebut($nilai));
  } else {
    $hasil = trim(penyebut($nilai));
  }         
  return $hasil . " rupiah";
}
            //////////////////////////////////////////////////////////////////////////// 
            $kdc   =$_SESSION['kdc'];
            $oleh   =$_GET['toleh'];
            //////////////////////////////////////////////////////////////////////////// 
            $oleh   =$_SESSION['id_kar'];
            $xx     = $oleh ;
            $tabel_tujuan   = "nawal_".$xx; 
            $tabel_asal     = "nawal";
            ////////////////////////////////////////////////////////////////////////////     
                $sql0 = mysqli_query($koneksi, "SELECT tgl_htg,tgl_sd FROM $tabel_tujuan WHERE mutasid>=1 limit 1 ");
                list($tgl_htg,$tgl_sd) = mysqli_fetch_array($sql0);            
              $tgl_d    = $tgl_htg;
              $tgl_s    = $tgl_sd;
              
              $tgl_ke_1 =date('2025-01-01');        
              $tgl_p    =strtotime("$tgl_d");
              $tgl_lalu =date('Y-m-d', strtotime("-1 day", $tgl_p));
            ////////////////////////////////////////////////////////////////////////////        
            
$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->SetFont('Arial','B',12);
$pdf->Cell(10,10,' ',0,1);

$pdf->Cell(20,15,'','0',1,'C');
$pdf->Cell(200,8,'LAPORAN POSISI KEUANGAN',0,1,'C');

$pdf->SetFont('Arial','I',10);
$pdf->Cell(200,5,'Per : '.date('d-m-Y', strtotime($tgl_s)).' dan '.date('d-m-Y', strtotime($tgl_lalu)).'',0,1,'C');

$pdf->SetFont('Arial','',9);
$pdf->Cell(60,4,'',0,0,'C');
$pdf->Cell(10,4,'',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(10,9,'No.',1,0,'C', true);
$pdf->Cell(100,9,'Keterangan',1,0,'C', true);
$pdf->Cell(35,9,'Per '.date('d-m-Y', strtotime($tgl_s)).' ',1,0,'C', true);
$pdf->Cell(35,9,'Per '.date('d-m-Y', strtotime($tgl_lalu)).' ',1,0,'C', true);
$pdf->Cell(5,9,'',0,1);
$pdf->SetFont('Arial','',8);
$pdf->SetTextColor(0, 0, 0); // Warna hitam

$pdf->SetFont('Arial','B',12);
$pdf->Cell(10,9,'A.',1,0,'C');
$pdf->Cell(100,9,'A S E T',1,0,'L');
$pdf->Cell(35,9,'',1,0,'C');
$pdf->Cell(35,9,'',1,0,'C');
$pdf->Cell(5,9,'',0,1);

$no=1;
$batas_klp='20';
            $data = mysqli_query($koneksi,"SELECT * FROM $tabel_tujuan WHERE klp<'$batas_klp' and kt<>'X' and (awald+awalk+mutasid+mutasik)<>0 order by noperk ASC ");
            while($d = mysqli_fetch_array($data)){
                
$nilai=$d['akhird'];
if ($nilai < 0) {
    $xnilai= '(' . number_format($nilai*-1).')';
} else {
    $xnilai= '' . number_format($nilai).'';
}

$nilai_a=$d['awald'];
if ($nilai_a < 0) {
    $xnilai_a= '(' . number_format($nilai_a*-1).')';
} else {
    $xnilai_a= '' . number_format($nilai_a).'';
}
$pdf->SetFont('Arial','',10);
  $pdf->Cell(10,7, $no++,1,0,'C');
  $pdf->Cell(100,7,$d['perk'],1,0,'L');
  $pdf->Cell(35,7,$xnilai,1,0,'R'); 
  $pdf->Cell(35,7,$xnilai_a,1,0,'R');
  $pdf->Cell(5,7,'',0,1);  

}
    $sql2 = mysqli_query($koneksi, "SELECT sum(awald) AS tawald,sum(awalk) AS tawalk,sum(mutasid) AS tmutd,sum(mutasik) AS tmutk,sum(akhird) AS takhird,sum(akhirk) AS takhirk FROM $tabel_tujuan WHERE kt<>'X' and klp<'$batas_klp' ");
    list($awald,$awalk,$tmutd,$tmutk,$takhird,$takhirk) = mysqli_fetch_array($sql2);
    
$pdf->SetFillColor(220, 220, 220); // Warna abu-abu

$pdf->SetFont('Arial','B',10);
$pdf->Cell(110,7,'TOTAL ASET',1,0,'C', true);
$pdf->Cell(35,7,'' . number_format($takhird).'',1,0,'R', true);
$pdf->Cell(35,7,'' . number_format($awald).'',1,0,'R', true);
$pdf->Cell(5,7,'',0,1);  
  

$pdf->SetFont('Arial','B',12);
$pdf->Cell(10,9,'B.',1,0,'C');
$pdf->Cell(100,9,'LIABILITAS DAN EQUITAS',1,0,'L');
$pdf->Cell(35,9,'',1,0,'C');
$pdf->Cell(35,9,'',1,0,'C');
$pdf->Cell(5,9,'',0,1);

$pdf->SetFont('Arial','',10);
$pdf->Cell(10,9,'B.1.',1,0,'C');
$pdf->Cell(100,9,'LIABILITAS',1,0,'L');
$pdf->Cell(35,9,'',1,0,'C');
$pdf->Cell(35,9,'',1,0,'C');
$pdf->Cell(5,9,'',0,1);

$no=1;
$batas_klp_b='30';
            $data = mysqli_query($koneksi,"SELECT * FROM $tabel_tujuan WHERE klp>'$batas_klp' and klp<'$batas_klp_b' and kt<>'X' and (awald+awalk+mutasid+mutasik)<>0 order by noperk ASC ");
            while($d = mysqli_fetch_array($data)){

$nilai=$d['akhirk'];
if ($nilai < 0) {
    $xnilai= '(' . number_format($nilai*-1).')';
} else {
    $xnilai= '' . number_format($nilai).'';
}

$nilai_a=$d['awalk'];
if ($nilai_a < 0) {
    $xnilai_a= '(' . number_format($nilai_a*-1).')';
} else {
    $xnilai_a= '' . number_format($nilai_a).'';
}

  $pdf->Cell(10,7, $no++,1,0,'C');
  $pdf->Cell(100,7,$d['perk'],1,0,'L');
  $pdf->Cell(35,7,$xnilai,1,0,'R'); 
  $pdf->Cell(35,7,$xnilai_a,1,0,'R');
  $pdf->Cell(5,7,'',0,1);  

}
    $sql2 = mysqli_query($koneksi, "SELECT sum(awald) AS tawald,sum(awalk) AS tawalk,sum(mutasid) AS tmutd,sum(mutasik) AS tmutk,sum(akhird) AS takhird,sum(akhirk) AS takhirk FROM $tabel_tujuan WHERE kt<>'X' and klp>'$batas_klp' and klp<'$batas_klp_b' ");
    list($awald,$awalk,$tmutd,$tmutk,$takhird,$takhirk) = mysqli_fetch_array($sql2);
    
$pdf->SetFillColor(220, 220, 220); // Warna abu-abu

$pdf->SetFont('Arial','B',10);
$pdf->Cell(110,7,'TOTAL LIABILITAS',1,0,'C');
$pdf->Cell(35,7,'' . number_format($takhirk).'',1,0,'R');
$pdf->Cell(35,7,'' . number_format($awalk).'',1,0,'R');
$pdf->Cell(5,7,'',0,1);  


$pdf->SetFont('Arial','',10);
$pdf->Cell(10,9,'B.2.',1,0,'C');
$pdf->Cell(100,9,'EQUITAS',1,0,'L');
$pdf->Cell(35,9,'',1,0,'C');
$pdf->Cell(35,9,'',1,0,'C');
$pdf->Cell(5,9,'',0,1);

$no=1;
$batas_klp_c='40';
            $data = mysqli_query($koneksi,"SELECT * FROM $tabel_tujuan WHERE klp>'$batas_klp_b' and klp<'$batas_klp_c' and kt<>'X' and (awald+awalk+mutasid+mutasik)<>0 order by noperk ASC ");
            while($d = mysqli_fetch_array($data)){

$nilai=$d['akhirk'];
if ($nilai < 0) {
    $xnilai= '(' . number_format($nilai*-1).')';
} else {
    $xnilai= '' . number_format($nilai).'';
}

$nilai_a=$d['awalk'];
if ($nilai_a < 0) {
    $xnilai_a= '(' . number_format($nilai_a*-1).')';
} else {
    $xnilai_a= '' . number_format($nilai_a).'';
}    
  $pdf->Cell(10,7, $no++,1,0,'C');
  $pdf->Cell(100,7,$d['perk'],1,0,'L');
  $pdf->Cell(35,7,$xnilai,1,0,'R'); 
  $pdf->Cell(35,7,$xnilai_a,1,0,'R');
  $pdf->Cell(5,7,'',0,1);  

}
    $sql2 = mysqli_query($koneksi, "SELECT sum(awald) AS tawald,sum(awalk) AS tawalk,sum(mutasid) AS tmutd,sum(mutasik) AS tmutk,sum(akhird) AS takhird,sum(akhirk) AS takhirk FROM $tabel_tujuan WHERE kt<>'X' and klp>'$batas_klp_b' and klp<'$batas_klp_c' ");
    list($awald,$awalk,$tmutd,$tmutk,$takhird,$takhirk) = mysqli_fetch_array($sql2);

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

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

$pdf->SetFillColor(220, 220, 220); // Warna abu-abu

$pdf->SetFont('Arial','B',10);
$pdf->Cell(110,7,'TOTAL EQUITAS',1,0,'C');
  $pdf->Cell(35,7,$xnilai,1,0,'R'); 
  $pdf->Cell(35,7,$xnilai_a,1,0,'R');
$pdf->Cell(5,7,'',0,1);  

    $sql2 = mysqli_query($koneksi, "SELECT sum(awald) AS tawald,sum(awalk) AS tawalk,sum(mutasid) AS tmutd,sum(mutasik) AS tmutk,sum(akhird) AS takhird,sum(akhirk) AS takhirk FROM $tabel_tujuan WHERE kt<>'X' and klp>'$batas_klp' and klp<'$batas_klp_c' ");
    list($awald,$awalk,$tmutd,$tmutk,$takhird,$takhirk) = mysqli_fetch_array($sql2);
    
$pdf->SetFillColor(220, 220, 220); // Warna abu-abu

$pdf->SetFont('Arial','B',10);
$pdf->Cell(110,7,'TOTAL LIABILITAS & EQUITAS',1,0,'C', true);
$pdf->Cell(35,7,'' . number_format($takhirk).'',1,0,'R', true);
$pdf->Cell(35,7,'' . number_format($awalk).'',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();
?>