<?php

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

  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     = 'Laporan Laba Rugi';
    $this->set->form    = $this->setForm();
    $this->set->filter    = $this->report->setFilter($_GET, $this->setForm());
    $this->set->query     = false;
    $this->set->notNull   = array("date1", "date2", "paper");
    $this->set->button    = array("Cetak", "Excel");
    $this->set->groupDis  = array("Account" => 'kdac');
    $this->set->groupBy   = "";
    $this->set->hitung    = array();
    $this->set->rowPage   = 50;
    $this->baseTitle      = $this->set->modul;
	
	
    $filter = $this->set->filter;
	$date = new Date;
	$tgl = explode("/", $filter->date1->value);
	$tgl = $tgl[1].'-'.$tgl[0].'-01';
	$awal = date("Y-01-01", strtotime($tgl));
	$akhir = date("Y-m-01", strtotime($tgl));
	$akhir = $date->getPenjumlahanTanggal($akhir, 1, 0, 0);
	$akhir = $date->getPenguranganTanggal($akhir, 0, 1, 0);
	$tLalu = $date->getPenguranganTanggal($akhir, 0, 0, 1);
	$tLalu = date("Y-12-31", strtotime($tLalu));

	$blnIndo = $date->blnIndo();
	$lalu = $date->getPenguranganTanggal($tgl, 0, 1, 0);
	$lalu = date("Y-m-01", strtotime($lalu));
	if($lalu < $awal){
		$awal = $lalu;
	}

	$bulan = array();

	$idB = (int)date("m", strtotime($akhir));
	$i = array();
	$i['label'] = 'BULAN '.strtoupper($blnIndo[$idB]);
	$i['name'] 	= "vals";
	$bulan[] =  $i;

	$idB = (int)date("m", strtotime($lalu));
	$i = array();
	$i['label'] = 'BULAN '.strtoupper($blnIndo[$idB]);
	$i['name'] 	= "vall";
	$bulan[] =  $i;

	$i = array();
	$i['label'] = 'TAHUN '.date("Y", strtotime($akhir));
	$i['name'] 	= "valt";
	$bulan[] =  $i;
	
	
	$setPer = array();
	$setPer['bulan'] 	= $bulan;
	$setPer['awal'] 	= $awal;
	$setPer['akhir'] 	= $akhir;
	$setPer['lalu'] 	= $lalu;
	$setPer['tLalu'] 	= $tLalu;
	
	$this->periode = $setPer;
    $this->set->display   = $this->setReport();
	
    return $this->set;
  }
  
  
  protected function defValue()
  {
    $lap = new Lap;
    // $res->cabang  = $lap->getMinMax($this->set, "cabang", "kd_cabang");
    $res->account = $lap->getMinMax($this->set, "acc", "kdac");
    return $res; 
  }
  
  protected function setForm()
  {
    $this->defVal = $this->defValue();
    
    $form[] = array('row' =>  0,  'col' => 0, 'display' => true,   "label" => array("name" => "Periode",   "width" => 100),  "input" => array("type" => 'text', 'attr' => array("name" => 'date1', "size" => 7, 'class' => 'meioMask', 'alt' => 'date-us', 'value' => date("m/Y"))));
  
    $form[] = array('row' =>  25,  'col' => 0,   "label" => array("name" => "Hilangkan 0",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'hide0', "size" => 2, 'value' => 'T', 'after' => 'select()', "select" => 'tf')));
  
    $form[] = array('row' =>  50,  '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;
  }
  
  protected function setReport()
  {
		$res[] = array('id' => 'ket',		'nama' => 'Nama Account', 	'type' => 'text',   'width' => 410,  'size' => 200);
		// if($this->login['kd_user'] == 'ADMIN'){
			// $res[] = array('id' => 'detil',		'nama' => 'DET', 	'type' => 'text',   'width' => 30,  'size' => 200);
		// }
		foreach($this->periode['bulan'] as $r){
			$res[] = array('id' => 'spasi'.$no++,		'nama' => '&nbsp;',   	'type' => 'text', 'width' => 1,  'size' => 15, 'null' => true);
			$res[] = array('id' => $r['name'],		'nama' => $r['label'],   	'type' => 'numeric', 'width' => 100,  'size' => 15, 'dec' => 2, 'null' => true);
		}
		
    
		return $res;
  }
  
  public function Index()
  {
    $this->data = $this->set;
    $this->view = "default/report";
  }
  
  public function getNilai()
  {
    $filter = $this->set->filter;

	$res = array();
	$idL = date("Y-m", strtotime($this->periode['lalu']));
	$idS = date("Y-m", strtotime($this->periode['akhir']));
	$blnAw = date("Y-01", strtotime($this->periode['awal']));
	
	if($blnAw == '2026-01'){
		$sql = $this->em->addSelect("kdac, to_char(a.tgl, 'yyyy-mm') as bln, sum(credit-debet) as saldo");
		$sql = $this->em->getFrom("acr2", 'a');
		$sql = $this->em->getWhere("kdac >= '2'");
		$sql = $this->em->ANDWhere("alias = 'SAW-LR'");
		$sql = $this->em->getGroupBy("kdac, to_char(a.tgl, 'yyyy-mm')");
		$sql = $this->em->getMultiSort("kdac asc");
		$eks = $this->em->queryEks($sql);
		while($row = $this->em->fetchObject($eks)){
			$res['valt'][$row->kdac] = $res['valt'][$row->kdac] + $row->saldo;
			
			if($idL == $row->bln){
				$res['vall'][$row->kdac] = $res['vall'][$row->kdac] + $row->saldo;
			}else
			if($idS == $row->bln){
				$res['vals'][$row->kdac] = $res['vals'][$row->kdac] + $row->saldo;
			}
		}
	}
	
    $sql = $this->em->addSelect("kdac, to_char(a.tgl, 'yyyy-mm') as bln, sum(credit-debet) as saldo");
    $sql = $this->em->getFrom("acd", 'a');
    $sql = $this->em->getWhere("kdac >= '2'");
    $sql = $this->em->ANDWhere("alias != 'JPN'");
	$sql = $this->em->andWhere("a.tgl >= '2026-03-01 00:00:00'");
	$sql = $this->em->andWhere("a.tgl >= '".date('Y-m-d 00:00:00', strtotime($this->periode['awal']))."'");
	$sql = $this->em->andWhere("a.tgl <= '".date('Y-m-d 23:59:59', strtotime($this->periode['akhir']))."'");
    $sql = $this->em->getGroupBy("kdac, to_char(a.tgl, 'yyyy-mm')");
    $sql = $this->em->getMultiSort("kdac asc");
	$eks = $this->em->queryEks($sql);
	while($row = $this->em->fetchObject($eks)){
		
		
		if($row->bln >= $blnAw){
			$res['valt'][$row->kdac] = $res['valt'][$row->kdac] + $row->saldo;
		}
		
		if($idL == $row->bln){
			$res['vall'][$row->kdac] = $res['vall'][$row->kdac] + $row->saldo;
		}else
		if($idS == $row->bln){
			$res['vals'][$row->kdac] = $res['vals'][$row->kdac] + $row->saldo;
		}
		

		foreach($this->periode['bulan'] as $r){
			$id = $r['name'];
			$res[$id][$row->kdac] = number_format($res[$id][$row->kdac], 5, '.', '');
		}
	}
	
	return $res;
  }
  
  public function cekTotal($data, $temp, $nilai, $tot = 0)
  {
		foreach($data as $row){
			if(!empty($nilai[$row->kdac])){
				$tot = $tot + $nilai[$row->kdac];
			}
			if(count($temp[$row->kdac])){
				$tot = $this->cekTotal($temp[$row->kdac], $temp, $nilai, $tot);
			}
		}
		return $tot;
  }
  
  public function cekDet($res, $temp, $nilai, $row, $jum = 0)
  {
		if(empty($row->kdac)){
				return $res;
		}
	  
		$kode =  new kode;
		$jrk = $jum * 5;
		$spasi = $kode->getRight('', $jrk, '&nbsp;');
		$spasi = "<span style='color: #fff'>$spasi</span>";
		$res[$row->kdac]->ket = $spasi.$row->nmac;
		$res[$row->kdac]->kdac = $row->kdac;
		$res[$row->kdac]->detil = $row->detil;
		foreach($this->periode['bulan'] as $r){
			$id = $r['name'];
			if(!empty($nilai[$id][$row->kdac])){
				$res[$row->kdac]->$id = $nilai[$id][$row->kdac];
			}
		}

		$jum = $jum + 1;
		foreach($temp[$row->kdac] as $row2){
			$res = $this->cekDet($res, $temp, $nilai, $row2, $jum);
		}
				
		if(count($temp[$row->kdac])){
			$jrk = ($jum * 5) - 2;
			$spasi = $kode->getRight('', $jrk, '&nbsp;');
			$spasi = "<span style='color: #fff'>$spasi</span>";
			$res[$row->kdac.'A']->ket = $spasi.'TOTAL '.$row->nmac;
			$res[$row->kdac.'A']->ket_css = 'text-align: right; padding-right: 30px;';
			$res[$row->kdac.'A']->detil = 'F';
			foreach($this->periode['bulan'] as $r){
				$id = $r['name'];
				$idCss = $r['name'].'_css';
				$total = $this->cekTotal($temp[$row->kdac], $temp, $nilai[$id]);
				$res[$row->kdac.'A']->$id = $total;
				$res[$row->kdac.'A']->$idCss = '; border-top: 1px dashed #000; border-bottom: 3px double #000';
			}
		}
		
		return $res;
  }
  
	public function delKecil($data, $temp)
	{
		$delInd = array();
		foreach($data as $k => $r){
			if(!empty($temp[$k])){
				$cek = false;
				foreach($temp[$k] as $rd){
					if($rd->detil == 'T'){
						$cek = true;
					}
				}
				if($cek == false){
					$delInd[$k] = $k;
				}
			}
		}
		foreach($temp as $k => $r){
			if(!empty($temp[$k])){
				$cek = false;
				foreach($temp[$k] as $rd){
					if($rd->detil == 'T'){
						$cek = true;
					}
				}
				if($cek == false){
					$delInd[$k] = $k;
				}
			}
		}
		return $delInd;
	}
  
  public function genData()
  {
	$nilai = $this->getNilai();
  
	$kode = new Kode;
	$data = $this->em->getFindBy("acc", "kdac, nmac, induk, detil", array("kdac >= '2'"), "kdac asc");
	$temp = array();
	$tempAsli = array();
	$asli = array();
	foreach($data as $row){
		$tempAsli[$row->induk][$row->kdac] = $row;
		if(!empty($row->kdac)){
			if(substr($row->induk, 0, 6) == '5.2.00' and $row->detil == 'T'){
				$row->induk = '5.2.00';
			}else
			if(substr($row->induk, 0, 6) == '5.3.00' and $row->detil == 'T'){
				$row->induk = '5.3.00';
			}else
			if(substr($row->induk, 0, 6) == '5.4.00' and $row->detil == 'T'){
				$row->induk = '5.4.00';
			}else
			if(substr($row->induk, 0, 6) == '5.1.00' and $row->detil == 'T'){
				$row->induk = '5.1.00';
			}
			
			$temp[$row->induk][$row->kdac] = $row;
			$asli[$row->kdac] = $row;
		}
	}
	
	$data = $this->em->getFindById("acc", "kdac, nmac", array("kdac >= '2'", "(induk is null or induk = '')"), "kdac asc", "kdac");
	$dataAsli = $data;
	
	$delInd = $this->delKecil($data, $temp);
	foreach($delInd as $k => $r){
		unset($temp[$k]);
	}
	$data = array();
	foreach($temp as $k => $r){
		$data[] = $asli[$k];
	}
	

	$totS = array();
	$total = array();
	foreach($dataAsli as $row){
		$tot = array();
		foreach($this->periode['bulan'] as $r){
			$id = $r['name'];
			if(!empty($nilai[$id][$row->kdac])){
				$tot[$id] = $nilai[$id][$row->kdac];
			}
			$total[$id] = $total[$id] + $tot[$id];
			if(count($tempAsli[$row->kdac])){
				$tot[$id] = $this->cekTotal($tempAsli[$row->kdac], $tempAsli, $nilai[$id], 0);
			}
			$total[$id] = $total[$id] + $tot[$id];
		}
		$totS[$row->kdac] = $tot;
	}
	
	$totLrKotor = array();
	$totLrNonOp = array();
	foreach($totS as $k => $row){
		if($k <= '5.9'){
			foreach($row as $k => $r){
				$totLrKotor[$k] = $totLrKotor[$k] + $r;
				$totLrKotor[$k] = number_format($totLrKotor[$k], 5, '.', '');
			}
		}else{
			foreach($row as $k => $r){
				$totLrNonOp[$k] = $totLrNonOp[$k] + $r;
				$totLrNonOp[$k] = number_format($totLrNonOp[$k], 5, '.', '');
			}
		}
	}
	
	$res = array();
	foreach($data as $row){
		
		if($row->kdac == '6.1.00'){
			$res['5.9.99']->ket = '&nbsp;';
			
			$res['5.9.99.A']->ket = 'LABA (RUGI) KOTOR';
			foreach($this->periode['bulan'] as $r){
				$id = $r['name'];
				$idCss = $r['name'].'_css';
				$res['5.9.99.A']->$id = $totLrKotor[$id];
			}
			
			$res['5.9.99.B']->ket = '&nbsp;';
		}
		
		$res = $this->cekDet($res, $temp, $nilai, $row, 0);
	}


	

	if(strtoupper($_GET['hide0']) == 'T'){
		foreach($res as $k => $r){
			if($r->vall == 0 and $r->vals == 0 and $r->valt == 0){
				if($r->detil == 'T'){
					unset($res[$k]);
				}else{
					if(!empty($r->kdac)){
						if(empty($temp[$r->kdac])){
							unset($res[$k]);
						}
					}
				}
			}
		}
	}
	
	$res['z06']->ket = '&nbsp;';
	
	$res['z07']->ket = 'LABA (RUGI) NON OPERASI';
	foreach($this->periode['bulan'] as $r){
		$id = $r['name'];
		$idCss = $r['name'].'_css';
		$res['z07']->$id = $totLrNonOp[$id];
	}
	
	$res['z08']->ket = '&nbsp;';
	
	$res['z09']->ket = 'TOTAL LABA (RUGI)';
	foreach($this->periode['bulan'] as $r){
		$id = $r['name'];
		$idCss = $r['name'].'_css';
		$res['z09']->$id = $total[$id];
	}
	
	$res['z11']->ket = '&nbsp;';
	$res['z11']->css = '; border-top: 3px double #000';
	
	$lrDitahan = $this->em->getFind("acd", "sum(credit-debet) as val", array("kdac = '1.3.09.10'", "tgl <= '".$this->periode['akhir']." 23:59:59'"), false);
	$res['z12']->ket  = 'SALDO LABA (DEFISIT) DITAHAN PER 31 DESEMBER '.date("Y", strtotime($this->periode['tLalu']));
	$res['z12']->valt = $lrDitahan->val;
	
	$res['z13']->ket  = 'LABA (RUGI) SELAMA TAHUN '.date("Y", strtotime($this->periode['akhir']));
	$res['z13']->valt = $total['valt'];

	$date = new Date;
	$blnIndo = $date->blnIndo();
	$lalu = $date->getPenjumlahanTanggal($this->periode['lalu'], 1, 0, 0);
	$lalu = $date->getPenguranganTanggal($lalu, 0, 1, 0);
	
	$m = (int)date("m", strtotime($this->periode['lalu']));
	$res['z14']->ket  = 'SALDO LABA (DEFISIT) DITAHAN PER '.date("d", strtotime($lalu)).' '.strtoupper($blnIndo[$m]).' '.date("Y", strtotime($lalu));
	$res['z14']->valt = $res['z12']->valt + $res['z13']->valt;

	$res['z15']->ket = '&nbsp;';
	$res['z15']->css = '; border-bottom: 3px double #000';
	
	$set = $this->em->getFind("setting", "value", array("id_setting = '11'"));
	$after = "
		<style>
		#_ttdDir{
			width: 100%;
		}
		#_ttdDir tr td{
			width: 50%;
			text-align: center;
			font-size: 13px;
		}
		._namaTtd{
			padding-top: 30px;
		}
		</style>
		<table id='_ttdDir'>
			<tr>
				<td>Disahkan oleh,</td>
				<td>Disusun oleh,</td>
			</tr>
			<tr>
				<td class='_namaTtd'><u>".$set->value."</u></td>
				<td class='_namaTtd'><u>".$this->login['kd_user']."</u></td>
			</tr>
			<tr>
				<td>Direktur</td>
				<td>Accounting</td>
			</tr>
		</table>
	";

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