I think this is going to be somewhat adjacent to Tim’s epic auto-numbering thread.
I have been tasked with this code review for months, and I’m just not even sure where to start. The way it was written was a little brute-force. Here is the layout:
Each branch is just a different Company. The “ceiling” for automated check numbers for each company has been updated manually as required over the years. This happens whenever the code breaks due to manually-entered check numbers causing it to reach the limit. Here’s an example of the script:
//Automatically assigns the next CheckNum if creating a manual payment.
//Manual checks fall into 2 categories:
//"Manual PR Checks" = Manual corrections in the Payroll system requiring a check to be cut to an employee. These are issued from the payroll system but are logged into Epicor using the "Manual PR Checks" Supplier.
//Other Manual Checks = Most / All other manual checks are actually EFT payments direct deducted out of our bank accounts. Finance enters these as manual checks so that visibility is clear in Epicor.
//Code had to be split for each Company that had jumped massive number sequences; WSF is one of those instances.
//2015-12-18 Created
//2016-06-01 Migrated
//2017-05-11 Updated
//2019-11-18 Updated | CheckNum ceiling set from 1000 to 1255
//2021-04-15 Updated | CheckNum ceiling set from 1255 to 1661 & made sure the NextCheck started at the beginning of the loop + 1
int iNextCheck = 1257; //Updated on 11/4/2020. Iterator was stuck on and assigning "0" each time. Changed this variable from being set at "0" to being = to start of CheckNum loop +1.
string cExMsg = string.Empty; //Define Error Message variable. This message will only come into play when the code is reaching the end of its life so that the user can notify the System Admin to update.
//This should only be necessary once, maybe twice in the lifetime of the company, and should become obsolete once all gaps are filled in.
Erp.Tables.CheckHed CheckHed; //Define additional table references.
//Loop through the ADDED Check rows where Manual Print is true and the Supplier ISN'T "Manual PR Checks".
foreach (var ttCheckHed_iterator in (from ttCheckHed_Row in ttCheckHed
where ttCheckHed_Row.Company == Session.CompanyID
&& string.Equals(ttCheckHed_Row.RowMod, IceRow.ROWSTATE_ADDED, StringComparison.OrdinalIgnoreCase)
&& ttCheckHed_Row.ManualPrint == true
&& ttCheckHed_Row.Name != "Manual PR Checks"
select ttCheckHed_Row))
{
var ttCheckHedRow = ttCheckHed_iterator;
foreach (var CheckHed2_iterator in (from CheckHed2_Row in Db.CheckHed
where CheckHed2_Row.Company == ttCheckHedRow.Company
&& CheckHed2_Row.ManualPrint == true
&& CheckHed2_Row.Name != "Manual PR Checks"
&& CheckHed2_Row.CheckNum > 1256 //KM, Added 4/15/2021
&& CheckHed2_Row.CheckNum < 1661 //In WSF the CheckNum jumped from 1001 to 1256, so we cut the loop off at 1255.
orderby CheckHed2_Row.CheckNum
select CheckHed2_Row))
{
var CheckHed2Row = CheckHed2_iterator;
//If the last CheckNum is greater than the Next Check variable, make the variable equal to the last CheckNum + 1.
if (CheckHed2Row.CheckNum >= iNextCheck)
{
iNextCheck = CheckHed2Row.CheckNum + 1;
}
//Because the code to find the highest number is set to END before 1255, the code below looks if the final Next Check number is exactly 1255.
//If it IS 1255, an error message displays so that the user notifies IT so that the "&& CheckHed2_Row.CheckNum < 1255" can be updated or deleted, and the below "if (iNextCheck == 1000)" can be adjusted accordingly.
if (iNextCheck == 1661)
{
cExMsg = "Automatic numbering limit reached.\n\nPlease contact your System Administrator for assitance.\n";
}
if (cExMsg != "")
{
cExMsg = cExMsg + "\n[BPM PaymentEntry.OnChangeVendor Set_ManualCheckNum]\n.\n";
CallContext.Current.ExceptionManager.AddBLException(cExMsg);
}
}
//In the case where there has been NO manual check cut before, make sure that the CheckNum given is still at least 1.
//if (iNextCheck == 0)
// {
//iNextCheck = 1;
//}
ttCheckHedRow.CheckNum = iNextCheck; //Set the CheckNum on the UI to the Next Check variable.
}
Does anyone have any advice on where to start with this?