<?php
include("db.php");
include("pdate.php");
  $pdate1=$date;
    
     $findCurrency_Sql="select distinct currencyId from customerbalance";
     $findCurrency_Query= $db ->query($findCurrency_Sql);
	 $ii=-1;
	 while($findCurrency_Row=$findCurrency_Query ->fetch_array()){
 
	   $ii++;
      $findCurrency=$findCurrency_Row['currencyId'];
	  
	  //echo "Currency Come from other page:";
	    
		     /* $currency_and_rate_row=$_POST['currency_and_rate'][$ii];
		     $explode=explode("/",$currency_and_rate_row);
			 $currency_and_rate_row */
		
		
		 
	     $currencyId=$_POST['currencyId'][$ii];
	     $ratep=$_POST['rate'][$ii];
         $rateTake = preg_replace('/[^\d\.-]/', '', $ratep);
		 
		  
	  //===============================================================================
	  //انتخاب تمام پولی هایی که از دالر کوچکتر می باشند 
	    $checkRate_sql="select * from crrencyrate where  counterCurrency='2' and baseCurrency='$currencyId' order by date desc limit 1";
		$checkRate_query= $db ->query($checkRate_sql);
		$checkRate_Row=$checkRate_query ->fetch_array();
	     
		$unit=$checkRate_Row['unit'];
		$baseCurrency=$checkRate_Row['baseCurrency'];  
		 
             $courencyName_sql="select id,name from  currencies where id='$currencyId'";
			 $courencyName_Query =$db ->query($courencyName_sql);
			 $courencyName_Row=$courencyName_Query ->fetch_array();
			 $courencyId=$courencyName_Row['id'];
			 $courencyName=$courencyName_Row['name'];
			 if($courencyName!='یورو'){
				  $courencyName;
				  
				//findIng Opening Balance  
				  $sumCustomerOP_sql="select sum(amount) from customeropeningbalance where  currencyId='$currencyId' and amount>0 and remove='0'";	
				  $sumCustomerOP_Query= $db ->query($sumCustomerOP_sql);
				  $sumCustomerOP_Row=$sumCustomerOP_Query ->fetch_array();
				   
				  
				  $sumCustomerOP=$sumCustomerOP_Row['sum(amount)'];
				  
				  $sumCustomerOP_bordegi_sql="select sum(amount) from customeropeningbalance where  currencyId='$currencyId' and amount<0 and remove='0'";	 
				  $sumCustomerOP_bordegi_Query= $db ->query($sumCustomerOP_bordegi_sql);
				  $sumCustomerOP_bordegi_Row=$sumCustomerOP_bordegi_Query ->fetch_array();
				   
				  $sumCustomerOP_bordegi=$sumCustomerOP_bordegi_Row['sum(amount)'];
				  $sumCustomerOP_bordegi=-$sumCustomerOP_bordegi; 

				  
				  
				 
				  //رسیدگی مشتری
				  $sumCustomerRE_sql="select sum(amount) from customerpayment where  transactCurrency='$currencyId' and transacttype='1' and date<='$pdate1' and remove='0'";	
				  $sumCustomerRE_Query= $db ->query($sumCustomerRE_sql);
				  $sumCustomerRE_Row=$sumCustomerRE_Query ->fetch_array();
				  
				  $sumCustomerRE=$sumCustomerRE_Row['sum(amount)'];
				  
				  
				  //بردگی مشتری
				  $sumCustomerPY_sql="select sum(amount) from customerpayment where  transactCurrency='$currencyId' and transacttype='2' and date<='$pdate1' and remove='0'";	
				  $sumCustomerPY_Query= $db ->query($sumCustomerPY_sql);
				  $sumCustomerPY_Row=$sumCustomerPY_Query ->fetch_array();
				  $findCurrency;
				  $sumCustomerPY=$sumCustomerPY_Row['sum(amount)'];
				  
				    
				  $sum_total_residegi=($sumCustomerRE+$sumCustomerOP)-($sumCustomerPY+$sumCustomerOP_bordegi);
	 
				 
				       $total=$sum_total_residegi/$rateTake;
				       $total_s+=$total;
				  
			 } 
	      
	 //===================================================================
	  //===============================================================================
	  //انتخاب تمام پولهایی که از دالر بزرگتر می باشند 
	    $checkRate_B_sql="select * from crrencyrate where  counterCurrency='$currencyId' and baseCurrency='2' order by date desc limit 1";
		$checkRate_B_query= $db ->query($checkRate_B_sql);
		$checkRate_B_Row=$checkRate_B_query ->fetch_array();
	     
		$unit=$checkRate_B_Row['unit'];
		$baseCurrency=$checkRate_B_Row['counterCurrency']; 
		//تنها واحد پولی بزرگتر از دالر
          if($baseCurrency!=2){
		  
		  
		     $courencyName_sql="select id,name from  currencies where id='$currencyId'";
			 $courencyName_Query =$db ->query($courencyName_sql);
			 $courencyName_Row=$courencyName_Query ->fetch_array();
			 $courencyId=$courencyName_Row['id'];
			 $courencyName=$courencyName_Row['name'];
			

			 if($courencyName=='یورو'){
				  $courencyName;
				    
				 //findIng Opening Balance  
				  $sumCustomerOP_sql="select sum(amount) from customeropeningbalance where  currencyId='$currencyId' and amount>0 and remove='0'";	
				  $sumCustomerOP_Query= $db ->query($sumCustomerOP_sql);
				  $sumCustomerOP_Row=$sumCustomerOP_Query ->fetch_array();
				 
				  $sumCustomerOP=$sumCustomerOP_Row['sum(amount)'];
				  
				  $sumCustomerOP_bordegi_sql="select sum(amount) from customeropeningbalance where  currencyId='$currencyId' and amount<0 and remove='0'";	 
				  $sumCustomerOP_bordegi_Query= $db ->query($sumCustomerOP_bordegi_sql);
				  $sumCustomerOP_bordegi_Row=$sumCustomerOP_bordegi_Query ->fetch_array();
				  
				  $sumCustomerOP_bordegi=$sumCustomerOP_bordegi_Row['sum(amount)'];
				  $sumCustomerOP_bordegi=-$sumCustomerOP_bordegi; 

				  
				  
				  //رسیدگی مشتری
				  $sumCustomerRE_sql="select sum(amount) from customerpayment where  transactCurrency='$currencyId' and transacttype='1' and date<='$pdate1' and remove='0'";	
				  $sumCustomerRE_Query= $db ->query($sumCustomerRE_sql);
				  $sumCustomerRE_Row=$sumCustomerRE_Query ->fetch_array();
				  
				  $sumCustomerRE=$sumCustomerRE_Row['sum(amount)'];
				  
				   
				  //بردگی مشتری
				  $sumCustomerPY_sql="select sum(amount) from customerpayment where  transactCurrency='$currencyId' and transacttype='2' and date<='$pdate1' and remove='0'";	
				  $sumCustomerPY_Query= $db ->query($sumCustomerPY_sql);
				  $sumCustomerPY_Row=$sumCustomerPY_Query ->fetch_array();
				  $findCurrency;
				  $sumCustomerPY=$sumCustomerPY_Row['sum(amount)'];
				  
				   
				    $sum_total_residegi=($sumCustomerRE+$sumCustomerOP)-($sumCustomerPY+$sumCustomerOP_bordegi);
	              
				  
				      $total_b=$sum_total_residegi*$rateTake;
                      $total_b=$total_b;
				  
				  
				 
			 } 
             
			   
			   
			    
			}		  
		 
	  
	 
     }
	  
	 //echo $sumOf_all=$total_b+$total; 
	  $sumOf_all=$total_s+$total_b; 
     
?>