///<summary>Mimics ContrAccount.CreateClaim(...).  Removes items from listProcs until unique diagnosis code count is low enough.</summary>
        private void GetUniqueDiagnosticCodes(List <ProcNotBilled> listProcs, List <Procedure> listPatProcs, List <PatPlan> listPatPlans,
                                              List <InsSub> listInsSubs, List <InsPlan> listInsPlans)
        {
            List <Procedure> listProcedures = new List <Procedure>();

            for (int i = 0; i < listProcs.Count; i++)
            {
                listProcedures.Add(Procedures.GetProcFromList(listPatProcs, listProcs[i].ProcNum));
            }
            //If they have medical insurance and no dental, make the claim type Medical.  This is to avoid the scenario of multiple med ins and no dental.
            bool isMedical = false;

            if (PatPlans.GetOrdinal(PriSecMed.Medical, listPatPlans, listInsPlans, listInsSubs) > 0 &&
                PatPlans.GetOrdinal(PriSecMed.Primary, listPatPlans, listInsPlans, listInsSubs) == 0 &&
                PatPlans.GetOrdinal(PriSecMed.Secondary, listPatPlans, listInsPlans, listInsSubs) == 0)
            {
                isMedical = true;
            }
            while (!isMedical && Procedures.GetUniqueDiagnosticCodes(listProcedures, false).Count > 4)            //dental
            {
                int index = listProcedures.Count - 1;
                listProcedures.RemoveAt(index);
                listProcs.RemoveAt(index);
            }
            while (isMedical && Procedures.GetUniqueDiagnosticCodes(listProcedures, true).Count > 12)            //medical
            {
                int index = listProcedures.Count - 1;
                listProcedures.RemoveAt(index);
                listProcs.RemoveAt(index);
            }
        }
        private void butNewClaims_Click(object sender, EventArgs e)
        {
            if (gridMain.SelectedIndices.Length == 0)           //No selections made.
            {
                MsgBox.Show(this, "Please select at least one procedure.");
                return;
            }
            if (!ContrAccount.CheckClearinghouseDefaults())
            {
                return;
            }
            //Generate List and Table----------------------------------------------------------------------------------------------------------------------
            //List of all procedures being shown.
            //Pulls procedures based off of the PatNum, if the row was selected in gridMain and if it has been attached to a claim.
            List <ProcNotBilled> listNotBilledProcs = new List <ProcNotBilled>();
            List <long>          listPatNums        = new List <long>();
            Patient          patOld            = new Patient();
            List <Claim>     listPatClaims     = new List <Claim>();
            List <ClaimProc> listPatClaimProcs = new List <ClaimProc>();
            List <ClaimProc> listCurClaimProcs = new List <ClaimProc>();
            //Table rows need to be 1:1 with gridMain rows due to logic in ContrAccount.toolBarButIns_Click(...).
            DataTable table = new DataTable();

            //Required columns as mentioned by ContrAccount.toolBarButIns_Click().
            table.Columns.Add("ProcNum");
            table.Columns.Add("chargesDouble");
            table.Columns.Add("ProcNumLab");
            for (int i = 0; i < gridMain.Rows.Count; i++)                         //Loop through gridMain to construct listNotBilledProcs.
            {
                long      procNumCur = PIn.Long(gridMain.Rows[i].Tag.ToString()); //Tag is set to procNum in fillGrid().
                Procedure procCur    = Procedures.GetOneProc(procNumCur, false);
                long      patNumCur  = procCur.PatNum;
                if (patOld.PatNum != patNumCur)               //Procedures in gridMain are ordered by patient, so when the patient changes, we know previous patient is complete.
                {
                    listPatClaims     = Claims.Refresh(patNumCur);
                    listPatClaimProcs = ClaimProcs.Refresh(patNumCur);
                    patOld            = Patients.GetPat(procCur.PatNum);
                }
                listCurClaimProcs = ClaimProcs.GetForProc(listPatClaimProcs, procNumCur);
                bool hasPriClaim = false;
                bool hasSecClaim = false;
                for (int j = 0; j < listCurClaimProcs.Count; j++)
                {
                    ClaimProc claimProcCur = listCurClaimProcs[j];
                    if (claimProcCur.ClaimNum > 0 && claimProcCur.Status != ClaimProcStatus.Preauth && claimProcCur.Status != ClaimProcStatus.Estimate)
                    {
                        Claim claimCur = Claims.GetFromList(listPatClaims, claimProcCur.ClaimNum);
                        switch (claimCur.ClaimType)
                        {
                        case "P":
                            hasPriClaim = true;
                            break;

                        case "S":
                            hasSecClaim = true;
                            break;
                        }
                    }
                }
                bool isSelected = gridMain.SelectedIndices.Contains(i);
                listNotBilledProcs.Add(new ProcNotBilled(patOld, procNumCur, i, isSelected, hasPriClaim, hasSecClaim, procCur.ClinicNum, procCur.PlaceService));
                DataRow row = table.NewRow();
                row["ProcNum"] = procNumCur;
                #region Calculate chargesDouble
                //Logic copied from AccountModules.GetAccount(...) line 857.
                double qty = (procCur.UnitQty + procCur.BaseUnits);
                if (qty == 0)
                {
                    qty = 1;
                }
                double writeOffCapSum = listPatClaimProcs.Where(x => x.Status == ClaimProcStatus.CapComplete).Select(y => y.WriteOff).ToList().Sum();
                row["chargesDouble"] = (procCur.ProcFee * qty) - writeOffCapSum;
                row["ProcNumLab"]    = procCur.ProcNumLab;
                #endregion Calculate chargesDouble
                table.Rows.Add(row);
                if (listPatNums.Contains(patNumCur))
                {
                    continue;
                }
                listPatNums.Add(patNumCur);
            }
            List <List <ProcNotBilled> > listGroupedProcs = new List <List <ProcNotBilled> >();
            Patient          patCur           = null;
            List <PatPlan>   listPatPlans     = null;
            List <InsSub>    listInsSubs      = null;
            List <InsPlan>   listInsPlans     = null;
            List <Procedure> listPatientProcs = null;
            ProcNotBilled    procNotBilled    = new ProcNotBilled();    //When automatically grouping,  this is used as the procedure to group by.
            long             patNumOld        = 0;
            int claimCreatedCount             = 0;
            int patIndex = 0;
            //The procedures show in the grid ordered by patient.  Also listPatNums contains unique patnums which are in the same order as the grid.
            while (patIndex < listPatNums.Count)
            {
                List <ProcNotBilled> listProcs = listNotBilledProcs.Where(x => x.Patient.PatNum == listPatNums[patIndex] && x.IsRowSelected && !x.IsAttached).ToList();
                if (listProcs.Count == 0)
                {
                    patNumOld = listPatNums[patIndex];
                    patIndex++;                    //No procedures were selected for this patient.
                    continue;
                }
                else
                {
                    //Maintain the same patient, in order to create one or more additional claims for the remaining procedures.
                    //Currently will only happen for specific instances;
                    //--Canadian customers who are attempting to create a claim with over 7 procedures.
                    //--When checkAutoGroupProcs is checked and when there are multiple procedure groupings by GroupKey status, ClinicNum, and placeService.
                }
                if (patNumOld != listPatNums[patIndex])               //The patient could repeat if we had to group the procedures for the patinet into multiple claims.
                {
                    patCur           = Patients.GetPat(listPatNums[patIndex]);
                    listPatPlans     = PatPlans.Refresh(patCur.PatNum);
                    listInsSubs      = InsSubs.RefreshForFam(Patients.GetFamily(patCur.PatNum));
                    listInsPlans     = InsPlans.RefreshForSubList(listInsSubs);
                    listPatientProcs = Procedures.Refresh(patCur.PatNum);
                }
                if (checkAutoGroupProcs.Checked)                 //Automatically Group Procedures.
                {
                    procNotBilled = listProcs[0];
                    //Update listProcs to reflect those that match the procNotBilled values.
                    listProcs = listProcs.FindAll(x => x.HasPriClaim == procNotBilled.HasPriClaim && x.HasSecClaim == procNotBilled.HasSecClaim);
                    if (PrefC.HasClinicsEnabled)                     //Group by clinic only if clinics enabled.
                    {
                        listProcs = listProcs.FindAll(x => x.ClinicNum == procNotBilled.ClinicNum);
                    }
                    else if (!PrefC.GetBool(PrefName.EasyHidePublicHealth))                     //Group by Place of Service only if Public Health feature is enabled.
                    {
                        listProcs = listProcs.FindAll(x => x.PlaceService == procNotBilled.PlaceService);
                    }
                }
                GetUniqueDiagnosticCodes(listProcs, listPatientProcs, listPatPlans, listInsSubs, listInsPlans);
                if (listProcs.Count > 7 && CultureInfo.CurrentCulture.Name.EndsWith("CA")) //Canadian. en-CA or fr-CA
                {
                    listProcs = listProcs.Take(7).ToList();                                //Returns first 7 items of the list.
                }
                listProcs.ForEach(x => x.IsAttached = true);                               //This way we can not attach procedures to multiple claims thanks to the logic above.
                if (listProcs.Any(x => listProcs[0].PlaceService != x.PlaceService) || listProcs.Any(x => listProcs[0].ClinicNum != x.ClinicNum))
                {
                    //Regardless if we are automatically grouping or not,
                    //if all procs in our list at this point do not share the same PlaceService or ClinicNum then claims will not be made.
                }
                else                  //Basic validation passed.
                {
                    if (!listProcs[0].HasPriClaim &&                 //Medical claim.
                        PatPlans.GetOrdinal(PriSecMed.Medical, listPatPlans, listInsPlans, listInsSubs) > 0 &&                    //Has medical ins.
                        PatPlans.GetOrdinal(PriSecMed.Primary, listPatPlans, listInsPlans, listInsSubs) == 0 &&                    //Does not have primary dental ins.
                        PatPlans.GetOrdinal(PriSecMed.Secondary, listPatPlans, listInsPlans, listInsSubs) == 0)                       //Does not have secondary dental ins.
                    {
                        claimCreatedCount++;
                    }
                    else                                                                                                                      //Not a medical claim.
                    {
                        if (!listProcs[0].HasPriClaim && PatPlans.GetOrdinal(PriSecMed.Primary, listPatPlans, listInsPlans, listInsSubs) > 0) //Primary claim.
                        {
                            claimCreatedCount++;
                        }
                        if (!listProcs[0].HasSecClaim && PatPlans.GetOrdinal(PriSecMed.Secondary, listPatPlans, listInsPlans, listInsSubs) > 0)                  //Secondary claim.
                        {
                            claimCreatedCount++;
                        }
                    }
                }
                listGroupedProcs.Add(listProcs);
            }
            if (!MsgBox.Show(this, true, "Clicking continue will create up to " + POut.Int(claimCreatedCount) + " claims and cannot be undone, except by manually going to each account.  "
                             + "Some claims may not be created if there are validation issues.  Would you like to proceed?"))
            {
                return;
            }
            //Create Claims--------------------------------------------------------------------------------------------------------------------------------
            claimCreatedCount = 0;
            string claimErrors = "";
            foreach (List <ProcNotBilled> listProcs in listGroupedProcs)
            {
                patCur = listProcs[0].Patient;
                gridMain.SetSelected(false);                //Need to deslect all rows each time so that ContrAccount.toolBarButIns_Click(...) only uses pertinent rows.
                for (int j = 0; j < listProcs.Count; j++)
                {
                    gridMain.SetSelected(listProcs[j].RowIndex, true);                   //Select the pertinent rows so that they will be attached to the claim below.
                }
                ContrAccount.toolBarButIns_Click(false, patCur, Patients.GetFamily(patCur.PatNum), gridMain, table,
                                                 procNotBilled.HasPriClaim, procNotBilled.HasSecClaim);
                string errorTitle = patCur.PatNum + " " + patCur.GetNameLFnoPref() + " - ";
                if (patNumOld == patCur.PatNum && !string.IsNullOrEmpty(ContrAccount.ClaimErrorsCur))
                {
                    claimErrors += "\t\t" + ContrAccount.ClaimErrorsCur + "\r\n";
                }
                else if (!string.IsNullOrEmpty(ContrAccount.ClaimErrorsCur))
                {
                    claimErrors += errorTitle + ContrAccount.ClaimErrorsCur + "\r\n";
                }
                claimCreatedCount += ContrAccount.ClaimCreatedCount;
                patNumOld          = patCur.PatNum;
            }
            FillGrid();
            if (!string.IsNullOrEmpty(claimErrors))
            {
                MsgBoxCopyPaste form = new MsgBoxCopyPaste(claimErrors);
                form.ShowDialog();
            }
            MessageBox.Show(Lan.g(this, "Number of claims created") + ": " + claimCreatedCount);
        }
        private Claim CreateClaim(string claimType, List <PatPlan> patPlanList, List <InsPlan> planList, List <ClaimProc> claimProcList, Procedure proc, List <InsSub> subList)
        {
            long           claimFormNum = 0;
            InsPlan        planCur      = new InsPlan();
            InsSub         subCur       = new InsSub();
            Relat          relatOther   = Relat.Self;
            long           clinicNum    = proc.ClinicNum;
            PlaceOfService placeService = proc.PlaceService;

            switch (claimType)
            {
            case "P":
                subCur  = InsSubs.GetSub(PatPlans.GetInsSubNum(patPlanList, PatPlans.GetOrdinal(PriSecMed.Primary, patPlanList, planList, subList)), subList);
                planCur = InsPlans.GetPlan(subCur.PlanNum, planList);
                break;

            case "S":
                subCur  = InsSubs.GetSub(PatPlans.GetInsSubNum(patPlanList, PatPlans.GetOrdinal(PriSecMed.Secondary, patPlanList, planList, subList)), subList);
                planCur = InsPlans.GetPlan(subCur.PlanNum, planList);
                break;

            case "Med":
                //It's already been verified that a med plan exists
                subCur  = InsSubs.GetSub(PatPlans.GetInsSubNum(patPlanList, PatPlans.GetOrdinal(PriSecMed.Medical, patPlanList, planList, subList)), subList);
                planCur = InsPlans.GetPlan(subCur.PlanNum, planList);
                break;
            }
            ClaimProc claimProcCur = Procedures.GetClaimProcEstimate(proc.ProcNum, claimProcList, planCur, subCur.InsSubNum);

            if (claimProcCur == null)
            {
                claimProcCur = new ClaimProc();
                ClaimProcs.CreateEst(claimProcCur, proc, planCur, subCur);
            }
            Claim claimCur = new Claim();

            claimCur.PatNum       = proc.PatNum;
            claimCur.DateService  = proc.ProcDate;
            claimCur.ClinicNum    = proc.ClinicNum;
            claimCur.PlaceService = proc.PlaceService;
            claimCur.ClaimStatus  = "W";
            claimCur.DateSent     = DateTimeOD.Today;
            claimCur.PlanNum      = planCur.PlanNum;
            claimCur.InsSubNum    = subCur.InsSubNum;
            InsSub sub;

            switch (claimType)
            {
            case "P":
                claimCur.PatRelat   = PatPlans.GetRelat(patPlanList, PatPlans.GetOrdinal(PriSecMed.Primary, patPlanList, planList, subList));
                claimCur.ClaimType  = "P";
                claimCur.InsSubNum2 = PatPlans.GetInsSubNum(patPlanList, PatPlans.GetOrdinal(PriSecMed.Secondary, patPlanList, planList, subList));
                sub = InsSubs.GetSub(claimCur.InsSubNum2, subList);
                if (sub.PlanNum > 0 && InsPlans.RefreshOne(sub.PlanNum).IsMedical)
                {
                    claimCur.PlanNum2  = 0;                         //no sec ins
                    claimCur.PatRelat2 = Relat.Self;
                }
                else
                {
                    claimCur.PlanNum2  = sub.PlanNum;                         //might be 0 if no sec ins
                    claimCur.PatRelat2 = PatPlans.GetRelat(patPlanList, PatPlans.GetOrdinal(PriSecMed.Secondary, patPlanList, planList, subList));
                }
                break;

            case "S":
                claimCur.PatRelat   = PatPlans.GetRelat(patPlanList, PatPlans.GetOrdinal(PriSecMed.Secondary, patPlanList, planList, subList));
                claimCur.ClaimType  = "S";
                claimCur.InsSubNum2 = PatPlans.GetInsSubNum(patPlanList, PatPlans.GetOrdinal(PriSecMed.Primary, patPlanList, planList, subList));
                sub = InsSubs.GetSub(claimCur.InsSubNum2, subList);
                claimCur.PlanNum2  = sub.PlanNum;
                claimCur.PatRelat2 = PatPlans.GetRelat(patPlanList, PatPlans.GetOrdinal(PriSecMed.Primary, patPlanList, planList, subList));
                break;

            case "Med":
                claimCur.PatRelat  = PatPlans.GetFromList(patPlanList, subCur.InsSubNum).Relationship;
                claimCur.ClaimType = "Other";
                if (PrefC.GetBool(PrefName.ClaimMedTypeIsInstWhenInsPlanIsMedical))
                {
                    claimCur.MedType = EnumClaimMedType.Institutional;
                }
                else
                {
                    claimCur.MedType = EnumClaimMedType.Medical;
                }
                break;

            case "Other":
                claimCur.PatRelat  = relatOther;
                claimCur.ClaimType = "Other";
                //plannum2 is not automatically filled in.
                claimCur.ClaimForm = claimFormNum;
                if (planCur.IsMedical)
                {
                    if (PrefC.GetBool(PrefName.ClaimMedTypeIsInstWhenInsPlanIsMedical))
                    {
                        claimCur.MedType = EnumClaimMedType.Institutional;
                    }
                    else
                    {
                        claimCur.MedType = EnumClaimMedType.Medical;
                    }
                }
                break;
            }
            if (planCur.PlanType == "c")          //if capitation
            {
                claimCur.ClaimType = "Cap";
            }
            claimCur.ProvTreat = proc.ProvNum;
            if (Providers.GetIsSec(proc.ProvNum))
            {
                claimCur.ProvTreat = Patients.GetPat(proc.PatNum).PriProv;
                //OK if zero, because auto select first in list when open claim
            }
            claimCur.IsProsthesis  = "N";
            claimCur.ProvBill      = Providers.GetBillingProvNum(claimCur.ProvTreat, claimCur.ClinicNum);    //OK if zero, because it will get fixed in claim
            claimCur.EmployRelated = YN.No;
            claimCur.ClaimForm     = planCur.ClaimFormNum;
            Claims.Insert(claimCur);
            //attach procedure
            claimProcCur.ClaimNum = claimCur.ClaimNum;
            if (planCur.PlanType == "c")           //if capitation
            {
                claimProcCur.Status = ClaimProcStatus.CapClaim;
            }
            else
            {
                claimProcCur.Status = ClaimProcStatus.NotReceived;
            }
            if (planCur.UseAltCode && (ProcedureCodes.GetProcCode(proc.CodeNum).AlternateCode1 != ""))
            {
                claimProcCur.CodeSent = ProcedureCodes.GetProcCode(proc.CodeNum).AlternateCode1;
            }
            else if (planCur.IsMedical && proc.MedicalCode != "")
            {
                claimProcCur.CodeSent = proc.MedicalCode;
            }
            else
            {
                claimProcCur.CodeSent = ProcedureCodes.GetProcCode(proc.CodeNum).ProcCode;
                if (claimProcCur.CodeSent.Length > 5 && claimProcCur.CodeSent.Substring(0, 1) == "D")
                {
                    claimProcCur.CodeSent = claimProcCur.CodeSent.Substring(0, 5);
                }
                if (CultureInfo.CurrentCulture.Name.EndsWith("CA"))         //Canadian. en-CA or fr-CA
                {
                    if (claimProcCur.CodeSent.Length > 5)                   //In Canadian e-claims, codes can contain letters or numbers and cannot be longer than 5 characters.
                    {
                        claimProcCur.CodeSent = claimProcCur.CodeSent.Substring(0, 5);
                    }
                }
            }
            claimProcCur.LineNumber = (byte)1;
            ClaimProcs.Update(claimProcCur);
            return(claimCur);
        }
        private void butOK_Click(object sender, System.EventArgs e)
        {
            RepeatCharge[] chargeList  = RepeatCharges.Refresh(0);         //Gets all repeating charges for all patients, they may be disabled
            int            countAdded  = 0;
            int            claimsAdded = 0;
            DateTime       startDate;
            Procedure      proc;

            for (int i = 0; i < chargeList.Length; i++)
            {
                if (!chargeList[i].IsEnabled)                 //first make sure it is not disabled
                {
                    continue;
                }
                if (chargeList[i].DateStart > DateTime.Today)              //not started yet
                {
                    continue;
                }
                //if(chargeList[i].DateStop.Year>1880//not blank
                //	&& chargeList[i].DateStop<DateTime.Today)//but already ended
                //{
                //	continue;
                //}
                //get a list dates of all completed procedures with this Code and patNum
                ArrayList ALdates = RepeatCharges.GetDates(ProcedureCodes.GetCodeNum(chargeList[i].ProcCode), chargeList[i].PatNum);
                startDate = chargeList[i].DateStart;
                //This is the repeating date using the old methodology.  It is necessary for checking if the repeating procedure was already added using the old methodology.
                DateTime possibleDateOld = startDate;
                //This is a more accurate repeating date which will allow procedures to be added on the 28th and later.
                DateTime possibleDateNew = startDate;
                int      countMonths     = 0;
                //start looping through possible dates, beginning with the start date of the repeating charge
                while (possibleDateNew <= DateTime.Today)
                {
                    //Only allow back dating up to one month and 20 days.
                    if (possibleDateNew < DateTime.Today.AddDays(-50))
                    {
                        possibleDateOld = possibleDateOld.AddMonths(1);
                        countMonths++;
                        possibleDateNew = startDate.AddMonths(countMonths);
                        continue;                        //don't go back more than one month and 20 days
                    }
                    //check to see if the possible date is present in the list
                    if (ALdates.Contains(possibleDateNew) ||
                        ALdates.Contains(possibleDateOld))
                    {
                        possibleDateOld = possibleDateOld.AddMonths(1);
                        countMonths++;
                        possibleDateNew = startDate.AddMonths(countMonths);
                        continue;
                    }
                    if (chargeList[i].DateStop.Year > 1880 &&              //not blank
                        chargeList[i].DateStop < possibleDateNew)                           //but already ended
                    {
                        break;
                    }
                    //otherwise, insert a procedure to db
                    proc                = new Procedure();
                    proc.CodeNum        = ProcedureCodes.GetCodeNum(chargeList[i].ProcCode);
                    proc.DateEntryC     = DateTimeOD.Today;
                    proc.PatNum         = chargeList[i].PatNum;
                    proc.ProcDate       = possibleDateNew;
                    proc.DateTP         = possibleDateNew;
                    proc.ProcFee        = chargeList[i].ChargeAmt;
                    proc.ProcStatus     = ProcStat.C;
                    proc.ProvNum        = PrefC.GetLong(PrefName.PracticeDefaultProv);
                    proc.MedicalCode    = ProcedureCodes.GetProcCode(proc.CodeNum).MedicalCode;
                    proc.BaseUnits      = ProcedureCodes.GetProcCode(proc.CodeNum).BaseUnits;
                    proc.DiagnosticCode = PrefC.GetString(PrefName.ICD9DefaultForNewProcs);
                    //Check if the repeating charge has been flagged to copy it's note into the billing note of the procedure.
                    if (chargeList[i].CopyNoteToProc)
                    {
                        proc.BillingNote = chargeList[i].Note;
                    }
                    Procedures.Insert(proc);                    //no recall synch needed because dental offices don't use this feature
                    countAdded++;
                    possibleDateOld = possibleDateOld.AddMonths(1);
                    countMonths++;
                    possibleDateNew = startDate.AddMonths(countMonths);
                    if (chargeList[i].CreatesClaim && !ProcedureCodes.GetProcCode(chargeList[i].ProcCode).NoBillIns)
                    {
                        List <PatPlan>   patPlanList = PatPlans.Refresh(chargeList[i].PatNum);
                        List <InsSub>    subList     = InsSubs.RefreshForFam(Patients.GetFamily(chargeList[i].PatNum));
                        List <InsPlan>   insPlanList = InsPlans.RefreshForSubList(subList);;
                        List <Benefit>   benefitList = Benefits.Refresh(patPlanList, subList);
                        Claim            claimCur;
                        List <Procedure> procCurList = new List <Procedure>();
                        procCurList.Add(proc);
                        if (patPlanList.Count == 0)                       //no current insurance, do not create a claim
                        {
                            continue;
                        }
                        //create the claimprocs
                        Procedures.ComputeEstimates(proc, proc.PatNum, new List <ClaimProc>(), true, insPlanList, patPlanList, benefitList, Patients.GetPat(proc.PatNum).Age, subList);
                        //get claimprocs for this proc, may be more than one
                        List <ClaimProc> claimProcList = ClaimProcs.GetForProc(ClaimProcs.Refresh(proc.PatNum), proc.ProcNum);
                        string           claimType     = "P";
                        if (patPlanList.Count == 1 && PatPlans.GetOrdinal(PriSecMed.Medical, patPlanList, insPlanList, subList) > 0)                  //if there's exactly one medical plan
                        {
                            claimType = "Med";
                        }
                        claimCur      = CreateClaim(claimType, patPlanList, insPlanList, claimProcList, proc, subList);
                        claimProcList = ClaimProcs.Refresh(proc.PatNum);
                        if (claimCur.ClaimNum == 0)
                        {
                            continue;
                        }
                        claimsAdded++;
                        ClaimL.CalculateAndUpdate(procCurList, insPlanList, claimCur, patPlanList, benefitList, Patients.GetPat(proc.PatNum).Age, subList);
                        if (PatPlans.GetOrdinal(PriSecMed.Secondary, patPlanList, insPlanList, subList) > 0 &&                //if there exists a secondary plan
                            !CultureInfo.CurrentCulture.Name.EndsWith("CA"))                               //and not canada (don't create secondary claim for canada)
                        {
                            claimCur = CreateClaim("S", patPlanList, insPlanList, claimProcList, proc, subList);
                            if (claimCur.ClaimNum == 0)
                            {
                                continue;
                            }
                            claimsAdded++;
                            claimProcList        = ClaimProcs.Refresh(proc.PatNum);
                            claimCur.ClaimStatus = "H";
                            ClaimL.CalculateAndUpdate(procCurList, insPlanList, claimCur, patPlanList, benefitList, Patients.GetPat(proc.PatNum).Age, subList);
                        }
                    }
                }
            }
            //MessageBox.Show(countAdded.ToString()+" "+Lan.g(this,"procedures added."));
            MessageBox.Show(countAdded.ToString() + " " + Lan.g(this, "procedures added.") + "\r\n" + claimsAdded.ToString() + " " + Lan.g(this, "claims added."));
            DialogResult = DialogResult.OK;
        }
        ///<summary>Sets the foreground text to red if any row has a DOS between textDOSFrom and textDOSTo and matches textClaimFee </summary>
        private void HighlightRows()
        {
            DateTime dateFrom           = PIn.Date(textDateFrom.Text);
            DateTime dateTo             = PIn.Date(textDateTo.Text);
            double   fee                = PIn.Double(textClaimFee.Text);
            int      rowsHighlightCount = 0;
            int      lastHighlightIndex = 0;

            gridClaims.BeginUpdate();
            for (int i = 0; i < gridClaims.ListGridRows.Count; i++)
            {
                gridClaims.ListGridRows[i].ColorText = Color.Black; //reset row highlighting
                gridClaims.ListGridRows[i].Bold      = false;       //reset row highlighting
                Claim claim       = (Claim)gridClaims.ListGridRows[i].Tag;
                YN    isFeeMatch  = YN.No;                          //If fee matches then yes, if fee doesnt match then no, if no fee entered then unknown
                YN    isDateMatch = YN.No;                          //If both dates match then yes, if both dates dont match then no, if no dates entered then unknown
                //Check fee
                if (textClaimFee.Text == "")                        //No fee entered
                {
                    isFeeMatch = YN.Unknown;
                }
                else
                {
                    if (claim.ClaimFee.ToString("f").Contains(textClaimFee.Text))
                    {
                        isFeeMatch = YN.Yes;
                    }
                }
                //Check date
                if (dateFrom == DateTime.MinValue && dateTo == DateTime.MinValue)               //No dates entered
                {
                    isDateMatch = YN.Unknown;
                }
                else                    //At least one date entered
                {
                    if ((dateFrom.CompareTo(claim.DateService) <= 0 || dateFrom == DateTime.MinValue) &&
                        (dateTo.CompareTo(claim.DateService) >= 0 || dateTo == DateTime.MinValue))
                    {
                        isDateMatch = YN.Yes;
                    }
                }
                Hx835_Proc proc         = _x835Claim.ListProcs.FirstOrDefault();
                bool       isPlanMatch  = true;                                                                                             //Assume true for backwards compatiablity, because older 837s did not send out the Ordinal and PlanNum.
                bool       isBasicMatch = (isFeeMatch == YN.Yes || isDateMatch == YN.Yes) && (isFeeMatch != YN.No && isDateMatch != YN.No); //If either match and neither don't match
                if (isBasicMatch && proc != null && proc.PlanNum != 0)                                                                      //Consider new 837 REF*6R pattern for values starting with 'x'.
                {
                    if (_listPatPlans == null)
                    {
                        _listPatPlans = PatPlans.GetPatPlansForPat(_patNum);
                    }
                    //Strict PlanNum AND Ordinal match.  Users can manually change ordinals easily after sending claims.
                    //A failure to match here is OK because we will give the user the option to manually select the correct claim from the grid.
                    if (proc.PlanNum != claim.PlanNum || PatPlans.GetOrdinal(claim.InsSubNum, _listPatPlans) != proc.PlanOrdinal)
                    {
                        isPlanMatch = false;
                    }
                }
                if (isBasicMatch && isPlanMatch)
                {
                    //Highlight row
                    gridClaims.ListGridRows[i].ColorText = Color.Red;
                    gridClaims.ListGridRows[i].Bold      = true;
                    rowsHighlightCount++;
                    lastHighlightIndex = i;
                }
            }
            gridClaims.EndUpdate();
            if (rowsHighlightCount == 1)
            {
                gridClaims.SetSelected(lastHighlightIndex, true);
                FillClaimDetails(lastHighlightIndex);
            }
        }