<?php

CLASS fpja extends RasioProject
{
	protected $set;
	protected $enableCk = false;
	protected $report;
	protected $baseTitle;

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

	protected function set()
	{
		$this->report       = new Report;
		$this->set->em      = $this->em;
		$this->set->kode    = 'ACC-001';
		$this->set->date    = date("d/m/Y H:i");
		$this->set->modul     = 'Input Faktur Pajak All';
		$this->set->form    = $this->setForm();
		$this->set->display   = $this->setReport();
		$this->set->filter    = $this->report->setFilter($_GET, $this->setForm());
		$this->set->query     = $this->setQuery();
		$this->set->notNull   = array("date1", "date2", "paper");
		$this->set->button    = array("Cetak");
		$this->set->groupDis  = array();
		$this->set->groupBy   = "nojnl";
		$this->set->hitung    = array("debet", "credit", "saldo");
		$this->set->rowPage   = 500;
		$this->baseTitle      = $this->set->modul;
		return $this->set;
	}

	protected function defValue()
	{
		$lap = new Lap;
		$res->cabang  = '';
		return $res; 
	}

	protected function setForm()
	{
		$this->defVal = $this->defValue();

		$date	= new Date;
		$akhir 	= $date->getPenjumlahanTanggal(date("Y-m-01"), 1, 0, 0);
		$akhir 	= $date->getPenguranganTanggal($akhir, 0, 1, 0);
		
		$row = 0;
		$form[] = array('row' =>  $row,  'col' => 25,   "label" => array("name" => "Tgl. Nota",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'date1', "size" => 10, 'class' => 'tanggal', 'value' => date("01/m/Y"))));
		$form[] = array('row' =>  $row,  'col' => 260, "label" => array("name" => "s/d",     "width" => 30),   "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'date2', "size" => 10, 'class' => 'tanggal', 'value' => date("d/m/Y", strtotime($akhir)))));

		$row = $row + 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Profit Center", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_cabang', "size" => 9, 'after' => 'load("cabang", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_cabang=>nm_cabang')));
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_cabang', "size" => 30, 'readonly' => true)));
		
		$row = $row + 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Status", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'model', "size" => 20, 'after' => 'select("")', 'select' => 'status_fp')));
		
		return $form;
	}

	protected function setReport()
	{
		$res[] = array('id' => '_cek',		'nama' => '<input type="checkbox" class="_cekAll">',  		'type' => 'text',   'width' => 30, 'set' => '_cekAnalisa', 'input' => true);
		$res[] = array('id' => 'no',		'nama' => 'No',  		'type' => 'no',   'width' => 30);
		$res[] = array('id' => 'tgl',		'nama' => 'Tanggal',		'type' => 'date',   'width' => 60,  'size' => 20, 'noduplicate' => true);
		$res[] = array('id' => 'nonjl',		'nama' => 'No.Invoice', 'type' => 'text',   'width' => 130,  'size' => 20);
		$res[] = array('id' => 'nm_sub',	'nama' => 'Customer','type' => 'text',   'width' => 280,  'size' => 30);
		$res[] = array('id' => 'dpp',		'nama' => 'DPP',		'type' => 'numeric','width' => 80,  'size' => 20);
		$res[] = array('id' => 'ppn',		'nama' => 'PPN',		'type' => 'numeric','width' => 80,  'size' => 20);
		$res[] = array('id' => 'no_fp',		'nama' => 'Nomor FP','type' => 'text', 		'width' => 190, 'set' => '_nofp', 'input' => true, 'induk' => 'Faktur Pajak');
		$res[] = array('id' => 'tgl_fp',	'nama' => 'Tanggal',   	'type' => 'text', 	'width' => 80, 'set' => '_tglfp', 'input' => true, 'induk' => 'Faktur Pajak');
		// $res[] = array('id' => 'std',		'nama' => 'STD/ SDHN',   'type' => 'text', 	'width' => 50, 'set' => '_stdfp', 'input' => true, 'induk' => 'Faktur Pajak');


		return $res;
	}

	public function setQuery()
	{
		$filter = $this->set->filter;
		
		$sql = $this->em->addSelect("a.*
									, to_char(a.tgl, 'dd/mm/yyyy') as tgl
									, to_char(a.tgl_fp, 'dd/mm/yyyy') as tgl_fp
									, a.nonjl as no_invoice
									, a.ppn_nilai as ppn
									, a.gtot - a.ppn_nilai as dpp
									");
		$sql = $this->em->getFrom("njl", 'a');
		$sql = $this->em->addLeftJoin("sub", 'b', array("a.kd_sub = b.kd_sub"));
		$sql = $this->em->getWhere("a.control = '1'");
		$sql = $this->em->andWhere("a.ppn_nilai > 0");
		if(!empty($filter->date1->value)){
			$sql = $this->em->andWhere("a.tgl >= '".date("Y-m-d 00:00:00", strtotime($filter->date1->value))."'");
		}
		if(!empty($filter->date2->value)){
			$sql = $this->em->andWhere("a.tgl <= '".date("Y-m-d 23:59:59", strtotime($filter->date2->value))."'");
		}
		if(!empty($filter->kd_cabang->value)){
			$sql = $this->em->andWhere("a.kd_cabang = '".$filter->kd_cabang->value."'");
		}
		if(strtoupper($filter->model->value) == 'BELUM ADA NO.FP'){
			$sql = $this->em->andWhere("a.tgl_fp is null");
		}
		if(strtoupper($filter->model->value) == 'SUDAH ADA NO.FP'){
			$sql = $this->em->andWhere("a.tgl_fp is not null");
		}
		$sql = $this->em->getMultiSort("a.tgl, a.nonjl");
		return $sql;
	}

	public function Index()
	{
		$this->data = $this->set;
		$this->view = "default/report";
	}

	public function View()
	{
		
		$before = '<script src="'.$this->baseUrl.'assets/js/jquery-1.7.2.min.js" type="text/javascript"></script>';
		$before = '<script src="'.$this->baseUrl.'assets/js/jquery-ui-1.8.19.custom.min.js" type="text/javascript"></script>';
		$before.= '<script src="'.$this->baseUrl.'assets/js/apank.js" type="text/javascript"></script>';
		$before.= '<script src="'.$this->baseUrl.'assets/js/jquery.meio.mask.js" type="text/javascript"></script>';
		$before.= '<script src="'.$this->baseUrl.'assets/js/accounting.js" type="text/javascript"></script>';
		$before.= '<form method="POST" action="'.$this->baseUrl.'fpja/simpan">';
		
		$url = $this->baseUrl.$_SERVER['QUERY_STRING'];
		$url = str_ireplace("ref=", "", $url);
		
		$after = '
			<p style="text-align: right; padding-right: 5px;"><input type="submit" value="Simpan"></p>
			<input type="hidden" name="link" value="'.$url.'">
			</form>
			<div style="position: fixed; bottom: 0; text-align: center; font-size: 11px; background-color: #fff">
				No. : <input type="text" id="nom" size="4" class="decimal _inputTahan"> 
				sd <input type="text" id="nos" size="4" class="decimal _inputTahan"> 
				Prefik : <input type="text" id="prefik" size="25" class=" _inputTahan"> 
				Nomor : <input type="text" id="nomor" size="8" class=" _inputTahan"> 
				Tgl.FP : <input type="text" id="tanggal" size="10" id="tanggal" class="tanggal _inputTahan"> 
				<input type="button" id="_genFp" value="Urut FP">
			</div>
			<script type="text/javascript">
			$(document).ready(function(){
				setDec();
				
				disAct = function(_id){
					_cek = $("#"+_id).attr("checked");
					if(_cek === "checked"){
						$("#no_"+_id).removeAttr("disabled");
						$("#tgl_"+_id).removeAttr("disabled");
						$("#pr_"+_id).removeAttr("disabled");
						$("#std_"+_id).removeAttr("disabled");
					}else{
						$("#no_"+_id).attr("disabled", true).val("");
						$("#tgl_"+_id).attr("disabled", true).val("");
						$("#pr_"+_id).attr("disabled", true).val("");
					}
				}
				
				$("._cekAll").click(function(){
					_cek = $(this).attr("checked");
					if(_cek === "checked"){
						$("._cek").attr("checked", true);
					}else{
						$("._cek").removeAttr("checked");
					}
					$("._cek").after(function(){ disAct(this.id); })
				})
				$("._cek").click(function(){
					disAct(this.id);
				})
				$("._input").keypress(function(e){
					if (e.which == 13) {
						return false;
					}	
				})
				
				_cekId = function(_no){
					_id = $(".ck_"+_no).attr("checked");
					if(_id === "checked"){
						return true;
					}
					return false;
				}
				
				getRight = function(_no, _vl, _ln){
					_jun = _no.toString();
					_jun = parseInt(_jun.length);
					_jum = _ln - _jun;
					_n = "";
					for(_iLp = 1; _iLp <= _jum; _iLp++){
					  _n+= _vl;
					}
					_n+= _no;
					return _n;
				}
				
				$("#_genFp").click(function(){
					_aw = Number($("#nom").val());
					_ak = Number($("#nos").val());
					_ln = $("#nomor").val();
					_ln = parseInt(_ln.length);
					_no = parseInt($("#nomor").val());
					for(_i = _aw; _i <= _ak; _i++){
						if(_cekId(_i) === true){
							_noD = getRight(_no, "0", _ln);
							$(".std_"+_i).val($("#std").val());
							$(".tg_"+_i).val($("#tanggal").val());
							$(".pr_"+_i).val($("#prefik").val()+_noD);
							_no = _no + 1;
						}
					}
				})
				
				$("._copyFp").click(function(){
					_val = $(this).val();
					if(_val != ""){
						_kir = _val.substring(0, 11);
						_kan = _val.substring(11, 25);
						_kan = parseInt(_kan) + 1;
						$("#prefik").val(_kir);
						$("#nomor").val(_kan);
					}
				})
			})
			</script>
		';
		
		$this->set->before = $before;
		$this->set->after = $after;
		
		$this->data = $this->report->getDef($this->set);
		$this->layout = "report";
	}

	public function Simpan()
	{
		if(empty($_POST['cek'])){
			echo "<script>alert('Proses Gagal, tidak ada data yang dicentang!')</script>";
			echo "<script>window.location = '".$_POST['link']."'</script>";
		}else{
			$date = new Date;
			$qb = array();
			foreach($_POST['cek'] as $k => $r){

				$fp = $_POST['no_fp'][$k];
				if(!empty($fp)){
					$attr = array();
					$attr['no_fp'] 	= strtoupper($_POST['prefik'][$k].$fp);
					$attr['tgl_fp'] = $date->serialDate($_POST['tgl_fp'][$k]);
					$attr['user_fp'] = $this->login['kd_user'];
					$attr['jam_fp'] = date("Y-m-d H:i:s");

					$sql = $this->em->addUpdate("njl");
					$sql = $this->em->addAttrUp($attr);
					$sql = $this->em->getWhere("nonjl = '$k'");
					$qb[] = $sql;
				}else{
					$attr = array();
					$attr['no_fp'] 	= 'null';
					$attr['tgl_fp'] = 'null';
					$attr['user_fp'] = $this->login['kd_user'];
					$attr['jam_fp'] = date("Y-m-d H:i:s");

					$sql = $this->em->addUpdate("njl");
					$sql = $this->em->addAttrUp($attr);
					$sql = $this->em->getWhere("nonjl = '$k'");
					$qb[] = $sql;
				}
				
			}
			
			// echo "<pre>";
			// var_dump($_POST);
			// var_dump($qb);
			// var_dump($_POST['link']);
			
			$save = $this->em->getFlush($qb);
			if($save != false){
				if($save != false){
					echo "<script>window.location = '".$_POST['link']."'</script>";
				}else{
					echo "<script>alert('Proses Gagal!')</script>";
				}
			}else{
				echo "<script>alert('Proses Gagal!')</script>";
			}
		}
		
		$this->layout = "null";
	}
}