<?php
/***********************************************************************************/
/* MassCRM客户关系管理系统							   */
/* ========================      						   */
/* 欢迎使用 MassCRM ,在您开始使用之前,请务必完整阅读本软件附带的全部说明档	   */
/***********************************************************************************/
/* Welcome to http://www.massboxes.com & http://masscrm.massboxes.com		   */
/***********************************************************************************/
	function checkRole($table_name,$module_num,$bu_auto_id,$role_name){
	 	global $smart;
	 	global $boxesDB;
	 			
	 	$boxesDB->setTableName("boxes_user");
		$sql="where bu_auto_id='".$bu_auto_id."'";
		$boxesDB->setCondition($sql);
		$boxes_check=$boxesDB->selectResult();
		
		$bu_role=$boxes_check[0][bu_role];
		
		$boxesDB->setTableName("boxes_role");
		$sql="where br_name='".$bu_role."'";
		$boxesDB->setCondition($sql);
		$boxes_check=$boxesDB->selectResult();
	 	
		$br_value=$boxes_check[0][br_value];
	 	
	 	$boxesDB->setTableName("boxes_function");
		$sql="where (bf_module_num='$module_num' and bf_ename='".$role_name."')";
		$boxesDB->setCondition($sql);
		$boxes_check=$boxesDB->selectResult();
	 	
		$bf_auto_id=$boxes_check[0][bf_auto_id];
		
		if(!$bu_role or !$br_value or ($bf_auto_id and substr($br_value,$bf_auto_id-1,1)!=1)){
			$error="请求失败：你没有模块“".$boxes_check[0][bf_module]."”“".$boxes_check[0][bf_name]."”的权限！";
			echo"<script>alert(\"$error\");self.close();</script>";
			exit;
		}
	}
	 
	require('../print/chinese.php');
	require_once "../account/".$_REQUEST[account].".boxes.inc.php";
	require_once "../includes/config.inc.php";
	require_once "../includes/BiaoCommon.class.php";
	require_once('../adodb/adodb.inc.php');
	
	$conn = &ADONewConnection('mysql');
	$conn->debug=false;
	$conn->PConnect($MYSQL_HOSTNAME, $MYSQL_USERNAME, $MYSQL_PASSWORD, $DATABASE);
	$boxesDB=new BiaoCommon($conn); 

	$table_name=$_REQUEST[table_name];
	$module_name=$_REQUEST[module_name];
	$module_num=$_REQUEST[module_num];
	$bu_auto_id=$_REQUEST[bu_auto_id];
	$startTime=$_REQUEST[search_start_time];
	$endTime=$_REQUEST[search_end_time];
	$operator=$_REQUEST["operator"];
	
	checkRole($table_name,$module_num,$bu_auto_id,"PRINT");
	
	
	if(!$table_name){
		echo"<script>alert(\"参数传递不全！按确定退出！\");</script>";
		exit;
	}
	$boxesDB->setTableName("field_display_table");
	$sql="where table_name='".$table_name."'";
	$boxesDB->setCondition($sql);
	$boxes_check=$boxesDB->selectResult();
	if(!count($boxes_check)){
		echo"<script>alert(\"系统设置出问题啦！\");</script>";
		exit;
	}
	$prefix=$boxes_check[0][prefix];
	$id_name=$prefix."id";
	$voucher_time=$prefix."time";
	$auto_id=$prefix."auto_id";
	$title=$table_name."_title";
	$title=$$title;
	
/*############################################################*/	
	$sort_by_field	=	$_REQUEST[sort_by_field];
	$sort_order		=	$_REQUEST[sort_order];
	if(!$sort_order){
		$sort_order="desc";
	}
		
	if(!$sort_by_field)	
		$index="order by $auto_id ".$sort_order;
	else{ 
		$index="order by $sort_by_field ".$sort_order;
	}
