<?php 
/*
Fungsi : Controller untuk Post Expense
*/

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

class Postexp extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
/*-------------Inisialisasi variabel--------------*/ 
	
	//Menu ID utk keperluan otorisasi
	var $menu_post_expense  = 80;

/*-------------Inisialisasi variabel--------------*/ 	

/*------------Fungsi lokal------------------------*/
	


/*------------Fungsi local------------------------*/	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('accounting/acc_model', '', TRUE);
		$this->load->model('admin/admin_model', '', TRUE);
		$this->load->model('finance/fin_model', '', TRUE);
		$this->load->model('branch/branch_model', '', TRUE);
		$this->fungsi->check_login();
	}
	
	public function get_glacc($gl)
	{
			$query_gl = $this->branch_model->get_list_gl();
			$data_gl = $query_gl->result();
			$glcbbox='';
			foreach($data_gl as $row)
			{
				$glcbbox.= $row->glacc == $gl ? '<option value="'.$row->glacc.'" selected="selected">': '<option value="'.$row->glacc.'">';
				$glcbbox.= $row->glacc.' - '.$row->desc.'</option>';
			}
			
		return $glcbbox;
	}
	public function index()
	{	
		$this->postexp_form();
		
	}

//Petty cash operasional OP revisi 2 (Clearing)
function postexpoa_form3()
{	

	//Get data cabang get_cab_detail($idcab)
		$query_get_cab = $this->admin_model->get_cab_detail($this->uri->segment(6));
		foreach ($query_get_cab->result() as $row_cab);
		
		$data['pcglaccuo'] = $row_cab->pcglaccuo;
		$data['pcbankglacc'] = $row_cab->pcbankglacc;
		$data['pcopglacc'] = $row_cab->pcglaccop;
		
		
	$query_cpv_detail = $this->fin_model->get_cpv_bynoyear($this->uri->segment(4),$this->uri->segment(5));
	foreach ($query_cpv_detail->result() as $row_cpv)
		{
			$data['cpvappby'] = $row_cpv->appby;
			$data['idcab'] = $row_cpv->idcab;
			$data['cpvappdate'] = $row_cpv->appdate;
			$data['default']['txt_bktxt'] = 'OA '.$row_cab->cabshorttxt;
			$data['default']['txt_zounr'] = 'OA '.$row_cab->cabshorttxt;
			$data['nocpvform'] = $row_cpv->nocpvform;
			$data['default']['txt_xblnr'] = $row_cpv->nocpdu;
			$data['txt_totalcpv'] = $row_cpv->amount_trf;
			$data['txt_vendor'] = $this->fungsi_sap->fm_convert_vendor($row_cab->lifnr);
			$data['txt_sgtxt'] = strtoupper($row_cab->cabshorttxt.'-Reimburse P.Cash OP OA per');
			$data['txt_sgtxt2'] = strtoupper('3101 -'.$row_cab->cabshorttxt.'-Reimburse P.Cash OP OA per');
			$data['txt_zounr'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
			$data['default']['txt_miro'] = trim($this->fungsi_sap->fm_accno_miro($row_cpv->miro.$row_cpv->year));
		}
		
		$data['tables']='';
		$bg=0;
		$i = 0;
		$query_bpkk_detail = $this->acc_model->get_bpkk_bynocpv($row_cpv->nocpvform);
		foreach ($query_bpkk_detail->result() as $row_bpkk);
		
		//miro data
		$data['txt_totalval2'] = $this->fungsi->format_idr($row_cpv->amount_trf);
		
		
		//Bank data cabang
		$data['txt_glacc1'] = $row_cab->pcbankglacc;
		$data['txt_wrbtr1'] = $this->fungsi->format_idr($row_cpv->amount_trf);
		$data['txt_zounr3'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
		$data['txt_sgtxt3'] = strtoupper('1401 -'.$row_cab->cabshorttxt.'-Reimburse P.Cash OP OA per');;
		
		//Bank data pusat
		$data['txt_glacc2'] = "1112116";
		$data['txt_wrbtr2'] = $this->fungsi->format_idr($row_cpv->amount_trf) ;
		$data['txt_zounr4'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
		$data['txt_sgtxt4'] = strtoupper($row_cab->cabshorttxt.'-Reimburse P.Cash OP OA per');
		
	$data['menu_path'] = 'ACCOUNTING > DETAIL CPV ONGKOS ANGKUT > '.$row_cpv->nocpvform;
	$data['form_action']= site_url('accounting/postexp/postexpoa_proc3'.'/'.$this->uri->segment(4).'/'.$this->uri->segment(5).'/'.		$this->uri->segment(6));
	$data['link'] = array('link_back' => anchor('accounting/acc_gl','Back', array('class' => 'back')));
	$this->load->view('accounting/acc_postexpoa_form3', $data);

}

//Petty cash operasional OP Process clearing
function postexpoa_proc3()
{
	//Create clearing
	
	$this->fungsi_sap->fm_post_clear_oa($this->input->post('txt_bldat'), $this->input->post('txt_budat'), $this->input->post('txt_monat'), $this->input->post('txt_xblnr'), $this->input->post('txt_bktxt'), $this->input->post('txt_pk'), $this->input->post('txt_gl'),$this->input->post('txt_totalcpv'),$this->input->post('txt_zounr'),$this->input->post('txt_sgtxt'),$this->input->post('txt_vendor'),$this->input->post('txt_miro'),$this->input->post('txt_zounr2'),$this->input->post('txt_sgtxt2'));	

if($this->session->userdata('sap_clear_no_oa') != '')
{

/* Update data CPV */	
	$cpv = array( 
								'clrno' => $this->session->userdata('sap_clear_no_oa'),
								'accglpostby'	=> $this->session->userdata('uid'),
								'accglpostdate' => date( 'Y-m-d H:i:s'),
								);
				
$this->fin_model->update_cpv($this->uri->segment(4), $this->uri->segment(5), $cpv);


/*------------------------------------Jurnal Bank--------------------------------------*/
	$accountgl[] = array( 		'ITEMNO_ACC' => 1,
								'GL_ACCOUNT' => $this->input->post('txt_glacc1'),
								'PSTNG_DATE' => $this->input->post('txt_budat'),
								'FIS_PERIOD' => $this->input->post('txt_budat'),
								'ALLOC_NMBR' => $this->input->post('txt_zounr3'),
								'ITEM_TEXT'	 => $this->input->post('txt_sgtxt3'),
								);
	$accountgl[] = array( 		'ITEMNO_ACC' => 2,
								'GL_ACCOUNT' => $this->input->post('txt_glacc2'),
								'PSTNG_DATE' => $this->input->post('txt_budat'),
								'FIS_PERIOD' => $this->input->post('txt_budat'),
								'ALLOC_NMBR' => $this->input->post('txt_zounr4'),
								'ITEM_TEXT'	 => $this->input->post('txt_sgtxt4'),
								);
								
	$curramount[] = array( 		'ITEMNO_ACC' => 1,
								'AMT_DOCCUR' => $this->fungsi->convert_idr_to_num($this->input->post('txt_wrbtr1')),
								);
	$curramount[] = array( 		'ITEMNO_ACC' => 2,
								'AMT_DOCCUR' => $this->fungsi->convert_idr_to_num($this->input->post('txt_wrbtr2')) * -1,
								);
	
	//print_r($accountgl);
	//print_r($curramount);
	
	$this->fungsi_sap->fm_post_gl_acc($this->input->post('txt_bktxt'),$this->input->post('txt_budat'),$this->input->post('txt_bldat'),$this->input->post('txt_xblnr'),  $accountgl,$curramount);
	
	if ($this->session->userdata('sap_obj_key') != '')
	{
		/* Insert data */
			$bankbal = array( 	'idcab'		 => $this->uri->segment(6),
								'sap_ref'	 => $this->session->userdata('sap_obj_key'),
								'rlis_ref'	 => $this->input->post('txt_nocpvform'),
								'amount'	 => $this->fungsi->convert_idr_to_num($this->input->post('txt_wrbtr1')),
								'addby'		 => $this->session->userdata('uid'),
								//Postdate
								'postdate'	 => date("Y-m-d",   strtotime($this->input->post('txt_budat'))),
								'adddate' 	 => date( 'Y-m-d H:i:s'),
								);
		
			$this->fin_model->add_bankbal($bankbal);
			
	/* Update data CPV */	
	$cpv = array( 				'accglpost'		=> 1,
								'accglno' => $this->session->userdata('sap_obj_key'),
								'clrno' => $this->fungsi_sap->fm_accno_miro($this->session->userdata('sap_miro_oa_num').date('Y')),
								'accglpostby'	=> $this->session->userdata('uid'),
								'accglpostdate' => date( 'Y-m-d H:i:s'),
								);
				
	$this->fin_model->update_cpv($this->uri->segment(4), $this->uri->segment(5), $cpv);
	
	/* Insert data history PC OP */
			$hpcop = array( 	'idcab'		 => $this->input->post('txt_idcab'),
								'sap_ref'	 => $this->session->userdata('sap_obj_key'),
								'rlis_ref'	 => $this->input->post('txt_nocpvform'),
								'amount'	 => $this->fungsi->convert_idr_to_num($this->input->post('txt_wrbtr1')),
								'addby'		 => $this->session->userdata('uid'),
								//Postdate
								'postdate'	 => date("Y-m-d",   strtotime($this->input->post('txt_budat'))),
								'adddate' 	 => date( 'Y-m-d H:i:s'),
								);
			$this->acc_model->add_h_pcop($hpcop);
		
	}
/*------------------------------------Jurnal Bank--------------------------------------*/
	
$this->session->set_flashdata('message_ok', 'Clearing Number :'.$this->session->userdata('sap_clear_no_oa').' ,Doc. number : '.$this->session->userdata('sap_obj_key'));
redirect('accounting/acc_gl');		
}


}
//Petty cash operasional OP revisi 2
function postexpoa_form2()
{	

	//Get data cabang get_cab_detail($idcab)
		$query_get_cab = $this->admin_model->get_cab_detail($this->uri->segment(6));
		foreach ($query_get_cab->result() as $row_cab);
		
		$data['pcglaccuo'] = $row_cab->pcglaccuo;
		$data['pcbankglacc'] = $row_cab->pcbankglacc;
		$data['pcopglacc'] = $row_cab->pcglaccop;
		
		
	$query_cpv_detail = $this->fin_model->get_cpv_bynoyear($this->uri->segment(4),$this->uri->segment(5));
	foreach ($query_cpv_detail->result() as $row_cpv)
		{
			$data['cpvappby'] = $row_cpv->appby;
			$data['idcab'] = $row_cpv->idcab;
			$data['cpvappdate'] = $row_cpv->appdate;
			$data['default']['txt_bktxt'] = 'OA '.$row_cab->cabshorttxt;
			$data['default']['txt_zounr'] = 'OA '.$row_cab->cabshorttxt;
			$data['nocpvform'] = $row_cpv->nocpvform;
			$data['default']['txt_xblnr'] = $row_cpv->nocpdu;
			$data['txt_totalcpv'] = $row_cpv->amount_trf;
			$data['txt_vendor'] = $this->fungsi_sap->fm_convert_vendor($row_cab->lifnr);
			$data['default']['txt_sgtxt'] = strtoupper($row_cab->cabshorttxt.'-Reimburse P.Cash OP OA per');
		}
		
		$data['tables']='';
		$bg=0;
		$i = 0;
		$query_bpkk_detail = $this->acc_model->get_bpkk_bynocpv($row_cpv->nocpvform);
		foreach ($query_bpkk_detail->result() as $row_bpkk);
		
		//miro data
		$data['txt_totalval2'] = $this->fungsi->format_idr($row_cpv->amount_trf);
		
		
		//Bank data cabang
		/*$data['txt_glacc1'] = $row_cab->pcbankglacc;
		$data['txt_wrbtr1'] = $this->fungsi->format_idr($row_cpv->amount_trf);
		$data['txt_zounr1'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
		$data['txt_sgtxt1'] = "Reimburse";*/
		
		//Bank data pusat
		/*
		$data['txt_glacc2'] = "1112116";
		$data['txt_wrbtr2'] = $this->fungsi->format_idr($row_cpv->amount_trf) ;
		$data['txt_zounr2'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
		$data['txt_sgtxt2'] = "Reimburse";
		*/
		
	$data['menu_path'] = 'ACCOUNTING > DETAIL CPV ONGKOS ANGKUT > '.$row_cpv->nocpvform;
	$data['form_action']= site_url('accounting/postexp/postexpoa_proc2'.'/'.$this->uri->segment(4).'/'.$this->uri->segment(5).'/'.		$this->uri->segment(6));
	$data['link'] = array('link_back' => anchor('accounting/acc_gl','Back', array('class' => 'back')));
	$this->load->view('accounting/acc_postexpoa_form2', $data);

}

//Petty cash operasional OP Process
function postexpoa_proc2()
{
	//Create MIRO
	$this->fungsi_sap->fm_create_miro_oa2($this->input->post('txt_bldat'), $this->input->post('txt_budat'), $this->input->post('txt_zfbdt'),$this->input->post('txt_xblnr'),$this->input->post('txt_bktxt'), $this->input->post('txt_vendor'), $this->input->post('txt_zounr'), $this->fungsi->convert_idr_to_num($this->input->post('txt_wrbtr')),$this->input->post('txt_sgtxt'));

if($this->session->userdata('sap_miro_oa_num') != '')
{

/* Update data CPV */	
	$cpv = array( 
								'miro' => $this->session->userdata('sap_miro_oa_num'),
								'accglpostby'	=> $this->session->userdata('uid'),
								'accglpostdate' => date( 'Y-m-d H:i:s'),
								);
				
	$this->fin_model->update_cpv($this->uri->segment(4), $this->uri->segment(5), $cpv);
	
$this->session->set_flashdata('message_ok', 'MIRO Number :'.$this->session->userdata('sap_miro_oa_num').',G/L Number :'.$this->fungsi_sap->fm_accno_miro($this->session->userdata('sap_miro_oa_num').date('Y')));
redirect('accounting/acc_gl');		
}

}

//Petty cash operasional OP
function postexpoa_form()
{	

	//Get data cabang get_cab_detail($idcab)
		$query_get_cab = $this->admin_model->get_cab_detail($this->uri->segment(6));
		foreach ($query_get_cab->result() as $row_cab);
		
		$data['pcglaccuo'] = $row_cab->pcglaccuo;
		$data['pcbankglacc'] = $row_cab->pcbankglacc;
		$data['pcopglacc'] = $row_cab->pcglaccop;
		
		
	$query_cpv_detail = $this->fin_model->get_cpv_bynoyear($this->uri->segment(4),$this->uri->segment(5));
	foreach ($query_cpv_detail->result() as $row_cpv)
		{
			
	
			$data['cpvappby'] = $row_cpv->appby;
			$data['idcab'] = $row_cpv->idcab;
			$data['cpvappdate'] = $row_cpv->appdate;
			$data['default']['txt_bktxt'] = $row_cpv->nocpvform;
			$data['nocpvform'] = $row_cpv->nocpvform;
			$data['default']['txt_xblnr'] = $row_cpv->nocpdu;
			$data['txt_totalcpv'] = $row_cpv->amount_trf;
			
			/*$data['default']['txt_zounr'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
			$data['default']['txt_zounr2'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
			$data['default']['txt_zounr3'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
			$data['default']['txt_zounr4'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
			
			$data['default']['txt_sgtxt2'] = '3101 -'.strtoupper($row_cab->cabshorttxt).'- P.Cash Used Oil '.$row_cpv->nocpvform;
			$data['default']['txt_sgtxt'] = '4905 -'.strtoupper($row_cab->cabshorttxt).'- P.Cash Used Oil '.$row_cpv->nocpvform;
			$data['default']['txt_sgtxt3'] = '1401 -'.strtoupper($row_cab->cabshorttxt).'- P.Cash Used Oil '.$row_cpv->nocpvform;
			$data['default']['txt_sgtxt4'] = strtoupper($row_cab->cabshorttxt).'- P.Cash Used Oil '.$row_cpv->nocpvform;*/
			
		}
		
		$data['tables']='';
		$bg=0;
		$i = 0;
		$query_bpkk_detail = $this->acc_model->get_bpkk_bynocpv($row_cpv->nocpvform);
		foreach ($query_bpkk_detail->result() as $row_bpkk);
		
		//miro data
		$data['txt_totalval1'] = $this->fungsi->format_idr($row_cpv->amount_trf);
		$data['txt_totalval2'] = $this->fungsi->format_idr($row_cpv->amount_trf);
		$data['txt_sgtxt'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
		$data['txt_vendor'] = $this->fungsi_sap->fm_convert_vendor($row_cab->lifnr);
		
		//Bank data cabang
		$data['txt_glacc1'] = $row_cab->pcbankglacc;
		$data['txt_wrbtr1'] = $this->fungsi->format_idr($row_cpv->amount_trf);
		$data['txt_zounr1'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
		$data['txt_sgtxt1'] = "Reimburse";
		
		//Bank data pusat
		$data['txt_glacc2'] = "1112116";
		$data['txt_wrbtr2'] = $this->fungsi->format_idr($row_cpv->amount_trf) ;
		$data['txt_zounr2'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
		$data['txt_sgtxt2'] = "Reimburse";
		
	$data['menu_path'] = 'ACCOUNTING > DETAIL CPV ONGKOS ANGKUT > '.$row_cpv->nocpvform;
	$data['form_action']= site_url('accounting/postexp/postexpoa_proc'.'/'.$this->uri->segment(4).'/'.$this->uri->segment(5).'/'.		$this->uri->segment(6));
	$data['link'] = array('link_back' => anchor('accounting/acc_gl','Back', array('class' => 'back')));
	$this->load->view('accounting/acc_postexpoa_form', $data);

}
//Petty cash operasional OP Process
function postexpoa_proc()
{
	//Create MIRO
	$this->fungsi_sap->fm_create_miro_oa($this->input->post('txt_bldat'), $this->input->post('txt_budat'), $this->input->post('txt_xblnr'), $this->input->post('txt_vendor'), $this->input->post('txt_gl_pcopr'), $this->fungsi->convert_idr_to_num($this->input->post('txt_glopr_val1')),$this->input->post('txt_zounr'));

if($this->session->userdata('sap_miro_oa_num') != '')
{
	//Jurnal Bank
	$accountgl[] = array( 		'ITEMNO_ACC' => 1,
								'GL_ACCOUNT' => $this->input->post('txt_glacc1'),
								'PSTNG_DATE' => $this->input->post('txt_bldat'),
								'FIS_PERIOD' => $this->input->post('txt_bldat'),
								'ALLOC_NMBR' => $this->input->post('txt_zounr1'),
								'ITEM_TEXT'	 => $this->input->post('txt_sgtxt1'),
								);
	$accountgl[] = array( 		'ITEMNO_ACC' => 2,
								'GL_ACCOUNT' => $this->input->post('txt_glacc2'),
								'PSTNG_DATE' => $this->input->post('txt_bldat'),
								'FIS_PERIOD' => $this->input->post('txt_bldat'),
								'ALLOC_NMBR' => $this->input->post('txt_zounr2'),
								'ITEM_TEXT'	 => $this->input->post('txt_sgtxt2'),
								);
								
	$curramount[] = array( 		'ITEMNO_ACC' => 1,
								'AMT_DOCCUR' => $this->fungsi->convert_idr_to_num($this->input->post('txt_wrbtr1')),
								);
	$curramount[] = array( 		'ITEMNO_ACC' => 2,
								'AMT_DOCCUR' => $this->fungsi->convert_idr_to_num($this->input->post('txt_wrbtr2')) * -1,
								);
	
	//print_r($accountgl);
	//print_r($curramount);
	
	$this->fungsi_sap->fm_post_gl_acc($this->input->post('txt_bktxt'),$this->input->post('txt_bldat'),$this->input->post('txt_xblnr'),  $accountgl,$curramount);
	
	if ($this->session->userdata('sap_obj_key') != '')
	{
		/* Insert data */
			$bankbal = array( 	'idcab'		 => $this->uri->segment(6),
								'sap_ref'	 => $this->session->userdata('sap_obj_key'),
								'rlis_ref'	 => $this->input->post('txt_nocpvform'),
								'amount'	 => $this->fungsi->convert_idr_to_num($this->input->post('txt_wrbtr1')),
								'addby'		 => $this->session->userdata('uid'),
								'adddate' 	 => date( 'Y-m-d H:i:s'),
								);
		
			$this->fin_model->add_bankbal($bankbal);
			
	/* Update data CPV */	
	$cpv = array( 				'accglpost'		=> 1,
								'accglno' => $this->session->userdata('sap_obj_key'),
								'clrno' => $this->fungsi_sap->fm_accno_miro($this->session->userdata('sap_miro_oa_num').date('Y')),
								'accglpostby'	=> $this->session->userdata('uid'),
								'accglpostdate' => date( 'Y-m-d H:i:s'),
								);
				
			$this->fin_model->update_cpv($this->uri->segment(4), $this->uri->segment(5), $cpv);
	
	/* Insert data history PC OP */
			$hpcop = array( 	'idcab'		 => $this->uri->segment(6),
								'sap_ref'	 => $this->session->userdata('sap_obj_key'),
								'rlis_ref'	 => $this->input->post('txt_nocpvform'),
								'amount'	 => $this->fungsi->convert_idr_to_num($this->input->post('txt_wrbtr1')),
								'addby'		 => $this->session->userdata('uid'),
								'adddate' 	 => date( 'Y-m-d H:i:s'),
								);
			$this->acc_model->add_h_pcop($hpcop);
		
	}
	
}

$this->session->set_flashdata('message_ok', 'G/L data posted, Number :'.$this->session->userdata('sap_obj_key').','.$this->fungsi_sap->fm_accno_miro($this->session->userdata('sap_miro_oa_num').date('Y')));
redirect('accounting/acc_gl');

}
	
function postexp_form()
{	

			
	//Get data cabang get_cab_detail($idcab)
		$query_get_cab = $this->admin_model->get_cab_detail($this->uri->segment(6));
		foreach ($query_get_cab->result() as $row_cab);
		
		$data['pcglaccuo'] = $row_cab->pcglaccuo;
		$data['pcbankglacc'] = $row_cab->pcbankglacc;
		
		
	$query_cpv_detail = $this->fin_model->get_cpv_bynoyear($this->uri->segment(4),$this->uri->segment(5));
	foreach ($query_cpv_detail->result() as $row_cpv)
		{	
			//Check apakah termasuk ongkos angkut ?
			$query_get_pcoptyp = $this->acc_model->get_pcoptyp_bynocpv($row_cpv->nocpvform);	
			foreach ($query_get_pcoptyp ->result() as $row_pcoptyp);
			if(	$row_pcoptyp->idpcoprtyp  == 2 )
			{
				//Check apakah belum di miro?
				if($row_cpv->miro == '')
				{
				redirect('accounting/postexp/postexpoa_form2/'.$this->uri->segment(4).'/'.$this->uri->segment(5).'/'.$this->uri->segment(6).'/'.$this->uri->segment(7));
				}
				else
				{
					redirect('accounting/postexp/postexpoa_form3/'.$this->uri->segment(4).'/'.$this->uri->segment(5).'/'.$this->uri->segment(6).'/'.$this->uri->segment(7));
				}
				
			}
			
			$data['cpvappby'] = $row_cpv->appby;
			$data['idcab'] = $row_cpv->idcab;
			$data['cpvappdate'] = $row_cpv->appdate;
			$data['default']['txt_bktxt'] = $row_cpv->nocpvform;
			$data['nocpvform'] = $row_cpv->nocpvform;
			$data['default']['txt_xblnr'] = $row_cpv->nocpdu;
			$data['txt_totalcpv'] = $row_cpv->amount_trf;
			
			/*$data['default']['txt_zounr'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
			$data['default']['txt_zounr2'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
			$data['default']['txt_zounr3'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
			$data['default']['txt_zounr4'] = strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y'));
			
			$data['default']['txt_sgtxt2'] = '3101 -'.strtoupper($row_cab->cabshorttxt).'- P.Cash Used Oil '.$row_cpv->nocpvform;
			$data['default']['txt_sgtxt'] = '4905 -'.strtoupper($row_cab->cabshorttxt).'- P.Cash Used Oil '.$row_cpv->nocpvform;
			$data['default']['txt_sgtxt3'] = '1401 -'.strtoupper($row_cab->cabshorttxt).'- P.Cash Used Oil '.$row_cpv->nocpvform;
			$data['default']['txt_sgtxt4'] = strtoupper($row_cab->cabshorttxt).'- P.Cash Used Oil '.$row_cpv->nocpvform;*/
			
		}
		
		$data['tables']='';
		$bg=0;
		$i = 0;
		$query_bpkk_detail = $this->acc_model->get_bpkk_bynocpv($row_cpv->nocpvform);
		foreach ($query_bpkk_detail->result() as $row_bpkk)
		{
			if($bg==0)
				{
				$data['tables'].="<tr style=background-color:#eeeeee;>";
				$bg++;
				}
				else
				{
				$data['tables'].="<tr style=background-color:#;>";
				$bg--;
				}
			$i = $i + 1;
			
			$data['tables'].= '
     <td><select name="glacc'.$i.'" id="glacc'.$i.'">'.$this->get_glacc($row_bpkk->glacc).'</select></td>
	   <td><input name="txt_costctr'.$i.'" type="text" id="txt_costctr'.$i.'" size="10" maxlength="10" value="'.$row_bpkk->costctr.'" /></td>
     <td><label for="select">Debit</label></td>
     <td><input name="txt_wrbtr'.$i.'" readonly="readonly" type="text" id="txt_wrbtr'.$i.'" size="15" value="'.$this->fungsi->format_idr($row_bpkk->total_item).'" /></td>
     <td><input name="txt_zounr'.$i.'" type="text" id="txt_zounr'.$i.'" size="18" maxlength="18" value="'. strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y')).'" /></td>
     <td><input name="txt_sgtxt'.$i.'" type="text" id="txt_sgtxt'.$i.'" size="50" maxlength="50" value="'.$row_bpkk->cashflow.' -'.$row_bpkk->uraian.'"/></td>
    </tr>';
		}
	//Insert Data G/L PC OP
	$i = $i + 1;
	$data['tables'].= '<tr bgcolor="#FFFF99">
     <td><input name="glacc'.$i.'" type="text"  id="glacc'.$i.'" size="10" maxlength="10" value="'.$row_cab->pcglaccop.'" readonly="readonly" /></td>
	   <td><input name="txt_costctr'.$i.'" type="text" id="txt_costctr'.$i.'" size="10" maxlength="10" value="" readonly="readonly" /></td>
     <td><label for="select">Credit</label></td>
     <td><input name="txt_wrbtr'.$i.'" readonly="readonly" type="text" id="txt_wrbtr'.$i.'" size="15" value="'.$this->fungsi->format_idr($row_cpv->amount_trf * -1).'" /></td>
     <td><input name="txt_zounr'.$i.'" type="text" id="txt_zounr'.$i.'" size="18" maxlength="18" value="'. strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y')).'" /></td>
     <td><input name="txt_sgtxt'.$i.'" type="text" id="txt_sgtxt'.$i.'" size="50" maxlength="50" value="Reimburse"/></td>
    </tr>';
	
	//Insert Data Bank BDG
	$i = $i + 1;
	$data['tables'].= '<tr bgcolor="#FFFF99">
     <td><input name="glacc'.$i.'" type="text"  id="glacc'.$i.'" size="10" maxlength="10" value="'.$row_cab->pcbankglacc.'" readonly="readonly" /></td>
	   <td><input name="txt_costctr'.$i.'" type="text" id="txt_costctr'.$i.'" size="10" maxlength="10" value="" readonly="readonly" /></td>
     <td><label for="select">Debit</label></td>
     <td><input name="txt_wrbtr'.$i.'" readonly="readonly" type="text" id="txt_wrbtr'.$i.'" size="15" value="'.$this->fungsi->format_idr($row_cpv->amount_trf).'" /></td>
     <td><input name="txt_zounr'.$i.'" type="text" id="txt_zounr'.$i.'" size="18" maxlength="18" value="'. strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y')).'" /></td>
     <td><input name="txt_sgtxt'.$i.'" type="text" id="txt_sgtxt'.$i.'" size="50" maxlength="50" value="Reimburse"/></td>
    </tr>';
	
	//Insert Data Bank Pusat
	$i = $i + 1;
	$data['tables'].= '<tr bgcolor="#FFFF99">
     <td><input name="glacc'.$i.'" type="text"  id="glacc'.$i.'" size="10" maxlength="10" value="1112116" readonly="readonly" /></td>
	   <td><input name="txt_costctr'.$i.'" type="text" id="txt_costctr'.$i.'" size="10" maxlength="10" value="" readonly="readonly" /></td>
     <td><label for="select">Credit</label></td>
     <td><input name="txt_wrbtr'.$i.'" readonly="readonly" type="text" id="txt_wrbtr'.$i.'" size="15" value="'.$this->fungsi->format_idr($row_cpv->amount_trf * -1).'" /></td>
     <td><input name="txt_zounr'.$i.'" type="text" id="txt_zounr'.$i.'" size="18" maxlength="18" value="'. strtoupper($row_cab->cabshorttxt.' - '.date('M').'\''.date('y')).'" /></td>
     <td><input name="txt_sgtxt'.$i.'" type="text" id="txt_sgtxt'.$i.'" size="50" maxlength="50" value="Reimburse"/></td>
    </tr>';
	
	$data['totaldata'] = $i;
	$data['menu_path'] = 'ACCOUNTING > DETAIL CPV > '.$row_cpv->nocpvform;
	$data['form_action']= site_url('accounting/postexp/postexp_proc'.'/'.$this->uri->segment(4).'/'.$this->uri->segment(5).'/'.$this->uri->segment(6));
	$data['link'] = array('link_back' => anchor('accounting/acc_gl','Back', array('class' => 'back')));
	$this->load->view('accounting/acc_postexp_form', $data);
	
}

function postexp_proc()
{
		//Cek dulu document CPV sudah ada pada database atau belom ?
	/*if($this->fungsi->check_cpv_bank($this->input->post('txt_nocpvform')) == TRUE)
	{*/
$config = array(
               array(
                     'field'   => 'txt_xblnr',
                     'label'   => 'Reference',
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'txt_bktxt',
                     'label'   => 'Doc Header Text',
                     'rules'   => 'required'
                  )
            );
	
	
		//Check validasi form
		$this->form_validation->set_rules($config); 	
	
if ($this->form_validation->run() == TRUE)
			{	
			
				for ($i = 1; $i <= $this->input->post('totaldata'); $i++)
				{
					$accountgl[] = array('ITEMNO_ACC' => $i,
								'GL_ACCOUNT' => $this->input->post('glacc'.$i),
								'COSTCENTER' => $this->input->post('txt_costctr'.$i),
								'PSTNG_DATE' => $this->input->post('txt_budat'),
								'FIS_PERIOD' => $this->input->post('txt_budat'),
								'ALLOC_NMBR' => $this->input->post('txt_zounr'.$i),
								'ITEM_TEXT'	 => $this->input->post('txt_sgtxt'.$i),
								);
					$curramount[] = array( 'ITEMNO_ACC' => $i,
								'AMT_DOCCUR' => $this->fungsi->convert_idr_to_num($this->input->post('txt_wrbtr'.$i)),
								);
				}
	
	//Check if array is empty
	
	//Input additional line item disini
	$gl = $this->input->post('addgl');
	$costctr = $this->input->post('addcostctr');
	$amount = $this->input->post('addamount');
	$assign = $this->input->post('addassign');
	$itemtxt = $this->input->post('additemtxt');

$empty = array_filter($gl);

if (!empty($empty)) {
	
	//echo 'not empty';
	$i = $i - 1;	
foreach( $gl as $key => $n ) {
$i = $i + 1;
 // print "The name is ".$n.$costctr[$key];
 					$accountgl[] = array('ITEMNO_ACC' => $i,
								'GL_ACCOUNT' => $n,
								'COSTCENTER' => $costctr[$key],
								'PSTNG_DATE' => $this->input->post('txt_budat'),
								'FIS_PERIOD' => $this->input->post('txt_budat'),
								'ALLOC_NMBR' => $assign[$key],
								'ITEM_TEXT'	 => $itemtxt[$key],
								);
					$curramount[] = array( 'ITEMNO_ACC' => $i,
								'AMT_DOCCUR' => $this->fungsi->convert_idr_to_num($amount[$key]),
								);
}
}
//Input additional line item disini
	
	
				
				$this->fungsi_sap->fm_post_gl_acc($this->input->post('txt_bktxt'),$this->input->post('txt_budat'),$this->input->post('txt_bldat'),$this->input->post('txt_xblnr'),  $accountgl,$curramount);
	
	if ($this->session->userdata('sap_obj_key') != '')
	{
	/* Insert data */
			$bankbal = array( 	'idcab'		 => $this->uri->segment(6),
								'sap_ref'	 => $this->session->userdata('sap_obj_key'),
								'rlis_ref'	 => $this->input->post('txt_nocpvform'),
								'amount'	 => $this->fungsi->convert_idr_to_num($this->input->post('txt_totalcpv')),
								//Postdate
								'postdate'	 => date("Y-m-d",   strtotime($this->input->post('txt_budat'))),
								'addby'		 => $this->session->userdata('uid'),
								'adddate' 	 => date( 'Y-m-d H:i:s'),
								);
		
			$this->fin_model->add_bankbal($bankbal);
			
	/* Update data CPV */	
	$cpv = array( 				'accglpost'		=> 1,
								'accglno' => $this->session->userdata('sap_obj_key'),
								'accglpostby'	=> $this->session->userdata('uid'),
								'accglpostdate' => date( 'Y-m-d H:i:s'),
								);
								
			$this->fin_model->update_cpv($this->uri->segment(4), $this->uri->segment(5), $cpv);
	
	/* Insert data history PC OP */
			$hpcop = array( 	'idcab'		 => $this->uri->segment(6),
								'sap_ref'	 => $this->session->userdata('sap_obj_key'),
								'rlis_ref'	 => $this->input->post('txt_nocpvform'),
								'amount'	 => $this->fungsi->convert_idr_to_num($this->input->post('txt_totalcpv')),
								'addby'		 => $this->session->userdata('uid'),
								//Postdate
								'postdate'	 => date("Y-m-d",   strtotime($this->input->post('txt_budat'))),
								'adddate' 	 => date( 'Y-m-d H:i:s'),
								);
			$this->acc_model->add_h_pcop($hpcop);
			
			/*Set pesan sukses dan kembali ke menu add cabang*/
			$this->session->set_flashdata('message_ok', 'G/L data posted, Number :'.$this->session->userdata('sap_obj_key'));
			redirect('accounting/acc_gl');
	}
			/*	$query_get_cab = $this->admin_model->get_cab_detail($this->input->post('txt_idcab'));
				foreach ($query_get_cab->result() as $row_cab);
				
				//Jurnal petty cash OP
				$i = $i+1;
				$accountgl[] = array('ITEMNO_ACC' => $i,
								'GL_ACCOUNT' => $row_cab->pcglaccop,
								'PSTNG_DATE' => $this->input->post('txt_bldat'),
								'FIS_PERIOD' => $this->input->post('txt_bldat'),
								'ALLOC_NMBR' => $row_cab->cabshorttxt.date('M').'\''.date('y'),
								'ITEM_TEXT'	 => '4904 -'.strtoupper($row_cab->cabshorttxt).'- P.Cash Oprsnl '.$this->input->post('txt_nocpvform'),
								);
				$curramount[] = array( 'ITEMNO_ACC' => $i,
								'AMT_DOCCUR' => $this->input->post('txt_totalcpv') * -1,
								);
				//Insert saldo
				$i = $i+1;
				$accountgl[] = array( 'ITEMNO_ACC' => $i,
								'GL_ACCOUNT' => $row_cab->pcglaccop,
								'PSTNG_DATE' => date('d.m.Y'),
								'FIS_PERIOD' => date('d.m.Y'),
								'ALLOC_NMBR' => $row_cab->cabshorttxt.date('M').'\''.date('y'),
								'ITEM_TEXT'	 => '4904 -'.strtoupper($row_cab->cabshorttxt).'- P.Cash Oprsnl '.$nocpvform,
								);		
				$curramount[] = array( 		'ITEMNO_ACC' => $i,
											'AMT_DOCCUR' =>$this->input->post('txt_totalcpv'),
											);
				$i = $i+1;
				$accountgl[] = array( 'ITEMNO_ACC' => $i,
								'GL_ACCOUNT' => $row_cab->pcbankglacc,
								'PSTNG_DATE' => date('d.m.Y'),
								'FIS_PERIOD' => date('d.m.Y'),
								'ALLOC_NMBR' => $row_cab->cabshorttxt.date('M').'\''.date('y'),
								'ITEM_TEXT'	 => strtoupper($row_cab->cabshorttxt).'- P.Cash Oprsnl '.$nocpvform,
								);
				$curramount[] = array( 		'ITEMNO_ACC' => $i,
												'AMT_DOCCUR' =>$this->input->post('txt_totalcpv') * -1,
												);*/
				
				
//print_r($accountgl);
//echo '<br>';
//print_r($curramount);
			/*}
			else
			{
			
			$this->postexp_form();
			
			}*/
	
}

}

}