<?php
class Pdf extends PDF_MC_Table{
	//Page header
	//public $kas_masuk,$tgl1,$tgl2;
	function __construct() {
		//set Page 
		parent::__construct('P','mm',array(210 ,330));
    }
	function Header(){
        
		if($this->PageNo() != 0){		
                $this->SetMargins(3, 3, 3);
                $this->setFont('Arial','B',12);
                $this->setFillColor(255,255,255);
                $this->setTextColor(0,0,0);
                
                //$this->cell(100, 5, "Detail Penjualan", 0, 1, "L");
                $this->SetX(3);
                $this->cell(100, 5, "Laporan Penjualan Per Pelanggan (Detail)", 0, 1, "L");

                $this->setFont('Arial','',10);
                $this->cell(20, 5, "Langganan", 0, 0, "L");
                $this->cell(5, 5, ":", 0, 0, "L");
                $this->MultiCell(100, 5, $this->pelanggan->nama, "FJ");
                $this->setFont('Arial','',10);
                $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('Arial','B',10);

                $x = $this->GetX();
                $y = $this->GetY();

                $this->MultiCell(21, 7, "Tanggal", 1, "C", 1);

                $this->SetXY($x+21, $y);
                $this->MultiCell(26, 7, "No. Faktur", 1, "C", 1);

                $this->SetXY($x+47, $y);
                $this->MultiCell(50, 7, "Nama Barang", 1, "C", 1);

                $this->SetXY($x+97, $y);
                $this->MultiCell(10, 7, "Qty", 1, "C", 1);

                $this->SetXY($x+107, $y);
                $this->MultiCell(24, 7, "Hrg Stn", 1, "C", 1);

                $this->SetXY($x+131, $y);
                $this->MultiCell(25, 7, "Jumlah", 1, "C", 1);

                $this->SetXY($x+156, $y);
                $this->MultiCell(20, 7, "Diskon", 1, "C", 1);

                $this->SetXY($x+176, $y);
                $this->MultiCell(26, 7, "Netto", 1, "C", 1);
                
            }
				
	}
	
	function Content()
	{
        $set_font_size = 10;
        $set_h = 5;
        $this->setFont('Arial','', $set_font_size);

        $jml_penjualan = 0;
        $jml_harga_satuan = 0;
        $total_penjualan = 0;
        $total_diskon = 0;
        $total_piutang = 0;
        $row_piutang = 0;
        if(count($this->data) > 0){
            foreach($this->faktur AS $id=>$value){
                $no = 1;
                $temp_nomor_penjualan = "";
                $temp_id_penjualan = "";

                $x = $this->GetX();
                
                foreach($this->data as $key=>$d){
                    $y = $this->GetY();

                    if($d->id_penjualan!=$value->id_penjualan){
                        continue;
                    }

                    $penjualan = $d->jml_barang*$d->harga_satuan;
                    $potongan = $d->jml_barang*$d->potongan;
                    $piutang = $penjualan-$potongan;

                    $set_nomor_penjualan = "";
                    $set_tanggal_fakur = "";
                    if($temp_nomor_penjualan != nomor_penjualan($d)){
                        $set_nomor_penjualan = nomor_penjualan($d);
                        $set_tanggal_fakur = tgl_full($d->tanggal_faktur, '');

                    }else{
                        $set_nomor_penjualan = "";
                        $set_tanggal_fakur = "";
                    }

                    $this->setFont('Arial','', $set_font_size);

                    $this->cell(21,5,$set_tanggal_fakur,1,0,'C',0);
                    $this->cell(26,5,$set_nomor_penjualan,1,0,'L',0);
                    $this->cell(50,5,$d->nama_barang,1,0,'L',0);
                    //$this->cell(15,5,$d->jml_barang.' '.$d->satuan,1,0,'C',0);
                    $this->cell(10,5,$d->jml_barang,1,0,'C',0);
                    $this->cell(24,5,rupiah2($d->harga_satuan, 0),1,0,'R',0);
                    $this->cell(25,5,rupiah2($penjualan, 0),1,0,'R',0);
                    $this->cell(20,5,rupiah2($potongan, 0),1,0,'R',0);
                    $this->cell(26,5,rupiah2($piutang, 0),1,1,'R',0);

                    /*$this->MultiCell(21, 5, $set_tanggal_fakur, 1);

                    $this->SetXY($x+21, $y);
                    $this->MultiCell(26, 5, $set_nomor_penjualan, 1);

                    $this->SetXY($x+47, $y);
                    $this->MultiCell(50, 5, $d->nama_barang, 1);

                    $this->SetXY($x+97, $y);
                    $this->MultiCell(10, 5, $d->jml_barang, 1, "C");

                    $this->SetXY($x+107, $y);
                    $this->MultiCell(24, 5, rupiah2($d->harga_satuan, 0), 1, "R");

                    $this->SetXY($x+131, $y);
                    $this->MultiCell(25, 5, rupiah2($penjualan, 0), 1, "R");

                    $this->SetXY($x+156, $y);
                    $this->MultiCell(20, 5, rupiah2($potongan, 0), 1, "R");

                    $this->SetXY($x+176, $y);
                    $this->MultiCell(26, 5, rupiah2($piutang, 0), 1, "R");*/

                    $no++;
                    $jml_penjualan += $d->jml_barang;
                    $jml_harga_satuan += $d->harga_satuan;
                    $total_penjualan += $penjualan;
                    $total_diskon += $potongan;
                    $total_piutang += $piutang;
                    $row_piutang += $piutang;

                    $temp_nomor_penjualan = nomor_penjualan($d);
                    $temp_id_penjualan = $d->id_penjualan;

                }

                $this->setFont('Arial','B', $set_font_size);
                $this->cell(176,7,"Sub Total",1,0,'R',0);
                $this->cell(26,7,rupiah2($row_piutang, 0),1,1,'R',0);
                $row_piutang = 0;
            }
        }
        $this->setFont('Arial','B', $set_font_size);
        $this->cell(94,7,"Total",1,0,'R',0);
        $this->cell(10,7,$jml_penjualan,1,0,'C',0);
        $this->cell(25,7,rupiah2($jml_harga_satuan, 0),1,0,'R',0);
        $this->cell(27,7,rupiah2($total_penjualan, 0),1,0,'R',0);
        $this->cell(20,7,rupiah2($total_diskon, 0),1,0,'R',0);
        $this->cell(27,7,rupiah2($total_piutang, 0),1,1,'R',0);
        
	}
	
	function Footer()
	{
       
	}
}
$Pdf = new Pdf();
$Pdf->periode=$data['periode'];
$Pdf->faktur=$data['faktur'];
$Pdf->data=$data['data'];
$Pdf->pelanggan=$data['pelanggan'];

$Pdf->SetAutoPageBreak(true ,15);

$Pdf->SetMargins(10,10,10);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Arial','',12);
$Pdf->Content();
$Pdf->SetTitle("Detail Persediaan Keseluruhan periode ".$data['periode']);
$Pdf->AutoPrint(false);
$Pdf->Output("Detail Persediaan Keseluruhan periode ".$data['periode'].".pdf", "I");

?>