<?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,thisname, cname,minqty, psizenumber, psize","'".$row['0']."','".$row['1']."','".$row['2']."','".$row['3']."','".$row['4']."','".$row['5']."','".$row['6']."','".$row['7']."'");
				
				$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,thisname, cname,minqty, psize, psizenumber) select model,PN, description,thisname, 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);	
}


?>