<?php
//****************************************请求权限****************************************
require("locked.php");                                                                 //*
setlocked(9,"序时账簿—恩格会计管理系统");                                             //*
//****************************************************************************************
$t1=microtime(true);//页面执行起始时间；
include("header.php"); 
include("cnn.php");
?>

<?php 
if(($_SERVER['REQUEST_METHOD']=='POST')&&isset($_POST[qj0])&&isset($_POST[qj1])){//提交查询页面；
	echo '<form name="form1" method="post">
	<div align="center">
	<table>
	<caption><strong><font size="+2">序时账簿查找条件</font></strong></caption>
	<tr>
	<td>期间：</td>
	<td><input name="qj0" type="text" style="width:100px" value="'.$_POST[qj0].'" /></td>
	<td align="center">—</td>
	<td><input name="qj1" type="text" style="width:100px" value="'.$_POST[qj1].'" ondblclick="autoQj()" /></td>
	</tr>
	<tr>
	<td>凭证号：</td>
	<td><input name="pzh0" type="text" style="width:100px" value="'.$_POST[pzh0].'" /></td>
	<td align="center">—</td>
	<td><input name="pzh1" type="text" style="width:100px" value="'.$_POST[pzh1].'" /></td>
	</tr>
	<tr>
	<td>摘要：</td>
	<td colspan="3"><input name="zy" type="text" style="width:255px" value="'.$_POST[zy].'" /></td>
	</tr>
	<tr>
	<td>科目全称：</td>
	<td colspan="3"><input name="kmF" type="text" style="width:255px" value="'.$_POST[kmF].'" /></td>
	</tr>
	<tr>
	<td>借贷金额：</td>
	<td colspan="3"><input name="DC" type="text" style="width:255px" value="'.$_POST[DC].'" /></td>
	</tr>
	<tr>
	<td>录入：</td>
	<td><input name="etr" type="text" style="width:100px" value="'.$_POST[etr].'" /></td>
	<td align="center">审核：</td>
	<td><abbr title="１、是：已审核；２、否：未审核；３、审核人名称"><input name="chk" type="text" style="width:100px" value="'.$_POST[chk].'"/></abbr></td>
	</tr>
	<td colspan="4" align="center"><input id="tj1" name="tj" type="button" onclick="tjcx();" value="提交" /></td>
	</tr>
	</table>
	</div>
	</form>';
	
	//提交数据成功，先构造查询凭证表的SQL语句，然后将查询到的相关数据写入table；
	$strsql='select ywrq,lrrq,qj,pzh,xh,zy,km,kmF,dr,cr,etr,chk,acc from V1 where (qj between "'.$_POST[qj0].'" and "'.$_POST[qj1].'")';
	
	if(($_POST[pzh0]!="")&&($_POST[pzh1]=="")){
		$strsql.=' and pzh>="'.$_POST[pzh0].'"';
	}else if(($_POST[pzh0]=="")&&($_POST[pzh1]!="")){
	    $strsql.=' and pzh<="'.$_POST[pzh1].'"';
	}else if(($_POST[pzh0]!="")&&($_POST[pzh1]!="")){
		$strsql.=' and (pzh between "'.$_POST[pzh0].'" and "'.$_POST[pzh1].'")';
	}
	
	if($_POST[zy]!=""){
		$strsql.=' and zy like "%'.$_POST[zy].'%"';
	}
	
	if($_POST[kmF]!=""){
		$strsql.=' and kmF like "%'.$_POST[kmF].'%"';
	}
	
	if($_POST[DC]!=""){
		$strsql.=' and (dr="'.$_POST[DC].'" or cr="'.$_POST[DC].'")';
	}
	
	if($_POST[etr]!=""){
		$strsql.=' and etr="'.$_POST[etr].'"';
	}
	
	if($_POST[chk]=="是"){
		$strsql.=' and chk is not null';
	}else if($_POST[chk]=="否"){
		$strsql.=' and chk is null';
	}else if($_POST[chk]!=""){
		$strsql.=' and chk="'.$_POST[chk].'"';
	}
	
	$strsql.=' order by qj,pzh,xh';
	
	$sql=$mysqli->query($strsql);
    $info=$sql->fetch_array(MYSQLI_BOTH);
	
	$num_rows=$sql->num_rows;
	
	echo '<hr width="95%">';
	echo '<div id="ts" align="center">查找到 '.$num_rows.' 条符合条件的记录</div>';//显示查询的记录数及处理时间；
	echo '<hr width="95%">';
	
    if($info){ //如果存在数据；
	    echo '<div align="center">
		<table width="95%">
		<tr><td>
		<a href="V_1.php" target="_blank">新增凭证</a>|
		<a href="javascript:CopyTable(\'tbl1\');">复制数据</a>|
		<a href="javascript:JZSY()">结转损益</a>|
		<a href="javascript:accountV()">凭证记账</a>|
		<a href="javascript:accountNodoV()">凭证反记账</a>|
		<a href="javascript:void(MyPreview())">凭证打印</a>
		</td></tr>
		</table>
		</div>
		<div align="center">
        <table id="tbl1" border="1" width="95%" cellspacing="0">
		<caption><strong><font size="+2">会计凭证序时账簿</font></strong></caption>
		<tr align="center">
		<td>操作</td>
		<td>业务日期</td>
		<td>录入日期</td>
		<td>期间</td>
		<td>凭证号</td>
		<td>序号</td>
		<td>摘要</td>
		<td>科目</td>
		<td>科目全称</td>
		<td>借方</td>
		<td>贷方</td>
		<td>录入</td>
		<td>审核</td>
		<td>记账</td>
		</tr>';
		$intI=1;//表格行号；
		$intJt=1;//根据凭证号递增自动递增，使不同凭证号的表格底色交替显示；
		$intPzh=$info[qj].'and'.$info[pzh];//初始化“期间and凭证号”变量；
		DO{
		   if($intPzh!=$info[qj].'and'.$info[pzh]){
			   $intJt++;//凭证号不同时自动递增；
		   }
		   if(($intJt%2)==1){
			   $strColor='#CCCCCC';
		   }else{
			   $strColor='';
		   }
		   echo '<tr id='.$intI.' style="background:'.$strColor.'" onmouseover="trMouseover(this)" onmouseout="trMouseout(this)">';
		   echo '<td align="center">';
		   echo '<a href="javascript:editV('.$info[qj].','.$info[pzh].');"><abbr title="修改凭证"><img src="picture/b_edit.png" /></abbr></a>|';
		   echo '<a href="javascript:void(dropV('.$info[qj].','.$info[pzh].','.$intI.'));"><abbr title="删除凭证"><img src="picture/b_drop.png" /></abbr></a>|';
		   echo '<a href="javascript:void(checkV('.$info[qj].','.$info[pzh].','.$intI.'));"><abbr title="审核凭证"><img src="picture/b_check.png" /></abbr></a>|';
		   echo '<a href="javascript:copyV('.$info[qj].','.$info[pzh].');"><abbr title="复制凭证"><img src="picture/b_copy.png" /></abbr></a>';
		   echo '</td>';
		   echo '<td align="center">'.$info[ywrq].'</td>';
		   echo '<td align="center">'.$info[lrrq].'</td>';
		   echo '<td align="center">'.$info[qj].'</td>';
		   echo '<td align="center">'.$info[pzh].'</td>';
		   echo '<td align="center">'.$info[xh].'</td>';
		   echo '<td>'.$info[zy].'</td>';
		   echo '<td>'.$info[km].'</td>';
		   echo '<td>'.$info[kmF].'</td>';
		   echo '<td align="right">'.($info[dr]==0?"":$info[dr]).'</td>';
		   echo '<td align="right">'.($info[cr]==0?"":$info[cr]).'</td>';
		   echo '<td>'.$info[etr].'</td>';
		   echo '<td>'.$info[chk].'</td>';
		   echo '<td>'.$info[acc].'</td>';
		   echo '</tr>';
		   $intI++;
		   $intPzh=$info[qj].'and'.$info[pzh];//重新指定期间和凭证号；
	    }while($info=$sql->fetch_array(MYSQLI_BOTH));
	    echo '</table></div>';
	}else{
		echo '<div align="center">未检索到相关数据；</div>';
	}
?>
<script language="javascript" type="text/javascript">
//限制频繁点击提交按钮，再次提交需要等待三秒；
document.getElementById("tj1").disabled=true;
setTimeout("getTj()",3000)
function getTj(){
	document.getElementById("tj1").disabled=false;
}
</script>
<?PHP  
}else{//非提交查询页面；
    $strsql="select distinct qj from V1 order by qj desc";
	$sql=$mysqli->query($strsql);
    $info=$sql->fetch_array(MYSQLI_BOTH);
	if($info){
		echo '<form name="form1" method="post">
		<div align="center">
		<table>
		<caption><strong><font size="+2">序时账簿查找条件</font></strong></caption>
		<tr>
		<td>期间：</td>
		<td><input name="qj0" type="text" style="width:100px" value="'.$info[qj].'"/></td>
		<td align="center">—</td>
		<td><input name="qj1" type="text" style="width:100px" ondblclick="autoQj()" value="'.$info[qj].'"/></td>
		</tr>
		<tr>
		<td>凭证号：</td>
		<td><input name="pzh0" type="text" style="width:100px" /></td>
		<td align="center">—</td>
		<td><input name="pzh1" type="text" style="width:100px" /></td>
		</tr>
		<tr>
		<td>摘要：</td>
		<td colspan="3"><input name="zy" type="text" style="width:255px" /></td>
		</tr>
		<tr>
		<td>科目全称：</td>
		<td colspan="3"><input name="kmF" type="text" style="width:255px" /></td>
		</tr>
		<tr>
		<td>借贷金额：</td>
		<td colspan="3"><input name="DC" type="text" style="width:255px" /></td>
		</tr>
		<tr>
		<td>录入：</td>
		<td><input name="etr" type="text" style="width:100px" /></td>
		<td align="center">审核：</td>
		<td><abbr title="１、是：已审核；２、否：未审核；３、审核人名称"><input name="chk" type="text" style="width:100px" /></abbr></td>
		</tr>
		<tr>
		<td colspan="4" align="center"><input name="tj" type="button" onclick="tjcx();" value="提交" /></td>
		</tr>
		</table>
		</div>
		</form>';
	}else{
		echo '<form name="form1" method="post">
		<div align="center">
		<table>
		<caption><strong><font size="+2">操作提示</font></strong></caption>
		<tr>
		<td>凭证表中暂无任何数据！</td>
		</tr>
		</table>
		</div>
		</form>';
	}
    $sql->free();
	$mysqli->close();
}
?>

