<?php
 include "../lib/db/connection.php";
 
 
 function find_a_field($table_name,$field,$unique_id){
global $conn;
 $sql = "select ".$field." from ".$table_name." where ".$unique_id."";

$select = $conn->query($sql);
if ($select->num_rows>0){
$data = $select->fetch_assoc();
return $data[$field];

}
 }
 
 function find_all($table_name,$unique_id){
  global $conn;
$sql = "select * from ".$table_name." where ".$unique_id."";

$select = $conn->query($sql);
if ($select->num_rows>0){
$data = $select->fetch_assoc();
return $data;

}
 }
 
 
 
 
 // foreign relation

function foreign_relation($table,$field1,$field2,$value,$condition){
    //return date('Y-m-d');
global $conn;
if($condition !=""){
	$con=" WHERE $condition";
}

 echo $sql = "SELECT $field1,$field2 FROM $table $con ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
   // echo '<option></option>';
  while($row = $result->fetch_assoc()) {
	  if($value==$row[$field1]){
	echo '<option value="'.$row[$field1].'" selected >'.$row[$field2].'</option>';
	  }else{
		echo '<option value="'.$row[$field1].'">'.$row[$field2].'</option>';
	  }
  }
} 

 
 }




  

//journal entry for payable

function journal($max_jv,$jv_date,$ledger_id,$dr_amt,$cr_amt,$tr_from,$tr_no,$status='',$narration=''){
global $conn;

$payable_dr = "INSERT INTO `journal` (`jv_no`, `jv_date`, `ledger_id`,`dr_amt`,`cr_amt`, `tr_from`, `tr_no`,`status`,`entry_by` ,`group_for`,`narration`) 
VALUES ('".$max_jv."','".$jv_date."','".$ledger_id."','".$dr_amt."','".$cr_amt."','".$tr_from."','".$tr_no."','".$status."','".$_SESSION['user_id']."' 
,'".$_SESSION['group_for']."','".$narration."')";

$journal_insert_dr =$conn->query($payable_dr);

}
?>

