<?php

CLASS mex 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 		= 'Exibist';
		$this->set->dBaseH 		= 'temah';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('kode');
		$this->set->dSort 		= 'asc';
		$this->set->form 		= $this->setForm();
		$this->set->clearForm 	= array("file_mex");
		$this->set->notNull 	= array("nama", "model", "ket");
		$this->set->grid 		= $this->setGrid();
		$this->set->cetak 		= $this->setPrint();
		$this->set->button 		= array("First", "Prev", "Next", "Last", "Browse", "Add", "Del", "Edit", "Export");
		$this->set->addClear 	= true;
		$this->set->detail 		= $this->setDetail();
		$this->set->notUpper 	= true;
		return $this->set;
	}
	
	protected function setForm()
	{
		$form[] = array('row' => 0, 'col' => 25, "label" => array("name" => "Kode", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kode', "size" => 15, "readonly" => true)));
		$form[] = array('row' => 25, 'col' => 25, "label" => array("name" => "Judul", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nama', "size" => 30)));
		$form[] = array('row' => 50, 'col' => 25, "label" => array("name" => "Type", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'model', "size" => 10, 'after' => 'select("")', 'select' => 'jenexibit')));
		$form[] = array('row' => 75, 'col' => 25, "label" => array("name" => "Jenis", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket', "size" => 5, 'after' => 'select("")', 'select' => 'jenisacc')));
		$form[] = array('row' => 100, 'col' => 25, "label" => array("name" => "File (.xls 2013)", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'file_mex', "size" => 15, 'readonly' => true)));
		$form[] = array('row' => 100, 'col' => 250, "label" => array("name" => '<div id="_fileMex"></div>', "width" => 100), "input" => false);
		
		$form[] = array('row' => 0, 'col' => 400, "label" => array("name" => "Note :", "width" => 400), false);
		$form[] = array('row' => 25, 'col' => 410, "label" => array("name" => "Style : U (Underline), B (Bold), BU (Bold Underline)", "width" => 400), false);
		$form[] = array('row' => 50, 'col' => 410, "label" => array("name" => "Garis : T (Ya), F (Tidak), 'Kosong' (Tidak)", "width" => 400), false);
		$form[] = array('row' => 75, 'col' => 410, "label" => array("name" => "Data : B (Bulanan), K (Kumulatif), 'Kosong' (Sesuai Dengan Type Laporan)", "width" => 600), false);
		$form[] = array('row' => 100, 'col' => 410, "label" => array("name" => "Dibalik : T (- = +, + = -), F (Normal), 'Kosong' (Normal)", "width" => 400), false);

		$form[] = array('row' => 475, 'col' => 25, "label" => array("name" => "By", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chusr', 'size' => 15, 'readonly' => true)));
		$form[] = array('row' => 475, '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 	= 900;
		$res->height	= 300;
		$res->col		= 25;
		$res->row		= 135;
		$res->data		= 'temad';
		$res->pk		= 'kode';
		$res->sort		= 'no';
		
		$res->attr[] = array("label" => 'Urut',		"attr" => 'no', 	'width' => 30);
		$res->attr[] = array("label" => 'Induk',	"attr" => 'induk',	'width' => 40);
		$res->attr[] = array("label" => 'ID',		"attr" => 'id', 	'width' => 40);
		$res->attr[] = array("label" => 'Judul',	"attr" => 'judul', 	'width' => 200);
		$res->attr[] = array("label" => 'Account',	"attr" => 'acc', 	'width' => 100);
		$res->attr[] = array("label" => 'Style',	"attr" => 'bold', 	'width' => 30);
		$res->attr[] = array("label" => 'Garis',	"attr" => 'garis', 	'width' => 30);
		$res->attr[] = array("label" => 'Marg',		"attr" => 'jarak', 	'width' => 20, 	"class" => 'decimal');
		$res->attr[] = array("label" => 'Data',		"attr" => 'periode','width' => 60);
		$res->attr[] = array("label" => 'Dibalik',	"attr" => 'action',	'width' => 40);
		$res->attr[] = array("label" => 'Sub',		"attr" => 'kd_sub',	'width' => 60);
		$res->attr[] = array("label" => 'PC',		"attr" => 'cab',	'width' => 60);
		$res->attr[] = array("label" => 'Mode',		"attr" => 'fix',	'width' => 40);
		$res->attr[] = array("label" => '+/- Period',"attr" => 'jenis',	'width' => 30, 	"class" => 'decimal');
		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" => 'Judul', 'attr' => 'a.nama', "sort" => 'true', "width" => 200, 'align' => 'left', 'focus' => 'true');
		$res->attr[]	= array("label" => 'Jenis', 'attr' => 'a.model', "sort" => 'true', "width" => 200, 'align' => 'left');
		
		return $res;
	}
	
	protected function setPrint()
	{
		$kode = new Kode;
		$res->sortA = $kode->getPk($this->set->dBasePk);
		$res->sortT = 'asc';
		$res->width = '600px';
		
		$res->attr[]	= array("label" => 'Judul', 'attr' => 'a.nama', "sort" => 'true', "width" => 200, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Jenis', 'attr' => 'a.model', "sort" => 'true', "width" => 200, 'align' => 'left', 'filter' => 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()
	{
		$basic = new Basic;
		$validasi = $this->validasi();
		if($validasi == true){
			$qb = array();
			$attrTambah = array();
			$attrTambah['hapus'] = 0;
			if($_POST['_prosesAction'] == 'simpan'){
				$attrTambah['kode'] = date("YmdHis");
				$_POST['kode'] = $attrTambah['kode'];
			}
			$attrTambah['nama'] = strtoupper($_POST['nama']);
			
			$det = $_POST['no'];
			$qb[] = $this->deleteDet($_POST['kode']);
			$no = 1;
			foreach($det as $n => $no){
				if(!empty($no)){
					$qb[] = $this->saveDet($n, $_POST['kode'], $no++);
				}
			}
			
			$basic->proses($_POST, $this->set, $attrTambah, $qb);
		}
		$this->layout = "null";
	}
	
	public function saveDet($id, $pk, $nom)
	{
		$kode = new Kode;
		$attr = array();
		foreach($this->set->detail->attr as $k => $r){
			$no = $r['attr'];
			$attr[$no] = $_POST[$no][$id];
		}
		$attr['kd_det'] = $pk.'.'.$kode->getRight($nom, 3, 0);
		$attr['kode']   = $pk;
		
		$sql = $this->em->addInsert($this->set->detail->data);
		$sql = $this->em->addAttrIns($attr);

		return $sql;
	}
	
	public function deleteDet($id)
	{
		$sql = $this->em->addDelete($this->set->detail->data);
		$sql = $this->em->getWhere("kode = '$id'");
		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 popUp()
	{
		$basic = new Basic;
		$this->data = $basic->data($_GET, $this->set);
		$this->view = "default/displayDataPopUp";
		$this->layout = "view";
	}
	
	public function Detail()
	{
		$this->view = "default/detail";
		$this->layout = "view";
	}
	
	public function Export()
	{
		$head = $this->em->getFind("temah", '*', array("kode = '".$_GET['idRef']."'"));
		$tema = $this->em->getFindBy("temad", '*', array("kode = '".$_GET['idRef']."'"), "no asc");
	
		$data = "<table border='1'>";
		$data.= "<thead>";
		$data.= "<tr>";
		$data.= "<th>Nomor</th>";
		$data.= "<th>induk</th>";
		$data.= "<th>ID</th>";
		$data.= "<th>Nama</th>";
		$data.= "<th>Account</th>";
		$data.= "<th>Style</th>";
		$data.= "<th>Garis</th>";
		$data.= "<th>Margin</th>";
		$data.= "<th>Periode Data</th>";
		$data.= "<th>Dibalik</th>";
		$data.= "<th>Sub</th>";
		$data.= "<th>Profit Center</th>";
		$data.= "<th>Mode</th>";
		$data.= "<th>+/- Periode</th>";
		$data.= "</tr>";
		$data.= "</thead>";
		$data.= "<tbody>";
		foreach($tema as $row){
			$data.= "<tr>";
			$data.= "<td>".$row->no."</td>";
			$data.= "<td>".$row->induk."</td>";
			$data.= "<td>".$row->id."</td>";
			$data.= "<td>".$row->judul."</td>";
			$data.= "<td>".$row->acc."</td>";
			$data.= "<td>".$row->bold."</td>";
			$data.= "<td>".$row->garis."</td>";
			$data.= "<td>".$row->jarak."</td>";
			$data.= "<td>".$row->periode."</td>";
			$data.= "<td>".$row->action."</td>";
			$data.= "<td>".$row->kd_sub."</td>";
			$data.= "<td>".$row->cab."</td>";
			$data.= "<td>".$row->model."</td>";
			$data.= "<td>".$row->jenis."</td>";
			$data.= "</tr>";
		}
		$data.= "</tbody>";
		$data.= "<table>";
	
		$this->set->modul = str_ireplace(" ", "_", $head->nama);
		$this->data->data = $data;
		$this->data->set  = $this->set;
		$this->layout = "excel";
	}
	
	public function Refresh()
	{
		$namaFile = $this->basePath.'assets/upload/'.$_GET['idRef'];
		$excel = new Excel;
		$data  = $excel->getReadExcel($namaFile);
		$baris = $data->rowcount(0);

		$det = array();
		for ($i=2; $i<=$baris; $i++) {
		// for ($i=($baris - 20); $i<=$baris; $i++) {
			$ii = 1;
			foreach($this->setDetail()->attr as $k => $r){
				$id = $r['attr'];
				$det[$i]->$id = trim($data->val($i, $ii));
				$ii++;
			}
		}
		
		if(count($det) > 0){
			$this->sessionDet(__CLASS__, $this->set, $det);
			echo "<script>$('#file_mex').val('".$_GET['idRef']."')</script>";
			echo "<script>_listLoadDetail('".__CLASS__."', '_totalAgs')</script>";
			echo "<script>$('#apankForm').empty()</script>";
		}
		
		$this->layout = "null";
	}
}