<?php

CLASS fix extends RasioProject
{
	protected $set;
	protected $enableCk = true;

	public function __construct($config)
	{
		$this->getConfig($config);
		$this->validation();
		$this->set();
	}

	protected function set()
	{
		$this->set->em 			= $this->em;
		$this->set->modul 		= 'Fix Assets';
		$this->set->dBaseH 		= 'inv';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('kd_inv');
		$this->set->dSort 		= 'asc';
		$this->set->where 		= array("jenis = '212'");
		$this->set->form 		= $this->setForm();
		$this->set->clearForm 	= array("ket_induk");
		$this->set->notNull 	= array("nm_inv", "model", "lok", "pemilik");
		$this->set->grid 		= $this->setGrid();
		$this->set->cetak 		= $this->setPrint();
		$this->set->button 		= array("First", "Prev", "Next", "Last", "Browse", "Add", "Del", "Edit", "prinT");
		$this->set->addClear 	= true;
		$this->set->detail 		= $this->setDetail();
		$this->set->funct 		= '_fixAsset';
		return $this->set;
	}
	
	protected function setForm()
	{
		$rwd = 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Kode Fix Assets", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_inv', "size" => 20)));

		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Nama Fix Assets", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nm_inv', "size" => 30)));
		
		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 180, "label" => true, "input" => array("type" => 'text',	'attr' => array("name" => 'model_name', "size" => 40, 'readonly' => true)));
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Jenis", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'model', "size" => 4, 'after' => 'select("")', 'select' => 'fixasset', 'display' => '_selectDisplay', 'target' => 'model_name')));
		
		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 180, "label" => true, "input" => array("type" => 'text',	'attr' => array("name" => 'lok_name', "size" => 40, 'readonly' => true)));
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Lokasi", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'lok', "size" => 4, 'after' => 'select("")', 'select' => 'gudang', 'select_width' => 280, 'select_height' => 245, 'select_label' => "Kode=>50;Name=>200", 'select_target' => "name=>lok_name", 'select_warning' => true, 'display' => 'selectDisplay')));
		
		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Pemilik", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'pemilik', "size" => 15, 'after' => 'select("")', 'select' => 'pemilik')));
		
		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Metode", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'metode', "size" => 15, 'after' => 'select("")', 'select' => 'metodepenyusutan')));

		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Lama (tahun)", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'lama', "size" => 4, 'class' => 'meioMask', 'alt' => 'cc')));
		
		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Tgl. Penyusutan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'tgl_penyusutan', "size" => 10, 'class' => 'tanggal')));
		
		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Nilai Perolehan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'harga', "size" => 10, 'class' => 'decimal')));
		$form[] = array('row' => $row, 'col' => 220, "label" => array("name" => "<input type='button' id='_fixHit' value='Calc'>", "width" => 100), "input" => false);

		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nmac', "size" => 30, 'readonly' => true)));
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Akm Penyusutan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kdac', "size" => 10, 'after' => 'load("Macd", "false", "account")', 'display' => 'account', 'target' => 'nmac')));

		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nmac2', "size" => 30, 'readonly' => true)));
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Biaya", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kdac2', "size" => 10, 'after' => 'load("Macd", "false", "account")', 'display' => 'account', 'target' => 'nmac2')));

		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub', "size" => 30, 'readonly' => true)));
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Sub 1", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub', "size" => 10, 'after' => 'load("sub", "false", "sub")', 'display' => 'sub', 'target' => 'nm_sub')));

		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub2', "size" => 30, 'readonly' => true)));
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Sub 2", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub2', "size" => 10, 'after' => 'load("sub", "false", "sub")', 'display' => 'sub', 'target' => 'nm_sub2')));

		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Keterangan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket', "size" => 50)));

		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "By", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chusr', 'size' => 15, 'readonly' => true)));
		$row = $row + $rwd;
		$form[] = array('row' => $row, 'col' => 500, "label" => array("name" => "Last", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chtime', 'size' => 20, 'readonly' => true)));
		return $form;
	}
	
	public function setDetail()
	{
		$res->width 	= 300;
		$res->height	= 300;
		$res->col		= 500;
		$res->row		= 0;
		$res->data		= 'susut';
		$res->pk		= 'kd_inv';
		$res->sort		= 'nosusut';
		
		$res->attr[] = array("label" => 'Periode',		"attr" => 'tgl', 	'width' => 50, 'readonly' => true, 'type' => 'hidden');
		$res->attr[] = array("label" => 'Bulan',		"attr" => 'bln', 	'width' => 40, 'readonly' => true);
		$res->attr[] = array("label" => 'Tahun', 		"attr" => 'thn', 	'width' => 40, 'readonly' => true);
		$res->attr[] = array("label" => 'Nilai',		"attr" => 'nilai',	'width' => 100, 'class' => 'decimal', 'readonly' => true);
		return $res;
	}
	
	protected function setGrid()
	{
		$kode = new Kode;
		$res->page 	= 50;
		$res->sortA = $kode->getPk($this->set->dBasePk);
		$res->sortT = 'asc';
		
		$res->attr[]	= array("label" => 'Kode', 'attr' => 'a.kd_inv', "sort" => 'true', "width" => 50, 'align' => 'left');
		$res->attr[]	= array("label" => 'Nama', 'attr' => 'a.nm_inv', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'Acc.Per', 'attr' => 'a.kdac', "sort" => 'true', "width" => 60, 'align' => 'left');
		$res->attr[]	= array("label" => 'Acc.Bi', 'attr' => 'a.kdac2', "sort" => 'true', "width" => 60, 'align' => 'left');
		$res->attr[]	= array("label" => 'Jenis', 'attr' => 'a.model', "sort" => 'true', "width" => 100, 'align' => 'left');
		$res->attr[]	= array("label" => 'Metode', 'attr' => 'a.metode', "sort" => 'true', "width" => 100, 'align' => 'left');
		$res->attr[]	= array("label" => 'Lama', 'attr' => 'a.lama', "sort" => 'true', "width" => 40, 'align' => 'right', "type" => 'numeric');
		$res->attr[]	= array("label" => 'Tgl. Peny', 'attr' => 'a.tgl_penyusutan', "sort" => 'true', "width" => 80, 'align' => 'left', "type" => 'date');
		
		return $res;
	}
	
	protected function setPrint()
	{
		$kode = new Kode;
		$res->sortA = $kode->getPk($this->set->dBasePk);
		$res->sortT = 'asc';
		$res->width = '1000px';
		
		$res->attr[]	= array("label" => 'Kode', 'attr' => 'a.kd_inv', "sort" => 'true', "width" => 50, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Nama', 'attr' => 'a.nm_inv', "sort" => 'true', "width" => 150, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Acc.Per', 'attr' => 'a.kdac', "sort" => 'true', "width" => 60, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Acc.Bi', 'attr' => 'a.kdac2', "sort" => 'true', "width" => 60, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Jenis', 'attr' => 'a.model', "sort" => 'true', "width" => 100, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Metode', 'attr' => 'a.metode', "sort" => 'true', "width" => 100, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Lama (Tahun)', 'attr' => 'a.lama', "sort" => 'true', "width" => 40, 'align' => 'right', "type" => 'numeric', 'filter' => true);
		$res->attr[]	= array("label" => 'Tgl. Peny', 'attr' => 'a.tgl_penyusutan', "sort" => 'true', "width" => 80, 'align' => 'left', "type" => 'date', 'filter' => true);
		$res->attr[]	= array("label" => 'Keterangan', 'attr' => 'a.ket', "sort" => 'true');
		
		return $res;
	}
	
	public function Index()
	{
		$this->data = $this->set;
		$this->view = "default/index";
	}
	
	public function data()
	{
		$basic = new Basic;
		$this->data = $basic->data($_GET, $this->set);
		$this->view = "default/displayData";
		$this->layout = "view";
	}
	
	public function Find()
	{
		$basic = new Basic;
		$this->data = $basic->find($_GET, $this->set);
		$this->view = "default/displayForm";
		$this->layout = "view";
	}
	
	protected function validasi()
	{
		$basic = new Basic;
		$error[] = $basic->validasiBasic($_POST, $this->set);
		
		return $basic->getError($error);
	}
	
	public function proses()
	{
		if($_POST['pemilik'] == 'PERUSAHAAN'){
			$this->set->notNull = array("nm_inv", "kdac", "kdac2", "model", "tgl_penyusutan", "lama", "metode", "lok", "pemilik");
		}
		
		$basic = new Basic;
		$validasi = $this->validasi();
		if($validasi == true){
			$tambah['jenis'] = 212;
			$tambah['kd_skat'] = 'FIX-FIX';
			$tambah['nm_skat'] = 'ASSET';
			
			if(empty($_POST['kd_sub'])){
				$_POST['nm_sub'] = '';
			}
			if(empty($_POST['kd_sub2'])){
				$_POST['nm_sub2'] = '';
			}
			
			$date = new Date;
			$_POST['tgl_penyusutan'] = $date->serialDate($_POST['tgl_penyusutan']);
			$kode = new Kode;
			if($_POST['_prosesAction'] == 'simpan'){
				// $model = $_POST['model'];
				// $prefik = 'FIX-'.date("Ym", strtotime($_POST['tgl_penyusutan'])).'-';
				// $last = $this->em->getFindBy("inv", "kd_inv", array("jenis = '212'", "kd_inv like '$prefik%'"), "kd_inv desc");
				// $last = $last[0]->kd_inv;
				// $last = str_ireplace($prefik, "", $last);
				// $last = (int)$last + 1;
				// $last = $kode->getRight($last, 3, '0');
				
				// $_POST['kd_inv'] = $prefik.$last;
			}
			
			$no = 1;
			$qb = array();
			$qb[] = $this->deleteDet($_POST['kd_inv']);
			foreach($_POST['tgl'] as $k => $r){
				$r = trim($r);
				if(!empty($r)){
					$qb[] = $this->saveDet($k, $no++);
				}
			}
			
			// echo "<pre>";
			// var_dump($qb);
			
			// ini_set('display_errors',1); 
			$basic->proses($_POST, $this->set, $tambah, $qb);
		}
		$this->layout = "null";
	}
	
	public function deleteDet($inv)
	{
		$sql = $this->em->addDelete('susut');
		$sql = $this->em->getWhere("kd_inv = '$inv'");
		return $sql;
	}
	
	public function saveDet($id, $nom)
	{
		$kode = new Kode;
		$attr = array();
		foreach($this->set->detail->attr as $k => $r){
			$no = $r['attr'];
			$attr[$no] = strtoupper($_POST[$no][$id]);
		}
		$attr['nosusut'] 	= $_POST['kd_inv'].'.'.$kode->getRight($nom, 4, 0);
		$attr['kd_inv'] 	= $_POST['kd_inv'];
	
		$sql = $this->em->addInsert($this->set->detail->data);
		$sql = $this->em->addAttrIns($attr);
		return $sql;
	}
	
	public function Delete()
	{
		$basic = new Basic;
		$this->data = $basic->delete($_GET, $this->set);
		$this->layout = "null";
	}
	
	public function cetak()
	{
		$basic = new Basic;
		$this->data->data = $basic->printer($_GET, $this->set);
		$this->data->width = $this->set->cetak->width;
		$this->view = "default/cetak";
		$this->layout = "print";
	}
	
	public function load()
	{
		$basic = new Basic;
		$this->data = $basic->load($_GET, $this->set);
		$this->layout = "null";
	}
	
	public function account()
	{
		$data = $this->em->getFind("acc", "nmac", array("kdac = '".$_GET['idRef']."'"));
		
		$res->target = $_GET['target'];
		$res->nama 	= $data->nmac;
		echo json_encode($res);
		$this->layout = "null";
	}
	
	public function popUp()
	{
		$basic = new Basic;
		$this->data = $basic->data($_GET, $this->set);
		$this->view = "default/displayDataPopUp";
		$this->layout = "view";
	}
	
	public function loadSusut()
	{
		$date	= new Date;
		$metode = strtoupper($_GET['metode']);
		$lama 	= $_GET['lama'];
		$tgl 	= $date->serialDate($_GET['tgl']);
		$harga 	= str_ireplace(",", "", $_GET['harga']);
		$data  	= array();
		
		if($metode == 'STRAIGHTLINE'){
			$bulan	= $lama * 12;
			$nilai  = ceil($harga / $bulan);
			$total	= 0;
			for($i = 1; $i <= $bulan; $i++){
				$tam = $i - 1;
				$bln = $date->getPenjumlahanTanggal($tgl, $tam, 0, 0);
				
				if($i == $bulan){
					$nilai = $harga - $total; 
				}
				$total = $total + $nilai;
				
				$data[$bln]->tgl = $bln;
				$data[$bln]->bln = (int)date("m", strtotime($bln));
				$data[$bln]->thn = (int)date("Y", strtotime($bln));
				$data[$bln]->nilai = $nilai;

			}
		}

		$_SESSION[__CLASS__]->det = $data;
		$this->sessionDet(__CLASS__, $this->set, $data);
		echo "<script>_listLoadDetail('".__CLASS__."', '')</script>";
		
		$this->layout = "null";
	}
}