<?php 
class CT_aw_beli_save extends CI_Controller{
	function __construct()
	{
	  parent::__construct();
		if (!$this->dx_auth->is_logged_in()) {
			header('HTTP/1.1 401 Unauthorize');die('expire'); 
		}
		
		$uid = $this->session->userdata('DX_user_id');
		
		if(!permissionAuth(2) && $uid != RID){ 
			header('HTTP/1.1 401 Unauthorize');die('noaccess');
		} 
	} 
	function index(){	 
				$bc_prefix = isset($_POST['bc_prefix'])?$_POST['bc_prefix']:'-1';   
				$cust_code = isset($_POST['cust_code'])?$_POST['cust_code']:'';  //1 add
				$id_header = isset($_POST['id_header'])?$_POST['id_header']:'-1';   
				$data['cust_code'] = $cust_code;
				$data['id_header'] = $id_header;
				$data['bc_prefix'] = $bc_prefix;
				
				$data['use_kurs'] = '0';
				if ($bc_prefix=='27')
					$data['use_kurs'] = '1';
			
				$bc_prefix = isset($_POST['bc_prefix'])?$_POST['bc_prefix']:'-1';   
			 	$xmldoc =  XMLbasic('json'); 
				AddField($xmldoc ,'bc_prefix',$bc_prefix); 
				$str = $xmldoc->saveXML();  
			    exec_sp_array($xmldoc, 'dbo.GET_SETTING_DEFAULT_CEISA',$sts,$des,$rows,$detail);   
		 		if ($sts=='00'){
					$data['KURS'] = $detail['KURS'];
					$data['VALID_KURS'] = $detail['VALID_KURS'];
					$data['KURS_DATE'] = $detail['KURS_DATE']; 
				}
				else
				{
					$data['KURS'] =1;  
					$data['VALID_KURS'] =0; 
					$data['KURS_DATE'] =''; 
				}
		
		//		$this->load->view('pasi_ceisa_invoice/view_invoice_beli_import', $data);
	}   
	
