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

/*	
 *	Author 	: Wahyu
 *	Tanggal : 01-09-2014	
 *	Revisi 	: 
 /*
	 Author : Fachri
	 Tgl 	: 31/01/2015
 */ 
 

class Piutang_karyawan extends CI_Controller {
	// inisialisasi manual
	public $menu_id = 247;
	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_piutang_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 ="AE";
		
		$piutang_no = $this->m_piutang_karyawan->count_piutang_number($bulan, $cek_tahun, $BranchCode);
		if($piutang_no > 0){
			$ceknourut= $this->m_piutang_karyawan->get_last_piutang_number($BranchCode);
			foreach($ceknourut as $rows){
				$pecah = explode("/", $rows->EDebtNumber);
				$no_urut = $pecah[2]+1;
				$th_kode = $pecah[1];
				$BrhCode    = $pecah[0];
		
				$new_nourut = $no_urut;
				$cnim=strlen($new_nourut);
		
				if(empty($rows->EDebtNumber)){ 
				   $no="001"; 
				}
				 elseif($cnim=="1"){ 
				   $no="00"; 
				}
				 elseif($cnim=="2"){ 
				   $no="0"; 
				}
				 elseif($cnim=="3"){ 
				   $no=""; 
				}
			}
			$data['piutang_number'] = $BrhCode.'/'.$bulan.$tahun.$kode.'/'.$no.$new_nourut;
		} 
		 else {
			$no_urut ='001';
			$data['piutang_number'] = $BranchCode.'/'.$bulan.$tahun.$kode.'/'.$no_urut;
		}
		$data['piutang_karyawan']  		= $this->m_piutang_karyawan->create_piutang_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/piutang_karyawan/v_piutangkaryawan', $data);
		$this->load->view('v_footer');
	}
	
	public function get_piutang_karyawan_number() {
        $preg_date      = $this->input->post('Piutang_karyawan_date') == '' ? date('d-m-Y') : $this->input->post('Piutang_karyawan_date');
		
        $karyawan_number    = $this->m_piutang_karyawan->create_piutang_karyawan_number($preg_date);
        
//        print_r($data['sq_number1']);
        
        $result['status']   = true;
        $result['number']   = $karyawan_number;
        echo json_encode($result);
    }
	
	public function get_company_edit($branch){
		$branch_explode = str_replace('_','-', $branch);
		$list_company = $this->m_piutang_karyawan->get_company_edit($branch_explode);
		echo"$list_company";
	}
	
	public function get_akun_bank_edit($BranchCode){
		$branch_explode = str_replace('_','-', $BranchCode);
		$list_akun = $this->m_piutang_karyawan->get_akun_bank_edit($branch_explode);
		echo $list_akun;	
	}
	
	public function get_karyawan_edit($BranchCode){
		$branch_explode = str_replace('_','-', $BranchCode);
		$list_karyawan = $this->m_piutang_karyawan->get_karyawan_bycompany_edit($branch_explode);
		echo  $list_karyawan;
	}
	
	public function get_company(){
	    $CompanyCode = $this->session->userdata('company');
		$branch = $this->session->userdata('branch');
		$list_company = $this->m_piutang_karyawan->get_company($CompanyCode, $branch);
		echo"$list_company";
	}
	
	public function get_akun_bank($BranchCode){
		$list_akun = $this->m_piutang_karyawan->get_akun_bank($BranchCode);
		echo $list_akun;
		
	}
	
	public function get_karyawan($BranchCode){
	    $company_perusahaan 	= $this->session->userdata('company');
		$branch 	= $this->session->userdata('branch');
		$list_karyawan = $this->m_piutang_karyawan->get_karyawan_bycompany($branch, $company_perusahaan);
		echo  $list_karyawan;
	}
	
	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() {
        $company 	= $this->session->userdata('company');
		
        $bulan 		= date('m');
		$tahun 		= date('y');
		$cek_tahun 	= date('Y');
		$kode 		= "AE";
		$result_insert = 0;
		$com_trnas = $this->input->post('cmb_from_company');
		
		$txt_piutang_no = $this->input->post('txt_piutang_no');
		
		$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 = "";
			 $cheq_date 	= "NULL";
		}
		
		$data_piutang = array(
			'BranchCode' => $this->input->post('cmb_from_company'),
			'EDebtDate' => date("Y-m-d", strtotime($this->input->post('txt_payment_date'))),
			'EDebtNumber' => $txt_piutang_no,
			'CompTransCode' => $company,
			'BankTransfer' => $this->input->post('cmb_from_bank'),
			'EmplRecpCode' => $this->input->post('cmb_karyawan'),
			'BankReceipt' => $this->input->post('txt_rekening_karyawan'),
			'ChequeNumber' => $cheq_number,
			'ChequeDate' => $cheq_date,
			'EDebtDesc' => $this->input->post('txa_description'),
			'TransStatus' => 6,
			'EdebtIsLock' 	=> 1,
			'EDebtAmount' => $this->input->post('txt_amount'),
			'LastUser' => $this->session->userdata('username')
		);
		
		$this->db->trans_begin();
		$result_insert = $this->m_piutang_karyawan->insert_piutang_karyawan($data_piutang);
		if($result_insert == 0){ $this->proses_tutup(); }
		
		$CompanyCode = $this->session->userdata('company');
		$branch_code = $this->session->userdata('branch');
		$ExpCategoryID = $this->m_piutang_karyawan->get_expt_piutang($CompanyCode);
		$get_coa_piutang_karyawan = $this->m_piutang_karyawan->get_coa_piutang_karyawan($company, $branch_code);
		
		//$PiutangAccount = '1-3-04-004-00';
		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{
			$journal_debet = array(
				'EDebtDate' => date("Y-m-d", strtotime($this->input->post('txt_payment_date'))),
				'EDebtNumber' => $txt_piutang_no,
				'EDebtAccountNumber' => $get_coa_piutang_karyawan,
				'EDebtSaldoDebet'  => $this->input->post('txt_amount'),
				'EDebtSaldoCredit'  => 0,
				'EDebtDesc'  => $this->input->post('txa_description'),
				'EDebtSignStatus' => 0,
				'LastUser' => $this->session->userdata('username')
			);
	
			$result_insert = $this->m_piutang_karyawan->insert_journal_debit_piutang($journal_debet);
			if($result_insert == 0){ $this->proses_tutup(); }
			
			$journal_credit = array(
				'EDebtDate' => date("Y-m-d", strtotime($this->input->post('txt_payment_date'))),
				'EDebtNumber' => $txt_piutang_no,
				'EDebtAccountNumber' => $this->input->post('cmb_from_bank'),
				'EDebtSaldoDebet'  => 0,
				'EDebtSaldoCredit'  => $this->input->post('txt_amount'),
				'EDebtDesc'  => $this->input->post('txa_description'),
				'EDebtSignStatus' => 1,
				'LastUser' => $this->session->userdata('username')
			);
	
			$result_insert = $this->m_piutang_karyawan->insert_journal_credit_piutang($journal_credit);
			if($result_insert == 0){ $this->proses_tutup(); }
		}
		
		if($result_insert == 0) {
			$this->db->trans_rollback();
			$result['status'] = false;
			$result['message'] = get_notification('insert', $result_insert);
		}
		else {
			$this->db->trans_commit();
			$result['status'] = true;
			$result['message'] = get_notification('insert', $result_insert);
		}	
		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_piutang_karyawan->get_detail($id);

		// set for view
		$this->load->view('v_header');
		$this->load->view('cash_bank/piutang_karyawan/v_edit_piutangkaryawan', $data);
		$this->load->view('v_footer');
	}
	
	public function update(){
		$BranchCode 	= $this->session->userdata('branch');		
		$EDebtNumber = $this->input->post('txt_piutang_no');
		$result_insert = 0;
		$com_trans = $this->input->post('cmb_from_company');
		
		$id = $EDebtNumber;
		$piutang_no = $EDebtNumber;
		$cek_app_status_mgr = $this->m_piutang_karyawan->cek_status_app_mgr($piutang_no);
		$cek_app_status_dir = $this->m_piutang_karyawan->cek_status_app_dir($piutang_no);
		$cek_status = $this->m_piutang_karyawan->cek_trans_status($id);
		$chk_suspend = $this->input->post('chk_suspend');
		
		$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="";
			 $cheq_date="NULL";
		}
		$this->db->trans_begin();
		
		if($chk_suspend == ""){
			$chk_cancel = 6;
		}else{
			$chk_cancel = 3;
		}
		
		if(($cek_status == 0 || $cek_status == 4 || $cek_status == 3 || $cek_status == 5 || $cek_status == 6) && ($cek_app_status_mgr !=1) || ($cek_app_status_dir !=1)){	
			$data_piutang = array(
				//'BranchCode' => $BranchCode,
				'EDebtDate' => date("Y-m-d", strtotime($this->input->post('txt_payment_date'))),
				'EDebtNumber' => $EDebtNumber,
				//'CompTransCode' => $this->input->post('cmb_from_company'),
				'BankTransfer' => $this->input->post('cmb_from_bank'),
				'EmplRecpCode' => $this->input->post('cmb_karyawan'),
				'BankReceipt' => $this->input->post('txt_rekening_karyawan'),
				'ChequeNumber' => $cheq_number,
				'ChequeDate' => date("Y-m-d", strtotime($this->input->post('txt_cheque_date'))),
				'EDebtDesc' => $this->input->post('txa_description'),
				'EDebtAmount' => $this->input->post('txt_amount'),
				'TransStatus' => $chk_cancel,
				'EdebtIsLock' => 0,
				'LastUser' => $this->session->userdata('username')
			);
		} else {
			$data_piutang = array(
				//'BranchCode' => $BranchCode,
				'EDebtDate' => date("Y-m-d", strtotime($this->input->post('txt_payment_date'))),
				'EDebtNumber' => $EDebtNumber,
				//'CompTransCode' => $this->input->post('cmb_from_company'),
				'BankTransfer' => $this->input->post('cmb_from_bank'),
				'EmplRecpCode' => $this->input->post('cmb_karyawan'),
				'BankReceipt' => $this->input->post('txt_rekening_karyawan'),
				'ChequeNumber' => $cheq_number,
				'ChequeDate' =>date("Y-m-d", strtotime($this->input->post('txt_cheque_date'))),
				'EDebtDesc' => $this->input->post('txa_description'),
				'EDebtAmount' => $this->input->post('txt_amount'),
				'LastUser' => $this->session->userdata('username')
			);
		}
		
		$result_insert = $this->m_piutang_karyawan->update_piutang_karyawan($data_piutang, $EDebtNumber);
		if($result_insert == 0){ $this->proses_tutup(); }
		
        $CompanyCode 	= $this->session->userdata('company');
		$ExpCategoryID 	= $this->m_piutang_karyawan->get_expt_piutang($CompanyCode);
		$get_coa_piutang_karyawan = $this->m_piutang_karyawan->get_coa_piutang_karyawan_edit($com_trans);
		//$PiutangAccount = '1-3-04-004-00';
		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{
			$journal_debet = array(
				'EDebtDate' => date("Y-m-d", strtotime($this->input->post('txt_payment_date'))),
				'EDebtNumber' => $EDebtNumber,
				'EDebtAccountNumber' => $get_coa_piutang_karyawan,
				'EDebtSaldoDebet'  => $this->input->post('txt_amount'),
				'EDebtSaldoCredit'  => 0,
				'EDebtDesc'  => $this->input->post('txa_description'),
				'LastUser' => $this->session->userdata('username')
			);
	
			$result_insert = $this->m_piutang_karyawan->update_journal_debit_piutang($journal_debet, $EDebtNumber);
			if($result_insert == 0){ $this->proses_tutup(); }
			
			$journal_credit = array(
				'EDebtDate' => date("Y-m-d", strtotime($this->input->post('txt_payment_date'))),
				'EDebtNumber' => $EDebtNumber,
				'EDebtAccountNumber' => $this->input->post('cmb_from_bank'),
				'EDebtSaldoDebet'  => 0,
				'EDebtSaldoCredit'  => $this->input->post('txt_amount'),
				'EDebtDesc'  => $this->input->post('txa_description'),
				'LastUser' => $this->session->userdata('username')
			);
	
			$result_insert = $this->m_piutang_karyawan->update_journal_credit_piutang($journal_credit, $EDebtNumber);
			if($result_insert == 0){ $this->proses_tutup(); }
		}
		
		if($cek_status == 0  || $cek_status == 1 || $cek_status == 4 || $cek_status == 3 || $cek_status == 5 || $cek_status == 6 || $cek_status !=2){
			if($result_insert == 0) {
				$this->db->trans_rollback();
				$result['status'] = false;
				$result['message'] = get_notification('update', $result_insert);
			}
			else {
				$this->db->trans_commit();
				$result['status'] = true;
				$result['message'] = get_notification('update', $result_insert);
			}
			
			echo json_encode($result);
		} else {
			if($cek_app_status_mgr == 1 && $cek_app_status_dir == 1){
			    echo json_encode(array('status'=>false, 'message'=>'Piutang Karyawan tidak bisa diubah'));
			} else {
				$result['status'] = true;
				$result['message'] = get_notification_transaksi('Piutang Karyawan', 'update', 0);
				echo json_encode($result);
			}
		}
	}
	
	public function approval1() {
		if($this->session->userdata("jobtitle_status")==0){
			$piutang_no= $this->input->post('txt_piutang_no');
			$result_insert = 0;
			$cek_app_status_mgr = $this->m_piutang_karyawan->cek_status_app_mgr($piutang_no);
            if(!empty($cek_app_status_mgr)){
				$TransStatus = 1; 
			} else {
				$TransStatus = 4;
			}
			
			$data = array (		
				'AppLevel2Status' 		=> 1,
				'TransStatus'           => $TransStatus,
				'AppLevel2Code'         => $this->session->userdata("username"),
				'AppLevel2Date'			=> date("Y-m-d"),	
				'EdebtIsLock' 			=> 0,
				'LastUser' 				=> $this->session->userdata("username")
			);
			// transaction begin
			$this->db->trans_begin();
				$result_insert = $this->m_piutang_karyawan->approval_status($data,$piutang_no);
				if($result_insert == 0){ $this->proses_tutup(); }
				
				$app_finished = array('AppFinished' => 1);
				$cek_app = $this->m_piutang_karyawan->cek_app($piutang_no);
				
				if(!empty($cek_app)){
					$result_insert = $this->m_piutang_karyawan->status_finished($app_finished, $piutang_no);
					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);
		}
	}
	
	public function approval2() {
		if($this->session->userdata("jobtitle_status")==1){
			$piutang_no= $this->input->post('txt_piutang_no');
			$result_insert = 0;
			$cek_app_status_dir = $this->m_piutang_karyawan->cek_status_app_dir($piutang_no);
            if(!empty($cek_app_status_dir)){
				$TransStatus = 1; 
			} else {
				$TransStatus = 5;
			}
			
			$data = array (		
				'AppLevel1Status' 		=> 1,
				'TransStatus'           => $TransStatus,
				'AppLevel1Code'         => $this->session->userdata("username"),
				'AppLevel1Date'			=> date("Y-m-d"),	
				'EdebtIsLock' 			=> 0,
				'LastUser' 				=> $this->session->userdata("username")
			);
			// transaction begin
			$this->db->trans_begin();
				$result_insert = $this->m_piutang_karyawan->approval_status($data,$piutang_no);
				if($result_insert == 0){ $this->proses_tutup(); }
				
				$app_finished = array('AppFinished' => 1);
				$cek_app = $this->m_piutang_karyawan->cek_app($piutang_no);
				
				if(!empty($cek_app)){
					$result_insert = $this->m_piutang_karyawan->status_finished($app_finished, $piutang_no);
					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);
		}
	}
    
	public function to_journal(){
	    $CompanyCode = $this->session->userdata('company');
		$BranchCode 	= $this->session->userdata('branch');	
		
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);		
		
        $piutang_number = str_replace($data['menu_link'].'/to_journal/',"",$this->uri->uri_string());
		$pecah = explode("/", $piutang_number);
		$branch_explode = $pecah[0];
		
		$data['date_journal'] = $this->m_piutang_karyawan->get_date_journal_piutang($piutang_number);
		$data['list_jurnal']  = $this->m_piutang_karyawan->get_all_journal_piutang($piutang_number, $branch_explode);
		
		$this->load->view('v_header');
		$this->load->view('cash_bank/piutang_karyawan/v_print_journal_piutang_karyawan', $data);	
		$this->load->view('v_footer');	
	}
	
	public function suspend_data() {
		$id = $this->input->post('id');
		$hasil 				= 0;
		
		$data = array(
			'TransStatus'	=> 3,
			'LastUser'		=> $this->session->userdata('username')
		);
		
		$this->db->trans_begin();
		$hasil = $this->m_piutang_karyawan->suspend($data, $id);
		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
        $piutang_number = $this->input->post('id');
		$id = str_replace(".","/", $piutang_number);  
		//$result_insert = 0;
		$data = array (
			'TransStatus' => 3,
			'LastUser' => $this->session->userdata("username")
		);		
		//$this->db->trans_begin();
		//$result_insert = $this->m_piutang_karyawan->pembatalan_transaksi($id, $data);
		/*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);*/
			
		$cek_status = $this->m_piutang_karyawan->cek_trans_status($id);
		if($cek_status == 0 || $cek_status == 4 || $cek_status == 5 || $cek_status == 6){
			if($this->m_piutang_karyawan->pembatalan_transaksi($id, $data)){
				echo json_encode(array('status'=>true, 'message'=>'Piutang Karyawan berhasil dbatalkan'));
			} else {
				echo json_encode(array('status'=>false));
			}
		} else {
			echo json_encode(array('status'=>false, 'message'=>'Piutang Karyawan sudah tidak bisa dbatalkan'));
		}
	}
}