<script language="javascript" src="LodopFuncs.js"></script>

<script language="javascript" type="text/javascript">
<!--
//自动将起始期间填入终止期间；
function autoQj(){
	form1.qj1.value=form1.qj0.value;
}
//提交查询凭证条件；
function tjcx(){
	//查找凭证时必须保证期间条件完整；
	if(form1.qj0.value==""){
		alert("请输入起始期间！");
		form1.qj0.focus();
		return false;
	}
	
	if(isNaN(form1.qj0.value)){
		alert("起始期间必须为数字！");
		form1.qj0.focus();
		return false;
	}
	
	if(form1.qj1.value==""){
		alert("请输入终止期间！");
		form1.qj1.focus();
		return false;
	}
	
	if(isNaN(form1.qj1.value)){
		alert("终止期间必须为数字！");
		form1.qj1.focus();
		return false;
	}
	
	if(form1.qj0.value>form1.qj1.value){
		alert("终止期间小于起始期间！");
		form1.qj1.focus();
		return false;
	}

	form1.submit();
}

function CopyTable(tableid){
//将网页表格数据成功复制到剪切板上；
try{
	var tbl=document.getElementById(tableid);
	var arr=new Array();
	for(var i=0,j=tbl.rows.length;i<j;i++){
		for(var m=1,n=tbl.rows[i].cells.length;m<n;m++){
			arr.push(tbl.rows[i].cells[m].innerText);
			if(m<n-1)arr.push("\t");
		}
		if(i<j-1)arr.push("\n");
	}
	var str=arr.join("");
	window.clipboardData.setData('text',str);
	alert('成功复制表格数据！');
}catch(error){
	alert('您的浏览器不支持复制功能！');
}finally{
	
}
}

