This commit is contained in:
parent 54b4e11a2f
commit 5def95e150
2 changed files with 28 additions and 34 deletions

View File

@ -28,7 +28,6 @@ namespace ProjectPolyclinic.Forms
comboBoxDoctor.DisplayMember = "Last_Name"; comboBoxDoctor.DisplayMember = "Last_Name";
comboBoxDoctor.ValueMember = "Id"; comboBoxDoctor.ValueMember = "Id";
} }
private void buttonBuild_Click(object sender, EventArgs e) private void buttonBuild_Click(object sender, EventArgs e)
{ {
try try
@ -40,7 +39,7 @@ namespace ProjectPolyclinic.Forms
if (comboBoxDoctor.SelectedIndex < 0) if (comboBoxDoctor.SelectedIndex < 0)
{ {
throw new Exception("Не выбран спортсмен"); throw new Exception("Не выбран врач");
} }
if (dateTimePickerDateEnd.Value <= dateTimePickerDateBegin.Value) if (dateTimePickerDateEnd.Value <= dateTimePickerDateBegin.Value)
@ -48,7 +47,15 @@ namespace ProjectPolyclinic.Forms
throw new Exception("Дата начала должна быть раньше даты окончания"); throw new Exception("Дата начала должна быть раньше даты окончания");
} }
if (_container.Resolve<TableReport>().CreateTable(textBoxFilePath.Text, (int)comboBoxDoctor.SelectedValue!, dateTimePickerDateBegin.Value, dateTimePickerDateEnd.Value)) // Приведение дат к началу и концу месяца
var startDate = new DateTime(dateTimePickerDateBegin.Value.Year, dateTimePickerDateBegin.Value.Month, 1);
var endDate = new DateTime(dateTimePickerDateEnd.Value.Year, dateTimePickerDateEnd.Value.Month, 1).AddMonths(1).AddDays(-1);
if (_container.Resolve<TableReport>().CreateTable(
textBoxFilePath.Text,
(int)comboBoxDoctor.SelectedValue!,
startDate,
endDate))
{ {
MessageBox.Show("Документ сформирован", "Формирование документа", MessageBox.Show("Документ сформирован", "Формирование документа",
MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBoxButtons.OK, MessageBoxIcon.Information);
@ -61,11 +68,12 @@ namespace ProjectPolyclinic.Forms
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show(ex.Message, "Ошибка при создании очета", MessageBox.Show(ex.Message, "Ошибка при создании отчета",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
private void buttonSelectFilePath_Click(object sender, EventArgs e) private void buttonSelectFilePath_Click(object sender, EventArgs e)
{ {
var sfd = new SaveFileDialog() var sfd = new SaveFileDialog()

View File

@ -29,21 +29,27 @@ public class TableReport
throw new ArgumentNullException(nameof(logger)); throw new ArgumentNullException(nameof(logger));
} }
public bool CreateTable(string filePath, int doctorId, DateTime startDate, DateTime endDate) public bool CreateTable(string filePath, int doctorId, DateTime startDate, DateTime endDate)
{ {
try try
{ {
// Приведение дат к началу и концу месяцев
var startOfMonth = new DateTime(startDate.Year, startDate.Month, 1);
var endOfMonth = new DateTime(endDate.Year, endDate.Month, 1).AddMonths(1).AddDays(-1);
var excelBuilder = new ExcelBuilder(filePath) var excelBuilder = new ExcelBuilder(filePath)
.AddHeader("Сводка по оплате", 0, 3) .AddHeader("Сводка по оплате", 0, 3)
.AddParagraph($"За период с {startDate:MM.yyyy} по {endDate:MM.yyyy}", 0) .AddParagraph($"За период с {startOfMonth:MM.yyyy} по {endOfMonth:MM.yyyy}", 0)
.AddParagraph($"Id врача: {doctorId}", 0) .AddParagraph($"Id врача: {doctorId}", 0)
.AddTable(new[] { 25, 25, 25 }, GetData(doctorId, startDate, endDate)); .AddTable(new[] { 25, 25, 25 }, GetData(doctorId, startOfMonth, endOfMonth));
excelBuilder.AddParagraph("", 0); excelBuilder.AddParagraph("", 0);
excelBuilder excelBuilder
.AddHeader("Назначенные лекарства", 0, 2) .AddHeader("Назначенные лекарства", 0, 2)
.AddTable(new[] { 25, 25 }, GetDrugData(doctorId, startDate, endDate)); .AddTable(new[] { 25, 25 }, GetDrugData(doctorId, startOfMonth, endOfMonth));
excelBuilder.Build(); excelBuilder.Build();
return true; return true;
@ -56,21 +62,13 @@ public class TableReport
} }
private List<string[]> GetData(int doctorId, DateTime startOfMonth, DateTime endOfMonth)
private List<string[]> GetData(int doctorId, DateTime startDate, DateTime endDate)
{ {
//перевод даты к началу и концу месяца
var startOfMonth = new DateTime(startDate.Year, startDate.Month, 1);
var endOfMonth = new DateTime(endDate.Year, endDate.Month, 1).AddMonths(1).AddDays(-1);
var data = _doctorPayRepository var data = _doctorPayRepository
.ReadDoctorPayments() .ReadDoctorPayments()
.Where(x => .Where(x =>
{ {
var paymentMonth = DateTime.ParseExact(x.Month, "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture); var paymentMonth = DateTime.ParseExact(x.Month, "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture);
return paymentMonth >= startOfMonth && paymentMonth <= endOfMonth && x.IdDoctor == doctorId; return paymentMonth >= startOfMonth && paymentMonth <= endOfMonth && x.IdDoctor == doctorId;
}) })
.Select(x => new .Select(x => new
@ -105,24 +103,17 @@ public class TableReport
return result; return result;
} }
private List<string[]> GetDrugData(int doctorId, DateTime startOfMonth, DateTime endOfMonth)
private List<string[]> GetDrugData(int doctorId, DateTime startDate, DateTime endDate)
{ {
var startOfMonth = new DateTime(startDate.Year, startDate.Month, 1);
var endOfMonth = new DateTime(endDate.Year, endDate.Month, 1).AddMonths(1).AddDays(-1);
var medicalHistories = _medicalHistoryRepository var medicalHistories = _medicalHistoryRepository
.ReadMedicalHistory(startOfMonth, endOfMonth, null, doctorId) .ReadMedicalHistory(startOfMonth, endOfMonth, null, doctorId)
.Where(mh => mh.DoctorId == doctorId && mh.VisitDate >= startDate && mh.VisitDate <= endDate) .Where(mh => mh.DoctorId == doctorId && mh.VisitDate >= startOfMonth && mh.VisitDate <= endOfMonth)
.ToList(); .ToList();
var result = new List<string[]> var result = new List<string[]>
{ {
new[] { "Дата", "Назначенные лекарства" } new[] { "Дата", "Назначенные лекарства" }
}; };
var sortedDrugData = medicalHistories var sortedDrugData = medicalHistories
.Where(mh => mh.DrugMedHistory != null && mh.DrugMedHistory.Any()) .Where(mh => mh.DrugMedHistory != null && mh.DrugMedHistory.Any())
@ -139,17 +130,12 @@ public class TableReport
{ {
item.Date.ToString("dd.MM.yyyy"), item.Date.ToString("dd.MM.yyyy"),
item.Count.ToString() item.Count.ToString()
}) }));
);
var totalDrugs = sortedDrugData.Sum(x => x.Count); var totalDrugs = sortedDrugData.Sum(x => x.Count);
result.Add(new[] { "Всего", totalDrugs.ToString() }); result.Add(new[] { "Всего", totalDrugs.ToString() });
return result; return result;
} }
} }