/// <summary>Runs repeating charges for the date passed in, usually today. Can't use 'out' variables because this runs over Middle Tier.</summary> public static RepeatChargeResult RunRepeatingCharges(DateTime dateRun) { if (RemotingClient.RemotingRole == RemotingRole.ClientWeb) { return(Meth.GetObject <RepeatChargeResult>(MethodBase.GetCurrentMethod(), dateRun)); } Prefs.UpdateDateT(PrefName.RepeatingChargesBeginDateTime, dateRun); try { RepeatChargeResult result = new RepeatChargeResult(); List <RepeatCharge> listRepeatingCharges = RepeatCharges.Refresh(0).ToList(); if (PrefC.IsODHQ) { //Remove all eService repeating charges. //EService charges have already been calculated and stored in EServiceBilling table. Add those here. List <string> listEServiceCodes = EServiceCodeLink.GetProcCodesForAll(); listRepeatingCharges.RemoveAll(x => listEServiceCodes.Contains(x.ProcCode)); result.ProceduresAddedCount += EServiceBillings.AddEServiceRepeatingChargesHelper(dateRun).Count; } DateTime startedUsingFKs = UpdateHistories.GetDateForVersion(new Version("16.1.0.0")); //We started using FKs from procs to repeat charges in 16.1. foreach (RepeatCharge repeatCharge in listRepeatingCharges) { if (!repeatCharge.IsEnabled || (repeatCharge.DateStop.Year > 1880 && repeatCharge.DateStop.AddMonths(3) < dateRun)) { continue; //This repeating charge is too old to possibly create a new charge. Not precise but greatly reduces calls to DB. //We will filter by more stringently on the DateStop later on. } long codeNum = ProcedureCodes.GetCodeNum(repeatCharge.ProcCode); //Must contain all procedures that affect the date range. DateTime procRangeStart = repeatCharge.DateStart.AddMonths(-1); //Minus 1 month to catch accounts that have changed their billing date List <Procedure> listExistingProcs = Procedures.GetCompletedForDateRange(procRangeStart, dateRun, new List <long> { codeNum }, new List <long> { repeatCharge.PatNum }); for (int j = listExistingProcs.Count - 1; j >= 0; j--) //iterate backwards to remove elements { Procedure proc = listExistingProcs[j]; if (((proc.RepeatChargeNum == repeatCharge.RepeatChargeNum) //Check the procedure's FK first //Use the old logic without matching FKs only if the procedure was added before updating to 16.1 //Match patnum, codenum, fee, year, and month (IsRepeatDateHelper uses special logic to determine correct month) //Procedures with the ProcDate prior to the RepeatCharge.StartDate should not be considered as valid procedures //associated to the current repeat charge. || ((proc.ProcDate < startedUsingFKs || startedUsingFKs.Year < 1880) && proc.PatNum == repeatCharge.PatNum && proc.CodeNum == codeNum && proc.ProcFee.IsEqual(repeatCharge.ChargeAmt))) && (proc.ProcDate >= repeatCharge.DateStart || //Consider procedures that fall on or after the repeat charge start date. proc.ProcDate.Day != repeatCharge.DateStart.Day)) //Consider procs only when days are not the same. Catches procs that have changed their billing date { continue; //This existing procedure needs to be counted for this repeat charge. } listExistingProcs.RemoveAt(j); //Removing so that another repeat charge of the same code, date, and amount will be added. } List <DateTime> listBillingDates; //This list will have 1 or 2 dates where a repeating charge might be added if (PrefC.GetBool(PrefName.BillingUseBillingCycleDay)) { listBillingDates = GetBillingDatesHelper(repeatCharge.DateStart, repeatCharge.DateStop, dateRun, listExistingProcs, repeatCharge, Patients.GetPat(repeatCharge.PatNum).BillingCycleDay); } else { listBillingDates = GetBillingDatesHelper(repeatCharge.DateStart, repeatCharge.DateStop, dateRun, listExistingProcs, repeatCharge); } listBillingDates.RemoveAll(x => x.Date > DateTime.Today.Date && !PrefC.GetBool(PrefName.FutureTransDatesAllowed)); //If any billing dates have not been filtered out, add a repeating charge on those dates foreach (DateTime billingDate in listBillingDates) { Procedure procAdded = AddRepeatingChargeHelper(repeatCharge, billingDate, dateRun); List <Claim> listClaimsAdded = new List <Claim>(); if (repeatCharge.CreatesClaim && !ProcedureCodes.GetProcCode(repeatCharge.ProcCode).NoBillIns) { listClaimsAdded = AddClaimsHelper(repeatCharge, procAdded); } result.ProceduresAddedCount++; result.ClaimsAddedCount += listClaimsAdded.Count; } } return(result); } finally { Prefs.UpdateString(PrefName.RepeatingChargesBeginDateTime, ""); } }
/// <summary>Runs repeating charges for the date passed in, usually today. Can't use 'out' variables because this runs over Middle Tier.</summary> public static RepeatChargeResult RunRepeatingCharges(DateTime dateRun) { if (RemotingClient.RemotingRole == RemotingRole.ClientWeb) { return(Meth.GetObject <RepeatChargeResult>(MethodBase.GetCurrentMethod(), dateRun)); } Prefs.UpdateDateT(PrefName.RepeatingChargesBeginDateTime, dateRun); try { RepeatChargeResult result = new RepeatChargeResult(); List <RepeatCharge> listRepeatingCharges = RepeatCharges.Refresh(0).ToList(); if (PrefC.IsODHQ) { //Remove all eService repeating charges. //EService charges have already been calculated and stored in EServiceBilling table. Add those here. List <string> listEServiceCodes = EServiceCodeLink.GetProcCodesForAll(); listRepeatingCharges.RemoveAll(x => listEServiceCodes.Contains(x.ProcCode)); result.ProceduresAddedCount += EServiceBillings.AddEServiceRepeatingChargesHelper(dateRun).Count; } //Must contain all procedures that affect the date range, safe to contain too many, bad to contain too few. List <Procedure> listExistingProcs = Procedures.GetCompletedForDateRange(dateRun.AddMonths(-3), dateRun.AddDays(1), listRepeatingCharges.Select(x => x.ProcCode).Distinct().Select(x => ProcedureCodes.GetProcCode(x).CodeNum).ToList()); DateTime startedUsingFKs = UpdateHistories.GetDateForVersion(new Version("16.1.0.0")); //We started using FKs from procs to repeat charges in 16.1. foreach (RepeatCharge repeatCharge in listRepeatingCharges) { if (!repeatCharge.IsEnabled || (repeatCharge.DateStop.Year > 1880 && repeatCharge.DateStop.AddMonths(3) < dateRun)) { continue; //This repeating charge is too old to possibly create a new charge. Not precise but greatly reduces calls to DB. //We will filter by more stringently on the DateStop later on. } Patient pat = null; List <DateTime> listBillingDates; //This list will have 1 or 2 dates where a repeating charge might be added if (PrefC.GetBool(PrefName.BillingUseBillingCycleDay)) { pat = Patients.GetPat(repeatCharge.PatNum); listBillingDates = GetBillingDatesHelper(repeatCharge.DateStart, repeatCharge.DateStop, dateRun, pat.BillingCycleDay); } else { listBillingDates = GetBillingDatesHelper(repeatCharge.DateStart, repeatCharge.DateStop, dateRun); } long codeNum = ProcedureCodes.GetCodeNum(repeatCharge.ProcCode); //Remove billing dates if there is a procedure from this repeat charge in that month and year for (int i = listBillingDates.Count - 1; i >= 0; i--) //iterate backwards to remove elements { DateTime billingDate = listBillingDates[i]; for (int j = listExistingProcs.Count - 1; j >= 0; j--) //iterate backwards to remove elements { Procedure proc = listExistingProcs[j]; if ((proc.RepeatChargeNum == repeatCharge.RepeatChargeNum && //Check the procedure's FK first IsRepeatDateHelper(repeatCharge, billingDate, proc.ProcDate, pat)) //Use the old logic without matching FKs only if the procedure was added before updating to 16.1 //Match patnum, codenum, fee, year, and month (IsRepeatDateHelper uses special logic to determine correct month) || ((proc.ProcDate < startedUsingFKs || startedUsingFKs.Year < 1880) && proc.PatNum == repeatCharge.PatNum && proc.CodeNum == codeNum && IsRepeatDateHelper(repeatCharge, billingDate, proc.ProcDate, pat) && proc.ProcFee.IsEqual(repeatCharge.ChargeAmt))) { //This is a match to an existing procedure. listBillingDates.RemoveAt(i); //Removing so that a procedure will not get added on this date. listExistingProcs.RemoveAt(j); //Removing so that another repeat charge of the same code, date, and amount will be added. break; //Go to the next billing date } } } //If any billing dates have not been filtered out, add a repeating charge on those dates foreach (DateTime billingDate in listBillingDates) { Procedure procAdded = AddRepeatingChargeHelper(repeatCharge, billingDate, dateRun); List <Claim> listClaimsAdded = new List <Claim>(); if (repeatCharge.CreatesClaim && !ProcedureCodes.GetProcCode(repeatCharge.ProcCode).NoBillIns) { listClaimsAdded = AddClaimsHelper(repeatCharge, procAdded); } result.ProceduresAddedCount++; result.ClaimsAddedCount += listClaimsAdded.Count; } } return(result); } finally { Prefs.UpdateString(PrefName.RepeatingChargesBeginDateTime, ""); } }