<?php class CT_ms_change_part_list 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(){	
		$this->load->view('ms_change_part/view_ms_change_part_list');
	} 
	
	function view_new_item(){  
		$row_data=isset($_POST['row_data'])?$_POST['row_data']:null; 
		$data['row_data'] =$row_data;  
		$this->load->view('ms_change_part/view_ms_change_part_list_new',$data);
	} 

	function view_item(){  
		$row_data=isset($_POST['row_data'])?$_POST['row_data']:null; 
		$data['row_data'] =$row_data;  
		$this->load->view('ms_change_part/view_ms_change_part_view_item',$data);
	} 
	
	function loaddata(){; 
		$def_col= array();    
		$spt='dbo.MG_MS_OUT_CHANGE_PART_LIST_HEADER';
		
		$def_col['rID']=array("db" => "rID" ,"sc" => "rID","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r1']=array("db" => "r1" ,"sc" => "r15","ctype"=>"int", "bypassvalue"=>"-100");
		$def_col['r2']=array("db" => "r2" ,"sc" => "r2","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r3']=array("db" => "r3" ,"sc" => "r3","ctype"=>"date", "bypassvalue"=>"");
		$def_col['r4']=array("db" => "r4" ,"sc" => "r4","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r5']=array("db" => "r5" ,"sc" => "r5","ctype"=>"text", "bypassvalue"=>"");
		
		$def_col['r6']=array("db" => "r6" ,"sc" => "r6","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r7']=array("db" => "r7" ,"sc" => "r7","ctype"=>"text", "bypassvalue"=>"");
		
		$def_col['r8']=array("db" => "r8" ,"sc" => "r8","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r9']=array("db" => "r9" ,"sc" => "r9","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r10']=array("db" => "r10" ,"sc" => "r10","ctype"=>"date", "bypassvalue"=>"");
		$def_col['r11']=array("db" => "r11" ,"sc" => "r11","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r12']=array("db" => "r12" ,"sc" => "r12","ctype"=>"date", "bypassvalue"=>"");
		$def_col['r13']=array("db" => "r13" ,"sc" => "r13","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r14']=array("db" => "r14" ,"sc" => "r14","ctype"=>"date", "bypassvalue"=>"");
		$def_col['r15']=array("db" => "r15" ,"sc" => "r15","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r16']=array("db" => "r16" ,"sc" => "r16","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r17']=array("db" => "r17" ,"sc" => "r17","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r18']=array("db" => "r18" ,"sc" => "r18","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r19']=array("db" => "r19" ,"sc" => "r19","ctype"=>"int", "bypassvalue"=>"");
 		
 		$search = isset($_POST['_search'])?$_POST['_search']:false; 
		$searchField = isset($_POST['searchField'])?$_POST['searchField']:''; 
		$searchString = isset($_POST['searchString'])?$_POST['searchString']:''; 
		$filters =  isset($_POST['filters'])?$_POST['filters']:''; 
	 	 
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page 
		$limit = isset($_POST['rows'])?$_POST['rows']:10; // get how many rows we want to have into the grid 
		$sidx = isset($_POST['sidx'])?$_POST['sidx']:'rID'; // get index row - i.e. user click to sort 
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction 
  	   
		if ($sord == 'asc') { $sord = '0'; } else if ($sord == 'desc') { $sord = '1'; } 
 		 
		if (array_key_exists($sidx, $def_col)) {  
			$fsidx=$def_col[$sidx];
			$sidx=$fsidx['db'];
		}else{
			$sidx='rID'; //DEFAULT SORT KALU GA ADA
		}
		
		$sidx=$sidx.'='. $sord;
		  
		$xmldoc =  XMLbasic($spt); 
		paramViewJSON($xmldoc, $limit, $page,  $sidx, $sord); 
  		// AddField($xmldoc ,'ms_type_id',$ms_type_id); 
		$adaSearchStatus=0;
		 
		if ($search == 'true'){ 
			if (!empty($filters)){ 
				$array = json_decode($filters); 
				foreach($array->rules as $arr=>$k){ 
					$fcr=$def_col[$k->field]; 
					if ($fcr != null) 
					{ 
						if ($fcr['ctype']=='int'){ 
							$searchString = $k->data; 
							if ($searchString!=$fcr['bypassvalue']) { 
								addSearchNum($xmldoc,$fcr['sc'],$searchString,'2','1');  
								if (($fcr['sc'])=='r1' || ($fcr['sc'])=='r17'){
									$adaSearchStatus=1;
								}
							}
						} 
						elseif ($fcr['ctype']=='text'){ 
							$searchString = $k->data; 
							if ($searchString!=$fcr['bypassvalue']) 
								addSearchText($xmldoc,$fcr['sc'],'1','1',$searchString); 
						}  
						elseif ($fcr['ctype']=='date'){ 
							$searchString = $k->data; 
							if (trim($searchString) != '' ){ 
								$dateArray=explode('/',$searchString);  
								$searchString = $dateArray[2].$dateArray[1].$dateArray[0]; 
							} 
								addSearchDate($xmldoc,$fcr['sc'],$searchString,'1','1'); 
						} 
					} 
				} 
			} 
	   }  
  
		if ($adaSearchStatus==0){
			addSearchNum($xmldoc,'r17','-1','3','-1'); 
		}
  
		$str = $xmldoc->saveXML();   
		// print($str);
		wh_exec_sp_json($xmldoc, $spt,$sts,$des,$rows,$detail);  
  		echo $rows; 
	}

	function loaddata_detail(){; 
		$def_col= array();    
		$spt='dbo.MG_MS_OUT_CHANGE_PART_LIST_DETAIL'; 
		$def_col['rID']=array("db" => "rID" ,"sc" => "rID","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r1']=array("db" => "r1" ,"sc" => "r1","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r2']=array("db" => "r2" ,"sc" => "r2","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r3']=array("db" => "r3" ,"sc" => "r3","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r4']=array("db" => "r4" ,"sc" => "r4","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r5']=array("db" => "r5" ,"sc" => "r5","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r6']=array("db" => "r6" ,"sc" => "r6","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r7']=array("db" => "r7" ,"sc" => "r7","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r8']=array("db" => "r8" ,"sc" => "r8","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r9']=array("db" => "r9" ,"sc" => "r9","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r10']=array("db" => "r10" ,"sc" => "r10","ctype"=>"date", "bypassvalue"=>"");
		$def_col['r11']=array("db" => "r11" ,"sc" => "r11","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r12']=array("db" => "r12" ,"sc" => "r12","ctype"=>"date", "bypassvalue"=>"");
		$def_col['r13']=array("db" => "r13" ,"sc" => "r13","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r14']=array("db" => "r14" ,"sc" => "r14","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r15']=array("db" => "r15" ,"sc" => "r15","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r16']=array("db" => "r16" ,"sc" => "r16","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r_s']=array("db" => "r_s" ,"sc" => "r_s","ctype"=>"int", "bypassvalue"=>"");
		
 		$search = isset($_POST['_search'])?$_POST['_search']:false; 
		$searchField = isset($_POST['searchField'])?$_POST['searchField']:''; 
		$searchString = isset($_POST['searchString'])?$_POST['searchString']:''; 
		$filters =  isset($_POST['filters'])?$_POST['filters']:''; 
	 	 
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page 
		$limit = isset($_POST['rows'])?$_POST['rows']:10; // get how many rows we want to have into the grid 
		$sidx = isset($_POST['sidx'])?$_POST['sidx']:'rID'; // get index row - i.e. user click to sort 
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction 
		
  		$header_id = isset($_POST['header_id'])?$_POST['header_id']:-1; // get the direction 
  	   
		if ($sord == 'asc') { $sord = '0'; } else if ($sord == 'desc') { $sord = '1'; } 
 		 
		if (array_key_exists($sidx, $def_col)) {  
			$fsidx=$def_col[$sidx];
			$sidx=$fsidx['db'];
		}else{
			$sidx='rID'; //DEFAULT SORT KALU GA ADA
		}
		
		$sidx=$sidx.'='. $sord;
		  
		$xmldoc =  XMLbasic($spt); 
		paramViewJSON($xmldoc, $limit, $page,  $sidx, $sord); 
  		AddField($xmldoc ,'header_id',$header_id); 
		 
		if ($search == 'true'){ 
			if (!empty($filters)){ 
				$array = json_decode($filters); 
				foreach($array->rules as $arr=>$k){ 
					$fcr=$def_col[$k->field]; 
					if ($fcr != null) 
					{ 
						if ($fcr['ctype']=='int'){ 
							$searchString = $k->data; 
							if ($searchString!=$fcr['bypassvalue'])  
								addSearchNum($xmldoc,$fcr['sc'],$searchString,'2','1');  
						} 
						elseif ($fcr['ctype']=='text'){ 
							$searchString = $k->data; 
							if ($searchString!=$fcr['bypassvalue']) 
								addSearchText($xmldoc,$fcr['sc'],'1','1',$searchString); 
						}  
						elseif ($fcr['ctype']=='date'){ 
							$searchString = $k->data; 
							if (trim($searchString) != '' ){ 
								$dateArray=explode('/',$searchString);  
								$searchString = $dateArray[2].$dateArray[1].$dateArray[0]; 
							} 
							addSearchDate($xmldoc,$fcr['sc'],$searchString,'1','1'); 
						}  
					} 
				} 
			} 
	   }  
  
		$str = $xmldoc->saveXML(); 
		// print($str);		
		wh_exec_sp_json($xmldoc, $spt,$sts,$des,$rows,$detail);  
		echo $rows; 
	}

	function loaddata_detail_supply(){; 
		$def_col= array();    
		$spt='dbo.MG_MS_OUT_CHANGE_PART_LIST_DETAIL_SUPPLY'; 
		$def_col['rID']=array("db" => "rID" ,"sc" => "rID","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r1']=array("db" => "r1" ,"sc" => "r1","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r2']=array("db" => "r2" ,"sc" => "r2","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r3']=array("db" => "r3" ,"sc" => "r3","ctype"=>"date", "bypassvalue"=>"");
		$def_col['r4']=array("db" => "r4" ,"sc" => "r4","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r5']=array("db" => "r5" ,"sc" => "r5","ctype"=>"date", "bypassvalue"=>"");
		$def_col['r6']=array("db" => "r6" ,"sc" => "r6","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r7']=array("db" => "r7" ,"sc" => "r7","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r8']=array("db" => "r8" ,"sc" => "r8","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r9']=array("db" => "r9" ,"sc" => "r9","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r10']=array("db" => "r10" ,"sc" => "r10","ctype"=>"int", "bypassvalue"=>"");
			
 		$search = isset($_POST['_search'])?$_POST['_search']:false; 
		$searchField = isset($_POST['searchField'])?$_POST['searchField']:''; 
		$searchString = isset($_POST['searchString'])?$_POST['searchString']:''; 
		$filters =  isset($_POST['filters'])?$_POST['filters']:''; 
	 	 
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page 
		$limit = isset($_POST['rows'])?$_POST['rows']:10; // get how many rows we want to have into the grid 
		$sidx = isset($_POST['sidx'])?$_POST['sidx']:'rID'; // get index row - i.e. user click to sort 
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction 
		
 		$ms_part_list_change_part_detail_id = isset($_POST['ms_part_list_change_part_detail_id'])?$_POST['ms_part_list_change_part_detail_id']:-1; // get the direction 
  		   
		if ($sord == 'asc') { $sord = '0'; } else if ($sord == 'desc') { $sord = '1'; } 
 		 
		if (array_key_exists($sidx, $def_col)) {  
			$fsidx=$def_col[$sidx];
			$sidx=$fsidx['db'];
		}else{
			$sidx='rID'; //DEFAULT SORT KALU GA ADA
		}
		
		$sidx=$sidx.'='. $sord;
		  
		$xmldoc =  XMLbasic($spt); 
		paramViewJSON($xmldoc, $limit, $page,  $sidx, $sord); 
  		AddField($xmldoc ,'ms_part_list_change_part_detail_id',$ms_part_list_change_part_detail_id); 
		 
		if ($search == 'true'){ 
			if (!empty($filters)){ 
				$array = json_decode($filters); 
				foreach($array->rules as $arr=>$k){ 
					$fcr=$def_col[$k->field]; 
					if ($fcr != null) 
					{ 
						if ($fcr['ctype']=='int'){ 
							$searchString = $k->data; 
							if ($searchString!=$fcr['bypassvalue'])  
								addSearchNum($xmldoc,$fcr['sc'],$searchString,'2','1');  
						} 
						elseif ($fcr['ctype']=='text'){ 
							$searchString = $k->data; 
							if ($searchString!=$fcr['bypassvalue']) 
								addSearchText($xmldoc,$fcr['sc'],'1','1',$searchString); 
						}  
						elseif ($fcr['ctype']=='date'){ 
							$searchString = $k->data; 
							if (trim($searchString) != '' ){ 
								$dateArray=explode('/',$searchString);  
								$searchString = $dateArray[2].$dateArray[1].$dateArray[0]; 
							} 
							addSearchDate($xmldoc,$fcr['sc'],$searchString,'1','1'); 
						}  
					} 
				} 
			} 
	   }  

		$str = $xmldoc->saveXML();   
		// print_r($str);die();
		wh_exec_sp_json($xmldoc, $spt,$sts,$des,$rows,$detail);  
  		echo $rows; 
	}
	
	function loaddata_source_available(){; 
		$def_col= array();    
		
		$spt='dbo.MG_MS_OUT_CHANGE_PART_LIST_SOURCE_AVAILABLE'; 
		$def_col['rID']=array("db" => "rID" ,"sc" => "rID","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r1']=array("db" => "r1" ,"sc" => "r1","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r2']=array("db" => "r2" ,"sc" => "r2","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r3']=array("db" => "r3" ,"sc" => "r3","ctype"=>"date", "bypassvalue"=>"");
		$def_col['r4']=array("db" => "r4" ,"sc" => "r4","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r5']=array("db" => "r5" ,"sc" => "r5","ctype"=>"date", "bypassvalue"=>"");
		$def_col['r6']=array("db" => "r6" ,"sc" => "r6","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r7']=array("db" => "r7" ,"sc" => "r7","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r8']=array("db" => "r8" ,"sc" => "r8","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r9']=array("db" => "r9" ,"sc" => "r9","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r_u']=array("db" => "r_u" ,"sc" => "r_u","ctype"=>"int", "bypassvalue"=>"");
		
 		$search = isset($_POST['_search'])?$_POST['_search']:false; 
		$searchField = isset($_POST['searchField'])?$_POST['searchField']:''; 
		$searchString = isset($_POST['searchString'])?$_POST['searchString']:''; 
		$filters =  isset($_POST['filters'])?$_POST['filters']:''; 
	 	 
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page 
		$limit = isset($_POST['rows'])?$_POST['rows']:10; // get how many rows we want to have into the grid 
		$sidx = isset($_POST['sidx'])?$_POST['sidx']:'rID'; // get index row - i.e. user click to sort 
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction 
		
  		$ms_part_list_change_part_detail_id = isset($_POST['ms_part_list_change_part_detail_id'])?$_POST['ms_part_list_change_part_detail_id']:-1; // get the direction 
  	   
		if ($sord == 'asc') { $sord = '0'; } else if ($sord == 'desc') { $sord = '1'; } 
 		 
		if (array_key_exists($sidx, $def_col)) {  
			$fsidx=$def_col[$sidx];
			$sidx=$fsidx['db'];
		}else{
			$sidx='rID'; //DEFAULT SORT KALU GA ADA
		}
		
		$sidx=$sidx.'='. $sord;
		  
		$xmldoc =  XMLbasic($spt); 
		paramViewJSON($xmldoc, $limit, $page,  $sidx, $sord); 
  		AddField($xmldoc ,'ms_part_list_change_part_detail_id',$ms_part_list_change_part_detail_id); 
		 
		if ($search == 'true'){ 
			if (!empty($filters)){ 
			$array = json_decode($filters); 
			foreach($array->rules as $arr=>$k){ 
					$fcr=$def_col[$k->field]; 
					if ($fcr != null) 
					{ 
						if ($fcr['ctype']=='int'){ 
							$searchString = $k->data; 
							if ($searchString!=$fcr['bypassvalue'])  
								addSearchNum($xmldoc,$fcr['sc'],$searchString,'2','1');    
						} 
						elseif ($fcr['ctype']=='text'){ 
							$searchString = $k->data; 
							if ($searchString!=$fcr['bypassvalue']) 
								addSearchText($xmldoc,$fcr['sc'],'1','1',$searchString); 
						}  
						elseif ($fcr['ctype']=='date'){ 
							$searchString = $k->data; 
							if (trim($searchString) != '' ){ 
								$dateArray=explode('/',$searchString);  
								$searchString = $dateArray[2].$dateArray[1].$dateArray[0]; 
							} 
								addSearchDate($xmldoc,$fcr['sc'],$searchString,'1','1'); 
						}  
					} 
				} 
			 } 
	   }  
  
		$str = $xmldoc->saveXML();   
		// print_r($str);die();
		wh_exec_sp_json($xmldoc, $spt,$sts,$des,$rows,$detail);  
  		echo $rows; 
	}
	
	function save_header(){
		
		$header_id 									= isset($_POST['header_id'])?$_POST['header_id']:'-1'; // get the requested page 
		$change_part_list_no 					= isset($_POST['change_part_list_no'])?$_POST['change_part_list_no']:''; // get the requested page 
		$change_part_list_date 					= isset($_POST['change_part_list_date'])?$_POST['change_part_list_date']:''; // get the requested page 
		$change_part_list_assets_list_id 	= isset($_POST['change_part_list_assets_list_id'])?$_POST['change_part_list_assets_list_id']:-1; // get the requested page 
		$change_part_list_assets_list_name 	= isset($_POST['change_part_list_assets_list_name'])?$_POST['change_part_list_assets_list_name']:''; // get the requested page
		$change_part_car_line_no 	= isset($_POST['change_part_car_line_no'])?$_POST['change_part_car_line_no']:''; // get the requested page
		
		$SPT=$header_id =='-1'?'dbo.MG_MS_OUT_CHANGE_PART_LIST_HEADER_ADD':'dbo.MG_MS_OUT_CHANGE_PART_LIST_HEADER_EDIT';

		$xmldoc =  XMLbasic('json');  
		AddField($xmldoc ,'header_id',$header_id );
		AddField($xmldoc ,'change_part_list_no',$change_part_list_no);
		AddField($xmldoc ,'change_part_list_date',$change_part_list_date);
		AddField($xmldoc ,'change_part_list_assets_list_id',$change_part_list_assets_list_id);
		AddField($xmldoc ,'change_part_list_assets_list_name',$change_part_list_assets_list_name);
		AddField($xmldoc ,'change_part_car_line_no',$change_part_car_line_no);
		
		$str = $xmldoc->saveXML();
		// print_r($str);die();
		wh_exec_sp_array($xmldoc, $SPT,$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false)
			,'stsno' => $sts
			, 'stsmsg' => $des
			, 'detail' => $detail)
		); 
	}
	
	function auto_supply(){
		
		$detail_id = isset($_POST['detail_id'])?$_POST['detail_id']:'-1'; // get the requested page 
		$mode_supply = isset($_POST['mode_supply'])?$_POST['mode_supply']:'0'; // get the requested page 
		$renew_supply = isset($_POST['renew_supply'])?$_POST['renew_supply']:'0'; // get the requested page 
		
		$SPT='dbo.MG_MS_OUT_PART_LIST_DETAIL_AUTO_SUPPLY';

		$xmldoc =  XMLbasic('json');  
		AddField($xmldoc ,'detail_id',$detail_id ); 
		AddField($xmldoc ,'mode_supply',$mode_supply ); 
		AddField($xmldoc ,'renew_supply',$renew_supply ); 
		
		$str = $xmldoc->saveXML();
		// print_r($str);die();
		wh_exec_sp_array($xmldoc, $SPT,$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false)
			, 'stsno' => $sts
			, 'stsmsg' => $des
			, 'detail' => $detail)
		); 
	}
	
	function auto_supply_all(){  
		$header_id = isset($_POST['header_id'])?$_POST['header_id']:'-1'; // get the requested page 
		$mode_supply = isset($_POST['mode_supply'])?$_POST['mode_supply']:'0'; // get the requested page 
		$renew_supply = isset($_POST['renew_supply'])?$_POST['renew_supply']:'0'; // get the requested page 
	 
		$SPT='dbo.MG_MS_OUT_PART_LIST_HEADER_AUTO_SUPPLY';

		$xmldoc =  XMLbasic('json');  
		AddField($xmldoc ,'ms_part_list_production_header_id',$header_id ); 
		AddField($xmldoc ,'mode_supply',$mode_supply ); 
		AddField($xmldoc ,'renew_supply',$renew_supply ); 
		
		$str = $xmldoc->saveXML();
		// print_r($str);die();
		 wh_exec_sp_array($xmldoc, $SPT,$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false)
			, 'stsno' => $sts
			, 'stsmsg' => $des
			, 'detail' => $detail)
		); 
	}
	
	function approve_header(){  
		$header_id = isset($_POST['header_id'])?$_POST['header_id']:'-1'; // get the requested page 
	  
		$SPT='dbo.MG_MS_OUT_CHANGE_PART_LIST_HEADER_APPROVE';

		$xmldoc =  XMLbasic('json');  
		AddField($xmldoc ,'ms_part_list_change_part_header_id',$header_id ); 
		
		$str = $xmldoc->saveXML();
		// print_r($str);die();
		 wh_exec_sp_array($xmldoc, $SPT,$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false)
			,'stsno' => $sts
			, 'stsmsg' => $des
			, 'detail' => $detail)
		); 
	}

	function cancel_header(){  
		$header_id = isset($_POST['header_id'])?$_POST['header_id']:'-1'; // get the requested page 
		$info = isset($_POST['info'])?$_POST['info']:'-1'; // get the requested page 
	  
		$SPT='dbo.MG_MS_OUT_CHANGE_PART_LIST_HEADER_CANCEL';

		$xmldoc =  XMLbasic('json');  
		AddField($xmldoc ,'ms_part_list_change_part_header_id',$header_id );
		AddField($xmldoc ,'info',$info );
		
		$str = $xmldoc->saveXML();
		// print_r($str);die();
		wh_exec_sp_array($xmldoc, $SPT,$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false)
			,'stsno' => $sts
			, 'stsmsg' => $des
			, 'detail' => $detail)
		); 
	}
	 
	function save_detail(){
		
		$header_id = isset($_POST['header_id'])?$_POST['header_id']:'-1'; // get the requested page 
		$detail_id = isset($_POST['detail_id'])?$_POST['detail_id']:'-1'; // get the requested page 
		$ms_item_id = isset($_POST['ms_item_id'])?$_POST['ms_item_id']:'-1'; // get the requested page 
		$part_no = isset($_POST['part_no'])?$_POST['part_no']:''; // get the requested page 
		$quantity = isset($_POST['qty'])?$_POST['qty']:''; // get the requested page 
		$info = isset($_POST['info'])?$_POST['info']:''; // get the requested page 
		
		$SPT=$detail_id =='-1'?'dbo.MG_MS_OUT_CHANGE_PART_LIST_DETAIL_ADD':'dbo.MG_MS_OUT_CHANGE_PART_LIST_DETAIL_EDIT';
		
		$xmldoc =  XMLbasic('json');  
		AddField($xmldoc ,'header_id',$header_id );
		AddField($xmldoc ,'detail_id',$detail_id );
		AddField($xmldoc ,'ms_item_id',$ms_item_id );
		AddField($xmldoc ,'part_no',$part_no );
		AddField($xmldoc ,'quantity',$quantity );
		AddField($xmldoc ,'info',$info ); 
		
		$str = $xmldoc->saveXML();
		// print_r($str);die();
		wh_exec_sp_array($xmldoc, $SPT ,$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false)
			,'stsno' => $sts
			, 'stsmsg' => $des
			, 'detail' => $detail)
		); 
	}
	
	function change_part_detail(){
		
		$header_id = isset($_POST['header_id'])?$_POST['header_id']:'-1'; // get the requested page 
		$detail_id = isset($_POST['detail_id'])?$_POST['detail_id']:'-1'; // get the requested page 
		$ms_item_id = isset($_POST['ms_item_id'])?$_POST['ms_item_id']:'-1'; // get the requested page 
		$part_no = isset($_POST['part_no'])?$_POST['part_no']:''; // get the requested page 
		$quantity = isset($_POST['qty'])?$_POST['qty']:''; // get the requested page 
		$info = isset($_POST['info'])?$_POST['info']:''; // get the requested page 
		
		$SPT='MG_MS_OUT_CHANGE_PART_LIST_DETAIL_PROCESS';
		
		$xmldoc =  XMLbasic('json');  
		AddField($xmldoc ,'header_id',$header_id );
		AddField($xmldoc ,'detail_id',$detail_id );
		AddField($xmldoc ,'ms_item_id',$ms_item_id );
		AddField($xmldoc ,'part_no',$part_no );
		AddField($xmldoc ,'quantity',$quantity );
		AddField($xmldoc ,'info',$info ); 
		
		$str = $xmldoc->saveXML();
		// print_r($str);die();
		wh_exec_sp_array($xmldoc, $SPT ,$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false)
			,'stsno' => $sts
			, 'stsmsg' => $des
			, 'detail' => $detail)
		); 
	}
	
	function delete_detail(){  
	 	$detail_id = isset($_POST['detail_id'])?$_POST['detail_id']:'-1'; // get the requested page 
		
		$SPT='dbo.MG_MS_OUT_CHANGE_PART_LIST_DETAIL_DELETE';
		
		$xmldoc =  XMLbasic('json');  
		AddField($xmldoc ,'detail_id',$detail_id );
		
		$str = $xmldoc->saveXML();
		// print_r($str);die();
		 wh_exec_sp_array($xmldoc, $SPT ,$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false)
			,'stsno' => $sts
			, 'stsmsg' => $des
			, 'detail' => $detail)
		); 
	}
	
	function save_supply(){  
		$ms_part_list_change_part_detail_id = isset($_POST['ms_part_list_change_part_detail_id'])?$_POST['ms_part_list_change_part_detail_id']:'-1'; // get the requested page 
		$ms_item_in_detail_id = isset($_POST['ms_item_in_detail_id'])?$_POST['ms_item_in_detail_id']:'-1'; // get the requested page 
	 	$ms_part_list_out_id = isset($_POST['ms_part_list_out_id'])?$_POST['ms_part_list_out_id']:'-1'; // get the requested page 
	 	$quantity = isset($_POST['qty'])?$_POST['qty']:''; // get the requested page 
	 	$info = isset($_POST['info'])?$_POST['info']:''; // get the requested page
		$is_sumber = isset($_POST['is_sumber'])?$_POST['is_sumber']:'-1'; // get the requested page 		
	 	
		$SPT=$ms_part_list_out_id =='-1'?'dbo.MG_MS_OUT_CHANGE_PART_LIST_DETAIL_SUPPLY_ADD':'dbo.MG_MS_OUT_CHANGE_PART_LIST_DETAIL_SUPPLY_EDIT';
		
		$xmldoc =  XMLbasic('json');  
		AddField($xmldoc ,'ms_part_list_change_part_detail_id',$ms_part_list_change_part_detail_id );
		AddField($xmldoc ,'ms_item_in_detail_id',$ms_item_in_detail_id );
		AddField($xmldoc ,'ms_part_list_out_id',$ms_part_list_out_id ); 
		AddField($xmldoc ,'quantity',$quantity ); 
		AddField($xmldoc ,'info',$info ); 
		AddField($xmldoc ,'is_sumber',$is_sumber ); 
		
		$str = $xmldoc->saveXML();
		// print_r($str);die();
		 wh_exec_sp_array($xmldoc, $SPT ,$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false)
			,'stsno' => $sts
			, 'stsmsg' => $des
			, 'detail' => $detail)
		); 
	} 
	
	function delete_supply(){  
		$ms_part_list_change_part_detail_id = isset($_POST['ms_part_list_change_part_detail_id'])?$_POST['ms_part_list_change_part_detail_id']:'-1'; // get the requested page 
		$ms_part_list_out_id = isset($_POST['ms_part_list_out_id'])?$_POST['ms_part_list_out_id']:'-1'; // get the requested page 
	 	$ms_item_in_detail_id = isset($_POST['ms_item_in_detail_id'])?$_POST['ms_item_in_detail_id']:'-1'; // get the requested page 
	 	$is_sumber = isset($_POST['is_sumber'])?$_POST['is_sumber']:'-1'; // get the requested page 		
		
		$SPT='MG_MS_OUT_CHANGE_PART_LIST_DETAIL_SUPPLY_DELETE';
		
		$xmldoc =  XMLbasic('json');  
		AddField($xmldoc ,'ms_part_list_change_part_detail_id',$ms_part_list_change_part_detail_id );
		AddField($xmldoc ,'ms_part_list_out_id',$ms_part_list_out_id );
		AddField($xmldoc ,'ms_item_in_detail_id',$ms_item_in_detail_id );  
		AddField($xmldoc ,'is_sumber',$is_sumber ); 
		
		$str = $xmldoc->saveXML();
		// print_r($str);die();
		 wh_exec_sp_array($xmldoc, $SPT ,$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false)
			,'stsno' => $sts
			, 'stsmsg' => $des
			, 'detail' => $detail)
		); 
	} 
	
	// function change_status(){  
			 // $status_id = isset($_POST['status_id'])?$_POST['status_id']:'-1'; // get the requested page 
			 // $status_info = isset($_POST['status_info'])?$_POST['status_info']:''; // get the requested page 
			 // $ms_prod_car_line_id = isset($_POST['ms_prod_car_line_id'])?$_POST['ms_prod_car_line_id']:'-1'; // get the requested page 
			 
			// $xmldoc =  XMLbasic('json'); 
			// AddField($xmldoc ,'ms_prod_car_line_id',$ms_prod_car_line_id ); 
			// AddField($xmldoc ,'status_id',$status_id );
			// AddField($xmldoc ,'status_info',$status_info );
		
			// $str = $xmldoc->saveXML();
			// // print_r($str);die();
			 // wh_exec_sp_array($xmldoc, 'dbo.MG_ms_prod_car_line_CHANGE_STATUS',$sts,$des,$rows,$detail); 
			// echo json_encode(array('valid'=>($sts=='00'?true:false)
			// ,'stsno' => $sts
			// , 'stsmsg' => $des
			// , 'detail' => $detail)
			// ); 
	// }  
	
	function view_ms_upload_part_list_change_part_dialog()
	{
		$ms_change_part_assets_list_edit_item_id=isset($_POST['ms_change_part_assets_list_edit_item_id'])?$_POST['ms_change_part_assets_list_edit_item_id']:null; 
		$ms_change_part_list_edit_part_no=isset($_POST['ms_change_part_list_edit_part_no'])?$_POST['ms_change_part_list_edit_part_no']:null; 
		$data['ms_change_part_assets_list_edit_item_id'] =$ms_change_part_assets_list_edit_item_id; 
		$data['ms_change_part_list_edit_part_no'] =$ms_change_part_list_edit_part_no; 

		$this->load->view('ms_change_part/view_ms_upload_part_list_change_part_dialog', $data);
	}
	
	function upload_file_change_part_list(){
		$assets_list_id = $_POST['change_part_assets_list_id'];
		$part_no = $_POST['change_part_no'];
		
		$file_element_name = 'userfile_change_part_list';
		$error = 0;
		$msg = '';
		$sts = 02;
		
		$apppath = $this->config->item('APP_upload_path');
		$dir = $apppath.'masterdata/';
		$dir .= 'CHANGE PART LIST/';
		if (!is_dir($dir)) {  
			mkdir($dir, 0777);
		}
		
		if (!is_dir($dir)) {  
			mkdir($dir, 0777);
		}
		$dir .= date('Y').'/';
		if (!is_dir($dir)) {  
			mkdir($dir, 0777);
		}
		$dir .= date('m').'/';
		if (!is_dir($dir)) {  
			mkdir($dir, 0777);
		}
		
		$config['overwrite'] = false;
		$config['file_name'] = $_FILES[$file_element_name]['name'];
		$config['upload_path'] = $dir;
		$config['allowed_types'] = 'xls|xlsx';
		$config['max_size'] = '10000000';
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		
		if ($this->upload->do_upload($file_element_name)) {
			$uploaded = $this->upload->data();
			$file_name = $uploaded['file_name'];
			$file_type = $uploaded['file_type'];
			$file_path = $uploaded['file_path'];
			$full_path = $uploaded['full_path'];
			$raw_name = $uploaded['raw_name'];
			$orig_name = $uploaded['orig_name'];
			$client_name = $uploaded['client_name'];
			$file_ext = $uploaded['file_ext'];
			$file_size = $uploaded['file_size'];
			$is_image = $uploaded['is_image'];
			$image_width = $uploaded['image_width'];
			$image_height = $uploaded['image_height'];
			$image_type = $uploaded['image_type'];
			$image_size_str = $uploaded['image_size_str'];
		} else {
			$error++;
			$msg .= $this->upload->display_errors();
		}
		
		if($error == 0){
			$spt='dbo.MG_UPLOAD_PART_LIST_DETAIL'; 
			$xmldoc =  XMLbasic($spt); 
			AddField($xmldoc,'file_name',$file_name);
			AddField($xmldoc,'file_type',$file_type);
			AddField($xmldoc,'file_path',$file_path);
			AddField($xmldoc,'full_path',$full_path);
			AddField($xmldoc,'raw_name',$raw_name);
			AddField($xmldoc,'orig_name',$orig_name);
			AddField($xmldoc,'client_name',$client_name);
			AddField($xmldoc,'file_ext',$file_ext);
			AddField($xmldoc,'file_size',$file_size);
			AddField($xmldoc,'is_image',$is_image);
			AddField($xmldoc,'image_width',$image_width);
			AddField($xmldoc,'image_height',$image_height);
			AddField($xmldoc,'image_type',$image_type);
			AddField($xmldoc,'image_size_str',$image_size_str);
			AddField($xmldoc,'assets_list_id',$assets_list_id);
			AddField($xmldoc,'part_no',$part_no);
			AddField($xmldoc,'file_type_id',1);
			AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));
			
			// connectwebservice($xmldoc->saveXML(), '',$sts,$des,$rows,$detail); 
			$str = $xmldoc->saveXML();   
			// print_r($str);die();
			 wh_exec_sp_json($xmldoc, $spt,$sts,$des,$rows,$detail);  
			// echo $rows; 
			if($sts == '00'){
				$msg = $des;		
			} else {
				$msg = $des;
				@unlink($_FILES[$file_element_name]);
			}			
		} 
		
		$array = array();
		$array['sts'] = $sts;
		$array['des'] = $msg;
		echo json_encode($array);
	}
	
}
 
 