<html>
<head>
    <script src="dist/sweetalert-dev.js"></script>
    <link rel="stylesheet" href="dist/sweetalert.css">
    <link rel="stylesheet" href="css/screen.css" type="text/css"   />
</head>
<?php
session_start();
 
 ob_start();
 ini_set('session.bug_compat_warn', 0);
 ini_set('session.bug_compat_42', 1);
 //$customar_id=$_SESSION['customar_code'];
if($_POST['submit']){
 include("db.php");
 
  $day = $_POST['day'];
  $month = $_POST['month'];
  $year = $_POST['year']; 
   
  $date=$year."-".$month."-".$day;
   
  $transact_from_raw= $_POST['customerName'];
  $transact_from_explode=explode("-",$transact_from_raw);
  $transact_from=$transact_from_explode[0];
   
  $transact_to  = $_POST['s_customerName'];
  $transact_to_explode=explode("-",$transact_to);
  $transact_to=$transact_to_explode[0];
  
  $counterCurrency=$_POST['counterCurrency'];
   
  $currencyAmountP=$_POST['currencyAmount'];
  $currencyAmount = preg_replace('/[^\d\.-]/', '', $currencyAmountP);
  $desc=$_POST['desc'];
 echo "<br>";
  
   
 //echo  "<br>============BANK ASSET===============================</br>";
 if(strlen(trim($transact_from))==0 or strlen(trim($transact_to))==0 or strlen(trim($counterCurrency))==0){
		echo' <script>
					 sweetAlert("اخطا ر... ", "لطفا جاهای خالی را پرکنید !", "error");
				  </script>';

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


}
 //===============================check bank====================================
    $asset_Sql="select * from `assets`  where  currencies_id='$counterCurrency'";
	$asset_Query=$db ->query($asset_Sql);
	$asset_Row=$asset_Query ->fetch_array();
    $asset_Amount=$asset_Row['amount'];
	$asset_Dollar_Amount=$asset_Row['dollarAmount'];
  
	if(!isset($asset_Amount)){
	    echo' <script>
					sweetAlert("هشــدار...", "لطفا ابتدا پول را در قسمت دارییها ثبت کنید!", "error");
			   </script>'; 
		echo '<meta http-equiv="refresh" content ="2; url='.$target.'" >';
         die;
	
	}
 //===============================End check bank====================================
 //..........................................................................................
   if($transact_from!="" and $transact_to!=""){ //اگر مشتری وجود داشت 
	   
 
       //در صــــــــــــــــــــــورتــــی کـه معــاملــــه رسیــــدگــــی باشد	  
	   if(isset($transact_to)){  // اگر نوع معامله رسیدگی بود
	       $transactType=1;
		   $customerRemainSql="select amount,dollarAmount from `customerbalance` 
	                                       where
	      customerId='$transact_to' and currencyId='$counterCurrency' ";
	       $customerRemainQuery=$db ->query($customerRemainSql);
	       $customerRemainRow=$customerRemainQuery ->fetch_array();
	  
	       $customerRemainAmount=$customerRemainRow['amount'];
	       $customerRemaindollarAmount=$customerRemainRow['dollarAmount'];
		   
		   
		   $newCustomerRemain=$customerRemainAmount+$currencyAmount;
		   $newCustomerdollarRemain=$customerRemaindollarAmount+$sumDollar;
				  
		   $updateSql="update customerbalance set amount='$newCustomerRemain',dollarAmount='$newCustomerdollarRemain' where customerId='$transact_to' and currencyId='$counterCurrency'";
		   $updatequery=$db ->query($updateSql);
	       
		   if(!isset($customerRemainAmount)){ //اگر موجودی بنام این مشتری وجود نداشت برای مشتری جدیدا درست شو د
				 
				$remainInsertionSql="insert into customerbalance (date,customerId,currencyId,amount,dollarAmount)
			    values('$date','$transact_to','$counterCurrency','$currencyAmount','$sumDollar')";
				    
				$remainInsertionquery=$db ->query($remainInsertionSql);
		    }
			
		  
			 
			    //تغییر موجودی بـــانک
			    $newassetamount=$asset_Amount+$currencyAmount;
			    $newassetDollaramount=$asset_Dollar_Amount+$sumDollar;
				  
				$udateAssetSql="update assets set amount='$newassetamount',dollarAmount='$newassetDollaramount' where currencies_id='$counterCurrency'";
				$udateAssetQuery=$db ->query($udateAssetSql); 
				  //وارد شدن به بخش پرداخت مشتری
	    
					   $insertCustomerPaymentSql="insert into customerpayment 
					   (customerId,date,referenceNo,transacttype,transactCurrency,customerasset,amount,customerRemain,dollarRate,dollarTotal,description) 
												   values
					('$transact_to','$date','$refrenceNumber','$transactType','$counterCurrency','$customerAmount','$currencyAmount','$customerRemain','$dollarRate','$sumDollar','$desc')";
				   
					  $insertCustomerPaymentQuery=$db ->query($insertCustomerPaymentSql);
					  
					  if($insertCustomerPaymentQuery){
						  
						   //آخرین رو را بگیــــــــــــــــــــر
						  $selectLast_sql = "select id from customerpayment order by id desc limit 1";
						  $selectLast_sqlQuery = $db->query($selectLast_sql);
						  $selectLastRow = $selectLast_sqlQuery->fetch_array();
						  $lastID = $selectLastRow['id'];
						  
						  // انزیرشن استیتمنت مشتری
						   $insertStatement ="insert into customer_statement
						  (reference,transaction_id,customerId,currency_id,amount,date,transaction_type,description)values('معاملات مشتری','$lastID','$transact_to','$counterCurrency','$currencyAmount','$date','$transactType','$desc')";
							 $iinsertStatementQuery = $db->query($insertStatement);
						  
						  
						  // انزیرشن به جـــــــــــــــــــــورنال====================================================================
						  //نام مشتری رو بگیر
						  $selectCustomer = "select name from customers where id =$transact_to";
						  $selectCusetomerQeury = $db->query($selectCustomer);
						  $selectCustomerRow = $selectCusetomerQeury->fetch_array();
						  $customerName = $selectCustomerRow['name'];
						  
						 
						  //========
						 
							  $reference ='رسیدگی مشتری';
						   
						  //بــــــــــــــــــــــــــــــــــــه جورنال ثبتــــــــــــــــــــــــ کـــــــــــــــــــــــــــــــن
						  $journal_sql = "insert into journal(date,name,reference,reference_id, currency,threadment,amount,description) values('$date','$customerName','$reference','$lastID','$counterCurrency','$reference','$currencyAmount','$desc')"; 
						  $journalInsert = $db->query($journal_sql);
						  
						  
					  } 
				
				 
	   
	   } //پــایــــان رسیــــدگــــــــــــــــــــــــــــی
	   //...............................................................................
	   //...............................................................................
	    //درصــــــورتــــــی کــه معـــاملـــــه بــــردگـــی بـــاشـــــد
		
	   if(isset($transact_from)){  // اگــر نــوع معــاملــه بــردگــی بــاشد
	        $transactType=2;
		    $customerRemainSql="select amount,dollarAmount from `customerbalance` 
	                                       where
	        customerId='$transact_from' and currencyId='$counterCurrency' ";
	       $customerRemainQuery=$db ->query($customerRemainSql);
	       $customerRemainRow=$customerRemainQuery ->fetch_array();
	  
	       $customerRemainAmount=$customerRemainRow['amount'];
	       $customerRemaindollarAmount=$customerRemainRow['dollarAmount'];
		     $newCustomerRemain=$customerRemainAmount-$currencyAmount;
		    $newCustomerdollarRemain=$customerRemaindollarAmount-$sumDollar;
				   
		    $updateSql="update customerbalance set amount='$newCustomerRemain',dollarAmount='$newCustomerdollarRemain' where customerId='$transact_from' and currencyId='$counterCurrency'";
		    $updatequery=$db ->query($updateSql);
	       
		   if(!isset($customerRemainAmount)){ // اگر به این حساب مشتری وجود نداشت حساب جدید ایجاد شود
				 
			    $remainInsertionSql="insert into customerbalance (date,customerId,currencyId,amount,dollarAmount)
				values('$date','$transact_from','$counterCurrency','$customerRemain','$sumDollar')";
				$remainInsertionquery=$db ->query($remainInsertionSql);
		    }
			//تغییر موجودی بــانــــــــــــک
			$newassetamount_type_second=$asset_Amount-$currencyAmount;
		    $newassetDollaramount_type_second=$asset_Dollar_Amount-$sumDollar;
				   
		    $udateAssetSql="update assets set amount='$newassetamount_type_second',dollarAmount='$newassetDollaramount_type_second' where currencies_id='$counterCurrency'";
		    $udateAssetQuery=$db ->query($udateAssetSql);  
			
						  //وارد شدن به بخش پرداخت مشتری
					
				   $insertCustomerPaymentSql="insert into customerpayment 
				   (customerId,date,referenceNo,transacttype,transactCurrency,customerasset,amount,customerRemain,dollarRate,dollarTotal,description) 
											   values
				('$transact_from','$date','$refrenceNumber','$transactType','$counterCurrency','$customerAmount','$currencyAmount','$customerRemain','$dollarRate','$sumDollar','$desc')";
			   
				  $insertCustomerPaymentQuery=$db ->query($insertCustomerPaymentSql);
				  
				  if($insertCustomerPaymentQuery){
					  
					   //آخرین رو را بگیــــــــــــــــــــر
					  $selectLast_sql = "select id from customerpayment order by id desc limit 1";
					  $selectLast_sqlQuery = $db->query($selectLast_sql);
					  $selectLastRow = $selectLast_sqlQuery->fetch_array();
					  $lastID = $selectLastRow['id'];
					  
					  // انزیرشن استیتمنت مشتری
					   $insertStatement ="insert into customer_statement
					  (reference,transaction_id,customerId,currency_id,amount,date,transaction_type,description)values('معاملات مشتری','$lastID','$transact_from','$counterCurrency','$currencyAmount','$date','$transactType','$desc')";
						 $iinsertStatementQuery = $db->query($insertStatement);
					  
					  
					  // انزیرشن به جـــــــــــــــــــــورنال====================================================================
					  //نام مشتری رو بگیر
					  $selectCustomer = "select name from customers where id =$transact_from";
					  $selectCusetomerQeury = $db->query($selectCustomer);
					  $selectCustomerRow = $selectCusetomerQeury->fetch_array();
					  $customerName = $selectCustomerRow['name'];
					  
					 
					  //========
					   
					   
						  $reference ='بردگی مشتری';
						  
 
					  //بــــــــــــــــــــــــــــــــــــه جورنال ثبتــــــــــــــــــــــــ کـــــــــــــــــــــــــــــــن
					  $journal_sql = "insert into journal(date,name,reference,reference_id, currency,threadment,amount,description) values('$date','$customerName','$reference','$lastID','$counterCurrency','$reference','$currencyAmount','$desc')"; 
					  $journalInsert = $db->query($journal_sql);
					  
					  
				  } 
		 
	   
	   } //پـــایـــان بــــــردگــــــــــی
	  //......................................................................................... 
	  //......................................................................................... 
	 
	   echo' <script>
					 
					
				   swal("ذخیره موفق!", "اطلاعات وارده با موفقیت ثبت سیستم شد", "success")
				</script>';
	   echo '<meta http-equiv="refresh" content ="1; url=intern_transaction.php" >';
	   
   }//END OF $CUSTOMERID!=""
 
 
 
 //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  
}

?>
</html>