<?php
     include('db.php');
    include('messages.php');
    $mode=$_POST['mode'];
    $target=$_POST['target'];
	
	$day = $_POST['day'];
	$month = $_POST['month'];
	$year = $_POST['year'];
    echo $date=$year."-".$month."-".$day;
	
	
    echo "<br>";
	
    $desc=$_POST['desc']; 
    $counterCurrency=$_POST['counterCurrency'];// ارزی که باید اضافه شود
 
    $counterAmountP=$_POST['counterAmount'];    //مقدار ارزی که باید اضافه شود(مقدار)
    echo $counterAmount = preg_replace('/[^\d\.-]/', '', $counterAmountP);
     
 
    $bacecurrency=$_POST['basecurrency'];     //ارزی که باید کم شود
 
    $baceAmountP=$_POST['baceAmount'];   //مقداری که باید کم شود(ارزش به پول فروخته شده)
    $baceAmount = preg_replace('/[^\d\.-]/', '', $baceAmountP);
  
    $balance_p=$_POST['balance'];
    $balance = preg_replace('/[^\d\.-]/', '', $balance_p); //موجودی اولیه 
	 
	 
 //===================================================================================
         $rate_currencyPriec=$_POST['rate_currencyPriec'];   //قیمت به پـــول پـــایه 
		 $rateRow=$_POST['currencyPriec'];        //به پـــول پــایـــه
          
		  if($mode=='mode_1'){
			   $basePriceRowmix=explode("/",$rateRow);
			   $first_baseRatePrice =$basePriceRowmix[0];
		       $second_baseRatePrice=$basePriceRowmix[1]; 
			   
			   $rate=($rate_currencyPriec/$second_baseRatePrice);
		  }
		  if($mode=='mode_2'){
			  
			  $basePriceRowmix=explode("/",$rateRow);
			  $first_baseRatePrice =$basePriceRowmix[0];
			  echo "<br>";
		      $second_baseRatePrice=$basePriceRowmix[1]; 
			  
			 //echo  $rate=($first_baseRatePrice/$rate_currencyPriec);
			  $rate=($rate_currencyPriec/$first_baseRatePrice);
			   //$rate_gross=number_format($counterAmount/$baceAmount,4);
			   //$rate = preg_replace('/[^\d\.-]/', '', $rate_gross);  
		  }
		 
		   
		 
		  
		   
 //==================================================================================
 //===================================================================================
         $dollarrateRow=$_POST['dollarRatePrice'];        //بــه دلار
         $dollarRatePriceRowmix=explode("/",$dollarrateRow);
		 $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;
			  
		   } */
		   
		  // $dollar_rate=($first_dollarRatePrice/$second_dollarRatePrice);
	 
		   
 //==================================================================================
 
 
 
 
    $totalDollarP=$_POST['totalDollar'];
     $totalDollar = preg_replace('/[^\d\.-]/', '', $totalDollarP);
  
 
 if(strlen(trim($counterCurrency))==0 or strlen(trim($bacecurrency))==0 or strlen(trim($baceAmount))==0){
		echo' <script>
					 sweetAlert("اخطا ر... ", "لطفا جاهای خالی را پرکنید !", "error");
				  </script>';

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


}
   
 $insertSql="INSERT INTO `transaction` (`date` ,`counterCurrency`,`counterAmount`,`rate`,`baseCurrency`,`baseAmount`,`dollarRate`,`dollarTotal`,`treatment`,`desc`) 
 VALUES ('$date','$counterCurrency','$counterAmount','$rate','$bacecurrency','$baceAmount','$dollar_rate','$totalDollar','1','$desc')";
 $insertQuery = $db ->query($insertSql);  

 
 
    //بــــــــــــــــــــــــــــــــــــه جورنال ثبتــــــــــــــــــــــــ کـــــــــــــــــــــــــــــــن
	 //آخرین رو را بگیــــــــــــــــــــر
		  $selectLast_sql = "select id from transaction order by id desc limit 1";
		  $selectLast_sqlQuery = $db->query($selectLast_sql);
		  $selectLastRow = $selectLast_sqlQuery->fetch_array();
		  $lastID = $selectLastRow['id'];
		  //journal out
		  $date=$date;
		  $namePart="خرید";
		  $reference="خرید";
		  $lastID=$lastID;
		  $currency=$bacecurrency;
		  $threadment="خرید";
		  $amount=$baceAmount;
		  $dr_cr=2;
		  include("entery_journal_majul.php");
		  //End of Journal OUT
		  //journal IN
		  $date=$date;
		  $namePart="خرید";
		  $reference="خرید";
		  $lastID=$lastID;
		  $currency=$counterCurrency;
		  $threadment="خرید";
		  $amount=$counterAmount;
		  $dr_cr=1;
		  include("entery_journal_majul.php");
		  //End of Journal IN
		  
		 
     
    //==============ارزی که باید از موجودی کم شود(Basecurrency)=========================
   if($insertQuery){
    
    
  
	//============update counterbank
	  
	 
	    
		  
		   echo' <script>
					 
					
				   swal("ذخیره موفق!", "اطلاعات وارده با موفقیت ثبت سیستم شد", "success")
				</script>';
	   echo '<meta http-equiv="refresh" content ="1; url='.$target.'" >';
	 
	 
	 } //End of update counterbank
	
	 

  
 
?>