<?php 
include("../connection.php");
include("../user.php");
?>
<link rel="stylesheet" href="../css/sweet-alert.css" /> 
<?php
	$branch=$_SESSION['division_name'];
	date_default_timezone_set('Asia/Calcutta');
	$cur_date=date('Y-m-d H:i:s');
	$user=$_SESSION['user'];
	$branch_id=$_SESSION['branch_id'];
	$ho_access=$_SESSION['ha'];
	$division=$_REQUEST['division'];
	$fdivision=$_REQUEST['fdivision'];
	$item_search=$_REQUEST['item_search'];
	$from_location=$_REQUEST['from_location'];
	$tolocation=$_REQUEST['from_location'];
	$fa_serial_no=$_REQUEST['fa_serial_no'];
	$quantity=$_REQUEST['quantity'];
	$remarks=$_REQUEST['remarks'];
	$trans_id=$_REQUEST['transaction_id'];
	$a=$_REQUEST['item_search'];
	$count = 1;
// 	list($a,$b) = explode(' / ',$category1);
// 	$count=count($category1);
	$item_details=mysqli_fetch_array(mysqli_query($con,"select value from item_master where material_name='$a' and status=0"));
    $item_value=$item_details['value']; 
    $tot_amount=$quantity*$item_value;
    if($ho_access=='yes')
    {
    	$stock_transfer=mysqli_query($con,"INSERT INTO stock_transfer(branch_id,from_division,to_division,transaction_id,date,no_of_item,from_location,to_location,flag,status, created_on, created_by) VALUES ('$branch_id','$fdivision','$division','$trans_id','$cur_date','$count','$from_location','$tolocation','1','0','$cur_date','$user')");
    // 	for($i=0;$i<$count;$i++)
    // 	{      
			$category=$a;
			$category_description="";
		    $stock_detail=mysqli_query($con,"INSERT INTO stock_item_detail( transaction_id, category,  category_description,fa_serial_no,quantity, value, amount, flag, status, created_on, created_by) VALUES ('$trans_id','$category','$category_description','$fa_serial_no','$quantity','$item_value','$tot_amount',1,0,'$cur_date','$user')");
    // 	}
    }
    else
    {
	    $stock_transfer=mysqli_query($con,"INSERT INTO stock_transfer(branch_id,from_division,to_division,transaction_id,date,no_of_item,from_location,to_location,flag,status, created_on, created_by) VALUES ('$branch_id','$branch','$division','$trans_id','$cur_date','$count','$branch_id','$tolocation','1','0','$cur_date','$user')");
		for($i=0;$i<$count;$i++)
		{      
			$category=$a;
			$category_description=$b;
			$stock_detail=mysqli_query($con,"INSERT INTO stock_item_detail( transaction_id, category,  category_description,fa_serial_no, quantity, value, amount, flag, status, created_on, created_by) VALUES ('$trans_id','$category','$category_description','$fa_serial_no','$quantity','$item_value','$tot_amount',1,0,'$cur_date','$user')");
		}	
    }		
    if($stock_transfer)
    {	
?>
<script>
    setTimeout(function() {
        swal({
            title: "Successfully",
            text: "Stock Transfered!",
            type: "success"
        }, function() {
      window.location = "stock_transfer_master.php";
        });
    }, 100);
</script>  
<?php	}	else {	?>
<script>
	setTimeout(function() {
		swal({
			title: "Not",
			text: "Stock Transfered!",
			type: "error"
		}, function() {
	   window.location = "stock_transfer_master.php";
		});
	}, 100);
</script> 	  
<?Php	}	?> 
<script src="../js/sweet-alert.js"></script>