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

// Get all _POST VALUES HERE..


  foreach($_POST as $p=>$t)
  {
     $_POST[$p] = trim($t);
  }

  $myCompany = $_POST['Company'];
  
  

  

  foreach($_POST as $p=>$t)
  {
     $_POST[$p] = trim($t);
  }

  $myCatcode = $_POST['Catcode'];
  
  foreach($_POST as $h=>$i)
  {
     $_POST[$h] = trim($i);
  }

  $myAyear = $_POST['Ayear'];  
  
  ?>
<style type="text/css">
body{
font-family:Verdana, Arial, Helvetica, sans-serif;
}
</style>
<link rel="stylesheet" href="../../include/style.css">
<body oncontextmenu = "javascript:return false;" ondragdrop= "javascript:return false;" onselectstart="javascript:return false;"><center>
<table cellpadding=0 cellspacing=4 width=590>
<tr>
<td style="padding:3px;" class="tdhead"  colspan="100%"><b>View Opening Balances</b></td>

</tr>



<form name='myForm01' method='POST' action='<?=$_SERVER['PHP_SELF']?>'>


<tr>
<td style="padding:3px;" colspan=2 class="tdhead" ><b>Select Company</b></td>
<td >
  
  <SELECT name='Company'   onChange="javascript:submit();">
    <?
	// Pull all the data from Catcode master & populate it
	
	$stQuery = mysqli_query($conn,"select distinct compcode,name from company order by name");


	
	
     echo "<option selected value=\" 0\"> Select Company ";
    while($row = mysqli_fetch_row($stQuery))
    {

		$temp = $row[1];
        if(isset($myCompany) && preg_match("/" . preg_quote($myCompany, '/') . "/", $row[0]))
	    {
            echo "<option selected value=\"$row[0]\">$temp";
        }
        else{
            echo "<option value=\"$row[0]\">$temp";
        }
    }
?>
  </SELECT></td></tr>
<?
/*


<tr>
<td style="padding:3px;" colspan=2 class="tdhead" ><b>Select Profit Centre</b></td>
<td >
  
  <SELECT name='Branch'   onChange="javascript:submit();">
    <?
	// Pull all the data from Catcode master & populate it
	
	$stQuery = mysqli_query($conn,"select distinct code,name from branches where compcode = '$myCompany' order by name");


	
	
     echo "<option selected value=\" 0\"> Select Profit Centre ";
    while($row = mysqli_fetch_row($stQuery))
    {

		$temp = $row[1];
        if(isset($myBranch) && preg_match("/" . preg_quote($myBranch", '/') . "/", $row[0]))
	    {
            echo "<option selected value=\"$row[0]\">$temp";
        }
        else{
            echo "<option value=\"$row[0]\">$temp";
        }
    }
?>
  </SELECT></td></tr>
*/
?>

<tr>
<td style="padding:3px;" colspan=2 class="tdhead" ><b>Select Group</b></td>
<td >
  
  <SELECT name='Catcode'   onChange="javascript:submit();">
    <?
	// Pull all the data from Catcode master & populate it
	
	$stQuery = mysqli_query($conn,"select distinct acctcode,groupname from acctgroup  order by groupname");


	
	
     echo "<option selected value=\" 0\"> Select Group";
    while($row = mysqli_fetch_row($stQuery))
    {

		$temp = $row[1];
        if(isset($myCatcode) && preg_match("/" . preg_quote($myCatcode, '/') . "/", $row[0]))
	    {
            echo "<option selected value=\"$row[0]\">$temp";
        }
        else{
            echo "<option value=\"$row[0]\">$temp";
        }
    }
?>
  </SELECT></td></tr>
  
  
<tr>
<td style="padding:3px;" colspan=2 class="tdhead" ><b>Select Account Year</b></td>
<td >
  
  <SELECT name='Ayear'   onChange="javascript:submit();">
    <?
	// Pull all the data from Catcode master & populate it
	
	$stQuery = mysqli_query($conn,"select distinct account_year from ledger union select distinct account_year from openbalance order by account_year  desc");


	
	
     echo "<option selected value=\" 0\"> Select Account Year";
    while($row = mysqli_fetch_row($stQuery))
    {

		$temp = $row[0];
        if(isset($myAyear) && preg_match("/" . preg_quote($myAyear, '/') . "/", $row[0]))
	    {
            echo "<option selected value=\"$row[0]\">$temp";
        }
        else{
            echo "<option value=\"$row[0]\">$temp";
        }
    }
?>
  </SELECT></td></tr>  
  
</form>

<?
$query = "select  openbalance.record,acctmast.description,openbalance.opening_balance,openbalance.db_cr,account_year from openbalance,acctmast where acctgroup = '$myCatcode' and openbalance.branch_code = '$myBranch' and openbalance.compcode = '$myCompany' and openbalance.account_year = '$myAyear' and openbalance.acctcode = acctmast.acctcode    ";
$result = mysqli_query($conn, $query);

if (!$result) {
    die("Query failed: " . mysqli_error($conn));
}

$number_cols = mysqli_num_fields($result);

echo '<table border="1" class="table table-striped">';
echo '<tr align="center">';

for ($i = 0; $i < $number_cols; $i++) {
    $fieldInfo = mysqli_fetch_field_direct($result, $i);
    echo '<th>' . $fieldInfo->name . '</th>';
}

echo '</tr>';

while ($row = mysqli_fetch_assoc($result)) {
    echo '<tr align="left">';
    echo '<td>';
    
    if (!isset($row['record'])) {
        echo 'NULL';
    } else {
        echo '<a href="editview.php?record=' . $row['record'] . '"><img src="../../images/b_edit.png"></a>';
    }

    foreach ($row as $key => $value) 
    {
       if ($key != 'record') {
        echo '<td>';
        echo $value;
       }
    }
    
    echo '</tr>';
}

echo '</table>';

echo '<br><br><font size="1"><center><a href="index.html">Return to index page</a></center></font>';
?>
</div>

</body>
</html>
