<?php session_start();
if($_SESSION[invcox] == ""){
header("location:login.php?action=need");
}else{

include("config.php");?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8" />
<title>A2Z Accounting Software</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<SCRIPT language="JavaScript" type="text/javascript">

function validate()

{
	if(document.enquiryform.pname.value=="")

		{//alert ("Please type Product Code.")
			document.enquiryform.pname.focus()
			return false

		}
}

function validate2()

{
	if(document.form1.product.value=="")

		{
			alert ("Please Select Product .")
			document.form1.product.focus()
			return false

		}
		
	if(document.form1.product_type.value=="")

		{
			alert ("Please Type product type .")
			document.form1.product_type.focus()
			return false

		}
	if(document.form1.product_model.value=="")

		{
			alert ("Please Type product model .")
			document.form1.product_model.focus()
			return false

		}
	if(document.form1.product_code.value=="")

		{
			alert ("Please Type product code.")
			document.form1.product_code.focus()
			return false

		}
}
function validate3()

{
	if(document.form2.pname2.value=="")

		{
			alert ("Please Type Product .")
			document.form2.pname2.focus()
			return false

		}
		
	if(document.form2.product_type2.value=="")

		{
			alert ("Please Type product type .")
			document.form2.product_type2.focus()
			return false

		}
	if(document.form2.product_model2.value=="")

		{
			alert ("Please Type product model .")
			document.form2.product_model2.focus()
			return false

		}
	if(document.form2.product_code2.value=="")

		{
			alert ("Please Type product code.")
			document.form2.product_code2.focus()
			return false

		}
}
function formfile(){
var pmodo=document.form1.pmodo.value;
document.form1.product_model.value=pmodo;
}

function formfile1(){
var ptyp=document.form1.ptyp.value;
document.form1.product_type.value=ptyp;
}
function formfile3(){
var pmodo=document.form2.pmodo2.value;
document.form2.product_model2.value=pmodo;
}

function formfile2(){
var ptyp=document.form2.ptyp2.value;
document.form2.product_type2.value=ptyp;
}
</SCRIPT>
<style>

	  #myTable  table {
      border-collapse: collapse;
      width: 100%;
      max-width: 900px;
      margin: 1.5rem auto;
      font-family: system-ui, sans-serif;
    }
  #myTable  th,#myTable td {
      padding: 12px 16px;
      text-align: left;
      border-bottom: 1px solid #ddd;
    }
   #myTable th {
      background: #f5f5f5;
      cursor: pointer;
      user-select: none;
      position: relative;
      padding-right: 24px;
    }
  #myTable  th:hover {
      background: #e8e8e8;
    }
   #myTable th::after {
      content: '↕';
      position: absolute;
      right: 10px;
      opacity: 0.4;
      font-size: 0.9em;
    }
	
   #myTable th.asc::after  { content: '↑'; opacity: 1; }
   #myTable th.desc::after { content: '↓'; opacity: 1; }
   #myTable tr:hover {
      background: #f9f9ff;
    }
 
</style>
</head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<body>

<table width="95%" border="0" align="center" cellpadding="0" cellspacing="1" class="box-wrap">
  <tr>
    <td><?php include("top.php");?></td>
  </tr>
  <tr>
    <td >
    <table width="100%" border="0" cellspacing="1" cellpadding="0">
        <tr>
          <td height="30" align="left" background="images/place_bg.jpg" class="link2"><strong>&nbsp;<a href="index.php">Home</a> &gt; <a href="inventory.php">Inventory</a></strong> &gt; <a href="add_product.php"><strong> Product Entry </strong></a></td>
        </tr>
        <tr>
          <td align="center">&nbsp;</td>
        </tr>
		<?php 
		if($_GET[action] == 'edit'){
		?>
		<?php }else{?>
		<?php }?>
        <tr>
          <td bgcolor="#FFFFFF"><br /><h3 align="center">Available Product List </h3>
            <table width="100%" border="0" cellpadding="3" cellspacing="1"   id="myTable">
                 <thead>
              <tr>
                <th onclick="sortTable(0)"><strong>Sr.</strong></td>
                <th onclick="sortTable(1)" ><strong>Product Name </strong></td>
                <th onclick="sortTable(2)" ><strong>Product item </strong></td>
                <th onclick="sortTable(3)" >Material</td>
                <th onclick="sortTable(4)" width="5%" ><strong>Sale Rate</strong></td>
                <th onclick="sortTable(5)" width="40%" >Meterial Stock</td>
                <th onclick="sortTable(6)" width="9%" align="center" ><strong>Salable </strong></td>
              </tr>
                </thead>
  <tbody>
              <?php 
