<?php
session_start(); 

	if(!isset($_SESSION['linkarisecrm']))
	{
		header('Location: index.php');
		return;
	}
	
	include("connect.php");
	include("removespecial.php");
	date_default_timezone_set("Asia/Singapore");
	$mdate1= date('Y-m-d H:i:s');
	$mdate2= date('d-m-Y');
	
	$cmpmid=$_SESSION['linkarisecmpmid'];
	$umid = $_SESSION['linkariseumid'];
	
	$par1 = $_SESSION['linkariseumobile'];
	$ipaddress = "";

	if (getenv('HTTP_CLIENT_IP'))
		$ipaddress = getenv('HTTP_CLIENT_IP');
	else if(getenv('HTTP_X_FORWARDED_FOR'))
		$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
	else if(getenv('HTTP_X_FORWARDED'))
		$ipaddress = getenv('HTTP_X_FORWARDED');
	else if(getenv('HTTP_FORWARDED_FOR'))
		$ipaddress = getenv('HTTP_FORWARDED_FOR');
	else if(getenv('HTTP_FORWARDED'))
		$ipaddress = getenv('HTTP_FORWARDED');
	else if(getenv('REMOTE_ADDR'))
		$ipaddress = getenv('REMOTE_ADDR');
	else
		$ipaddress = 'UNKNOWN';
	
	$userserver=$_SERVER['HTTP_USER_AGENT'];
	$eve = "insert into res_user_log (lid,mobile,lastupdatedtime,pagename,taskname,ipaddress,useragentdetail) values(NULL,'$par1','$mdate1','Item','Add Item','$ipaddress','$userserver')";
	$re = mysqli_query($conn, $eve);
	
	$itemcode=$_POST['itemcode'];
	$itemcategory=removespecialchar($_POST['itemcategory']);
	$itemname=removespecialchar($_POST['itemname']);
	$itemdescription=removespecialchar($_POST['itemdescription']);
	$itemhsncode=$_POST['itemhsncode'];
	$itemtaxper=$_POST['itemtaxper'];
	$iunitmasterstock=0;
	$itemunit=$_POST['itemunit'];
	$itemminstock=$_POST['itemminstock'];
	$itemsalesrate=$_POST['itemsalesrate'];
	$itempurchaserate=$_POST['itempurchaserate'];
	
	
	$eve1 = "INSERT INTO res_item_master (itemcode,itemcategory,itemname,itemdescription,itemhsncode,itemtaxper,iunitmasterstock,itemunit,itemminstock,itemsalesrate,itempurchaserate,cmpmid) 
	VALUES('$itemcode','$itemcategory','$itemname','$itemdescription','$itemhsncode','$itemtaxper','$iunitmasterstock','$itemunit','$itemminstock','$itemsalesrate','$itempurchaserate','$cmpmid')";	
	if ($conn->query($eve1) === TRUE)
	{
		$last_id = $conn->insert_id;
		$_SESSION['msg'] = "Record has been added successfully";
	}
	else
	{
		$_SESSION['msg1'] = "Error";
	}
	
	header('Location: item.php');
?>