<?php
 session_start();  
 include("db.php");
  include('messages.php');
  $date=$_GET['date'];
   
  $_SESSION['date']=$date;
  $counterCurrency=$_GET['counterCurrency'];
  $_SESSION['counterCurrency']=$counterCurrency;
   
  $baseCurrency=$_GET['baseCurrency'];
  $_SESSION['baseCurrency']=$baseCurrency;
  
  if(strlen(trim($counterCurrency))==0 or strlen(trim($baseCurrency))==0 or strlen(trim($date))==0){
		echo' <script>
					sweetAlert("اخطا ر... ", "شما باید ارزپایه و ارز مورد معاوضه  را انتخاب کنید!", "error");
				  </script>';

         echo '<meta http-equiv="refresh" content ="1; url=dillyProfi.php" >';
		die;


		}
  //====================Finding counter Currency Name
  $currencyNameSql="select name from currencies where id='$counterCurrency'";
  $currencyNameQuery= $db ->query($currencyNameSql);
  $currencyNameRow=$currencyNameQuery ->fetch_array();
  $currencyName=$currencyNameRow['name'];
  
  //====================End of Finding Currency Name
  //====================Finding Base Currency Name
  $basecurrencyNameSql="select name from currencies where id='$baseCurrency'";
  $basecurrencyNameQuery= $db ->query($basecurrencyNameSql);
  $basecurrencyNameRow=$basecurrencyNameQuery ->fetch_array();
  $bacecurrencyName=$basecurrencyNameRow['name'];
  
   //====================End of Finding base Currency Name
   
  //==========================================================================
      $retriveBuySql="select sum(counterAmount),sum(baseAmount)  from transaction where date='$date' and counterCurrency='$counterCurrency' and baseCurrency='$baseCurrency' and treatment='1'";
      $retriveBuyQuery= $db ->query($retriveBuySql);
	  $retriveBuyRow=$retriveBuyQuery ->fetch_array();
	   //echo "Amount Bought Currency:";
	  $totalCounterAmount=$retriveBuyRow['sum(counterAmount)'];
	  
	    $currencyName;
	   //echo "<br>Amount Base Currency:";
	   $totalBaseAmount=$retriveBuyRow['sum(baseAmount)'];
	   $bacecurrencyName;
	   
      //echo "<br>================================================<br>"; 
	  
	  /* if($totalCounterAmount==""){
	  
	     header("Location: error.php");
	    
	  } */
//==========================================================================
     
      $retriveSellSql="SELECT SUM(counterAmount) , SUM(baseAmount) FROM transaction WHERE treatment ='2' AND DATE =  '$date' AND counterCurrency =  '$counterCurrency' AND baseCurrency =  '$baseCurrency'";
      $retriveSellQuery= $db ->query($retriveSellSql);
	  $retriveSellRow=$retriveSellQuery ->fetch_array();
	       //echo "Sale Currency:"; 
	  $totalCounterAmountsell=$retriveSellRow['SUM(counterAmount)'];
	   
	  $currencyName;
	  //echo "<br>amount of Corresponded:"; 
	  $totalBaseAmountsell=$retriveSellRow['SUM(baseAmount)'];
	  $bacecurrencyName;
	 // echo "<br>";
	  
	  //**************************************************************
	 $value=$totalCounterAmountsell;
	 if($totalBaseAmountsell=="" and $totalBaseAmount=="" ){
	     header("Location: error.php");
	 
	 }
	 $resutl = 0;
	 $set = 0;
	        $dailySellSql="select id,counterAmount,rate,baseAmount from transaction where  treatment !='2'  and counterCurrency='$counterCurrency' and baseCurrency='$baseCurrency' and date='$date'";
	        $dailySellQuery= $db ->query($dailySellSql);
			
			while($dailySellRow=$dailySellQuery ->fetch_array()){
			     $counterAmount=$dailySellRow['counterAmount'];
			     $buyId=$dailySellRow['id'];
			     $rate_kharid=$dailySellRow['rate'];
			      
			  // echo '<br>';
			   
			  
			    if($set == 0){
				 
			    $resutl += $value - $counterAmount;
				$insertSql="insert into dailly (amount,price,buyId,rate_kharid) values ($resutl,$counterAmount,$buyId,$rate_kharid)";
			    $insertQuery = $db ->query($insertSql);
				$set++;
				
			    }else{
				echo "<br>";
			    echo $resutl1 =  ($resutl - $counterAmount);
				echo "<br>"; 
				echo $resutl2 =  ($resutl - $counterAmount);
				echo "<br>";
				$resutl = 0;
				$resutl += $resutl2;
				$set++;
				$insertSql="insert into dailly (amount,price,buyId,rate_kharid) values ($resutl1,$counterAmount,$buyId,$rate_kharid)";
			    $insertQuery = $db ->query($insertSql);
				}
			    
			 
			   
			   if($insertQuery){
			    
			      // header("location: profitdillyResult.php?counterCurrency=$counterCurrency&baseCurrency=$baseCurrency & date=$date");
			   }
			   } 
	   
		 

?>