Example #1
0
        public PatientMedSupp SavePatientMedSupp(PostPatientMedSuppRequest request)
        {
            PatientMedSupp patientMedSupp = null;

            try
            {
                if (request.PatientMedSupp != null)
                {
                    string name     = string.IsNullOrEmpty(request.PatientMedSupp.Name) ? string.Empty : request.PatientMedSupp.Name.ToUpper();
                    string form     = string.IsNullOrEmpty(request.PatientMedSupp.Form) ? string.Empty : request.PatientMedSupp.Form.ToUpper();
                    string route    = string.IsNullOrEmpty(request.PatientMedSupp.Route) ? string.Empty : request.PatientMedSupp.Route.ToUpper();
                    string strength = string.IsNullOrEmpty(request.PatientMedSupp.Strength) ? string.Empty : request.PatientMedSupp.Strength;

                    #region Search MedicationMap
                    // Search if any record exists with the given combination of name, strength, route and form.
                    GetMedicationMapsRequest mmRequest = new GetMedicationMapsRequest
                    {
                        Name           = name,
                        Route          = route,
                        Form           = form,
                        Strength       = strength,
                        ContractNumber = request.ContractNumber,
                        UserId         = request.UserId,
                        Version        = request.Version
                    };
                    List <MedicationMapData> list = EndpointUtil.SearchMedicationMap(mmRequest);
                    #endregion
                    if (list == null)
                    {
                        MedicationMapData medData = null;
                        if (string.IsNullOrEmpty(request.PatientMedSupp.FamilyId))
                        {
                            #region Insert MedicationMap
                            PostMedicationMapRequest insertReq = new PostMedicationMapRequest
                            {
                                MedicationMap = new DTO.MedicationMap
                                {
                                    FullName      = name,
                                    SubstanceName = string.Empty,
                                    Strength      = strength,
                                    Route         = route,
                                    Form          = form,
                                    Custom        = true,
                                    Verified      = false
                                },
                                ContractNumber = request.ContractNumber,
                                UserId         = request.UserId,
                                Version        = request.Version
                            };
                            medData = EndpointUtil.InsertMedicationMap(insertReq);
                            #endregion
                        }
                        else
                        {
                            #region Update MedicationMap
                            // This saves the initialized medicine map
                            PutMedicationMapRequest req = new PutMedicationMapRequest
                            {
                                MedicationMap = new DTO.MedicationMap
                                {
                                    Id            = request.PatientMedSupp.FamilyId,
                                    FullName      = name,
                                    SubstanceName = string.Empty,
                                    Strength      = strength,
                                    Route         = route,
                                    Form          = form,
                                    Custom        = true,
                                    Verified      = false
                                },
                                ContractNumber = request.ContractNumber,
                                UserId         = request.UserId,
                                Version        = request.Version
                            };
                            medData = EndpointUtil.UpdateMedicationMap(req);
                            #endregion
                        }
                        RegisterMedication(request, medData);
                    }
                    #region Calculate NDC codes.
                    bool calculateNDC = false;
                    if (request.Insert)
                    {
                        calculateNDC = true;
                        request.PatientMedSupp.SystemName = Constants.SystemName;
                    }
                    else
                    {
                        // On update, check for ReCalculateNDC flag.
                        if (request.RecalculateNDC)
                        {
                            calculateNDC = true;
                        }
                    }
                    if (calculateNDC)
                    {
                        request.PatientMedSupp.NDCs = EndpointUtil.GetMedicationNDCs(request);
                    }
                    #endregion

                    string sigCode = CalculateSigCode(request);
                    if (!string.IsNullOrEmpty(sigCode))
                    {
                        request.PatientMedSupp.SigCode = sigCode;
                    }
                    PatientMedSuppData data = EndpointUtil.SavePatientMedSupp(request);
                    if (data != null)
                    {
                        patientMedSupp = Mapper.Map <PatientMedSupp>(data);
                    }
                }
                return(patientMedSupp);
            }
            catch (Exception ex) { throw ex; }
        }