var strLastColor;//记录鼠标经过表格行前表格原来的底色；

function trMouseover(tableTr){//鼠标经过表格行时的颜色；
    strLastColor=tableTr.style.background;
	tableTr.style.background="yellow";
}

function trMouseout(tableTr){//鼠标离开表格行时的颜色；
	tableTr.style.background=strLastColor;
}

function editV(intQj,intPzh){
	//修改凭证
	openWin('V_1.php?qj='+intQj+'&pzh='+intPzh);
}
		
function dropV(intQj,intPzh,intI){
	//删除凭证
	//删除前根据页面信息检查凭证是否审核；
	var tbl=document.getElementById('tbl1');
	if(tbl.rows[intI].cells[12].innerHTML!=''){
		alert('凭证已审核，不允许删除！');
		return false;
	}
	
	if(confirm("您确信要删除 "+intPzh+" 号凭证吗？")){
		
		request.open("GET",'V_drop.php?qj='+intQj+'&pzh='+intPzh);
    
		request.onreadystatechange=function(){
			if(request.readyState==4){
				if(request.status==200){
					alert(request.responseText);
				}
			}
		}
		
		request.send(null);
	}
}

function checkV(intQj,intPzh,intI){
	//审核凭证
	//审核前根据页面信息检查凭证是否记账；
	var tbl=document.getElementById('tbl1');
	if(tbl.rows[intI].cells[13].innerHTML!=''){
		alert('凭证已记账，不允许(反)审核操作！');
		return false;
	}
	
	request.open("GET",'V_check.php?qj='+intQj+'&pzh='+intPzh);
    
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(request.status==200){
				alert(request.responseText);
				var intJ;
				var strqp;
				array1=(request.responseText).split("/");
				if(trim(array1[0])=='审核成功'){
                	intJ=intI-1;
					while(intJ>1){
						strqp=tbl.rows[intJ].cells[3].innerHTML+'-'+tbl.rows[intJ].cells[4].innerHTML;
						if(strqp!=array1[1]){
							intJ=intJ+1;
							break;
						}else{
							intJ--;
						}
					}
					if(intJ==0){intJ=1}
					while(intJ>0){
						strqp=tbl.rows[intJ].cells[3].innerHTML+'-'+tbl.rows[intJ].cells[4].innerHTML;
						if(strqp==array1[1]){
							tbl.rows[intJ].cells[12].innerHTML=array1[2];
							intJ++;
						}else{
							break;
						}
					}				
				}
				if(trim(array1[0])=='反审核成功'){
					intJ=intI-1;
					while(intJ>1){
						strqp=tbl.rows[intJ].cells[3].innerHTML+'-'+tbl.rows[intJ].cells[4].innerHTML;
						if(strqp!=array1[1]){
							intJ=intJ+1;
							break;
						}else{
							intJ--;
						}
					}
					if(intJ==0){intJ=1}
					while(intJ>0){
						strqp=tbl.rows[intJ].cells[3].innerHTML+'-'+tbl.rows[intJ].cells[4].innerHTML;
						if(strqp==array1[1]){
							tbl.rows[intJ].cells[12].innerHTML='';
							intJ++;
						}else{
							break;
						}
					}
				}
			}
		}
	}
	
	request.send(null);
	
}