/*############################################################*/

	$boxesDB->setTableName("field_display_table");
	$sql="where (table_name='".$table_name."' and search_is_display='1') order by search_display_number asc";
	$boxesDB->setCondition($sql);
	$boxes_check=$boxesDB->selectResult();
	for($i=0;$i<count($boxes_check);$i++){
		$row=$boxes_check[$i];
		$field_name="search_".$row[field_name];
		if($_REQUEST[$field_name]){
			$condition.="$row[field_name] like '%".$_REQUEST[$field_name]."%' and ";//全部都是模糊查找
		}
	}

	if($condition){//去掉最后一个and	
		$condition=substr($condition,0,strlen($condition)-4);
		$condition="where ".$condition." ";
	}
	
	$condition.=$index;	
	
	//先检查总量，如果记录太多将会当机
	$boxesDB->setTableName($table_name);
	$boxesDB->setSelectField("*");
	$boxesDB->setCondition($condition);
	$boxes_check_row=$boxesDB->selectResult();
	if(count($boxes_check_row)>$PRINT_MAX){
		echo"<script>alert('记录太多了，已经超过了设置最大记录 $PRINT_MAX 条！');self.close();</script>";
		exit;
	}
	/*##################################################################################################*/

	class PDF extends PDF_Chinese
	{
		
		var $account;
		var $table_name;
		var $id_name;
		var $title;
		var $condition;
		var $company;
		var $operator;
		var $startTime;
		var $endTime;
		var $unit;
		var $orientation;
		var $format;
		var $print_width;
		var $total_pages;
		
		
		function PDF($orientation,$unit,$format){
			$this->orientation=$orientation;
			$this->unit=$unit;
			$this->format=$format;
			$this->FPDF();
		}
		function setPrintWidth($print_width){
			$this->print_width=$print_width;
		}	
		function setAccount($account){
			$this->account=$account;
		}	
		function setTableName($table_name){
			$this->table_name=$table_name;
		}	
		function setIdName($id_name){
			$this->id_name=$id_name;
		}	
		function setTitle($title){
			$this->title=$title;
		}	
		function setCondition($condition){
			$this->condition=$condition;
		}
		function setCompany($company){
			$this->company=$company;
		}
		function setOperator($operator){
			$this->operator=$operator;
		}
		function setTime($startTime,$endTime){
			$this->startTime=$startTime;
			$this->endTime=$endTime;
		}
			
		function num2rmb($num){//把数字金额转换成中文大写数字的函数
		   $c1="零壹贰叁肆伍陆柒捌玖";
		   $c2="分角元拾佰仟万拾佰仟亿";
		   
		   $num=round($num,2);
		   $num=$num*100;
		   if(strlen($num)>10){
			return "oh,sorry,the number is too long!";
		   }
			
		   $i=0;
		   $c="";
		 
		   while (1){
			if($i==0){
				$n=substr($num,strlen($num)-1,1);
			}else{
					$n=$num %10;
				}
		  
			   $p1=substr($c1,2*$n,2);
			   $p2=substr($c2,2*$i,2);
			   if($n!='0' || ($n=='0' &&($p2=='亿' || $p2=='万' || $p2=='元' ))){ 
					$c=$p1.$p2.$c;
			   }else{
					$c=$p1.$c;
			   } 
				 
			   $i=$i+1;
			   $num=$num/10;
			   $num=(int)$num;
			   
			   if($num==0){
					 break;
			   }
			}//end of while|  here, we got a chinese string with some useless character
			
			//we chop out the useless characters to form the correct output
			$j = 0; 
			$slen=strlen($c);
			while ($j< $slen) {
				$m = substr($c,$j,4);
			
			  if ($m=='零元' || $m=='零万' || $m=='零亿' || $m=='零零'){
						 $left=substr($c,0,$j);
				  $right=substr($c,$j+2);  
						 $c = $left.$right;     
						 $j = $j-2;
						 $slen = $slen-2; 
				}   
				$j=$j+2;
			 }
			 
			 if(substr($c,strlen($c)-2,2)=='零'){
				 $c=substr($c,0,strlen($c)-2);
			 }
			 
			return $c;
		}
		
		function PrintHeader()
		{
			global $boxesDB;
			
			$header[]=array('编制单位：'.$this->company,80);
			$header[]=array('打印帐套：'.$this->account,70);
			$header[]=array('打印人员：'.$this->operator,32);
			if(count($header)){
				foreach($header as $cel)
				{	
						$this->Cell(($cel[1]),6,$cel[0],0,0);
				}
				$this->Ln();
			}
			
			$boxesDB->setTableName("field_display_table");
			$boxesDB->setSelectField("sum(print_list_display_width) as total_display_width");
			$sql="where (table_name='".$this->table_name."' and print_list_is_display='1') group by table_name";
			$boxesDB->setCondition($sql);
			$boxes_temp=$boxesDB->selectResult();
			$total_display_width=$boxes_temp[0][total_display_width];
			if(!$total_display_width){
				echo"<script>alert(\"没有设置显示宽度！按确定退出！\");</script>";
				exit;
			}
			
			$boxesDB->setSelectField("*");
			$sql="where (table_name='".$this->table_name."' and print_list_is_display='1') order by print_list_display_number asc";
			$boxesDB->setCondition($sql);
			$boxes_body_list=$boxesDB->selectResult();
			
			for($i=0;$i<count($boxes_body_list);$i++){
				$row=$boxes_body_list[$i];
				$width=$row[print_list_display_width]/$total_display_width*$this->print_width;
				$value=$row[display_name];
				$body_head[0][]=array($value,$width);
			}
			
			if(count($body_head)){
				foreach($body_head as $row)
				{
					foreach($row as $col){
						$this->Cell(($col[1]),5.5,$col[0],1,0,'C');
					}
					$this->Ln();
				}
			}
		}
		
		
		function PrintBody()
		{
			global $boxesDB;
			
			$boxesDB->setTableName("field_display_table");
			$boxesDB->setSelectField("sum(print_list_display_width) as total_display_width");
			$sql="where (table_name='".$this->table_name."' and print_list_is_display='1') group by table_name";
			$boxesDB->setCondition($sql);
			$boxes_temp=$boxesDB->selectResult();
			$total_display_width=$boxes_temp[0][total_display_width];
			if(!$total_display_width){
				echo"<script>alert(\"没有设置显示宽度！按确定退出！\");</script>";
				exit;
			}
			
			$boxesDB->setSelectField("*");
			$sql="where (table_name='".$this->table_name."' and print_list_is_display='1') order by print_list_display_number asc";
			$boxesDB->setCondition($sql);
			$boxes_body_list=$boxesDB->selectResult();
			
			$boxesDB->setTableName($this->table_name);
			$boxesDB->setSelectField("*");
			$sql=$this->condition;
			$boxesDB->setCondition($sql);
			$boxes_body_result=$boxesDB->selectResult();
			
			
			$this->total_pages=floor(count($boxes_body_result)/26)+1;
			
			$number=0;
			for($i=0;$i<count($boxes_body_result);$i++){
				$row_body=$boxes_body_result[$i];
				for($j=0;$j<count($boxes_body_list);$j++){
					$row=$boxes_body_list[$j];
					$width=$row[print_list_display_width]/$total_display_width*$this->print_width;
					
					$value=$row_body[$row[field_name]];
					$body_main[$number][]=array($value,$width);
				}
				$number++;
			}
			
			if(count($body_main)){
				foreach($body_main as $row)
				{
					foreach($row as $col){
						$this->Cell(($col[1]),6,$col[0],1,0,'C');
					}
					$this->Ln();
				}
			}
		}
		
		
	
		function PrintBottom()
		{
			global $boxesDB;
		}
		
		function Header()
		{
			$this->SetFont('simhei','B',14);
			$this->Cell(0,10,$this->title,0,1,'C');
			$this->SetFont('simhei','B',10);
			$this->PrintHeader();
		}
		function Footer()
		{
			$this->SetY(-20);
			$this->SetFont('simhei','I',10);
			$this->Cell(0,10,"打印时间：".date("Y-m-d H:i:s",time())."         第".$this->PageNo()."页 总".$this->total_pages."页",0,0,'C');
		}
	}

	$pdf=new PDF($information_orientation,$information_unit,$information_format);
	$pdf->setTableName($table_name);
	$pdf->setIdName($id_name);
	$pdf->setTitle($title);
	$pdf->setCondition($condition);
	$pdf->setCompany($COMPANY_NAME);
	$pdf->setAccount($ACCOUNT_NAME);
	$pdf->setOperator($operator);
	$pdf->setTime($startTime,$endTime);
	$pdf->setPrintWidth($print_information_width);
	$pdf->AddGBFont('simsun','宋体'); 
	$pdf->AddGBFont('simhei','黑体'); 
	
	$pdf->AddPage($information_orientation);
	$pdf->SetFont('simsun','B',10);
	$bottom=$pdf->PrintBody();
	
	$bottom=$pdf->PrintBottom();
	$pdf->Output();
?>
