Пример #1
0
        public string Process(DtoDrugDispensed drugDispensed)
        {
            try
            {
                LookupLogic facilityLookup           = new LookupLogic();
                string      receivingFacilityMflCode = drugDispensed.MESSAGE_HEADER.RECEIVING_FACILITY;
                string      sendingFacilityMflCode   = drugDispensed.MESSAGE_HEADER.SENDING_FACILITY;
                //check if facility exists
                LookupFacility recieverfacility = facilityLookup.GetFacility(receivingFacilityMflCode);
                LookupFacility senderfacility   = facilityLookup.GetFacility(sendingFacilityMflCode);
                if (recieverfacility == null)
                {
                    return(Msg = $"The facility {receivingFacilityMflCode} does not exist");
                }
                if (senderfacility == null)
                {
                    return(Msg = $"The facility {sendingFacilityMflCode} does not exist");
                }

                if (recieverfacility.FacilityID != senderfacility.FacilityID)
                {
                    return(Msg = "The sending facility is not the same as the receiving facility!");
                }

                //check if it is the right facility
                LookupFacility thisFacility = facilityLookup.GetFacility();
                if (thisFacility == null)
                {
                    return(Msg = "Facility not found");
                }
                if (recieverfacility.FacilityID != thisFacility.FacilityID)
                {
                    return(Msg = $"This message belongs to {receivingFacilityMflCode}, not this facility {thisFacility.MFLCode}!");
                }
                var patientLookup = new PatientLookupManager();
                //check patient
                var identifier = drugDispensed.PATIENT_IDENTIFICATION.INTERNAL_PATIENT_ID.FirstOrDefault(n => n.IDENTIFIER_TYPE == "CCC_NUMBER");
                if (identifier == null)
                {
                    return(Msg = "Message does not contain a CCC number!");
                }
                var patient = patientLookup.GetPatientByCccNumber(identifier.ID);
                if (patient == null)
                {
                    return(Msg = "Patient could not be found!");
                }
                //check pharmacy order exists
                int orderId       = Convert.ToInt32(drugDispensed.COMMON_ORDER_DETAILS.PLACER_ORDER_NUMBER.NUMBER);
                var pharmacyOrder = _pharmacyOrderManager.GetPharmacyOrder(orderId);
                if (pharmacyOrder == null)
                {
                    return(Msg = "Pharmacy Order could not be found!");
                }
                var orderedDrugs = _pharmacyDispenseManager.GetByPharmacyOrderId(pharmacyOrder.ptn_pharmacy_pk);
                if (orderedDrugs != null)
                {
                    var newDispensedDrugs = orderedDrugs;
                    foreach (var drug in newDispensedDrugs)
                    {
                        //todo refactor to use drug codes and possibly drug ids to be included in the message
                        var               drugFind         = _drugManager.GetDrug(drug.Drug_Pk);
                        string            drugname         = drugFind.DrugName;
                        PHARMACY_DISPENSE messageDispensed = null;
                        messageDispensed = drugDispensed.PHARMACY_DISPENSE.FirstOrDefault(x => drugname.Contains(x.ACTUAL_DRUGS));
                        if (messageDispensed != null)
                        {
                            drug.DispensedQuantity = messageDispensed.QUANTITY_DISPENSED;
                            //todo get frequencyId
                            //drug.FrequencyID = messageDispensed.FREQUENCY;
                            drug.PatientInstructions = messageDispensed.DISPENSING_NOTES;
                            //todo get strength ids
                            //drug.StrengthID = messageDispensed.STRENGTH;
                            drug.Duration   = messageDispensed.DURATION;
                            drug.UpdateDate = DateTime.Now;
                            drug.UserID     = InteropUser.UserId;
                            //drug.SingleDose = Convert.ToDecimal(Regex.Replace(messageDispensed.DOSAGE, @"^[A-Za-z]+", ""));
                        }
                        try
                        {
                            _pharmacyDispenseManager.UpdatePatientPharmacyDispense(drug);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                            return(Msg = "error " + e.Message);
                        }
                    }
                }
                else
                {
                    PatientPharmacyDispense newlyDispensedDrugs = new PatientPharmacyDispense();
                    foreach (var drug in drugDispensed.PHARMACY_DISPENSE)
                    {
                        string drugNameQuery = "%" + drug.ACTUAL_DRUGS + "%";
                        var    drugFind      = _drugManager.GetDrugsByName(drugNameQuery).FirstOrDefault();
                        newlyDispensedDrugs.Drug_Pk           = drugFind.Drug_pk;
                        newlyDispensedDrugs.DispensedQuantity = drug.QUANTITY_DISPENSED;
                        newlyDispensedDrugs.Duration          = drug.DURATION;
                        //todo get frequencyId
                        //newlyDispensedDrugs.FrequencyID = drug.FREQUENCY;
                        //newlyDispensedDrugs.SingleDose = Convert.ToDecimal(Regex.Replace(drug.DOSAGE, @"^[A-Za-z]+", ""));
                        //todo get strength ids
                        //newlyDispensedDrugs.StrengthID = drug.STRENGTH;
                        newlyDispensedDrugs.PatientInstructions = drug.DISPENSING_NOTES;
                        newlyDispensedDrugs.Prophylaxis         = 0;
                    }
                    foreach (var order in drugDispensed.PHARMACY_ENCODED_ORDER)
                    {
                        newlyDispensedDrugs.OrderedQuantity     = Convert.ToInt32(order.QUANTITY_PRESCRIBED);
                        newlyDispensedDrugs.PatientInstructions = order.PRESCRIPTION_NOTES;
                    }
                    try
                    {
                        _pharmacyDispenseManager.AddPatientPharmacyDispense(newlyDispensedDrugs);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        return(Msg = "error " + e.Message);
                    }
                }
                var updatedPharmacyOrder = pharmacyOrder;
                var orderingPhysician    = drugDispensed.COMMON_ORDER_DETAILS.ORDERING_PHYSICIAN;
                updatedPharmacyOrder.OrderedByName = orderingPhysician.PREFIX + " " + orderingPhysician.FIRST_NAME +
                                                     " " + orderingPhysician.LAST_NAME;
                //todo harmonise users
                updatedPharmacyOrder.DispensedBy     = InteropUser.UserId;
                updatedPharmacyOrder.DispensedByDate = drugDispensed.MESSAGE_HEADER.MESSAGE_DATETIME;
                updatedPharmacyOrder.OrderStatus     = 2;
                string str = updatedPharmacyOrder.PharmacyNotes;
                if (str != null)
                {
                    str += " Dispensed from IL";
                }
                updatedPharmacyOrder.PharmacyNotes = str;
                try
                {
                    _pharmacyOrderManager.UpdatePharmacyOrder(updatedPharmacyOrder);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return(Msg = "error " + e.Message);
                }
                Msg = "Success";
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Msg = "error " + e.Message;
            }
            return(Msg);
        }
