<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$formname = 'Opening Stock';
$maindb = 'openstock';
$file = 'openstock.php';
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);



$dcode = mysqli_real_escape_string($conn, $_GET["code"]);
$colorcode = "#FF8C00";
if (!empty($dcode)) {
    $result = mysqli_query($conn, "SELECT * FROM `item`  ORDER BY name") or die(mysqli_error($conn));
    $flag = "";
} else {
    $result = mysqli_query($conn, "SELECT * FROM `item` ORDER BY name") or die(mysqli_error($conn));
    $flag = "";
}
$ctr = 0;

//echo '<table id="detail" border="1" width="40%" align="right">';
//echo "<tr>";
//echo '<th>
//    </th>';
//echo '<th align="right"><font color="Green" size="3px">Debit  </font><input type="text" size="10" id="totdebit" name="totdebit" class="form-control" readonly></th>';
//echo '<th align="right"><font color="red" size="3px">Credit  </font><input type="text" size="10" id="totcredit" name="totcredit" class="form-control" readonly></th>';
//echo '</tr>';
//echo '</table>';
echo '<table id="openstock" border="1" width="100%" align="center">';

echo "<tr>";
echo "<th style='font-family: arial-regular;'>Name $flag</label></th>";
echo "<th align=''  style='font-family: arial-regular;text-align:right;'><b>Cost</b></th>";
echo "<th align='' style='font-family: arial-regular;text-align:right;'><b>Rate</b></th>";
echo "<th align='' style='font-family: arial-regular;text-align:right;'><b>MRP</b></th>";
echo "<th align='' style='font-family: arial-regular;text-align:right;'><b>Opening</b></th>";

echo '</tr>';
while ($row = mysqli_fetch_array($result)) {
    $openqty = mysqli_query($conn, "SELECT sum(qty) as opqty  FROM `transacn` where itemcode = $row[code] and `godown`='$dcode' and source='OP'") or die(mysqli_error($conn));
    $getopen = mysqli_fetch_array($openqty); 
    
    echo "<tr>";
    echo '<td align="center"><input type="hidden" name="code_' . $ctr . '" value="' . $row["code"] . '">
             <input type="text" size="40"  maxlength=""  name="item_' . $ctr . '"  value="' . $row["name"] . '" class="form-control" readonly/></td>';
    echo '<td align="center">
              <input type="text" size="10" maxlength="" style="text-align: right;" name="cost_' . $ctr . '" value="' . $row["cost"] . '" class="form-control" autofocus  ></td>';
    echo '<td align="left"><input type="text" size="10"  maxlength="" name="rate_' . $ctr . '" value="' . $row["rate"] . '" class="form-control" style="text-align:right; padding-left: 5px;"  ></td>';
    echo '<td align="center "><input type="text" size="10" maxlength="" name="mrp_' . $ctr . '" value="'.$row["mrp"].'" style="text-align:right;" class="form-control" ></td>';
    echo '<td align="center"><input type="text" size="10" style="text-align: right;" maxlength="" name="qty_' . $ctr . '" value="'.$getopen["opqty"].'" class="qty form-control" /></td>';
    echo '</tr>';
    $ctr++;
}
echo "<input type='hidden' name='count' value='$ctr'>";
echo "</table>";
?>
