<?php

CLASS jpn 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-006';
    $this->set->date    = date("d/m/Y H:i");
    $this->set->modul     = 'Jurnal Penutup';
    $this->set->form    = $this->setForm();
    $this->set->query     = false;
    $this->set->notNull   = array("date1", "date2", "paper");
    $this->set->button    = array();
    $this->set->groupDis  = array("Account" => 'kdac');
    $this->set->groupBy   = "";
    $this->set->hitung    = array();
    $this->set->rowPage   = 60;
    $this->baseTitle      = $this->set->modul;
    return $this->set;
  }
  
  protected function setForm()
  {
    
    $form[] = array('row' =>  0,  'col' => 0,   "label" => array("name" => "Periode",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'tgl', "size" => 10, 'class' => 'tanggal')));
  
    $form[] = array('row' =>  30,  'col' => 100,   "label" => array("name" => "<input type='button' value='Posting Penutup' id='_jurnlPenutup'>",   "width" => 400),  "input" => false);

    return $form;
  }
  
  public function Index()
  {
    $this->data = $this->set;
    $this->view = "default/report";
  }
  
	public function cekLr($awal, $akhir, $nojnl)
	{
		$sql = $this->em->addSelect("a.kdac, b.nmac, a.kd_cabang, sum(debet-credit) as jum");
		$sql = $this->em->getFrom("acd", 'a');
		$sql = $this->em->addLeftJoin("acc", 'b', array("a.kdac = b.kdac"));
		$sql = $this->em->getWhere("b.nmac is not null");
		$sql = $this->em->andWhere("a.kdac >= '400000'");
		$sql = $this->em->andWhere("a.nojnl != '$nojnl'");
		$sql = $this->em->getGroupBy("a.kdac, b.nmac, a.kd_cabang");
		$sql = $this->em->getMultiSort("a.kdac, a.kd_cabang");
		
		return $this->em->queryEks($sql);
	}
  
	
	public function saveAcd($data, $row, $balik = false)
	{
		$data['kd_cabang'] 	= $row->kd_cabang;
		$data['kdac'] 		= $row->kdac;
		$data['nmac'] 		= $row->nmac;		
		
		if($balik == false){
			if($row->jum > 0){
				$data['debet']	= 0;
				$data['credit']	= str_ireplace("-", "", $row->jum);
			}else{
				$data['debet']	= str_ireplace("-", "", $row->jum);
				$data['credit']	= 0;
			}
		}else{
			if($row->jum > 0){
				$data['debet']	= str_ireplace("-", "", $row->jum);
				$data['credit']	= 0;
			}else{
				$data['debet']	= 0;
				$data['credit']	= str_ireplace("-", "", $row->jum);
			}
		}
		
		$sql = $this->em->addInsert("acd");
		$sql = $this->em->addAttrIns($data);
		return $sql;
	}
	
	public function cekBallance($nojnl)
	{
		$sql = $this->em->addSelect("a.kd_cabang, sum(debet-credit) as jum");
		$sql = $this->em->getFrom("acd", 'a');
		$sql = $this->em->getWhere("a.nojnl = '$nojnl'");
		$sql = $this->em->getGroupBy("a.kd_cabang");
		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function delJurnal($kode)
	{
		$sql = $this->em->addDelete('acd');
		$sql = $this->em->getWhere("nojnl = '$kode'");
		return $sql;
	}
	
	public function Tutup()
	{
		$date 	= new Date;
		$tgl 	= $date->serialDate($_GET['idRef']);
		$awal 	= date("Y-m-01", strtotime($tgl));
		$akhir 	= $date->getPenjumlahanTanggal($awal, 1, 0, 0);
		$tgl 	= $date->getPenguranganTanggal($akhir, 0, 1, 0);
		
		$alias = 'JPN';
		$nojnl = $alias.'/SG/'.date("ymd", strtotime($tgl));

		$lr = $this->cekLr($awal, $akhir, $nojnl);
		
		$attr = array();
		$attr['nojnl'] 	= $nojnl;
		$attr['tgl'] 	= $tgl;
		$attr['alias'] 	= $alias;
		$attr['ket'] 	= 'Jurnal Penutup';
		
		$qb = array();
		$qb[] = $this->delJurnal($nojnl);

		$tot = array();
		while($row = $this->em->fetchObject($lr)){
			if($row->jum <> 0){
				$qb[] = $this->saveAcd($attr, $row);
				$tot[$row->kd_cabang] = $tot[$row->kd_cabang] + $row->jum;
			}
		}
		
		$acc = $this->em->getFind("acc", "*", array("kdac = '314200'"));
		$row->kdac = $acc->kdac;
		$row->nmac = $acc->nmac;

		foreach($tot as $k => $r){
			$row->kd_cabang = $k;
			$row->jum = $r;
			$qb[] = $this->saveAcd($attr, $row, true);
		}
		
		$save = $this->em->getFlush($qb);
		if($save == false){
			echo "<script>alert('Gagal');</script>";
		}else{
			echo "<script>alert('Sukses');</script>";
		}
		echo "<pre>";
		var_dump($qb);
		
		$cek = $this->cekBallance($nojnl);
		$qb = array();
		foreach($cek as $k => $r){
			if($r->jum <> 0){
				$row->kd_cabang = $r->kd_cabang;
				$row->jum = $r->jum;
				$qb[] = $this->saveAcd($attr, $row);
			}
		}
		$save = $this->em->getFlush($qb);
		var_dump($qb);
		
		$this->layout = "null";
	}
}