<?php

CLASS efak extends RasioProject
{
	protected $set;
	protected $enableCk = false;
	protected $report;
	protected $baseTitle;

	public function __construct($config)
	{
		$this->getConfig($config);
		$this->validation();
		$this->set();
	}

	protected function set()
	{
		$this->report       	= new Report;
		$this->set->em      	= $this->em;
		$this->set->kode    	= 'ACC-001';
		$this->set->date      	= date("d/m/Y H:i");
		$this->set->modul     	= 'E-Faktur';
		$this->set->form      	= $this->setForm();
		$this->set->display      = $this->report();
		$this->set->filter    	= $this->report->setFilter($_GET, $this->setForm());
		$this->set->notNull   	= array("date1", "date2", "paper");
		$this->set->button    	= array("Cetak", "Csv");
		$this->set->groupDis  	= array("ap");
		$this->set->groupBy   	= "nonjl";
		$this->set->hitung    	= array("total", "dpp", "ppn_nil");
		$this->set->rowPage   	= 1000000000;
		$this->baseTitle      	= $this->set->modul;
		return $this->set;
	}

	protected function defValue()
	{
		$lap = new Lap;
		$res->cabang  = '';
		return $res; 
	}

	protected function setForm()
	{
		$this->defVal = $this->defValue();

		if(!empty($_GET['nonjl'])){
			$form[] = array('row' =>  0,  'col' => 25,   "label" => array("name" => "No.Nota",   "width" => 100),  "input" => array("type" => 'text',  'attr' => array("name" => 'nonjl')));
		}
		
		$row = 0;
		$form[] = array('row' =>  $row,  'col' => 25,   "label" => array("name" => "Periode",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'date1', "size" => 10, 'class' => 'tanggal', 'value' => date("01/m/Y"))));
		$form[] = array('row' =>  $row,  'col' => 250, "label" => array("name" => "s/d",     "width" => 30),   "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'date2', "size" => 10, 'class' => 'tanggal', 'value' => date("d/m/Y"))));
		
		$row = $row + 25;
		$form[] = array('row' =>  $row,  'col' => 25,   "label" => array("name" => "No. FP",   "width" => 100),  "input" => array("type" => 'text',  'attr' => array("name" => 'nofp1', "size" => 20)));
		$form[] = array('row' =>  $row,  'col' => 300, "label" => array("name" => "s/d",     "width" => 30),   "input" => array("type" => 'text',  'attr' => array("name" => 'nofp2', "size" => 20)));

		$row = $row + 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Profit Center", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_cabang', "size" => 9, 'after' => 'load("cabang", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_cabang=>nm_cabang')));
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_cabang', "size" => 30, 'readonly' => true)));

		$row = $row + 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Customer", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub', "size" => 9, 'after' => 'load("mastercust", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_sub=>nm_sub')));
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub', "size" => 30, 'readonly' => true)));


		$row = $row + 25;
		$form[] = array('row' =>  $row,  'col' => 25,   "label" => array("name" => "Paper",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'paper', "size" => 2, 'value' => 'P', 'after' => 'select()', "select" => 'paper')));

		return $form;
	}

	protected function report()
	{
		$tmp = array("FK","KD_JENIS_TRANSAKSI","FG_PENGGANTI","NOMOR_FAKTUR","MASA_PAJAK","TAHUN_PAJAK","TANGGAL_FAKTUR","NPWP","NAMA","ALAMAT_LENGKAP","JUMLAH_DPP","JUMLAH_PPN","JUMLAH_PPNBM","ID_KETERANGAN_TAMBAHAN","FG_UANG_MUKA","UANG_MUKA_DPP","UANG_MUKA_PPN","UANG_MUKA_PPNBM","REFERENSI");
		
		foreach($tmp as $r){
			$id = strtolower($r);
			$res[] = array('id' => $id,		'nama' => $r, 		'type' => 'text', 'width' => 100,  	'size' => 1000);
		}

		return $res;
	}

	public function genData()
	{
		$filter = $this->set->filter;

		$sql = $this->em->addSelect("
						a.*, b.*, c.npwp
						, to_char(a.tgl_fp, 'dd/mm/yyyy') as tgl
						, to_char(a.tgl_fp, 'mm') as bulan
						, to_char(a.tgl_fp, 'yyyy') as tahun
						, c.alamat, c.alamat2, c.alamat3
						, c.alamat_npwp, c.alamat_npwp2, c.alamat_npwp3
						, c.nppkp
						");
		$sql = $this->em->getFrom("njl", 'a');
		$sql = $this->em->addLeftJoin("njld", 'b', array("a.nonjl = b.nonjl"));
		$sql = $this->em->addLeftJoin("sub", 'c', array("a.kd_sub = c.kd_sub"));
		$sql = $this->em->getWhere("a.control = '1'");
		$sql = $this->em->andWhere("a.tgl_fp is not null");
		if(!empty($filter->date1->value)){
			$sql = $this->em->andWhere("a.tgl_fp >= '".date('Y-m-d 00:00:00', strtotime($filter->date1->value))."'");
		}
		if(!empty($filter->date2->value)){
			$sql = $this->em->andWhere("a.tgl_fp <= '".date('Y-m-d 23:59:59', strtotime($filter->date2->value))."'");
		}
		if(!empty($filter->kd_cabang->value)){
			$sql = $this->em->andWhere("a.kd_cabang = '".$filter->kd_cabang->value."'");
		}
		if(!empty($filter->nofp1->value)){
			$sql = $this->em->andWhere("a.no_fp >= '".$filter->nofp1->value."'");
		}
		if(!empty($filter->nofp2->value)){
			$sql = $this->em->andWhere("a.no_fp <= '".$filter->nofp2->value."'");
		}
		if(!empty($filter->kd_sub->value)){
			$sql = $this->em->andWhere("a.kd_sub = '".$filter->kd_sub->value."'");
		}

		$sql = $this->em->getMultiSort("a.tgl_fp asc, a.no_fp asc");

		$hed = array("FK","KD_JENIS_TRANSAKSI","FG_PENGGANTI","NOMOR_FAKTUR","MASA_PAJAK","TAHUN_PAJAK","TANGGAL_FAKTUR","NPWP","NAMA","ALAMAT_LENGKAP","JUMLAH_DPP","JUMLAH_PPN","JUMLAH_PPNBM","ID_KETERANGAN_TAMBAHAN","FG_UANG_MUKA","UANG_MUKA_DPP","UANG_MUKA_PPN","UANG_MUKA_PPNBM","REFERENSI");
		
		$res = array();
		
		$tmp = array(
				array("LT","NPWP","NAMA","JALAN","BLOK","NOMOR","RT","RW","KECAMATAN","KELURAHAN","KABUPATEN","PROPINSI","KODE_POS","NOMOR_TELEPON"),
				array("OF","KODE_OBJEK","NAMA","HARGA_SATUAN","JUMLAH_BARANG","HARGA_TOTAL","DISKON","DPP","PPN","TARIF_PPNBM","PPNBM"),
			);
			
		foreach($tmp as $tmp2){
			$i = "";
			foreach($hed as $k => $r){
				$id = strtolower($r);
				$i->$id = $tmp2[$k];
			}
			$res[] = $i;
		}
		
		$eks = $this->em->queryEks($sql);
		$tmp = array();
		foreach($this->em->getFetchObject($eks) as $r){
			$tmp[$r->nonjl][] = $r;
		}
		
		$hed = array("FK","KD_JENIS_TRANSAKSI","FG_PENGGANTI","NOMOR_FAKTUR","MASA_PAJAK","TAHUN_PAJAK","TANGGAL_FAKTUR","NPWP","NAMA","ALAMAT_LENGKAP","JUMLAH_DPP","JUMLAH_PPN","JUMLAH_PPNBM","ID_KETERANGAN_TAMBAHAN","FG_UANG_MUKA","UANG_MUKA_DPP","UANG_MUKA_PPN","UANG_MUKA_PPNBM","REFERENSI");
		
		$kode = new Kode;
		foreach($tmp as $k => $r){
			
			$row = $r[0];
			if(empty($row->nppkp)){
				$row->nppkp = $row->nm_sub;
			}
			
			$alamat = $row->alamat_npwp;
			$alamat.= ' '.$row->alamat_npwp2;
			$alamat.= ' '.$row->alamat_npwp3;
			$alamat = trim($alamat);
			if(empty($alamat)){
				$alamat = $row->alamat;
				$alamat.= ' '.$row->alamat2;
				$alamat.= ' '.$row->alamat3;
				$alamat = trim($alamat);
			}
			
			$row->ppn_nilai = round($row->ppn_nilai);
			
			$i = '';
			$i->fk 					= 'FK';
			$i->kd_jenis_transaksi 	= '01';
			$i->fg_pengganti 		= 0;
			$i->nomor_faktur 		= $row->no_fp;
			$i->masa_pajak 			= $row->bulan;
			$i->tahun_pajak 		= $row->tahun;
			$i->tanggal_faktur 		= $row->tgl;
			$i->npwp 				= str_ireplace("-", "", str_ireplace(".", "", $row->npwp));
			$i->nama 				= $row->nppkp;
			$i->alamat_lengkap 		= $alamat;
			$i->jumlah_dpp 			= $row->gtot - $row->ppn_nilai;
			$i->jumlah_ppn 			= $row->ppn_nilai;
			$i->jumlah_ppnbm 		= 0;
			$res[] = $i;
			
			foreach($r as $k2 => $r2){
				$tot = $kode->totBertingkat($r2);
				
				if(strtoupper($r2->ppn) == 'I'){
					$ppn = $tot['tot'] / 11;
					$ppn = round($ppn);
					$dpp = $tot['tot'] - $ppn;
				}
				if(strtoupper($r2->ppn) == 'E'){
					$dpp = $tot['tot'];
					$ppn = ($tot['tot'] * 10) / 100;
					$ppn = round($ppn);
				}
				
				$i = '';
				$i->fk 					= 'OF';
				$i->kd_jenis_transaksi 	= $r2->kd_inv;
				$i->fg_pengganti 		= $r2->nm_inv;
				$i->nomor_faktur 		= $r2->harga;
				$i->masa_pajak 			= $r2->qty;
				$i->tahun_pajak 		= $tot['bruto'];
				$i->tanggal_faktur 		= $tot['dis'];
				$i->npwp 				= $dpp;;
				$i->nama 				= $ppn;
				$i->alamat_lengkap 		= 0;
				$i->jumlah_dpp 			= 0;
				$res[] = $i;
			}
		}
		
		return $res;
	}
	
	public function Index()
	{
		$this->data = $this->set;
		$this->view = "default/report";
	}

	public function View()
	{
		$this->set->dataLoad = $this->genData();
		$this->data = $this->report->getDef($this->set);
		$this->layout = "report";
	}
}