<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<?php
ob_start();
ini_set('session.bug_compat_warn', 0);
ini_set('session.bug_compat_42', 1);

 include("db.php");
 include("../pdate.php");
     $get_id=$_GET['editID'];
  
     $table=$_GET['table'];
 
    
    
     $customerRetriveSql="select * from $table  where id='$get_id'";
	 $customerRetriveQuery=$db ->query($customerRetriveSql);
	 $customerRetriveRow=$customerRetriveQuery ->fetch_array();
	 $id_retrive=$customerRetriveRow['id'];
	 $counterCurrency_retrive=$customerRetriveRow['currencyUnit'];
	 $amount_retrive=$customerRetriveRow['amount'];
	 $rate_retrive=$customerRetriveRow['rate'];
	 $totalDollar_retrive=$customerRetriveRow['totalDollar'];
	  
	  
	 
	   
?>  
<html>
<head>
	<title>ویـرایـش دارییـهای نقـــدی</title>
	 
	<link rel="stylesheet" type="text/css" href="../css/style.css">
   <script src="dist/sweetalert-dev.js"></script>
  <link rel="stylesheet" href="dist/sweetalert.css"> 
  
<!---==========================================================================-->
 
 <script type="text/javascript" src="../js/decimal.js"></script>
 <script type="text/javascript" src="../js/NumberFormat.js"></script>
<!---==========================================================================-->
<link href="css/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../js/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="../js/jquery.autocomplete.pack.js"></script>
<script type="text/javascript" src="../js/script.js"></script>
<!---==========================================================================-->
<!--========================================================================-->
  <script>
	     function get_amount(){
	    $('#rate').hide();
		$.post('../retiverateRate.php', {
		   rateOfDollar:form.money.value  
		    
		   },
			function(output){
				$('#rate').html(output).fadeIn(10);
		})
	
	}
	</script>
	<script>
	 
		  function findingcalculation(){
	     insertval=setInterval("zarb()",1);
	    }
		  function zarb(){
	      amountp=document.getElementById('amount').value; // مقدار ارز مورد معاوضه
		  var decimalCharacter = ".",
          regex = new RegExp("[\\d-" + decimalCharacter + "]+", "g"),
          num_caldar=amountp;
          var amount=+num_caldar.match(regex).join("");
		 
		 // نرخ قدیم
		  dollarmixData=document.getElementById('dollarmixData').value; 
		  dollartakeData=dollarmixData.split('/');
		  dollarmainrate=dollartakeData[0];
		  dollarunit=dollartakeData[1];
		   var dollarexactresult=amount/dollarunit;
		   
		   totalDollar=(dollarmainrate*1)*(dollarexactresult*1);
           findTotalDollar=totalDollar.toFixed(2);   
		   document.getElementById('dollarRate').value=findTotalDollar;
		  /* var dollarexactresult=amount/dollarunit;
		  mainratep  =document.getElementById('mainrate').value;
	      var decimalCharacter = ".",
          regex = new RegExp("[\\d-" + decimalCharacter + "]+", "g"),
          num_caldar=mainratep;
          var mainrate=+num_caldar.match(regex).join("");  
		
		 total=(mainrate*1)*(amount*1); */
		  
		// document.getElementById('dollarRate').value=FormatNumberBy3(total.toFixed(2));
		  
		
		}   
	</script>
   
</head>
<body onload="get_amount();findingcalculation()">

	<div id="wrap">
		 
		<div id="contentt">
			 
			<div id="menu">
				 
			</div>
		    <div id="matlab" >
			<div id="title"><p>ویـرایـش دارییـهای نقـــدی</p> </div>
			<div id="picture">
					<table border="0" width="400px">
						<tr>
							<td style="width:180px;"><img src="../img/opening_asset.png" width="250px" height="90px">  </td>
							<td > </td>
						</tr>
					</table>
			   </div>
		    	<form method="post" action="" name="form">
               <table border="0"   id="rgistera_cart" width="800px" class="font">
		    		<tr>
						<td style="width:190px">&nbsp;</td>
		    			<th style="width:150px">واحد پـــولی</th>
						<td style="width:250px" >
						<select name="money" id="money" style="width:100%" onchange="get_amount()" onclick="get_amount()" >
							<?php
						 
							$sql="select * from currencies";
							$query=$db ->query($sql);
							while($row=$query ->fetch_array()){
							$id=$row['id'];
							$name=$row['name'];
							//echo "<option value='$id'>$name</option>";

                            if($counterCurrency_retrive == $row['id']){
								echo "<option value=$row[id] selected>$row[name] </option>";
								}else{
								    echo "<option value=$row[id]>$row[name]</option>";
								}


							
							}
							echo "</select>";
														
							?>
						 
						 </td>
						 
		    		</tr>
					<tr>
						<td style="width:190px">&nbsp;</td>
		    			<th style="width:150px">مقدار</th>
						<td style="width:250px" >
							<input type="text" name="amount" id="amount" autocomplete="off" onkeyup="addSeparator(this)"  value="<?php echo number_format($amount_retrive,2)?>" class="filed" onChange="findingcalculation()" onClick="findingcalculation()" />
														 
						</td>
						 
		    		</tr> 
					<!--<tr>
						<td style="width:190px">&nbsp;</td>
		    			<th style="width:150px">نرخ</th>
						<td style="width:250px" >-->
							<div id='rate'><input type="hidden" id="determainRate" name="rate" class='filed' ></div>
														 
						<!--</td>
						 
		    		</tr> 
					 
					
					<!--<tr>
						<td style="width:190px">&nbsp;</td>
		    			<th style="width:150px">مقدار به دلار</th>
						<td style="width:250px" >-->
						<input type="hidden" name="dollarRate" id="dollarRate" placeholder="" class="filed" />
														 
						<!--</td>
						 
		    		 
					</tr>-->
						 
					 
					<tr>
						<td style="width:190px">&nbsp;</td>
		    			<th style="width:150px"> </th>
						<td style="width:230px"> 
						    <input type="submit" id="save"   name="save"  value="ذخیره">
							<input type="submit" id="back"   name="back"  value="انصراف">
							
						</td>
						<td style="width:300px;"> </td> 
		    		</tr>
                    
		    	</table>
		    	 
				</form>
		    </div>


		</div>
		 
	</div>
