<?php
ini_set('memory_limit', '-1');
	require_once("../include/config.php");	
	require_once("../include/startup.php");
	require_once("../include/tax_related_functions.php");
	require_once("../include/classes/alltables.class.php");
	//ini_set('error_reporting', E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
	$obj  = new Product();
	
	$data = array();
	$type = $_POST['datatype'];
if($type == '1')
{	
	$fromcompanyid 	= $_POST['fromcompanyid'];
	$name = $_POST['searchstring'];
	
	//$sqltext = "Select * from mstproducts  where isdeleted = 0 and  UPPER(itemno) LIKE '".strtoupper($name)."%' and membercompanyid = $fromcompanyid order by Product_ID ASC";
	$sqltext = "Select * from mstproducts  where isdeleted = 0 and  UPPER(itemno) LIKE '".strtoupper($name)."%'  order by Product_ID ASC";
	$row = $obj-> getProduct('',$sqltext);
	
	if(is_array($row))
	{
		$rowcount =count($row);
	}
	
	for($i=0;$i<$rowcount;$i++)
	{
		$data[] = array("Product_ID"=>$row[$i]['Product_ID'],"itemno"=>$row[$i]['itemno'],"productname"=>$row[$i]['productname'],"product_description"=>$row[$i]['product_description']);
	}	
	echo json_encode($data);
}


if($type == '2')
{	
	$itemno 	= $_POST['itemno'];
	$total_amount = $_POST['total_amount'];
	$sqltext = "Select * from mstproducts  where isdeleted = 0 and  UPPER(Product_ID) = '".strtoupper($itemno)."'  order by Product_ID ASC";
	
	$row = $obj-> getProduct('',$sqltext);
//echo json_encode($sqltext);
	$i = 0;
	$total_amount += $row[$i]['price'];
		$data[] = array("itemno"=>$row[$i]['itemno'],"productname"=>$row[$i]['productname'],"product_description"=>$row[$i]['product_description'],"hsn_code"=>$row[$i]['hsn_code'],"price"=>$row[$i]['price'],"total_amount"=>$total_amount);
	
	echo json_encode($data);
}

if($type == '3')
{	
	$tocompanyid 		= $_POST['tocompanyid'];
	$fromcompanyid 	= $_POST['fromcompanyid'];
	$name					= $_POST['searchstring'];
	
	//$sqltext = "Select * from mstproducts  where isdeleted = 0 and  UPPER(itemno) LIKE '".strtoupper($name)."%' and membercompanyid = $fromcompanyid order by Product_ID ASC";
	$sqltext = "Select * from mstproducts  where isdeleted = 0 and  UPPER(itemno) LIKE '".strtoupper($name)."%'  order by Product_ID ASC";
	$row = $obj-> getProduct('',$sqltext);
	
	if(is_array($row))
	{
		$rowcount =count($row);
	}
	
	for($i=0;$i<$rowcount;$i++)
	{
		$data[] = array("Product_ID"=>$row[$i]['Product_ID'],"itemno"=>$row[$i]['itemno'],"productname"=>$row[$i]['productname'],"product_description"=>$row[$i]['product_description']);
	}	
	echo json_encode($data);
}


if($type == '4')
{	
	$tocompanyid 		= $_POST['tocompanyid'];
	$itemno 				= $_POST['itemno'];
	$total_amount 		= $_POST['total_amount'];
	$sqltext = "Select * from mstproducts  where isdeleted = 0 and  UPPER(Product_ID) = '".strtoupper($itemno)."'  order by Product_ID desc";
	$row = $obj-> getProduct('',$sqltext);
	//echo json_encode($sqltext);
	//$sqltext_company_statecode = "Select * from user_company  where isdeleted = 0 and membercompanyid = $fromcompanyid ";
	$sqltext_company_statecode = "Select * from user_company  where isdeleted = 0  ";
	$statecode = $obj-> getProduct('',$sqltext_company_statecode);
	
	$statecode		= $statecode[0]['head_Stateid1'];
	
	if($tocompanyid > 0)
	{
		$taxtype = CheckTypeOfGST($tocompanyid,$statecode);
	}
	else 
	{
		$taxtype = 'IGST';
	}
	//echo json_encode($taxtype);
	$i = 0;
		$total = $row[$i]['price'] ;
		$productid = $row[$i]['Product_ID'];
		$taxcal 		= TaxCalculation($total,$productid,$taxtype);
		$total1		=  $taxcal['total_amount_with_tax']; 
		$subtotal   =  $taxcal['amount'];
		$cgst_amt	=  $taxcal['cgstamt']; 
		$sgst_amt	=  $taxcal['sgstamt']; 	
		$igst_amt	=  $taxcal['igstamt']; 
	
	$total_amount += $row[$i]['price'];
		$data[] = array("itemno"=>$row[$i]['itemno'],"productname"=>$row[$i]['productname'],"product_description"=>$row[$i]['product_description'],"hsn_code"=>$row[$i]['hsn_code'],"price"=>$row[$i]['price'],"total_amount"=>$total_amount,"total_amount_with_tax"=>$total1,"subtotal"=>$subtotal,"cgst_amt"=>$cgst_amt,"sgst_amt"=>$sgst_amt,"igst_amt"=>$igst_amt);
	
	echo json_encode($data);
}
?>
