///<summary>Called from FormPayPlan. Also deletes all attached payplancharges. Throws exception if there are any paysplits attached.</summary> public static void Delete(PayPlan plan) { if (RemotingClient.RemotingRole == RemotingRole.ClientWeb) { Meth.GetVoid(MethodBase.GetCurrentMethod(), plan); return; } string command; if (plan.PlanNum == 0 || plan.IsDynamic) //Patient payment plan { command = "SELECT COUNT(*) FROM paysplit WHERE PayPlanNum=" + POut.Long(plan.PayPlanNum); if (Db.GetCount(command) != "0") { throw new ApplicationException (Lans.g("PayPlans", "You cannot delete a payment plan with patient payments attached. Unattach the payments first.")); } } else //Insurance payment plan { command = "SELECT COUNT(*) FROM claimproc WHERE PayPlanNum=" + POut.Long(plan.PayPlanNum) + " AND claimproc.Status IN (" + POut.Int((int)ClaimProcStatus.Received) + "," + POut.Int((int)ClaimProcStatus.Supplemental) + ")"; if (Db.GetCount(command) != "0") { throw new ApplicationException (Lans.g("PayPlans", "You cannot delete a payment plan with insurance payments attached. Unattach the payments first.")); } //if there are any unreceived items, detach them here, then proceed deleting List <ClaimProc> listClaimProcs = ClaimProcs.GetForPayPlans(new List <long> { plan.PayPlanNum }); foreach (ClaimProc claimProc in listClaimProcs) { claimProc.PayPlanNum = 0; ClaimProcs.Update(claimProc); } } command = "DELETE FROM payplancharge WHERE PayPlanNum=" + POut.Long(plan.PayPlanNum); Db.NonQ(command); command = $"DELETE FROM payplanlink WHERE PayPlanNum={POut.Long(plan.PayPlanNum)}"; Db.NonQ(command); command = "DELETE FROM payplan WHERE PayPlanNum =" + POut.Long(plan.PayPlanNum); Db.NonQ(command); }