Example #1
0
        ///<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);
        }