<?php
 session_start();
   include("db.php");
   include("../jdf.php");
    $counterCurrency=$_POST['counterCurrency'];
  
    if($counterCurrency!="all"){
     $customerTotal_sql="select sum(amount) from customerbalance where currencyId='$counterCurrency' and amount>0 ";
     $customerTotal_query= $db ->query($customerTotal_sql);
     $customerTotal_row=$customerTotal_query ->fetch_array();
	 $customerTotal=number_format($customerTotal_row['sum(amount)'],2);
	 
	 
  }  
   $get_customerTotal=$_GET['customerTotal'];
   
   if(isset($get_customerTotal)){
	   $customerTotal= $get_customerTotal;  
   }
   
   
?>
<!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 href="../css/pagenation.css" rel="stylesheet" type="text/css" /> 
   <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" /> 

<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';  

}
a{
    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" >
<form method="post" action="">
<table border="0" style="width: 800px;margin: auto;" dir="rtl" >
<tr>
    <td>از تاریخ</td>
    <td>
        
        
                        <?php
                        $todayDate= $pdate1;
                         if($todayDate==null){
                             $todayDate=jdate("Y-m-d");
                          } 
                          $explode=explode("-",$todayDate);
                          $year=$explode[0];
                           
                          $mah=$explode[1];
                           
                          $day=$explode[2];
  
                        
                        ?>
                        <select id="d"  name="day"  id="adad"  style ="width:90px;color: #000;height:30px"  >
					    <option <?php if($day==1){ echo "selected";} ?> value="01">1</option>
						<option <?php if($day==2){ echo "selected";} ?> value="02">2</option>
						<option <?php if($day==3){ echo "selected";} ?> value="03">3</option>
						<option <?php if($day==4){ echo "selected";} ?> value="04">4</option>
						<option <?php if($day==5){ echo "selected";} ?> value="05">5</option>
						<option <?php if($day==6){ echo "selected";} ?> value="06">6</option>
						<option <?php if($day==7){ echo "selected";} ?> value="07">7</option>
						<option <?php if($day==8){ echo "selected";} ?> value="08">8</option>
						<option <?php if($day==9){ echo "selected";} ?> value="09">9</option>
						<option <?php if($day==10){ echo "selected";} ?> value="10">10</option>
						<option <?php if($day==11){ echo "selected";} ?> value="11">11</option>
						<option <?php if($day==12){ echo "selected";} ?> value="12">12</option>
						<option <?php if($day==13){ echo "selected";} ?> value="13">13</option>
						<option <?php if($day==14){ echo "selected";} ?> value="14">14</option>
						<option <?php if($day==15){ echo "selected";} ?> value="15">15</option>
						<option <?php if($day==16){ echo "selected";} ?> value="16">16</option>
						<option <?php if($day==17){ echo "selected";} ?> value="17">17</option>
						<option <?php if($day==18){ echo "selected";} ?> value="18">18</option>
						<option <?php if($day==19){ echo "selected";} ?> value="19">19</option>
						<option <?php if($day==20){ echo "selected";} ?> value="20">20</option>
						<option <?php if($day==21){ echo "selected";} ?> value="21">21</option>
						<option <?php if($day==22){ echo "selected";} ?> value="22">22</option>
						<option <?php if($day==23){ echo "selected";} ?> value="23">23</option>
						<option <?php if($day==24){ echo "selected";} ?> value="24">24</option>
						<option <?php if($day==25){ echo "selected";} ?> value="25">25</option>
						<option <?php if($day==26){ echo "selected";} ?> value="26">26</option>
						<option <?php if($day==27){ echo "selected";} ?> value="27">27</option>
						<option <?php if($day==28){ echo "selected";} ?> value="28">28</option>
						<option <?php if($day==29){ echo "selected";} ?> value="29">29</option>
						<option <?php if($day==30){ echo "selected";} ?> value="30">30</option>
						<option <?php if($day==31){ echo "selected";} ?> value="31">31</option>
					
					 
				</select>
				 
				 
					<select id="m"  style="width:90px;color: #000;height:30px" name="month" >
						 
						 <option  <?php if($mah==1) { echo "selected"; } ?>   value ='01' >1</option>  
						 <option  <?php if($mah==2) { echo "selected"; } ?>   value ='02' >2</option>  
						 <option  <?php if($mah==3) { echo "selected"; } ?>   value ='03' >3</option>  
						 <option  <?php if($mah==4) { echo "selected"; } ?>   value ='04' >4</option>  
						 <option  <?php if($mah==5) { echo "selected"; } ?>   value='05'  >5</option>  
						 <option  <?php if($mah==6) { echo "selected"; } ?>   value ='06' >6</option>  
						 <option  <?php if($mah==7) { echo "selected"; } ?>   value='07'  >7</option>  
						 <option  <?php if($mah==8) { echo "selected"; } ?>   value='08'  >8</option>  
						 <option  <?php if($mah==9) { echo "selected"; } ?>   value ='09' >9</option>  
						 <option  <?php if($mah==10) { echo "selected"; } ?>  value ='10' >10</option>  
						 <option  <?php if($mah==11) { echo "selected"; } ?>  value ='11' >11</option>  
						 <option  <?php if($mah==12) { echo "selected"; } ?>  value ='12' >12</option>  
				</select>
				    <select  id="y"   name="year" style="width:90px;color: #000;height: 30px" >
						 
					   <?php
					   for($i=1400;$i<1410;$i++){
						   if($i==$year){
							   	echo "<option selected value=$i>$i</option>";
						   }else{
							    echo "<option  value=$i>$i</option>"; 

						   }
					   }
					   
					   
					   ?>
					
				</select>
    
    
    </td>
    <td> تا تاریخ</td>
     <td> 
     
       <select id="d"  name="eday"  id="adad"  style ="width:95px;color: #000;height:30px"  >
					<option <?php if($day==1){ echo "selected";} ?> value="01">1</option>
						<option <?php if($day==2){ echo "selected";} ?> value="02">2</option>
						<option <?php if($day==3){ echo "selected";} ?> value="03">3</option>
						<option <?php if($day==4){ echo "selected";} ?> value="04">4</option>
						<option <?php if($day==5){ echo "selected";} ?> value="05">5</option>
						<option <?php if($day==6){ echo "selected";} ?> value="06">6</option>
						<option <?php if($day==7){ echo "selected";} ?> value="07">7</option>
						<option <?php if($day==8){ echo "selected";} ?> value="08">8</option>
						<option <?php if($day==9){ echo "selected";} ?> value="09">9</option>
						<option <?php if($day==10){ echo "selected";} ?> value="10">10</option>
						<option <?php if($day==11){ echo "selected";} ?> value="11">11</option>
						<option <?php if($day==12){ echo "selected";} ?> value="12">12</option>
						<option <?php if($day==13){ echo "selected";} ?> value="13">13</option>
						<option <?php if($day==14){ echo "selected";} ?> value="14">14</option>
						<option <?php if($day==15){ echo "selected";} ?> value="15">15</option>
						<option <?php if($day==16){ echo "selected";} ?> value="16">16</option>
						<option <?php if($day==17){ echo "selected";} ?> value="17">17</option>
						<option <?php if($day==18){ echo "selected";} ?> value="18">18</option>
						<option <?php if($day==19){ echo "selected";} ?> value="19">19</option>
						<option <?php if($day==20){ echo "selected";} ?> value="20">20</option>
						<option <?php if($day==21){ echo "selected";} ?> value="21">21</option>
						<option <?php if($day==22){ echo "selected";} ?> value="22">22</option>
						<option <?php if($day==23){ echo "selected";} ?> value="23">23</option>
						<option <?php if($day==24){ echo "selected";} ?> value="24">24</option>
						<option <?php if($day==25){ echo "selected";} ?> value="25">25</option>
						<option <?php if($day==26){ echo "selected";} ?> value="26">26</option>
						<option <?php if($day==27){ echo "selected";} ?> value="27">27</option>
						<option <?php if($day==28){ echo "selected";} ?> value="28">28</option>
						<option <?php if($day==29){ echo "selected";} ?> value="29">29</option>
						<option <?php if($day==30){ echo "selected";} ?> value="30">30</option>
						<option <?php if($day==31){ echo "selected";} ?> value="31">31</option>
					
					 
				</select>
				 
				 
					<select id="m"  style="width:100px;color: #000;height:30px" name="emonth" >
						 
						 <option  <?php if($mah==1) { echo "selected"; } ?>   value ='01' >1</option>  
						 <option  <?php if($mah==2) { echo "selected"; } ?>   value ='02' >2</option>  
						 <option  <?php if($mah==3) { echo "selected"; } ?>   value ='03' >3</option>  
						 <option  <?php if($mah==4) { echo "selected"; } ?>   value ='04' >4</option>  
						 <option  <?php if($mah==5) { echo "selected"; } ?>   value='05'  >5</option>  
						 <option  <?php if($mah==6) { echo "selected"; } ?>   value ='06' >6</option>  
						 <option  <?php if($mah==7) { echo "selected"; } ?>   value='07'  >7</option>  
						 <option  <?php if($mah==8) { echo "selected"; } ?>   value='08'  >8</option>  
						 <option  <?php if($mah==9) { echo "selected"; } ?>   value ='09' >9</option>  
						 <option  <?php if($mah==10) { echo "selected"; } ?>  value ='10' >10</option>  
						 <option  <?php if($mah==11) { echo "selected"; } ?>  value ='11' >11</option>  
						 <option  <?php if($mah==12) { echo "selected"; } ?>  value ='12' >12</option>  
				</select>
				    <select  id="y"   name="eyear" style="width:100px;color: #000;height: 30px" >
						 
					   <?php
					   for($i=1400;$i<1410;$i++){
						   if($i==$year){
							   	echo "<option selected value=$i>$i</option>";
						   }else{
							    echo "<option  value=$i>$i</option>"; 

						   }
					   }
					   
					   
					   ?>
					
				</select>
     
     
     
     
     </td>
    <td><input name='submit' src='../img/prent.png' type='image' border='0'  style='width:40px; height:40px;align:left'  title='Print'     class='dontPrint' onclick='window.print();return false;'/> </td> 
    <td><input type="submit" name="submit" value="جستجو" ></td>
</tr>
</table>
</form>
<table border="0" align="center"   width="100%" dir="rtl" class="font">
		<tr>
			<th style="width:166px;text-align:right">
			   
			</th>	 
			
			
			
			
			<?php 
			    $start_day=$_POST['day'];
				$start_month=$_POST['month'];
				$start_year=$_POST['year'];
				 
				$startDate=$start_year."-".$start_month."-".$start_day;
				$end_day=$_POST['eday'];
				$end_month=$_POST['emonth'];
				$end_year=$_POST['eyear'];
				 
				$endDate=$end_year."-".$end_month."-".$end_day;
			     echo '</th>
				<th style="width:206px"><h2>';
				 include("../info/companyName.php");
				echo '</h2>لیست طلبکاران مشتریان از تاریخ&nbsp;(<span dir="rtl">'.$startDate.' تا تاریخ <span dir="ltr">'.$endDate.'</span>)<br></span>';
			    
				 
				 
			  
			  
			  ?>
			
			<th style="width:186px;text-align:left;height: 40px" > </th>
			 
		</tr>
		</table> 

 <div id='content'>
 
  <?php
   if($_POST['submit']){
	 
    include("db.php");
	$tableName="havale";		
	$targetpage = "havale_Customer_Report.php"; 	
	 
	
	  echo'
			<table id="box-table-a"   dir="rtl"  align="center" >
	        <tr> 
				<th>شماره</th>
				<th>کد مشتری</th>
				<th>نام مشتری</th> 
				<th>دالر</th> 
				<th>افغانی</th> 
				<th>تومان</th>
                <th>کالدار</th>				
				<th>یورو</th> 
				';
			    
			    echo "</tr>";
				 
						 
						 $customerName_sql="SELECT  *  FROM  customers   ";
						 $customerName_query= $db ->query($customerName_sql);
				  
						  $ii=$countId;
                          while($customerName_row=$customerName_query ->fetch_array()){
						    $ii++;
							 
							$id= $customerName_row['id'];
							$customerName= $customerName_row['name'];
							
							//===========usd====================================
							$cr_customerAmount_usd_sql="select sum(amount) as amount from customer_statement where customerId='$id' and transaction_type='1' and (date between '$startDate' and '$endDate') and currency_id='2' ";
							$cr_customerAmount_usd_query= $db ->query($cr_customerAmount_usd_sql);
                            $cr_customerAmount_usd_row=$cr_customerAmount_usd_query ->fetch_array();
                            $cr_customerAmount_usd=$cr_customerAmount_usd_row['amount'];
                             
							$dr_customerAmount_usd_sql="select sum(amount) as amount from customer_statement where customerId='$id' and transaction_type='2' and (date between '$startDate' and '$endDate') and currency_id='2' ";
							$dr_customerAmount_usd_query= $db ->query($dr_customerAmount_usd_sql);
                            $dr_customerAmount_usd_row=$dr_customerAmount_usd_query ->fetch_array();
                            $dr_customerAmount_usd=$dr_customerAmount_usd_row['amount'];

                           	$balance_usd=$cr_customerAmount_usd-$dr_customerAmount_usd;
                            			
                            //=============usd=======================================
							//===========afn====================================
							$cr_customerAmount_afn_sql="select sum(amount) as amount from customer_statement where customerId='$id' and transaction_type='1' and (date between '$startDate' and '$endDate') and currency_id='1' ";
							$cr_customerAmount_afn_query= $db ->query($cr_customerAmount_afn_sql);
                            $cr_customerAmount_afn_row=$cr_customerAmount_afn_query ->fetch_array();
                            $cr_customerAmount_afn=$cr_customerAmount_afn_row['amount'];
                            
							$dr_customerAmount_afn_sql="select sum(amount) as amount from customer_statement where customerId='$id' and transaction_type='2' and (date between '$startDate' and '$endDate') and currency_id='1' ";
							$dr_customerAmount_afn_query= $db ->query($dr_customerAmount_afn_sql);
                            $dr_customerAmount_afn_row=$dr_customerAmount_afn_query ->fetch_array();
                            $dr_customerAmount_afn=$dr_customerAmount_afn_row['amount'];

                           	$balance_afn=$cr_customerAmount_afn-$dr_customerAmount_afn;
                             							
                            //=============afn=======================================
						 
                           //===========IRR====================================
							$cr_customerAmount_irr_sql="select sum(amount) as amount from customer_statement where customerId='$id' and transaction_type='1' and (date between '$startDate' and '$endDate') and currency_id='5' ";
							$cr_customerAmount_irr_query= $db ->query($cr_customerAmount_irr_sql);
                            $cr_customerAmount_irr_row=$cr_customerAmount_irr_query ->fetch_array();
                            $cr_customerAmount_irr=$cr_customerAmount_irr_row['amount'];
                            
							$dr_customerAmount_irr_sql="select sum(amount) as amount from customer_statement where customerId='$id' and transaction_type='2' and (date between '$startDate' and '$endDate') and currency_id='5' ";
							$dr_customerAmount_irr_query= $db ->query($dr_customerAmount_irr_sql);
                            $dr_customerAmount_irr_row=$dr_customerAmount_irr_query ->fetch_array();
                            $dr_customerAmount_irr=$dr_customerAmount_irr_row['amount'];

                           	$balance_irr=$cr_customerAmount_irr-$dr_customerAmount_irr;	
                            //=============irr=======================================
                            //===========Euro====================================
							$cr_customerAmount_euro_sql="select sum(amount) as amount from customer_statement where customerId='$id' and transaction_type='1' and (date between '$startDate' and '$endDate') and currency_id='3' ";
							$cr_customerAmount_euro_query= $db ->query($cr_customerAmount_euro_sql);
                            $cr_customerAmount_euro_row=$cr_customerAmount_euro_query ->fetch_array();
                            $cr_customerAmount_euro=$cr_customerAmount_euro_row['amount'];
                            
							$dr_customerAmount_euro_sql="select sum(amount) as amount from customer_statement where customerId='$id' and transaction_type='2' and (date between '$startDate' and '$endDate') and currency_id='3' ";
							$dr_customerAmount_euro_query= $db ->query($dr_customerAmount_euro_sql);
                            $dr_customerAmount_euro_row=$dr_customerAmount_euro_query ->fetch_array();
                            $dr_customerAmount_euro=$dr_customerAmount_euro_row['amount'];

                           	$balance_euro=$cr_customerAmount_euro-$dr_customerAmount_euro;	
                            //=============Euro=======================================
                            //===========pkk====================================
							$cr_customerAmount_pk_sql="select sum(amount) as amount from customer_statement where customerId='$id' and transaction_type='1' and (date between '$startDate' and '$endDate') and currency_id='4' ";
							$cr_customerAmount_pk_query= $db ->query($cr_customerAmount_pk_sql);
                            $cr_customerAmount_pk_row=$cr_customerAmount_pk_query ->fetch_array();
                            $cr_customerAmount_pk=$cr_customerAmount_pk_row['amount'];
                            
							$dr_customerAmount_pk_sql="select sum(amount) as amount from customer_statement where customerId='$id' and transaction_type='2' and (date between '$startDate' and '$endDate') and currency_id='4' ";
							$dr_customerAmount_pk_query= $db ->query($dr_customerAmount_pk_sql);
                            $dr_customerAmount_pk_row=$dr_customerAmount_pk_query ->fetch_array();
                            $dr_customerAmount_pk=$dr_customerAmount_pk_row['amount'];

                           	$balance_pk=$cr_customerAmount_pk-$dr_customerAmount_pk;	
                            //=============pkk=======================================
 


							
			 		 		 
			 		 
			echo "<tbody>
			<tr>";
			if( ($balance_usd!=0 and  $balance_usd>0) or ($balance_afn!=0 and $balance_afn>0) or ($balance_irr!=0 and $balance_irr>0)   or ($balance_pk!=0 and $balance_pk>0) or  ($balance_euro!=0 and $balance_euro>0) ){
			echo "<td>".$ii."</td>
			<td>".$id."</td> 
		    <td>".$customerName."</td>";
			
			//=========================
			if($balance_usd>0){
			   echo "<td>".number_format($balance_usd)."</td>"; 
			   $totalUsd+=$balance_usd;
			}
			if($balance_usd<0){
			   echo "<td></td>"; 
			}
			if($balance_usd==0){
			   echo "<td></td>"; 
			}
		   //============================
		   //=========================
			if($balance_afn>0){
			   echo "<td>".number_format($balance_afn)."</td>"; 
			   $totalAfn+=$balance_afn;
			}
			if($balance_afn<0){
			   echo "<td></td>"; 
			}
			if($balance_afn==0){
			   echo "<td></td>"; 
			}
		   //============================
		   //=========================
			if($balance_irr>0){
			   echo "<td>".number_format($balance_irr)."</td>"; 
			   $totalIrr+=$balance_irr;
			}
			if($balance_irr<0){
			   echo "<td></td>"; 
			}
			if($balance_irr==0){
			   echo "<td></td>"; 
			}
		   //============================
		   //=========================
			if($balance_pk>0){
			   echo "<td>".number_format($balance_pk)."</td>"; 
			   $totalPk+=$balance_pk;
			}
			if($balance_pk<0){
			   echo "<td></td>"; 
			}
			if($balance_pk==0){
			   echo "<td></td>"; 
			}
			
		   //============================
		    //=========================
			if($balance_euro>0){
			   echo "<td>".number_format($balance_euro)."</td>";
               $totalEuro+=$balance_euro;			   
			}
			if($balance_euro<0){
			   echo "<td></td>"; 
			}
			if($balance_euro==0){
			   echo "<td></td>"; 
			}
			
			
		   //============================
		   
		  echo "</tr>";	
            }
                
             }	 
				 	    
		}
		echo "<tr>
		          <td colspan='3' style='background: silver'>مجموع</td>
				  <td style='background: silver'>".number_format($totalUsd)."</td>
				  <td style='background: silver'>".number_format($totalAfn)."</td>
				  <td style='background: silver'>".number_format($totalIrr)."</td>
				  <td style='background: silver'>".number_format($totalPk)."</td>
				   <td style='background: silver'>".number_format($totalEuro)."</td>
			</tr>";
	 echo'</table>';
 ?>
</body>
</html>  
 
     