<link rel="stylesheet" href="../css/sweet-alert.css" /> 
<?php include("../connection.php"); include("../user.php"); $user=$_SESSION['user'];	$cur_date=date('Y-m-d H:i:s');	$branch_id=$_SESSION['branch_id'];
	$ho_access=$_SESSION['ha'];
	
	$trans_id=$_REQUEST['transaction_id'];
	$date=$_REQUEST['date']; $dates=date('Y-m-d',strtotime($date));
	$from_locations=$_REQUEST['from_location'];
		$tolocation=$_REQUEST['to_location'];
	$fdivision=$_REQUEST['fdivision'];
	$division=$_REQUEST['division'];
	$department_id=$_REQUEST['department'];
	$fa=$_REQUEST['fa'];
	$dep_value=$_REQUEST['dep_value'];
	$dep_amount=$_REQUEST['dep_amount'];
	$expiry_dates=$_REQUEST['expiry_dates']; $expiry_date=date('Y-m-d',strtotime($expiry_dates));
	$remarks=$_REQUEST['remarks'];
		 	$category1=$_REQUEST['cat_name'];
			$category_description=$_REQUEST['cat_desc'];
			$item_quantity=$_REQUEST['item'];
			$item_value=$_REQUEST['value'];
			$item_amount=$_REQUEST['amount'];
	$count=count($category1);
		//$div=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM branch_master where id='$division'"));		 //$division_name=$division;
	$deps=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM type_master where id='$department_id'"));
		$department=$deps['type'];

