добавление возможности создания отчета по движению денег

This commit is contained in:
xom9kxom9k 2024-11-29 19:46:50 +04:00
parent fbfcd15ce7
commit ee1c7039da
4 changed files with 403 additions and 26 deletions

View File

@ -0,0 +1,186 @@
namespace ProjectFamilyBudget.Forms
{
partial class FormMoneyReport
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
label1 = new Label();
label2 = new Label();
label3 = new Label();
label4 = new Label();
label5 = new Label();
textBoxFilePath = new TextBox();
buttonSelectFilePath = new Button();
comboBoxSelectIncome = new ComboBox();
comboBoxSelectExpense = new ComboBox();
dateTimePickerStart = new DateTimePicker();
dateTimePickerEnd = new DateTimePicker();
buttonMakeReport = new Button();
SuspendLayout();
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(21, 19);
label1.Name = "label1";
label1.Size = new Size(87, 15);
label1.TabIndex = 0;
label1.Text = "Путь до файла";
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(21, 64);
label2.Name = "label2";
label2.Size = new Size(41, 15);
label2.TabIndex = 1;
label2.Text = "Доход";
//
// label3
//
label3.AutoSize = true;
label3.Location = new Point(21, 111);
label3.Name = "label3";
label3.Size = new Size(45, 15);
label3.TabIndex = 2;
label3.Text = "Расход";
//
// label4
//
label4.AutoSize = true;
label4.Location = new Point(21, 164);
label4.Name = "label4";
label4.Size = new Size(74, 15);
label4.TabIndex = 3;
label4.Text = "Дата начала";
//
// label5
//
label5.AutoSize = true;
label5.Location = new Point(21, 205);
label5.Name = "label5";
label5.Size = new Size(68, 15);
label5.TabIndex = 4;
label5.Text = "Дата конца";
//
// textBoxFilePath
//
textBoxFilePath.Location = new Point(137, 16);
textBoxFilePath.Name = "textBoxFilePath";
textBoxFilePath.Size = new Size(152, 23);
textBoxFilePath.TabIndex = 5;
//
// buttonSelectFilePath
//
buttonSelectFilePath.Location = new Point(295, 16);
buttonSelectFilePath.Name = "buttonSelectFilePath";
buttonSelectFilePath.Size = new Size(25, 23);
buttonSelectFilePath.TabIndex = 6;
buttonSelectFilePath.Text = "..";
buttonSelectFilePath.UseVisualStyleBackColor = true;
buttonSelectFilePath.Click += buttonSelectFilePath_Click;
//
// comboBoxSelectIncome
//
comboBoxSelectIncome.FormattingEnabled = true;
comboBoxSelectIncome.Location = new Point(137, 61);
comboBoxSelectIncome.Name = "comboBoxSelectIncome";
comboBoxSelectIncome.Size = new Size(183, 23);
comboBoxSelectIncome.TabIndex = 7;
//
// comboBoxSelectExpense
//
comboBoxSelectExpense.FormattingEnabled = true;
comboBoxSelectExpense.Location = new Point(137, 108);
comboBoxSelectExpense.Name = "comboBoxSelectExpense";
comboBoxSelectExpense.Size = new Size(183, 23);
comboBoxSelectExpense.TabIndex = 8;
//
// dateTimePickerStart
//
dateTimePickerStart.Location = new Point(137, 158);
dateTimePickerStart.Name = "dateTimePickerStart";
dateTimePickerStart.Size = new Size(183, 23);
dateTimePickerStart.TabIndex = 9;
//
// dateTimePickerEnd
//
dateTimePickerEnd.Location = new Point(137, 199);
dateTimePickerEnd.Name = "dateTimePickerEnd";
dateTimePickerEnd.Size = new Size(183, 23);
dateTimePickerEnd.TabIndex = 10;
//
// buttonMakeReport
//
buttonMakeReport.Location = new Point(103, 237);
buttonMakeReport.Name = "buttonMakeReport";
buttonMakeReport.Size = new Size(138, 23);
buttonMakeReport.TabIndex = 11;
buttonMakeReport.Text = "Сформировать";
buttonMakeReport.UseVisualStyleBackColor = true;
buttonMakeReport.Click += buttonMakeReport_Click;
//
// FormMoneyReport
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(343, 282);
Controls.Add(buttonMakeReport);
Controls.Add(dateTimePickerEnd);
Controls.Add(dateTimePickerStart);
Controls.Add(comboBoxSelectExpense);
Controls.Add(comboBoxSelectIncome);
Controls.Add(buttonSelectFilePath);
Controls.Add(textBoxFilePath);
Controls.Add(label5);
Controls.Add(label4);
Controls.Add(label3);
Controls.Add(label2);
Controls.Add(label1);
Name = "FormMoneyReport";
StartPosition = FormStartPosition.CenterParent;
Text = "Отчет по движению денег";
ResumeLayout(false);
PerformLayout();
}
#endregion
private Label label1;
private Label label2;
private Label label3;
private Label label4;
private Label label5;
private TextBox textBoxFilePath;
private Button buttonSelectFilePath;
private ComboBox comboBoxSelectIncome;
private ComboBox comboBoxSelectExpense;
private DateTimePicker dateTimePickerStart;
private DateTimePicker dateTimePickerEnd;
private Button buttonMakeReport;
}
}

