<?php
include("../include/session.php");

	$today = date("Y-m-d");
	$d = date("d");
	$m = date("m");
	$y = date("Y");
	
	$user_id = $_SESSION['username'];
	date_default_timezone_set('Africa/Gaborone');
    $entryDate = date("Y-m-d H:i:s",time()-3600); 
	
	$balance_back = $_GET['received_selection'];
	if ($balance_back=='yes') {
		$table_name = $_SESSION['db_prefix']."_pettycash_lines_temp";
		$back_query = "INSERT INTO $table_name ( pettycash_id, reference_date, reference, reference_no, amount_debit, user_name ) VALUES ('".$_SESSION['pettycash_id']."', '".$today."', 'Cash back', '', '".$_SESSION['cash_balance']."', '".$_SESSION['username']."'  ) ";
		execsql($back_query);
	}
		
	$table_name1 = $_SESSION['db_prefix']."_pettycash_lines_temp";
	$table_name2 = $_SESSION['db_prefix']."_pettycash_lines";
    $copy_query = "INSERT INTO $table_name2 (pettycash_id, reference_date, reference, reference_no, amount_debit ) SELECT pettycash_id, reference_date, reference, reference_no, amount_debit  FROM $table_name1 WHERE pettycash_id = '".$_SESSION['pettycash_id']."' AND user_name = '".$_SESSION['username']."' ";	
	execsql($copy_query);	
	
	$table_name = $_SESSION['db_prefix']."_pettycash_lines";
	$return_query = "SELECT SUM(amount_debit) AS tot_amount_debit  FROM $table_name WHERE pettycash_id = '".$_SESSION['pettycash_id']."' GROUP BY pettycash_id   ";
	$return_result = mySQL_query($return_query);
	$return = mysql_result($return_result, 0, "tot_amount_debit");
	
	$table_name = $_SESSION['db_prefix']."_pettycash";
	$returned_amount_query = "UPDATE $table_name SET returned_amount = '".$return."' WHERE pettycash_id = '".$_SESSION['pettycash_id']."' ";
	execsql($returned_amount_query);
	
	$table_name = $_SESSION['db_prefix']."_tracking_hr";
   $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '26', '".$_SESSION['emp_id']."' ,'".$_SESSION['pettycash_id']."','".$return."','' )";
   $trackingResult = mySQL_query($trackingQuery);
	
	if (isset($_GET['new_button']) )
	   {
		 $action='pettycashReturnWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='pettycash_list.php?date=0&emp_id=';
	  }
	
	$table_name = $_SESSION['db_prefix']."_pettycash_lines_temp";
	$delete_query = "DELETE FROM $table_name WHERE  user_name = '".$_SESSION['username']."'   "; 
	execsql($delete_query);
	
	
	 $_SESSION['emp_id']="";
     $_SESSION['amount'] = "0.00";
	 $_SESSION['pettycash_id'] = "";
	 $_SESSION['cash_balance'] = "";
	 $_SESSION['issued_by'] = "";
	 $_SESSION['issue_date'] = date('Y-m-d');
     $_SESSION['employee_name'] = "";
	 $_SESSION['use'] = "";
	 $_SESSION['notes'] = "";

	 
?>

<script language="javascript" type="text/javascript">
        <!--
		  window.location="<?php echo $action ?> ";
        -->
       </script>



