void AddBedToWard(Bed bed, Ward ward) { if (ward != null && ward.Beds.FindIndex(x => x.PatientId == bed.PatientId && x.VisitId == bed.VisitId && x.FromDate == bed.FromDate && x.CampusId == bed.CampusId && x.WardId == bed.WardId && x.RoomId == bed.RoomId && x.BedId == bed.BedId) == -1) { if (log.IsInfoEnabled) log.InfoFormat("Ward {0}: added bed {1}-{2}-{3}-{4} fromDate '{5}' for visit {6}", ward.Id, bed.CampusId, bed.WardId, bed.RoomId, bed.BedId, bed.FromDate.Maybe(d => d.Value.ToString("yyyy-MM-dd HH:mm")), bed.VisitId); ward.Beds.Add(bed); } }
async Task ProcessBedOccupation() { if (!string.IsNullOrEmpty(transfer.DepartmentId) && skippedDepartments.Contains(transfer.DepartmentId)) return; //DO NOT STORE BED OCCUPATION FOR SKIPPED DEPARTMENTS bed = new Bed(transfer, patient, visit) { ConsultingDoctorLastName = consultingDoctorLastName, ConsultingDoctorFirstName = consultingDoctorFirstName }; if (!string.IsNullOrEmpty(transfer.WardId)) assignedWard = await GetWard(transfer.CampusId, transfer.WardId).ConfigureAwait(false); if (!string.IsNullOrEmpty(transfer.PriorWardId)) { if (transfer.WardId == transfer.PriorWardId) priorWard = assignedWard; else priorWard = await GetWard(transfer.PriorCampusId, transfer.PriorWardId).ConfigureAwait(false); } switch (eventType) { case "A01": //Admit a patient case "A04": //Register a patient case "A14": //Pending admit await RemoveVisitFromAllWard(); AddBedToWard(bed, assignedWard); break; case "A02": //Transfer a patient if (visit.DischargeDate.HasValue && !visit.PendingDischarge.GetValueOrDefault() && visit.DischargeDate.Value > bed.FromDate.GetValueOrDefault(DateTime.Now)) break; //transfer before dichargedate if (visit.DischargeDate.HasValue && !visit.PendingDischarge.GetValueOrDefault() && visit.DischargeDate.Value < bed.FromDate.GetValueOrDefault(DateTime.Now).AddMonths(-1)) break; //discharged more than 1 month ago if (visit.Transfers != null) { var lastFromDate = (from t in visit.Transfers where t.FromDate < DateTime.Now && !t.Pending.GetValueOrDefault() orderby t.FromDate descending select t.FromDate).FirstOrDefault(); if (bed.FromDate < lastFromDate) { assignedWard = null; // transfer in the past break; } } if (bed.LocationStatus != "1" && bed.LocationStatus != "2" && visit.Transfers != null) //back to original bed!! { foreach (var t in visit.Transfers) { if (string.IsNullOrEmpty(t.WardId) || t.Pending.GetValueOrDefault() || t.FromDate > DateTime.Now) continue; var ward = await httpClient.GetAsync<Ward>(string.Format(wardUri, string.Concat(t.CampusId, "-", t.WardId))).ConfigureAwait(false); if (ward != null) { RemoveBedFromWard(t, ward, checkFromDate: false, removePendingDischarge: false); await StoreWard(ward).ConfigureAwait(false); } } priorWard = null; } if (!visit.PendingDischarge.GetValueOrDefault() && visit.DischargeDate.HasValue && visit.DischargeDate <= DateTime.Now) assignedWard = null; AddBedToWard(bed, assignedWard); break; case "A15": //Pending transfer AddBedToWard(bed, assignedWard); break; case "A03": //Discharge a patient await RemoveVisitFromAllWard(); RemoveBedFromWard(transfer, assignedWard, checkFromDate: false); break; case "A05": //Preadmit a patient break; case "A08": //Update patient information break; case "A11": //Cancel admit case "A27": //Cancel pending admit RemoveBedFromWard(transfer, assignedWard, checkFromDate: false); break; case "A12": //Cancel transfer if (visit.DischargeDate.HasValue && !visit.PendingDischarge.GetValueOrDefault() && visit.DischargeDate.Value > bed.FromDate.GetValueOrDefault(DateTime.Now)) break; //transfer before dichargedate if (visit.DischargeDate.HasValue && !visit.PendingDischarge.GetValueOrDefault() && visit.DischargeDate.Value < bed.FromDate.GetValueOrDefault(DateTime.Now).AddMonths(-1)) break; //discharged more than 1 month ago RemoveBedFromWard(transfer, assignedWard); if (visit.Transfers != null) { var lastFromDate = (from t in visit.Transfers where t.FromDate < DateTime.Now orderby t.FromDate descending select t.FromDate).FirstOrDefault(); if (bed.FromDate < lastFromDate) { assignedWard = null; // transfer in the past break; } } if (!visit.PendingDischarge.GetValueOrDefault() && visit.DischargeDate.HasValue && visit.DischargeDate <= DateTime.Now) { //discharged in the past assignedWard = null; break; } if (visit.Transfers != null && visit.Transfers.Count > 1) { //more than 1 transferline bed.CampusId = bed.PriorCampusId; bed.WardId = bed.PriorWardId; bed.RoomId = bed.PriorRoomId; bed.BedId = bed.PriorBedId; bed.LocationStatus = bed.PriorLocationStatus; bed.PriorCampusId = null; bed.PriorWardId = null; bed.PriorRoomId = null; bed.PriorBedId = null; bed.PriorLocationStatus = null; bed.FromDate = (from t in visit.Transfers where t.FromDate < bed.FromDate orderby t.FromDate descending select t.FromDate).FirstOrDefault() ?? bed.FromDate; AddBedToWard(bed, priorWard); } break; case "A26": //Cancel pending transfer RemoveBedFromWard(transfer, assignedWard); //check if next visit is pending discharge and has the same bed // THAN change bed to previous bed break; case "A13": //Cancel discharge if (visit.Transfers != null) { var lastFromDate = (from t in visit.Transfers where t.FromDate < bed.FromDate orderby t.FromDate descending select t.FromDate).FirstOrDefault(); if (lastFromDate.HasValue) bed.FromDate = lastFromDate; AddBedToWard(bed, assignedWard); } bed.DischargeDate = null; bed.PendingDischarge = null; break; case "A25": //Cancel pending discharge await RemoveDischargeDateFromAllBeds(visit.Id); RemoveBedFromWard(transfer, assignedWard, checkLocation: false); break; case "A16": //Pending discharge bed.PendingDischarge = true; AddBedToWard(bed, assignedWard); break; case "A24": //Link Patient Information break; case "A28": //Add person information break; case "A31": //Update person information break; case "A37": //Unlink Patient Information break; case "A38": //Cancel preadmit break; case "A40": //Merge patient break; default: break; } await StoreWard(assignedWard); if (priorWard != null && assignedWard != priorWard) await StoreWard(priorWard); }