$data = array();

$xresult = mysql_query("
    SELECT 
        category,
        SUM(CASE WHEN stype = '0' THEN qty ELSE 0 END) AS qty_in,
        SUM(CASE WHEN stype = '1' THEN qty ELSE 0 END) AS qty_out
    FROM meterial_in
    GROUP BY category
");

while ($xrow = mysql_fetch_assoc($xresult)) {

    $categoryKey = 'c_' . $xrow['category'];

    $data[$categoryKey] = array(
        'in'  => $xrow['qty_in'],
        'out' => $xrow['qty_out'],
		'balance' => $xrow['qty_in']-$xrow['qty_out']
		
    );
}

//---------------
 

$saledata = array();

$xresult = mysql_query("SELECT product_type, sum(qty) AS qty FROM restaurant_chalan_product  GROUP BY product_type");
while ($xrow = mysql_fetch_assoc($xresult)) {

    $product_typeKey = 'c_' . $xrow['product_type'];

    $saledata[$product_typeKey] = array(
        
		'psale' => $xrow['qty']
		
    );
}
//---------------

//---------------
 

$needdata = array();

$xresult = mysql_query("SELECT * FROM restaurant_product_details order by id asc ");
while ($xrow = mysql_fetch_assoc($xresult)) {

    $product_needKey = 'd_' . $xrow['id'];
 	
    $needdata[$product_needKey] = array(
        
		'meterial_1' => $xrow['meterial_qty1'],
		'meterial_2' => $xrow['meterial_qty2'],
		'meterial_3' => $xrow['meterial_qty3'],
		'meterial_4' => $xrow['meterial_qty4'],
		'meterial_5' => $xrow['meterial_qty5']
		
    );
}
//---------------

 

			
			$sr=0;
			$balancex=0;
			 
$result=mysql_query("SELECT rpd . * , mc1.name AS material1_name, mc2.name AS material2_name, mc3.name AS material3_name, mc4.name AS material4_name, mc5.name AS material5_name
FROM restaurant_product_details rpd
LEFT JOIN meterial_category mc1 ON rpd.meterial_1 = mc1.id
LEFT JOIN meterial_category mc2 ON rpd.meterial_2 = mc2.id
LEFT JOIN meterial_category mc3 ON rpd.meterial_3 = mc3.id
LEFT JOIN meterial_category mc4 ON rpd.meterial_4 = mc4.id
LEFT JOIN meterial_category mc5 ON rpd.meterial_5 = mc5.id
  order by rpd.shorting,rpd.sr asc");
			$row=mysql_fetch_array($result);

			
			  do{
			 
             $sr=$sr+1;    


		
  	
			
$salable=0;
$salable1=0;
$salable2=0;
$salable3=0;
$salable4=0;
$salable5=0;

if($row[meterial_1]!='0'){
	
 
$categoryKey = 'c_' . $row['meterial_1'];
$product_typeKey = 'c_' . $row['id'];
$product_needKey = 'd_' . $row['id'];


$in=$data[$categoryKey]['in'];   // category 5 IN
$out=$data[$categoryKey]['out'];  // category 5 OUT
$balancex=$data[$categoryKey]['balance'];  // balance
$sale_qty=$saledata[$product_typeKey]['psale']; // sale qty 
$meterial_1=$needdata[$product_needKey]['meterial_1'];
$salex=$sale_qty*$meterial_1;
$availbel=$balancex-$salex;
$data[$categoryKey]['balance'] = $availbel;
$salable1=round($availbel);


$data[$categoryKey]['salable'] = $salable1;

$mydata=$data[$categoryKey]['salable'];

}



			  }while($row=mysql_fetch_array($result));
			   

 	
			
			$sr=0;
			$balancex=0;
			 
			$result=mysql_query("SELECT rpd . * , mc1.name AS material1_name, mc2.name AS material2_name, mc3.name AS material3_name, mc4.name AS material4_name, mc5.name AS material5_name
FROM restaurant_product_details rpd
LEFT JOIN meterial_category mc1 ON rpd.meterial_1 = mc1.id
LEFT JOIN meterial_category mc2 ON rpd.meterial_2 = mc2.id
LEFT JOIN meterial_category mc3 ON rpd.meterial_3 = mc3.id
LEFT JOIN meterial_category mc4 ON rpd.meterial_4 = mc4.id
LEFT JOIN meterial_category mc5 ON rpd.meterial_5 = mc5.id
  order by rpd.shorting,rpd.sr asc");
			$row=mysql_fetch_array($result);

			
			  do{
			 
             $sr=$sr+1;    

?>

              <tr>
                <td width="4%" ><?php echo $sr;?></td>
                <td width="11%" ><?php  
				 
				echo $row[pname];
				?></td>
                <td width="15%" ><?php echo $row[product_item];?></td>
                <td width="16%" valign="top" ><?php 

if($row[meterial_qty1]!="0"){ echo "($row[material1_name]  - $row[meterial_qty1]) ";}
if($row[meterial_qty2]!="0"){ echo "<br /> ($row[material2_name]  - $row[meterial_qty2]) ";}
if($row[meterial_qty3]!="0"){ echo "<br /> ($row[material3_name]  - $row[meterial_qty3]) ";}
if($row[meterial_qty4]!="0"){ echo "<br /> ($row[material4_name]  - $row[meterial_qty4])  ";}
if($row[meterial_qty5]!="0"){ echo "<br /> ($row[material5_name]  - $row[meterial_qty5]) ";}

				
  	
				?></td>
                <td valign="top" ><?php echo $row[sale_rate];?></td>
                <td valign="top" >
                  <?php 
				  
$salable=0;
$salable1=0;
$salable2=0;
$salable3=0;
$salable4=0;
$salable5=0;

if($row[meterial_1]!='0'){
	
echo "$row[material1_name] - ";
$categoryKey = 'c_' . $row['meterial_1'];
$final_out=$data[$categoryKey]['salable'];
$salable1=round($final_out/$row[meterial_qty1]);

echo "Stock  ($final_out) = ";
echo " <strong>For sale  ($salable1) </strong>";
}

if($row[meterial_2]!='0'){
	
echo "<br />$row[material2_name] - ";
	
$categoryKey = 'c_' . $row['meterial_2'];
$final_out=$data[$categoryKey]['salable'];
$salable2=round($final_out/$row[meterial_qty2]);

echo "Stock  ($final_out) = ";
echo " <strong>For sale  ($salable2) </strong> ";
}


if($row[meterial_3]!='0'){
	
echo "<br />$row[material3_name] - ";
	
$categoryKey = 'c_' . $row['meterial_3'];
$final_out=$data[$categoryKey]['salable'];
$salable3=round($final_out/$row[meterial_qty3]);

echo "Stock  ($final_out) = ";
echo " <strong>For sale  ($salable3) </strong> ";
}



if($row[meterial_4]!='0'){
	
echo "<br />$row[material4_name] - ";
	
$categoryKey = 'c_' . $row['meterial_4'];
$final_out=$data[$categoryKey]['salable'];
$salable4=round($final_out/$row[meterial_qty4]);

echo "Stock  ($final_out) = ";
echo " <strong>For sale  ($salable4) </strong> ";
}


if($row[meterial_5]!='0'){
	
echo "<br />$row[material5_name] - ";
	
$categoryKey = 'c_' . $row['meterial_5'];
$final_out=$data[$categoryKey]['salable'];
$salable5=round($final_out/$row[meterial_qty5]);

echo "Stock  ($final_out) = ";
echo " <strong>For sale  ($salable5) </strong>";
}
?>
                </td>
                <td align="center"  class="link"><strong>
                  <?php 

if($row[meterial_1]!='0'){$salable=$salable1;}
if($row[meterial_2]!='0'){if($salable>$salable2){$salable=$salable2; } }
if($row[meterial_3]!='0'){if($salable>$salable3){$salable=$salable3; } }
if($row[meterial_3]!='0'){if($salable>$salable4){$salable=$salable4; } }
if($row[meterial_3]!='0'){if($salable>$salable5){$salable=$salable5; } }


    echo $salable; // 2

				

				?>
                </strong></td>
              </tr>

			  <?php 
        
     
			  }while($row=mysql_fetch_array($result));
			  ?>
                </tbody>
            </table>
          <br /></td>
        </tr>
    </table></td>
  </tr>
  <tr>
    <td><?php include("bot.php");?></td>
  </tr>
</table>
<script>
function sortTable(col) {
  let table = document.getElementById("myTable");
  let rows = Array.from(table.rows).slice(1);
  let asc = table.getAttribute("data-sort") !== "asc";

  rows.sort((a, b) => {
    let x = a.cells[col].innerText.toLowerCase();
    let y = b.cells[col].innerText.toLowerCase();
    return asc ? x.localeCompare(y, undefined, {numeric:true}) 
               : y.localeCompare(x, undefined, {numeric:true});
  });

  rows.forEach(row => table.tBodies[0].appendChild(row));
  table.setAttribute("data-sort", asc ? "asc" : "desc");
}
</script>
</body>
</html>
<?php }?>