	function import_data_bc23(){
		$bc_prefix = "23"; // isset($_POST['bc_prefix'])?$_POST['bc_prefix']:die('Type BC tidak di ketahui');; // get the requested page 
		$replace_if_exists = isset($_POST['replace_if_exists'])?$_POST['replace_if_exists']:die('Type BC tidak di ketahui');; // get the requested page 
		$lstsa = isset($_POST['lstsa'])?$_POST['lstsa']:die('pilih penyusun Dokument');; // get the requested page 
  
		$xmldoc =  XMLbasic('json'); 
		 AddField($xmldoc ,'bc_prefix',$bc_prefix);
		 AddField($xmldoc ,'replace_if_exists',$replace_if_exists);
		 AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));

		for ($i = 0; $i <  count($lstsa); $i++) {
			$this->_insert_sa_to_field($xmldoc,$lstsa[$i]);
		} 
		$str = $xmldoc->saveXML();  
		 exec_sp_array($xmldoc, 'dbo.SPX_SYS_BC23_ADD',$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
	}

	function import_data_bc27(){
		$bc_prefix = "27"; // isset($_POST['bc_prefix'])?$_POST['bc_prefix']:die('Type BC tidak di ketahui');; // get the requested page 
		$replace_if_exists = isset($_POST['replace_if_exists'])?$_POST['replace_if_exists']:die('Type BC tidak di ketahui');; // get the requested page 
		$lstsa = isset($_POST['lstsa'])?$_POST['lstsa']:die('pilih penyusun Dokument');; // get the requested page 
  
		$xmldoc =  XMLbasic('json'); 
		 AddField($xmldoc ,'bc_prefix',$bc_prefix);
		 AddField($xmldoc ,'replace_if_exists',$replace_if_exists);
		 AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));

		for ($i = 0; $i <  count($lstsa); $i++) {
			$this->_insert_sa_to_field($xmldoc,$lstsa[$i]);
		} 

		$str = $xmldoc->saveXML();  
		 exec_sp_array($xmldoc, 'dbo.SPX_SYS_BC27_ADD',$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
	}


	function import_data_bc40(){
		$bc_prefix = "40"; // isset($_POST['bc_prefix'])?$_POST['bc_prefix']:die('Type BC tidak di ketahui');; // get the requested page 
		$replace_if_exists = isset($_POST['replace_if_exists'])?$_POST['replace_if_exists']:die('Type BC tidak di ketahui');; // get the requested page 
		$lstsa = isset($_POST['lstsa'])?$_POST['lstsa']:die('pilih penyusun Dokument');; // get the requested page 
  
		$xmldoc =  XMLbasic('json'); 
		 AddField($xmldoc ,'bc_prefix',$bc_prefix);
		 AddField($xmldoc ,'replace_if_exists',$replace_if_exists); 
		 AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));

		for ($i = 0; $i <  count($lstsa); $i++) {
			$this->_insert_sa_to_field($xmldoc,$lstsa[$i]);
		} 
		$str = $xmldoc->saveXML();  
		 exec_sp_array($xmldoc, 'dbo.SPX_SYS_BC40_ADD',$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
	}


	function prosses_data_bc40(){ 
		$bc_prefix = "40"; // isset($_POST['bc_prefix'])?$_POST['bc_prefix']:die('Type BC tidak di ketahui');; // get the requested page 
		$replace_if_exists = isset($_POST['replace_if_exists'])?$_POST['replace_if_exists']:die('Type BC tidak di ketahui');; // get the requested page 
		$lstsa = isset($_POST['lstsa'])?$_POST['lstsa']:die('pilih penyusun Dokument');; // get the requested page 
		$id_header = isset($_POST['id_header'])?$_POST['id_header']:die('pilih penyusun Dokument');;
		$xmldoc =  XMLbasic('json'); 
		 AddField($xmldoc ,'bc_prefix',$bc_prefix);
		 AddField($xmldoc ,'id_header',$id_header);
		 AddField($xmldoc ,'replace_if_exists',$replace_if_exists);
		 AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));

	 
		$str = $xmldoc->saveXML();   
		exec_sp_array($xmldoc, 'dbo.SPX_PO_TO_BC40_PROSES',$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
}

function prosses_data_bc23(){ 
	$bc_prefix = "23"; // isset($_POST['bc_prefix'])?$_POST['bc_prefix']:die('Type BC tidak di ketahui');; // get the requested page 
	$replace_if_exists = isset($_POST['replace_if_exists'])?$_POST['replace_if_exists']:die('Type BC tidak di ketahui');; // get the requested page 
	$lstsa = isset($_POST['lstsa'])?$_POST['lstsa']:die('pilih penyusun Dokument');; // get the requested page 
	$id_header = isset($_POST['id_header'])?$_POST['id_header']:die('pilih penyusun Dokument');;
	$xmldoc =  XMLbasic('json'); 
	 AddField($xmldoc ,'bc_prefix',$bc_prefix);
	 AddField($xmldoc ,'id_header',$id_header);
	 AddField($xmldoc ,'replace_if_exists',$replace_if_exists);
	 AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));

 
	$str = $xmldoc->saveXML();   
	exec_sp_array($xmldoc, 'dbo.SPX_PO_TO_BC23_PROSES',$sts,$des,$rows,$detail); 
	echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
}


