<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;
  
  $havale_sender=$_POST['havale_sender'];
   
  $havale_reciver=$_POST['havale_reciver'];
  
  $havale_num=$_POST['havale_num'];
   
  $payment_place=$_POST['payment_place'];
   
  $commissionAmount_poor=$_POST['commissionAmount'];
  $commissionAmount = preg_replace('/[^\d\.-]/', '', $commissionAmount_poor);
 
  
  $customerId=$_POST['customerId'];
 /*  echo "<br>Mahle Pardakht comission:";
  echo $commissionPlace=$_POST['commissionPlace']; */
   
  $transactType=$_POST['transactType'];
  $counterCurrency=$_POST['counterCurrency'];
  $currencyAmount_poor=$_POST['currencyAmount'];
  $currencyAmount= preg_replace('/[^\d\.-]/', '', $currencyAmount_poor);
 /*  echo "<br>Meqdar hamrahe commisssion:";
  echo $amount_pl_commission=$_POST['amount_pl_commission']; */
  
  $desc=$_POST['desc'];
  
 
  
 $havale=1;
 /* $amountP=$_POST['amount'];//مـــــوجــــودی صنـــــــدوق
 $amount = preg_replace('/[^\d\.-]/', '', $amountP);
 $customerAmount=$_POST['customerAmount']; // موجودی مشتری به ارز موجود
 $customerRemainP=$_POST['customerRemain']; //  الباقی حساب مشتری
 $customerRemain = preg_replace('/[^\d\.-]/', '', $customerRemainP);
   */
    
  
 if(strlen(trim($customerId))==0 or strlen(trim($transactType))==0 or strlen(trim($currencyAmount))==0 or strlen(trim($havale_sender))==0 or strlen(trim($havale_reciver))==0 or strlen(trim($havale_num))==0 or strlen(trim($payment_place))==0 ){
		echo' <script>
					 sweetAlert("اخطا ر... ", "لطفا جاهای خالی را پرکنید !", "error");
				  </script>';

         echo '<meta http-equiv="refresh" content ="1; url=intern_transaction.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'];
	 
	if(!isset($asset_Amount)){
	    echo' <script>
					sweetAlert("هشــدار...", "لطفا ابتدا پول را در قسمت دارییها ثبت کنید!", "error");
			   </script>'; 
		 echo '<meta http-equiv="refresh" content ="2; url='.$target.'" >';
         die;
	
	}
 //===============================End check bank====================================
 //..........................................................................................
   if($customerId!="" and $customerId!=0){ //اگر مشتری وجود داشت 
	   
	  //-------CUSTOMER BALANCE 
	    $customerRemainSql="select amount,dollarAmount from `customerbalance` 
	                                       where
	    customerId='$customerId' and currencyId='$counterCurrency' ";
	    $customerRemainQuery=$db ->query($customerRemainSql);
	    $customerRemainRow=$customerRemainQuery ->fetch_array();
	  
	    $customerRemainAmount=$customerRemainRow['amount'];
	     
	  //----END CUSTOMER BALANCE
       //در صــــــــــــــــــــــورتــــی کـه معــاملــــه رسیــــدگــــی باشد	  
	   if($transactType=='1'){  // اگر نوع معامله رسیدگی بود
	       
		   
		   $newCustomerRemain=$customerRemainAmount+$currencyAmount;
		   $updateSql="update customerbalance set amount='$newCustomerRemain' where customerId='$customerId' and currencyId='$counterCurrency'";
		   $updatequery=$db ->query($updateSql);
	       
		   if(!isset($customerRemainAmount)){ //اگر موجودی بنام این مشتری وجود نداشت برای مشتری جدیدا درست شو د
				  
				 
				$remainInsertionSql="insert into customerbalance 
				(
				date,
				customerId,
				currencyId,
				amount,
				dollarAmount
				)
			    values(
				'$date',
				'$customerId',
				'$counterCurrency',
				'$currencyAmount',
				'$sumDollar'
				)";
				    
				$remainInsertionquery=$db ->query($remainInsertionSql);
		    }
			
		  
			 
			      
				 
			   $newassetamount=$asset_Amount+$commissionAmount+$currencyAmount;//پول پرداختی همراه کمیشن به دخل وارد شود
			   $udateAssetSql="update assets set amount='$newassetamount',dollarAmount='$newassetDollaramount' where currencies_id='$counterCurrency'";
			   $udateAssetQuery=$db ->query($udateAssetSql);
				//customerStatment
				
				//customerStatment
	   
	   } //پــایــــان رسیــــدگــــــــــــــــــــــــــــی
	   //...............................................................................
	   //...............................................................................
	    //درصــــــورتــــــی کــه معـــاملـــــه بــــردگـــی بـــاشـــــد
		
	   if($transactType=='2'){  // اگــر نــوع معــاملــه بــردگــی بــاشد
	       
		    $newCustomerRemain=$customerRemainAmount-$currencyAmount;
		     	   
		    $updateSql="update customerbalance set amount='$newCustomerRemain' where customerId='$customerId' and currencyId='$counterCurrency'";
		    $updatequery=$db ->query($updateSql);
	       
		   if(!isset($customerRemainAmount)){ // اگر به این حساب مشتری وجود نداشت حساب جدید ایجاد شود
				 
			    $remainInsertionSql="insert into customerbalance (
				date,
				customerId,
				currencyId,
				amount,
				dollarAmount
				)
				values(
				'$date',
				'$customerId',
				'$counterCurrency',
				'$customerRemain',
				'$sumDollar'
				)";
				$remainInsertionquery=$db ->query($remainInsertionSql);
		    }
			//تغییر موجودی بــانــــــــــــک
			$newassetamount_type_second=$asset_Amount-$currencyAmount;
 
		    $udateAssetSql="update assets set amount='$newassetamount_type_second' where currencies_id='$counterCurrency'";
		    $udateAssetQuery=$db ->query($udateAssetSql);
	   
	   } //پـــایـــان بــــــردگــــــــــی
	  //......................................................................................... 
	  //......................................................................................... 
	   //وارد شدن به بخش پرداخت مشتری
	    
	  $insertCustomerPaymentSql="insert into customerpayment 
       (
	   customerId,
	   date,
	   referenceNo,
	   transacttype,
	   transactCurrency,
	   customerasset,
	   amount,
	   customerRemain,
	   dollarRate,
	   dollarTotal,
	   havale_sender,
	   havale_reciver,
	   havale_num,
	   payment_place,
	   commissionAmount,
	   description
	   ) 
      values
    (
	'$customerId',
	'$date',
	'$refrenceNumber',
	'$transactType',
	'$counterCurrency',
	'$customerAmount',
	'$currencyAmount',
	'$customerRemain',
	'$dollarRate',
	'$sumDollar',
	'$havale_sender',
	'$havale_reciver',
	'$havale_num',
	'$payment_place',
	'$commissionAmount',
	'$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',
		  '$customerId',
		  '$counterCurrency',
		  '$currencyAmount',
		  '$date',
		  '$transactType',
		  '$desc'
		  )";
    		 $iinsertStatementQuery = $db->query($insertStatement);
		  
		  
		  // انزیرشن به جـــــــــــــــــــــورنال====================================================================
		  //نام مشتری رو بگیر
		  $selectCustomer = "select name from customers where id =$customerId";
		  $selectCusetomerQeury = $db->query($selectCustomer);
		  $selectCustomerRow = $selectCusetomerQeury->fetch_array();
		  $customerName = $selectCustomerRow['name'];
		  
		 
		  //========
		  if($transactType==1){
			  $reference ='رسیدگی مشتری';
			  $dr_cr=1;
		  }
		  if($transactType==2){
			  $reference ='بردگی مشتری';
			  $dr_cr=2;
			  
		  }
		  //بــــــــــــــــــــــــــــــــــــه جورنال ثبتــــــــــــــــــــــــ کـــــــــــــــــــــــــــــــن
		  $journal_sql = "insert into journal(
		  date,
		  name,
		  reference,
		  reference_id,
		  currency,
		  threadment,
		  amount,
		  dr_cr,
		  description
		  ) 
		  values
		  (
		  '$date',
		  '$customerName',
		  '$reference',
		  '$lastID',
		  '$counterCurrency',
		  '$reference',
		  '$currencyAmount',
		  '$dr_cr',
		  '$desc'
		  )"; 
	      $journalInsert = $db->query($journal_sql);
		  
		  if($commissionAmount!=null){
		  
		  $name_search_sql="select * from incomename where incomeName='کمیشن' and remove=0";
		  $name_search_query= $db ->query($name_search_sql);
		  $name_search_row=$name_search_query ->fetch_array();
		  $name_search_id=$name_search_row['id'];
		  $incomeId=$name_search_id;
		  if($name_search_id==null){
		   $insert_in_income_sql="insert into incomename (incomeName) value ('کمیشن');  ";
		   $insert_in_income_query= $db ->query($insert_in_income_sql);
		   echo "<br>";
		   $takeId_sql="select * from incomename where incomeName='کمیشن' and remove=0";
		   $takeId_query= $db ->query($takeId_sql);
		   $takeId_row=$takeId_query ->fetch_array();
		   $takeId=$takeId_row['id'];
		   $incomeId=$takeId;
		  }
		  
		    $income_sql="insert into others(
		   date,
		   expensesId,
		   currencyId,
		   amount,
		   typeOthers,
		   havale_id,
		   description)
		   values
		   (
		   '$date',
		   '$incomeId',
		   '$counterCurrency',
		   '$commissionAmount',
		   '2',
		   '$lastID',
		   '$desc'
		   )
		   ";
		  $income_query= $db ->query($income_sql);
		  
		  $income_lastId_sql="select * from others order by id desc limit 1";
		  $income_lastId_query = $db ->query($income_lastId_sql);
		  $income_lastId_row=$income_lastId_query ->fetch_array();
		  $income_lastId=$income_lastId_row['id'];
		  
		  //insert journal
		  $date=$date;
		  $namePart="کمیشن حواله";
		  $reference="کمیشن حواله";
		  $lastID=$income_lastId;
		  $currency=$counterCurrency;
		  $threadment=$reference;
		  $amount=$commissionAmount;
		  $dr_cr=1;
		  $description=$desc;
		  include("entery_journal_majul.php");
		  //insert journal
		  
		  }
		  
	  }
	 
	  
	  header("location:intern_transaction.php");
	   
   }//END OF $CUSTOMERID!=""
 
 
 
 //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
 if($customerId=="" or $customerId==0){ //اگر مشتری وجود داشت 
 
      echo' <script>
					sweetAlert("هشــدار...", "خطا در ثبت لطفا اطلاعات را دوباره وارد کنید!", "error");
			   </script>';

	 echo '<meta http-equiv="refresh" content ="2; url='.$target.'" >';
         die;
	
 
  
   
}
}

?>
</html>