<?php

CLASS lext 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()
  {
	if(!empty($_GET['_modul'])){
		$idEx = $_GET['_modul'];
	}else{
		$idEx = explode("/", $_GET['ref']);
		$idEx = explode("___", $idEx[0]);
		$idEx = $idEx[1];
	}
	$thema = $this->em->getFind("temah", "*", array("kode = '$idEx'"));
  
    $this->report       = new Report;
    $this->set->thema   = $thema;;
    $this->set->em      = $this->em;
    $this->set->kode    = 'ACC-001';
    $this->set->date    = date("d/m/Y H:i");
    $this->set->modul     = 'Laporan Detail Jurnal';
    $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");
    $this->set->groupDis  = array();
    $this->set->groupBy   = "nojnl";
    $this->set->hitung    = array("debet", "credit", "saldo");
    $this->set->rowPage   = 60;
    $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($this->set->thema->model == 'STANDART'){

		$form[] = array('row' =>  0,  'col' => 0,   "label" => array("name" => "Group",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'grup', "size" => 5, 'after' => 'load("gcab", "false", "gcab")')));

		$form[] = array('row' =>  25,  'col' => 0,   "label" => array("name" => "Sub Group",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'sub_grup', "size" => 5, 'after' => 'load("scab", "false", "scab")')));

		$form[] = array('row' =>  50,  'col' => 0,   "label" => array("name" => "Profit Center",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'cabang', "size" => 5, 'after' => 'load("cabang", "false", "cabang")')));

		$form[] = array('row' =>  75,  'col' => 0,   "label" => array("name" => "Periode",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'awal', "size" => 10, 'class' => 'tanggal', 'value' => date("01/m/Y"))));
		$form[] = array('row' =>  75,  'col' => 220, "label" => array("name" => "s/d",     "width" => 30),   "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'akhir', "size" => 10, 'class' => 'tanggal', 'value' => date("d/m/Y"))));
	}

	if($this->set->thema->model == 'SD'){

		$form[] = array('row' =>  0,  'col' => 0,   "label" => array("name" => "Group",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'grup', "size" => 5, 'after' => 'load("gcab", "false", "gcab")')));

		$form[] = array('row' =>  25,  'col' => 0,   "label" => array("name" => "Sub Group",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'sub_grup', "size" => 5, 'after' => 'load("scab", "false", "scab")')));

		$form[] = array('row' =>  50,  'col' => 0,   "label" => array("name" => "Profit Center",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'cabang', "size" => 5, 'after' => 'load("cabang", "false", "cabang")')));

		$form[] = array('row' =>  75,  'col' => 0,   "label" => array("name" => "Periode",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'tgl', "size" => 10, 'class' => 'tanggal', 'value' => date("d/m/Y"))));
	}
	
    $form[] = array('row' =>  100,  'col' => 0, "label" => array("name" => "Modul",     "width" => 100),   "input" => array("type" => 'text', 'attr' => array("name" => '_modul', "size" => 20, "value" => $this->set->thema->kode, 'readonly' => true)));
  
    $form[] = array('row' =>  125,  'col' => 0,   "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;
  }
  
  
  public function Index()
  {
    $this->data = $this->set;
    $this->view = "default/report";
  }
  
	public function setTgl($filter)
	{
		$awal 	= (int)date("m", strtotime($filter->awal->value));
		$akhir 	= (int)date("m", strtotime($filter->akhir->value));
		$tgl = array();
		for($i = $awal; $i <= $akhir; $i++){
			$bulan = $i;
			if($i < 10){
				$bulan = '0'.$i;
			}
			$tgl[$i] = date("Y", strtotime($filter->awal->value)).'-'.$bulan.'-01';
			$tgl[$i] = date("Y-m-d", strtotime($tgl[$i]));
		}

		return $tgl;
	}
	
	protected function detAcc($num, $grup, $res)
	{
		foreach($grup[$num] as $det){
			if(count($grup[$det->kdac]) > 0){
				$res = $this->detAcc($det->kdac, $grup, $res);
			}else{
				$res[] = "a.kdac = '".$det->kdac."'";
			}
		}
		return $res;
	}
	
	public function cariAcc($det, $grupAcc, $grupAcf, $tema)
	{
		$grup = $grupAcc;
		if($tema->ket == 'CF'){
			$grup = $grupAcf;
		}
		if($det->fix == 'ACC'){
			$grup = $grupAcc;
		}
		if($det->fix == 'CF'){
			$grup = $grupAcf;
		}
		
		$res = array();
		$ex = explode(",", $det->acc);
		foreach($ex as $row){
			$row = trim($row);
			if(!empty($row)){
				$ex2 = explode("-", $row);
				if(count($ex2) == '1'){
					if(count($grup[$row]) > 0){
						$res = $this->detAcc($row, $grup, $res);
					}else{
						$res[] = "a.kdac = '".$row."'";
					}
				}else{
					$res[] = "a.kdac >= '".$ex2[0]."' and a.kdac <= '".$ex2[1]."'";
				}
			}
		}
		
		// if(count($grup[$det->a]) > 0){
			// $res = $this->detAcc($det->no, $grup, $res);
		// }
		
		$acc = "";
		foreach($res as $row){
			if(!empty($acc)){
				$acc.= " or ";
			}
			$acc.= "($row)";
		}
		return $acc;
	}
	
	public function setFilKoma($id, $data)
	{
		$exp = explode(",", $data);
		$res = "";
		foreach($exp as $row){
			$row = trim($row);
			if(!empty($res)){
				$res.= " or ";
			}
			$res.= "($id = '$row')";
		}
		return $res;
	}
	
	public function getHitung($tema, $row, $bln, $acc, $filter, $jum)
	{
		if($row->periode == 'B'){
			$awal 	= $bln;
			$akhir 	= $this->getPlugin("Date")->getPenjumlahanTanggal($bln, $jum, 0, 0);
		}else{
			$awal 	= '2000-01-01';
			$akhir 	= $this->getPlugin("Date")->getPenjumlahanTanggal($bln, $jum, 0, 0);
		}
		
		$row->jenis = (int)$row->jenis;
		if($row->jenis <> 0){
			$minplus = str_ireplace("-", "", $row->jenis);
			if($row->jenis > 0){
				$awal 	= $this->getPlugin("Date")->getPenjumlahanTanggal($awal, $minplus, 0, 0);
				$akhir 	= $this->getPlugin("Date")->getPenjumlahanTanggal($akhir, $minplus, 0, 0);
			}else{
				$awal 	= $this->getPlugin("Date")->getPenguranganTanggal($awal, $minplus, 0, 0);
				$akhir 	= $this->getPlugin("Date")->getPenguranganTanggal($akhir, $minplus, 0, 0);
			}
		}
		
		if($row->action == 'T'){
			$sel = "sum(a.credit-a.debet) as saldo";
		}else{
			$sel = "sum(a.debet-a.credit) as saldo";
		}
		
		$sql = $this->em->addSelect($sel);
		$sql = $this->em->getFrom('acd', 'a');
		$sql = $this->em->addLeftJoin('cabang', 'b', array("a.kd_cabang = b.kd_cabang"));
		$sql = $this->em->addLeftJoin('sub_cab', 'c', array("b.kd_sub_cab = c.kd_sub_cab"));
		$sql = $this->em->getWhere("1=1");
		
		if($tema->ket != 'CF' and $row->fix != 'ACC'){
			$sql = $this->em->andWhere("case when kdac >= '400000' then alias != 'JPN' else 1=1 end");
		}
		$sql = $this->em->andWhere("a.kd_cabang != 'AP1'");
		$sql = $this->em->andWhere("a.kd_cabang != 'AP2'");
		$sql = $this->em->andWhere("a.tgl <= '".date("Y-m-d")."'");
		if(!empty($acc)){
			if($tema->ket == 'CF'){
				$acc = str_ireplace("kdac", "kdacf", $acc);
			}
			if($row->fix == 'ACC'){
				$acc = str_ireplace("kdacf", "kdac", $acc);
			}
			if($row->fix == 'CF'){
				$acc = str_ireplace("kdac", "kdacf", $acc);
			}
		
			$sql = $this->em->andWhere("($acc)");
		}else{
			$sql = $this->em->andWhere("a.kdac = ''");
		}
		if(!empty($awal)){
			$sql = $this->em->andWhere("a.tgl >= '".date('Y-m-d', strtotime($awal))."'");
		}
		if(!empty($akhir)){
			$sql = $this->em->andWhere("a.tgl < '".date('Y-m-d', strtotime($akhir))."'");
		}
		if(!empty($row->kd_sub)){
			$filSub = $this->setFilKoma("a.kd_sub", $row->kd_sub);
			$sql = $this->em->andWhere("($filSub)");
		}
		if(!empty($row->cab) and $row->cab != 'SG'){
			$filCab1 = $this->setFilKoma("a.kd_cabang", $row->cab);
			$filCab2 = $this->setFilKoma("b.kd_sub_cab", $row->cab);
			$filCab3 = $this->setFilKoma("c.kd_grp_cab", $row->cab);
			$sql = $this->em->andWhere("( ($filCab1) or ($filCab2) or ($filCab3) )");
		}

		if(!empty($filter->cabang->value)){
			$sql = $this->em->andWhere("a.kd_cabang = '".$filter->cabang->value."'");
		}
		if(!empty($filter->sub_grup->value)){
			$sql = $this->em->andWhere("b.kd_sub_cab = '".$filter->sub_grup->value."'");
		}
		if(!empty($filter->grup->value)){
			$sql = $this->em->andWhere("c.kd_grp_cab = '".$filter->grup->value."'");
		}
		
		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);
		$saldo = $res[0]->saldo;
		
		$aw = date('Y-m-d', strtotime($awal));

		if($_GET['awal'] == '01/08/2016'){
			$kasbank = array();
			$kasbank['SM'] = -15169098239;
			$kasbank['HS'] = 758510066;
		
			$exRumus = explode(",", $row->acc);
			
			foreach($exRumus as $r){
				$r = strtoupper(trim($r));
				if($r == 'KASBANKCF'){
					$tambah = $kasbank[$filter->grup->value];
					$saldo = $saldo + $tambah;

					$tambah = $kasbank[$filter->sub_grup->value];
					$saldo = $saldo + $tambah;
				}
			}
		}
			
		if($row->no == 'A.295'){
			// var_dump($row);
			// echo $sql.'<hr>';
		}
		
		return $saldo;
	}
	
	public function nilaiGroup($nilai, $bln, $no, $val, $child, $grup, $nomor = 1)
	{
		$val = $val + $nilai[$bln][$no];
	
		if($no == 'A.010'){
			// var_dump($val);
			// var_dump($child);
		}
		
		if(count($child) > 0){
			foreach($child as $k => $row){
				if(!empty($grup[$row->no])){
					$nomor = $nomor + 1;
					$val = $this->nilaiGroup($nilai, $bln, $row->no, $val, $grup[$row->no], $grup, $nomor);
				}else{
					$val = $val + $nilai[$bln][$row->no];
				}
			}
		}
		
		return $val;
	}
	
	protected function STANDART($tema)
	{
		$filter = $this->set->filter;
		$tgl = $this->setTgl($filter);
		$det = $this->em->getFindBy("temad", "*", array("kode = '".$_GET['_modul']."'"), "no asc");
		
		$grup = array();
		foreach($det as $row){
			$grup[$row->induk][] = $row;
		}
		
		$dbacc = $this->em->getFindBy("acc", "kdac, induk", array(), "kdac");
		$grupAcc = array();
		foreach($dbacc as $row){
			$grupAcc[$row->induk][$row->kdac] = $row;
		}
		$dbacc = $this->em->getFindBy("accf", "kdac, induk", array(), "kdac");
		$grupAcf = array();
		foreach($dbacc as $row){
			$grupAcf[$row->induk][$row->kdac] = $row;
		}
		
		if(!empty($filter->cabang->value)){
			$cab = $this->em->getFind("cabang", "nm_cabang", array("kd_cabang = '".$filter->cabang->value."'"));
		}elseif(!empty($filter->sub_grup->value)){
			$cab = $this->em->getFind("sub_cab", "nm_sub_cab as nm_cabang", array("kd_sub_cab = '".$filter->sub_grup->value."'"));
		}elseif(!empty($filter->grup->value)){
			$cab = $this->em->getFind("grp_cab", "nm_grp_cab as nm_cabang", array("kd_grp_cab = '".$filter->grup->value."'"));
		}
		if(empty($cab->nm_cabang)){
			$cab->nm_cabang = "Konsolidate";
		}
		
		$nilai = array();
		foreach($det as $row){
			$acc = $this->cariAcc($row, $grupAcc, $grupAcf, $tema);
			foreach($tgl as $bln){
				$val = $this->getHitung($tema, $row, $bln, $acc, $filter, 1);
				$nilai[$bln][$row->no] = $val;
			}
		}
		
		$data = '<table class="lapTable" cellpadding="0" cellspacing="0" border="1">';
		$data.= '<thead>';
		$data.= '<tr>';
		$data.= '<th class="_title" style="width: 700px; text-align: left" align="left">';
		$data.= '<div style="font-size: 15px; margin: 0px;">'.$this->login['pt'].'</div>';
		$data.= '<div style="font-size: 12px; margin: 0px;">'.$cab->nm_cabang.'</div>';
		$data.= '<div style="font-size: 12px; margin: 0px;">'.$this->set->thema->nama.'</div>';
		$data.= '<div style="font-size: 12px; margin: 0px;">Periode : '.date("d/m/Y", strtotime($filter->awal->value)).' s/d '.date("d/m/Y", strtotime($filter->akhir->value)).'</div>';
		$data.= '<div style="font-size: 12px; margin: 0px;">Tgl. Cetak : '.date("d/m/Y H:i:s").' Oleh '.$this->login['nm_user'].'</div>';
		$data.= '</th>';
		$data.= '</tr>';
		$data.= '<tr>';
		$data.= '<th class="_thead _tLeaft">Keterangan</th>';
		$width = 500;
		foreach($tgl as $row){
			$data.= '<th width="100px" class="_thead">'.date("M", strtotime($row)).'</th>';
			$width = $width + 100;
		}
		$this->data->widthPaper = $width;
		$data.= '<tr>';
		$data.= '</thead>';
		$data.= '<tbody>';
		
		foreach($det as $row){
			$jarak = $row->jarak * 20;
			$style = "margin-left: ".$jarak.'px';
			$bold = '';
			if(strtoupper($row->bold) == strtoupper('B')){
				$bold  = "; font-weight: bold";
				$style.= $bold;
			}elseif(strtoupper($row->bold) == strtoupper('BU')){
				$bold  = "; font-weight: bold; text-decoration: underline";
				$style.= $bold;
			}elseif(strtoupper($row->bold) == strtoupper('U')){
				$bold  = "; text-decoration: underline";
				$style.= $bold;
			}
			
			if(empty($row->judul)){
				$row->judul = '&nbsp;';
			}
			
			if($_GET['convert'] == 'excel'){
				$spasi = "";
				for($iJ = 1; $iJ <= $jarak; $iJ++){
					$spasi.= "&nbsp;";
				}
				$judul = $spasi.$row->id.' '.$row->judul;
			}else{
				$judul = '<div style = "width: 20px; float: left; '.$style.'">'.$row->id.'</div>';
				$judul.= '<div style = "'.$style.'">'.$row->judul.'</div>';
			}
			
			$clsGrs = '';
			if(strtoupper($row->garis) == strtoupper('T')){
				$clsGrs = "_borderBottom";
			}
			$data.= '<tr>';
			$data.= '<td class="_tLeaft '.$clsGrs.'">'.$judul.'</td>';
			
			foreach($tgl as $bln){
				$nil = $this->nilaiGroup($nilai, $bln, $row->no, 0, $grup[$row->no], $grup);
				
				$val = '';
				if(!empty($nil)){
					if($nil <> 0){
						$val = '<span style = "'.$bold.'">'.$this->getPlugin('Kode')->accNumeric($nil, 2).'</span>';
					}
				}
				
				if(count($grup[$row->no]) > 0 and empty($val)){
					$val = 0;
				}
				
				$data.= '<td align="right" class="'.$clsGrs.'">'.$val.'</td>';
			}
			$data.= '</tr>';
		}
		$data.= '</tbody>';
		$data.= "<table>";
		return $data;
	}
	
	protected function SD($tema)
	{
		$filter = $this->set->filter;
		$tgl = $this->setTgl($filter);
		$det = $this->em->getFindBy("temad", "*", array("kode = '".$_GET['_modul']."'"), "no asc");
		
		$grup = array();
		foreach($det as $row){
			$grup[$row->induk][] = $row;
		}
		
		if(!empty($filter->cabang->value)){
			$cab = $this->em->getFind("cabang", "nm_cabang", array("kd_cabang = '".$filter->cabang->value."'"));
		}elseif(!empty($filter->sub_grup->value)){
			$cab = $this->em->getFind("sub_cab", "nm_sub_cab as nm_cabang", array("kd_sub_cab = '".$filter->sub_grup->value."'"));
		}elseif(!empty($filter->grup->value)){
			$cab = $this->em->getFind("grp_cab", "nm_grp_cab as nm_cabang", array("kd_grp_cab = '".$filter->grup->value."'"));
		}
		if(empty($cab->nm_cabang)){
			$cab->nm_cabang = "Konsolidate";
		}
		
		$nilai = array();
		foreach($det as $row){
			$acc = $this->cariAcc($row, $grup);
			foreach($tgl as $bln){
				$val = $this->getHitung($tema, $row, $bln, $acc, $filter, 1);
				$nilai[$bln][$row->no] = $val;
			}
		}
		
		$maxJum = count($tgl);
		
		$data = '<table class="lapTable" cellpadding="0" cellspacing="0" style="min-width: 800px">';
		$data.= '<thead>';
		$data.= '<tr>';
		$data.= '<th colspan="'.count($maxJum).'" class="_title">';
		$data.= '<div style="font-size: 15px; margin: 0px;">'.$this->login['pt'].'</div>';
		$data.= '<div style="font-size: 12px; margin: 0px;">'.$cab->nm_cabang.'</div>';
		$data.= '<div style="font-size: 12px; margin: 0px;">'.$this->set->thema->nama.'</div>';
		$data.= '<div style="font-size: 12px; margin: 0px;">Periode : '.date("d/m/Y", strtotime($filter->awal->value)).' s/d '.date("d/m/Y", strtotime($filter->akhir->value)).'</div>';
		$data.= '<div style="font-size: 12px; margin: 0px;">Tgl. Cetak : '.date("d/m/Y H:i:s").' Oleh '.$this->login['nm_user'].'</div>';
		$data.= '</th>';
		$data.= '</tr>';
		$data.= '<tr>';
		$data.= '<th class="_thead _tLeaft">Keterangan</th>';
		$width = 500;
		foreach($tgl as $row){
			$data.= '<th width="100px" class="_thead">'.date("M", strtotime($row)).'</th>';
			$width = $width + 100;
		}
		$this->data->widthPaper = $width;
		$data.= '<tr>';
		$data.= '</thead>';
		$data.= '<tbody>';
		
		foreach($det as $row){
			$jarak = $row->jarak * 20;
			$style = "margin-left: ".$jarak.'px';
			$bold = '';
			if(strtoupper($row->bold) == strtoupper('B')){
				$bold  = "; font-weight: bold";
				$style.= $bold;
			}elseif(strtoupper($row->bold) == strtoupper('BU')){
				$bold  = "; font-weight: bold; text-decoration: underline";
				$style.= $bold;
			}elseif(strtoupper($row->bold) == strtoupper('U')){
				$bold  = "; text-decoration: underline";
				$style.= $bold;
			}
			
			if(empty($row->judul)){
				$row->judul = '&nbsp;';
			}
			
			$spasi = "";
			for($iJ = 1; $iJ <= $jarak; $iJ++){
				$spasi.= "&nbsp;";
			}
			
			$judul = $row->id.' '.$row->judul;
			// $judul = '<div style = "width: 20px; float: left; '.$style.'">'.$row->id.'</div>';
			// $judul.= '<div style = "'.$style.'">'.$row->judul.'</div>';
			
			$clsGrs = '';
			if(strtoupper($row->garis) == strtoupper('T')){
				$clsGrs = "_borderBottom";
			}
			$data.= '<tr>';
			$data.= '<td class="_tLeaft '.$clsGrs.'">'.$judul.'</td>';
			
			foreach($tgl as $bln){
				$nil = $this->nilaiGroup($nilai, $bln, $row->no, 0, $grup[$row->no], $grup);
				
				$val = '';
				if(!empty($nil)){
					if($nil <> 0){
						$val = '<span style = "'.$bold.'">'.$this->getPlugin('Kode')->accNumeric($nil, 2).'</span>';
					}
				}
				$data.= '<td align="right" class="'.$clsGrs.'">'.$val.'</td>';
			}
			$data.= '<tr>';
		}
		$data.= '</tbody>';
		$data.= "<table>";
		return $data;
	}
	
	public function createData()
	{
		$tema = $this->em->getFind("temah", "*", array("kode = '".$_GET['_modul']."'"));
		$model = $tema->model;
		$data = $this->$model($tema);
		$this->data->data = $data;
	}
	
  public function View()
  {
	$data = $this->createData();
    $this->view = "exibit";
    $this->layout = "print";
	
	if($_GET['convert'] == 'excel'){
		$this->data->set->modul = 'Laporan_'.str_ireplace(" ", "_", $this->set->thema->nama);
		$this->layout = "excel";
	}
	
  }
}