<link rel="stylesheet" href="../css/sweet-alert.css" /> 
<?php
error_reporting(E_ALL ^ E_NOTICE);
include("../connection.php");
include("../user.php");
$cur_date=date('Y-m-d H:i:s');
$sys_date=date('Y-m-d');
$user=$_SESSION['user'];
 $branch_id=$_SESSION['branch_id'];
if($branch_id<>"")
{
	$branch_id1=$_SESSION['branch_id'];
}
else
{
	$branch_id1=$_REQUEST['branch'];
}
	
	$tr_count=$_REQUEST['counter_tr'];
	$category=$_REQUEST['cat'];
	$head=$_REQUEST['configuration'];
	$po_no=$_REQUEST['po_number'];

	$value=$_REQUEST['confie'];
	$tot_qty=$_REQUEST['inward_qty'];
	$inward_table_id=$_REQUEST['inward_table_id'];
 //echo count($inward_table_id);

$cat_split=implode(',',$category);
$value_split1=implode(',',$value);
echo "<br>";
implode(',',$head);

 $myArray = explode(',',$cat_split);
 $myArray1 = explode(',',$value_split1);

	$k=0;
	$u=0;
for($i=0;$i<=sizeof($category);$i++)
{
	echo "<br>";
	$get_cat_id=mysqli_query($con,"SELECT id,prefix_name FROM `category_master` where category='$myArray[$i]'");
	$get_cat_fetch=mysqli_fetch_array($get_cat_id);
	$cat_id_db=$get_cat_fetch['id'];
	$prefix_name=$get_cat_fetch['prefix_name']; 

	$confi_query=mysqli_query($con,"SELECT * FROM `config_detail` where category_name='$cat_id_db'");
	$head_count=mysqli_num_rows($confi_query);
	$multi=$head_count*$tot_qty[$i];
	while($confi_fetch=mysqli_fetch_array($confi_query))
	{	 
		 $confi_cat[]=$confi_fetch['config_detail'];
	}
	  $es=implode(',',$confi_cat);
	
	for($j=0;$j<$tot_qty[$k];$j++)
	{
	$prefix_check=mysqli_query($con,"SELECT asset_id FROM `item_master` where asset_id like ('%$prefix_name%') ORDER by asset_id DESC limit 1");
	// echo "SELECT * FROM `item_master` where asset_id like ('%$prefix_name%') ORDER by id DESC limit 1";
	$check_prefix_count=mysqli_num_rows($prefix_check);
	 if($check_prefix_count==0)
	 {
		   $prefix_set=$prefix_name."-1"; 
	 }
	 else
	 {
	$asset_id=mysqli_fetch_array($prefix_check);
	$asset=$asset_id['asset_id'];
	$asset_split=explode('-',$asset);
	$number=$asset_split[1]+1;
	 $prefix_set=$prefix_name."-".$number;
	 }
	
	$val=array();
	for($r=0;$r<$head_count;$r++)
	{
	$myArray1[$u];	 
	$val[]=$myArray1[$u];
	$u++;
	}	
	 $confi_value=implode(',',$val);
	$category_insert=mysqli_query($con,"INSERT INTO `item_master`(`branch_id`,`po_no`,`category_id`,`header`,`value`,`asset_id`,`created_by`)VALUES('$branch_id1','$po_no','$cat_id_db','$es','$confi_value','$prefix_set','$user')");
	
}
$update_rec=mysqli_fetch_array(mysqli_query($con,"select received_qty from inward_create_table where id='$inward_table_id[$i]'"));
$get_rec_qty[]=$update_rec['received_qty'];
$get_received_qty=$get_rec_qty[$i]+$tot_qty[$i];

$po_update=mysqli_query($con,"update `inward_create_table` SET received_qty='$get_received_qty' where id=$inward_table_id[$i]");
	 $confi_cat=[];
	 $k++;
	}
	
	
if($category_insert)
	{
	?>
	<script>
	setTimeout(function() {
        swal({
            title: "Successfully",
            text: "completed!",
            type: "success"
        }, function() {
       window.location = "inventory_master.php";
        });
    }, 100);
</script>  
    <?php
	}	
	else
	{
	?>
	<script>
    setTimeout(function() {
        swal({
            title: "Not",
            text: "completed!",
            type: "error"
        }, function() {
      window.location = "inventory_master.php";
        });
    }, 100);
</script> 	  
	<?Php
	}	
?>
<script src="../js/sweet-alert.js"></script>