<?php
require("../connection.php");
include("../user.php");

$from_location_id=$_REQUEST['from_location'];
	$location_det=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM location_master where id='$from_location_id'"));
	$from_location=$location_det['location'];
$division_id=$_REQUEST['division'];
	$division_det=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM branch_master where id='$division_id'"));
	$division=$division_det['branch_name'];
$department_id=$_REQUEST['department'];
$department_det=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM type_master where id='$department_id'"));
$department=$department_det['type'];
$category_id=$_REQUEST['category'];
$category_det=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM category_master where id='$category_id'"));
$category=$category_det['category_type'];
$qty=$_REQUEST['qty'];

$from=substr($from_location,0,3); $div=substr($division,0,3); $dep=substr($department,0,3); $cat=substr($category,0,3);
	$fa_serial=$from.''.$div;

$check_rows=mysqli_query($con,"SELECT fa_serial_no FROM `item_master` where fa_serial_no like '%$fa_serial%' order by id desc limit 0,1");

$count_rows=mysqli_num_rows($check_rows);

if($count_rows)
{
 $fa_serial_gen=mysqli_fetch_array($check_rows);  $fa_serials=$fa_serial_gen['fa_serial_no']; 
  $fa_serial_number=substr($fa_serials,13,16);
	 $fa_serial_things=substr($fa_serial_number,0,6); 
	$counts=$fa_serial_things+$qty;  
	$get_string= strlen($counts);
		 if($get_string<=4)
		 {
			$counts= ''.str_pad($counts, 4, '0', STR_PAD_LEFT);
		 }
		 else
		 {
			 $counts;
		 }
	echo $fa_serial=$from.''.$div.''.$dep.''.$cat.''.$counts;
}
else
{
	$initial="0000";
	$counts=$initial+$qty; 
	$get_string= strlen($counts);
		 if($get_string<=4)
		 {
			$counts= ''.str_pad($counts, 4, '0', STR_PAD_LEFT);
		 }
		 else
		 {
			 $counts;
		 }
	echo $fa_serial=$from.''.$div.''.$dep.''.$cat.''.$counts;
}


?>