<?php

	include "../dbconfig.php" ;
	//echo $_POST['source_id'];
	//exit;
	$amount=$_POST['amount'];
	
	$source_id=$_POST['source_id'];
	//$source_id=268;
//	echo $_POST['transaction_date'];
	$transaction_date=$_POST['transaction_date']; 
	$transaction_date=date("Y-m-d",strtotime($transaction_date));
	//echo $transaction_date;
	//exit;
	//$source_id=$_POST['source_id'];
	$payment_type=$_POST['payment_type'];
	$mode_of_payment=$_POST['mode_of_payment'];
	$details=$_POST['details'];
			//echo $amount;
			// now we will update the passbook table to update the balance.

			$pass_sql = "SELECT current_bal FROM passbook WHERE company_id=4";
			$pass=mysqli_query($con,$pass_sql);
			$passbook=mysqli_fetch_assoc($pass);
			$bank_bal=$passbook['current_bal'];
			// now we will update the passbook table to update the balance of this client.

			$client_sql = "SELECT current_bal FROM passbook WHERE company_id=20";
			$pass_sql=mysqli_query($con,$client_sql);
			$passbook=mysqli_fetch_assoc($pass_sql);
			$client_bal=$passbook['current_bal'];
			
			//now check if money is paid or money is recieved
			if($payment_type=="dr")
			{
				
			$debt_sql = "SELECT balance FROM debtors WHERE source_id=".$source_id;
			$debt=mysqli_query($con,$debt_sql);
			$d_balance=mysqli_fetch_assoc($debt);
			$d_bal=$d_balance['balance'];
			//echo $d_bal;
					if($d_bal==0)
					{
					 echo "<script>var confirm = confirm('There is nothing to pay, you still want to pay ?');
          if(confirm)
		  {
		//  alert('dfsdThere is nothing to pay, you still want to pay ?');
		  }
		  else
		  {
		   window.location='../new_transaction.php';
		 }  
		  </script>";
		  
		 
		
		  
				/*	echo '<script language="javascript">';
					echo 'alert("There is nothing to pay, you still want to pay ?")';
					echo '</script>';
					//header("Refresh: 1; url=../new_transaction.php");
					 echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../new_transaction.php">';
					exit;		*/			
					}	
			//just check if you are paying him more money
					if($amount>$d_bal)
					{
					 echo "<script>var confirm = confirm('You are paying more than balance, you still want to pay ?');
          if(confirm)
		  {
		 // alert('dfsdThere is nothing to pay, you still want to pay ?');
		  }
		  else
		  {
		   window.location='../new_transaction.php';
		 }  
		  </script>";
			
				/*	echo '<script language="javascript">';
					echo 'alert("You are paying more than balance.")';
					echo '</script>'; */
					//header("Refresh: 1; url=../new_transaction.php");
					//exit;					
					}	
			
					if($bank_bal<$amount)
					{
				
					
					echo '<script language="javascript">';
					echo 'alert("There are not enough funds in your account.")';
					echo '</script>';
					//header("Refresh: 1; url=../new_transaction.php");
					 echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../new_transaction.php">';
					exit;					
					}		
					
				  	  $dr_sql = "SELECT name,balance FROM debtors WHERE source_id=".$source_id;
					  $dr=mysqli_query($con,$dr_sql);
					  $dr_balance=mysqli_fetch_assoc($dr);
					  $dr_bal=$dr_balance['balance'];
					  $d_name=$dr_balance['name'];
					  
					  //Update the balance of this debtor
					  $new_bal=$dr_bal-$amount;							
					  $upd_dr = "UPDATE debtors SET ". 
					  "balance =".$new_bal."". 
					  " WHERE source_id=".$source_id;
					 // echo "Query is <br> " .$upd_dr. "<br>";
					  mysqli_query($con,$upd_dr);
					  
					  //Now update Passbook
					  $client_passbook_bal=$bank_bal-$amount;
					  $upd = "UPDATE passbook SET ". 
					  "current_bal =".$client_passbook_bal."". 
					  " WHERE company_id=4";
					  mysqli_query($con,$upd);
					  //Now update Passbook
					  $passbook_bal=$client_bal-$amount;
					  $upd = "UPDATE passbook SET ". 
					  "current_bal =".$passbook_bal."". 
					  " WHERE company_id=20";
					  mysqli_query($con,$upd);
					  //Make a transaction
					  $current_bal=$bank_bal-$amount;
							  
					  $sql = "INSERT INTO transactions". 
					  //type database fields exactly here
					  " (
					  transaction_date,
					  source_id,
					  name,
					  transaction_type,
					  mode_of_payment,
					  amount,
					  details,
					  current_bal	
					  )" . 
					  
					  //type names of form variables exactly here
					  "VALUES 
					  (
					  '$transaction_date',
					  '$source_id',
					  '$d_name',
					  '$payment_type',
					  '$mode_of_payment',
					  '$amount',
					  '$details',
					  '$current_bal'
					  ) ";
						
						//echo $sql;
						mysqli_query($con,$sql);
						$last_insert_id=mysqli_insert_id($con);	
						echo '<script language="javascript">';
					echo 'alert("Transaction Successful")';
					echo '</script>';
						//header("Refresh: 1; url=../new_transaction.php");
						 echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../new_transaction.php">';
						exit;
			}
				
			elseif($payment_type=="cr")
			{
				       
					  $cr_sql = "SELECT  name,balance FROM creditors WHERE source_id=".$source_id;
					 // echo $cr_sql;
					  //exit;
					  $cr=mysqli_query($con,$cr_sql);
					  $cr_balance=mysqli_fetch_assoc($cr);
					  $cr_bal=$cr_balance['balance'];
					  $c_name=$cr_balance['name'];
					 // echo $cr_bal;
					 // exit;
					  if($amount>$cr_bal)
					{
					echo '<script language="javascript">';
					echo 'alert("You are recieving more than balance.")';
					echo '</script>';
						
					//header("Refresh: 1; url=../new_transaction.php");
					 echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../new_transaction.php">';
					exit;			
					}	
					
					  //Update the balance of this creditor
					  $new_bal=$cr_bal-$amount;							
					  $upd_cr = "UPDATE creditors SET ". 
					  "balance =".$new_bal."". 
					  " WHERE source_id=".$source_id;
					  //echo "Query is <br> " .$upd_cr. "<br>";
					  mysqli_query($con,$upd_cr);
					  
					  //Now update Passbook
					  $passbook_bal=$bank_bal+$amount;
					  $upd = "UPDATE passbook SET ". 
					  "current_bal =".$passbook_bal."". 
					  " WHERE company_id=4";
					  mysqli_query($con,$upd);


                        $inv_sql = "SELECT  * FROM invoices WHERE customer_id=".$source_id;
					 // echo $cr_sql;
					  //exit;
					  $inv=mysqli_query($con,$inv_sql);
					  $inv_balance=mysqli_fetch_assoc($inv);
					  $inv_bal=$inv_balance['amount_paid'];
					  $inv_bal1=$inv_bal+$amount;
					                          
					  $upd_inv = "UPDATE invoices SET ". 
					  "amount_paid =".$inv_bal1."". 
					  " WHERE customer_id=".$source_id;
					  mysqli_query($con,$upd_inv);
					  
					  
					   $inv_sql1 = "SELECT  * FROM invoices_wings_courier WHERE customer_id=".$source_id;
					 // echo $cr_sql;
					  //exit;
					  $inv1=mysqli_query($con,$inv_sql1);
					  $inv_balance1=mysqli_fetch_assoc($inv1);
					  $inv_bal1=$inv_balance1['amount_paid'];
					  $inv_bal11=$inv_bal1+$amount;
					                          
					  $upd_inv1 = "UPDATE invoices_wings_courier SET ". 
					  "amount_paid =".$inv_bal11."". 
					  " WHERE customer_id=".$source_id;
					  mysqli_query($con,$upd_inv1);

					  
					  $current_bal=$bank_bal+$amount;
							  //Make a transaction
						$sql = "INSERT INTO transactions". 
						//type database fields exactly here
						" (
					  transaction_date,
					  source_id,
					  name,
					  transaction_type,
					  mode_of_payment,
					  amount,
					  details,
					  current_bal	
						)" . 
						
						//type names of form variables exactly here
						"VALUES 
						(
						'$transaction_date',
						'$source_id',
						'$c_name',
						'$payment_type',
						'$mode_of_payment',
						'$amount',
						'$details',
						'$current_bal'
						) ";
						
						//echo $sql;
						mysqli_query($con,$sql);
						$last_insert_id=mysqli_insert_id($con);	
						echo '<script language="javascript">';
					echo 'alert("Transaction Successful")';
					echo '</script>';
						//header("Refresh: 1; url=../new_transaction.php");
						 echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../new_transaction.php">';
						exit;
			}
			
	
	$sql = "INSERT INTO transactions". 
	//type database fields exactly here
	" (
	transaction_date,
	source_id,
	payment_type,
	mode_of_payment,
	amount,
	details,
	balance	
	)" . 
	
	//type names of form variables exactly here
	"VALUES 
	(
	'$transaction_date',
	'$source_id',
	'$payment_type',
	'$mode_of_payment',
	'$amount',
	'$details',
	'$current_bal'
	) ";
	
	//echo $sql;
	//exit;
	//echo "<BR>";
	$result=mysqli_query($con,$sql);
		
		
		
		if ($result)
			
		{
				echo '<script language="javascript">';
				echo 'alert("Transaction created successfully,")';
				echo '</script>';
				$upd = "UPDATE passbook SET ". 
				"current_bal =".$current_bal.",". 
				"transaction_id=".$last_insert_id."".
				" WHERE company_id=4";
				mysqli_query($con,$upd);
				//echo "Query is <br> " .$upd. "<br>";
				
				//update the debtors account as well
				
				$upd_debt = "UPDATE debtors SET ". 
				"balance =".$d_bal."". 
				" WHERE source_id=".$source_id;
				//echo "Query is <br> " .$upd_debt. "<br>";
				mysqli_query($con,$upd_debt);
				
				//header("Refresh: 1; url=../new_transaction.php");
				 echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../new_transaction.php">';
		exit;
		}
			
		else
		
			{	
				echo '<script language="javascript">';
				echo 'alert("Transaction was not created. ")';
				echo '</script>';
			}

?>
