<?php

CLASS mmaterial extends RasioProject
{
	protected $set;
	protected $enableCk = true;
	protected $jenis = 11;

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

	protected function set()
	{
		$this->set->em 			= $this->em;
		$this->set->modul 		= 'Master Inventori';
		$this->set->dBaseH 		= 'inv';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('kd_inv');
		$this->set->where 		= array("jenis = '".$this->jenis."'");
		$this->set->dSort 		= 'asc';
		$this->set->form 		= $this->setForm();
		$this->set->clearForm 	= array();
		$this->set->notNull 	= array("kd_inv", "nm_inv");
		$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 		= 'mInv';
		
		
		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" => 'kd_inv', "size" => 20)));
		$form[] = array('row' => 25, 'col' => 25, "label" => array("name" => "Nama", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nm_inv', "size" => 30)));
        
		$form[] = array('row' => 50, 'col' => 25, "label" => array("name" => "Keterangan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket', "size" => 30)));
		//$form[] = array('row' => 50, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_skat', "size" => 30, 'readonly' => true)));
		//$form[] = array('row' => 50, 'col' => 25, "label" => array("name" => "Sub Kategori", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_skat', "size" => 10, 'after' => 'select("")', 'select' => 'skat', 'select_width' => 280, 'select_height' => 245, 'select_label' => "Kode=>50;Name=>200", 'select_target' => "name=>nm_skat", 'select_warning' => true, 'display' => 'selectDisplay')));

		//$form[] = array('row' => 75, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub', "size" => 30, 'readonly' => true)));
		//$form[] = array('row' => 75, 'col' => 25, "label" => array("name" => "Supplier", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub', "size" => 10, 'after' => 'load("master-supp", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_sub=>nm_sub')));

		//$form[] = array('row' => 100, 'col' => 25, "label" => array("name" => "Qty Min", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'qty_min', "size" => 4, "class" => "decimal")));
		//$form[] = array('row' => 125, 'col' => 25, "label" => array("name" => "Disc. Max", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'diskon_max', "size" => 4, "class" => "decimal")));
		
		//$form[] = array('row' => 125, 'col' => 200, "label" => array("name" => "Stok", "width" => 30), "input" => array("type" => 'text',	'attr' => array("name" => 'qty', "size" => 4, "class" => "decimal")));
		// $form[] = array('row' => 25, 'col' => 500, "label" => array("name" => "Etikel", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'etikel', "size" => 10)));
		//$form[] = array('row' => 0, 'col' => 500, "label" => array("name" => "Harga Beli", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'harga_beli', "size" => 10, "class" => "decimal")));
		//$form[] = array('row' => 25, 'col' => 500, "label" => array("name" => "Harga Jual 1", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'harga_jual', "size" => 10, "class" => "decimal")));
		//$form[] = array('row' => 50, 'col' => 500, "label" => array("name" => "Harga Jual 2", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'harga1', "size" => 10, "class" => "decimal")));
		//$form[] = array('row' => 75, 'col' => 500, "label" => array("name" => "Harga Jual 3", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'harga2', "size" => 10, "class" => "decimal")));
		//$form[] = array('row' => 100, 'col' => 500, "label" => array("name" => "Harga Grosir", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'harga_grs', "size" => 10, "class" => "decimal")));
		//$form[] = array('row' => 125, 'col' => 500, "label" => array("name" => "Harga Grosir 2", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'harga_grs1', "size" => 10, "class" => "decimal")));
		//$form[] = array('row' => 150, 'col' => 500, "label" => array("name" => "Harga Grosir 3", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'harga_grs2', "size" => 10, "class" => "decimal")));
		//$form[] = array('row' => 175, 'col' => 500, "label" => array("name" => "Diskon Max", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'diskon_max', "size" => 4, "class" => "decimal")));


		$form[] = array('row' => 100, 'col' => 25, "label" => array("name" => "By", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chusr', 'size' => 15, 'readonly' => true)));
		$form[] = array('row' => 100, '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 	= 400;
		$res->height	= 150;
		$res->col		= 25;
		$res->row		= 165;
		$res->data		= 'satuan';
		$res->pk		= 'kd_inv';
		$res->sort		= 'kdsat';
		
		$res->attr[] = array("label" => 'Satuan',	"attr" => 'nmsat', 'width' => 50);
		$res->attr[] = array("label" => 'Skala',	"attr" => 'skala',	'width' => 40, 'class' => 'decimal');
		$res->attr[] = array("label" => 'Sat. Beli', "attr" => 'defb',	'width' => 50, 'after' => 'select("")', "select" => 'tf');
		$res->attr[] = array("label" => 'Sat. Jual', "attr" => 'defj',	'width' => 50, 'after' => 'select("")', "select" => 'tf');
		$res->attr[] = array("label" => 'Sat. Stok', "attr" => 'defs',	'width' => 50, 'after' => 'select("")', "select" => 'tf');
		$res->attr[] = array("label" => 'H.Beli',	"attr" => 'hargab',	'width' => 60, 'class' => 'decimal');
		$res->attr[] = array("label" => 'H.Jual',	"attr" => 'hargaj',	'width' => 60, '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" => 'Sub Kategori', 'attr' => 'a.nm_skat', "sort" => 'true', "width" => 100, 'align' => 'left');
		$res->attr[]	= array("label" => 'Kode', 'attr' => 'a.kd_inv', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'Nama', 'attr' => 'a.nm_inv', "sort" => 'true', "width" => 200, 'align' => 'left');
		$res->attr[]	= array("label" => 'H.Jual 1', 'attr' => 'a.harga_jual', "sort" => 'true', "width" => 80, 'align' => 'right', 'type' => 'number');
		$res->attr[]	= array("label" => 'H.Jual 2', 'attr' => 'a.harga1', "sort" => 'true', "width" => 80, 'align' => 'right', 'type' => 'number');
		$res->attr[]	= array("label" => 'H.Jual 3', 'attr' => 'a.harga2', "sort" => 'true', "width" => 80, 'align' => 'right', 'type' => 'number');
		$res->attr[]	= array("label" => 'Stok', 'attr' => 'a.qty', "sort" => 'true', "width" => 80, 'align' => 'right', 'type' => 'number');
		$res->attr[]	= array("label" => 'Keterangan', 'attr' => 'a.ket', "sort" => 'true', "width" => 100, 'align' => 'left');
		
		return $res;
	}
	
	protected function setPrint()
	{
		$kode = new Kode;
		$res->sortA = $kode->getPk($this->set->dBasePk);
		$res->sortT = 'asc';
		$res->width = '900px';
		
		$res->attr[]	= array("label" => 'Kd.Kat', 'attr' => 'a.kd_kat', "sort" => 'true', "width" => 80, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Nm.Kat', 'attr' => 'a.nm_kat', "sort" => 'true', 'width' => 200, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Kode', 'attr' => 'a.kd_inv', "sort" => 'true', "width" => 80, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Nama', 'attr' => 'a.nm_inv', "sort" => 'true', 'width' => 200, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Keterangan', 'attr' => 'a.ket', "sort" => 'true', '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);
	}

	protected function kode($post)
	{
		$kode  = new Kode;
		$basic = new Basic;
		$this->set->prefik = $_POST['kd_skat'].'-';
		return $basic->getAutoNumber($this->set, 3);
	}
	
	public function delDet($kode)
	{
		$sql = $this->em->addDelete('satuan');
		$sql = $this->em->getWhere("kd_inv = '$kode'");
		return $sql;
	}
	
	public function saveDet($id, $post, $nom)
	{
		$kode = new Kode;
		$attr = array();
		foreach($this->set->detail->attr as $k => $r){
			$no = $r['attr'];
			$attr[$no] = $post[$no][$id];
		}
		$attr['kdsat'] = $post['kd_inv'].'.'.$kode->getRight($nom, 2, 0);
		$attr['kd_inv'] = $post['kd_inv'];
		$attr['chtime'] = date("Y-m-d H:i:s");
		$attr['chusr'] = $this->login['kd_user'];
		
		$sql = $this->em->addInsert($this->set->detail->data);
		$sql = $this->em->addAttrIns($attr);

		return $sql;
	}
	
	public function proses()
	{
		$basic = new Basic;
		$validasi = $this->validasi();
		if($validasi == true){
			$tambah = array();
			$tambah['jenis'] = $this->jenis;
			$qb = array();
			
			// echo "<pre>";
			// var_dump($tambah);
			// var_dump($qb);
			
			$basic->proses($_POST, $this->set, $tambah, $qb);
		}
		$this->layout = "null";
	}
	
	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 disDefLoad()
	{
		$data = $this->em->getFind("inv", "nm_inv as nama", array("kd_inv = '".$_GET['idRef']."'"));
		
		$res->target = $_GET['target'];
		$res->nama 	= $data->nama;
		echo json_encode($res);
		$this->layout = "null";
	}
	
	public function popUp()
	{
		$this->set->where 	= array("(jenis = '".$this->jenis."' or jenis = '212')");
		$basic = new Basic;
		$this->data = $basic->data($_GET, $this->set);
		$this->view = "default/displayDataPopUp";
		$this->layout = "view";
	}
	
	public function loadDisplay()
	{
		$pk = $this->set->dBasePk[0];
		$data = $this->em->getFind($this->set->dBaseH, '*', array("$pk = '".$_GET['idRef']."'"));
		
		$basic = new Basic;
		$res = $basic->loadDisplay($data, $_GET);
		echo json_encode($res);
		$this->layout = "null";
	}
}