<?php
error_reporting(0);
include('cocoon_config.php');
function openingBalance($function_filter, $item)
{

	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	if ($site != '')
		$filter[] = "site = '" . $site . "'";
	if ($store != '')
		$filter[] = "store = '" . $store . "'";
	if ($from != '--')
		$filter[] = "reveive_date <= '" . $from . "'";
	if ($item != '')
		$filter[] = "itemcode = '" . $item . "'";

	$filter = implode(' and ', $filter);
	// echo $sql1="Select sum(stock) as sumstock, rate from opening_stockitemwise where created_by='".$_SESSION['UserID']."' and $filter";
	$sql2 = "Select store from opening_stockitemwise where  $filter";
	$result2 = DB_query($sql2, $db);
	$myrow2 = DB_fetch_array($result2);
	$store = $myrow2['store'];
	$sql1 = "Select sum(stock) as sumstock, rate from opening_stockitemwise where  store = '" . $store . "' and $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['sumstock'];
}



////1. MRN/////
function receivedMRN($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	if ($site != '')
		$filter[] = "addmrn.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "addmrn.StoreCode = '" . $store . "'";
	if ($from != '--')
		$filter[] = "MRNDate <= '" . $from . "'";

	if ($item != '')
		$filter[] = "addmrnqty.mst_itemid = '" . $item . "'";

	$filter = implode(' and ', $filter);
	$sql1 = "Select SUM(addmrnqty.reciveqty) from addmrn left join addmrnqty 
on(addmrn.id=addmrnqty.issue_id) where Cancel_Status='N' and $filter ";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(addmrnqty.reciveqty)'];
}


////1. Issue Slip/////
function issueSlip($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	if ($site != '')
		$filter[] = "addissue.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "addissue.mst_storeid = '" . $store . "'";
	if ($from != '--')
		$filter[] = "issuedate <= '" . $from . "'";

	if ($item != '')
		$filter[] = "issueqty.mst_itemid = '" . $item . "'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(issueqty.issueqty) from addissue left join 
issueqty on(addissue.id=issueqty.issue_id) where $filter and cancel_issue=0";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);

	return $myrow1['SUM(issueqty.issueqty)'];
}

// Material Conversion received
function MaterialConReceived($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];

	if ($site != '')
		$filter[] = "mat_conversion.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "mat_conversion.store = '" . $store . "'";
	if ($from != '--')
		$filter[] = "mat_conversion.mat_con_date <= '" . $from . "'";

	$filter[] = "mat_conversion_details.trnx_type = 'received'";

	if ($item != '')
		$filter[] = "mat_conversion_details.item_id = '" . $item . "'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(mat_conversion_details.pre_qty) from mat_conversion left join 
mat_conversion_details on(mat_conversion.id=mat_conversion_details.parent_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);

	return $myrow1['SUM(mat_conversion_details.pre_qty)'];
}

// Material Conversion issue
function MaterialConIssue($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];

	if ($site != '')
		$filter[] = "mat_conversion.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "mat_conversion.store = '" . $store . "'";
	if ($from != '--')
		$filter[] = "mat_conversion.mat_con_date <= '" . $from . "'";

	$filter[] = "mat_conversion_details.trnx_type = 'issue'";

	if ($item != '')
		$filter[] = "mat_conversion_details.item_id = '" . $item . "'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(mat_conversion_details.pre_qty) from mat_conversion left join 
mat_conversion_details on(mat_conversion.id=mat_conversion_details.parent_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);

	return $myrow1['SUM(mat_conversion_details.pre_qty)'];
}




////2. Material return/////
function receivedReturn($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];


	if ($site != '')
		$filter[] = "material_return.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "material_return.store = '" . $store . "'";
	if ($from != '--')
		$filter[] = "material_return.returndate  <= '" . $from . "'";
	if ($item != '')
		$filter[] = "material_return_details.itemid = '" . $item . "'";

	$filter = implode(' and ', $filter);
	$sql1 = "Select SUM(material_return_details.receiveqty) 
from material_return left join material_return_details 
on(material_return.id=material_return_details.parentId) where $filter";


	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(material_return_details.receiveqty)'];
}


