Auto generate bill no in php mysql

It is in the month and year IF condition where the code is not working as expected because the offset to get to the month is now 3 instead of 2 and year is 5 instead of 4 because of the extra letter.

<?php // Get the current month and year as two-digit strings
$month = date("m"); 
$year = date("y");

// Get the last bill number from the database
$query = "SELECT patientno FROM iap2 ORDER BY patientno DESC";
$result = mysqli_query($conn,$query); 
// Use mysqli_fetch_assoc() to get an associative array of the fetched row
$row = mysqli_fetch_assoc($result); 
// Use $row['patientno'] to get the last bill number
$lastid = $row['patientno'];	 

// Check if the last bill number is empty or has a different month or year
if(empty($lastid) || substr($lastid, 3, 2) != $month || substr($lastid, 5, 2) != $year) {
	// Start a new sequence with 0001 
	$number = "Ls-$month$year-0001"; 
} 
else 
{
	// Increment the last four digits by one
	$idd = substr($lastid, 8); 
	echo $idd;
	// e.g. 0001
	$id = str_pad($idd + 1, 4, 0, STR_PAD_LEFT);
	// e.g. 0002 
	$number = "Ls-$month$year-$id"; 
}	
echo $number; 
?>

got it sir! thanks alot. But I have one more scenario. Can you tell me how can i fix it in this code
When i have to entered company initials randomly . for example
first entry is B-0923-0001
2nd entry is BA-0923-0001
3rd entry is B-09230002 (Current code again start with B-09230001 as its not incremneted)
currently the code is looking for last insert id and simply increment it by 1. what if it incremented on the basis of company initials. How can I manage that situation then???

You defiantly should use my version and make sure your iap2 table has a company field or a field that holds the initials representing the company. You would then add a WHERE condition to the query so the company field matches that value you are using so all results are related to the company. Something like this.

<?php

$CI	= "BAP";	//Example only
$CIcnt = strlen($CI);
$offset	= $CIcnt + 6;
 
// Get the current month and year as two-digit strings 
$month = date("m"); // e.g. 09 
$year = date("y"); // e.g. 23  

// Get the last bill number from the database 
$query = $conn->prepare("SELECT bilno FROM iap2 WHERE `ci` = ? ORDER BY bilno DESC");   
$query->bind_param("s", $CI); 
$query->execute(); 
$result = $query->get_result();   
$row = $result->fetch_assoc();
$lastid = $row['bilno']; 	

if(empty($lastid) || (substr($lastid, $CIcnt + 1, 2) != $month) || (substr($lastid, $CIcnt + 3, 2) != $year)) { 
	// Start a new sequence with 0001 
	$number = "$CI-$month$year-0001"; 
} else { 	
	// Increment the last four digits by one 
	$idd =  substr($lastid, $offset); // e.g. 0001 
	$id = str_pad($idd + 1, 4, 0, STR_PAD_LEFT); // e.g. 0002 
	$number = "$CI-$month$year-$id"; 
} 
echo $number;
 	 
?>

The prefix you are using in your coding, always worked on two letters long. If you use only one letter, then you need to change it to substr($lastid, 1, 2) and substr($lastid, 3, 2) to get the correct positions.

1 Like

If the letter prefix is always followed by a dash, you could always just search for the first dash, and base the various offsets on the position of that dash.

2 Likes

@droopsnoot I agree with you searching for the first dash would make the code more flexible and robust. Nice suggestion!!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.