<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Trans_uang_keluar extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('uang_keluar_model', 'uang_keluar');
		$this->gparam['privilage'] = getPrivilage($this->gparam['controller']);
		$this->gparam['restrict'] = 'You Cannot Access This Menu';
    }
	
	public function index()
	{
		
		if ($this->gparam['privilage']->privilage_view == 'Y') {
			
			$gridopt = $this->input->get(array('psize', 'pnumber'));
			header('Content-Type: text/html');
			
			$create_button = '""';			
			$update_button = '';
			$delete_button = '';		
			$preview = '';		
			
			if ($this->gparam['privilage']->privilage_create == 'Y') {
				//create button
				$create_button = '<a href="javascript:void(0)"  class="btn btn-primary btn-xs" onclick="create_view();" >
									<i class="fa fa-lg fa-fw fa-plus"></i>Create
								  </a>';
			}
			
			$cetakFaktur = "<a style=\"margin:4px;\" class=\"btn btn-success btn-xs\" onclick=\"cetakFaktur(\''+row.no+'\')\" href=\"javascript:void(0)\"\
								group=\"\" data-toggle=\"tooltip\" title=\"Update\"><i class=\"fa fa-print fa-lg\"></i>\
							  </a>";		
			
			/* if ($this->gparam['privilage']->privilage_update == 'Y') {
				$update_button = "<a class=\"l-btn l-btn-small l-btn-plain\" onclick=\"edit_view(\''+row.no+'\')\" href=\"javascript:void(0)\"\
									group=\"\" data-toggle=\"tooltip\" title=\"Update\"><i class=\"fa fa-pencil fa-lg\"></i>\
								  </a>";
			} 
			
			$preview = "<a class=\"l-btn l-btn-small l-btn-plain\" onclick=\"edit_view(\''+row.no+'\')\" href=\"javascript:void(0)\"\
									group=\"\" data-toggle=\"tooltip\" title=\"Update\"><i class=\"fa fa-camera-retro fa-lg\"></i>\
								  </a>";*/

			/*if ($this->gparam['privilage']->privilage_delete == 'Y') {	
				$delete_button = "<a class=\"l-btn l-btn-small l-btn-plain\" onclick=\"delete_data('+row.id+')\" href=\"javascript:void(0)\" group=\"\" data-toggle=\"tooltip\" title=\"Delete\">\
									<i class=\"fa fa-trash-o  fa-lg\"></i>\
								</a>"; 
			}*/		

			
			
			// pnumber, psize
			$data = array(
					'controller'  			=> $this->gparam['controller'],
					'psize'  				=> (empty($gridopt['psize'])?10:$gridopt['psize']),
					'pnumber'  				=> (empty($gridopt['pnumber'])?1:$gridopt['pnumber']),
					'create' 				=> $create_button,
					'cetakFaktur' 			=> $cetakFaktur
			);
			
			$this->load->view('uang_keluar_view',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}
	
	
	
	function form() {
		
		if ($this->gparam['privilage']->privilage_view == 'Y') {
			$data['controller'] = $this->gparam['controller'];				
			$data['action'] = base_url()."index.php/".$this->gparam['controller']."/crud";
			$no_trans = $this->uri->segment(3);	
			//echo $no_trans;
						
			if (!empty($no_trans)) {
				
				if ($this->gparam['privilage']->privilage_update == 'Y') {				
					
					//get value for edit
					/* $edit = $this->uang_keluar->get_value_edit($no_trans);					
					$t_trans = strtotime($edit->tanggaltransaksi);
					$no_transaksi = $edit->no;
					$data['faktur']  			= '<input id="no" type="text" name="no"  value="'.$no_transaksi.'" readonly />';
					$data['t_trans'] 			= date("d-m-Y",$t_trans);
					$data['diserahkan'] 		= $edit->diserahkan;
					$data['diterima'] 		= $edit->diterima;
					$data['header'] 			= 'Edit Uang Keluar';
					$data['submit'] 			= 'update';	 */
					
					//get trans detail
					/* $g_detail = $this->uang_keluar->get_trans_detail($no_trans);
					$trans_detail = '<div class="input_fields_wrap">
										<div class="row">
										<section class="col col-3">
											<label class="input" > 
												<a id ="add_button" class="btn btn-default" href="javascript:void(0);" style="height:30px; width:80px; margin:5px 0 0 0; ">
													<i class="fa fa-plus"></i>
														Add
												</a>									
											</label>
										</section>
									</div>';
					$i = 1; 
					foreach($g_detail as $detail_data) {
						$trans_detail .= '<div><div class="row">';
						
						$trans_detail .= '<section class="col col-2">
											<label class="label">Kode Deskripsi</label>
												<label class="select">	
													<select name="kodedesk[]" onchange="get_kddetail(this.value,'.$i.')" >
														<option value="">--Select--</option>';
						$ge_kddesk = $this->uang_keluar->get_kodedesk();
						
							foreach ($ge_kddesk as $va_kddesk) {
								if ($detail_data['kodedesk'] == $va_kddesk['kodedesk']) {
									$trans_detail .= '<option value = "'.$va_kddesk['kodedesk'].'" selected>'.$va_kddesk['deskripsi'].'</option>';
								} else {
									$trans_detail .= '<option value = "'.$va_kddesk['kodedesk'].'">'.$va_kddesk['deskripsi'].'</option>';
								}								
							}
							
						$trans_detail .= '</select><i></i></label></section>';
						
						if ($i == 1) {
							$trans_detail .= '<section class="col col-2">
											<div id="b_detail">
												<label class="label">Kode Detail</label>
													<label class="select">
														<select name="kodesub[]" >
															<option value="">--Select--</option>';
						} else {
							$trans_detail .= '<section class="col col-2">
											<div id="b_detail_'.$i.'">
												<label class="label">Kode Detail</label>
													<label class="select">
														<select name="kodesub[]" >
															<option value="">--Select--</option>';
						}
						
						$g_kdsub = $this->uang_keluar->get_kd_detail($detail_data['kodedesk']);
						foreach($g_kdsub as $v_kdsub) {
							if ($detail_data['kodesub'] == $v_kdsub['kodesub']) {
								$trans_detail .= '<option value = "'.$v_kdsub['kodesub'].'" selected>'.$v_kdsub['daftarlist'].'</option>';
							} else {
								$trans_detail .= '<option value = "'.$v_kdsub['kodesub'].'">'.$v_kdsub['daftarlist'].'</option>';
							}
						}	
						$trans_detail .= '</select><i></i></label></div></section>';
						
						$trans_detail	.= '<section class="col col-3">
												<label class="label">Besarnya</label>
													<label class="input">'; 
						$trans_detail   .= '<input type="text" name="besarnya[]"  value="'.$detail_data['besarnya'].'"/>';									
						$trans_detail .= '</label></section>';
						
						if ($i != 1) {
							$trans_detail .= '<a href="#" class="remove_field"><i class="fa fa-lg fa-fw fa-times" style="padding-top:10px;"></i></a>';
						}
						$trans_detail .= '</div></div>';
						$i++;
					}					
					$trans_detail .= '<div">';
					
					$ge_kddesk = $this->uang_keluar->get_kodedesk();
					$kd_desk = '';
			
					foreach ($ge_kddesk as $va_kddesk) {
						$kd_desk .= '<option value = "'.$va_kddesk['kodedesk'].'">'.$va_kddesk['deskripsi'].'</option>';
					}
					
					$data['num'] = $i+1;
					$data['kd_desk'] = $kd_desk;
					$data['trans_detail'] = $trans_detail;*/
					echo $this->gparam['restrict']; die();
					
				} else {
					echo $this->gparam['restrict']; die();
				}
				
			} else {
				if ($this->gparam['privilage']->privilage_create == 'Y') {	
								
					$ge_kddesk = $this->uang_keluar->get_kodedesk();
					$kd_desk = '';
			
					foreach ($ge_kddesk as $va_kddesk) {
						$kd_desk .= '<option value = "'.$va_kddesk['kodedesk'].'">'.$va_kddesk['deskripsi'].'</option>';
					}
					
					$last_id = $this->uang_keluar->get_last_seq();
					
					$last_id = $last_id + 1;
					$length = strlen($last_id);
					
					if ($length == 1) {
						$num = '0000';
					} else if ($length == 2){
						$num = '000';
					} else if ($length == 3) {
						$num = '00';
					}else if ($length == 4) {
						$num = '0';
					
					} else {
						$num = '';
					}
					$data['num'] = 1;
					$data['last_id'] = $last_id;
					$data['faktur']  = '<input id="no" type="text" name="no"  value="FK'.date('Ymd').$num.$last_id.'" readonly />'; 
					$data['kd_desk'] = $kd_desk;	
					$data['header']  = 'Add Uang Keluar';
					$data['submit']  = 'create';					
					
					$data['trans_detail'] = '
						<div class="input_fields_wrap">
					<div class="row">
						<section class="col col-3">
							<label class="input" > 
								<a id ="add_button" class="btn btn-primary" href="javascript:void(0);" style="height:30px; width:80px; margin:5px 0 0 0; padding-top:5px; ">
									<i class="fa fa-plus"></i>
										Add
								</a>									
							</label>
						</section>
					</div>
					
					
					<div class="row">
						
						<section class="col col-2">
							<label class="label">Kode Deskripsi</label>
							<label class="select">	
								<select id="kode_desk" name="kodedesk[]" onchange="get_kddetail(this.value,1)" >
									<option value="">--Select--</option>
									'.$kd_desk.'									
								</select>
								<i></i>		
							</label>
						</section>
						
						<section class="col col-2">
							<div id="b_detail">
								<label class="label">Kode Detail</label>
								<label class="select">
									<select id="kode_sub" name="kodesub[]" >
										<option value="">--Select--</option>											
									</select>
									<i></i>		
								</label>
							</div>
						</section>
						
						<section class="col col-3">
							<label class="label">Besarnya</label>
							<label class="input"> 
									<input id="besarnya" type="text" name="besarnya[]"  value=""/>									
							</label>
						</section>
					</div>
					
				</div>';
					
				} else {
					echo $this->gparam['restrict']; die();
				}	
			}				
			$this->load->view('crud_uang_keluar',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}

	
	function get_kd_detail() {
	
		$kd_desk = $this->input->post('kd_desk');
		$get = $this->uang_keluar->get_kd_detail($kd_desk);
		
		$select  = '<label class="label">Kode Detail</label>';
		$select .= '<label class="select">
						<select name="kodesub[]">
							<option value="">--Select--</option>';
		foreach ($get as $value) {
			$select .= '<option value="'.$value['kodesub'].'">'.$value['daftarlist'].'</option>';
		}		
		$select .='</select>';
		$select .='<i></i>';
		$select .='</label>';
		
		echo $select;
		
	}
	
	function get_kd_detail_secondRow() {
		
		$kd_desk = $this->input->post('kd_desk');
		$get = $this->uang_keluar->get_kd_detail($kd_desk);
		
		$select = '<label class="select">
						<select name="kodesub[]">
							<option value="">--Select--</option>';
		foreach ($get as $value) {
			$select .= '<option value="'.$value['kodesub'].'">'.$value['daftarlist'].'</option>';
		}		
		$select .='</select>';
		$select .='<i></i>';
		$select .='</label>';
		
		echo $select;
		
	}
	
	public function crud() {
		
		$crud = $this->input->post('submit');
		if ($crud == 'create') {
			$id_fak = $this->uang_keluar->next_faktur();
			$insert = $this->uang_keluar->insert_data($id_fak);
			if ($insert) {
				//$faktur = $this->input->post("no");
				$this->session->set_flashdata('faktur', $id_fak);				
				$json = true;
			}
			
		} else if ($crud == 'update') {
			echo $this->gparam['restrict']; die();
			/* $update = $this->uang_keluar->update_data();
			if ($update) {
				$json = true;			
			} */
				
		}
		
		echo json_encode($json);
		
	}
	
	/* public function delete() {
		
		$delete = $this->uang_keluar->delete_data();
		if ($delete) {
			$json = true;
		}
		echo json_encode($json);	
	} */
	
	function load_data() {
		
		header('Content-Type: application/jsonp');
        $list = $this->uang_keluar->get_list_data();		
        echo json_encode($list);
	}
	
	function printFaktur() {
	
		define('FPDF_FONTPATH',$this->config->item('fonts_path'));
		$faktur = $_GET['faktur'];
		$get = $this->uang_keluar->getTransaksi($faktur);
		$total = $this->uang_keluar->getTotal($faktur);
		$diserahkan  = $this->uang_keluar->getDiserahkan($faktur);
		$diterima  	= $this->uang_keluar->getDiterima($faktur);
		//update faktur ke cetak
		$upStatus = $this->uang_keluar->updateStatusFaktur($faktur);
		
		$data = array(
			'detail' 	 => $get, 
			'total'	 	 => $total,
			'diserahkan' => $diserahkan,
			'diterima' => $diterima,
			'faktur'	 => $faktur
		);
		
		$this->load->view("printUangKeluar",$data);
	}
	
	function successPage() {
		$this->load->view("success_page");
	}
	
	
}
