<?php
include_once('database_connection.php');


if(isset($_GET['action']))
{

	if($_GET['action']=="stocksummary")
	{
		$data=array();

		if(isset($_GET['CompanyID']) && $_GET['CompanyID']>0)
		{
			$CompanyID="godownid=".$_GET['CompanyID']." and ";
		}
		else
		{
			$CompanyID='';
		}

		if(isset($_GET['item']) && $_GET['item']>0)
		{
			$item="parent.ITEMID=".$_GET['item']." and ";
		}
		else
		{
			$item='';
		}

		if((isset($_GET['startdate']) && $_GET['startdate']!="") && (isset($_GET['enddate']) && $_GET['enddate']!=""))
		{
			$date="InvoiceDate between '".$_GET['startdate']."' and '".$_GET['enddate']."' and ";
		}
		else
		{
			$date='';
		}	
		$searchbox=$CompanyID."".$item."".$date;
		 $sql="   select ITEMID,Item,Category,partnumber,specs,saleprice,barcode,costprice,isNull((select sum(qty) from stocksummary where ItemID=parent.ItemID and InvoiceType=2 and    godownid=parent.godownid),0 ) as recstock,(select sum(qty) from stocksummary where ItemID=parent.ItemID and InvoiceType=4 and  godownid=parent.godownid) as purchase
   ,isnull((select sum(qty) from stocksummary where ItemID=parent.ItemID and InvoiceType=3 and    godownid=parent.godownid),0) as purchaseRet,
   isnull((select sum(qty) from stocksummary where ItemID=parent.ItemID and (InvoiceType=5 or Invoicetype=8) and godownid=parent.godownid),0) as Sale,
   isNull((select sum(qty) from stocksummary where ItemID=parent.ItemID and InvoiceType=7  and  godownid=parent.godownid),0) as DepartISSUE,isNull(
   (select sum(qty) from stocksummary where ItemID=parent.ItemID and InvoiceType=6  and godownid=parent.godownid),0) as StoreIssue,  company,
isNull((select sum(qty) from InventoryOpeningBalance where ID=parent.ItemID  and godownid=parent.godownid),0) as Opening ,godownid ,isNull(
   (select sum(qty) from stocksummary where ItemID=parent.ItemID and InvoiceType=17  and godownid=parent.godownid),0) as DepartRet,isNull(
   (select sum(qty) from stocksummary where ItemID=parent.ItemID and InvoiceType=16  and godownid=parent.godownid),0) as StoreRet,0 as SalesRet
   from StockSummary as parent
   where $searchbox 1=1
   group by ITEMID,Item,Category,partnumber,specs,saleprice,barcode,costprice,godownid,company";
   	$result=odbc_exec($connection, $sql);
		while($row=odbc_fetch_array($result))
		{

		foreach($row as $key=>$value)
			$row[$key]= utf8_encode($value);	

		$data[]=$row;	
		}
		//$data['description']=$sum;
	//	print_r($data);
		echo json_encode($data);

	}


}



?>