////////////site to site receive//////
function receivedSiteToSite($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	$to = $function_filter[3];

	if ($site != '')
		$filter[] = "transfer_receipt.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "transfer_receipt.store_to = '" . $store . "'";
	if ($from != '--')
		$filter[] = "transfer_receipt.received_date <= '" . $from . "'";
	if ($item != '')
		$filter[] = "transfer_receipt_details.itemid = '" . $item . "'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(transfer_receipt_details.reciveqty) 
from transfer_receipt left join transfer_receipt_details 
on(transfer_receipt.id=transfer_receipt_details.transfer_receipt_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(transfer_receipt_details.reciveqty)'];
}


////2. Site to site transfer issue/////
function issueSiteToSite($function_filter, $item)
{
	global $db;

	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	$to = $function_filter[3];

	if ($site != '')
		$filter[] = "transfer_issue.sitecode = '" . $site . "'";
	if ($store != '')
		$filter[] = "transfer_issue.tostore = '" . $store . "'";
	if ($from != '--')
		$filter[] = "transfer_issue.issuedate <= '" . $from . "'";
	if ($item != '')
		$filter[] = "transfer_issue_details.itemid = '" . $item . "'";

	$filter = implode(' and ', $filter);
	$sql1 = "Select SUM(transfer_issue_details.issueqty) 
from transfer_issue left join transfer_issue_details 
on(transfer_issue.id=transfer_issue_details.transfer_issue_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	$return_array = $myrow1['SUM(transfer_issue_details.issueqty)'];
	//$avg_rate=$myrow1['SUM(transfer_issue_details.rate)'];
	//$return_array[]=$avg_rate;
	return $return_array;
}
////3. Material return to vendor///
function issueMatarialReturn($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	$to = $function_filter[3];

	if ($site != '')
		$filter[] = "inv_material_return.sitecode = '" . $site . "'";
	if ($store != '')
		$filter[] = "inv_material_return.store = '" . $store . "'";
	if ($from != '--')
		$filter[] = "inv_material_return.return_date <= '" . $from . "'";
	if ($item != '')
		$filter[] = "inv_material_return_details.itemid = '" . $item . "'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(inv_material_return_details.ReturnQty) 
from inv_material_return left join inv_material_return_details 
on(inv_material_return.id=inv_material_return_details.parentId) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(inv_material_return_details.ReturnQty)'];
}

function Rgpchallan($function_filter, $item)
{

	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];


	if ($site != '')
		$filter[] = "rgp_tab.branch = '" . $site . "'";
	if ($store != '')
		$filter[] = "rgp_tab.store = '" . $store . "'";
	if ($from != '--' && $to != '--')
		$filter[] = "rgp_tab.date <= '" . $from . "'";
	if ($item != '')
		$filter[] = "rgp_tab_details.mst_itemid = '" . $item . "'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(rgp_tab_details.qty) 
from rgp_tab left join rgp_tab_details 
on(rgp_tab.id=rgp_tab_details.parent_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(rgp_tab_details.qty)'];
}
function Rgpreturn($function_filter, $item)
{

	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];

	if ($site != '')
		$filter[] = "rgp_return.branch = '" . $site . "'";
	if ($store != '')
		$filter[] = "rgp_return.store = '" . $store . "'";
	if ($from != '--' && $to != '--')
		$filter[] = "rgp_return.return_date <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "rgp_return_details.item_id = '" . $item . "'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(rgp_return_details.qty) 
from rgp_return left join rgp_return_details 
on(rgp_return.id=rgp_return_details.parent_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(rgp_return_details.qty)'];
}

function Nrgp($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];


	if ($site != '')
		$filter[] = "nrgp_challan.branch = '" . $site . "'";
	if ($store != '')
		$filter[] = "nrgp_challan.store = '" . $store . "'";
	if ($from != '--' && $to != '--')
		$filter[] = "nrgp_challan.date <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "nrgp_challan_details.item_code = '" . $item . "'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(nrgp_challan_details.qty) 
from nrgp_challan left join nrgp_challan_details 
on(nrgp_challan.id=nrgp_challan_details.parant_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(nrgp_challan_details.qty)'];
}

function FinishedMR($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];

	if ($site != '')
		$filter[] = "finished_recieved.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "finished_recieved.store = '" . $store . "'";
	if ($from != '--')
		$filter[] = "finished_recieved.recieved_date <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "finished_recieved_details.mst_itemid = '" . $item . "'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(finished_recieved_details.reciveqty) 
from finished_recieved left join finished_recieved_details 
on(finished_recieved.id=finished_recieved_details.finished_recieved_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(finished_recieved_details.reciveqty)'];
}
function dispatchsaleorder($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	if ($site != '')
		$filter[] = "dbl_dealer_dispatch.dealer = '" . $site . "'";
	if ($store != '')
		$filter[] = "dbl_dealer_dispatch_details.dblstore = '" . $store . "'";
	if ($from != '--' && $to != '--')
		$filter[] = "dbl_dealer_dispatch.date <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "dbl_dealer_dispatch_details.mst_itemid = '" . $item . "'";
	$filter = implode(' and ', $filter);
	$sql1 = "Select SUM(dbl_dealer_dispatch_details.dispatch_qty) 
from dbl_dealer_dispatch left join dbl_dealer_dispatch_details 
on(dbl_dealer_dispatch.id=dbl_dealer_dispatch_details.parentid) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(dbl_dealer_dispatch_details.dispatch_qty)'];
}
function PhysicalStockPlus($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	$to = $function_filter[3];

	if ($site != '')
		$filter[] = "physical_stock.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "physical_stock.store = '" . $store . "'";
	if ($from != '--')
		$filter[] = "physical_stock.date1 <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "physical_stock_details.mstitemid = '" . $item . "'";
	$filter[] = "physical_stock_details.adjustment_type = 'plus'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(physical_stock_details.adjustment_qty) 
from physical_stock left join physical_stock_details 
on(physical_stock.id=physical_stock_details.physical_stock_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(physical_stock_details.adjustment_qty)'];
}
function PhysicalStockMinus($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	$to = $function_filter[3];

	if ($site != '')
		$filter[] = "physical_stock.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "physical_stock.store = '" . $store . "'";
	if ($from != '--')
		$filter[] = "physical_stock.date1 <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "physical_stock_details.mstitemid = '" . $item . "'";
	$filter[] = "physical_stock_details.adjustment_type = 'minus'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(physical_stock_details.adjustment_qty) 
from physical_stock left join physical_stock_details 
on(physical_stock.id=physical_stock_details.physical_stock_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(physical_stock_details.adjustment_qty)'];
}

function dispatchIssue($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	$to = $function_filter[3];

	if ($site != '')
		$filter[] = "dispatch.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "dispatch.store = '" . $store . "'";
	if ($from != '--')
		$filter[] = "dispatch.dispatchDate <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "dispatch_details.item = '" . $item . "'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(dispatch_details.dispatchQty) 
from dispatch left join dispatch_details 
on(dispatch.id=dispatch_details.parentId) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(dispatch_details.dispatchQty)'];
}

function jobWorkChallan57Issue($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	$to = $function_filter[3];

	if ($site != '')
		$filter[] = "job_work_issue.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "job_work_issue.store = '" . $store . "'";
	if ($from != '--')
		$filter[] = "job_work_issue.jobDate <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "job_work_issue_details1.mst_itemid = '" . $item . "'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(job_work_issue_details1.qty) 
from job_work_issue left join job_work_issue_details1 
on(job_work_issue.id=job_work_issue_details1.parent_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(job_work_issue_details1.qty)'];
}

function jobWorkChallan57Received($function_filter, $item)
{ // It is working for job work return challan

	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	$to = $function_filter[3];

	if ($site != '')
		$filter[] = "job_work_return.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "job_work_return.store = '" . $store . "'";
	if ($from != '--')
		$filter[] = "job_work_return.returnDate <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "job_return_details.mst_itemId = '" . $item . "'";

	$filter = implode(' and ', $filter);

	$sql1 = "Select SUM(job_return_details.receivedQty) 
from job_work_return left join job_return_details 
on(job_work_return.id=job_return_details.masterId) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(job_return_details.receivedQty)'];
}

function dispatchreturnsaleorder($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	if ($site != '')
		$filter[] = "dbl_sale_return.dealer = '" . $site . "'";
	if ($store != '')
		$filter[] = "dbl_sale_return_details.dblstore = '" . $store . "'";
	if ($from != '--' && $to != '--')
		$filter[] = "dbl_sale_return.form_date <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "dbl_sale_return_details.mst_itemid = '" . $item . "'";
	$filter = implode(' and ', $filter);
	$sql1 = "Select SUM(dbl_sale_return_details.return_qty) 
from dbl_sale_return left join dbl_sale_return_details 
on(dbl_sale_return.id=dbl_sale_return_details.parant_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(dbl_sale_return_details.return_qty)'];
}

function invoice($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	if ($site != '')
		$filter[] = "invoice.branch_name = '" . $site . "'";
	if ($store != '')
		$filter[] = "invoice.store = '" . $store . "'";
	if ($from != '--' && $to != '--')
		$filter[] = "invoice.invoiceDate <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "invoice_details.ItemCode = '" . $item . "'";
	$filter = implode(' and ', $filter);
	$sql1 = "Select SUM(invoice_details.Quantity) 
from invoice left join invoice_details 
on(invoice.invoice_id=invoice_details.invoiceid) where  $filter and invoice_type='Direct'";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(invoice_details.Quantity)'];
}
/*  Dispatch */
function dispatch($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	if ($site != '')
		$filter[] = "dispatch.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "dispatch.store = '" . $store . "'";
	if ($from != '--')
		$filter[] = "dispatch.dispatchDate <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "dispatch_details.item = '" . $item . "'";
	$filter = implode(' and ', $filter);
	$sql1 = "Select SUM(dispatch_details.dispatchQty) 
from dispatch left join dispatch_details 
on(dispatch.id=dispatch_details.parentId) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(dispatch_details.dispatchQty)'];
}




function directPurchase($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	if ($site != '')
		$filter[] = "purchase_billbook.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "purchase_billbook.store = '" . $store . "'";
	if ($from != '--' && $to != '--')
		$filter[] = "purchase_billbook.bill_book_date <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "purchase_bill_details.mst_productid = '" . $item . "'";
	$filter = implode(' and ', $filter);
	$sql1 = "Select SUM(purchase_bill_details.quantity) 
from purchase_billbook left join purchase_bill_details 
on(purchase_billbook.id=purchase_bill_details.purch_billbookid) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(purchase_bill_details.quantity)'];
}

function saleReturn($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	if ($site != '')
		$filter[] = "inv_sale_agaist.branch = '" . $site . "'";
	if ($store != '')
		$filter[] = "inv_sale_agaist.store = '" . $store . "'";
	if ($from != '--' && $to != '--')
		$filter[] = "inv_sale_agaist.return_date <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "inv_sale_agaist_details.item_id = '" . $item . "'";
	$filter = implode(' and ', $filter);
	$sql1 = "Select SUM(inv_sale_agaist_details.accepted_qty) 
from inv_sale_agaist left join inv_sale_agaist_details 
on(inv_sale_agaist.id=inv_sale_agaist_details.parent_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(inv_sale_agaist_details.accepted_qty)'];
}


function drugissue($function_filter, $item)
{
	global $db;
	$sqlitem = DB_fetch_array(DB_query("select StockID from item_master where id='" . $item . "'", $db));


	$store = $function_filter[1];
	$from = $function_filter[2];
	$to = $function_filter[3];
	$sqlstore = DB_query("select StoreCode from storetype where id in('" . $store . "')", $db);
	while ($rowdrg = DB_fetch_array($sqlstore)) {
		$storedata[] = $rowdrg['StoreCode'];
	}
	if (!empty($storedata))
		$stdata = implode("','", $storedata);
	if ($stdata != '')
		$filter[] = "drugissues.store_code in('" . $stdata . "')";
	if ($from != '--')
		$filter[] = "date(drugissues.issue_date) <= '" . $from . "'";
	if ($item != '')
		$filter[] = "drugissue_batch_issues.item_code = '" . $sqlitem['StockID'] . "'";

	$filter = implode(' and ', $filter);


	//$sql1="Select SUM(finished_recieved_details.reciveqty) from finished_recieved left join finished_recieved_details 	on(finished_recieved.id=finished_recieved_details.finished_recieved_id) where $filter";

	$sql1 = "Select SUM(drugissue_batch_issues.qty) 
					from drugissues left join drugissue_batch_issues 
				on(drugissues.id=drugissue_batch_issues.drugissue_id) where $filter";


	$result1 = mysqli_query($db, $sql1);
	$myrow1 = mysqli_fetch_array($result1);

	return $myrow1['SUM(drugissue_batch_issues.qty)'];
}

function drugReversal($function_filter, $item)
{
	global $db;
	$sqlitem = DB_fetch_array(DB_query("select StockID from item_master where id='" . $item . "'", $db));


	$store = $function_filter[1];
	$from = $function_filter[2];
	$to = $function_filter[3];
	$sqlstore = DB_query("select StoreCode from storetype where id in('" . $store . "')", $db);
	while ($rowdrg = DB_fetch_array($sqlstore)) {
		$storedata[] = $rowdrg['StoreCode'];
	}
	if (!empty($storedata))
		$stdata = implode("','", $storedata);
	if ($stdata != '')
		$filter[] = "vl.store_code in('" . $stdata . "')";
	if ($from != '--')
		$filter[] = "DATE(vr.reversal_date) between '" . $from . "' and '" . $to . "'";
	if ($item != '')
		$filter[] = "vl.item_code = '" . $sqlitem['StockID'] . "'";
	$filter = implode(' and ', $filter);

	$sql1 = "SELECT SUM(vl.quantity)  AS qty
FROM `voucherlists` AS vl
LEFT JOIN `voucherreversals` AS vr ON(vl.voucherreversal_id=vr.id) 
WHERE $filter";


	$result1 = mysqli_query($db, $sql1);
	$myrow1 = mysqli_fetch_array($result1);

	return $myrow1['qty'];
}


function storereceipt($function_filter, $item)
{
	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	if ($site != '')
		$filter[] = "mrs_receipt.site = '" . $site . "'";
	if ($store != '')
		$filter[] = "mrs_receipt.store_to = '" . $store . "'";
	if ($from != '--' && $to != '--')
		$filter[] = "mrs_receipt.receipt_date <= '" . $from . "' ";
	if ($item != '')
		$filter[] = "mrs_receipt_details.itemid = '" . $item . "'";
	$filter = implode(' and ', $filter);
	$sql1 = "Select SUM(mrs_receipt_details.issueqty) 
from mrs_receipt left join mrs_receipt_details 
on(mrs_receipt.id=mrs_receipt_details.mrs_receipt_id) where $filter";
	$result1 = DB_query($sql1, $db);
	$myrow1 = DB_fetch_array($result1);
	return $myrow1['SUM(mrs_receipt_details.issueqty)'];
}



function openingBalancepr($function_filter, $item)
{

	global $db;
	$site = $function_filter[0];
	$store = $function_filter[1];
	$from = $function_filter[2];
	if ($site != '')
		$filter[] = "site = '" . $site . "'";
	if ($store != '')
		$filter[] = "store in( " . $store . ")";
	if ($from != '--')
		$filter[] = "reveive_date <= '" . $from . "'";
	if ($item != '')
		$filter[] = "itemcode = '" . $item . "'";

	$filter = implode(' and ', $filter);
	// echo $sql1="Select sum(stock) as sumstock, rate from opening_stockitemwise where created_by='".$_SESSION['UserID']."' and $filter";
	/*$sql2="Select store from opening_stockitemwise where  $filter";
$result2=DB_query($sql2,$db);
$myrow2=DB_fetch_array($result2);
$store = $myrow2['store']; */
	if ($store != '') {
		$sql1 = "Select sum(stock) as sumstock, rate from opening_stockitemwise where  store in(" . $store . ") and $filter";
		$result1 = DB_query($sql1, $db);
		$myrow1 = DB_fetch_array($result1);
		return $myrow1['sumstock'];
	}
}
