<?php
class Pdf extends PDF_MC_Table{
	//Page header
	//public $kas_masuk,$tgl1,$tgl2;
    public $paper_w = 210, $paper_h = 297;

	function __construct() {
		//set Page 
		parent::__construct('P','mm',array($this->paper_w , $this->paper_h));
    }

	function Header(){
		if($this->PageNo() != 0){		
            $this->setFont('Tahoma','B',12);
            $this->setFillColor(255,255,255);
            $this->setTextColor(0,0,0);
            
            $this->cell(100, 5, $this->judul, 0, 0, "L");
            $this->Ln();

            $y = $this->getY();
            $this->setFont('Tahoma','',9);
            $this->cell(20, 5, "Pelanggan", 0, 0, "L");
            $this->cell(5, 5, ":", 0, 0, "L");
            $this->MultiCell(100, 5, $this->pelanggan->nama, "FJ");

            $this->cell(20, 5, "Periode", 0, 0, "L");
            $this->cell(5, 5, ":", 0, 0, "L");
            $this->MultiCell(100, 5, $this->periode, "FJ");

            $this->Ln();

            $this->setFont('Tahoma','B',9);

            $this->setWidths([
                    $this->paper_w*4/100,
                    $this->paper_w*9/100,
                    $this->paper_w*12/100,
                    $this->paper_w*27/100,
                    $this->paper_w*13/100,
                    $this->paper_w*13/100,
                    $this->paper_w*13/100,
                ]);
            $this->setAligns([
                "C",
                "C",
                "C",
                "C",
                "C",
                "C",
                "C",
            ]);
            $this->Row([
                    "No",
                    "Tanggal",
                    "No Faktur",
                    "Keterangan",
                    "Penjualan",
                    "Pembayaran",
                    "Saldo",
                ], 8);
        }
	}
	
