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

/*	
 *	Author 	: Wahyu
 *	Tanggal : 01-09-2014	
 *	Revisi 	: 
 */ 

class payment_karyawan extends CI_Controller {
	// inisialisasi manual
	public $menu_id = 248;
	public $menu_model = '';
	
	// inisialisasi otomatis
	public $add = "add";
	public $edit = "edit";
	public $delete = "delete";
	public $to_preview = "to_preview";
	public $to_pdf = "to_pdf";
	public $to_excel = "to_excel";
	
	public function __construct() {
        parent::__construct();
		$this->load->model('m_menu');
		$this->load->model('m_payment_karyawan');
		$this->load->helper('message');	
        $this->load->helper('convert_date');	
		date_default_timezone_set('Asia/Jakarta');
	}	
	
	// Function Index	
	public function index() {
		// pengecekan otoritas
		/* if($this->session->userdata("checking") == "") {
			redirect(base_url() . 'custom/cek/'.$this->menu_id, 'refresh');	
		} */
		
		// reset session 
		$this->session->unset_userdata('checking');
		
		// get link from tbl_menu 
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
		
		// set data
		$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);
		
		$CompanyCode = $this->session->userdata('company');
		$BranchCode 	= $this->session->userdata('branch');
		
		$bulan = date('m');
		$tahun = date('y');
		$cek_tahun = date('Y');
		$kode ="EI";
		
		$payment_no = $this->m_payment_karyawan->count_payment_number($bulan, $cek_tahun, $BranchCode);
		
		if($payment_no > 0){
			$ceknourut= $this->m_payment_karyawan->get_last_payment_number($BranchCode);
			foreach($ceknourut as $rows){
				$pecah = explode("/", $rows->EInsNumber);
				$no_urut = $pecah[2]+1;
				$th_kode = $pecah[1];
				$BrhCode    = $pecah[0];
		
				$new_nourut = $no_urut;
				$cnim=strlen($new_nourut);
		
				if(empty($rows->EInsNumber)){ 
				   $no="001"; 
				}
				 elseif($cnim=="1"){ 
				   $no="00"; 
				}
				 elseif($cnim=="2"){ 
				   $no="0"; 
				}
				 elseif($cnim=="3"){ 
				   $no=""; 
				}
			}
			$data['payment_number'] = $BrhCode.'/'.$bulan.$tahun.$kode.'/'.$no.$new_nourut;
		} 
		 else {
			$no_urut ='001';
			$data['payment_number'] = $BranchCode.'/'.$bulan.$tahun.$kode.'/'.$no_urut;
		}
		$data['payment_karyawan']  		= $this->m_payment_karyawan->create_payment_karyawan_number(date('d-m-Y'));
		$data['date_now']  		= date('d-m-Y');
		
