 <?php
  session_start();
 if($_POST['submit']){
  include("db.php");

   $date = $_POST['year']."-".$_POST['month']."-".$_POST['day'];  
   
   $counterCurrency=$_POST['counterCurrency'];  //ارزی که باید کم شود
    
   $counterAmount_p=$_POST['counterAmount']; //مقدار ارز معاوضه فروخته شده که باید کم شود 
   $counterAmount = preg_replace('/[^\d\.-]/', '', $counterAmount_p);
   
   $retrivebuyeramount_p=$_POST['retrivebuyeramount'];
   $retrivebuyeramount = preg_replace('/[^\d\.-]/', '', $retrivebuyeramount_p);
   
   $retrivebuyerafn_p=$_POST['retrivebuyerafn'];  //موجودی ارز مورد معاوضه 
   $retrivebuyerafn = preg_replace('/[^\d\.-]/', '', $retrivebuyerafn_p); 
    
   $basecurrency=$_POST['basecurrency']; //نوع ارز پایه 
            
   $baceAmount_p=$_POST['baceAmount']; //مقدار فروخته شده به ارزش پایه
   $baceAmount = preg_replace('/[^\d\.-]/', '', $baceAmount_p);
    
   $balance_p=$_POST['balance']; //موجودی ارز پایه
   $balance = preg_replace('/[^\d\.-]/', '', $balance_p);
   
   //==================================================================================
     $rateRow=$_POST['currencyPriec'];
     $RatePriceRowmix=split("/",$rateRow);
	 $first_RatePrice =$RatePriceRowmix[0];
	 $second_RatePrice=$RatePriceRowmix[1]; 
	 
	 /* if($first_RatePrice=='1'){
	          
             $unit_Rate   =$first_RatePrice; 	  
			 $RatePrice   = $second_RatePrice;		  
	   }else{
	         
	         $unit_Rate=$second_RatePrice;  
			 $RatePrice= $first_RatePrice;
		  
	   } */
	   
	   $rate=($first_RatePrice/$second_RatePrice);
 //=====================================================================================
     $dollarRatePriceRow=$_POST['dollarRatePrice']; //قیمت به دالر
	 $dollarRatePriceRowmix=split("/",$dollarRatePriceRow);
	 $first_dollarRatePrice =$dollarRatePriceRowmix[0];
	 $second_dollarRatePrice=$dollarRatePriceRowmix[1]; 
	 
	 /* if($first_dollarRatePrice=='1'){
	          
             $unit_dollarRate   =$first_dollarRatePrice; 	  
			 $dollarRatePrice = $second_dollarRatePrice;		  
	   }else{
	         
	         $unit_dollarRate=$second_dollarRatePrice;  
			 $dollarRatePrice= $first_dollarRatePrice;
		  
	   } */
	   
	   $dollarRate=($first_dollarRatePrice/$second_dollarRatePrice);
	   
 //=====================================================================================  
   
       $totalDollar_p=$_POST['totalDollar'];
       $totalDollar = preg_replace('/[^\d\.-]/', '', $totalDollar_p);//مجموع به دالر
  
       $rate=$_POST['currencyPriec']; 
       $desc=$_POST['desc'];   
       $calculateAsset=$balance+$baceAmount;
  //====================================================================================
						/*ارزی که باید از موجودی کم شود(Basecurrency)*/
   //====================================================================================	
   
    
	  $baseBankSql="select amount,dollarAmount from assets where currencies_id='$basecurrency'";
	  $baseBankQuery=$db ->query($baseBankSql);
	  $baseBankRow=$baseBankQuery ->fetch_array();
	  
	  $baseBank=$baseBankRow['amount']; 	
	  $basedollarAmountBank=$baseBankRow['dollarAmount'];
      
	  
	  if(isset($baseBank)){
	     $newBasecurrency=$baseBank-$baceAmount;
		 $newDollarBasecurrency=$basedollarAmountBank-$totalDollar;
	     
	     $updateBaseBankSql="update assets set amount='$newBasecurrency',dollarAmount='$newDollarBasecurrency' where currencies_id='$basecurrency' ";
	     $updateBaseQuery= $db ->query($updateBaseBankSql);
    }
	 
   //========================/*مقدار ارزی که باید اضافه شود*/===============
  
	$assetsell_sql="select currencies_id,amount,dollarAmount  from assets where currencies_id='$counterCurrency' ";
    $assetsellQuery= $db ->query($assetsell_sql);
    $assetsellRow=$assetsellQuery ->fetch_array();
	 
	$assetsellamount=$assetsellRow['amount'];
    $assetsellDollaramount=$assetsellRow['dollarAmount']; 
    
	
	if(isset($assetsellamount)){
	 
        $newcountercurrency=$assetsellamount+$counterAmount;
	    $newDollarcountercurrency=$assetsellDollaramount+$totalDollar;

        $updatecounterBankSql="update assets set amount='$newcountercurrency',dollarAmount='$newDollarcountercurrency' where currencies_id='$counterCurrency' ";
	    $updatecounterBankQuery= $db ->query($updatecounterBankSql);
    
	}
	 
	//====================================================================================
 						 /*مقدار ارزی که باید اضافه شود*/   
   //====================================================================================
      if(!isset($assetsellamount)){
	  echo "<br>";
	  	     $insertSql="insert into `assets`(amount,currencies_id,dollarAmount) values ('$counterAmount','$counterCurrency','$totalDollar')";
	         $insertQuery= $db ->query($insertSql);
	 
	 }
    echo "<br>";	
        $insertSql="INSERT INTO `banking` (`date` ,`counterCurrency`,`counterAmount`,`baseCurrency`,`rate`,`baseAmount`,`dollarRate`,`totalDollart`,`descr`) 
	    VALUES ('$date','$counterCurrency','$counterAmount','$basecurrency','$rate','$baceAmount','$dollarRate','$totalDollar','$desc')";
	    $insertQuery = $db ->query($insertSql);	
		
		header("location:banking.php");
	 
   
  }
 
 ?>