if($ho_access=='yes')
{  
	$stock_inward=mysqli_query($con,"INSERT INTO stock_inward(branch_id,transaction_id,date,item_no,fa_serial_no,from_location,to_location,from_division,to_division,department,flag,status,created_on, created_by)VALUES('$branch_id','$trans_id','$dates','$count','$fa','$from_locations','$tolocation','$fdivision','$division','$department','1','1','$cur_date','$user')");
	
	for($i=0;$i<$count;$i++)
	{      
		$category=$category1;
			$item_values=$item_value;
			$item_amounts=$item_amount;
		$stock_detail=mysqli_query($con,"INSERT INTO stock_inward_detail(transaction_id, category, category_description,quantity,value,amount, flag, status, created_on, created_by) VALUES ('$trans_id','$category','$category_description','$item_quantity','$item_values','$item_amounts','1','1','$cur_date','$user')");
	}
		if($stock_detail!='')
		{
			$stock_inupdate=mysqli_query($con,"update stock_transfer set status='1' where transaction_id='$trans_id'");
			$stock_itemdetails=mysqli_query($con,"update stock_item_detail set status='1' where transaction_id='$trans_id'");
			
			$item_rems=mysqli_query($con,"SELECT quantity,value FROM item_master where location='$from_locations' and category='$category'  and category_description='$category_description'");
				 
			$item_rem=mysqli_fetch_array($item_rems);
			 $quantitys=$item_rem['quantity'];
			 $values=$item_rem['value'];
			
			if($quantitys==$item_quantity)
			{
				$updates=mysqli_query($con,"update item_master set quantity=0 and value=0 and amount=0 where location='$from_locations' and category='$category' and category_description='$category_description'");
			}
			else
			{
				$adj_quantity=$quantitys-$item_quantity;
				$overall_amt=$adj_quantity*$values;
					$updates=mysqli_query($con,"update item_master set quantity='$adj_quantity',value='$values',amount='$overall_amt' where location='$from_locations' and category='$category' and category_description='$category_description'");
			}
				$item_remss=mysqli_fetch_array(mysqli_query($con,"SELECT quantity FROM item_master where location='$from_locations' and category='$category' and category_description='$category_description'"));
					$item_quan=$item_remss['quantity'];
			if($item_quan>0)
			{
			}
			else
			{ 
				$updates=mysqli_query($con,"update item_master set quantity=0,status=1 where location='$from_locations' and category='$category' and category_description='$category_description'");
			}
		}	
$item_insert=mysqli_query($con,"insert into item_master( po_date, division, location, department, category, category_description, quantity, value, amount, deprication_percentage, deprication_value, expiry_date, fa_serial_no, remarks, status, created_by, created_on)values('$dates','$division','$tolocation','$department','$category','$category_description','$item_quantity','$item_values','$item_amount','$dep_value','$dep_amount','$expiry_date','$fa','$remarks',0,'$user',now())");
}
else
{  
	/*
//$stock_inward=mysqli_query($con,"INSERT INTO stock_inward(branch_id,transaction_id,date,item_no,from_location,to_location,flag,status, created_on, created_by)VALUES('$tolocation','$trans_id','$date','$count','$from_locations','$tolocation','1','1','$cur_date','$user')");
	
		for($i=0;$i<$count;$i++)
		{      
			$category=$category1;
				$item_values=$item_value;
				$item_amounts=$item_amount;
				
			//$stock_detail=mysqli_query($con,"INSERT INTO stock_inward_detail(transaction_id, category, quantity,value,amount, flag, status, created_on, created_by) VALUES ('$trans_id','$category','$item_quantity','$item_values','$item_amounts','1','1','$cur_date','$user')");
		}	
	if($stock_detail!='')
	{
		//$stock_inupdate=mysqli_query($con,"update stock_transfer set status='1' where transaction_id='$trans_id'");
//$stock_itemdetails=mysqli_query($con,"update stock_item_detail set status='1' where transaction_id='$trans_id'");
	}
$item_insert=mysqli_query($con,"insert into item_master( po_date, division, location, department, category, category_description, quantity, value, amount, deprication_percentage, deprication_value, expiry_date, fa_serial_no, remarks, status, created_by, created_on)values('$date','$division','$tolocation','$department','$category','$category_description','$item_quantity','$item_amounts','($item_quantity*$item_amounts)','','','','$fa','$remarks',0,'$user',now())");

	echo "insert into item_master( po_date, division, location, department, category, category_description, quantity, value, amount, deprication_percentage, deprication_value, expiry_date, fa_serial_no, remarks, status, created_by, created_on)values('$dates','$division','$tolocation','$department','$category','$category_description','$item_quantity','$item_values','$item_amount','$dep_value','$dep_amount','$expiry_date','$fa','$remarks',0,'$user',now())";*/
	
	$stock_inward=mysqli_query($con,"INSERT INTO stock_inward(branch_id,transaction_id,date,item_no,fa_serial_no,from_location,to_location,from_division,to_division,department,flag,status, created_on, created_by)VALUES('$branch_id','$trans_id','$dates','$count','$fa','$from_locations','$tolocation','$fdivision','$division','$department','1','1','$cur_date','$user')");
	
	for($i=0;$i<$count;$i++)
	{      
		$category=$category1;
			$item_values=$item_value;
			$item_amounts=$item_amount;
		$stock_detail=mysqli_query($con,"INSERT INTO stock_inward_detail(transaction_id, category,  category_description, quantity,value,amount, flag, status, created_on, created_by) VALUES ('$trans_id','$category','$category_description','$item_quantity','$item_values','$item_amounts','1','1','$cur_date','$user')");
	}
		if($stock_detail!='')
		{  
			$stock_inupdate=mysqli_query($con,"update stock_transfer set status='1' where transaction_id='$trans_id'");
			$stock_itemdetails=mysqli_query($con,"update stock_item_detail set status='1' where transaction_id='$trans_id'");
			
			$item_rems=mysqli_query($con,"SELECT quantity,value FROM item_master where location='$from_locations' and category='$category'  and category_description='$category_description'");
				 
			$item_rem=mysqli_fetch_array($item_rems);
			 $quantitys=$item_rem['quantity'];
			 $values=$item_rem['value'];
			
			if($quantitys==$item_quantity)
			{
				$updates=mysqli_query($con,"update item_master set quantity=0 and value=0 and amount=0 where location='$from_locations' and category='$category' and category_description='$category_description'");
			}
			else
			{
				$adj_quantity=$quantitys-$item_quantity;
				$overall_amt=$adj_quantity*$values;
					$updates=mysqli_query($con,"update item_master set quantity='$adj_quantity',value='$values',amount='$overall_amt' where location='$from_locations' and category='$category' and category_description='$category_description'");
			}
				$item_remss=mysqli_fetch_array(mysqli_query($con,"SELECT quantity FROM item_master where location='$from_locations' and category='$category' and category_description='$category_description'"));
				
			  $item_quan=$item_remss['quantity'];
			if($item_quan>0)
			{
			}
			else
			{ 
				$updates=mysqli_query($con,"update item_master set quantity=0,status=1 where location='$from_locations' and category='$category' and category_description='$category_description'");
			}
		}	
$item_insert=mysqli_query($con,"insert into item_master( po_date, division, location, department, category, category_description, quantity, value, amount, deprication_percentage, deprication_value, expiry_date, fa_serial_no, remarks, status, created_by, created_on)values('$dates','$division','$tolocation','$department','$category','$category_description','$item_quantity','$item_values','$item_amount','$dep_value','$dep_amount','$expiry_date','$fa','$remarks',0,'$user',now())");
	
	
}
	if($item_insert)
	{
	?>
	<script>
	
	
    setTimeout(function() {
        swal({
            title: "Successfully",
            text: "Stock Received!",
            type: "success"
        }, function() {
			window.location = "stock_inward_master.php";
        });
    }, 100);
</script>  
    <?php
	}	
	else
	{
	?>
	<script>
    setTimeout(function() {
        swal({
            title: "Not",
            text: "Stock Received!",
            type: "error"
        }, function() {
			window.location = "stock_inward_master.php";
        });
    }, 100);
</script> 	  
	<?Php
	}	
?> 
  <script src="../js/sweet-alert.js"></script>

