<?php 
session_start();
include('db.php'); 
 $date=$_GET['date'];
  

?>
<title> جزئیات سرمایه نقدی</title>
<link href="css/fontstyle.css" rel="stylesheet" type="text/css"/>	
<div align="center"  ><h1 class='fotntitle'><?php include("info/companyName.php"); ?></h1></div>
<div>
   <fieldset  dir='rtl' style="border: 1px solid #000;font-size: 16px;font-weight: bold">
  <legend class='fotntitle'>  جزئیات حساب های قابل دریافت</legend>
 

  <blockquote class='fotntitle' dir='rtl' style="border: 1px solid #000;width: 700px;font-size: 14px;font-weight: bold">
			 
			 <ul>
			 <table border="0" width="600px" style="line-height: 40px">
				  <tr>
					<td  colspan='3' style='font-size: 25px;font-weight: bold'>حسابهای قابل دریافت به تاریخ <?php echo $date?></td>
				</tr>  
				<tr>
					 <td> نوعیت پــول</td>
					 <td>مقدار به پول</td>
					<!-- <td>قیمت</td>-->
					 <td>مقدار به دالر</td>
				</tr> 

			<?php
			 
			
			//=========================================================================
			
	//=========================================================================
			$findCurrency_Sql="select distinct currencyId from customerbalance";
			$findCurrency_Query= $db ->query($findCurrency_Sql);
			while($findCurrency_Row=$findCurrency_Query ->fetch_array()){

			$findCurrency=$findCurrency_Row['currencyId'];
			
 //============================نام پول=====================================
        $currencyName_sql="select name from currencies where  id='$findCurrency'  ";
		$currencyName_query= $db ->query($currencyName_sql);
		$currencyName_Row=$currencyName_query ->fetch_array();
		$currencyName=$currencyName_Row['name'];

 //============================نام پول=====================================
			
//==========================finding Currency Rate=========================
	    $checkRate_sql="select * from crrencyrate where  counterCurrency='$findCurrency' and baseCurrency='2' order by id desc limit 1";
		$checkRate_query= $db ->query($checkRate_sql);
		$checkRate_Row=$checkRate_query ->fetch_array();
		$checkRate=$checkRate_Row['rate'];

		$unit=$checkRate_Row['unit'];

		$first=$checkRate;
		$second=$unit;
		 if(isset($checkRate)){
		  //$rate= $first/$second;
		  $rate= $checkRate/$unit;
		   
		 }
		//===============================================================================
		if(!isset($checkRate)){
		 
		$checkRate1_sql="select * from crrencyrate where  counterCurrency='2' and baseCurrency='$findCurrency' order by id desc limit 1";
		$checkRate1_query= $db ->query($checkRate1_sql);
		$checkRate1_Row=$checkRate1_query ->fetch_array();
		$unit1=$checkRate1_Row['unit'];
		$checkRate1=$checkRate1_Row['rate'];
		 
		 $first=$unit1;
		 
		 $second=$checkRate1;
        $rate= $first/$second;
		 
		 
		}
	 // echo "<br>===================================</br>";
	  //==========================finding Currency Rate=========================
	  //==========================Account Reciveable=========================
    
			
      $sumMoneyrecivable_sql="select sum(amount) from customerbalance where amount<0 and currencyId='$findCurrency'";	
	 
	  $sumMoneyrecivable_Query= $db ->query($sumMoneyrecivable_sql);
	  $sumMoneyrecivable_Row=$sumMoneyrecivable_Query ->fetch_array();
	  $sumMoneyrecivable=$sumMoneyrecivable_Row['sum(amount)'];
	  
	  $sumDollarRecivable=$sumMoneyrecivable*$rate;
	
	  $allDollarRecivable+=$sumDollarRecivable;
	  
	  echo "<tr>";
				echo "<td>$currencyName</td>";
				echo "<td>".number_format(-$sumMoneyrecivable,2)."</td>";
				//echo "<td>$rate</td>";
				echo "<td>".number_format(-$sumDollarRecivable,2)."</td>";
			echo "</tr>";
	  
	  
	  
	  
						  
}
              echo "<tr><td colspan='3' ><hr style='border-color: #000'></hr></td></tr>";
			  echo "<tr><td colspan='3' style='text-align: center;color: green'> ";
              
			 echo number_format(-$allDollarRecivable,2);		    
			
			 echo " </td></tr></table>";
			
			?>
	
		
			 
			  
			 
			 
			 
							
			 
			</table>
			</ul>
			
		</blockquote>
		 
	  
	</table>
	</fieldset>
	</div>