<?php
class Thread extends CI_Controller{
	function __construct()
	{
	  parent::__construct();
		
	}

	function index(){
		$this->load->library('session');
		
		$qry = $this->db->query("
			select id_sa, id_sa_hdr from tbl_sa
			where id_sa_hdr = 93329
		
		");
		foreach ($qry->result() as $r ){
			$session_id = $this->session->userdata('session_id');
			echo "####" . $session_id;
			
			$param = array(
				'id_sa_hdr' => $r->id_sa_hdr,
				'id_sa' => $r->id_sa
			);
			//$res = 	$this->postCURL($param);
			//echo $res . "<br />";
		}
		
			
	}
	
	function proses(){
		
		ini_set('memory_limit', '1024M');
		//set_time_limit(20*60);
		$id_sa_hdr = isset($_POST['id_sa_hdr'])?$_POST['id_sa_hdr']:''; 
		$id_sa = isset($_POST['id_sa'])?$_POST['id_sa']:''; 
		
		$qry = $this->db->query("
			BEGIN TRY

			 exec('[sp_bahan_baku_asal] @id_sa_hdr=$id_sa_hdr, @id_sa=$id_sa  ')

			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
		
		");
		
		
	}
	
	public function postCURL($_param){
		$_url='http://localhost:5758/pemi2/exim/thread/proses';
		
        $postData = '';
       
        foreach($_param as $k => $v) 
        { 
          $postData .= $k . '='.$v.'&'; 
        }
        rtrim($postData, '&');


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, false); 
        curl_setopt($ch, CURLOPT_POST, count($postData));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    

        $output=curl_exec($ch);

        curl_close($ch);

        return $output;
    }
	
	
	
	
  
 }