<?php

CLASS lkh extends RasioProject
{
	protected $set;
	protected $enableCk = false;
	protected $report;
	protected $baseTitle;
	protected $kdac1 = '0.0.01.10';
	protected $kdac2 = '111.02';

	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 Kas Harian';
		$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();
		
		$form[] = array('row' =>  0,  'col' => 0,   "label" => array("name" => "Date",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'awal', "size" => 10, 'class' => 'tanggal', 'value' => date("d/m/Y"))));
		$form[] = array('row' =>  0,  '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"))));

		$form[] = array('row' =>  25,  'col' => 0,   "label" => array("name" => "Account",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'kdac1', "size" => 10, 'value' => $this->kdac1, 'after' => 'load("macd", "false", "false")', 'load' => 'lkh')));

		return $form;
	}

	
	public function tab()
	{
		$res[] = array('id' => 'tgl', 'nama' => 'Tgl. jurnal', 'type' => 'date', 'size' => 80);
		$res[] = array('id' => 'nojnl', 'nama' => 'No. Jurnal', 'type' => 'text', 'size' => 140, 'link' => array("url" => $this->baseUrl.'Lap-Acc-Det-Jurnal/Cetak/index/&group_1=nojnl___nojnl', 'attr' => array("nojnl")));
		$res[] = array('id' => 'kdac', 'nama' => 'Acc', 'type' => 'text', 'size' => 40);
		$res[] = array('id' => 'kd_sub', 'nama' => 'Sub', 'type' => 'text', 'size' => 40);
		$res[] = array('id' => 'ket', 'nama' => 'Keterangan', 'type' => 'text');
		$res[] = array('id' => 'debet', 'nama' => 'Debet', 'type' => 'numeric', 'dec' => 2, 'size' => 60);
		$res[] = array('id' => 'credit', 'nama' => 'Kredit', 'type' => 'numeric', 'dec' => 2, 'size' => 60);
		$res[] = array('id' => 'saldo', 'nama' => 'Saldo', 'type' => 'numeric', 'dec' => 2, 'size' => 60);
		
		return $res;
	}

	public function genData()
	{
		$rep = new Report;
		
		$filter = $this->set->filter;
		$qlkh = new q_lkh($this->set, 'bank');
		
		$filB = array();
		$filB[] = "detil = 'T'";
		$filB[] = "(kdac < '0.0.03')";
		if(!empty($filter->kdac1->value)){
			$filB[] = "kdac = '".$filter->kdac1->value."'";
		}
		$acc  = $this->em->getFindBy("acc", "kdac, nmac", $filB, "kdac");
		
		$data = '<p class="_judul">LAPORAN KAS/BANK HARIAN</p>';
		$data.= '<p class="_periode">Periode : '.$_GET['awal'].' s/d '.$_GET['akhir'].', Profit Center : '.$_GET['cabang'].'</p>';
		$data.= '<table class="lapTable" cellpadding="0" cellspacing="0" width="100%" style="">';
		
		foreach($acc as $r){
			$filter->kdac->value = $r->kdac;
			$awal = $qlkh->getSaldoAwal($filter);
			
			$data.= '<tr><td colspan="100" class="_groupTable">'.$r->kdac.' - '.$r->nmac.'</td></tr>';
			$data.= '<tr>';
			$data.= '<th width="30px">No.</th>';
			foreach($this->tab() as $tk => $tr){
				if(empty($tr['nama'])){
					$tr['nama'] = '&nbsp;';
				}
				$data.= '<th width="'.$tr['size'].'px">'.$tr['nama'].'</th>';
			}
			$data.= "</tr>";
				
			$tgl = date('d/m/Y', strtotime($filter->awal->value));
			$saldo = $awal[$r->kdac]->total;
			
			$no = 1;
			$data.= '<tr>';
			$data.= '<td align="center">'.$no++.'</td>';
			$data.= '<td align="center">'.$tgl.'</td>';
			$data.= '<td>Saldo Awal</td>';
			$data.= '<td >&nbsp;</td>';
			$data.= '<td >&nbsp;</td>';
			$data.= '<td >&nbsp;</td>';
			$data.= '<td >&nbsp;</td>';
			$data.= '<td >&nbsp;</td>';
			$data.= '<td align="right">'.number_format($saldo, 2).'</td>';
			$data.= '</tr>';
			
			$asli  = $qlkh->getAsli($filter, $r->kdac);
			$lawan = $qlkh->getDataLawan($asli, $r->kdac, $filter);
			
			$debet  = 0;
			$credit = 0;
			foreach($lawan as $i){
				if($tgl != $i->tgl){
					$tgl = $i->tgl;
				}else{
					$i->tgl = '';
				}
			
				$debet  = $debet + $i->debet;
				$credit = $credit + $i->credit;
			
				$saldo = $saldo + ($i->debet - $i->credit);
				$data.= '<tr>';
				$data.= '<td align="center">'.$no++.'</td>';
				$data.= '<td align="center">'.$i->tgl.'</td>';
				$data.= '<td><a href="'.$this->baseUrl.'ldj/View/&nojnl='.$i->nojnl.'&paper=P&convert=cetak" target="_blank" style="text-decoration: none">'.$i->nojnl.'</td>';
				$data.= '<td >'.$i->kdac.'</td>';
				$data.= '<td >'.$i->kd_sub.'</td>';
				$data.= '<td >'.$i->ket.'</td>';
				$data.= '<td align="right">'.number_format($i->debet, 2).'</td>';
				$data.= '<td align="right">'.number_format($i->credit, 2).'</td>';
				$data.= '<td align="right">'.number_format($saldo, 2).'</td>';
				$data.= '</tr>';
			}
			$data.= '<tr><td colspan="100" class="_groupTable">';
			$data.= '<table>';
			$data.= '<tr>';
			$data.= '<td style="border: none" width="00px">Saldo Awal</td>';
			$data.= '<td style="border: none" width="10px">:</td>';
			$data.= '<td style="border: none" width="100px" align="right">'.number_format($awal[$acc->kdac]->total, 2).'</td>';
			$data.= '<td style="border: none" width="100px"></td>';
			$data.= '<td style="border: none" width="50px">Debet</td>';
			$data.= '<td style="border: none" width="10px">:</td>';
			$data.= '<td style="border: none" width="100px" align="right">'.number_format($debet, 2).'</td>';
			$data.= '<td style="border: none"></td>';
			$data.= '</tr>';
			$data.= '<tr>';
			$data.= '<td style="border: none">Saldo Akhir Buku</td>';
			$data.= '<td style="border: none">:</td>';
			$data.= '<td style="border: none" align="right">'.number_format($saldo, 2).'</td>';
			$data.= '<td style="border: none"></td>';
			$data.= '<td style="border: none">Credit</td>';
			$data.= '<td style="border: none">:</td>';
			$data.= '<td style="border: none" align="right">'.number_format($credit, 2).'</td>';
			$data.= '</tr>';
			$data.= "</table>";
		}
		$data.= "</table>";
	
		$this->data->data = $data;
		$this->data->widthPaper= 1000;
	}

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

	public function View()
	{
		$this->genData();
		$this->layout = "report/manual";
		
		if($_GET['convert'] == 'excel'){
			$filename = str_ireplace(" ", "_",$this->set->modul).".xls";
			header('Content-type: application/ms-excel');
			header('Content-Disposition: attachment; filename='.$filename);
			echo $this->data->data;
			$this->layout = "null";
		}
		
	}
}