	function Content()
	{           
        $this->setFont('Tahoma','',9);

        $total = 0;
        $total_penjualan = 0;
        $total_pembayaran = 0;
        $saldo = 0;
        $no = 1;
        if($this->saldo_awal != null){
            // $saldo_awal = $this->saldo_awal;
            $saldo_awal_keterangan = "SALDO AWAL";
            // $saldo = ($saldo_awal->total_retur != null) ? ($saldo_awal->nominal_netto-$saldo_awal->total_bayar)-$saldo_awal->total_retur : $saldo_awal->nominal_netto-$saldo_awal->total_bayar;
            $saldo = $this->saldo_awal;
        }else{
            $saldo_awal_keterangan = "SALDO AWAL";
            $saldo = 0;
        }

            $this->setWidths([
                    $this->paper_w*4/100,
                    $this->paper_w*9/100,
                    $this->paper_w*12/100,
                    $this->paper_w*27/100,
                    $this->paper_w*13/100,
                    $this->paper_w*13/100,
                    $this->paper_w*13/100,
                ]);
            $this->setAligns([
                "C",
                "L",
                "L",
                "L",
                "R",
                "R",
                "R",
            ]);
            $this->Row_bawahputus([
                    $no,
                    "",
                    "",
                    $saldo_awal_keterangan,
                    "",
                    "",
                    rupiah2($saldo, 0),
                ], 5, ['valign'=>'M']);
            $no++;

        $sub_saldo = $saldo;

        if (count($this->data) > 0) {
            foreach ($this->data as $key => $d) {
                if ($d->flag == "piutang") {
                    $sub_saldo += ($d->nominal_total*$d->kurs_jual)-($d->pembayaran*$d->kurs_jual);
                } else if ($d->flag == "retur_tagihan") {
                    $sub_saldo += $d->pembayaran*$d->kurs_jual;
                } else if ($d->flag == "jurnal_peny_piutang") {
                    if ($d->tipe_akun == "D") {
                        $sub_saldo -= $d->pembayaran*$d->kurs_jual;
                    } else {
                        $sub_saldo += $d->pembayaran*$d->kurs_jual;
                    }
                } else {
                    $sub_saldo -= ($d->nominal_total*$d->kurs_jual)-($d->pembayaran*$d->kurs_jual);
                }

                $this->setWidths([
                        $this->paper_w*4/100,
                        $this->paper_w*9/100,
                        $this->paper_w*12/100,
                        $this->paper_w*27/100,
                        $this->paper_w*13/100,
                        $this->paper_w*13/100,
                        $this->paper_w*13/100,
                    ]);
                $this->setAligns(["C", "L", "L", "L", "R", "R", "R"]);

                if ($d->flag == "piutang") {
                    $penjualan = $d->nominal_total*$d->kurs_jual;
                } else if ($d->flag == "retur_tagihan") {
                    $penjualan = $d->nominal_total*$d->kurs_jual;
                } else {
                    $penjualan = abs($d->nominal_total*$d->kurs_jual) * -1;
                }

                if ($d->flag == "jurnal_peny_piutang") {
                    if ($d->tipe_akun == "D") {
                        $pembayaran = $d->pembayaran*$d->kurs_jual;
                    } else {
                        $pembayaran = abs($d->pembayaran*$d->kurs_jual) * -1;
                    }
                } else if ($d->flag == "retur_tagihan") {
                    $pembayaran = abs($d->pembayaran*$d->kurs_jual) * -1;
                } else {
                    $pembayaran = $d->pembayaran*$d->kurs_jual;
                }

                $this->Row_bawahputus([
                        $no, 
                        tgl_full($d->tanggal, ''), 
                        $d->nomor.$d->sufix, 
                        $d->keterangan,
                        setNominal($penjualan, 0), 
                        setNominal($pembayaran, 0), 
                        setNominal($sub_saldo, 0),
                    ], 5, ['valign'=>'M']);

                $no++;

                $total_penjualan += $penjualan;
                $total_pembayaran += $pembayaran;
            }
        }

        // if(count($this->data) > 0){
        //     foreach($this->data as $d){
        //         // if($d->nomor=="0" || $d->nomor==NULL){
        //         //     continue;
        //         // }

        //         if ($d->mata_uang != 1) {
        //             // $sub_saldo += ($d->penjualan-$d->pembayaran)*$d->kurs_jual;
        //             if ($d->flag == "piutang") {
        //                 $sub_saldo += ($d->nominal_total-$d->pembayaran)*$d->kurs_jual;
        //             } else if ($d->flag == "retur_tagihan") {
        //                 $sub_saldo += $d->pembayaran*$d->kurs_jual;
        //             } else {
        //                 $sub_saldo -= ($d->nominal_total-$d->pembayaran)*$d->kurs_jual;
        //             }
        //         } else {
        //             // $sub_saldo += $d->penjualan-$d->pembayaran;
        //             if ($d->flag == "piutang") {
        //                 $sub_saldo += $d->nominal_total-$d->pembayaran;
        //             } else if ($d->flag == "retur_tagihan") {
        //                 $sub_saldo += $d->pembayaran;
        //             } else if ($d->flag == "jurnal_peny_piutang") {
        //                 if ($d->tipe_akun == "D") {
        //                     $sub_saldo += $d->pembayaran;
        //                 } else {
        //                     $sub_saldo -= $d->pembayaran;
        //                 }
        //             } else {
        //                 $sub_saldo -= $d->nominal_total-$d->pembayaran;
        //             }
        //         }

        //         $this->setWidths([
        //                 $this->paper_w*4/100,
        //                 $this->paper_w*9/100,
        //                 $this->paper_w*12/100,
        //                 $this->paper_w*27/100,
        //                 $this->paper_w*13/100,
        //                 $this->paper_w*13/100,
        //                 $this->paper_w*13/100,
        //             ]);
        //         $this->setAligns([
        //             "C",
        //             "L",
        //             "L",
        //             "L",
        //             "R",
        //             "R",
        //             "R",
        //         ]);

        //         if ($d->mata_uang != 1) {
        //             if ($d->flag == "piutang") {
        //                 $penjualan = rupiah2($d->nominal_total*$d->kurs_jual, 0);
        //             } else if ($d->flag == "retur_tagihan") {
        //                 $penjualan = rupiah2($d->nominal_total*$d->kurs_jual, 0);
        //             } else {
        //                 $penjualan = "(".rupiah2($d->nominal_total*$d->kurs_jual, 0).")";
        //             }

        //             $this->Row_bawahputus([
        //                     $no, 
        //                     tgl_full($d->tanggal, ''), 
        //                     $d->nomor.$d->sufix, 
        //                     $d->keterangan,
        //                     $penjualan, 
        //                     ($d->flag == "retur_tagihan") ? "(".rupiah2($d->pembayaran, 0).")" : rupiah2($d->pembayaran, 0), 
        //                     setNominal($sub_saldo, 0),
        //                 ], 5, ['valign'=>'M']);
        //         } else {
        //             if ($d->flag == "piutang") {
        //                 $penjualan = rupiah2($d->nominal_total*$d->kurs_jual, 0);
        //             } else if ($d->flag == "retur_tagihan") {
        //                 $penjualan = rupiah2($d->nominal_total*$d->kurs_jual, 0);
        //             } else {
        //                 $penjualan = "(".rupiah2($d->nominal_total*$d->kurs_jual, 0).")";
        //             }

        //             $pembayaran = $d->pembayaran;

        //             if ($d->flag == "jurnal_peny_piutang") {
        //                 if ($d->tipe_akun == "D") {
        //                     $pembayaran += $d->pembayaran;
        //                 } else {
        //                     $pembayaran -= $d->pembayaran;
        //                 }
        //             }
        //             $this->Row_bawahputus([
        //                     $no, 
        //                     tgl_full($d->tanggal, ''), 
        //                     $d->nomor.$d->sufix, 
        //                     $d->keterangan,
        //                     $penjualan, 
        //                     ($d->flag == "retur_tagihan") ? "(".rupiah2($pembayaran, 0).")" : rupiah2($pembayaran, 0), 
        //                     setNominal($sub_saldo, 0),
        //                 ], 5, ['valign'=>'M']);
        //         }

        //         $no++;

        //         if ($d->mata_uang != 1) {
        //             if ($d->flag == "piutang") {
        //                 $total_penjualan += $d->nominal_total*$d->kurs_jual;
        //                 $total_pembayaran += $d->pembayaran*$d->kurs_jual;
        //             } else if ($d->flag == "retur_tagihan") {
        //                 $total_penjualan += $d->nominal_total*$d->kurs_jual;
        //                 $total_pembayaran -= $d->pembayaran*$d->kurs_jual;
        //             } else {
        //                 $total_penjualan -= $d->nominal_total*$d->kurs_jual;
        //                 $total_pembayaran += $d->pembayaran*$d->kurs_jual;
        //             }
        //         } else {
        //             if ($d->flag == "piutang") {
        //                 $total_penjualan += $d->nominal_total;
        //                 $total_pembayaran += $d->pembayaran;
        //             } else if ($d->flag == "retur_tagihan") {
        //                 $total_penjualan += $d->nominal_total;
        //                 $total_pembayaran -= $d->pembayaran;
        //             } else {
        //                 $total_penjualan -= $d->nominal_total;
        //                 $total_pembayaran += $d->pembayaran;
        //             }
        //         }
        //     }
        // }
        
        $saldo_piutang = $sub_saldo;

        $this->setFont('Tahoma','B',9);
        $this->setWidths([
                $this->paper_w*52/100,
                $this->paper_w*13/100,
                $this->paper_w*13/100,
                $this->paper_w*13/100,
            ]);
        $this->setAligns([
            "R",
            "R",
            "R",
            "R",
        ]);
        $this->Row([
                "Jumlah",
                $this->setNominal($total_penjualan),
                $this->setNominal($total_pembayaran),
                $this->setNominal($saldo_piutang),
            ], 8);

        $this->setFont('Tahoma','',9);
        $this->setFillColor(255,255,255);
        $this->setTextColor(0,0,0);
        $this->ln();

        $this->setX($this->paper_w*45/100);
        $this->cell(50, 5, "Mengetahui", "TLR", 0, "C");
        $this->cell(50, 5, "Dilaporkan Oleh : ", "TLR", 1, "C");

        $this->setX($this->paper_w*45/100);
        $this->cell(50, 15, "", "BLR", 0, "C");
        $this->cell(50, 15, "", "BLR", 1, "C");
	}
	
	function Footer()
	{
        //
	}

    function setNominal($value=0)
    {
        if($value < 0){
            $set = "( ".rupiah2(abs($value))." )";
        }else{
            $set = rupiah2($value);
        }
        return $set;
    }
}

$Pdf = new Pdf();
$Pdf->judul		= $data['judul'];
$Pdf->periode 	= $data['periode'];
$Pdf->saldo_awal= $data['saldo_awal'];
$Pdf->data      = $data['data'];
$Pdf->pelanggan = $data['pelanggan'];
$count 			= (count($data['data']) > 0) ? $data['data'][0]->nama_pelanggan.' ('.$data['data'][0]->pic.')':"";

$Pdf->AddFont("Tahoma","","tahoma.php");
$Pdf->AddFont("Tahoma","B","tahomabd.php");
$Pdf->SetAutoPageBreak(true ,15);
$Pdf->SetMargins(10,10,10);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Tahoma','',12);
$Pdf->Content();
$Pdf->SetTitle($data['judul']);
$Pdf->Output($data['judul'].".pdf", "I");

?>