<?php
  ob_start();
 session_start();
   
   include("../pdate.php");
   include("../db.php");
   $todayDate= pdate("Y-m-d");
   $date = new DateTime('-30 day');

    
  $startDate=$date->format('Y-m-d'); 
  $startDate=pdate("Y-m-d"); 
  
   
   $customer_id=$_GET['customerID'];
   $get_currency_id=$_GET['currency_id'];
   
   $counterCurrency=$_POST['counterCurrency'];
  
  if($get_currency_id!=null){
	 $counterCurrency=$get_currency_id; 
  }
 //====customerName================================
   $customerName_sql="SELECT * FROM `customers` WHERE id='$customer_id'";
   $customerName_query= $db->query($customerName_sql);
   $customreName_row=$customerName_query->fetch_array();
   $customerName=$customreName_row['name'];
 
//====customerName================================
?>
<!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") ?><?php echo '</h2>صورت حساب محترم:&nbsp;'.ucfirst($customerName);
			       
				  echo '<br>';
				  echo pdate("Y-m-d" );
				  echo '</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"; 	
	 
	//================================================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
	                   //=primery account=============
					   $credit_sql="select sum(amount) as amount FROM `customer_statement` where customerId='$customer_id' and currency_id='$counterCurrency' and amount!=0 and check_point=1  and transaction_type=1 ";
					   $credit_query= $db ->query($credit_sql);
					   $credit_row=$credit_query ->fetch_array();
					   $befor_statment_Debit=$credit_row['amount'];
					   
					   $debit_sql="select sum(amount) as amount FROM `customer_statement` where customerId='$customer_id' and currency_id='$counterCurrency' and amount!=0 and check_point=1  and transaction_type=2 ";
					   $debit_query= $db ->query($debit_sql);
					   $debit_row=$debit_query ->fetch_array();
					   $befor_statment=$debit_row['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;
					      $befor = $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 style='text-align: center' dir='ltr'>".number_format($remian)."</td>";
	                   //=primery account=============
			                 
						 $table_name="customer_statement";
						 $mony_sql="SELECT   *  FROM `customer_statement` where customerId='$customer_id' and currency_id='$counterCurrency' and amount!=0 and check_point=0   order by date asc  ";
						 $mony_query= $db ->query($mony_sql);
				  
						  
                          while($nrow=$mony_query ->fetch_array()){
						    $ii++;
							$id = $nrow['id'];
							$reference = $nrow['reference'];
							$amount = $nrow['amount'];
							$date = $nrow['date'];
							$currency_id = $nrow['currency_id'];
							$description = $nrow['description'];
							$retrive_check=$nrow['check'];
							//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_king=$amount;  
								     $debit=0;
								     //if($credit_king<0){$credit=-$amount;}
								     //if($credit_king>0){$credit=$amount;}
								     $credit=$amount; 
								     $balance_credit+=$credit;
									 
									  
							}
						
							echo "<form method='post' action='take_check_cu_st.php'>"; 
			                $remian = ($balance_debit-$balance_credit)+$befor;
						   
							echo "<tbody>
							<tr>
							<td>".$ii."</td>
							<td>".$date."</td>
							<td>".$currencyName."</td>
							<td style='font-size: 15px'>".$description."</td>
							<td>".number_format($debit)."</td> 
							<td>".number_format($credit)."</td> 
							<td style='text-align: center' dir='ltr'>".number_format($remian)."</td>";
							 
							  
							
		    					$totalDebit+=$debit;  
		    					$totalCredit+=$credit;
                                $remain_calculat=$totalDebit-$totalCredit;								
				 	  } 
					    if($befor>0){$totalDebit=$totalDebit+$befor;}
					    if($befor<0){$totalCredit=$totalCredit+(-$befor);}
						$remain_calculat=$totalDebit-$totalCredit;
					   echo "<tr>
					            <td colspan='4' style='background: #ffffb3' >مجموع</td>
					            <td style='background: #ffffcc;text-align: right' dir='ltr'>".$totalDebit."</td>
					            <td style='background: #ffffcc'>$totalCredit</td>
					            <td colspan='2' style='background: #ffffb3;text-align: center' dir='ltr' >$remain_calculat</td>
					        </tr>";
							 echo '<tr style="color: red">
								 <td colspan="13"  style="text-align: right;"><hr style="border:1px solid #000;"> 
								 
								  
				                  <input type="submit" name="submit_checking" value="ذخیره"> 
								 </td>';
							 
		 
	 echo'    	</table></form>';
	  
 
//===================================================================================================================================				 			
  ?> 
	
			
 </tbody></table>
</div>
</div>  
 </body>
 </form>
 </html>  
 