function prosses_data_bc27(){ 
	$bc_prefix = "27"; // isset($_POST['bc_prefix'])?$_POST['bc_prefix']:die('Type BC tidak di ketahui');; // get the requested page 
	$replace_if_exists = isset($_POST['replace_if_exists'])?$_POST['replace_if_exists']:die('Type BC tidak di ketahui');; // get the requested page 
	$lstsa = isset($_POST['lstsa'])?$_POST['lstsa']:die('pilih penyusun Dokument');; // get the requested page 
	$id_header = isset($_POST['id_header'])?$_POST['id_header']:die('pilih penyusun Dokument');;
	$xmldoc =  XMLbasic('json'); 
	 AddField($xmldoc ,'bc_prefix',$bc_prefix);
	 AddField($xmldoc ,'id_header',$id_header);
	 AddField($xmldoc ,'replace_if_exists',$replace_if_exists);
	 AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));

 
	$str = $xmldoc->saveXML();   
	exec_sp_array($xmldoc, 'dbo.SPX_PO_TO_BC27_PROSES',$sts,$des,$rows,$detail); 
	echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
}


	function map_bc40_item_auto(){ 
		$bc_prefix = "40"; // isset($_POST['bc_prefix'])?$_POST['bc_prefix']:die('Type BC tidak di ketahui');; // get the requested page 
		$replace_if_exists = isset($_POST['replace_if_exists'])?$_POST['replace_if_exists']:die('Type BC tidak di ketahui');; // get the requested page 
		$lstsa = isset($_POST['lstsa'])?$_POST['lstsa']:die('pilih penyusun Dokument');; // get the requested page 
  
		$xmldoc =  XMLbasic('json'); 
		 AddField($xmldoc ,'bc_prefix',$bc_prefix);
		 AddField($xmldoc ,'replace_if_exists',$replace_if_exists);
		 
		for ($i = 0; $i <  count($lstsa); $i++) {
			$this->_insert_sa_to_field($xmldoc,$lstsa[$i]);
		} 
 

			  $str = $xmldoc->saveXML();
			 // print_r($str);die();
			 exec_sp_array($xmldoc, 'dbo.SPX_MAP_BC40_ITEM_AUTO',$sts,$des,$rows,$detail); 
			echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
	}



	function map_bc27_item_auto(){ 
		$bc_prefix = "27"; // isset($_POST['bc_prefix'])?$_POST['bc_prefix']:die('Type BC tidak di ketahui');; // get the requested page 
		$replace_if_exists = isset($_POST['replace_if_exists'])?$_POST['replace_if_exists']:die('Type BC tidak di ketahui');; // get the requested page 
		$lstsa = isset($_POST['lstsa'])?$_POST['lstsa']:die('pilih penyusun Dokument');; // get the requested page 
  
		$xmldoc =  XMLbasic('json'); 
		 AddField($xmldoc ,'bc_prefix',$bc_prefix);
		 AddField($xmldoc ,'replace_if_exists',$replace_if_exists);
		 
		for ($i = 0; $i <  count($lstsa); $i++) {
			$this->_insert_sa_to_field($xmldoc,$lstsa[$i]);
		} 


			  $str = $xmldoc->saveXML();
			 // print_r($str);die();
			 exec_sp_array($xmldoc, 'dbo.SPX_MAP_BC27_ITEM_AUTO',$sts,$des,$rows,$detail); 
			echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
	}

	function map_bc23_item_auto(){ 
		$bc_prefix = "23"; // isset($_POST['bc_prefix'])?$_POST['bc_prefix']:die('Type BC tidak di ketahui');; // get the requested page 
		$replace_if_exists = isset($_POST['replace_if_exists'])?$_POST['replace_if_exists']:die('Type BC tidak di ketahui');; // get the requested page 
		$lstsa = isset($_POST['lstsa'])?$_POST['lstsa']:die('pilih penyusun Dokument');; // get the requested page 
  
		$xmldoc =  XMLbasic('json'); 
		 AddField($xmldoc ,'bc_prefix',$bc_prefix);
		 AddField($xmldoc ,'replace_if_exists',$replace_if_exists);
		 
		for ($i = 0; $i <  count($lstsa); $i++) {
			$this->_insert_sa_to_field($xmldoc,$lstsa[$i]);
		} 


			  $str = $xmldoc->saveXML();
			 // print_r($str);die();
			 exec_sp_array($xmldoc, 'dbo.SPX_MAP_BC23_ITEM_AUTO',$sts,$des,$rows,$detail); 
			echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
	}



	function map_bc40_item_manual(){ 
		$ID_DETAIL = isset($_POST['ID_DETAIL'])?$_POST['ID_DETAIL']:die('pilih item yang akan di mapping manual');; // get the requested page 
		$map_item_id = isset($_POST['map_item_id'])?$_POST['map_item_id']:die('pilih item yang akan di mapping manual');; // get the requested page 
		$map_type_item_id = isset($_POST['map_type_item_id'])?$_POST['map_type_item_id']:die('pilih item yang akan di mapping manual');; // get the requested page 
		 $xmldoc =  XMLbasic('json'); 
		AddField($xmldoc ,'ID_DETAIL',$ID_DETAIL);
		AddField($xmldoc ,'map_item_id',$map_item_id);
		AddField($xmldoc ,'map_type_item_id',$map_type_item_id);
		  $str = $xmldoc->saveXML();
		 // print_r($str);die();
		 exec_sp_array($xmldoc, 'dbo.SPX_MAP_BC40_ITEM_MANUAL',$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
}

function map_bc23_item_manual(){ 
	$ID_DETAIL = isset($_POST['ID_DETAIL'])?$_POST['ID_DETAIL']:die('pilih item yang akan di mapping manual');; // get the requested page 
	$map_item_id = isset($_POST['map_item_id'])?$_POST['map_item_id']:die('pilih item yang akan di mapping manual');; // get the requested page 
	$map_type_item_id = isset($_POST['map_type_item_id'])?$_POST['map_type_item_id']:die('pilih item yang akan di mapping manual');; // get the requested page 
	 $xmldoc =  XMLbasic('json'); 
	AddField($xmldoc ,'ID_DETAIL',$ID_DETAIL);
	AddField($xmldoc ,'map_item_id',$map_item_id);
	AddField($xmldoc ,'map_type_item_id',$map_type_item_id);
	  $str = $xmldoc->saveXML();
	 // print_r($str);die();
	 exec_sp_array($xmldoc, 'dbo.SPX_MAP_BC23_ITEM_MANUAL',$sts,$des,$rows,$detail); 
	echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
}

function map_bc27_item_manual(){ 
	$ID_DETAIL = isset($_POST['ID_DETAIL'])?$_POST['ID_DETAIL']:die('pilih item yang akan di mapping manual');; // get the requested page 
	$map_item_id = isset($_POST['map_item_id'])?$_POST['map_item_id']:die('pilih item yang akan di mapping manual');; // get the requested page 
	$map_type_item_id = isset($_POST['map_type_item_id'])?$_POST['map_type_item_id']:die('pilih item yang akan di mapping manual');; // get the requested page 
	 $xmldoc =  XMLbasic('json'); 
	AddField($xmldoc ,'ID_DETAIL',$ID_DETAIL);
	AddField($xmldoc ,'map_item_id',$map_item_id);
	AddField($xmldoc ,'map_type_item_id',$map_type_item_id);
	  $str = $xmldoc->saveXML();
	 // print_r($str);die();
	 exec_sp_array($xmldoc, 'dbo.SPX_MAP_BC27_ITEM_MANUAL',$sts,$des,$rows,$detail); 
	echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
}


	function _insert_sa_to_field($doc,$id){
		$param = $doc->getElementsByTagName('param')->item(0);
		$field = $param->getElementsByTagName('field')->item(0);
		$lst = $doc->createElement("lstsa","");
		$field->appendChild($lst); 
		$tmp = $doc->createElement("id",$id);
		$lst->appendChild($tmp); 
	}		
		
	function cancel_document_bc40(){ 
		$id_header = isset($_POST['id_header'])?$_POST['id_header']:die('pilih dokumen yang akan di cancel');; // get the requested page 
		$status_info = isset($_POST['status_info'])?$_POST['status_info']:''; // get the requested page 
  
		$xmldoc =  XMLbasic('json'); 
		AddField($xmldoc ,'id_header',$id_header);
		AddField($xmldoc ,'status_info',$status_info );
		$str = $xmldoc->saveXML();
		 // print_r($str);die();
		 exec_sp_array($xmldoc, 'dbo.SPX_SYS_BC40_CANCEL',$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
}

function cancel_document_bc27(){ 
	$id_header = isset($_POST['id_header'])?$_POST['id_header']:die('pilih dokumen yang akan di cancel');; // get the requested page 
	$status_info = isset($_POST['status_info'])?$_POST['status_info']:''; // get the requested page 

	$xmldoc =  XMLbasic('json'); 
	AddField($xmldoc ,'id_header',$id_header);
	AddField($xmldoc ,'status_info',$status_info );
	$str = $xmldoc->saveXML();
	 // print_r($str);die();
	 exec_sp_array($xmldoc, 'dbo.SPX_SYS_BC27_CANCEL',$sts,$des,$rows,$detail); 
	echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
}

function cancel_document_bc23(){ 
	$id_header = isset($_POST['id_header'])?$_POST['id_header']:die('pilih dokumen yang akan di cancel');; // get the requested page 
	$status_info = isset($_POST['status_info'])?$_POST['status_info']:''; // get the requested page 

	$xmldoc =  XMLbasic('json'); 
	AddField($xmldoc ,'id_header',$id_header);
	AddField($xmldoc ,'status_info',$status_info );
	$str = $xmldoc->saveXML();
	 // print_r($str);die();
	 exec_sp_array($xmldoc, 'dbo.SPX_SYS_BC23_CANCEL',$sts,$des,$rows,$detail); 
	echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
}



function cancel_draft_bc23(){ 
	$id_header = isset($_POST['id_header'])?$_POST['id_header']:'';// die('pilih dokumen yang akan di cancel');; // get the requested page 
	$status_info = isset($_POST['status_info'])?$_POST['status_info']:''; // get the requested page 
	$nomor_ked = isset($_POST['nomor_ked'])?$_POST['nomor_ked']:'';
	$xmldoc =  XMLbasic('json'); 
	AddField($xmldoc ,'id_header',$id_header);
	AddField($xmldoc ,'nomor_ked',$nomor_ked);
	AddField($xmldoc ,'status_info',$status_info );
	$str = $xmldoc->saveXML();
	 // print_r($str);die();
	 exec_sp_array($xmldoc, 'dbo.SPX_SYS_BC23_CANCELDRAFT',$sts,$des,$rows,$detail); 
	echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
}


function cancel_draft_bc27(){ 
	//	$id_header = isset($_POST['id_header'])?$_POST['id_header']:die('pilih dokumen yang akan di cancel');; // get the requested page 
		$id_header = isset($_POST['id_header'])?$_POST['id_header']:'';// die('pilih dokumen yang akan di cancel');; // get the requested page 
		$status_info = isset($_POST['status_info'])?$_POST['status_info']:''; // get the requested page 
		$nomor_ked = isset($_POST['nomor_ked'])?$_POST['nomor_ked']:'';
		
		$xmldoc =  XMLbasic('json'); 
		AddField($xmldoc ,'id_header',$id_header);
		AddField($xmldoc ,'nomor_ked',$nomor_ked);
		AddField($xmldoc ,'status_info',$status_info );
		$str = $xmldoc->saveXML();
		 // print_r($str);die();
		 exec_sp_array($xmldoc, 'dbo.SPX_SYS_BC27_CANCELDRAFT',$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
	}


	function cancel_draft_bc40(){ 
		$id_header = isset($_POST['id_header'])?$_POST['id_header']:'';// die('pilih dokumen yang akan di cancel');; // get the requested page 
		$status_info = isset($_POST['status_info'])?$_POST['status_info']:''; // get the requested page 
		$nomor_ked = isset($_POST['nomor_ked'])?$_POST['nomor_ked']:'';
			
			$xmldoc =  XMLbasic('json'); 
			AddField($xmldoc ,'id_header',$id_header);
			AddField($xmldoc ,'nomor_ked',$nomor_ked);
			AddField($xmldoc ,'status_info',$status_info );
			$str = $xmldoc->saveXML();
			 // print_r($str);die();
			 exec_sp_array($xmldoc, 'dbo.SPX_SYS_BC40_CANCELDRAFT',$sts,$des,$rows,$detail); 
			echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
		}	

 }