<?php

CLASS mprom extends RasioProject
{
	protected $set;
	protected $enableCk = true;
	protected $jenis = 37;

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

	protected function set()
	{
		$this->set->em 			= $this->em;
		$this->set->modul 		= 'Master Promo';
		$this->set->dBaseH 		= 'promo';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('noprm');
		$this->set->where 		= array();
		$this->set->dSort 		= 'asc';
		$this->set->form 		= $this->setForm();
		$this->set->clearForm 	= array();
		$this->set->notNull 	= array("nilai", "tgl", "tgl_jt", "nama");
		$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;
		
		if($_GET['jso'] == 'undefined'){
			$this->set->where 	= array("tgl <= '".date("Y-m-d")."'", "tgl_jt >= '".date("Y-m-d 23:59:59")."'");
		}
		
		return $this->set;
	}
	
	protected function setForm()
	{
		$form[] = array('row' => 25, 'col' => 25, "label" => array("name" => "No.Promo", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'noprm', "size" => 15, 'readonly' => true)));

		$form[] = array('row' => 50, 'col' => 25, "label" => array("name" => "Nama Promo", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nama', "size" => 30)));
		$form[] = array('row' => 75, 'col' => 25, "label" => array("name" => "Nilai Promo", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nilai', "size" => 10, 'class' => 'decimal')));
		
		$form[] = array('row' => 100, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub', "size" => 30, 'readonly' => true, 'child' => 'prv')));
		$form[] = array('row' => 100, 'col' => 25, "label" => array("name" => "Supplier", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub', "size" => 9, 'after' => 'load("mastersupp", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_sub=>nm_sub')));

		$form[] = array('row' => 25, 'col' => 500, "label" => array("name" => "Mulai", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'tgl', "size" => 10, 'class' => 'tanggal')));
		$form[] = array('row' => 50, 'col' => 500, "label" => array("name" => "Sampai", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'tgl_jt', "size" => 10, 'class' => 'tanggal')));
		
		$row = 50;
		for($i = 1; $i <= 5; $i++){
			$row = $row + 25;
			$form[] = array('row' => $row, 'col' => 500, "label" => array("name" => "Diskon ".$i, "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'dis'.$i, "size" => 4, 'after' => 'select("")', "select" => 'tf')));
		}
		

		$form[] = array('row' => 125, 'col' => 25, "label" => array("name" => "Keterangan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket', "size" => 30)));
		
		$form[] = array('row' => 200, 'col' => 25, "label" => array("name" => "By", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chusr', 'size' => 15, 'readonly' => true)));
		$form[] = array('row' => 225, 'col' => 25, "label" => array("name" => "Last", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chtime', 'size' => 20, 'readonly' => true, 'mode' => 'datetime')));
		return $form;
	}
	
	protected function setGrid()
	{
		$kode = new Kode;
		$res->page 	= 50;
		$res->sortA = $kode->getPk($this->set->dBasePk);
		$res->sortT = 'asc';
		
		$res->attr[]	= array("label" => 'Nomor', 'attr' => 'a.noprm', "sort" => 'true', "width" => 120, 'align' => 'left');
		$res->attr[]	= array("label" => 'Mulai', 'attr' => 'a.tgl', "sort" => 'true', "width" => 70, 'align' => 'center', 'type' => 'date');
		$res->attr[]	= array("label" => 'Selesai', 'attr' => 'a.tgl_jt', "sort" => 'true', "width" => 70, 'align' => 'center', 'type' => 'date');
		$res->attr[]	= array("label" => 'Nama Promo', 'attr' => 'a.nama', "sort" => 'true', "width" => 200, 'align' => 'left');
		$res->attr[]	= array("label" => 'Supplier', 'attr' => 'anm_sub', "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" => 'Kode', 'attr' => 'a.kd_kat', "sort" => 'true', "width" => 70, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Nama', 'attr' => 'a.nm_kat', "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;
		$prefik = 'PRM.'.date("ym", strtotime($post['tgl'])).'.';
		$lastU  = $basic->getLastNum($this->set->dBaseH, 'noprm', $prefik, '', 5, $this->set);
		return $prefik.$kode->getRight($lastU, 5, 0);
		
	}
	
	public function proses()
	{
		$date  = new Date;
		$basic = new Basic;
		$validasi = $this->validasi();
		if($validasi == true){
			
			$_POST['tgl'] 		= $date->serialDate($_POST['tgl']);
			$_POST['tgl_jt'] 	= $date->serialDate($_POST['tgl_jt']);
			
			if($_POST['_prosesAction'] == 'simpan'){
				$_POST['noprm'] = $this->kode($_POST);
			}
			
			$tambah = array();
			$tambah['control'] = 1;
			$basic->proses($_POST, $this->set, $tambah);
		}
		$this->layout = "null";
	}
	
	public function Delete()
	{
		$basic = new Basic;
		$this->data = $basic->delete2($_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("kat", "nm_kat", array("kd_kat = '".$_GET['idRef']."'"));
		
		$res->target = $_GET['target'];
		$res->nama 	= $data->nm_kat;
		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 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";
	}
	
	public function Select()
	{
		$data = $this->em->getFindBy($this->set->dBaseH, "kd_kat as kode, CONCAT(kd_kat, '___', nm_kat) as nama", array(), "kd_kat");
		
		$res->jum 	= count($data);
		$res->data 	= $data;
		$res->div 	= strtoupper($_GET['idDiv']);
		
		echo json_encode($res);
		$this->layout = "null";
	}
}