<?php
	header('Content-Type: application/json');
	include("includes/dbconfig.php");
	
	session_write_close();
	
	$err = "";
	
	/////
	
	
	/////////
	
	$server_response = array();
	 
	$supplier = intval($_POST['supplier']);
	$stockid =  $memo =  $_POST['memo'];
	 
	///////////////
	$supplier = intval($_POST['supplier']); 
	
	$quantity = intval($_POST['quantity']);
	
	
	 
	//supplier//stockid
	
	if($stockid > 0 and $quantity > 0){} else {  $err = $err."Quantity should be more than Zero!<br>";}
	
	
	if(isset($_SESSION['connecteduserid']))
	{
		if($_SESSION["connectedusergroup"] == 1 || $_SESSION["connectedusergroup"] == 2)
		{
			
			 
		}
		else
		{
			$err = $err."Limited Access!<br>";
		}
	}
	else
	{
		$err = $err."Un Authenticated User Access Detected!<br>";
	}
	
	if($err == "")
	{
			
		
		DB::$error_handler = false;
		DB::$throw_exception_on_error = true;
		
		try
		{
			$trxnConnextn = new MeekroDB();
			$nonTrxnConnextn = new MeekroDB();
			
			$trxnConnextn->startTransaction();
			$trxnConnextn->query("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE");
 
			
			 
				$itemstockdata = $trxnConnextn->queryFirstRow("SELECT quantity  FROM item_stock_level WHERE id=%i", $stockid);
			
				$trxnConnextn->update
				('item_stock_level', 
					array
					(
						'updated_date' => date('Y-m-d H:i:s'),
						'updated_by' => $_SESSION["connecteduserid"],
						'quantity' => ($itemstockdata['quantity'] + $quantity) 
					), 
					"id=%i", $stockid
				);
				
				$trxnConnextn->insert
				('stock_item_received', 
					array
					(
						'created_date' => date('Y-m-d H:i:s'),
						'created_by' => $_SESSION["connecteduserid"],
						'value_date' => date("Y-m-d"),
						'goods_received_note' => NULL,
						'item' => $stockid,
						'supplier' => 1,
						'quantity' => $quantity,
						'memo' => $memo
					)
				);
			 		
			
			$trxnConnextn->commit();
			$savestatusmessage = "<div class=\"alert alert-success\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>Item(s) Successfully Received!</div>";
			$server_response[] = array('issaved' => 1, 'message' => $savestatusmessage);
			DB::$error_handler = 'meekrodb_error_handler';
			DB::$throw_exception_on_error = false;
			echo json_encode($server_response);
		}
		catch(MeekroDBException $e)
		{
			$err = $err.$e->getMessage()."<br>";
			$err = $err.$e->getQuery()."<br>";
			$trxnConnextn->rollback();
			$errormessage = "<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>" . $err . "</div>";
			$server_response[] = array('issaved' => 0, 'message' => $errormessage);
			DB::$error_handler = 'meekrodb_error_handler';
			DB::$throw_exception_on_error = false;
			echo json_encode($server_response);
			exit();
		}
	}
	else
	{
		$errormessage = "<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>" . $err . "</div>";
		$server_response[] = array('issaved' => 0, 'message' => $errormessage);
		echo json_encode($server_response);
	}
?>