<?php
 if($_POST['submit']){
  include("db.php");
  include('messages.php');
  
  $date = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
  
  echo "<br>";
  $desc=$_POST['desc'];
  $incomeid=$_POST['incomeName'];
  
  $moneyId=$_POST['counterCurrency'];
  
  $assetCurrencyc=$_POST['assetCurrencyc'];
  
  $expensesAmountP=$_POST['expensesAmount'];
  $expensesAmount = preg_replace('/[^\d\.-]/', '', $expensesAmountP);

  //====================================================================
  $dollarRateRow=$_POST['dollarRatePrice'];
  $dollarRatePriceRowmix=split("/",$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;
		  
	   } */
  
    $dollarRate=($first_dollarRatePrice/$second_dollarRatePrice);
   
 //=============================================================================== 
  
  $result=$_POST['result'];
  if(strlen(trim($incomeid))==0 or strlen(trim($moneyId))==0 or strlen(trim($expensesAmount))==0){
		echo' <script>
					 sweetAlert("اخطا ر... ", "لطفا جاهای خالی را پرکنید !", "error");
				  </script>';

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


}
  $insertSql="insert into others (date,expensesId,currencyId,currencyAsset,amount,dollarRate,dollarTotal,typeOthers,description) 
                   values
			  ('$date','$incomeid','$moneyId','$assetCurrencyc','$expensesAmount','$dollarRate','$result',2,'$desc')";
   
   $insertQuery=$db ->query($insertSql);
   if($insertQuery){
   
          $assetBalanceSql="select amount,dollarAmount from assets where currencies_id='$moneyId' ";
          $assetBalanceQuery= $db ->query($assetBalanceSql);
		  $assetBalanceRow=$assetBalanceQuery ->fetch_array();
		  
		  $assetBalanceAmount=$assetBalanceRow['amount'];
		  $assetBalancedollarAmount=$assetBalanceRow['dollarAmount'];
   
      $newAssetAmount=$assetBalanceAmount + $expensesAmount;
      $newAssetDollarAmount=$assetBalancedollarAmount + $result;
      
	  $updateSql="update assets set amount='$newAssetAmount',dollarAmount='$newAssetDollarAmount' where currencies_id='$moneyId'";
      $updateQuery =$db ->query($updateSql);
	  
	    // insertion in journal ============================================================
	  // انزیرشن به جـــــــــــــــــــــورنال====================================================================
		  //نام مشتری رو بگیر
		  $selectCustomer = "select incomeName from incomename where id =$incomeid";
		  $selectCusetomerQeury = $db->query($selectCustomer);
		  $selectCustomerRow = $selectCusetomerQeury->fetch_array();
		  $customerName = $selectCustomerRow['incomeName'];
		  
		  //آخرین رو را بگیــــــــــــــــــــر
		  $selectLast_sql = "select id from others order by id desc limit 1";
		  $selectLast_sqlQuery = $db->query($selectLast_sql);
		  $selectLastRow = $selectLast_sqlQuery->fetch_array();
		  $lastID = $selectLastRow['id'];
		  //========
		  //بــــــــــــــــــــــــــــــــــــه جورنال ثبتــــــــــــــــــــــــ کـــــــــــــــــــــــــــــــن
		  $journal_sql = "insert into journal(date,name,reference,reference_id, currency,threadment,amount) values('$date','$customerName','عواید','$lastID','$moneyId','عواید','$expensesAmount')"; 
	      $journalInsert = $db->query($journal_sql);
		  

	  
	  //end insertion to journal =========================================================
	  
	  
	  echo' <script>
					 
					
				   swal("ذخیره موفق!", "اطلاعات وارده با موفقیت ثبت سیستم شد", "success")
				</script>';
	   echo '<meta http-equiv="refresh" content ="1; url=defineIncome.php" >';
	  //header("location:defineIncome.php");
   
   }

}


?>