Пример #2
0
        public DtoDrugDispensed DrugOrderFulfilment(PharmacyDispenseEntity entity)
        {
            var internalIdentifiers  = new List <DTOIdentifier>();
            var pharmacyEncodedOrder = new List <PHARMACY_ENCODED_ORDER>();
            var pharmacyDispense     = new List <PHARMACY_DISPENSE>();

            var identify = new DTOIdentifier()
            {
                //ExternalPatientId =
                //{
                //    IdentifierValue = entity.Patientidentification.EXTERNAL_PATIENT_ID.ID,
                //    IdentifierType = entity.Patientidentification.EXTERNAL_PATIENT_ID.IDENTIFIER_TYPE,
                //    AssigningAuthority = entity.Patientidentification.EXTERNAL_PATIENT_ID.ASSIGNING_AUTHORITY
                //},
                //PatientName =
                //{
                //    FirstName = entity.Patientidentification.PATIENT_NAME.FIRST_NAME,
                //    LastName = entity.Patientidentification.PATIENT_NAME.LAST_NAME,
                //    MiddleName = entity.Patientidentification.PATIENT_NAME.MIDDLE_NAME
                //}
            };

            foreach (var identifier in internalIdentifiers)
            {
                var internalIdentity = new DTOIdentifier()
                {
                    IdentifierValue    = identifier.IdentifierValue,
                    IdentifierType     = identifier.IdentifierType,
                    AssigningAuthority = identifier.AssigningAuthority
                };

                internalIdentifiers.Add(internalIdentity);
            }

            foreach (var pharmacyEncorder in pharmacyEncodedOrder)
            {
                var encorder = new PHARMACY_ENCODED_ORDER()
                {
                    DRUG_NAME           = pharmacyEncorder.DRUG_NAME,
                    CODING_SYSTEM       = pharmacyEncorder.CODING_SYSTEM,
                    STRENGTH            = pharmacyEncorder.STRENGTH,
                    DOSAGE              = pharmacyEncorder.DOSAGE,
                    FREQUENCY           = pharmacyEncorder.FREQUENCY,
                    DURATION            = pharmacyEncorder.DURATION,
                    QUANTITY_PRESCRIBED = pharmacyEncorder.QUANTITY_PRESCRIBED,
                    PRESCRIPTION_NOTES  = pharmacyEncorder.PRESCRIPTION_NOTES,
                };
                pharmacyEncodedOrder.Add(encorder);
            }

            foreach (var drugDispense in entity.PHARMACY_DISPENSE)
            {
                var dispense = new PHARMACY_DISPENSE()
                {
                    ACTUAL_DRUGS       = drugDispense.ACTUAL_DRUGS,
                    CODING_SYSTEM      = drugDispense.CODING_SYSTEM,
                    DISPENSING_NOTES   = drugDispense.DISPENSING_NOTES,
                    DOSAGE             = drugDispense.DOSAGE,
                    DRUG_NAME          = drugDispense.DRUG_NAME,
                    DURATION           = drugDispense.DURATION,
                    FREQUENCY          = drugDispense.FREQUENCY,
                    QUANTITY_DISPENSED = drugDispense.QUANTITY_DISPENSED
                };
                pharmacyDispense.Add(dispense);
            }

            var dispenseOrder = new DtoDrugDispensed()
            {
                MESSAGE_HEADER =
                {
                    SENDING_FACILITY      = entity.MESSAGE_HEADER.SENDING_FACILITY,
                    SENDING_APPLICATION   = entity.MESSAGE_HEADER.SENDING_APPLICATION,
                    RECEIVING_APPLICATION = entity.MESSAGE_HEADER.RECEIVING_APPLICATION,
                    RECEIVING_FACILITY    = entity.MESSAGE_HEADER.RECEIVING_FACILITY,
                    SECURITY      = entity.MESSAGE_HEADER.SECURITY,
                    MESSAGE_TYPE  = entity.MESSAGE_HEADER.MESSAGE_TYPE,
                    PROCESSING_ID = entity.MESSAGE_HEADER.PROCESSING_ID
                },
                PATIENT_IDENTIFICATION =
                {
                    EXTERNAL_PATIENT_ID     =
                    {
                        ASSIGNING_AUTHORITY = entity.PATIENT_IDENTIFICATION.EXTERNAL_PATIENT_ID.ASSIGNING_AUTHORITY,
                        ID                  = entity.PATIENT_IDENTIFICATION.EXTERNAL_PATIENT_ID.ID,
                        IDENTIFIER_TYPE     = entity.PATIENT_IDENTIFICATION.EXTERNAL_PATIENT_ID.IDENTIFIER_TYPE
                    },
                    //InternalPatientId = internalIdentifiers,
                    PATIENT_NAME            =
                    {
                        FIRST_NAME  = entity.PATIENT_IDENTIFICATION.PATIENT_NAME.FIRST_NAME,
                        MIDDLE_NAME = entity.PATIENT_IDENTIFICATION.PATIENT_NAME.MIDDLE_NAME,
                        LAST_NAME   = entity.PATIENT_IDENTIFICATION.PATIENT_NAME.LAST_NAME
                    }
                },
                COMMON_ORDER_DETAILS =
                {
                    ORDER_CONTROL      = entity.COMMON_ORDER_DETAILS.ORDER_CONTROL,
                    NOTES              = entity.COMMON_ORDER_DETAILS.NOTES,
                    ORDERING_PHYSICIAN =
                    {
                        FIRST_NAME  = entity.COMMON_ORDER_DETAILS.ORDERING_PHYSICIAN.FIRST_NAME,
                        MIDDLE_NAME = entity.COMMON_ORDER_DETAILS.ORDERING_PHYSICIAN.MIDDLE_NAME,
                        LAST_NAME   = entity.COMMON_ORDER_DETAILS.ORDERING_PHYSICIAN.LAST_NAME
                    },
                    ORDER_STATUS        = entity.COMMON_ORDER_DETAILS.ORDER_STATUS,
                    PLACER_ORDER_NUMBER =
                    {
                        NUMBER = entity.COMMON_ORDER_DETAILS.PLACER_ORDER_NUMBER.NUMBER,
                        ENTITY = entity.COMMON_ORDER_DETAILS.PLACER_ORDER_NUMBER.ENTITY
                    },
                    FILLER_ORDER_NUMBER  =
                    {
                        NUMBER = entity.COMMON_ORDER_DETAILS.PLACER_ORDER_NUMBER.NUMBER,
                        ENTITY = entity.COMMON_ORDER_DETAILS.PLACER_ORDER_NUMBER.ENTITY
                    },
                    TRANSACTION_DATETIME = Convert.ToDateTime(entity.COMMON_ORDER_DETAILS.TRANSACTION_DATETIME),
                },
                PHARMACY_ENCODED_ORDER = pharmacyEncodedOrder,
                PHARMACY_DISPENSE      = pharmacyDispense,
                //  PH = drugsDispensed
            };

            return(dispenseOrder);
        }