function copyV(intQj,intPzh){
	//复制凭证
	openWin('V_1.php?handle=copyV&qj='+intQj+'&pzh='+intPzh);
}

function JZSY(){
	//结转损益功能；
	var aW=(screen.availWidth-500)/2;
	var aH=(screen.availHeight-300)/2;
	window.open("V_jzsy.php",'','left='+aW+',top='+aH+',width=500,height=300');
}

function accountV(){
	//凭证记账功能；
	var aW=(screen.availWidth-500)/2;
	var aH=(screen.availHeight-300)/2;
	window.open("V_account_1.php",'','left='+aW+',top='+aH+',width=500,height=300');
}

function accountNodoV(){
	//凭证反记账功能；
	var aW=(screen.availWidth-500)/2;
	var aH=(screen.availHeight-300)/2;
	window.open("V_accountNodo_1.php",'','left='+aW+',top='+aH+',width=500,height=300');
}

var LODOP; //声明为全局变量
var Company_Name=""; //凭证打印时显示的公司名称；

function MyPreview() {	
	var tbl=document.getElementById("tbl1");
	var sumDr=0,sumCr=0;
	var bgd=tbl.rows.item(0).style.background;
	
	//如果打印的行数一次性超出了１００条记录，系不执行打印任务。
	if(tbl.rows.length>200){
		alert("提示：一次打印记录数不能超出２００条，请缩小打印范围后再试。");
		return false;
	}
	
	LODOP=getLodop();  
	LODOP.PRINT_INIT("会计凭证打印");
	LODOP.SET_PRINT_PAGESIZE(1,2100,1400,"凭证纸张");
	for(var i=1;i<tbl.rows.length;i++){
		if(bgd!=tbl.rows.item(i).style.background ||j==6){//不同的凭证或者同一张凭证行数大数５后换行；
		    if(i!=1){
                LODOP.ADD_PRINT_HTM(432,504,118,40,"<div align='right'>"+sumDr.toFixed(2)+"</div>");
		        LODOP.ADD_PRINT_HTM(432,624,118,40,"<div align='right'>"+sumCr.toFixed(2)+"</div>");
            } 
		    if(j!=6){
				sumDr=0;
				sumCr=0;
			}
			var j=1;
			LODOP.NewPage();
			LODOP.ADD_PRINT_HTM(40,330,150,40,"<font size=6>会计凭证</font>");
			LODOP.SET_PRINT_STYLE("FontSize",12);
			LODOP.ADD_PRINT_TEXT(80,42,300,40,"编制单位："+Company_Name);
			LODOP.ADD_PRINT_TEXT(100,42,300,40,"业务日期："+tbl.rows[i].cells[1].innerHTML);
		    LODOP.ADD_PRINT_TEXT(100,322,300,40,"录入日期："+tbl.rows[i].cells[2].innerHTML);
		    LODOP.ADD_PRINT_TEXT(100,642,300,40,"凭证号："+tbl.rows[i].cells[4].innerHTML);	
			
			LODOP.ADD_PRINT_RECT(120,42,160,50,0,1);
			LODOP.ADD_PRINT_TEXT(135,97,120,30,"摘要");	
			
			LODOP.ADD_PRINT_RECT(120,202,300,50,0,1);
			LODOP.ADD_PRINT_TEXT(135,322,140,30,"科目全称");	
			
			LODOP.ADD_PRINT_RECT(120,502,120,50,0,1);
			LODOP.ADD_PRINT_TEXT(135,547,100,30,"借方");	
			
			LODOP.ADD_PRINT_RECT(120,622,120,50,0,1);
			LODOP.ADD_PRINT_TEXT(135,667,100,30,"贷方");
			
			//画出５行格子；
			for(n=1;n<=5;n++){
				LODOP.ADD_PRINT_RECT(120+50*n,42,160,50,0,1);
				LODOP.ADD_PRINT_RECT(120+50*n,202,300,50,0,1);
				LODOP.ADD_PRINT_RECT(120+50*n,502,120,50,0,1);
				LODOP.ADD_PRINT_RECT(120+50*n,622,120,50,0,1);
			}
			
			LODOP.ADD_PRINT_RECT(420,42,460,40,0,1);
			LODOP.ADD_PRINT_HTM(432,47,450,30,"<div align='center'>录入:"+tbl.rows[i].cells[11].innerHTML+
			"&nbsp;|&nbsp;审核:"+tbl.rows[i].cells[12].innerHTML+"&nbsp;|&nbsp;记账:"+tbl.rows[i].cells[13].innerHTML+"</div>");
			LODOP.ADD_PRINT_RECT(420,502,120,40,0,1);
			LODOP.ADD_PRINT_RECT(420,622,120,40,0,1);
		}
		
		LODOP.ADD_PRINT_TEXT(125+50*j,42,160,50,tbl.rows[i].cells[6].innerHTML);
		LODOP.ADD_PRINT_TEXT(125+50*j,202,300,50,tbl.rows[i].cells[8].innerHTML);
		LODOP.ADD_PRINT_HTM(135+50*j,504,118,30,"<div align='right'>"+tbl.rows[i].cells[9].innerHTML+"</div>");
		LODOP.ADD_PRINT_HTM(135+50*j,624,118,30,"<div align='right'>"+tbl.rows[i].cells[10].innerHTML+"</div>");
		
		sumDr+=Number(tbl.rows[i].cells[9].innerHTML);
		sumCr+=Number(tbl.rows[i].cells[10].innerHTML);
		
		if(i==tbl.rows.length-1){
            LODOP.ADD_PRINT_HTM(432,504,118,40,"<div align='right'>"+sumDr.toFixed(2)+"</div>");
		    LODOP.ADD_PRINT_HTM(432,624,118,40,"<div align='right'>"+sumCr.toFixed(2)+"</div>");
        }
	
		j=j+1;
		bgd=tbl.rows.item(i).style.background;
		
	}	
	LODOP.PREVIEW();
}
	
//自动加载高亮显示；
HighLight();

//-->
</script>

<?php 
$t2=microtime(true);//页面执行终止时间；
//给HTML控件赋值；
echo '<script language="javascript" type="text/javascript">document.getElementById("ts").innerHTML+="，耗时 '.round($t2-$t1,4).' 秒。";</script>';
	
include("footer.php");
?>