async Task GenerateTestResultRefNo(int SampleId)
{
var Location = _authenticationManager.GetAuthenticatedUser().Result.LabLocation;
var ReportRefNo = string.Empty;
var MaxJobRefNo = 0;
int JobLabId = _dbContext.TblSample.Where(k => k.Id == (int)SampleId).Select(k => (int)k.LabId).FirstOrDefault();
string CountryCode = _dbContext.MstLabLocation.Where(k => k.Id == JobLabId).Select(k => k.CountryCode).FirstOrDefault();
string LabCode = _dbContext.MstLabLocation.Where(k => k.Id == JobLabId).Select(k => k.LabCode).FirstOrDefault();
string StateCode = _dbContext.MstLabLocation.Where(k => k.Id == JobLabId).Select(k => k.StateCode).FirstOrDefault();
string MergeReportRefNo ="IGI:"+ CountryCode + LabCode + ":TR:" + ApplicationData.AppDateToYear(ApplicationData.DefaultDateTime).ToString().Substring(2, 2) + ":";
try
{
var SampleRefNoList = (from TblSampleLog in _dbContext.TblSampleReportHeader.Where(x => x.LabId == JobLabId)
select new
{
ReportNo = TblSampleLog.ReportNo
}).ToList();
if (SampleRefNoList.ToList().Count == 0)
{
ReportRefNo = MergeReportRefNo + "00001";
// IGI:INMUM:TR:23:00001
}
else
{
var Max = SampleRefNoList.AsEnumerable().Select(k => new { SampleRefNo = int.Parse(k.ReportNo.Substring(16, 5)) }).Max(x => x.SampleRefNo);
MaxJobRefNo = Max + 1;
var MaxTransmitalString = "00000" + MaxJobRefNo.ToString();
var GenerateNo = MaxTransmitalString.Substring(MaxTransmitalString.Length - 5, 5);
//SampleRefNo = "INMUM" + "/" + GenerateNo.ToString().Trim();
ReportRefNo = MergeReportRefNo + GenerateNo.ToString().Trim();
}
}
catch (Exception ex)
{
_logger.Error(ex.Message);
ReportRefNo = string.Empty;
}
return await Task.Run(() =>
{
return ReportRefNo;
});
} Loading
Mohamed Azarudeen ZPosted Jul 12, 2023, 3:21 PM
Hi Sanjeev
Here's an overview of the code and how it generates the reference number:
The method
GenerateTestResultRefNotakes aSampleIdas input and returns aTaskrepresenting the generated test result reference number.It retrieves the authenticated user's lab location, lab ID, country code, lab code, and state code from the database.
It constructs a base string
MergeReportRefNoby combining the retrieved country code, lab code, and other static elements.It queries the database to fetch a list of existing test result reference numbers (
ReportNo) associated with the lab.If no existing test result reference numbers are found (
SampleRefNoListcount is 0), it sets theReportRefNoto the base string followed by "00001".If there are existing test result reference numbers, it finds the maximum numeric portion of the reference numbers, increments it by 1, and formats it as a five-digit string.
The
ReportRefNois set as the base string followed by the formatted numeric portion.If any exception occurs during the process, the
ReportRefNois set to an empty string.The method returns the
Taskwith the generated test result reference number.To use this method, you can await the
GenerateTestResultRefNomethod to get the generated test result reference number asynchronously.If this helps kindly accpt thhe answer thanks