		// set for view
		$this->load->view('v_header');
		$this->load->view('cash_bank/payment_karyawan/v_paymentkaryawan', $data);
		$this->load->view('v_footer');
	}
	
	public function get_payment_karyawan_number() {
        $preg_date      = $this->input->post('Payment_karyawan_date') == '' ? date('d-m-Y') : $this->input->post('Payment_karyawan_date');
		
        $pay_karyawan_number    = $this->m_payment_karyawan->create_payment_karyawan_number($preg_date);
        
//        print_r($data['sq_number1']);
        
        $result['status']   = true;
        $result['number']   = $pay_karyawan_number;
        echo json_encode($result);
    }
	
	public function get_akun_bank_edit($BranchCode){
		$branch_explode = str_replace('_','-', $BranchCode);
		$list_akun = $this->m_payment_karyawan->get_akun_bank_edit($branch_explode);
		echo $list_akun;	
	}
	
	public function get_piutang(){
	    $BranchCode   = $this->session->userdata('branch');
		$list_piutang = $this->m_payment_karyawan->get_piutang($BranchCode);
		
		foreach($list_piutang as $row) {
		    $EDebtNumber  	= $row->EDebtNumber;
			$edeb_amoun 	=  $row->EDebtAmount;
		    $outstanding  = $this->m_payment_karyawan->get_sisa_piutang($EDebtNumber);
			
			
			if(!empty($outstanding)){
				$total_piutang = $outstanding->total_piutang;
				$BalanceAmount = $edeb_amoun-$total_piutang;
			}
			else{
				$BalanceAmount = 0;
			}
			
			if(empty($outstanding)){
				$outstanding = $outstanding;
			}
			else {
				$outstanding = $row->EDebtAmount;
			}
			
			
			if($row->EdebtIsLock == 1){
				$IsLock = "Locked";
			}
			else{
				$IsLock = "Un Locked";
			}
			
			$hasil[] = array(
				'EDebtAmount' => $row->EDebtAmount,
				'BranchName' => $row->BranchName,
				'BranchCode' => $row->BranchCode,
				//'CompTransCode' => $row->CompTransCode,
				'AccountName' => $row->AccountName,
				'EDebtNumber' => $row->EDebtNumber,
				'EmployeeName' => $row->EmployeeName,
				'EmplRecpCode' => $row->EmplRecpCode,
				'EdebtIsLock' => $IsLock,
				'outstanding' => $BalanceAmount
				
			); 
		}
		
		if(!empty($hasil)){
			echo json_encode($hasil);
		}
		else{
			$notFound = array();
			echo json_encode($notFound);
		}
	}
	
	/* public function no_payment(){
		$piutang_no = $this->input->post('piutang_no');
		$no_payment_before = $this->m_payment_karyawan->get_no_payment_before($piutang_no);
		echo"$no_payment_before";
	} */
	
	public function get_company_edit($branch){
		$branch_explode = str_replace('_','-', $branch);
		$list_company = $this->m_payment_karyawan->get_company_edit($branch_explode);
		echo"$list_company";
	}
	
	public function get_payment_periode($payment_num){
		
	    $uri4 = $this->uri->segment(4);
        $uri5 = $this->uri->segment(5);
        $uri6 = $this->uri->segment(6);
		$id = $uri4."/".$uri5."/".$uri6;
		$uri7 = $this->uri->segment(7);
		
		$list_payment = $this->m_payment_karyawan->get_payment_periode($id, $uri7);
		echo"$list_payment";   
	}
	
	public function get_payment_periode_cancel($payment_num){
	    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
	    $payment_before = str_replace($data['menu_link'].'/get_payment_periode_cancel/',"",$this->uri->uri_string());
		
	    $list_payment = $this->m_payment_karyawan->get_payment_periode_cancel($payment_before);
		
		echo"$list_payment";   
	}
	
	public function get_history_payment($piutang_no){
	    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
	    $piutang_num = str_replace($data['menu_link'].'/get_history_payment/',"",$this->uri->uri_string());
	    $result = $this->m_payment_karyawan->get_history_payment($piutang_num);
		foreach($result as $row) {
			$hasil[] = array(
				'LoanPayment' => $row->LoanPayment,
				'PaymentPeriod' => $row->PaymentPeriod,
				'PaymentPerPeriod' => $row->PaymentPerPeriod,
				'PaymentBalance' => $row->PaymentBalance,
				'EInsNumber' => $row->EInsNumber
				
			); 
		}
		
		if(!empty($hasil)){
			echo json_encode($hasil);
		}
		else{
			$notFound = array();
			echo json_encode($notFound);
		}
	}
	
	public function proses_tutup(){
		$this->db->trans_rollback();
		$hasil = 0;
		$result['status'] 	= false;
        $result['message']	= get_notification('proses_tutup',$hasil);
		echo json_encode($result);
		die();
	}
	
	public function insert() {
        $BranchCode 	= $this->session->userdata('branch');
		
		$bulan = date('m');
		$tahun = date('y');
		$cek_tahun = date('Y');
		$kode ="EI";
		$result_insert = 0;
		
		
		$EDebtNumber = $this->input->post('txt_piutang_no');
		$EInsNumber = $this->input->post('txt_payment_no');
		/*$detail 	= $this->input->post('detail');
		$detail 	= json_decode($detail);*/
		
		$checque_no = $this->input->post('txt_cheque_no');
		if(!empty($checque_no)){
		     $cheq_number = $this->input->post('txt_cheque_no');
			 $cheq_date   = date("Y-m-d", strtotime($this->input->post('txt_cheque_date')));
		} else {
			 $cheq_number="NULL";
			 $cheq_date="NULL";
		}
		
		$data_islock_status = array(
			'EdebtIsLock' => 1
		);
		$result_insert = $this->m_payment_karyawan->update_is_lock($data_islock_status, $EDebtNumber);
		if($result_insert == 0){ $this->proses_tutup(); }
			
		$ExpCategoryID = $this->m_payment_karyawan->get_expt_payment();
		//$PaymentAccount = '1-3-04-004-00';
		$com_rec = $this->input->post('cmb_to_company');
		$company = $this->session->userdata('company');
		$branch_code = $this->session->userdata('branch');
		//harus ceking disini
		$get_coa_piutang_karyawan = $this->m_payment_karyawan->get_coa_piutang_karyawan($company, $branch_code);
		
		$data_payment = array(
			'BranchCode' => $BranchCode,
			'EInsDate' => date("Y-m-d", strtotime($this->input->post('txt_payment_date'))),
			'EInsNumber' => $EInsNumber,
			'EDebtNumber' => $this->input->post('txt_piutang_no'),
			'EmplTransCode' => $this->input->post('txt_karyawan'),
			'BankTransfer' => $get_coa_piutang_karyawan,
			'CompRecpCode' => $company,
			'BankReceipt' => $this->input->post('cmb_to_bank'),
			'CqTermStatus' => 1,
			'ChequeNumber' => $cheq_number,
			'ChequeDate' => $cheq_date,
			'EInsDesc' => $this->input->post('txa_description'),
			'TransStatus' => 4,
			'EInsAmount' => $this->input->post('txt_amount'),
			'LastUser' => $this->session->userdata('username')
		);
		
		$this->db->trans_begin();
		
		if($get_coa_piutang_karyawan == ""){
			$this->db->trans_rollback();
			$result['status']   = false;
			$result['message']  = 'Transaksi tidak bisa terbentuk karena COA piutang karyawan belum tersedia';
	
			echo json_encode($result);
			die;
		}
		else{
			$EDebtNumber = $this->input->post('txt_piutang_no');
			$result_insert = $this->m_payment_karyawan->insert_payment_karyawan($data_payment);
			if($result_insert == 0){ $this->proses_tutup(); }
			
			$total_piutang = $this->input->post('txt_total_piutang');
			$dibayar = $this->input->post('txt_amount');
			$total_payment_period = $this->m_payment_karyawan->get_total_payment_period($EDebtNumber);
			$period = $this->m_payment_karyawan->get_max_period($EDebtNumber);
			
			$cek_payment = $this->m_payment_karyawan->cek_payment_data($EDebtNumber);
			
			if(!empty($cek_payment)){
				$Payment1 = ($total_piutang - $total_payment_period);
				$PaymentBalance = $Payment1 - $dibayar;
				$periode = $period + 1;
			} else {
				$PaymentBalance = $total_piutang - $dibayar;
				$periode =1;
			}
			
			/*if($PaymentBalance == 0){
				$EDebtNumber = $this->input->post('txt_piutang_no');
				$status_piutang = array('TransStatus' => 2);
				$result_insert = $this->m_payment_karyawan->update_status_piutang($EDebtNumber, $status_piutang);
				if($result_insert == 0){ $this->proses_tutup(); }
			}*/
			 
			/*if($detail){
				foreach($detail as $row_2){
					echo $row_2->PaymentPerPeriod;
					die();
				}
			}*/
			
			$detail_payment = array(
				'EInsNumber' => $EInsNumber,
				'EDebtNumber' => $EDebtNumber,
				'LoanPayment' => $total_piutang,
				'PaymentPeriod' => $periode,
				'PaymentPerPeriod' => $dibayar,
				'PaymentBalance' => $PaymentBalance,
				'LastUser' => $this->session->userdata('username')			
			);
			
			$balance = $this->m_payment_karyawan->get_payment_balance($EDebtNumber);
			
			
			if($period > 1){		
				if(($balance != 0 OR $balance == NULL) AND ($dibayar <= $balance)){
					$result_insert = $this->m_payment_karyawan->insert_detail_payment_karyawan($detail_payment);
					if($result_insert == 0){ $this->proses_tutup(); }
				} 
			} else {
					$result_insert = $this->m_payment_karyawan->insert_detail_payment_karyawan($detail_payment);
					if($result_insert == 0){ $this->proses_tutup(); }
			}
	
			$journal_debet = array(
				'EInsDate' => date("Y-m-d", strtotime($this->input->post('txt_payment_date'))),
				'EInsNumber' => $EInsNumber,
				'EInsAccountNumber' => $this->input->post('cmb_to_bank'),
				'EInsSaldoDebet'  => $this->input->post('txt_amount'),
				'EInsSaldoCredit'  => 0,
				'EInsDesc'  => $this->input->post('txa_description'),
				'EInsSignStatus' => 0,
				'LastUser' => $this->session->userdata('username')
			);
	
			if($period > 1){
				if(($balance != 0 OR $balance == NULL) AND ($dibayar <= $balance)){
					$result_insert = $this->m_payment_karyawan->insert_journal_debit_payment($journal_debet);
					if($result_insert == 0){ $this->proses_tutup(); }
				}
			} else {
					$result_insert = $this->m_payment_karyawan->insert_journal_debit_payment($journal_debet);
					if($result_insert == 0){ $this->proses_tutup(); }
			}
			
			$journal_credit = array(
				'EInsDate' => date("Y-m-d", strtotime($this->input->post('txt_payment_date'))),
				'EInsNumber' => $EInsNumber,
				'EInsAccountNumber' => $get_coa_piutang_karyawan,
				'EInsSaldoDebet'  => 0,
				'EInsSaldoCredit'  => $this->input->post('txt_amount'),
				'EInsDesc'  => $this->input->post('txa_description'),
				'EInsSignStatus' => 1,
				'LastUser' => $this->session->userdata('username')
			);
			
			if($period > 1){
				if(($balance != 0 OR $balance == NULL) AND ($dibayar <= $balance)){
					$result_insert = $this->m_payment_karyawan->insert_journal_credit_payment($journal_credit);
					if($result_insert == 0){ $this->proses_tutup(); }
				}
			} else {
					$result_insert = $this->m_payment_karyawan->insert_journal_credit_payment($journal_credit);
					if($result_insert == 0){ $this->proses_tutup(); }
			}
		}
		if($balance != ""){
			if($dibayar > $balance){
				$this->db->trans_rollback();
				$result['status']   = false;
				$result['message'] ='Payment Melebihi Sisa Piutang, Silahkan periksa kembali';	
				echo json_encode($result);
				die;
			}
			else {
		
				if($result_insert == 0) {
					$this->db->trans_rollback();
					$result['status'] = false;
					$result['message'] = get_notification_transaksi('Payment Karyawan', 'insert', $result_insert);
				}
				else {
					$this->db->trans_commit();
					$result['status'] = true;
					if($balance !=0 OR $balance == NULL){
						$result['message'] = get_notification_transaksi('Payment Karyawan', 'insert', $result_insert);
					} 
					else {
						$result['message'] = get_notification_transaksi('Payment Karyawan Sudah Lunas');
					}
				}
				
			}		
	
			echo json_encode($result);
		}
		else{
			if($result_insert == 0) {
					$this->db->trans_rollback();
					$result['status'] = false;
					$result['message'] = get_notification_transaksi('Payment Karyawan', 'insert', $result_insert);
				}
				else {
					$this->db->trans_commit();
					$result['status'] = true;
					if($balance !=0 OR $balance == NULL){
						$result['message'] = get_notification_transaksi('Payment Karyawan', 'insert', $result_insert);
					} 
					else {
						$result['message'] = get_notification_transaksi('Payment Karyawan Sudah Lunas');
					}
				}
				echo json_encode($result);
		}
	}
	
	public function edit(){
		// get link from tbl_menu 
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);		
		// set data
		$CompanyCode = $this->session->userdata('company');
		$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);

		// inisialisasi additional parameter
        $id 				= str_replace($data['menu_link'].'/edit/',"",$this->uri->uri_string());
		
		$data['detail'] 	= $this->m_payment_karyawan->get_detail($id);
		$data['piutang_no'] = $this->m_payment_karyawan->get_piutang_no_detail($id);
		$data['total_piutang_terakhir'] = $this->m_payment_karyawan->get_total_piutang_terakhir($id);
		$data['balance'] = $this->m_payment_karyawan->get_payment_balance($id);
     
		// set for view
		$this->load->view('v_header');
		$this->load->view('cash_bank/payment_karyawan/v_edit_paymentkaryawan', $data);
		$this->load->view('v_footer');
	}
	
	public function update() {
		$txt_payment_for		= $this->input->post('txt_payment_for');
		$txt_payment_no			= $this->input->post('txt_payment_no');
		$cmb_to_bank			= $this->input->post('cmb_to_bank');
		$txt_karyawan			= $this->input->post('txt_karyawan');
		$txt_payment_date		= $this->input->post('txt_payment_date');
		$txa_description		= $this->input->post('txa_description');
		$txt_amount				= $this->input->post('txt_amount');
		$result_update 			= 0;
		$pay_period				= $this->input->post('pay_period');
		$pay_balance			= $this->input->post('pay_balance');
		
		$detail 				= $this->input->post('detail');
		$detail 				= json_decode($detail);
		$company 				= $this->session->userdata('company');
		$branch_code 			= $this->session->userdata('branch');
		//harus ceking disini
		$get_coa_piutang_karyawan = $this->m_payment_karyawan->get_coa_piutang_karyawan($company, $branch_code);
		
		
		$data_group = array(
			'BankReceipt' 	=> $cmb_to_bank,
			'EInsDesc' 		=> $txa_description,
			'EInsAmount' 	=> $txt_amount,
			'EInsDate' 		=> date('Y-m-d', strtotime($txt_payment_date))
		);
		
		$this->db->trans_begin();
		$result_update = $this->m_payment_karyawan->update_group($data_group, $txt_payment_no);
		if($result_update == 0){ $this->proses_tutup(); }
		
		if($detail){
			foreach($detail as $row_2){
				$LoanPayment 		= $row_2->LoanPayment;
				$PaymentPeriod 		= $row_2->PaymentPeriod;
				$PaymentPerPeriod 	= $row_2->PaymentPerPeriod;
				$PaymentBalance 	= $row_2->PaymentBalance;
				$EInsDetailID 		= $row_2->EInsDetailID;
				
				$detail_data = array(
					'PaymentPerPeriod' => $pay_period,
					'PaymentBalance'   => $pay_balance			
				);
				
				$result_update = $this->m_payment_karyawan->update_detail($detail_data, $txt_payment_no);
				if($result_update == 0){ $this->proses_tutup(); }
			}
		}
		
		$journal_debet = array(
			'EInsDate' 			=> date("Y-m-d", strtotime($this->input->post('txt_payment_date'))),
			'EInsNumber' 		=> $txt_payment_no,
			'EInsAccountNumber' => $this->input->post('cmb_to_bank'),
			'EInsSaldoDebet'  	=> $pay_period,
			'EInsSaldoCredit'  	=> 0,
			//'EInsDesc'  		=> $this->input->post('txa_description'),
			'LastUser' 			=> $this->session->userdata('username')
		);
		$result_update = $this->m_payment_karyawan->update_payment_jurnal_debet($journal_debet, $txt_payment_no);
		if($result_update == 0){ $this->proses_tutup(); }	
			
		$journal_credit = array(
			'EInsDate' 			=> date("Y-m-d", strtotime($this->input->post('txt_payment_date'))),
			'EInsNumber' 		=> $txt_payment_no,
			'EInsAccountNumber' => $get_coa_piutang_karyawan,
			'EInsSaldoDebet'  	=> 0,
			'EInsSaldoCredit'  	=> $pay_period,
			//'EInsDesc'  		=> $this->input->post('txa_description'),
			'LastUser' 			=> $this->session->userdata('username')
		);
		$result_update = $this->m_payment_karyawan->update_payment_jurnal_credit($journal_credit, $txt_payment_no);
		if($result_update == 0){ $this->proses_tutup(); }
				
		if ($result_update == 0) {
			$this->db->trans_rollback();
			$result['status'] 	= false;
			$result['message']	= get_notification('update',$result_update);
		}
		else {
			$this->db->trans_commit();
			$result['status'] 	= true;
			$result['message']	= get_notification('update',$result_update);
		}
		echo json_encode($result);
	}
	
	public function to_journal($id){
	    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
		
	    $CompanyCode = $this->session->userdata('company');
		$BranchCode 	= $this->session->userdata('branch');	
		$payment_number = str_replace($data['menu_link'].'/to_journal/',"",$this->uri->uri_string());
		$explode_branch = explode('/', $payment_number);
		$branch_explode = $explode_branch[0];
		
		$data['date_journal'] = $this->m_payment_karyawan->get_date_journal_payment($payment_number);
		$data['list_jurnal']  = $this->m_payment_karyawan->get_all_journal_payment($payment_number, $branch_explode);
		
		$this->load->view('v_header');
		$this->load->view('cash_bank/payment_karyawan/v_print_journal_payment_karyawan', $data);	
		$this->load->view('v_footer');	
	}
	
	public function suspend_data() {
		$id = $this->input->post('id');
		$payment_no = str_replace(".","/", $id);  
		$hasil 				= 0;
		
		$data = array(
			'TransStatus'	=> 3,
			'LastUser'		=> $this->session->userdata('username')
		);
		
		$this->db->trans_begin();
		$data_islock_status = array(
			'EdebtIsLock' => 0
		);
		
		$this->db->trans_begin();
		$hasil = $this->m_payment_karyawan->update_is_lock($data_islock_status, $this->m_payment_karyawan->get_adebt_number($id));
		if($hasil == 0){ $this->proses_tutup(); }
		
		$hasil = $this->m_payment_karyawan->update_trans_status($payment_no, $data);
		if($hasil == 0){ $this->proses_tutup(); }
		
		if ($hasil == 0) {
			$this->db->trans_rollback();
			$hasil = 0;
		}
		else {
			$this->db->trans_commit();
			$hasil = 1;
		}
		
		if($hasil == 1){
			$result['status']	= true;
			$result['message']	= get_notification('suspend',$hasil);
		}
		else{
			$result['status'] 	= false;
			$result['message']	= get_notification('suspend',$hasil);
		}
		echo json_encode($result);
	}
	
	public function suspend() {
		// inisialisasi additional parameter
        $id = $this->input->post('id');
		$payment_no = str_replace(".","/", $id);  
		
		$cek_approval = $this->m_payment_karyawan->cek_approval_payment($payment_no);
		
		if($cek_approval != 1){
			$transtat_new = 3;
		} 
		 else {
			$transtat_new = 2;
		}
		
		$data = array (
			'TransStatus' => $transtat_new,
			'LastUser' => $this->session->userdata("username")
		);		
		
		$this->db->trans_begin();
					
		$data_islock_status = array(
			'EdebtIsLock' => 0
		);
		
		$this->db->trans_begin();
		$result_insert = $this->m_payment_karyawan->update_is_lock($data_islock_status, $this->m_payment_karyawan->get_adebt_number($id));
		if($result_insert == 0){ $this->proses_tutup(); }
		
		$result_insert = $this->m_payment_karyawan->update_trans_status($payment_no, $data);
		if($result_insert == 0){ $this->proses_tutup(); }
		
		if($result_insert == 0) {
			$this->db->trans_rollback();
			$result['status'] = false;
			$result['message'] = get_notification('Payment Karyawan', 'suspend', 1);
		}
		else {
			$this->db->trans_commit();
			$result['status'] = true;
			$result['message'] = get_notification('Payment Karyawan', 'suspend', 0);
		}
			
		if($result_insert){
		    if($cek_approval != 1){
			    echo json_encode(array('status'=>true, 'message'=>'Payment karyawan dibatalkan'));
			} else {
				echo json_encode(array('status'=>false, 'message'=>'Payment karyawan sudah diapprov'));
			}
		} else {
			echo json_encode(array('status'=>false));
		}
	}
	
	public function approval1() {
		if($this->session->userdata("jobtitle_status")==1){
			$pay_number = $this->input->post('txt_payment_no');
			$EDebtNumber = $this->input->post('txt_payment_for');
			$pay_balance = $this->input->post('pay_balance');
			
			$result_insert =0;				
			$data = array (		
				'AppFinished'	 		=> 1,
				'AppLevel1Status' 		=> 1,
				'TransStatus'           => 2,
				'AppLevel1Code'         => $this->session->userdata("username"),
				'AppLevel1Date'			=> date("Y-m-d"),	
				'LastUser' 				=> $this->session->userdata("username")
			);
			
			if($pay_balance == 0){
				$status_piutang = array('TransStatus' => 2);
				$result_insert = $this->m_payment_karyawan->update_status_piutang($EDebtNumber, $status_piutang);
				if($result_insert == 0){ $this->proses_tutup(); }
			}
			
			$data_islock_status = array(
				'EdebtIsLock' => 0
			);
			// transaction begin
			$this->db->trans_begin();
		
			$result_insert = $this->m_payment_karyawan->update_is_lock($data_islock_status, $EDebtNumber);
			if($result_insert == 0){ $this->proses_tutup(); }
			
			$result_insert = $this->m_payment_karyawan->approval_status($data,$pay_number);
			if($result_insert == 0){ $this->proses_tutup(); }
			
			if ($result_insert == 0) {
				$this->db->trans_rollback();
				$result['status'] = false;
				$result['message'] = 'Approval Gaggal';
			}
			else {
				$this->db->trans_commit();
				$result['status'] = true;
				$result['message'] = 'Approval Berhasil';
			}		
			echo json_encode($result);
		}
	}
	
}