<?php

       
     include("db.php");  
     $date=$_GET['date'];
     $counterCurrency=$_GET['counterCurrency'];
     $baseCurrency_id=$_GET['baseCurrency'];
     //==========
	  $currencyName_kharid_sql="select * from currencies where id='$counterCurrency' ";
	  $currencyName_kharid_query= $db ->query($currencyName_kharid_sql);
	  $currencyName_kharid_row=$currencyName_kharid_query ->fetch_array();
	  $currencyName_kharid=$currencyName_kharid_row['name'];
     //==========
     $base_currencyName_sql="select name from currencies where id='$baseCurrency_id'";
      $base_currencyName_query= $db ->query($base_currencyName_sql);
	  $base_currencyName_row=$base_currencyName_query->fetch_array();
	  $base_currencyName=$base_currencyName_row['name'];

?>
<!DOCTYPE html>
 
<html lang="en" class="no-js">
<!--<![endif]-->
<!-- BEGIN HEAD -->
<head>
  
<meta charset="utf-8"/>
<title><?php echo $page_title;?></title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1" name="viewport"/>
<meta content="" name="description"/>
<meta content="" name="author"/>
<style>
 #wrap{
   width: 900px;
   margin: auto;
   border: 0px solid red;
  }
  #title{
   width: 900px;
   margin: auto;
   border: 0px solid green;
  }
  table{
   margin: auto;
   direction: rtl;
   width: 100%;
  
  
  }
  
</style>
</head>
 <body>
 <div id="wrap">
    <div id="title"> 
	<table border="0" style="width: 100%;text-align: center">
	<tr>
	   <td>خریداری</td>
	 </tr>
	 <tr>
	   <td><?php echo $currencyName_kharid ?>به <?php echo $base_currencyName;?></td>
	 </tr>
	 
	 <tr>
	    <td><?php echo $date; ?></td>
	 </tr>
	 </table>
	
	</div>
	<table border="1" >
	<tr>
	    <td>خریداری</td>
	    <td>مقدار</td>
	    <td>قیمت</td>
	    <td>پول فروخته شده</td>
	    <td>مقدار</td>
	 </tr>
 </div><!--wrap-->
<?php


     // $date=$_POST['date'];
	
	   
      $tread_sql="select *  from journal where date='$date' and currency='$counterCurrency' and dr_cr=1";
      $tread_query= $db ->query($tread_sql);
	  while($tread_row=$tread_query->fetch_array()){
	   
	  $currency=$tread_row['currency'];
	  $amount=$tread_row['amount'];
	  $reference_id=$tread_row['reference_id'];
	  $allAmount+= $amount;
	  //currencyName
	  $currencyName_kharid_sql="select * from currencies where id='$currency' ";
	  $currencyName_kharid_query= $db ->query($currencyName_kharid_sql);
	  $currencyName_kharid_row=$currencyName_kharid_query ->fetch_array();
	  $currencyName_kharid=$currencyName_kharid_row['name'];
	  //end currencyName
	  //finding details tread
	  $details_tread_sql="select * from transaction where id='$reference_id' and treatment=1 and baseCurrency='$baseCurrency_id'";
	  $details_tread_query = $db ->query($details_tread_sql);
	  $details_tread_row=$details_tread_query ->fetch_array();
	  
	  $rate=$details_tread_row['rate'];
	  $baseCurrency_id=$details_tread_row['baseCurrency'];
	  $baseAmount=$details_tread_row['baseAmount'];
	  $totalBaseAmount+=$baseAmount;
	  //endfinding details tread
	  
	  $base_currencyName_sql="select name from currencies where id='$baseCurrency_id'";
      $base_currencyName_query= $db ->query($base_currencyName_sql);
	  $base_currencyName_row=$base_currencyName_query->fetch_array();
	  $base_currencyName=$base_currencyName_row['name'];
	  
	 
	  
	 echo "<td> ".$currencyName_kharid."</td>";
	 echo "<td> ".$amount."</td>";
	 echo "<td> ".number_format($rate,2)."</td>";
	 echo "<td> ".$base_currencyName."</td>";
	 echo "<td> ".number_format($baseAmount,2)."</td>";
	  echo "</tr>";
	     
	    
	  }
      echo "</tr>
	            <td style='background: red'>مچموع خریداریها</td>
	            <td colspan='2'>$allAmount</td>
				<td style='background: green'>مجموع پول فروخته شده</td>
	            <td>$totalBaseAmount</td>
				
		   </tr>";
 
		  ?>
												
		</table>	
        <div id="title"> 
	<table border="0" style="width: 100%;text-align: center">
	<tr>
	   <td>فروشات</td>
	 </tr>
	  <tr>
	   <td><?php echo $currencyName_kharid ?>به <?php echo $base_currencyName;?></td>
	 </tr>
	 <tr>
	    <td><?php echo $date; ?></td>
	 </tr>
	 </table>
	
	</div>
	<table border="1" >
	<tr>
	    <td>فروشات</td>
	    <td>مقدار</td>
	    <td>قیمت</td>
	    <td>پول خریداری شده</td>
	    <td>مقدار</td>
	 </tr>
 
<?php


     // $date=$_POST['date'];
	
	   
      $tread_forosh_sql="select *  from journal where date='$date' and currency='$counterCurrency' and dr_cr=2";
      $tread_forosh_query= $db ->query($tread_forosh_sql);
	  while($tread_forosh_row=$tread_forosh_query->fetch_array()){
	   
	  $currency=$tread_forosh_row['currency'];
	  $amount_2=$tread_forosh_row['amount'];
	  $totalAmount_2+=$amount_2;
	  $reference_id=$tread_forosh_row['reference_id'];
	  //currencyName
	  $currencyName_kharid_sql="select * from currencies where id='$currency' ";
	  $currencyName_kharid_query= $db ->query($currencyName_kharid_sql);
	  $currencyName_kharid_row=$currencyName_kharid_query ->fetch_array();
	  $currencyName_kharid=$currencyName_kharid_row['name'];
	  //end currencyName
	  //finding details tread
	  $details_tread_sql="select * from transaction where id='$reference_id' and treatment=2 and baseCurrency='$baseCurrency_id'";
	  $details_tread_query = $db ->query($details_tread_sql);
	  $details_tread_row=$details_tread_query ->fetch_array();
	  
	  $rate=$details_tread_row['rate'];
	  $baseCurrency_id=$details_tread_row['baseCurrency'];
	  $baseAmount_2=$details_tread_row['baseAmount'];
	  $total_baseAmount_2+=$baseAmount_2;
	  //endfinding details tread
	  
	  $base_currencyName_sql="select name from currencies where id='$baseCurrency_id'";
      $base_currencyName_query= $db ->query($base_currencyName_sql);
	  $base_currencyName_row=$base_currencyName_query->fetch_array();
	  $base_currencyName=$base_currencyName_row['name'];
	  
	 
	  
	 echo "<td> ".$currencyName_kharid."</td>";
	 echo "<td> ".$amount_2."</td>";
	 echo "<td> ".number_format($rate,2)."</td>";
	 echo "<td> ".$base_currencyName."</td>";
	 echo "<td> ".number_format($baseAmount_2,2)."</td>";
	  echo "</tr>";
	     
	    
	  }
     echo "</tr>
	            <td style='background: red'>مجموع فروشات دالر</td>
	            <td colspan='2'>$totalAmount_2</td>
				<td style='background: green'>مجموع پول خریداری شده</td>
	            <td>$total_baseAmount_2</td>
				
		   </tr>";  
 
		  ?>
												
		</table>									
	</div>											 
</body>
</html>												 
												 
												  
											 