public static long InsertOrUpdateErxMedication(RxPat rxOld, RxPat rx, long rxCui, string strDrugName, string strGenericName, bool isProv, bool canInsertRx = true) { if (rxOld == null) { if (canInsertRx) { rx.IsNew = true; //Might not be necessary, but does not hurt. rx.IsErxOld = false; SecurityLogs.MakeLogEntry(Permissions.RxCreate, rx.PatNum, "eRx automatically created: " + rx.Drug); RxPats.Insert(rx); } } else //The prescription was already in our database. Update it. { rx.RxNum = rxOld.RxNum; //Preserve the pharmacy on the existing prescription, in case the user set the value manually. //We do not pull pharmacy back from eRx yet. rx.PharmacyNum = rxOld.PharmacyNum; if (rxOld.IsErxOld) { rx.IsErxOld = true; rx.SendStatus = RxSendStatus.SentElect; //To maintain backward compatibility. } RxPats.Update(rx); } //If rxCui==0, then third party eRx option (DoseSpot, NewCrop, etc) did not provide an RxCui. //Attempt to locate an RxCui using the other provided drug information. An RxCui is not required for our program. //Meds missing an RxCui are not exported in CCD messages. if (rxCui == 0 && strDrugName != "") { List <RxNorm> listRxNorms = RxNorms.GetListByCodeOrDesc(strDrugName, true, true); //Exact case insensitive match ignoring numbers. if (listRxNorms.Count > 0) { rxCui = PIn.Long(listRxNorms[0].RxCui); } } //If rxCui==0, then third party eRx option (DoseSpot, NewCrop, etc) did not provide an RxCui and we could not locate an RxCui by DrugName //Try searching by GenericName. if (rxCui == 0 && strGenericName != "") { List <RxNorm> listRxNorms = RxNorms.GetListByCodeOrDesc(strGenericName, true, true); //Exact case insensitive match ignoring numbers. if (listRxNorms.Count > 0) { rxCui = PIn.Long(listRxNorms[0].RxCui); } } //If rxCui==0, then third party eRx option (DoseSpot, NewCrop, etc) did not provide an RxCui and we could not //locate an RxCui by DrugName or GenericName. if (rxCui == 0) { //We may need to enhance in future to support more advanced RxNorm searches. //For example: DrugName=Cafatine, DrugInfo=Cafatine 1 mg-100 mg Tab, GenericName=ergotamine-caffeine. //This drug could not be found by DrugName nor GenericName, but could be found when the GenericName was split by non-alpha characters, //then the words in the generic name were swapped. //Namely, "caffeine ergotamine" is in the RxNorm table. } //MedicationNum of 0, because we do not want to bloat the medication list in OD. //In this special situation, we instead set the MedDescript, RxCui and ErxGuid columns. return(MedicationPats.InsertOrUpdateMedOrderForRx(rx, rxCui, isProv)); }
///<summary>For CPOE. Used for both manual rx and eRx through NewCrop. Creates or updates a medical order using the given prescription information. ///Since rxCui is not part of the prescription, it must be passed in as a separate parameter. ///If isProvOrder is true, then the medical order provNum will be set to the prescription provNum. If isProvOrder is false, then the medical order provNum will be set to 0. ///The MedDescript and ErxGuid will always be copied from the prescription to the medical order and the medical order MedicationNum will be set to 0. ///This method return the medOrderNum for the new/updated medicationPat. Unlike most medical orders this does not create an entry in the medical order table.</summary> public static long InsertOrUpdateMedOrderForRx(RxPat rxPat, long rxCui, bool isProvOrder) { long medOrderNum; MedicationPat medOrderOld = null; if (!string.IsNullOrWhiteSpace(rxPat.ErxGuid)) //This check prevents an extra db call when making a new prescription manually inside OD. { medOrderOld = MedicationPats.GetMedicationOrderByErxIdAndPat(rxPat.ErxGuid, rxPat.PatNum); } MedicationPat medOrder = null; //The medication order corresponding to the prescription. if (medOrderOld == null) { medOrder = new MedicationPat(); } else { medOrder = medOrderOld.Clone(); //Maintain primary key and medicationnum for the update below. } medOrder.DateStart = rxPat.RxDate; int numDays = PrefC.GetInt(PrefName.MedDefaultStopDays); if (numDays != 0) { medOrder.DateStop = rxPat.RxDate.AddDays(numDays); } medOrder.MedDescript = rxPat.Drug; medOrder.RxCui = rxCui; if (rxCui != 0) { //The customer may not have a medication entered for this RxCui the first few times they get this particular medication back from eRx. //Once the customer adds the medication to their medication list, then we can automatically attach the order to the medication. //The reason we decided not to automatically create the medication if one does not already exist is because we do not want to //accidentally bloat the medication list, if for example, the user has the medication entered but has not set the RxCui on it yet. List <Medication> listMeds = Medications.GetAllMedsByRxCui(rxCui); if (listMeds.Count > 0) { medOrder.MedicationNum = listMeds[0].MedicationNum; } } medOrder.ErxGuid = rxPat.ErxGuid; medOrder.PatNote = rxPat.Sig; medOrder.PatNum = rxPat.PatNum; if (isProvOrder) { medOrder.ProvNum = rxPat.ProvNum; medOrder.IsCpoe = true; } if (medOrderOld == null) { medOrder.IsNew = true; //Might not be necessary, but does not hurt. medOrderNum = MedicationPats.Insert(medOrder); //If the ErxGuid has not been set, and it is a new medication, set the ErxGuid so that the medication can be sent to DoseSpot if (Erx.IsManualRx(rxPat.ErxGuid)) { try { int medPatNumAsInt = (int)medOrderNum; rxPat.ErxGuid = Erx.OpenDentalErxPrefix + medPatNumAsInt; RxPats.Update(rxPat); } catch (Exception ex) { //If we cannot downgrade a long to an int for the ErxGuid we can simply ignore trying to update the rxPat. //This is because this medication would never be sent to eRx because we would attempt this downgrade again in the exact same manner. ex.DoNothing(); } } } else //The medication order was already in our database. Update it. { medOrder.MedicationPatNum = medOrderOld.MedicationPatNum; MedicationPats.Update(medOrder, false); //Don't ErxGuid here, it was already purposefully set above. medOrderNum = medOrder.MedicationPatNum; } return(medOrderNum); }