<?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/header.inc');
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['site']=$_REQUEST['site'];
	//$data['reveive_date']=FormatDateForSQL($_REQUEST['reveive_date'][$i]);
	$data['reveive_date'] = FormatDateForSQL($_REQUEST['reveive_date']) . " " . date('H:i:s', time());
	$data['CompanyId'] = $_SESSION['ACGCompanyId'];
	//$data['store']=$_REQUEST['store'];
	//$data['mstid']=$_REQUEST['itemcode'];
	$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'] = date('Y-m-d h:i:s');
	$data['created_by'] = $_SESSION['UserID'];
	$data['created_date'] = date('Y-m-d');
	//print_r($data);
	//$data['user_type']=$_REQUEST['type'];
	$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'] = FormatDateForSQL($_REQUEST['reveive_date']) . " " . date('H:i:s', time());
	/*   
								//insert record into stock aging

							$data2['mst_itemid']=$items['mst_unitid'];
							$data2['inward_qty']=$_REQUEST['stock'];
							$data2['voucher_type']='op';
							$data2['against_voucher_id']=$Lastreq_id;
							$data2['transaction_time']=FormatDateForSQL($_REQUEST['reveive_date'])." ".date('H:i:s', time());
							$data2['created_by']=$_SESSION['UserID'];
							$data2['created_date']=date('Y-m-d');
							$data2['company_id']=$_SESSION['ACGCompanyId'];
						  $y=$s->RecordInsert('stock_aging',$data2);


							*/
	//  print_r($data1);
	// exit;
	$x = $s->RecordInsert('stock_valuation_tbl', $data1);
	/* hmis update start  */
	/*"SELECT * FROM opening_stockitemwise WHERE site ='".$_REQUEST['site']."' AND store='".$_REQUEST['store']."' AND itemcode='".$_REQUEST['item']."' and batch='".$_REQUEST['batch']."' "*/
	$queryItem = "select * from item_master where id ='" . $_REQUEST['item_id'] . "'";
	$resultItem = DB_query($queryItem, $db);
	$itemsItem = DB_fetch_assoc($resultItem, $db);
	$queryStore = "select * from storetype where id ='" . $_REQUEST['store'] . "'";
	$resultStore = DB_query($queryStore, $db);
	$itemsStore = DB_fetch_assoc($resultStore, $db);
	$queryMajorCode = "select * from stockmaincategory where id ='" . $itemsItem['CategoryID'] . "'";
	$resultMajorCode = DB_query($queryMajorCode, $db);
	$MajorCodeData = DB_fetch_assoc($resultMajorCode, $db);
	$srtoreItemSql = "select * from storeitems where store_code='" . $itemsStore['StoreCode'] . "' and item='" . $itemsItem['StockID'] . "' and major_code='" . $MajorCodeData['stockmaincategoryid'] . "'";
	//$srtoreItemSql="select * from storeitems where store_code='STRPHY' and item='".$itemsItem['StockID']."'";
	$srtoreItemQry = mysqli_query($link1, $srtoreItemSql);
	$srtoreItemRow = mysqli_fetch_array($srtoreItemQry);
	if (!empty($srtoreItemRow)) {
		$ItemId = $srtoreItemRow['id'];
		$srtoreItemQtySql = "UPDATE storeitems SET stock_level=stock_level+" . $_REQUEST['stock'] . " where id= $ItemId";
		$srtoreItemQtyQry = mysqli_query($link1, $srtoreItemQtySql);
		$srtorebatchItemSql = "select * from batchdetails where store_code='" . $itemsStore['StoreCode'] . "' and item_code='" . $itemsItem['StockID'] . "' and batch_no='" . $_REQUEST['batch'] . "'";
		$srtorebatchItemSql = mysqli_query($link1, $srtorebatchItemSql);
		$srtoreBatchItemRow = mysqli_fetch_array($srtorebatchItemSql);
		if (empty($srtoreBatchItemRow)) {
			$srtoreItemQtySql = "insert into batchdetails set qty='" . $_REQUEST['stock'] . "', uom='" . $_REQUEST['unit'] . "', mrp='" . $_REQUEST['rate'] . "', store_code='" . $itemsStore['StoreCode'] . "', item_code='" . $itemsItem['StockID'] . "', item_desc='" . $itemsItem['item_desc'] . "', batch_no='" . $_REQUEST['batch'] . "', manufacturing_date='" . FormatDateForSQL($_REQUEST['mfg']) . "', expire_date='" . FormatDateForSQL($_REQUEST['expiry']) . "' , created = '" . date('Y-m-d G:i:s') . "' ,created_by='" . $_SESSION['UserID'] . "'";
			$srtoreItemQtyQry = mysqli_query($link1, $srtoreItemQtySql);
		} else {
			$batchId = $srtoreBatchItemRow['id'];
			$srtoreItemQtySql = "update batchdetails set qty=qty+" . $_REQUEST['stock'] . " where id='" . $batchId . "'";
			$srtoreItemQtyQry = mysqli_query($link1, $srtoreItemQtySql);
		}
	}
	echo "0";

	exit;
}
if (isset($_REQUEST['q'])) {
	$_POST['site'] = $_REQUEST['q'];
}
$store_sql = "SELECT id, StoreCode, StoreName FROM storetype where Store_Site = '" . $_POST['site']."' ";

// $store_sql = "SELECT id, StoreCode, StoreName FROM storetype where Store_Site = '" . $_POST['site'] . "' and (id IN(SELECT hmis_storeid FROM www_users WHERE id='" . $_SESSION['UserID'] . "') or id IN(SELECT store_id FROM www_users_dtls WHERE user_id='" . $_SESSION['UserID'] . "'))";
$store_result = DB_query($store_sql, $db);
if (db_num_rows($store_result) > 0) {
	while ($store_row = DB_fetch_array($store_result)) {
		$p = '';
		if ($store_row['id'] == $_POST['store'])
			$p = "selected";
	}
}
?>
<select name="store" style="width:200px" id="store">
	<?php

	$store_result = DB_query($store_sql, $db);
	if (db_num_rows($store_result) > 0) {
		while ($store_row = DB_fetch_array($store_result)) {
			$p = '';
			if ($store_row['id'] == $_POST['store'])
				$p = "selected";

	?>
			<option value="<?php echo $store_row['id']; ?>" <?php echo $p; ?>><?php echo $store_row['StoreName']; ?></option>
		<?php }
		?>
</select>
<?php
	} else
		echo "no data";
?>