<?php  
session_start(); 
	include("connect.php");	
	include("connectp.php");
	include("removespecial.php");
	//header("Access-Control-Allow-Origin: *");
		$par1=$_SESSION['itemid'];
	
	$q = $db7->prepare("select * from res_item_detail where itemid =:p1 order by itemdid desc");
	$q->bindValue(':p1', "$par1");
	//$q->bindValue(':p2',  "$par2");
	$q->execute();
	
	
	
	
	$response = array();
	$posts = array();
	
	$result="0";
	
	if ($q->rowCount() > 0)
	{
		$i=1;
		while($i<=$q->rowCount())
		{
			$rt = $q->fetch(PDO::FETCH_ASSOC);
			
			 	$itemdid=$rt['itemdid'];
				
				
				
				$itransactiontype=$rt['itransactiontype'];
				$itemtrandate=date('d/m/Y', strtotime($rt['itemtrandate']));
				$itemtranmid=$rt['itemtranmid'];
				$grnfordisplay="";
				$invoicefordisplay="";
				if($itemtranmid!=0)
				{
					if($itransactiontype=='GRN In')
					{
						
						$grnfordisplay='';
				
						$eve1 = "select * from res_grn_master where grnmid ='$itemtranmid'";
			
						$re1 = mysqli_query($conn, $eve1);
																			
						if(mysqli_num_rows($re1) > 0)

						{											
																				
						while($rt1 = mysqli_fetch_assoc($re1))
						{
							$grnfordisplay=$rt1['grnfordisplay'];
						
						}
																				 
						}
							$grnfordisplay=
							"<div style='display: flex; flex-direction: row;'><form action='grndetail.php' method='post' style='flex: 1; margin-right: 2px;'><button name='grnmid' type='submit' value='$itemtranmid'>$grnfordisplay</button></form>";
					}
					else if($itransactiontype=='Invoice Out')
					{

						$invoicefordisplay='';
				
						$eve1 = "select * from res_invoice_master where invoicemid ='$itemtranmid'";
			
						$re1 = mysqli_query($conn, $eve1);
																			
						if(mysqli_num_rows($re1) > 0)

						{											
																				
						while($rt1 = mysqli_fetch_assoc($re1))
						{
							$invoicefordisplay=$rt1['invoicefordisplay'];
						
						}
																				 
						}
						
								$invoicefordisplay=
								"<div style='display: flex; flex-direction: row;'><form action='invoicestatus.php' method='post' style='flex: 1; margin-right: 2px;'><button name='invoicemid' type='submit' value='$itemtranmid'>$invoicefordisplay</button></form>";
					}
					
				}
				$itemtrandid=$rt['itemtrandid'];
				$iinouttype=$rt['iinouttype'];
				$iinqty=$rt['iinqtyactual'];
				$inunit	=$rt['inunitactual'];
				$ioutqty=$rt['ioutqtyactual'];
				$ioutunit=$rt['ioutunitactual'];
				$itransremarks=removespecialchar($rt['itransremarks']);
				$ilateststockback=$rt['ilateststockback'];
				
				$posts[] = array('srno'=> $i++, 'itemdid'=> $itemdid,'grnfordisplay'=> $grnfordisplay,'invoicefordisplay'=> $invoicefordisplay,'itransactiontype'=> $itransactiontype,'itemtrandate'=> $itemtrandate,'itemtranmid'=> $itemtranmid,'itemtrandid'=> $itemtrandid,'iinouttype'=> $iinouttype,'iinqty'=> $iinqty,'inunit'=> $inunit, 'ioutqty'=> $ioutqty, 'ioutunit'=> $ioutunit,'itransremarks'=> $itransremarks,'ilateststockback'=> $ilateststockback);
		}
		 
		$response['posts'] = $posts;
		echo stripslashes(json_encode( array('data' => $posts)));
		//mysqli_close($conn);
		//return;
	}
	
   else
	{
		$result="0";
		$msg="Invalid Error";
		
		$posts[] = array('p1'=> $result,'p2'=> $msg);
	
		$response['posts'] = $posts;
		echo stripslashes(json_encode( array('item' => $posts)));
	}
   
	mysqli_close($conn);	
?>