<?php
include '../../db_connect.php';

$book_type=$_POST['type'];

if($book_type=='pendingpackinglist')
{       
$sql="SELECT pkgdet.packingid AS pkgid, pkgdet.prodcode AS prdcode, (qty - salesissued) AS  'pending', buyercode AS bycode, DATE_FORMAT( packingdate,  '%d/%m/%Y' ) AS packingdate, 'PKL' as frm  FROM  `preclistregdet` AS pkgdet INNER JOIN preclistreg AS spkgreg ON pkgdet.packingid = spkgreg.packingid WHERE (qty - salesissued) >0 AND buyercode =  '$_POST[buyercode]' 
UNION ALL 
SELECT orddet.orderid AS pkgid, orddet.prodcode AS prdcode, (qty - salesissued) AS  'pending', buyercode AS bycode, DATE_FORMAT( orderdate,  '%d/%m/%Y' ) AS packingdate, 'ORD' as frm 
FROM  `porderregdet` AS orddet INNER JOIN porderreg AS sordreg ON orddet.orderid = sordreg.orderid WHERE (qty - salesissued) >0  AND buyercode =  '$_POST[buyercode]' ";
        
	$items = mysqli_query($con,$sql);
	//if result set is not empty
	if(mysqli_num_rows($items)!=0)
	{
		echo "<table width='100%' cellspacing='0' border='0'><thead><th align='center'>PACKING ID</th><th align='center'>PROD. CODE</th><th align='center'>PENDING QTY</th><th align='center'>FRM</th></thead>";
		echo "<tbody>";
		while($row = mysqli_fetch_array($items)) {
				
				echo "<tr><td align='center' class='packingid'>$row[pkgid]</td><td align='center' class='prodcode'>$row[prdcode]</td><td align='center' class='qty'>$row[pending]</td><td align='center' class='frm'>$row[frm]</td></tr>";
		}
	
		echo "</tbody>";
	}
	else
	{
		echo 0;
	}

}


else if($book_type=='pendingorderdetails')
{       
	$sql="SELECT orddet.orderid, prodcode,( qty-salesissued)  AS  'pending', buyercode, DATE_FORMAT( orderdate,  '%d/%m/%Y' ) AS orderdate FROM  `preclistregdet` AS orddet
INNER JOIN porderreg AS ord ON orddet.orderid = ord.orderid  where ( qty-salesissued) > 0 and prodcode = '$_POST[prodcode]' and agentcode = '$_POST[agent]' order by orderdate desc ";
        
	$items = mysqli_query($con,$sql);
	//if result set is not empty
	if(mysqli_num_rows($items)!=0)
	{
		echo "<table width='100%' cellspacing='0' border='0'><thead><th align='center'>ORDER ID</th><th align='center'>PROD. CODE</th><th align='center'>PENDING QTY</th><th align='center'>ISSUED QTY</th><th align='center'>BUYER</th><th align='center'>ORDERDATE</th>";
		echo "<tbody>";
		while($row = mysqli_fetch_array($items)) {
				
				echo "<tr><td align='center' ><textarea class='orderidissued' name='orderidissued' readonly='readonly'>$row[orderid]</textarea></td><td align='center'>$row[prodcode]</td><td align='center'>$row[pending]</td><td align='center'><textarea name ='issuedqty' class='issuedqty' style='width=50%'>$row[pending]</textarea></td><td align='center'>$row[buyercode]</td><td align='center'>$row[orderdate]</td></tr>";
		}
	
		echo "</tbody>";
	}
	else
	{
		echo 0;
	}

}
mysqli_close($con);


?>