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

/*	
 *	Author 	: Wahyu
 *	Tanggal : 22-10-2014	
 *	Revisi 	: Fachri
 */ 

class swift_bank extends CI_Controller {
	// inisialisasi manual
	public $menu_id = 230;
	//public $menu_model = 'm_payment';
	
	// 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_swapbank');
        $this->load->model('m_expense_cashbon');	
		$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 ="SB";
		
		$swapbank_no = $this->m_swapbank->count_swapbank_number($bulan, $cek_tahun, $BranchCode);
		if($swapbank_no > 0){
			$ceknourut= $this->m_swapbank->get_last_swapbank_number($BranchCode);
			foreach($ceknourut as $rows){
				$pecah = explode("/", $rows->BSweepNumber);
				$no_urut = $pecah[2]+1;
				$th_kode = $pecah[1];
				$BrhCode    = $pecah[0];
		
				$new_nourut = $no_urut;
				$cnim=strlen($new_nourut);
		
				if(empty($rows->BSweepNumber)){ 
				   $no="001"; 
				}
				 elseif($cnim=="1"){ 
				   $no="00"; 
				}
				 elseif($cnim=="2"){ 
				   $no="0"; 
				}
				 elseif($cnim=="3"){ 
				   $no=""; 
				}
			}
			$data['swapbank_number'] = $BrhCode.'/'.$bulan.$tahun.$kode.'/'.$no.$new_nourut;
		} 
		 else {
			$no_urut ='001';
			$data['swapbank_number'] = $BranchCode.'/'.$bulan.$tahun.$kode.'/'.$no_urut;
		}
		$data['sweep_number']  		= $this->m_swapbank->create_sweep_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/swift_bank/v_new_swift_bank', $data);
		$this->load->view('v_footer');
	}
	
	public function get_akun_bank_cabang_add(){
	    $CompanyCode = $this->session->userdata('company');
		$list_bank =  $this->m_swapbank->get_coa_bank_cabang_add($CompanyCode);
		
		echo"$list_bank";
	}
	
	public function get_akun_bank_cabang($branch){
	    $CompanyCode = $this->session->userdata('company');
		$replace = str_replace('_', '-',$branch);
		$list_bank =  $this->m_swapbank->get_coa_bank_cabang($CompanyCode, $replace);
		
		echo"$list_bank";
	}
	
	public function get_akun_bank_add(){
	    $CompanyCode = $this->session->userdata('company');
		$list_bank =  $this->m_swapbank->get_coa_bank_add($CompanyCode);
		
		
		echo"$list_bank";
	}
	
	public function get_akun_bank($branch){
		$replace = str_replace('_', '-',$branch);
	    $CompanyCode = $this->session->userdata('company');
		$list_bank =  $this->m_swapbank->get_coa_bank($CompanyCode, $replace);
		
		
		echo"$list_bank";
	}
	
	public function get_swep_number() {
        $preg_date      = $this->input->post('Sweep_date') == '' ? date('d-m-Y') : $this->input->post('Sweep_date');
        $dep_number    = $this->m_swapbank->create_sweep_number($preg_date);
        
//        print_r($data['sq_number1']);
        
        $result['status']   = true;
        $result['number']   = $dep_number;
        echo json_encode($result);
    }
	
	public function proses_tutup(){
		$this->db->trans_rollback();
		die();
	}
	
	// Function Insert
	public function insert() {
        $BranchCode 	= $this->session->userdata('branch');
		$company 	= $this->session->userdata('company');
		
		$bulan 			= date('m');
		$tahun 			= date('y');
		$cek_tahun 		= date('Y');
		$kode 			= "SB";
		$result_insert 	= 0;
		
		
		
		$txt_date 			= date("Y-m-d", strtotime($this->input->post('txt_date')));	
		$cmb_bank_from		= $this->input->post('cmb_bank_from');
		$cmb_bank_to 		= $this->input->post('cmb_bank_to');
		$txa_memo 			= $this->input->post('txa_memo');
		$txt_amount 		= $this->input->post('txt_amount');
		$txt_swapbank_no 	= $this->input->post('txt_swapbank_no');
		
		$this->db->trans_begin();
		
		$get_branch_ho = $this->m_swapbank->get_company($company);
		$data_swepbank = array(
			'BranchCode' 	=> $BranchCode,
			'BSweepDate' 	=> $txt_date,
			'BSweepNumber' 	=> $txt_swapbank_no,
			'BankTransfer' 	=> $cmb_bank_from,
			'BankReceipt' 	=> $cmb_bank_to,
			'BSweepDesc' 	=> $txa_memo,
			'BSweepAmount' 	=> $txt_amount,
			'TransStatus' 	=> 6,
			'CompRecpCode' 	=> $get_branch_ho,
			'CompTransCode' => $BranchCode,
			'LastUser' 		=> $this->session->userdata('username')
		);
		
		//comp to
		$result_insert = $this->m_swapbank->insert_swapbank($data_swepbank);
		if($result_insert == 0){ $this->proses_tutup(); }

        $journal_debet = array(
			'BSweepDate' 			=> $txt_date,
			'BSweepNumber' 			=> $txt_swapbank_no,
			'BSweepAccountNumber' 	=> $cmb_bank_to,
			'BSweepSaldoDebet'  	=> $txt_amount,
			'BSweepSaldoCredit'  	=> 0,
			'BSweepDesc'  			=> $txa_memo,
			'BranchRecpCode'  		=> $get_branch_ho,
			'BSweepSignStatus' 		=> 0,
			//'BranchCode' 			=> $get_branch_ho,
			'LastUser' 				=> $this->session->userdata('username')
		);

		$result_insert = $this->m_swapbank->insert_journal_debit_swapbank($journal_debet);
		if($result_insert == 0){ $this->proses_tutup(); }
		
		$journal_credit = array(
			'BSweepDate' 			=> $txt_date,
			'BSweepNumber' 			=> $txt_swapbank_no,
			'BSweepAccountNumber' 	=> $cmb_bank_from,
			'BSweepSaldoDebet'  	=> 0,
			'BSweepSaldoCredit'  	=> $txt_amount,
			'BSweepDesc'  			=> $txa_memo,
			'BranchRecpCode'  		=> $BranchCode,
			'BSweepSignStatus' 		=> 1,
			//'BranchCode' 			=> $BranchCode,
			'LastUser' 				=> $this->session->userdata('username')
		);

		$result_insert = $this->m_swapbank->insert_journal_credit_swapbank($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());
		$potongan_id 		= explode("/", $id);
		$Var_gabungan 		= $potongan_id[0];
		$data['detail'] = $this->m_swapbank->get_detail($id, $Var_gabungan);

		// set for view
		$this->load->view('v_header');
		$this->load->view('cash_bank/swift_bank/v_edit_swift_bank', $data);
		$this->load->view('v_footer');
	}
	
	// Function Update
	public function update() {
        $BranchCode 		= $this->session->userdata('branch');		
		$txt_swapbank_no 	= $this->input->post('txt_swapbank_no');
		$txt_date 			= date("Y-m-d", strtotime($this->input->post('txt_date')));	
		$cmb_bank_from 		= $this->input->post('cmb_bank_from');
		$cmb_bank_to 		= $this->input->post('cmb_bank_to');
		$txa_memo 			= $this->input->post('txa_memo');
		$txt_amount 		= $this->input->post('txt_amount');
		$chk_suspend 		= $this->input->post('chk_suspend');
		
		$id 				= $txt_swapbank_no;
		$cek_app_mgr 		= $this->m_swapbank->cek_app_mgr($id);
		$cek_app_dir 		= $this->m_swapbank->cek_app_dir($id);
		$cek_status  		= $this->m_swapbank->cek_trans_status($id);
		$result_insert 		= 0;
		
		if($chk_suspend == ""){
			$chk_suspend = 6;
		}
		
		$this->db->trans_begin();
		if(($cek_status == 4 || $cek_status == 3 || $cek_status == 5 || $cek_status == 6) && ($cek_app_mgr !=1) || ($cek_app_dir !=1)){	
			$data_swapbank = array(
				'BranchCode' 	=> $BranchCode,
				'BSweepDate' 	=> $txt_date,
				'BSweepNumber' 	=> $txt_swapbank_no,
				'BankTransfer' 	=> $cmb_bank_from,
				'BankReceipt' 	=> $cmb_bank_to,
				'BSweepDesc' 	=> $txa_memo,
				'TransStatus' 	=> $chk_suspend,
				'BSweepAmount' 	=> $txt_amount,
				'LastUser' 		=> $this->session->userdata('username')
			);
		} else {
			$data_swapbank = array(
				'BranchCode' 	=> $BranchCode,
				'BSweepDate' 	=> $txt_date,
				'BSweepNumber' 	=> $txt_swapbank_no,
				'BankTransfer' 	=> $cmb_bank_from,
				'BankReceipt' 	=> $cmb_bank_to,
				'BSweepDesc' 	=> $txa_memo,
				'BSweepAmount' 	=> $txt_amount,
				'LastUser' 		=> $this->session->userdata('username')
			);
		}
		
		$result_insert = $this->m_swapbank->update_swapbank($data_swapbank, $txt_swapbank_no);
		if($result_insert == 0){ $this->proses_tutup(); }

        $journal_debet = array(
			'BSweepDate' 			=> $txt_date,
			'BSweepNumber' 			=> $txt_swapbank_no,
			'BSweepAccountNumber' 	=> $cmb_bank_to,
			'BSweepSaldoDebet'  	=> $txt_amount,
			'BSweepSaldoCredit'  	=> 0,
			'BSweepDesc'  			=> $txa_memo,
			'BranchRecpCode'  		=> $get_branch_ho,
			'LastUser' 				=> $this->session->userdata('username')
		);

		$result_insert = $this->m_swapbank->update_journal_debit_swapbank($journal_debet, $txt_swapbank_no);
		if($result_insert == 0){ $this->proses_tutup(); }
		
		$journal_credit = array(
			'BSweepDate'		 	=> $txt_date,
			'BSweepNumber' 			=> $txt_swapbank_no,
			'BSweepAccountNumber' 	=> $cmb_bank_from,
			'BSweepSaldoDebet'  	=> 0,
			'BSweepSaldoCredit'  	=> $txt_amount,
			'BSweepDesc'  			=> $txa_memo,
			'BranchRecpCode'  		=> $BranchCode,
			'LastUser' 				=> $this->session->userdata('username')
		);

		$result_insert = $this->m_swapbank->update_journal_credit_swapbank($journal_credit, $txt_swapbank_no);
		if($result_insert == 0){ $this->proses_tutup(); }
		
		if($cek_status == 0 || $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_transaksi('Swepp Bank', 'update', 0);
			}
			else {
				$this->db->trans_commit();
				$result['status'] = true;
				$result['message'] = get_notification_transaksi('Swepp Bank', 'update', 1);
			}
			
			echo json_encode($result);
		} else {
			if($cek_app_mgr == 1 && $cek_app_dir == 1){
			    echo json_encode(array('status'=>false, 'message'=>'Swepp Bank Berhasil Diubah dan Suspended tidak bisa dbatalkan'));
			} else {
				$result['status'] = true;
				$result['message'] = get_notification_transaksi('Swepp Bank', 'update', 0);
				echo json_encode($result);
			}
		}

	}
	
	public function suspend() {
		// inisialisasi additional parameter
        $swapbank_number 	= $this->input->post('id');
		$id 				= str_replace(".","/", $swapbank_number);  
		$result_insert 		= 0;
		$this->db->trans_begin();
		$data = array (
			'TransStatus' => 3,
			'LastUser' => $this->session->userdata("username")
		);	
			
		$result_insert = $this->m_swapbank->pembatalan_transaksi($id, $data);
		if($result_insert == 0){ $this->proses_tutup(); }
		
		if ($result_insert == 0) {
			$this->db->trans_rollback();
			$result_insert = 0;
		}
		else {
			$this->db->trans_commit();
			$result_insert = 1;
		}
		
		if($result_insert == 1){
			$result['status']	= true;
			$result['message']	= get_notification('suspend',$result_insert);
		}
		else{
			$result['status'] 	= false;
			$result['message']	= get_notification('suspend',$result_insert);
		}
		echo json_encode($result);
	}
	
	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_swapbank->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 to_journal(){
	    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
		 
	    $CompanyCode = $this->session->userdata('company');
		$BranchCode 	= $this->session->userdata('branch');	
		$sweepbank_number = str_replace($data['menu_link'].'/to_journal/',"",$this->uri->uri_string());			
		$potongan_id 		= explode("/", $sweepbank_number);
		$Branch_id 		= $potongan_id[0];
		
		$get_company_from_n_to		= $this->m_swapbank->get_branchCode($sweepbank_number);
		
		$data['date_journal'] = $this->m_swapbank->get_date_journal_sweppbank($sweepbank_number);
		
		$data['list_jurnal_trans']  = $this->m_swapbank->get_all_journal_sweppbank_trans($sweepbank_number, $get_company_from_n_to->CompTransCode);
		
		$data['list_jurnal']  = $this->m_swapbank->get_all_journal_sweppbank($sweepbank_number, $get_company_from_n_to->CompRecpCode);
		
		//$data['list_jurnal_trans']  = $this->m_swapbank->get_all_journal_sweppbank_trans($sweepbank_number, $get_company_from_n_to->CompTransCode);
		
		$this->load->view('v_header');
		if($get_company_from_n_to->CompTransCode == $get_company_from_n_to->CompRecpCode){
			$this->load->view('cash_bank/swift_bank/v_print_journal_swuft_journal-bank', $data);
		}
		else{
			$this->load->view('cash_bank/swift_bank/v_print_journal_swuft_journal', $data);	
		}
		
		$this->load->view('v_footer');	
	}
	
	public function approval1() {
		if($this->session->userdata("jobtitle_status")==0){
			$sw_number= $this->input->post('id');
			
            $cek_app_status_mgr = $this->m_swapbank->cek_status_app_mgr($sw_number);
            if(!empty($cek_app_status_mgr)){
				$TransStatus = 2; 
			} else {
				$TransStatus = 4;
			}
			
			$data = array (		
				'AppLevel2Status' 		=> 1,
				'TransStatus'           => $TransStatus,
				'AppLevel2Code'         => $this->session->userdata("username"),
				'AppLevel2Date'			=> date("Y-m-d"),	
				'LastUser' 				=> $this->session->userdata("username")
			);
			// transaction begin
			$this->db->trans_begin();
				$this->m_swapbank->approval_status($data,$sw_number);
				
				$app_finished = array('AppFinished' => 1);
				$cek_app = $this->m_swapbank->cek_app($sw_number);
				
				if(!empty($cek_app)){
					$this->m_swapbank->status_finished($app_finished, $sw_number);
				}
			
			if ($this->db->trans_status() === FALSE) {
				$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){
			$sw_number= $this->input->post('id');
			$cek_app_status_dir = $this->m_swapbank->cek_status_app_dir($sw_number);
            if(!empty($cek_app_status_dir)){
				$TransStatus = 2; 
			} else {
				$TransStatus = 5;
			}
			
			$data = array (		
				'AppLevel1Status' 		=> 1,
				'TransStatus'           => $TransStatus,
				'AppLevel1Code'         => $this->session->userdata("username"),
				'AppLevel1Date'			=> date("Y-m-d"),	
				'LastUser' 				=> $this->session->userdata("username")
			);
			// transaction begin
			$this->db->trans_begin();
				$this->m_swapbank->approval_status($data,$sw_number);
				
				$app_finished = array('AppFinished' => 1);
				$cek_app = $this->m_swapbank->cek_app($sw_number);
				
				if(!empty($cek_app)){
					$this->m_swapbank->status_finished($app_finished, $sw_number);
				}
			
			if ($this->db->trans_status() === FALSE) {
				$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);
		}
	}

}