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

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

class Deposit extends CI_Controller {
	// inisialisasi manual
	public $menu_id = 190;
	
	// 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_deposit');
        $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);
		$CompanyCode = $this->session->userdata('company');
		$BranchCode 	= $this->session->userdata('branch');	
		
		// set data
		$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);		
		
		$bulan = date('m');
		$tahun = date('y');
		$cek_tahun = date('Y');
		$kode ="CD";
		
		$deposit_no = $this->m_deposit->count_deposit_number($bulan, $cek_tahun, $BranchCode);
		if($deposit_no > 0){
			$ceknourut= $this->m_deposit->get_last_deposit_number($BranchCode);
			foreach($ceknourut as $rows){
				$pecah = explode("/", $rows->CDepNumber);
				$no_urut = $pecah[2]+1;
				$th_kode = $pecah[1];
				$BrhCode    = $pecah[0];
		
				$new_nourut = $no_urut;
				$cnim=strlen($new_nourut);
		
				if(empty($rows->CDepNumber)){ 
				   $no="001"; 
				}
				 elseif($cnim=="1"){ 
				   $no="00"; 
				}
				 elseif($cnim=="2"){ 
				   $no="0"; 
				}
				 elseif($cnim=="3"){ 
				   $no=""; 
				}
			}
			$data['deposit_number'] = $BrhCode.'/'.$bulan.$tahun.$kode.'/'.$no.$new_nourut;
		} 
		 else {
			$no_urut ='001';
			$data['deposit_number'] = $BranchCode.'/'.$bulan.$tahun.$kode.'/'.$no_urut;
		}
		
		$data['dep_number']  		= $this->m_deposit->create_dep_number(date('d-m-Y'));
		
		// set for view
		$this->load->view('v_header');
		$this->load->view('cash_bank/deposit/v_deposit', $data);
		$this->load->view('v_footer');
	}
	
	public function get_akun_bank_add(){
	    $CompanyCode 	= $this->session->userdata('company');
		$BranchCode 	= $this->session->userdata('branch');
		$list_bank 		= $this->m_deposit->get_coa_bank_add($CompanyCode, $BranchCode);
		echo"$list_bank";
	}
	
	public function get_akun_bank($brach_code){
		$replace_branch = str_replace('_','-',$brach_code);
		
	    $CompanyCode 	= $this->session->userdata('company');
		//$BranchCode 	= $this->session->userdata('branch');
		$list_bank 		= $this->m_deposit->get_coa_bank($CompanyCode, $replace_branch);
		echo"$list_bank";
	}
	
	public function get_petty_cash_add(){
	    $CompanyCode = $this->session->userdata('company');
		$BranchCode 	= $this->session->userdata('branch');
		$list_petty_cash =  $this->m_deposit->get_petty_cash_add($CompanyCode, $BranchCode);
		echo"$list_petty_cash";
	}
	
	public function get_petty_cash($brach_code){
		$replace_branch = str_replace('_','-',$brach_code);
		
	    $CompanyCode = $this->session->userdata('company');
		//$BranchCode 	= $this->session->userdata('branch');
		$list_petty_cash =  $this->m_deposit->get_petty_cash($CompanyCode, $replace_branch);
		echo"$list_petty_cash";
	}
	
	public function get_dep_number() {
        $preg_date      = $this->input->post('Dep_date') == '' ? date('d-m-Y') : $this->input->post('Dep_date');
        $dep_number    = $this->m_deposit->create_dep_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');
		
        $bulan 					= date('m');
		$tahun 					= date('y');
		$cek_tahun 				= date('Y');
		$kode 					= "CD";
		$result_insert 			= 0;
		
		
		$txt_deposit_date 	= date("Y-m-d", strtotime($this->input->post('txt_deposit_date')));	
		$cmb_bank_from 		= $this->input->post('cmb_bank_from');
		$cmb_bank 			= $this->input->post('cmb_bank');
		$txa_memo 			= $this->input->post('txa_memo');
		$txt_amount 		= $this->input->post('txt_amount');
		$txt_deposit_no 	= $this->input->post('txt_deposit_no');
		
		$this->db->trans_begin();
		$data_deposit = array(
			'BranchCode' 		=> $BranchCode,
			'CDepDate' 			=> $txt_deposit_date,
			'CDepNumber' 		=> $txt_deposit_no,
			'BankPayment' 		=> $cmb_bank_from,
			'BankReceipt' 		=> $cmb_bank,
			'CqTermStatus' 		=> NULL,
			'CDepDesc' 			=> $txa_memo,
			'CDepTotalAmount'	=> $txt_amount,
			'TransStatus' 		=> 4,
			'LastUser' 			=> $this->session->userdata('username')
		);
		$result_insert = $this->m_deposit->insert_deposit($data_deposit);
		if($result_insert == 0){ $this->proses_tutup(); }
		
		$journal_debet = array(
			'CDepDate' 			=> $txt_deposit_date,
			'CDepNumber' 		=> $txt_deposit_no,
			'CDepAccountNumber' => $cmb_bank,
			'CDepSaldoDebet'  	=> $txt_amount,
			'CDepSaldoCredit'  	=> 0,
			'CDepDesc'  		=> $txa_memo,
			'CDepSignStatus' 	=> 0,
			'LastUser' 			=> $this->session->userdata('username')
		);
		$result_insert = $this->m_deposit->insert_journal_debit_deposit($journal_debet);
		if($result_insert == 0){ $this->proses_tutup(); }
		
        $journal_credit = array(
			'CDepDate' 			=> $txt_deposit_date,
			'CDepNumber' 		=> $txt_deposit_no,
			'CDepAccountNumber' => $cmb_bank_from,
			'CDepSaldoDebet'  	=> 0,
			'CDepSaldoCredit'  	=> $txt_amount,
			'CDepDesc'  		=> $txa_memo,
			'CDepSignStatus' 	=> 1,
			'LastUser' 			=> $this->session->userdata('username')
		);
		$result_insert = $this->m_deposit->insert_journal_credit_deposit($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_deposit->get_detail($id);

		// set for view
		$this->load->view('v_header');
		$this->load->view('cash_bank/deposit/v_edit_deposit', $data);
		$this->load->view('v_footer');
	}
	
	// Function Update
	public function update() {
        $BranchCode 		= $this->session->userdata('branch');		
		$txt_deposit_no 	= $this->input->post('txt_deposit_no');
		$txt_deposit_date 	= date("Y-m-d", strtotime($this->input->post('txt_deposit_date')));	
		$cmb_bank_from 		= $this->input->post('cmb_bank_from');
		$cmb_bank 			= $this->input->post('cmb_bank');
		$txa_memo 			= $this->input->post('txa_memo');
		$txt_amount 		= $this->input->post('txt_amount');
		$chk_suspend 		= $this->input->post('chk_suspend');
		$result_insert 		= 0;
		
		$data_deposit = array(
			//'BranchCode' 		=> $BranchCode,
			'CDepDate' 			=> $txt_deposit_date,
			'CDepNumber' 		=> $txt_deposit_no,
			'BankPayment' 		=> $cmb_bank_from,
			'BankReceipt' 		=> $cmb_bank,
			'CqTermStatus' 		=> NULL,
			'CDepDesc' 			=> $txa_memo,
			'CDepTotalAmount' 	=> $txt_amount,
			'LastUser' 			=> $this->session->userdata('username')
		);
		
		$this->db->trans_begin();
		$result_insert = $this->m_deposit->update_deposit($data_deposit, $txt_deposit_no);
		if($result_insert == 0){ $this->proses_tutup(); }
		
        $journal_debet = array(
			'CDepDate' 				=> $txt_deposit_date,
			'CDepNumber' 			=> $txt_deposit_no,
			'CDepAccountNumber' 	=> $cmb_bank_from,
			'CDepSaldoDebet'  		=> $txt_amount,
			'CDepSaldoCredit'  		=> 0,
			'CDepDesc'  			=> $txa_memo,
			'LastUser' 				=> $this->session->userdata('username')
		);

		$result_insert = $this->m_deposit->update_journal_debit_deposit($journal_debet, $txt_deposit_no);
		if($result_insert == 0){ $this->proses_tutup(); }
		
		$journal_credit = array(
			'CDepDate' 				=> $txt_deposit_date,
			'CDepNumber' 			=> $txt_deposit_no,
			'CDepAccountNumber' 	=> $cmb_bank,
			'CDepSaldoDebet'  		=> 0,
			'CDepSaldoCredit'  		=> $txt_amount,
			'CDepDesc'  			=> $txa_memo,
			'LastUser' 				=> $this->session->userdata('username')
		);

		$result_insert = $this->m_deposit->update_journal_credit_deposit($journal_credit, $txt_deposit_no);
		if($result_insert == 0){ $this->proses_tutup(); }
		
		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);
	}
	
	public function suspend() {
		// inisialisasi additional parameter
        $deposit_number = $this->input->post('id');
		$id = str_replace(".","/", $deposit_number);
		$hasil 		= 0;
		
		$data = array (
			'TransStatus' => 3,
			'LastUser' => $this->session->userdata("username")
		);
		
		$this->db->trans_begin();
		$cek_status = $this->m_deposit->cek_trans_status($id);
		
		if($cek_status == 6 || $cek_status == 4){
			$hasil = $this->m_deposit->pembatalan_transaksi($id, $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){	
				echo json_encode(array('status'=>true, 'message'=>'Deposit berhasil dbatalkan'));
			} else {
				echo json_encode(array('status'=>false, 'message'=>'Pembatalan Deposit Gagal'));
			}
		} else {
			echo json_encode(array('status'=>false, 'message'=>'Deposit sudah tidak bisa dbatalkan'));
		}
	}
	
	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_deposit->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($id){
	    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
		
	    $CompanyCode = $this->session->userdata('company');
		
		$BranchCode 	= $this->session->userdata('branch');	
        $deposit_number = str_replace($data['menu_link'].'/to_journal/',"",$this->uri->uri_string());		
		
		$data['date_journal'] = $this->m_deposit->get_date_journal_deposit($deposit_number);
		
		$ambil_dari_cashdeposit = $this->m_deposit->get_branch($deposit_number);
		$branch = $ambil_dari_cashdeposit->BranchCode;
		
		$data['list_jurnal']  = $this->m_deposit->get_all_journal_deposit($deposit_number, $branch);
		
		$this->load->view('v_header');
		$this->load->view('cash_bank/deposit/v_print_journal_deposit', $data);	
		$this->load->view('v_footer');	
	}
	
	public function to_print($CDepNumber){
		
		$id = str_replace(".","/", $CDepNumber);  
		
		$detail  = $this->m_deposit->get_detail($id);
		
		$data['BankPayment']     = $this->m_deposit->get_account_name_bank_payment($detail->BankPayment);
		$data['BankReceipt']     = $this->m_deposit->get_account_name_bank_receipt($detail->BankReceipt);
		$data['CDepNumber']      = $detail->CDepNumber;
        $data['CDepDate']        = $detail->CDepDate;
        $data['CDepDesc']        = $detail->CDepDesc;
        $data['CDepTotalAmount'] = $detail->CDepTotalAmount;
		$data['AppLevel1Code'] = $detail->AppLevel1Code;
		$data['AppLevel1Date'] = $detail->AppLevel1Date;
		
		
		
		$this->load->view('v_header');
		$this->load->view('cash_bank/deposit/v_print_deposit', $data);	
		$this->load->view('v_footer');	
	}
	
	public function approval1() {
		if($this->session->userdata("jobtitle")=='DIR'){
			$deposit_number= $this->input->post('txt_deposit_no');	
			
			$cek_app_status_mgr = $this->m_deposit->cek_status_app_mgr($deposit_number);
            if(!empty($cek_app_status_mgr)){
				$TransStatus = 2; 
				$stat_appfinished = array(
					'AppFinished' 		=> 1
				);
				$result_insert = $this->m_payment->update_journal_appfinis($stat_appfinished, $deposit_number);
				//if($result_insert == 0){ $this->proses_tutup(); }
				
			} 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_deposit->approval_status($data,$deposit_number);
				
				$app_finished = array('AppFinished' => 1);
				$cek_app = $this->m_deposit->cek_app($deposit_number);
				
				if(!empty($cek_app)){
					$this->m_deposit->status_finished($app_finished, $deposit_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")=='MGR'){
			$deposit_number= $this->input->post('txt_deposit_no');	
			
            /* $cek_app_status_dir = $this->m_deposit->cek_status_app_dir($deposit_number);
            if(!empty($cek_app_status_dir)){
				$TransStatus = 2; 
			} else {
				$TransStatus = 5;
			} */
			
			$data = array (		
				'AppLevel1Status' 		=> 1,
				'TransStatus'           => 2,
				'AppLevel1Code'         => $this->session->userdata("username"),
				'AppLevel1Date'			=> date("Y-m-d"),	
				'LastUser' 				=> $this->session->userdata("username")
			);
			// transaction begin
			$this->db->trans_begin();
				$this->m_deposit->approval_status($data,$deposit_number);
				
				$app_finished = array('AppFinished' => 1);
				$cek_app = $this->m_deposit->cek_app($deposit_number);
				
				if(!empty($cek_app)){
					$this->m_deposit->status_finished($app_finished, $deposit_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);
		}
	}
}