<?php

CLASS ljakh 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     	= 'Laporan Harga Akhir';
		$this->set->form      	= $this->setForm();
		$this->set->filter    	= $this->report->setFilter($_GET, $this->setForm());
		$this->set->notNull   	= array("date1", "date2", "paper");
		$this->set->button    	= array("View", "Cetak", "Pdf", "Excel", "Csv");
		$this->set->groupDis  	= array("Customer" => "kd_sub", "nm_sub");
		$this->set->groupBy   	= "kd_sub";
		$this->set->rowPage   	= 60;
		//$this->set->grandtotal  = true;
		$this->baseTitle      	= $this->set->modul;
		
		//$hit = array("dpp", "ppn_nil", "ppn_nilai", "diskt", "stot", "qtyt", "gtot");
		//foreach($this->jenBayar() as $k => $r){
		//	$hit[] = $k;
		//}
		//$this->set->hitung   = $hit;
		
		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" => "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)));

		$form[] = array('row' => $row, 'col' => 430, "label" => array("name" => "S/d", "width" => 30), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub2', "size" => 9, 'after' => 'load("mastercust", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_sub2=>nm_sub')));
		$form[] = array('row' => $row, 'col' => 550, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub2', "size" => 30, 'readonly' => true)));

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

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

		//$row = $row + 25;
		//$form[] = array('row' =>  $row,  'col' => 25,   "label" => array("name" => "Mode",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'mode', "size" => 10, 'value' => 'DETAIL', 'after' => 'select()', "select" => 'mode_report')));

		$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_detail()
	{
	    $res[] = array('id' => 'dark',		'nama' => '&nbsp;',   		'type' => 'text', 	'width' => 40,  'size' => 10);
		$res[] = array('id' => 'no',		'nama' => 'No.', 'type' => 'no', 'width' => 20,  'size' => 20);
		$res[] = array('id' => 'kd_inv',	'nama' => 'Inventory',  		'type' => 'text',   'width' => 150,  'size' => 20);
		$res[] = array('id' => 'nm_inv',	'nama' => 'Nama Barang',  		'type' => 'text',   'width' => 300,  'size' => 50);
		$res[] = array('id' => 'harga',		'nama' => 'Harga Akhir',  	'type' => 'text',   'width' => 80,  'size' => 20, 'type' => 'numeric');
		$res[] = array('id' => 'tgl',		'nama' => 'Tanggal', 'type' => 'text', 'width' => 100,  'size' => 20);
		$res[] = array('id' => 'nonjl',		'nama' => 'No Jurnal', 'type' => 'text', 'width' => 120,  'size' => 20);
		
		//$res[] = array('id' => 'nm_sub',	'nama' => 'Nama Customer',  		'type' => 'text',   'width' => 200,  'size' => 20);
		//$res[] = array('id' => 'kdac',	'nama' => 'Account',  		'type' => 'text',   'width' => 50,  'size' => 10);
		//$res[] = array('id' => 'nmac',	'nama' => 'Nama Account',  		'type' => 'text',   'width' => 200,  'size' => 30);
		//$res[] = array('id' => 'qty',		'nama' => 'Jumlah',   		'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2);
		//$res[] = array('id' => 'satuan',	'nama' => 'Unit',  		'type' => 'text',   'width' => 40,  'size' => 5);
		//$res[] = array('id' => 'diskon',	'nama' => 'Discount',  	'type' => 'text',   'width' => 120, 'align' => 'right',  'size' => 20);
		//$res[] = array('id' => 'total',		'nama' => 'Sub Total',  	'type' => 'text',   'width' => 90,  'size' => 20, 'type' => 'numeric');
		//$res[] = array('id' => 'ppn_nilai',	'nama' => 'PPN',   'type' => 'numeric', 'width' => 60,  'size' => 20);
		//$res[] = array('id' => 'gtot',		'nama' => 'Total',  	'type' => 'text',   'width' => 90,  'size' => 20, 'type' => 'numeric');
	
		return $res;
	}
	
	public function jenBayar()
	{
		$rep = new SELECT_APP_PENJUALAN;
		$res = array();
		foreach($rep->JEN_BAYAR() as $r){
			$id = str_ireplace("/", "", $r);
			$id = str_ireplace(" ", "", $id);
			$id = strtolower($id);
			$id = 'bayar_'.$id;
			$res[$id] = $r;
		}
		return $res;
	}

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

		$sql = $this->em->addSelect("
						a.*, b.*, c.kdac, c.nmac,
						to_char(a.tgl, 'dd/mm/yyyy') as tgl
						");
		$sql = $this->em->getFrom("njl", 'a');
		$sql = $this->em->addLeftJoin("njld", 'b', array("a.nonjl = b.nonjl"));
		$sql = $this->em->addLeftJoin("acd", 'c', array("a.nonjl = c.nojnl", "c.kdac = '0.0.06.10'"));
		$sql = $this->em->getWhere("a.control = '1'");
		if(!empty($filter->date1->value) && ((empty($filter->kd_sub->value) && empty($filter->kd_sub2->value)) || $filter->kd_sub->value != $filter->kd_sub2->value)){
			$sql = $this->em->andWhere("a.tgl >= '".date('Y-m-d 00:00:00', strtotime($filter->date1->value))."'");
		}
		if(!empty($filter->date2->value) && ((empty($filter->kd_sub->value) && empty($filter->kd_sub2->value)) || $filter->kd_sub->value != $filter->kd_sub2->value)){
			$sql = $this->em->andWhere("a.tgl <= '".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->kd_sub->value)){
			$sql = $this->em->andWhere("a.kd_sub >= '".$filter->kd_sub->value."'");
		}
		if(!empty($filter->kd_sub2->value)){
			$sql = $this->em->andWhere("a.kd_sub <= '".$filter->kd_sub2->value."'");
		}
		if(!empty($filter->kd_inv->value)){
			$sql = $this->em->andWhere("b.kd_inv = '".$filter->kd_inv->value."'");
		}
		if(!empty($_GET['nonjl'])){
			$sql = $this->em->andWhere("a.nonjl = '".$_GET['nonjl']."'");
		}
		if($filter->ppn->value == 'T'){
			$sql = $this->em->andWhere("a.nonjl like 'PPN%'");
		}
		else if($filter->ppn->value == 'F'){
			$sql = $this->em->andWhere("a.nonjl like 'NON%'");
		}
		$sql = $this->em->getMultiSort("a.kd_sub, b.kd_inv, a.tgl desc");
		$eks = $this->em->queryEks($sql);
		
		$data = array();
		while($r = $this->em->fetchObject($eks)){
			$id = $r->kd_sub.$r->kd_inv;
			if(empty($data[$id])){
			    $data[$id]->kd_sub = $r->kd_sub;
			    $data[$id]->nm_sub = $r->nm_sub;
			    $data[$id]->kd_inv = $r->kd_inv;
			    $data[$id]->nm_inv = $r->nm_inv;
			    $data[$id]->harga  = $r->harga;
			    $data[$id]->tgl    = $r->tgl;
			    $data[$id]->nonjl  = $r->nonjl;
			}
			
		}
	    
		$this->set->dataLoad = $data;
		
		return false;
	}

	protected function report_rekap()
	{
	    $res[] = array('id' => 'dark',		'nama' => '&nbsp;',   		'type' => 'text', 	'width' => 40,  'size' => 10);
		$res[] = array('id' => 'no',		'nama' => 'No.', 'type' => 'no', 'width' => 20,  'size' => 20);
		//$res[] = array('id' => 'tgl',		'nama' => 'Tanggal',   	'type' => 'text', 	'width' => 100,  'size' => 10, 'noduplicate' => true);
		$res[] = array('id' => 'nonjl',		'nama' => 'Nomor',  	'type' => 'text',   'width' => 130,  'size' => 30, 'link' => array("attr" => array("nonjl"), 'url' => $this->baseUrl.'ljnota/View/&date1=01/10/2010&date2='.date("d/m/Y").'&mode=DETAIL&paper=P&convert=cetak'));
		$res[] = array('id' => 'nm_sub',	'nama' => 'Nama Customer',  		'type' => 'text',   'width' => 200,  'size' => 20);
		//$res[] = array('id' => 'kdac',	'nama' => 'Account',  		'type' => 'text',   'width' => 50,  'size' => 10);
		//$res[] = array('id' => 'nmac',	'nama' => 'Nama Account',  		'type' => 'text',   'width' => 200,  'size' => 30);
		//$res[] = array('id' => 'noref',		'nama' => 'No.Ref#',  	'type' => 'text',   'width' => 130,  'size' => 30);
		//$res[] = array('id' => 'kd_cabang',	'nama' => 'PC',  		'type' => 'text',   'width' => 50,  'size' => 3);
		//$res[] = array('id' => 'kd_sub',	'nama' => 'Kode',  		'type' => 'text',   'width' => 100,  'size' => 20, 'induk' => 'Customer');
		//$res[] = array('id' => 'nm_sub',	'nama' => 'Nama',  		'type' => 'text',   'width' => 150,  'size' => 20, 'induk' => 'Customer');
        $res[] = array('id' => 'qtyt',		'nama' => 'Jumlah',  		'type' => 'text',   'width' => 90,  'size' => 20, 'type' => 'numeric');
		$res[] = array('id' => 'stot',		'nama' => 'Sub Total',  		'type' => 'text',   'width' => 90,  'size' => 20, 'type' => 'numeric');
		$res[] = array('id' => 'diskt',	'nama' => 'Discount',  	'type' => 'text',   'width' => 120, 'align' => 'right',  'size' => 20, 'type' => 'numeric');
		//$res[] = array('id' => 'ppn',		'nama' => 'Jen',   		'type' => 'text', 'width' => 30,  'size' => 20, 'induk' => 'PPN', 'align' => 'center', 'induk' => 'PPN');
		$res[] = array('id' => 'ppn_nilai',	'nama' => 'Nilai',   	'type' => 'numeric', 'width' => 80,  'size' => 20);
		$res[] = array('id' => 'gtot',		'nama' => 'Total',  	'type' => 'text',   'width' => 90,  'size' => 20, 'type' => 'numeric');
		$res[] = array('id' => 'ket',		'nama' => 'Keterangan', 'type' => 'text',   'width' => 200,  'size' => 60);
        $res[] = array('id' => 'dark2',		'nama' => '&nbsp;',   		'type' => 'text', 	'width' => 40,  'size' => 10);
        
		return $res;
	}


	public function query_rekap()
	{
		$this->set->groupDis  	= array();
		$this->set->groupBy   	= "";
		$this->set->rowPage   	= 20;
		$this->set->grandtotal  = true;
		
		$filter = $this->set->filter;
		
		$sql = $this->em->addSelect("a.nonjl
								, sum(COALESCE(a.qty, 0)) as qtyt
								");
		$sql = $this->em->getFrom("njld", 'a');
		$sql = $this->em->getGroupBy("a.nonjl");
		$sel = $sql;

		$sql = $this->em->addSelect("
						to_char(a.tgl, 'dd/mm/yyyy') as tgl
						, a.*, b.qtyt
						");
		$sql = $this->em->getFrom("njl", 'a');
		$sql = $this->em->addLeftJoin("($sel)", 'b', array("a.nonjl = b.nonjl"));
		$sql = $this->em->getWhere("a.control = '1'");
		if(!empty($filter->date1->value) && ((empty($filter->kd_sub->value) && empty($filter->kd_sub2->value)) || $filter->kd_sub->value != $filter->kd_sub2->value)){
			$sql = $this->em->andWhere("a.tgl >= '".date('Y-m-d 00:00:00', strtotime($filter->date1->value))."'");
		}
		if(!empty($filter->date2->value) && ((empty($filter->kd_sub->value) && empty($filter->kd_sub2->value)) || $filter->kd_sub->value != $filter->kd_sub2->value)){
			$sql = $this->em->andWhere("a.tgl <= '".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->kd_sub->value)){
			$sql = $this->em->andWhere("a.kd_sub >= '".$filter->kd_sub->value."'");
		}
		if(!empty($filter->kd_sub2->value)){
			$sql = $this->em->andWhere("a.kd_sub <= '".$filter->kd_sub2->value."'");
		}
		if($filter->ppn->value == 'T'){
			$sql = $this->em->andWhere("a.nonjl like 'PPN%'");
		}
		else if($filter->ppn->value == 'F'){
			$sql = $this->em->andWhere("a.nonjl like 'NON%'");
		}

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

		return $sql;
	}

	public function Index()
	{
		$this->data = $this->set;
		$this->view = "default/report";
	}

	public function View()
	{
		$mode 	= strtolower($_GET['mode']);
		$mode = "detail";
		$query 	= "query_".$mode;
		$report = "report_".$mode;
		
		$this->set->query 	= $this->$query();
		$this->set->display	= $this->$report();
		$this->set->header = "<style>*{font-weight: bold;}</style>";

		$this->data = $this->report->getDef($this->set);
		$this->layout = "report";
	}
}