<?php

require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/laporan_aset_tak_berwujud/business/AppLapAset.class.php';

class ViewLaporanAsetTakBerwujud extends HtmlResponse {

   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/laporan_aset_tak_berwujud/template');
      $this->SetTemplateFile('view_laporan_aset_tak_berwujud.html');
   }
   
   function ProcessRequest() {
      $Obj = new AppLapAset();
      $User = $Obj->GetDataUserByUsername($_SESSION['username']);
		
		$filter = array();
		if (isset($_POST['cari'])) $filter = $_POST->AsArray();
		
		if (!empty($filter))
      {
         $filter['tahun'] = $filter['tahun'];
      } else {	
			$filter['tahun'] = date('Y');
			$filter['jenis_kib'] = 'all';
			$filter['unit'] = 'all';
		}
		
		for($i=0; $i <= 20; $i++){
			$tanggal[] = array(
				'id' => date('Y')-$i,
				'name' => date('Y')-$i
			);
		}
		//sort($tanggal);
		Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'tahun', array('tahun', $tanggal, $filter['tahun'], "false", ''), Messenger::CurrentRequest);
		
		//set combo gedung
      $arrComboUnit = $Obj->GetComboUnit();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'unit', array('unit', $arrComboUnit, $filter['unit'], true, ''), Messenger::CurrentRequest);
		
		$cbxkib = $Obj->GetCbxJenisKib();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jenis_kib', array('jenis_kib', $cbxkib, $filter['jenis_kib'], true, ''), Messenger::CurrentRequest);
		
      if(isset($_POST['cari']))
         $return['data']= $Obj->GetListAset($filter);
      
      $return['unit']=$filter;
		
   	return $return;  
   }
   
   function ParseTemplate($data = NULL) {
      $urlAction = Dispatcher::Instance()->GetUrl('laporan_aset_tak_berwujud', 'laporanAsetTakBerwujud', 'view', 'html');
      $this->mrTemplate->addVar('content','URL_ACTION',$urlAction);
		
      $unit = Dispatcher::Instance()->Encrypt($data['unit']['unit']);
		$tahun = Dispatcher::Instance()->Encrypt($data['unit']['tahun']);
		$jenis_kib = Dispatcher::Instance()->Encrypt($data['unit']['jenis_kib']);
      
      $urlPrint = Dispatcher::Instance()->GetUrl('laporan_aset_tak_berwujud', 'printLaporanAsetTakBerwujud', 'view', 'html')
			.'&unit='.$unit
			.'&tahun='.$tahun
			.'&jenis_kib='.$jenis_kib;
      $this->mrTemplate->addVar('content','URL_CETAK',$urlPrint);
		$this->mrTemplate->addVar('content','TAHUN',$tahun);
		
      $urlExcel = Dispatcher::Instance()->GetUrl('laporan_aset_tak_berwujud', 'excelLaporanAsetTakBerwujud', 'view', 'xlsx')
			.'&unit='.$unit
			.'&tahun='.$tahun
			.'&jenis_kib='.$jenis_kib;
      $this->mrTemplate->addVar('content','URL_EXCEL',$urlExcel);
      
      $urlPopup = Dispatcher::Instance()->GetUrl('laporan_aset_tak_berwujud', 'PopupUnitKerja', 'view', 'html');
      $this->mrTemplate->addVar('content','URL_POPUP',$urlPopup);
      $this->mrTemplate->addVar('content','UNITNAMA',$data['unit']['unitnama']);
      $this->mrTemplate->addVar('content','UNIT',$data['unit']['unit']);
      
      if(empty($data['data'])){
      	$this->mrTemplate->AddVar('data','DATA_EMPTY','YES');
      	return;
		}else
			$this->mrTemplate->AddVar('data','DATA_EMPTY','NO');
			
			$dataGrid = $data['data'];
			$dataGrid2 = $dataGrid;
			$kode_akun ="";
			for($i=0;$i<count($dataGrid);$i++){
				
				if($kode_akun != $dataGrid[$i]['kode_akun']){
					$kode_akun = $dataGrid[$i]['kode_akun'];
					$send = array();
					$send['class_name'] = 'table-common-even1';
					$send['kode_barang'] = $dataGrid[$i]['kode_akun'];
					$send['nama_barang'] = $dataGrid[$i]['nama_golongan'];
					
					for($a=0;$a<count($dataGrid2);$a++){
						if($kode_akun == $dataGrid2[$a]['kode_akun']){
							$nilai_awal[$kode_akun] += $dataGrid2[$a]['nilai_awal'];
							$nilai_tambah[$kode_akun] += $dataGrid2[$a]['nilai_tambah'];
							$nilai_kurang[$kode_akun] += $dataGrid2[$a]['nilai_kurang'];
							$nilai_akhir[$kode_akun] += $dataGrid2[$a]['nilai_akhir'];
							$kuantitas_awal[$kode_akun] += $dataGrid2[$a]['kuantitas_awal'];
							$kuantitas_tambah[$kode_akun] += $dataGrid2[$a]['kuantitas_tambah'];
							$kuantitas_kurang[$kode_akun] += $dataGrid2[$a]['kuantitas_kurang'];
							$kuantitas_akhir[$kode_akun] += $dataGrid2[$a]['kuantitas_akhir'];
						}
					}
					$send['nilai_awal'] = number_format($nilai_awal[$kode_akun], 2, ',', '.');
					$send['nilai_tambah'] = number_format($nilai_tambah[$kode_akun], 2, ',', '.');
					$send['nilai_kurang'] = number_format($nilai_kurang[$kode_akun], 2, ',', '.');
					$send['nilai_akhir'] = number_format($nilai_akhir[$kode_akun], 2, ',', '.');
					$send['kuantitas_awal'] = number_format($kuantitas_awal[$kode_akun], 0, ',', '.');
					$send['kuantitas_tambah'] = number_format($kuantitas_tambah[$kode_akun], 0, ',', '.');
					$send['kuantitas_kurang'] = number_format($kuantitas_kurang[$kode_akun], 0, ',', '.');
					$send['kuantitas_akhir'] = number_format($kuantitas_akhir[$kode_akun], 0, ',', '.');
					$this->mrTemplate->AddVars('data_item',$send,'IN_');
					$this->mrTemplate->parseTemplate('data_item','a');
				}
				
				if(!empty($dataGrid[$i]['kode_barang'])){
					$send = array();
					$data1['nilai_awal_total'] += $dataGrid[$i]['nilai_awal'];
					$data1['nilai_tambah_total'] += $dataGrid[$i]['nilai_tambah'];
					$data1['nilai_kurang_total'] += $dataGrid[$i]['nilai_kurang'];
					$data1['nilai_akhir_total'] += $dataGrid[$i]['nilai_akhir'];
					$send['kode_barang'] = $dataGrid[$i]['kode_barang'];
					$send['nama_barang'] = $dataGrid[$i]['nama_barang'];
					$send['satuan'] = $dataGrid[$i]['satuan'];
					$send['nilai_awal'] = number_format($dataGrid[$i]['nilai_awal'], 2, ',', '.');
					$send['nilai_tambah'] = number_format($dataGrid[$i]['nilai_tambah'], 2, ',', '.');
					$send['nilai_kurang'] = number_format($dataGrid[$i]['nilai_kurang'], 2, ',', '.');
					$send['nilai_akhir'] = number_format($dataGrid[$i]['nilai_akhir'], 2, ',', '.');
					$send['kuantitas_awal'] = number_format($dataGrid[$i]['kuantitas_awal'], 0, ',', '.');
					$send['kuantitas_tambah'] = number_format($dataGrid[$i]['kuantitas_tambah'], 0, ',', '.');
					$send['kuantitas_kurang'] = number_format($dataGrid[$i]['kuantitas_kurang'], 0, ',', '.');
					$send['kuantitas_akhir'] = number_format($dataGrid[$i]['kuantitas_akhir'], 0, ',', '.');
					$this->mrTemplate->AddVars('data_item',$send,'IN_');
					$this->mrTemplate->parseTemplate('data_item','a');
				}
			}
			$data1['nilai_awal_total'] = number_format($data1['nilai_awal_total'], 2, ',', '.');
			$data1['nilai_tambah_total'] = number_format($data1['nilai_tambah_total'], 2, ',', '.');
			$data1['nilai_kurang_total'] = number_format($data1['nilai_kurang_total'], 2, ',', '.');
			$data1['nilai_akhir_total'] = number_format($data1['nilai_akhir_total'], 2, ',', '.');
			$this->mrTemplate->addVars('content',$data1, '');
   }
}
?>