View File

@ -0,0 +1,83 @@
using ProjectFamilyBudget.Reports;
using ProjectFamilyBudget.Repositories;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Unity;
namespace ProjectFamilyBudget.Forms
{
public partial class FormMoneyReport : Form
{
private readonly IUnityContainer _container;
public FormMoneyReport(IUnityContainer container, IIncome income, IExpense expense)
{
InitializeComponent();
_container = container ?? throw new ArgumentNullException(nameof(container));
comboBoxSelectIncome.DataSource = income.ReadIncome();
comboBoxSelectIncome.DisplayMember = "Name";
comboBoxSelectIncome.ValueMember = "Id";
comboBoxSelectExpense.DataSource = expense.ReadExpense();
comboBoxSelectExpense.DisplayMember = "Name";
comboBoxSelectExpense.ValueMember = "Id";
}
private void buttonSelectFilePath_Click(object sender, EventArgs e)
{
var sfd = new SaveFileDialog()
{
Filter = "Excel Files | *.xlsx"
};
if (sfd.ShowDialog() != DialogResult.OK)
{
return;
}
textBoxFilePath.Text = sfd.FileName;
}
private void buttonMakeReport_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(textBoxFilePath.Text))
{
throw new Exception("Отсутствует имя файла для отчета");
}
if (comboBoxSelectIncome.SelectedIndex < 0 || comboBoxSelectExpense.SelectedIndex < 0)
{
throw new Exception("Не выбран корм");
}
if (dateTimePickerEnd.Value <= dateTimePickerStart.Value)
{
throw new Exception("Дата начала должна быть раньше даты окончания");
}
if (_container.Resolve<TableReport>().CreateTable(textBoxFilePath.Text, (int)comboBoxSelectIncome.SelectedValue!, (int)comboBoxSelectExpense.SelectedValue!, dateTimePickerStart.Value, dateTimePickerEnd.Value))
{
MessageBox.Show("Документ сформирован",
"Формирование документа",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Возникли ошибки при формировании документа.Подробности в логах",
"Формирование документа",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при создании очета",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -13,21 +13,21 @@ public class TableReport
private readonly IPeopleIncome _peopleIncomeRepository;
private readonly IPeopleExpense _peopleExpenseRepository;
private readonly ILogger<TableReport> _logger;
internal static readonly string[] item = ["Человек", "Дата", "Заработано", "Потрачено"];
internal static readonly string[] item = ["Название операции", "Дата", "Заработано", "Потрачено"];
public TableReport(IPeopleIncome peopleIncomeRepository, IPeopleExpense peopleExpenseRepository, ILogger<TableReport> logger)
{
_peopleIncomeRepository = peopleIncomeRepository ?? throw new ArgumentNullException(nameof(peopleIncomeRepository));
_peopleExpenseRepository = peopleExpenseRepository ?? throw new ArgumentNullException(nameof(peopleExpenseRepository));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public bool CreateTable(string filePath, int peopleIncomeId, DateTime startDate, DateTime endDate)
public bool CreateTable(string filePath, int incomeId, int expenseId, DateTime startDate, DateTime endDate)
{
try
{
new ExcelBuilder(filePath)
.AddHeader("Сводка по движению денег", 0, 3)
.AddParagraph("за период", 0)
.AddTable([10, 15, 15], GetData(sumId, startDate, endDate))
.AddTable([15, 10, 15, 15], GetData(incomeId, expenseId, startDate, endDate))
.Build();
return true;
}
@ -37,36 +37,24 @@ public class TableReport
return false;
}
}
private List<string[]> GetData(int peopleIncomeId, DateTime startDate, DateTime endDate)
private List<string[]> GetData(int incomeId,int expenseId, DateTime startDate, DateTime endDate)
{
var data = _peopleIncomeRepository
.ReadPeopleIncome()
.Where(x => x.DataReciept >= startDate && x.DataReciept <= endDate && x.IncomePeopleIncomes.Any(y => y.PeopleIncomeId == peopleIncomeId))
.Select(x => new { x.PeopleId, Date = x.DataReciept, CountIn = x.IncomePeopleIncomes.FirstOrDefault(y => y.PeopleIncomeId == peopleIncomeId)?.Count, CountOut = (int?)null })
.Union(
_peopleExpenseRepository
.ReadPeopleExpense()
.Where(x => x.DataReciept >= startDate && x.DataReciept <= endDate && x.ExpensePeopleExpenses.Any(y => y.PeopleExpenseId == fuelId))
.Select(x => new { Date = x.SaleDate, CountIn = (int?)null, CountOut = x.FuelFuelSale.FirstOrDefault(y => y.FuelId == fuelId)?.Quantity })
)
.OrderBy(x => x.Date);
var groupedData = data
.GroupBy(x => x.Date)
.Select(g => new
{
Date = g.Key,
TotalIn = g.Sum(x => x.CountIn),
TotalOut = g.Sum(x => x.CountOut)
})
.ReadPeopleIncome()
.Where(x => x.DataReciept >= startDate && x.DataReciept <= endDate && x.IncomePeopleIncomes.Any(y => y.IncomeId == incomeId))
.Select(x => new { x.PeopleId, Date = x.DataReciept, CountIn = x.IncomePeopleIncomes.FirstOrDefault(y => y.IncomeId == incomeId)?.Sum, CountOut = (int?)null })
.Union(
_peopleExpenseRepository
.ReadPeopleExpense()
.Where(x => x.DataReciept >= startDate && x.DataReciept <= endDate && x.ExpensePeopleExpenses.Any(y => y.ExpenseId == expenseId))
.Select(x => new { x.PeopleId, Date = x.DataReciept, CountIn = (int?)null, CountOut = x.ExpensePeopleExpenses.FirstOrDefault(predicate: y => y.ExpenseId == expenseId)?.Sum }))
.OrderBy(x => x.Date);
return
new List<string[]>() { item }
.Union(groupedData
.Select(x => new string[] { x.Date.ToString("dd.MM.yyyy"), x.TotalIn.ToString()!, x.TotalOut.ToString()! }))
.Union(data.Select(x => new string[] {x.PeopleId.ToString(), x.Date.ToString(), x.CountIn?.ToString() ?? string.Empty,x.CountOut?.ToString() ?? string.Empty }))
.Union(
new[] { new string[] { "Всего", groupedData.Sum(x => x.TotalIn).ToString()!, groupedData.Sum(x => x.TotalOut).ToString()! } }
[[ "Всего", "", data.Sum(x => x.CountIn ?? 0).ToString(), data.Sum(x => x.CountOut ?? 0).ToString() ]]
)
.ToList();
}