Exemplo n.º 1
0
        private async void CloseAdmissionBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var requestAdmission = new Admission
                {
                    Id            = selectedAdmission.Id,
                    DischargeDate = DateTime.Now
                };

                string request = tcpService.SerializeCloseAdmission(requestAdmission, SingletoneObj.User);
                byte[] data    = await tcpService.CodeStreamAsync(request);

                await SingletoneObj.Stream.WriteAsync(data, 0, data.Length);

                string response = await tcpService.DecodeStreamAsync(SingletoneObj.Stream);

                var responseArgs = response.Split(';');
                if (responseArgs.Length > 1 && responseArgs[0].Contains("500"))
                {
                    throw new ArgumentException(responseArgs[1]);
                }
                if (responseArgs.Length == 1 && responseArgs[0].Equals("200"))
                {
                    selectedAdmission.DischargeDate = requestAdmission.DischargeDate;
                    DischargeDate.Content           = requestAdmission.DischargeDate.ToString();
                    SingletoneObj.DischargeDate     = requestAdmission.DischargeDate;
                    IsClosed = true;
                    var directory = Directory.GetCurrentDirectory() + @"\ClosedAdmissions";
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }
                    var    patientName = selectedAdmission.Patient?.FullName.Replace(' ', '_') ?? Guid.NewGuid().ToString().Replace(' ', '_');
                    string path        = Directory.GetCurrentDirectory() + @"\ClosedAdmissions\" + patientName + selectedAdmission.DischargeDate.ToString().Replace(' ', '_').Replace(':', '_') + ".txt";
                    using (var stream = new StreamWriter(path))
                    {
                        string patient       = "Пацієнт: " + selectedAdmission.Patient?.FullName ?? "noname";
                        string doctor        = "Лікар: " + selectedAdmission.Doctor?.FullName ?? "noname";
                        string ward          = "Палата: " + selectedAdmission.Ward?.WardNumber.ToString() ?? "none";
                        string diagosis      = "Діагноз: " + selectedAdmission.Diagnosis ?? "none";
                        string dateOfReceipt = "Дата надходження: " + selectedAdmission.DateOfReceipt.ToString();
                        string dischargeDate = "Дата виписки: " + selectedAdmission.DischargeDate.ToString();
                        string medicines     = "Ліки: " + string.Join("   ... , ...  ", selectedAdmission.Medisines.Select(el => el.Name));
                        string price         = "Ціна: " + selectedAdmission.Medisines.Sum(el => el.Price).ToString() + " грн.";
                        stream.WriteLine(patient);
                        stream.WriteLine(doctor);
                        stream.WriteLine(ward);
                        stream.WriteLine(diagosis);
                        stream.WriteLine(dateOfReceipt);
                        stream.WriteLine(dischargeDate);
                        stream.WriteLine(medicines);
                        stream.WriteLine(price);
                    }
                }

                this.Close();
            }
            catch (Exception ex)
            {
                SingletoneObj.DischargeDate = null;
                StatusLabel.Content         = "Status: " + ex.Message;
            }
            finally
            {
                ExitBtn.IsEnabled           = true;
                MoveMedicineBtn.IsEnabled   = true;
                CloseAdmissionBtn.IsEnabled = !IsClosed;
            }
        }