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

// Get all _POST VALUES HERE..

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

  $myProd = $_POST['Prod'];
  
  
  
  $aresult = mysqli_query($conn,"select distinct account_year from branches ") or die(mysqli_error());
  $arow = mysqli_fetch_row($aresult);
  $account_year = $arow[0];
  ?>
<body background="../../images/fibermat.gif"><center>
<h2><b> <font face="Arial, Helvetica, sans-serif">View / Edit Issue / Manufactured 
  Items</font></font></font></h2>
<font face="Arial, Helvetica, sans-serif"> 
<?php

// Pull all the data from Prod master & populate it

$stQuery = mysqli_query($conn,"select distinct acctcode from stktrans where account_year = '$account_year' and transact_type !='S' and transact_type !='P' and account_year = '$account_year' order by acctcode");

?>
</font> <font face="Arial, Helvetica, sans-serif"><form name='myForm01' method='POST' action='<?=$_SERVER['PHP_SELF']?>'> 
<SELECT name='Prod'   onChange="javascript:submit();">
  <?
     echo "<option selected value=\" 0\"> Select Product";
    while($row = mysqli_fetch_row($stQuery))
    {
		$presult = mysqli_query($conn,"select prodname from product where prodcode = '$row[0]' ") or die(mysqli_error());
		$prow = mysqli_fetch_row($presult);
		
		$temp = $prow[0];
        if(isset($myProd) && preg_match("/" . preg_quote($myProd, '/') . "/", $row[0]))
	    {
            echo "<option selected value=\"$row[0]\">$temp";
        }
        else{
            echo "<option value=\"$row[0]\">$temp";
        }
    }
?>
</SELECT></td></tr>
</font> <form> <center> 
<?

$query = "select distinct stktrans.record,invno as ir_no,date ,product.prodname,inqty,outqty,transact_type,remarks from stktrans,product where acctcode = '$myProd' and transact_type != 'S' and transact_type != 'P' and stktrans.acctcode = product.prodcode and account_year = '$account_year' ";
$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>
