<?php
ini_set('short_open_tag', '0');
$job="";
$username="";
$password="";
$tf=""; 
$er=""; 
$lanon=0;	//語言開啟
//ini_set("session.cookie_httponly", 1);
session_start();
require_once 'adm/config/config.php';
require_once 'adm/config/getformin.php';
require_once 'adm/config/createimg.php';
require_once 'adm/config/share.php';
require_once 'adm/capnew.php';
$pdo =  new PDO('mysql:host='.$GLOBALS["dbhost"].';dbname='.$GLOBALS["dbname"], $GLOBALS["dbuser"], $GLOBALS["dbpass"]);
$pdo ->exec("SET NAMES ".$GLOBALS["db_encode"]);

if($_SESSION['intime']){
}else{
	$_SESSION['intime']=date("Y/m/d H:i");
}
if (isset($_REQUEST['_SESSION'])) die("Get lost Muppet!");
if(!empty($job)){
	if(!empty($val)){
		$job($val);
	}else{
		$job();
	}
}
$pdo=null;
function filesitelist($x=''){
	$out[0]="OK";
	$code="check_".$_POST['file'];
	//$indate=$_POST['indate'];
	$file_type = pathinfo($_FILES["val"]["name"], PATHINFO_EXTENSION) ;	
	$fileName ="filesave/". $code.".".$file_type;  //文件名
	$rotate=$_FILES["val"]["tmp_name"];  //原件來源
	move_uploaded_file($rotate,$fileName);//1:源文件名称2:路径/文件名	
	 // 引入函式庫
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			if (strlen($row['0'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行QSD錯誤，錯誤內容:".$row['0'];
			}else{
				$mycount=share_getcount("sitelist WHERE QSD='".$row['0']."'");
				if($mycount>0){
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行QSD資料(跟資料庫)重覆輸入錯誤，錯誤內容:".$row['0'];		
				}					
			}			
			if (strlen($row['1'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行工廠名錯誤，錯誤內容:".$row['1'];
			}	
			if (strlen($row['2'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行工廠代號錯誤，錯誤內容:".$row['2'];
			}				
			if (strlen($row['4'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行產地(地址)錯誤，錯誤內容:".$row['4'];
			}

			if (strlen($row['5'])==0){
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行COO資料錯誤，錯誤內容:".$row['5'];
			}
		}	
	}	
	$out[2]= $rowindex-1;
	if ($out[0]=="OK")   
	{
		share_del("sitelisttemp");		
		$a=0;
		foreach($arr_content as $row){
			$a++;
			if ($a>1){
				share_insert("sitelisttemp","QSD,thisname, thiscode, another, thisaddress,  COO, lastadmin","'".$row['0']."','".$row['1']."','".$row['2']."','".$row['3']."','".$row['4']."','".$row['5']."','".$_SESSION['admuserid']."'");
				
				$mycount=share_getcount("sitelisttemp WHERE QSD='".$row['0']."'");
				if($mycount>1){
					$out[0]="ERR";						
					$out[1].="<BR>對不起第".$a."行資料(跟excel)重覆輸入錯誤"; 
				}
				$mycount=share_getcount("sitelist WHERE QSD='".$row['0']."'");				
				if($mycount>0){
					$out[0]="ERR";						
					$out[1].="<BR>對不起第".$a."行資料(跟資料庫)重覆輸入錯誤"; 
				}		
			}
		}				
		$out[3]=share_getcount("sitelisttemp");		
		if ($out[0]=="OK")   
		{
			if ($out[2]==$out[3])
			{
				share_getfree("insert into sitelist ( thisname, thiscode, another, thisaddress, QSD, COO, lastadmin) select  thisname, thiscode, another, thisaddress, QSD, COO, lastadmin from sitelisttemp");			
				share_del("sitelisttemp");	
			}else{
				$out[0]="ERR";
				$out[1].="<BR>對不起,上傳資料筆數".$out[2]."，資料上傳筆數".$out[3]; 
					
			}
		}
	}
	echo json_encode($out);	
}
	
function filelicenselist($x=''){
	$out[0]="OK";
	$code="check_".$_POST['file'];
	//$indate=$_POST['indate'];
	$file_type = pathinfo($_FILES["val"]["name"], PATHINFO_EXTENSION) ;	
	$fileName ="filesave/". $code.".".$file_type;  //文件名
	$rotate=$_FILES["val"]["tmp_name"];  //原件來源
	move_uploaded_file($rotate,$fileName);//1:源文件名称2:路径/文件名	
	 // 引入函式庫
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			if (strlen($row['0'])==0)
			{
				if ($row['1']=="I"){
					
				}else{
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行QSD錯誤，錯誤內容:".$row['0'];
				}
			}else{
				$mycount=share_getcount("sitelist WHERE QSD='".$row['0']."'");
				if($mycount>0){
	
				}else{
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行QSD資料輸入錯誤,工廠查無這筆資料，錯誤內容:".$row['0'];					
				}					
			}			
			if (strlen($row['1'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行類別錯誤，錯誤內容:".$row['1'];
			}	
			if (strlen($row['2'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行許可證號錯誤，錯誤內容:".$row['2'];
			}else{
				$mycount=share_getcount("licenselist WHERE licensenumber='".trim($row['2'])."'");
				if($mycount>0){
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行本許可證號已建立過，錯誤內容:".$row['2'];	
				}			
			}			
			if (strlen($row['3'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行台灣許可證號錯誤，錯誤內容:".$row['3'];
			}			
			
			if (strlen($row['4'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行許可證中文名稱錯誤，錯誤內容:".$row['4'];
			}
			if (strlen($row['5'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行許可證英文名稱錯誤，錯誤內容:".$row['5'];
			}
			if (strlen($row['6'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行製造商名稱錯誤，錯誤內容:".$row['6'];
			}			
			if (strlen($row['7'])==0){
			}else{
				if(isDate($row['7'])){

				}else{
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行發證日期資料錯誤，錯誤內容:".$row['7'];
				}		
			}
			if (strlen($row['8'])==0){
			}else{			
				if(isDate($row['8'])){

				}else{
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行許可證截止日資料錯誤，錯誤內容:".$row['8'];
				}	
			}
		}	
	}	
	$out[2]= $rowindex-1;
	if ($out[0]=="OK")   
	{
		share_del("licenselisttemp");		
		$a=0;
		foreach($arr_content as $row){
			$a++;
			if ($a>1){
				$tb=share_getinfo("sitelist","QSD",$row['0']);
				share_insert("licenselisttemp","`SID`, `classsification`, `licensenumber`, `clicensenumber`, `clicensename`, `clicensenameen`, `manufacturer`, `issueddate`, `expdate`,lastadmin","'".$tb['thisid']."','".$row['1']."','".trim($row['2'])."','".$row['3']."','".$row['4']."','".$row['5']."','".$row['6']."','".$row['7']."','".$row['8']."','".$_SESSION['admuserid']."'");
				
				$mycount=share_getcount("licenselisttemp WHERE licensenumber='".trim($row['2'])."'");
				if($mycount>1){
					$out[0]="ERR";						
					$out[1].="<BR>對不起第".$a."行資料(跟excel)重覆輸入錯誤"; 
				}
				$mycount=share_getcount("licenselist WHERE licensenumber='".trim($row['2'])."'");				
				if($mycount>0){
					$out[0]="ERR";						
					$out[1].="<BR>對不起第".$a."行資料(跟資料庫)重覆輸入錯誤"; 
				}		
			}
		}				
		$out[3]=share_getcount("licenselisttemp");		
		if ($out[0]=="OK")   
		{
			if ($out[2]==$out[3])
			{
				share_getfree("insert into licenselist (  `SID`, `classsification`, `licensenumber`, `clicensenumber`, `clicensename`, `clicensenameen`, `manufacturer`, `issueddate`, `expdate`,lastadmin) select   `SID`, `classsification`, `licensenumber`, `clicensenumber`, `clicensename`, `clicensenameen`, `manufacturer`, `issueddate`, `expdate`,lastadmin from licenselisttemp");			
				share_del("licenselisttemp");	
			}else{
				$out[0]="ERR";
				$out[1].="<BR>對不起,上傳資料筆數".$out[2]."，資料上傳筆數".$out[3]; 
					
			}
		}
	}
	echo json_encode($out);	
}



function fileproductlist($x=''){
	$out[0]="OK";
	$code="check_".$_POST['file'];
	//$indate=$_POST['indate'];
	$file_type = pathinfo($_FILES["val"]["name"], PATHINFO_EXTENSION) ;	
	$fileName ="filesave/". $code.".".$file_type;  //文件名
	$rotate=$_FILES["val"]["tmp_name"];  //原件來源
	move_uploaded_file($rotate,$fileName);//1:源文件名称2:路径/文件名	
	 // 引入函式庫
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			/*
			if (strlen($row['0'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行system model錯誤，錯誤內容:".$row['0'];
			}
*/			
			if (strlen($row['1'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行產品號碼錯誤，錯誤內容:".$row['1'];
			}else{
				$mycount=share_getcount("productlist WHERE PN='".$row['1']."'");
				if($mycount>0){
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行產品號碼資料(跟資料庫)重覆輸入錯誤，錯誤內容:".$row['1'];		
				}					
			}		
			if (strlen($row['2'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行產品英文名稱錯誤，錯誤內容:".$row['2'];
			}	
/*			
			if (strlen($row['3'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行產品名稱錯誤，錯誤內容:".$row['3'];
			}			
			if (strlen($row['4'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行產品中文名稱錯誤，錯誤內容:".$row['4'];
			}
			if (strlen($row['5'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行最小銷售單位錯誤，錯誤內容:".$row['5'];
			}
			if (strlen($row['6'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行最小銷售單位數值錯誤，錯誤內容:".$row['6'];
			}
			if (strlen($row['7'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行最小銷售單位內含個數錯誤，錯誤內容:".$row['7'];
			}		
*/			
		}	
	}	
	$out[2]= $rowindex-1;
	if ($out[0]=="OK")   
	{
		share_del("productlisttemp");		
		$a=0;
		foreach($arr_content as $row){
			$a++;
			if ($a>1){
				share_insert("productlisttemp","model,PN, description, cname,minqty, psizenumber, psize","'".$row['0']."','".$row['1']."','".$row['2']."','".$row['3']."','".$row['4']."','".$row['5']."','".$row['6']."'");
				
				$mycount=share_getcount("productlisttemp WHERE PN='".$row['1']."'");
				if($mycount>1){
					$out[0]="ERR";						
					$out[1].="<BR>對不起第".$a."行資料(跟excel)重覆輸入錯誤"; 
				}
				$mycount=share_getcount("productlist WHERE PN='".$row['1']."'");				
				if($mycount>0){
					$out[0]="ERR";						
					$out[1].="<BR>對不起第".$a."行資料(跟資料庫)重覆輸入錯誤"; 
				}		
			}
		}				
		$out[3]=share_getcount("productlisttemp");		
		if ($out[0]=="OK")   
		{
			if ($out[2]==$out[3])
			{
				share_getfree("insert into productlist ( model,PN, description, cname,minqty, psize, psizenumber) select model,PN, description, cname,minqty, psize, psizenumber from productlisttemp");			
				share_del("productlisttemp");	
			}else{
				$out[0]="ERR";
				$out[1].="<BR>對不起,上傳資料筆數".$out[2]."，資料上傳筆數".$out[3]; 
					
			}
		}
	}
	echo json_encode($out);	
}

function filecustomerlist($x=''){
	$out[0]="OK";
	
	$code="check_".$_POST['file'];
	//$indate=$_POST['indate'];
	$file_type = pathinfo($_FILES["val"]["name"], PATHINFO_EXTENSION) ;	
	$fileName ="filesave/". $code.".".$file_type;  //文件名
	$rotate=$_FILES["val"]["tmp_name"];  //原件來源
	move_uploaded_file($rotate,$fileName);//1:源文件名称2:路径/文件名	
	 // 引入函式庫
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			if (strlen($row['0'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行客戶全名錯誤，錯誤內容:".$row['0'];
			}else{
				$mycount=share_getcount("customerlist WHERE thisname='".$row['0']."'");
				if($mycount>0){
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行客戶全名資料(跟資料庫)重覆輸入錯誤，錯誤內容:".$row['0'];		
				}					
			}			
		}	
	}	
	$out[2]= $rowindex-1;
	if ($out[0]=="OK")   
	{
		share_del("customerlisttemp");		
		$a=0;
		foreach($arr_content as $row){
			$a++;
			if ($a>1){
				share_insert("customerlisttemp","thisname","'".$row['0']."'");
				
				$mycount=share_getcount("customerlisttemp WHERE thisname='".$row['0']."'");
				if($mycount>1){
					$out[0]="ERR";						
					$out[1].="<BR>對不起第".$a."行資料(跟excel)重覆輸入錯誤"; 
				}
				$mycount=share_getcount("customerlist WHERE thisname='".$row['0']."'");				
				if($mycount>0){
					$out[0]="ERR";						
					$out[1].="<BR>對不起第".$a."行資料(跟資料庫)重覆輸入錯誤"; 
				}		
			}
		}				
		$out[3]=share_getcount("customerlisttemp");		
		if ($out[0]=="OK")   
		{
			if ($out[2]==$out[3])
			{
				share_getfree("insert into customerlist ( thisname) select  thisname from customerlisttemp");			
				share_del("customerlisttemp");	
			}else{
				$out[0]="ERR";
				$out[1].="<BR>對不起,上傳資料筆數".$out[2]."，資料上傳筆數".$out[3]; 
					
			}
		}
	}
	echo json_encode($out);	
}
function filecontract($x=''){
	$out[0]="OK";
	$code="check_".$_POST['file'];
	//$indate=$_POST['indate'];
	$file_type = pathinfo($_FILES["val"]["name"], PATHINFO_EXTENSION) ;	
	$fileName ="filesave/". $code.".".$file_type;  //文件名
	$rotate=$_FILES["val"]["tmp_name"];  //原件來源
	move_uploaded_file($rotate,$fileName);//1:源文件名称2:路径/文件名	
	 // 引入函式庫
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			if (strlen($row['0'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行客戶名稱錯誤，錯誤內容:".$row['0'];
			}else{
				$mycount=share_getcount("customerlist WHERE thisname='".$row['0']."'");
				if($mycount>0){
	
				}else{
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行客戶名稱資料輸入錯誤,客戶名稱查無這筆資料，錯誤內容:".$row['0'];					
				}					
			}			
			if (strlen($row['1'])==0)
			{
				$tb=share_getinfo("customerlist","thisname",$row['0']);
				$mycount=share_getcount("c_contract WHERE thisname='無合約-".$tb['thisid']."'");
				if($mycount>0){				
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行本客戶名稱已建立過無合約資料，錯誤內容:".$row['1'];
				}
			}else{
				$mycount=share_getcount("c_contract WHERE thisname='".$row['1']."'");
				if($mycount>0){
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行本合約名稱已建立過，錯誤內容:".$row['1'];	
				}			
			}			
			if (strlen($row['2'])==0){
			}else{
				if(isDate($row['2'])){

				}else{
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行開始日期資料錯誤，錯誤內容:".$row['2'];
				}		
			}
			if (strlen($row['3'])==0){
			}else{			
				if(isDate($row['3'])){

				}else{
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行截止日期資料錯誤，錯誤內容:".$row['3'];
				}	
			}			
		}	
	}	
	$out[2]= $rowindex-1;
	if ($out[0]=="OK")   
	{
		share_del("c_contracttemp");		
		$a=0;
		foreach($arr_content as $row){
			$a++;
			if ($a>1){
				$tb=share_getinfo("customerlist","thisname",$row['0']);
				if (strlen($row['1'])==0)	{
					share_insert("c_contracttemp","`CID`, `thisname`, `issueddate`, `expdate`","'".$tb['thisid']."','無合約-".$tb['thisid']."','".$row['2']."','".$row['3']."'");					
				}else{		
					share_insert("c_contracttemp","`CID`, `thisname`, `issueddate`, `expdate`","'".$tb['thisid']."','".$row['1']."','".$row['2']."','".$row['3']."'");
				}
				$mycount=share_getcount("c_contracttemp WHERE thisname='".$row['1']."'");
				if($mycount>1){
					$out[0]="ERR";						
					$out[1].="<BR>對不起第".$a."行資料(跟excel)重覆輸入錯誤"; 
				}
				$mycount=share_getcount("c_contract WHERE thisname='".$row['1']."'");				
				if($mycount>0){
					$out[0]="ERR";						
					$out[1].="<BR>對不起第".$a."行資料(跟資料庫)重覆輸入錯誤"; 
				}		
				
				$mycount=share_getcount("c_contracttemp WHERE thisname='無合約-".$tb['thisid']."'");
				if($mycount>1){				
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行資料(跟excel)重覆輸入錯誤"; 
				}	
				$mycount=share_getcount("c_contract WHERE thisname='無合約-".$tb['thisid']."'");
				if($mycount>0){				
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行資料(跟資料庫)重覆輸入錯誤"; 
				}					
			}
		}				
		$out[3]=share_getcount("c_contracttemp");		
		if ($out[0]=="OK")   
		{
			if ($out[2]==$out[3])
			{
				share_getfree("insert into c_contract (  `CID`, `thisname`, `issueddate`, `expdate`) select   `CID`, `thisname`, `issueddate`, `expdate` from c_contracttemp");			
				share_del("c_contracttemp");	
			}else{
				$out[0]="ERR";
				$out[1].="<BR>對不起,上傳資料筆數".$out[2]."，資料上傳筆數".$out[3]; 
					
			}
		}
	}
	echo json_encode($out);	
}
function filelicenseproduct($x=''){
	$out[0]="OK";
	share_del("licenseproducttemp");
	$code="check_".$_POST['file'];
	//$indate=$_POST['indate'];
	$file_type = pathinfo($_FILES["val"]["name"], PATHINFO_EXTENSION) ;	
	$fileName ="filesave/". $code.".".$file_type;  //文件名
	$rotate=$_FILES["val"]["tmp_name"];  //原件來源
	move_uploaded_file($rotate,$fileName);//1:源文件名称2:路径/文件名	
	 // 引入函式庫
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			$myOK="OK";
			$myadd=0;
			if (strlen($row['0'])==0)
			{
				$myOK="ERR";
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行Status錯誤，錯誤內容:".$row['0'];
			}else{
				if (strtolower($row['0'])=="inactive"){
						$inactive="1";
						$mydate=date("Y/m/d");			
				}elseif (strtolower($row['0'])=="active"){
						$inactive="0";
						$mydate="";					
				}else{
					$myOK="ERR";
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行Status填寫錯誤，請填寫Inactive 或 Active，錯誤內容:".$row['0'];					
				}
			}			
			if (strlen($row['1'])==0)
			{
				$myOK="ERR";
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行PN錯誤，錯誤內容:".$row['1'];
			}else{
				$mycount=share_getcount("productlist WHERE PN='".$row['1']."'");
				if($mycount>0){

				}else{
					$myOK="ERR";
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行PN資料輸入錯誤,產品查無這筆資料，請先去建立產品基本資料，錯誤內容:".$row['1'];								
				}
			}				
			
			if (strlen($row['4'])==0)
			{		
				$myOK="ERR";
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行License Number錯誤，錯誤內容:".$row['4'];
			}else{
				if ($row['4']=="NMD"){
				
				}else{
					$myadd=1;
					$mycount=share_getcount("licenselist WHERE licensenumber='".trim($row['4'])."'");
					if($mycount>0){

					}else{
						$myOK="ERR";
						$out[0]="ERR";
						$out[1].="<BR>對不起第".$a."行License Number資料輸入錯誤,許可證查無這筆資料，請先去建立許可證號基本資料，錯誤內容:".$row['4'];
					}
				}
			}
			if ($myOK=="OK"){
				if ($myadd==1){
					$ta=share_getinfo("productlist","PN",trim($row['1']));
					$tb=share_getinfo("licenselist","licensenumber",trim($row['4']));
					$mycount=share_getcount("licenseproduct WHERE  PID='".$ta['thisid']."' and  LID='".$tb['thisid']."' ");
					if($mycount>0){	
						$me=share_gettable("licenseproduct WHERE  PID='".$ta['thisid']."' and  LID='".$tb['thisid']."'");
				
						if ($me[0]['isactive']==0){
							if ($inactive==1){	
								share_update("licenseproduct","	isactive='".$inactive."',`lastadmin`='".$_SESSION['admuserid']."'","thisid='".$me[0]['thisid']."'");

							}	 					
						}							
					/*
						$me=share_gettable("licenseproduct WHERE  PID='".$ta['thisid']."' and  LID='".$tb['thisid']."'");
						if ($me[0]['isactive']==0){
							if ($inactive==1){
								$out[0]="ERR";
								$out[1].="<BR>對不起第".$a."行資料更新成Inactive，請確認是否要更新，謝謝";		
								share_insert("licenseproducttemp","thisid,`isactive`, `PID`, `LID`, `closetime`,QSD, lastadmin","'".$me[0]['thisid']."','".$inactive."','".$ta['thisid']."','".$tb['thisid']."','".$mydate."','".$row['15']."','".$_SESSION['admuserid']."'");
							}						
						}							
						*/	
							
					}else{
						share_insert("licenseproduct","`isactive`, `PID`, `LID`, `closetime`,QSD, lastadmin","'".$inactive."','".$ta['thisid']."','".$tb['thisid']."','".$mydate."','".$row['15']."','".$_SESSION['admuserid']."'");
					}
				}
			}
		}	
	}	
	echo json_encode($out);	
}


function filelicenseproductold($x=''){
	$out[0]="OK";
	$code="check_".$_POST['file'];
	//$indate=$_POST['indate'];
	$file_type = pathinfo($_FILES["val"]["name"], PATHINFO_EXTENSION) ;	
	$fileName ="filesave/". $code.".".$file_type;  //文件名
	$rotate=$_FILES["val"]["tmp_name"];  //原件來源
	move_uploaded_file($rotate,$fileName);//1:源文件名称2:路径/文件名	
	 // 引入函式庫
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			
			if (strlen($row['0'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行Status錯誤，錯誤內容:".$row['0'];
			}else{
				if (strtolower($row['0'])=="inactive"){
					
				}elseif (strtolower($row['0'])=="active"){
					
				}else{
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行Status填寫錯誤，請填寫Inactive 或 Active，錯誤內容:".$row['0'];					
				}
			}			
			if (strlen($row['1'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行PN錯誤，錯誤內容:".$row['1'];
			}else{
				$mycount=share_getcount("productlist WHERE PN='".$row['1']."'");
				if($mycount>0){

				}else{
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行PN資料輸入錯誤,產品查無這筆資料，請先去建立產品基本資料，錯誤內容:".$row['1'];								
				}
			}				
			
			if (strlen($row['4'])==0)
			{		
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行License Number錯誤，錯誤內容:".$row['4'];
			}else{
				if ($row['4']=="NMD"){
				
				}else{
					$mycount=share_getcount("licenselist WHERE licensenumber='".trim($row['4'])."'");
					if($mycount>0){

					}else{
						$out[0]="ERR";
						$out[1].="<BR>對不起第".$a."行License Number資料輸入錯誤,許可證查無這筆資料，請先去建立許可證號基本資料，錯誤內容:".$row['4'];
					}
					/*
					$tb=share_getinfo("licenselist","licensenumber",trim($row['4']));
					$tc=share_getinfo("sitelist","thisid",$tb['SID']);
					if (strlen($row['15'])==0)
					{	
						if ($tc['QSD']=='NA'){
						
						}else{
							$out[0]="ERR";
							$out[1].="<BR>對不起第".$a."行QSD Number資料輸入錯誤,本許可證".$row['4']."所屬QSD錯誤，錯誤內容:".$row['15'];							
						}
					}else{
						if ($tc['QSD']==$row['15']){
							
						}else{
							$out[0]="ERR";
							$out[1].="<BR>對不起第".$a."行QSD Number資料輸入錯誤,本許可證".$row['4']."所屬QSD錯誤，錯誤內容:".$row['15'];						
						}
					}*/
					
				}
			}		
		}	
	}	
	$out[2]= $rowindex-1;
	if ($out[0]=="OK")   
	{
		share_del("licenseproducttemp");		
		$a=0;
		foreach($arr_content as $row){
			$a++;
			if ($a>1){
			 if (strlen($row['4'])==0)
			 {		

		  	 }else{				
				if ($row['4']=="NMD"){
					
				}else{				
					
					if (strtolower($row['0'])=="active"){
						$inactive="1";
						$mydate="";
					}elseif (strtolower($row['0'])=="inactive"){
						$inactive="0";
						$mydate=date("Y/m/d");
					}				
					$ta=share_getinfo("productlist","PN",trim($row['1']));
					$tb=share_getinfo("licenselist","licensenumber",trim($row['4']));
					$mycount=share_getcount("licenseproducttemp WHERE  PID='".$ta['thisid']."' and  LID='".$tb['thisid']."' ");
					if($mycount>0){				
						$me=share_gettable("licenseproducttemp WHERE  PID='".$ta['thisid']."' and  LID='".$tb['thisid']."'");
						if ($me[0]['isactive']==1){
							if ($inactive==0){

								$out[0]="ERR1";
								$out[1].="<BR>對不起第".$a."行資料更新成Inactive，請確認是否要更新，謝謝";		
	//							share_update("licenseproducttemp","closetime=now(),`lastadmin`='".$_SESSION['admuserid']."'","thisid='".$me[0]['thisid']."'");

								
							}						
						}
						
					}else{
						
						share_insert("licenseproducttemp","`isactive`, `PID`, `LID`, `closetime`,QSD, lastadmin","'".$inactive."','".$ta['thisid']."','".$tb['thisid']."','".$mydate."','".$row['15']."','".$_SESSION['admuserid']."'");	
					}
				 }
				}
				
			}
		}				

	}
	echo json_encode($out);	
}


function filecontractlist($x=''){
	$out[0]="OK";
	$code="check_".$_POST['file'];
	//$indate=$_POST['indate'];
	$file_type = pathinfo($_FILES["val"]["name"], PATHINFO_EXTENSION) ;	
	$fileName ="filesave/". $code.".".$file_type;  //文件名
	$rotate=$_FILES["val"]["tmp_name"];  //原件來源
	move_uploaded_file($rotate,$fileName);//1:源文件名称2:路径/文件名	
	 // 引入函式庫
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			
			if (strlen($row['2'])==0)
			{
				$myOK="ERR";
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行PN錯誤，錯誤內容:".$row['2'];
			}else{
				$mycount=share_getcount("productlist WHERE PN='".$row['2']."'");
				if($mycount>0){

				}else{
					$myOK="ERR";
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行PN資料輸入錯誤,產品查無這筆資料，請先去建立產品基本資料，錯誤內容:".$row['2'];								
				}
			}				



			if (strlen($row['8'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行許可證號錯誤，錯誤內容:".$row['8'];
			}else{
				$mycount=share_getcount("licenselist WHERE licensenumber='".trim($row['8'])."'");
				if($mycount>0){
					
				}else{					
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行許可證查無這筆資料，請先去建立許可證號基本資料，錯誤內容:".$row['8'];	
				}			
			}	
		}	
	}	
	$out[2]= $rowindex-1;
	if ($out[0]=="OK")   
	{
		share_del("c_contractproducttemp");		
		$a=0;
		foreach($arr_content as $row){
			$a++;
			if ($a>1){
				$ta=share_getinfo("productlist","PN",trim($row['2']));
				$tb=share_getinfo("licenselist","licensenumber",trim($row['8']));

				share_insert("c_contractproducttemp","`CCID`, `PID`, `LID`, `Material`, `lastadmin`","'".$_POST['file']."','".$ta['thisid']."','".$tb['thisid']."','".$row['9']."','".$_SESSION['admuserid']."'");

				$mycount=share_getcount("c_contractproducttemp WHERE CCID='".$_POST['file']."' and PID='".$ta['thisid']."' and LID='".$tb['thisid']."'");
				if($mycount>1){
					$out[0]="ERR";						
					$out[1].="<BR>對不起第".$a."行資料(跟excel)重覆輸入錯誤"; 
				}
				$mycount=share_getcount("c_contractproduct WHERE CCID='".$_POST['file']."' and PID='".$ta['thisid']."' and LID='".$tb['thisid']."'");			
				if($mycount>0){
					$out[0]="ERR";						
					$out[1].="<BR>對不起第".$a."行資料(跟資料庫)重覆輸入錯誤"; 
				}		
			}
		}				
		$out[3]=share_getcount("c_contractproducttemp");		
		if ($out[0]=="OK")   
		{
			if ($out[2]==$out[3])
			{
				share_getfree("insert into c_contractproduct ( `CCID`, `PID`, `LID`, `Material`, `lastadmin`) select  `CCID`, `PID`, `LID`, `Material`, `lastadmin` from c_contractproducttemp");			
				share_del("c_contractproducttemp");	
			}else{
				$out[0]="ERR";
				$out[1].="<BR>對不起,上傳資料筆數".$out[2]."，資料上傳筆數".$out[3]; 
					
			}
		}
	}
	echo json_encode($out);	
}

function fileproductup($x=''){
	$out[0]="OK";
	$code="check_".$_POST['file'];
	//$indate=$_POST['indate'];
	$file_type = pathinfo($_FILES["val"]["name"], PATHINFO_EXTENSION) ;	
	$fileName ="filesave/". $code.".".$file_type;  //文件名
	$rotate=$_FILES["val"]["tmp_name"];  //原件來源
	move_uploaded_file($rotate,$fileName);//1:源文件名称2:路径/文件名	
	 // 引入函式庫
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			if (strlen($row['0'])==0)
			{
				$myOK="ERR";
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行PN錯誤，錯誤內容:".$row['0'];
//echo "<BR>對不起第".$a."行PN錯誤，錯誤內容:".$row['0'];		
			}else{
				$mycount=share_getcount("productlist WHERE PN='".$row['0']."'");
				if($mycount>0){

				}else{
					$myOK="ERR";
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行PN資料輸入錯誤,產品查無這筆資料，請先去建立產品基本資料，錯誤內容:".$row['0'];	
//echo "<BR>對不起第".$a."行PN資料輸入錯誤,產品查無這筆資料，請先去建立產品基本資料，錯誤內容:".$row['0'];					
				}
			}	
			$mycheck=0;
		
			if(isDate($row['5'])){

			}else{
				$mycheck++;
//				echo "<BR>對不起第".$a."行結束生產日期(預計時間)，錯誤內容:".$row['5'];						
			}	
			if(isDate($row['7'])){

			}else{
				$mycheck++;
//				echo "<BR>對不起第".$a."行開始生產日期(預計時間)，錯誤內容:".$row['7'];						
			}	
			if (strlen($row['4'])==0)
			{
				$mycheck++;
//				echo "<BR>對不起第".$a."行對應的工廠ID(原)不可空白，錯誤內容:".$row['4'];					
			}else{
				$mycount=share_getcount("`sitelist` WHERE `thiscode` in ('".$row['4']."') or `another` in ('".$row['4']."')");
				if($mycount>0){
	
				}else{
					$mycheck++;
//echo "<BR>對不起第".$a."行對應的工廠ID(原)錯需新增，錯誤內容:".$row['4'];	
//					$out[0]="ERR";
//					$out[1].="<BR>對不起第".$a."行，工廠查無此筆資料，請先去建立工廠基本資料，錯誤內容:".$row['4'];					
				}					
			}	
			if (strlen($row['6'])==0)
			{
				$mycheck++;
//echo "<BR>對不起第".$a."行對應的對應的工廠ID(新)不可空白，錯誤內容:".$row['6'];					
			}else{
				$mycount=share_getcount("`sitelist` WHERE `thiscode` in ('".$row['6']."') or `another` in ('".$row['6']."')");
				if($mycount>0){
	
				}else{
					$mycheck++;
//echo "<BR>對不起第".$a."行對應的對應的工廠ID(新)錯需新增，錯誤內容:".$row['6'];
//					$out[0]="ERR";
//					$out[1].="<BR>對不起第".$a."行，工廠查無此筆資料，請先去建立工廠基本資料，錯誤內容:".$row['6'];	
				
				}					
			}
			if ($mycheck==0){
				$OLID="";
				$ODATE="";
				$NLID="";
				$NDATE="";	
				$OC=0;
				$NC=0;
				$ta=share_getinfo("productlist","PN",trim($row['0']));
				$mycount=share_gettable("licenseproduct WHERE PID='".$ta['thisid']."' and isactive=0 and isopen=2 and LID in (select thisid from licenselist WHERE SID in (select thisid from `sitelist` WHERE `thiscode` in ('".$row['4']."') or `another` in ('".$row['4']."') ) )");
				foreach($mycount as $m){
					$PID=$ta['thisid'];
					$OLID=$m['LID'];
					$ODATE=$row['5'];
					//echo "<BR><BR>對不起old第".$a."行 筆數:".count($mycount)." PID=".$PID." OLID=".$OLID." ODATE=".$ODATE;			
					$mycount1=share_gettable("licenseproduct WHERE PID='".$ta['thisid']."' and isactive=0  and isopen=2  and LID in (select thisid from licenselist WHERE SID in (select thisid from `sitelist` WHERE `thiscode` in ('".$row['6']."') or `another` in ('".$row['6']."') ) ) order by `Priority` DESC");			
					if(count($mycount1)>0){	
						//echo "<BR>licenseproduct WHERE PID='".$ta['thisid']."' and isactive=0  and isopen=2  and LID in (select thisid from licenselist WHERE SID in (select thisid from `sitelist` WHERE `thiscode` in ('".$row['6']."') or `another` in ('".$row['6']."') ) ) order by `Priority` DESC";	
						$NLID=$mycount1[0]['LID'];
						$NDATE=$row['7'];
						$mycount2=share_gettable("c_contractproduct WHERE PID='".$PID."' and LID='".$OLID."'");
						foreach($mycount2 as $m2){
//echo "<BR><BR>對不起第".$a."行筆數=".count($mycount2)." c_contractproduct WHERE PID='".$PID."' and LID='".$OLID."'";
//echo "<BR>c_contractproduct WHERE PID='".$PID."' and LID='".$OLID."'";
					$mycount3=share_gettable("c_contractproductup where CCID='".$m2['CCID']."' and  PID='".$PID."' and OLID='".$OLID."' and NLID='".$NLID."'");

//echo "<BR>c_contractproductup where CCID='".$m2['CCID']."' and  PID='".$PID."' and OLID='".$OLID."' and NLID='".$NLID."'";
//echo "<BR>mycount3筆數:".count($mycount3);
							if(count($mycount3)>0){	

								$ODATE1=$mycount3[0]['ODATE'];

								$NDATE1=$mycount3[0]['NDATE'];
//echo "<BR>ODATE=".$ODATE." ODATE1=".$ODATE1." NDATE=".$NDATE." NDATE1=".$NDATE1;

								if(strtotime($ODATE)>strtotime($ODATE1)){	
		
	
									share_update("c_contractproductup","	ODATE='".$ODATE."',`lastadmin`='".$_SESSION['admuserid']."'","thisid='".$mycount3[0]['thisid']."'");
								}

								if(strtotime($NDATE)<strtotime($NDATE1)){	
		
	
									share_update("c_contractproductup","	NDATE='".$NDATE."',`lastadmin`='".$_SESSION['admuserid']."'","thisid='".$mycount3[0]['thisid']."'");
								}							
							}else{

						$newcount=share_gettable("c_contractproduct WHERE CCID='".$m2['CCID']."' and PID='".$PID."' and LID='".$NLID."'");
								
							//	echo "<BR>c_contractproduct WHERE CCID='".$m2['CCID']."' and PID='".$PID."' and LID='".$NLID."'";		
								
								if (count($newcount)>0){
								
								}else{
							//		echo "<BR>c_contractproductup CCID, PID, OLID, NLID, ODATE, NDATE, lastadmin '".$m2['CCID']."','".$PID."','".$OLID."','".$NLID."','".$ODATE."','".$NDATE."','".$_SESSION['admuserid']."'";	
									
									share_insert("c_contractproductup","CCID, PID, OLID, NLID, ODATE, NDATE, lastadmin","'".$m2['CCID']."','".$PID."','".$OLID."','".$NLID."','".$ODATE."','".$NDATE."','".$_SESSION['admuserid']."'");
								}

						
							}
						}
					}
				}
			}
		}	
	
	}	
	echo json_encode($out);	
}
function mytest($x=''){
	$out[0]="OK";
	$fileName ="filesave/check_.xls";  //文件名
echo "<BR>fileName-".$fileName;	
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			if (strlen($row['0'])==0)
			{
				$myOK="ERR";
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行PN錯誤，錯誤內容:".$row['0'];
echo "<BR>對不起第".$a."行PN錯誤，錯誤內容:".$row['0'];		
			}else{
				$mycount=share_getcount("productlist WHERE PN='".$row['0']."'");
				if($mycount>0){

				}else{
					$myOK="ERR";
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行PN資料輸入錯誤,產品查無這筆資料，請先去建立產品基本資料，錯誤內容:".$row['0'];	
echo "<BR>對不起第".$a."行PN資料輸入錯誤,產品查無這筆資料，請先去建立產品基本資料，錯誤內容:".$row['0'];					
				}
			}	
			$mycheck=0;
		
			if(isDate($row['5'])){

			}else{
				$mycheck++;
//				echo "<BR>對不起第".$a."行結束生產日期(預計時間)，錯誤內容:".$row['5'];						
			}	
			if(isDate($row['7'])){

			}else{
				$mycheck++;
//				echo "<BR>對不起第".$a."行開始生產日期(預計時間)，錯誤內容:".$row['7'];						
			}	
			if (strlen($row['4'])==0)
			{
				$mycheck++;
//				echo "<BR>對不起第".$a."行對應的工廠ID(原)不可空白，錯誤內容:".$row['4'];					
			}else{
				$mycount=share_getcount("`sitelist` WHERE `thiscode` in ('".$row['4']."') or `another` in ('".$row['4']."')");
				if($mycount>0){
	
				}else{
					$mycheck++;
echo "<BR>對不起第".$a."行對應的工廠ID(原)錯需新增，錯誤內容:".$row['4'];	
//					$out[0]="ERR";
//					$out[1].="<BR>對不起第".$a."行，工廠查無此筆資料，請先去建立工廠基本資料，錯誤內容:".$row['4'];					
				}					
			}	
			if (strlen($row['6'])==0)
			{
				$mycheck++;
echo "<BR>對不起第".$a."行對應的對應的工廠ID(新)不可空白，錯誤內容:".$row['6'];					
			}else{
				$mycount=share_getcount("`sitelist` WHERE `thiscode` in ('".$row['6']."') or `another` in ('".$row['6']."')");
				if($mycount>0){
	
				}else{
					$mycheck++;
echo "<BR>對不起第".$a."行對應的對應的工廠ID(新)錯需新增，錯誤內容:".$row['6'];
//					$out[0]="ERR";
//					$out[1].="<BR>對不起第".$a."行，工廠查無此筆資料，請先去建立工廠基本資料，錯誤內容:".$row['6'];	
				
				}					
			}
			if ($mycheck==0){
				$OLID="";
				$ODATE="";
				$NLID="";
				$NDATE="";	
				$OC=0;
				$NC=0;
				$ta=share_getinfo("productlist","PN",trim($row['0']));
				$mycount=share_gettable("licenseproduct WHERE PID='".$ta['thisid']."' and isactive=0 and isopen=2 and LID in (select thisid from licenselist WHERE SID in (select thisid from `sitelist` WHERE `thiscode` in ('".$row['4']."') or `another` in ('".$row['4']."') ) )");
				foreach($mycount as $m){
					$PID=$ta['thisid'];
					$OLID=$m['LID'];
					$ODATE=$row['5'];
					echo "<BR><BR>對不起old第".$a."行 筆數:".count($mycount)." PID=".$PID." OLID=".$OLID." ODATE=".$ODATE;			
					$mycount1=share_gettable("licenseproduct WHERE PID='".$ta['thisid']."' and isactive=0  and isopen=2  and LID in (select thisid from licenselist WHERE SID in (select thisid from `sitelist` WHERE `thiscode` in ('".$row['6']."') or `another` in ('".$row['6']."') ) ) order by `Priority` DESC");			
					if(count($mycount1)>0){	
						echo "<BR>licenseproduct WHERE PID='".$ta['thisid']."' and isactive=0  and isopen=2  and LID in (select thisid from licenselist WHERE SID in (select thisid from `sitelist` WHERE `thiscode` in ('".$row['6']."') or `another` in ('".$row['6']."') ) ) order by `Priority` DESC";	
						$NLID=$mycount1[0]['LID'];
						$NDATE=$row['7'];
						$mycount2=share_gettable("c_contractproduct WHERE PID='".$PID."' and LID='".$OLID."'");
						foreach($mycount2 as $m2){
echo "<BR><BR>對不起第".$a."行筆數=".count($mycount2)." c_contractproduct WHERE PID='".$PID."' and LID='".$OLID."'";
echo "<BR>c_contractproduct WHERE PID='".$PID."' and LID='".$OLID."'";
					$mycount3=share_gettable("c_contractproductup where CCID='".$m2['CCID']."' and  PID='".$PID."' and OLID='".$OLID."' and NLID='".$NLID."'");

echo "<BR>c_contractproductup where CCID='".$m2['CCID']."' and  PID='".$PID."' and OLID='".$OLID."' and NLID='".$NLID."'";
echo "<BR>mycount3筆數:".count($mycount3);
							if(count($mycount3)>0){	

								$ODATE1=$mycount3[0]['ODATE'];

								$NDATE1=$mycount3[0]['NDATE'];
echo "<BR>ODATE=".$ODATE." ODATE1=".$ODATE1." NDATE=".$NDATE." NDATE1=".$NDATE1;

								if(strtotime($ODATE)>strtotime($ODATE1)){	
		
	
									share_update("c_contractproductup","	ODATE='".$ODATE."',`lastadmin`='".$_SESSION['admuserid']."'","thisid='".$mycount3[0]['thisid']."'");
								}

								if(strtotime($NDATE)<strtotime($NDATE1)){	
		
	
									share_update("c_contractproductup","	NDATE='".$NDATE."',`lastadmin`='".$_SESSION['admuserid']."'","thisid='".$mycount3[0]['thisid']."'");
								}							
							}else{

						$newcount=share_gettable("c_contractproduct WHERE CCID='".$m2['CCID']."' and PID='".$PID."' and LID='".$NLID."'");
								
								echo "<BR>c_contractproduct WHERE CCID='".$m2['CCID']."' and PID='".$PID."' and LID='".$NLID."'";		
								
								if (count($newcount)>0){
								
								}else{
									echo "<BR>c_contractproductup CCID, PID, OLID, NLID, ODATE, NDATE, lastadmin '".$m2['CCID']."','".$PID."','".$OLID."','".$NLID."','".$ODATE."','".$NDATE."','".$_SESSION['admuserid']."'";	
									
									share_insert("c_contractproductup","CCID, PID, OLID, NLID, ODATE, NDATE, lastadmin","'".$m2['CCID']."','".$PID."','".$OLID."','".$NLID."','".$ODATE."','".$NDATE."','".$_SESSION['admuserid']."'");
								}


						
							}
						}
					}
				}
			}
		}	
	
	}	
	echo json_encode($out);		
}



function mytest1($x=''){
	$out[0]="OK";
	$fileName ="filesave/check1_.xls";  //文件名
echo "<BR>fileName-".$fileName;	
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			if (strlen($row['0'])==0)
			{
				$myOK="ERR";
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行PN錯誤，錯誤內容:".$row['0'];
echo "<BR>對不起第".$a."行PN錯誤，錯誤內容:".$row['0'];		
			}else{
				$mycount=share_getcount("productlist WHERE PN='".$row['0']."'");
				
				if($mycount>0){

				}else{
					$myOK="ERR";
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行PN資料輸入錯誤,產品查無這筆資料，請先去建立產品基本資料，錯誤內容:".$row['0'];	
//echo "<BR>對不起第".$a."行PN資料輸入錯誤,產品查無這筆資料，請先去建立產品基本資料，錯誤內容:".$row['0'];					
				}
			}	
				
			if (strlen($row['4'])==0)
			{	
		
			}else{
				echo "<BR>對不起第".$a."行";				
				$ta=share_getinfo("productlist","PN",trim($row['0']));	
				$tb=share_getinfo("licenselist","licensenumber",trim($row['4']));				
				$mycount=share_gettable("licenseproduct WHERE PID='".$ta['thisid']."' and  LID ='".$tb['thisid']."' ");
				foreach($mycount as $m){
					
					share_update("licenseproduct","	Priority='1',`lastadmin`='".$_SESSION['admuserid']."'","thisid='".$m['thisid']."'");				
			
					echo "<BR> licenseproduct 	Priority='1',`lastadmin`='".$_SESSION['admuserid']."' thisid='".$m['thisid']."'";	

			
				}				
			}
			
			if (strlen($row['8'])==0)
			{	
		
			}else{
				echo "<BR>對不起2第".$a."行";				
				$ta=share_getinfo("productlist","PN",trim($row['0']));	
				$tb=share_getinfo("licenselist","licensenumber",trim($row['8']));				
				$mycount=share_gettable("licenseproduct WHERE PID='".$ta['thisid']."' and  LID ='".$tb['thisid']."' ");
				foreach($mycount as $m){
					
					share_update("licenseproduct","	Priority='2',`lastadmin`='".$_SESSION['admuserid']."'","thisid='".$m['thisid']."'");				
			
					echo "<BR> licenseproduct 	Priority='2',`lastadmin`='".$_SESSION['admuserid']."' thisid='".$m['thisid']."'";	

			
				}				
			}			
			
			if (strlen($row['12'])==0)
			{	
		
			}else{
				echo "<BR>對不起3第".$a."行";				
				$ta=share_getinfo("productlist","PN",trim($row['0']));	
				$tb=share_getinfo("licenselist","licensenumber",trim($row['12']));				
				$mycount=share_gettable("licenseproduct WHERE PID='".$ta['thisid']."' and  LID ='".$tb['thisid']."' ");
				foreach($mycount as $m){
					
					share_update("licenseproduct","	Priority='3',`lastadmin`='".$_SESSION['admuserid']."'","thisid='".$m['thisid']."'");				
			
					echo "<BR> licenseproduct 	Priority='3',`lastadmin`='".$_SESSION['admuserid']."' thisid='".$m['thisid']."'";	

			
				}				
			}				
			
		}	
	
	}	
	echo json_encode($out);	
}
function fileproductlist1($x=''){
	$out[0]="OK";
	$code="check_".$_POST['file'];
	//$indate=$_POST['indate'];
	$file_type = pathinfo($_FILES["val"]["name"], PATHINFO_EXTENSION) ;	
	$fileName ="filesave/". $code.".".$file_type;  //文件名
	$rotate=$_FILES["val"]["tmp_name"];  //原件來源
	move_uploaded_file($rotate,$fileName);//1:源文件名称2:路径/文件名	
	 // 引入函式庫
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			if (strlen($row['0'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行產品號碼錯誤，錯誤內容:".$row['0'];
			}else{
				$mycount=share_getcount("productlist WHERE PN='".$row['0']."'");
				if($mycount==0){
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行產品號碼錯誤，錯誤內容:".$row['0'];		
				}else{
					
					
					for($z=1;$z<=3;$z++){
						if(empty($row[$z])){
							$var[$z]="N/A";
						}else{
							$var[$z]=$row[$z];
						}
					}					
					
					share_update("productlist","var1='".$var[1]."',var2='".$var[2]."',var3='".$var[3]."',`lastadmin`='".$_SESSION['admuserid']."'","PN='".$row[0]."'");					
				}					
			}		
		}	
	}	


	echo json_encode($out);	
}

function filelicenseproduct1($x=''){
	$out[0]="OK";

	$code="check_".$_POST['file'];
	//$indate=$_POST['indate'];
	$file_type = pathinfo($_FILES["val"]["name"], PATHINFO_EXTENSION) ;	
	$fileName ="filesave/". $code.".".$file_type;  //文件名
	$rotate=$_FILES["val"]["tmp_name"];  //原件來源
	move_uploaded_file($rotate,$fileName);//1:源文件名称2:路径/文件名	
	 // 引入函式庫
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			if (strlen($row['0'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行產品號碼空白，錯誤內容:".$row['0'];
			}else{
				$mycount=share_getcount("productlist WHERE PN='".$row['0']."'");
				if($mycount==0){
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行產品號碼錯誤，錯誤內容:".$row['0'];		
				}				
			}
			if (strlen($row['1'])==0)
			{
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行許可證號空白，錯誤內容:".$row['1'];
			}else{
				$mycount=share_getcount("licenselist WHERE licensenumber='".$row['1']."'");
				if($mycount==0){
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行許可證號錯誤，錯誤內容:".$row['1'];		
				}					
			}			

				$ta=share_getinfo("productlist","PN",$row['0']);
				$tb=share_getinfo("licenselist","licensenumber",$row['1']);				
					$mycount=share_getcount("licenseproduct WHERE  PID='".$ta['thisid']."' and  LID='".$tb['thisid']."' ");
	
					if($mycount==0){
						$out[0]="ERR";
						$out[1].="<BR>對不起第".$a."行查無關連資料，請再確認關連資料有建立，謝謝";		
					}else{
						share_update("licenseproduct"," only='".$row['2']."',`lastadmin`='".$_SESSION['admuserid']."'"," PID='".$ta['thisid']."' and  LID='".$tb['thisid']."' ");
					}					
				
				
		}	
	}	


	echo json_encode($out);	
}
function filelicenseproductnew($x=''){
	$out[0]="OK";
	share_del("licenseproducttemp");
	$code="check_".$_POST['file'];
	//$indate=$_POST['indate'];
	$file_type = pathinfo($_FILES["val"]["name"], PATHINFO_EXTENSION) ;	
	$fileName ="filesave/". $code.".".$file_type;  //文件名
	$rotate=$_FILES["val"]["tmp_name"];  //原件來源
	move_uploaded_file($rotate,$fileName);//1:源文件名称2:路径/文件名	
	 // 引入函式庫
	require_once 'adm/PHPExcel.php';
	 // 設定要被讀取的檔案，不使用中文檔名
	 $file = $fileName;
	 try {
		 $objPHPExcel = PHPExcel_IOFactory::load($file);
	 } catch(Exception $e) {
		 die('Error loading file "'.pathinfo($file,PATHINFO_BASENAME).'": '.$e->getMessage());
	 }   
	 $arr_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
	 $colindex = 0;
	 $rowindex = 0;
	 $rownull  = true;
	 $arr_content = array();
	 foreach( $arr_data as $key => $col ){
		 if( $rowindex >= 0 ) {
		  foreach ( $col as $colkey => $colvalue ){  
			  if( $colindex >= 0 ){
				if( $colvalue != "" )
				  $rownull = false;            
				$arr_content[$rowindex][$colindex] = $colvalue;
				
			  }
			  $colindex++;
		  }

			 if($rownull && $rowindex > 0)
				 break;
			 $rownull  = true;
			 $colindex = 0;
		 }
		 $rowindex++;
	 } 	
	 $a=0;
	foreach($arr_content as $row){
		$a++;
		if ($a>1){
			$myOK="OK";
			$myadd=0;
			if (strlen($row['0'])==0)
			{
				$myOK="ERR";
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行Status錯誤，錯誤內容:".$row['0'];
			}else{
				if (strtolower($row['0'])=="inactive"){
						$inactive="1";
						$mydate=date("Y/m/d");			
				}elseif (strtolower($row['0'])=="active"){
						$inactive="0";
						$mydate="";					
				}else{
					$myOK="ERR";
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行Status填寫錯誤，請填寫Inactive 或 Active，錯誤內容:".$row['0'];					
				}
			}			
			if (strlen($row['1'])==0)
			{
				$myOK="ERR";
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行PN錯誤，錯誤內容:".$row['1'];
			}else{
				$mycount=share_getcount("productlist WHERE PN='".$row['1']."'");
				if($mycount>0){


				}else{
					share_insert("productlist","PN, description","'".$row['1']."','".$row['2']."'");
				}
			}				
			
			if (strlen($row['4'])==0)
			{		
				$myOK="ERR";
				$out[0]="ERR";
				$out[1].="<BR>對不起第".$a."行License Number錯誤，錯誤內容:".$row['4'];
			}else{
				if ($row['4']=="NMD"){


				}elseif (strlen($row['4'])==6){
				
				
					
					
					
					
					
					if (strlen($row['15'])==0){
						$qsd="NA";
					}else{
						$qsd1=$row['15'];
						$output = explode("(", $qsd1);
						$qsd=$output[0];
					}
					$mycount=share_getcount("sitelist WHERE QSD='".$qsd."'");
					if($mycount>0){


					}else{
						share_insert("sitelist","QSD,thisname, thiscode, another, thisaddress,  COO, lastadmin","'".$qsd."','".$row['16']."','".$row['16']."','','".$row['11']."','".$row['17']."','".$_SESSION['admuserid']."'");	
					}
					$myadd=1;
					$mycount=share_getcount("licenselist WHERE licensenumber='".trim($row['4'])."'");
					if($mycount>0){

					}else{
						$tb=share_getinfo("sitelist","QSD",$qsd);
						
						share_insert("licenselist","`SID`, `classsification`, `licensenumber`, `clicensenumber`, `clicensename`, `clicensenameen`, `manufacturer`, `issueddate`, `expdate`,lastadmin","'".$tb['thisid']."','".$row['3']."','".trim($row['4'])."','".$row['5']."','".$row['8']."','".$row['9']."','".$row['10']."','".$row['6']."','".$row['7']."','".$_SESSION['admuserid']."'");
//$out[98].="\na=".$a."行 4=".$row['4']." 41=".trim($row['4']);				
//$out[99].="<BR>a=".$a."行 licenselist `SID`, `classsification`, `licensenumber`, `clicensenumber`, `clicensename`, `clicensenameen`, `manufacturer`, `issueddate`, `expdate`,lastadmin '".$tb['thisid']."','".$row['3']."','".trim($row['4'])."','".$row['5']."','".$row['8']."','".$row['9']."','".$row['10']."','".$row['6']."','".$row['7']."','".$_SESSION['admuserid']."'";				
					}


				}else{

					$myOK="ERR";
					$out[0]="ERR";
					$out[1].="<BR>對不起第".$a."行License Number填寫錯誤，請確認格式是否正常，目前抓出資料為:".$row['4'];

				}
			}
			if ($myOK=="OK"){
				if ($myadd==1){
					$ta=share_getinfo("productlist","PN",trim($row['1']));
					$tb=share_getinfo("licenselist","licensenumber",trim($row['4']));
					$mycount=share_getcount("licenseproduct WHERE  PID='".$ta['thisid']."' and  LID='".$tb['thisid']."' ");
					if($mycount>0){	
						$me=share_gettable("licenseproduct WHERE  PID='".$ta['thisid']."' and  LID='".$tb['thisid']."'");
				
						if ($me[0]['isactive']==0){
							if ($inactive==1){	
								share_update("licenseproduct","	isactive='".$inactive."',`lastadmin`='".$_SESSION['admuserid']."'","thisid='".$me[0]['thisid']."'");

							}	 					
						}							
					}else{
						share_insert("licenseproduct","`isactive`, `PID`, `LID`, `closetime`,QSD, lastadmin","'".$inactive."','".$ta['thisid']."','".$tb['thisid']."','".$mydate."','".$row['15']."','".$_SESSION['admuserid']."'");
					}
				}
			}
		}	
	}	
	echo json_encode($out);	
}

?>