</body>
</html>
<?php
if($_POST['back']){
  header("location:../assetRegistration.php");
}
if($_POST['save']){
   
    $counterCurrency_retrive; //نوعیت پول قدیم
	$amount_retrive;  //مقدار قدیم
	$rate_retrive;    // نرخ قدیم 
	$totalDollar_retrive; // مجموعه به دالر قدیم
	echo "<br>";
	
	$currencyId=$_POST['money'];
	$amountP=$_POST['amount'];
    $amount = preg_replace('/[^\d\.-]/', '', $amountP);
	//=====================================================
	 $rateRow=$_POST['dollarRatePrice'];
	 $dollarRatePriceRowmix=split("/",$rateRow);
	 $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;
		  
	   } */
	   $rate=($first_dollarRatePrice/$second_dollarRatePrice);
	
	//========================================================
	
	 
	
	$dollarTotalP=$_POST['dollarRate'];
	$dollarTotal = preg_replace('/[^\d\.-]/', '', $dollarTotalP);
	echo "<br>------------------------</br>";
	
//===============================================
    /*=====================بــــر گشــــت بــــــه بـــانـــک======================*/
	$back_To_Asset_Sql="select * from assets where currencies_id='$counterCurrency_retrive'";
	$back_To_Asset_Query=$db ->query($back_To_Asset_Sql); 
	$back_To_Asset_Row=$back_To_Asset_Query ->fetch_array(); 
	$back_Amount=$back_To_Asset_Row['amount'];
	$back_DollarAmount=$back_To_Asset_Row['dollarAmount'];
	
    $back_updateAmount=$back_Amount-$amount_retrive;
	$back_updateDollarAmount=$back_DollarAmount-$totalDollar_retrive;
	
    $back_update_Sql="update assets set amount='$back_updateAmount',dollarAmount='$back_updateDollarAmount' where currencies_id='$counterCurrency_retrive'";	
    $back_update_Query =$db ->query($back_update_Sql);   
   /*=====================بــــر گشــــت بــــــه بـــانـــک==========================*/
   //==================================================================================
   //==================================================================================
   //=========================تغییـــرات جدید روی بانـــک==============================
    $new_To_Asset_Sql="select * from assets where currencies_id='$currencyId'";
	$new_To_Asset_Query=$db ->query($new_To_Asset_Sql); 
	$new_To_Asset_Row=$new_To_Asset_Query ->fetch_array(); 
	$new_Amount=$new_To_Asset_Row['amount'];
	 
	 $new_DollarAmount=$new_To_Asset_Row['dollarAmount'];
	if(isset($new_Amount)){
		$new_updateAmount=$new_Amount+$amount;
		$new_updateDollarAmount=$new_DollarAmount+$dollarTotal;
		
		$back_update_Sql="update assets set amount='$new_updateAmount',dollarAmount='$new_updateDollarAmount' where currencies_id='$currencyId'";	
		$back_update_Query =$db ->query($back_update_Sql);   
   }
   
   //===اگر بانک موجود نبود
   if(!isset($new_Amount)){
     
	 $createNewAsset_Sql="insert into assets (amount,currencies_id,dollarAmount) 
	 values('$amount','$currencyId','$dollarTotal')";
     $createNewAsset_Query= $db ->query($createNewAsset_Sql);
   }
   
   //=========================تغییـــرات جدید روی بانـــک==============================
   $update_assetopeningbalance_Sql="update assetopeningbalance set currencyUnit='$currencyId',amount='$amount',rate='$rate',totalDollar='$dollarTotal' where currencyUnit='$currencyId'";
   $update_assetopeningbalance_Query= $db ->query($update_assetopeningbalance_Sql);
   
   
   header("location:../assetRegistration.php");		
    
}



?>