<?php session_name('CAKEPHP'); session_start();
$PageSecurity = 4;
include('includes/DefinePOClass.php');
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/DefineSuppTransClass.php');
include('includes/PaginationClass.php');
include('cocoon_config.php');
include('includes/function_lib.php');
$table='opening_stockitemwise';
if($_REQUEST['checkbatch']=='yes')
{
	$query= DB_query($str="SELECT * FROM opening_stockitemwise WHERE site ='".$_REQUEST['site']."' 
		AND store='".$_REQUEST['store']."' AND itemcode='".$_REQUEST['item']."' 
		and batch='".$_REQUEST['batch']."' ",$db);
	echo $str;
    if(db_num_rows($query)>0)
    {
    	echo "0";

    }
    else
    {
    	echo "1";
    }
    exit;
}

if($_REQUEST['checkstock']=='yes')
{
	$query= DB_query($str="SELECT * FROM opening_stockitemwise WHERE site ='".$_REQUEST['site']."' 
		AND store='".$_REQUEST['store']."' AND itemcode='".$_REQUEST['item']."' 
		 ",$db);
	
    if(db_num_rows($query)>0)
    {
    	echo "0";
    	return false;
    }
    else
    {
    	echo "1";
    	return false;
    }
    exit;
}
if($_REQUEST['additem']=='yes')
{
	if($_REQUEST['batch']==""){
		$_REQUEST['batch']="N/A";
	}
   date_default_timezone_set('Asia/Calcutta');
   $SQL_item=DB_fetch_array(DB_query("SELECT * from item_master where id='".$_REQUEST['item']."'",$db ));
   $query= DB_query("SELECT * FROM opening_stockitemwise WHERE site ='".$_REQUEST['site']."' AND store='".$_REQUEST['store']."' AND itemcode='".$_REQUEST['item']."' and batch='".$_REQUEST['batch']."' ",$db);
    if(db_num_rows($query)>0)
    {
    	 $SQL_item['item_name'];
    	 echo "1";

    	 exit;
    }
	$data['site']=$_REQUEST['site'];
	$data['store']=$_REQUEST['store'];
	$reveive_date = $_REQUEST['reveive_date'];
	$data['reveive_date']= FormatDateForSQL($_REQUEST['reveive_date'])." ".date('H:i:s', time());
	$data['CompanyId']=$_SESSION['ACGCompanyId'];
	$data['itemcode']=$_REQUEST['item'];
	$data['itemdesc']=$SQL_item['item_name'];
	$data['stock']=$_REQUEST['stock'];
	$data['rate']=$_REQUEST['rate'];
	$data['batch']=$_REQUEST['batch'];
	$data['expiry']=FormatDateForSQL($_REQUEST['expiry']);
	$data['date_of_mfg']=FormatDateForSQL($_REQUEST['mfg']);
	$data['amount']=$_REQUEST['amount'];
	$data['trasaction']='2017-04-01 11:20:33';
	$data['created_by']=$_SESSION['UserID'];
	$data['created_date']='2017-04-01';
 	$insert=$s->RecordInsert($table, $data);
	$Lastreq_id=DB_Last_Insert_ID($db,'opening_stockitemwise',$ErrMsg);
	$query = "select * from item_master where id ='".$_REQUEST['item_id']."'";
	$result = DB_query($query,$db);
	$items = DB_fetch_assoc($result,$db);	
	$data1['site'] = $_REQUEST['site'];
	$data1['inwardqty_secondary'] = $items['conv_factor'] * $_REQUEST['stock'];
	$data1['primary_unit'] = $items['mst_unitid'];
	$data1['secondary_unit'] = $items['conv_unit'];
	$data1['store'] = $_REQUEST['store'];  
	$data1['batch_no']=$_REQUEST['batch']; 
	$data1['type ']='OP'; 
	$data1['against_voucher_main_id']  =$Lastreq_id;
	$data1['date_of_expiry']=FormatDateForSQL($_REQUEST['expiry']);
	$data1['inwardqty_primary']=$_REQUEST['stock'];	
	$data1['inward_rate']=$_REQUEST['rate'];
	$data1['inwared_value']=$_REQUEST['amount'];
	$data1['mst_itemid']=$_REQUEST['item_id'];
	$data1['pending_qty']=$_REQUEST['stock'];
	$data1['date_of_mfg']=FormatDateForSQL($_REQUEST['mfg']);
	$data1['trasaction']='2017-04-01 11:20:33';							
	$x=$s->RecordInsert('stock_valuation_tbl',$data1);

	exit;
}



