<?php
require_once('messages.php') ; 
 If($_POST['submit']){
 include("db.php");
 $money=$_POST['money'];
 
 $amountP=$_POST['amount'];
 $amount = preg_replace('/[^\d\.-]/', '', $amountP);
  
 $dollarRateP=$_POST['dollarRate'];
 $dollarRate = preg_replace('/[^\d\.-]/', '', $dollarRateP);
 
//=================================================== 
     $rateRow=$_POST['dollarRatePrice'];
	 $dollarRatePriceRowmix=split("/",$rateRow);
	 $first_dollarRatePrice =$dollarRatePriceRowmix[0];
	 $second_dollarRatePrice=$dollarRatePriceRowmix[1]; 
	 
	 
 //=====================================================
 
 if(strlen(trim($money))==0 or strlen(trim($amount))==0 ){
		 echo' <script>
					 sweetAlert("اخطار...", " لطفا قیمت پول مورد نظر را نسبت به دالر مشخص کنید!", "error");

				  </script>'; 
				  echo '<meta http-equiv="refresh" content ="1.70; url=assetRegistration.php" >';	
				  echo"<span style='color: red;font-size: 20px;text-align: center'>لطفا قیمت پول مورد نظر را به دالر مشخص کنید</span>";
				 die;
		}
$rate=$first_dollarRatePrice/$second_dollarRatePrice;
  //=================================checking =================================
   $checking_Sql="select * from assets  where currencies_id='$money'";
   $checking_Query= $db ->query($checking_Sql);
   $checkingRow=$checking_Query ->fetch_array();
   $checking=$checkingRow['currencies_id'];
   if(isset($checking)){
      echo' <script>
					 sweetAlert("اخطار...", " نقدنگی که شما می خواهید وارد کنید قبلا ثبت شده است !", "error");
			</script>';
      echo '<meta http-equiv="refresh" content ="1.70; url=assetRegistration.php" >';			
   die;
   }
  
  //=================================checking ================================= 	
 //=============================Insert into Opening Balance
    $insertOpenin_Sql="insert into assetopeningbalance (currencyUnit,amount,rate,totalDollar) values ('$money','$amount','$rate','$dollarRate')";
    $insertOpenin_Query=$db ->query($insertOpenin_Sql);
 
 //=============================Insert into Opening Balance
 
 if($insertOpenin_Query){
 $insertSql="insert into assets (amount,currencies_id,dollarAmount) values('$amount','$money','$dollarRate')";
 $insertQuery= $db ->query($insertSql);
 if($insertQuery){
 
 
  header("location:assetRegistration.php");
 
 
 }
 }
 
 
 }


?>