<?php
 session_start();
    
   include("../pdate.php");
   include("db.php");
  /*  $todayDate= pdate("Y-m-d");
   $date = new DateTime('-30 day'); */
   $startDay=$_POST['day'];
   $startMonth=$_POST['month'];
   $startyear=$_POST['year'];
   
   $startDate=$startyear."-".$startMonth."-".$startDay;
    
   $endDay=$_POST['eday'];
   $endMonth=$_POST['emonth'];
   $endyear=$_POST['eyear'];
   
   $endDate=$endyear."-".$endMonth."-".$endDay;
  
  
  
  //$customerName=$_SESSION['customerName'];
 // $customar_id=$_SESSION['customar_code'];
   $customer_id=$_GET['customerID'];
    
   $get_currency_id=$_GET['currency_id'];
   
   $counterCurrency=$_POST['counterCurrency'];
  
  if(isset($get_currency_id)){
	 $counterCurrency=$get_currency_id; 
  }
 //============================================
  $customerName_sql="select * from customers where id='$customer_id'";
  $customerName_query=$db ->query($customerName_sql);
  $customerName_row=$customerName_query ->fetch_array();
  $customerName=$customerName_row['name'];
   //============================================
   $selectCurrency = "select * from currencies where id  = '$counterCurrency'";
   $currenciesQuery = $db->query($selectCurrency);
   $selectCurrencyRow = $currenciesQuery->fetch_array();
   $currencyName = $selectCurrencyRow['name'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<title>صورت حساب مشتری</title>
<head>
   <link rel="icon" href="../images/favicon.ico" type="image/png"/>  
   <link rel="stylesheet" href="table_style/table_style7.css">
   <link href="../css/pagenation.css" rel="stylesheet" type="text/css" /> 
<script>
function myconfirm()
{
    if(confirm('آیا مطمئن هستید می خواهید اطلاعات را به طور دایم حذف کنید'))
        return true;
    return false;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../css/print.css" /> 
<!-- ===================ÇíÌÇÏ íÇã ÈÑÇí æíÑÇíÔ ÝÇíá====================================-->
<script type="text/javascript" src="../change/edite/revenue_edite.js"></script> 
<script type="text/javascript" src="../change/delete/revenue_delete.js"></script>
 
<!--======================= ÇíÇä  ============================================================-->
 <style>
 a:link {
    text-decoration: none;
	color: #000;
}

 a:hover {
    color: red;
	text-decoration: underline;
}
a:active {
     
    color: #000;
}

@font-face {
	  font-family: 'hekmat';
	  src: url('../font/BHoma.ttf'); /* IE */
      src: local('BHoma-Bold'), url('../font/BHoma.ttf') format('truetype'); /* non-IE */
      font-weight:normal;
}
.font{
    color: #000;
    font-family: 'hekmat';  

}
@media print {

   .dontPrint {
       display:none;
    }

    }
	
	@media all{
.page-break{display:none;}
}
@media print{
.page-break{display:block;page-break-after:always;}
.dote img {visibility: visible;}
}
 
@page 
{
   size: auto;   /* auto is the initial value */
    margin: 3mm;  /* this affects the margin in the printer settings */
 }
  @media print {
    html, body {
        height: 99%;    
    }
}
 </style>

</head>
	<body onkeydown = "onKeyDown()" onKeyPress="return disableEnterKey(event)">
 <?php  //include("r_c_d_s_p/back_print_save_refresh.php"); ?>
<div id="wrap">
<table border="0" align="center"   width="950px" dir="rtl" class="font">
		<tr>
			<th style="width:166px;text-align:right">
			  
			<?php 
			   
			  
			   
			     echo '</th>
			     <th style="width:206px"><h2>'?> <?php include("../info/companyName.php") ?></h2><?php echo 'صورت حساب تاریخ وار محترم&nbsp;('.ucfirst($customerName).")";
			      
				  echo '</br> '.pdate("Y-m-d" ).'</th>';

				  ?>
			
			<th style="width:186px;text-align:left" > </th>
			 
		</tr>
		</table> 

 <div id='content'>
 

	<?php  
	    
   include("db.php");
	 
   //$customer_id = $_SESSION['customar_code'];
	 
    
	$tableName="customer_statement";		
	$targetpage = "customer_statement.php"; 	
	$limit = 23000; 
	
	 $sql="SELECT COUNT(*) as num FROM `$tableName` where customerId='$customer_id' ";
	
	$query= $db ->query($sql);
	$total_pages= $query ->fetch_array();
	
	 
	$total_pages = $total_pages[num];
	
	$stages = 3;
	$page = mysql_escape_string($_GET['page']);
	if($page){
		$start = ($page - 1) * $limit; 
	}else{
		$start = 0;	
		}	
	//================================================inter page cods HERE===============================================
    // Get page data
		
	  //echo "<div id='content'>";
	
	  echo'
			<table id="box-table-a"   dir="rtl"  align="center" >
	        <thead> 
				<th scope="col">شماره</th>
				<th scope="col">تاریخ</th>
				<th scope="col">واحد پولی</th>
				<th scope="col">مرجع</th>
				<th scope="col">رسیدگی</th>
				<th scope="col">بردگی</th>
				<th scope="col">حساب کنونی</th>
			</thead>
				 ';
    // Get page data
	 echo "<div id='content'>";
	  
			                include('db.php');
				         

						 

							
						 $table_name="customer_statement";
						 //======================================================================
						  //beforDate
						  //customer credit balance
						  $befor_statment_sql="SELECT sum(amount)  FROM `customer_statement` where customerId='$customer_id' and transaction_type='2' and currency_id='$counterCurrency' and (date < '$startDate')    ";
						  $befor_statment_query= $db ->query($befor_statment_sql);
				          $befor_statment_row=$befor_statment_query ->fetch_array();
						  $befor_statment=$befor_statment_row['sum(amount)'];
						 
						  //customer debit balance
						  $befor_statment_Debit_sql="SELECT sum(amount)  FROM `customer_statement` where customerId='$customer_id' and transaction_type='1' and currency_id='$counterCurrency' and (date <  '$startDate')    ";
						  $befor_statment_Debit_query= $db ->query($befor_statment_Debit_sql);
				          $befor_statment_Debit_row=$befor_statment_Debit_query ->fetch_array();
						  $befor_statment_Debit=$befor_statment_Debit_row['sum(amount)'];
						  
						  $accountBefor=$befor_statment_Debit-$befor_statment;
						  if($accountBefor>0){
						    $beforDebit=$accountBefor;
							$beforCredit=0;
						  }
						  if($accountBefor<0){
						    $beforCredit=$accountBefor;
							$beforDebit=0;
						  }
						  $remian = $befor_statment_Debit-$befor_statment;
						 echo "<td>".$ii."</td>
							<td>".$startDate."</td>
							<td>".$currencyName."</td>
							<td>مجموع از حسابات قبلی</td>
							<td>".number_format($beforDebit)."</td> 
							<td>".number_format($beforCredit)."</td> 
							<td>".number_format($remian)."</td>";
						 //======================================================================
					 
						 $mony_sql="SELECT   *  FROM `customer_statement` where customerId='$customer_id' and currency_id='$counterCurrency' and (date between '$startDate' and '$endDate')   order by date asc   LIMIT $start, $limit";
						 $mony_query= $db ->query($mony_sql);
				  
						  
                          while($nrow=$mony_query ->fetch_array()){
						    $ii++;
							$reference = $nrow['reference'];
							$amount = $nrow['amount'];
							$date = $nrow['date'];
							$currency_id = $nrow['currency_id'];
							$description = $nrow['description'];
							//select currency
							$selectCurrency = "select * from currencies where id  = '$currency_id'";
							$currenciesQuery = $db->query($selectCurrency);
							$selectCurrencyRow = $currenciesQuery->fetch_array();
							$currencyName = $selectCurrencyRow['name'];
						    
						
							$transacttype=$nrow['transaction_type'];
							if($transacttype==1){
								$debit=$amount;
								 
								$credit=0;
								$balance_debit+=$debit;
								 
							}
							if($transacttype==2){
								 
									 $credit=$amount;
									  
									 $debit=0;
								 
								 $balance_credit+=$credit;
								 
							}
							 $remian = ($balance_debit+$beforDebit)-($balance_credit+(-$beforCredit));
							// $remian = ($balance_debit)-($balance_credit);
							 
							//$ramian+=$debit-$credit;
			                
							echo "<tbody>
							<tr>
							<td>".$ii."</td>
							<td>".$date."</td>
							<td>".$currencyName."</td>
							<td>".$description."</td>
							<td>".number_format($debit)."</td> 
							<td>".number_format($credit)."</td> 
							<td>".number_format($remian)."</td>";
		    					  
				 	  } 
							 echo '<tr style="color: red">
								 <td colspan="13"  style="text-align: right;"><hr style="border:1px solid #000;"> 
								 
								 
								 
								 </td>';
							 
		 
	 echo'    	</table>';
	 echo "<br></div>";
	 echo "<div class='dontPrint'>";
//====================================================================================================================
	// Initial page num setup
	if ($page == 0){$page = 1;}
	$prev = $page - 1;	
	$next = $page + 1;							
	$lastpage = ceil($total_pages/$limit);		
	$LastPagem1 = $lastpage - 1;					
	
	
	$paginate = '';
	if($lastpage > 1)
	{	
	

	
	
		$paginate .= "<div class='paginate'>";
		// Previous
		if ($page > 1){
			$paginate.= "<a href='$targetpage?page=$prev&customerID=$customer_id&currency_id=$counterCurrency'>previous</a>";
		}else{
			$paginate.= "<span class='disabled'>previous</span>";	}
			

		
		// Pages	
		if ($lastpage < 7 + ($stages * 2))	// Not enough pages to breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page){
					$paginate.= "<span class='current'>$counter</span>";
				}else{
					$paginate.= "<a href='$targetpage?page=$counter&customerID=$customer_id&currency_id=$counterCurrency'>$counter</a>";}					
			}
		}
		elseif($lastpage > 5 + ($stages * 2))	// Enough pages to hide a few?
		{
			// Beginning only hide later pages
			if($page < 1 + ($stages * 2))		
			{
				for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?page=$counter&customerID=$customer_id&currency_id=$counterCurrency'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?page=$LastPagem1&customerID=$customer_id&currency_id=$counterCurrency'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?page=$lastpage&customerID=$customer_id&currency_id=$counterCurrency'>$lastpage</a>";		
			}
			// Middle hide some front and some back
			elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
			{
				$paginate.= "<a href='$targetpage?page=1&customerID=$customer_id&currency_id=$counterCurrency'>1</a>";
				$paginate.= "<a href='$targetpage?page=2&customerID=$customer_id&currency_id=$counterCurrency'>2</a>";
				$paginate.= "...";
				for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?page=$counter&customerID=$customer_id&currency_id=$counterCurrency'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?page=$LastPagem1&customerID=$customer_id&currency_id=$counterCurrency'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?page=$lastpage&customerID=$customer_id&currency_id=$counterCurrency'>$lastpage</a>";		
			}
			// End only hide early pages
			else
			{
				$paginate.= "<a href='$targetpage?page=1&customerID=$customer_id&currency_id=$counterCurrency'>1</a>";
				$paginate.= "<a href='$targetpage?page=2&customerID=$customer_id&currency_id=$counterCurrency'>2</a>";
				$paginate.= "...";
				for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?page=$counter&customerID=$customer_id&currency_id=$counterCurrency'>$counter</a>";}					
				}
			}
		}
					
				// Next
		if ($page < $counter - 1){ 
			$paginate.= "<a href='$targetpage?page=$next&customerID=$customer_id&currency_id=$counterCurrency'>next</a>";
		}else{
			$paginate.= "<span class='disabled'>next</span>";
			}
			
		$paginate.= "</div>";		
	
	
}
 
 echo $paginate;
//===================================================================================================================================				 			
  ?> 
	
			
 </tbody></table>
</div>
</div>  
 </body>
 </form>
 </html>  
  
 
     