<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Wahyu
 *	Tanggal : 24-11-2014	
 *	Revisi 	: 
 */
 
class Payment_lebih extends CI_Controller {
	// inisialisasi manual
	public $menu_id = 266;
	public $menu_model = 'm_payment_lebih';
	
    public function __construct()
    {
        parent::__construct();
		$this->load->model('m_payment_lebih');
		$this->load->helper('message_helper');
		$this->load->helper('status');
	}
	
	//Function Index
    public function index()
	{	
		// set data
		$data['menu_id'] =  $this->menu_id;
		$data['menu_model'] = $this->menu_model;
		
		// get link from tbl_menu 
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
		
		$this->load->view('v_header');
		$this->load->view('cash_and_bank/payment/v_payment_lebih', $data);	
		$this->load->view('v_footer');	
	}
	
	//Function Menampilkan Data Pada Data Grid View
	public function ajax(){
		$result = $this->m_payment->get_all();
		
		foreach($result as $row){
			$TransStatus = get_status($row->PaymentStatus);
			
			$hasil[] = array(
				'CPayNumber' 		=>	$row->CPayNumber,
				'CPayDate' 			=>	$row->CPayDate,
				'CPayTotalAmount' 	=>	$row->CPayTotalAmount,
				'CPayDesc' 			=>	$row->CPayDesc,
				'TransStatus' 		=>	$TransStatus
			);
		}

		if(!empty($hasil)){
			echo json_encode($hasil);
		}
		else{
			$emptyVar = array();
			echo json_encode($emptyVar);
		}
	}
	
	public function accountgl(){
        $data = $this->load->m_payment_lebih->get_accountgl();
		echo json_encode($data);
    }
	
	public function get_petty_cash(){
	    $CompanyCode = $this->session->userdata('company');
		$list_petty_cash =  $this->m_payment_lebih->get_petty_cash($CompanyCode);
		echo"$list_petty_cash";
	}
	
	public function all_accountpembiayaan($company_code){
		$data = $this->m_payment_lebih->get_account_by_company($company_code);
		echo json_encode($data);
	}
	
	public function all_department($company_code){
		$data = $this->m_payment_lebih->get_department_by_company($company_code);
		echo json_encode($data);
	}
	
	//Functiom Add Form
	public function add(){
	    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id);
		$data['menu_label']	= $this->m_menu->get_menulabel($this->menu_id);
		$BranchCode 	= $this->session->userdata('branch');	
		
		//custom data
		$bulan = date('m');
		$tahun = date('y');
		$cek_tahun = date('Y');
		$kode ="CP";
		
		$pay_no = $this->m_payment_lebih->count_pay_number($bulan, $cek_tahun, $BranchCode);
		if($pay_no > 0){
			$ceknourut= $this->m_payment_lebih->get_last_pay_number($BranchCode);
			foreach($ceknourut as $rows){
				$pecah = explode("/", $rows->CPayNumber);
				$no_urut = $pecah[2]+1;
				$th_kode = $pecah[1];
				$BrhCode    = $pecah[0];
		
				$new_nourut = $no_urut;
				$cnim=strlen($new_nourut);
		
				if(empty($rows->CPayNumber)){ 
				   $no="001"; 
				}
				 elseif($cnim=="1"){ 
				   $no="00"; 
				}
				 elseif($cnim=="2"){ 
				   $no="0"; 
				}
				 elseif($cnim=="3"){ 
				   $no=""; 
				}
			}
			$data['pay_number'] = $BrhCode.'/'.$bulan.$tahun.$kode.'/'.$no.$new_nourut;
		} 
		 else {
			$no_urut ='001';
			$data['pay_number'] = $BranchCode.'/'.$bulan.$tahun.$kode.'/'.$no_urut;
		}
		
		$this->load->view('v_header');
		$this->load->view('cash_and_bank/payment/v_new_payment_lebih',$data);
		$this->load->view('v_footer');	
	}
	
	public function insert(){
		$txt_payment_no 		= $this->input->post('txt_payment_no');
		$txt_payment_date		= $this->input->post('txt_payment_date');
		$cmb_bank_account		= $this->input->post('cmb_bank_account');
		$txa_memo				= $this->input->post('txa_memo');
		$txt_amount				= $this->input->post('txt_amount');
		$detail 				= $this->input->post('detail');
		
		$BranchCode 	= $this->session->userdata('branch');
		
		// inisialisasi array
		$data = array (
			'BranchCode' 		=> $BranchCode,
			'CPayNumber' 		=> $txt_payment_no, 	
			'CPayDate' 			=> date("Y-m-d", strtotime($txt_payment_date)),	
			'BankPayment' 		=> $cmb_bank_account,
			'CPayDesc' 			=> $txa_memo,
			'CPayTotalAmount' 	=> $txt_amount,
			'LastUser' 			=> $this->session->userdata("username")
		);
		//insert to at_cashpayment
		$hasil = $this->m_payment_lebih->insert($data);
		
		//insert to at_cpaydetail
		$detail = json_decode($detail);
		if($detail){
			foreach($detail as $row){
				$AccountNumber = $this->m_payment->get_accountnumber($row->AccountName);
				foreach($AccountNumber as $acc_numb){
					$acc = $acc_numb->AccountNumber;
				}
				$DepartmentCode = $this->m_payment->get_departmentcode($row->DepartmentName);
				foreach($DepartmentCode as $dept_numb){
					$dept = $dept_numb->DepartmentCode;
				}
				
				$detail_data=array(
					'CPayNumber' 		=> $txt_payment_no,
					'CPayAccountNumber' => $acc,
					'CPayAmount'		=> $row->Amount,
					'CPayMemo'			=> $row->Memo,
					'CPayDeptCode'		=> $dept,
					'LastUser' 		    => $this->session->userdata("username")				
				);                    
				$this->m_payment->insert_fromgrid($detail_data);
		
				//insert to at_cpayjournal
				$data_journal_debet = array(
					'CPayDate' 			=> date("Y-m-d", strtotime($txt_payment_date)),
					'CPayNumber' 		=> $txt_payment_no,
					'CPayAccountNumber' => $acc,
					'CPaySaldoDebet' 	=> $row->Amount,
					'CPaySaldoCredit' 	=> 0,
					'CPayDesc' 			=> $txa_memo,
					'LastUser' 			=> $this->session->userdata('username')
				);
				$this->m_payment->insert_tojournal($data_journal_debet);
			}
			$data_journal_credit = array(
				'CPayDate' 			=> date("Y-m-d", strtotime($txt_payment_date)),
				'CPayNumber' 		=> $txt_payment_no,
				'CPayAccountNumber' => $cmb_bank_account,
				'CPaySaldoDebet' 	=> 0,
				'CPaySaldoCredit' 	=> $txt_amount,
				'CPayDesc' 			=> $txa_memo,
				'LastUser' 			=> $this->session->userdata('username')
			);
			$this->m_payment->insert_tojournal($data_journal_credit);
		}
		
		//get notification
		if($hasil == 0){
			$result['status'] 	= true;
			$result['message']	= get_notification('insert',$hasil);
		}
		else{
			$result['status'] 	= false;
			$result['message']	= get_notification('insert',$hasil);
		}
		echo json_encode($result);
	}
	
}
	