lab2 super final

This commit is contained in:
2025-12-08 00:26:46 +04:00
parent 6cf0f868c0
commit d62f8368dc
11 changed files with 301 additions and 119 deletions

View File

@@ -11,8 +11,8 @@ public class Assemblies
public decimal SellingPrice { get; private set; }
public AssemblyStatus Status { get; private set; }
public static Assemblies CreateEntity(int id, string assemblyNumber, DateTime assemblyDate, decimal costPrice, AssemblyStatus status, decimal sellingPrice)
public static Assemblies CreateEntity(int id, string assemblyNumber, DateTime assemblyDate,
decimal costPrice, decimal sellingPrice, AssemblyStatus status)
{
return new Assemblies
{
@@ -20,6 +20,7 @@ public class Assemblies
AssemblyNumber = assemblyNumber,
AssemblyDate = assemblyDate,
CostPrice = costPrice,
SellingPrice = sellingPrice,
Status = status
};
}

View File

@@ -3,12 +3,16 @@
public class Sales
{
public int Id { get; private set; }
public int? AssemblyId { get; private set; }
public int? DeviceId { get; private set; }
public DateTime SaleDate { get; private set; }
public decimal TotalAmount { get; private set; }
public string SaleType { get; private set; }
public string SaleType { get; private set; } = string.Empty;
public static Sales CreateEntity(
int id,
int? assemblyId,
int? deviceId,
DateTime saleDate,
decimal totalAmount,
string saleType)
@@ -16,9 +20,11 @@
return new Sales
{
Id = id,
AssemblyId = assemblyId,
DeviceId = deviceId,
SaleDate = saleDate,
TotalAmount = totalAmount,
SaleType = saleType
SaleType = saleType ?? string.Empty
};
}
}

View File

@@ -27,7 +27,10 @@ namespace ProjectComputerShop.Forms
dateTimePickerAssembly.Value = assembly.AssemblyDate;
numericCostPrice.Value = assembly.CostPrice;
numericSellingPrice.Value = assembly.SellingPrice;
comboBoxStatus.SelectedValue = assembly.Status;
// Исправлено: устанавливаем SelectedItem вместо SelectedValue
comboBoxStatus.SelectedItem = assembly.Status;
_assemblyId = value;
}
catch (Exception ex)
@@ -49,6 +52,7 @@ namespace ProjectComputerShop.Forms
_partsRepository = partsRepository ??
throw new ArgumentNullException(nameof(partsRepository));
comboBoxStatus.DataSource = Enum.GetValues(typeof(AssemblyStatus));
comboBoxStatus.DropDownStyle = ComboBoxStyle.DropDownList;
dateTimePickerAssembly.Value = DateTime.Today;
@@ -94,19 +98,17 @@ namespace ProjectComputerShop.Forms
MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e) => Close();
private Assemblies CreateAssembly(int id)
{
return Assemblies.CreateEntity(
id: id,
assemblyNumber: textBoxNumber.Text.Trim(),
assemblyDate: dateTimePickerAssembly.Value.Date,
costPrice: numericCostPrice.Value,
sellingPrice: numericSellingPrice.Value,
status: (AssemblyStatus)comboBoxStatus.SelectedValue
);
id: id,
assemblyNumber: textBoxNumber.Text.Trim(),
assemblyDate: dateTimePickerAssembly.Value.Date,
costPrice: numericCostPrice.Value,
sellingPrice: numericSellingPrice.Value,
status: (AssemblyStatus)comboBoxStatus.SelectedItem
);
}
}
}

View File

@@ -151,6 +151,7 @@
//
numericUpDownRetPrice.DecimalPlaces = 2;
numericUpDownRetPrice.Location = new Point(166, 148);
numericUpDownRetPrice.Maximum = new decimal(new int[] { 1316134912, 2328, 0, 0 });
numericUpDownRetPrice.Name = "numericUpDownRetPrice";
numericUpDownRetPrice.Size = new Size(125, 27);
numericUpDownRetPrice.TabIndex = 13;

View File

@@ -9,7 +9,7 @@ namespace ProjectComputerShop.Forms
public partial class FormPart : Form
{
private readonly IPartsRepository _repository;
private readonly ICategoriesRepository _categoriesRepository; // добавил
private readonly ICategoriesRepository _categoriesRepository;
private int? _partId;
public int Id
@@ -64,7 +64,9 @@ namespace ProjectComputerShop.Forms
buttonCancel.Click += buttonCancel_Click;
comboBoxCategory.DropDownStyle = ComboBoxStyle.DropDownList;
}
private void LoadCategories()
{

View File

@@ -10,29 +10,19 @@ namespace ProjectComputerShop.Forms
{
private readonly IUnityContainer _container;
private readonly IPartsRepository _partsRepository;
private readonly ICategoriesRepository _categoriesRepository;
public int Id { get; private set; }
public FormParts(IUnityContainer container, IPartsRepository partsRepository)
public FormParts(IUnityContainer container, IPartsRepository partsRepository, ICategoriesRepository categoriesRepository)
{
InitializeComponent();
_container = container ?? throw new ArgumentNullException(nameof(container));
_partsRepository = partsRepository ?? throw new ArgumentNullException(nameof(partsRepository));
_categoriesRepository = categoriesRepository ?? throw new ArgumentNullException(nameof(categoriesRepository));
LoadList();
}
private void FormParts_Load(object sender, EventArgs e)
{
try
{
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonAdd_Click(object sender, EventArgs e)
{
try
@@ -85,7 +75,9 @@ namespace ProjectComputerShop.Forms
private void LoadList()
{
dataGridViewParts.DataSource = _partsRepository.ReadAll().ToList();
var parts = _partsRepository.ReadAll().ToList();
dataGridViewParts.DataSource = parts;
}
private bool TryGetIdentifierFromSelectedRow(out int id)
@@ -101,4 +93,4 @@ namespace ProjectComputerShop.Forms
return true;
}
}
}
}

View File

@@ -13,88 +13,155 @@
private void InitializeComponent()
{
this.labelType = new System.Windows.Forms.Label();
this.labelAmount = new System.Windows.Forms.Label();
this.labelDate = new System.Windows.Forms.Label();
this.comboBoxSaleType = new System.Windows.Forms.ComboBox();
this.numericUpDownTotal = new System.Windows.Forms.NumericUpDown();
this.dateTimePickerSale = new System.Windows.Forms.DateTimePicker();
this.buttonSave = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownTotal)).BeginInit();
this.SuspendLayout();
labelType = new Label();
labelAmount = new Label();
labelDate = new Label();
comboBoxSaleType = new ComboBox();
numericUpDownTotal = new NumericUpDown();
dateTimePickerSale = new DateTimePicker();
buttonSave = new Button();
buttonCancel = new Button();
comboBoxAssemblyId = new ComboBox();
comboBoxDeviceId = new ComboBox();
labelAssemblyId = new Label();
labelDeviceId = new Label();
((System.ComponentModel.ISupportInitialize)numericUpDownTotal).BeginInit();
SuspendLayout();
//
// labelType
this.labelType.AutoSize = true;
this.labelType.Location = new System.Drawing.Point(30, 25);
this.labelType.Size = new System.Drawing.Size(80, 20);
this.labelType.Text = "Тип продажи";
//
labelType.AutoSize = true;
labelType.Location = new Point(30, 25);
labelType.Name = "labelType";
labelType.Size = new Size(118, 20);
labelType.TabIndex = 7;
labelType.Text = "Тип продажи";
//
// labelAmount
this.labelAmount.AutoSize = true;
this.labelAmount.Location = new System.Drawing.Point(30, 75);
this.labelAmount.Size = new System.Drawing.Size(55, 20);
this.labelAmount.Text = "Сумма";
//
labelAmount.AutoSize = true;
labelAmount.Location = new Point(30, 75);
labelAmount.Name = "labelAmount";
labelAmount.Size = new Size(63, 20);
labelAmount.TabIndex = 6;
labelAmount.Text = "Сумма";
//
// labelDate
this.labelDate.AutoSize = true;
this.labelDate.Location = new System.Drawing.Point(30, 125);
this.labelDate.Size = new System.Drawing.Size(100, 20);
this.labelDate.Text = "Дата продажи";
//
labelDate.AutoSize = true;
labelDate.Location = new Point(30, 125);
labelDate.Name = "labelDate";
labelDate.Size = new Size(131, 20);
labelDate.TabIndex = 5;
labelDate.Text = "Дата продажи";
//
// comboBoxSaleType
this.comboBoxSaleType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSaleType.FormattingEnabled = true;
this.comboBoxSaleType.Location = new System.Drawing.Point(160, 22);
this.comboBoxSaleType.Size = new System.Drawing.Size(280, 28);
//
comboBoxSaleType.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSaleType.FormattingEnabled = true;
comboBoxSaleType.Location = new Point(160, 22);
comboBoxSaleType.Name = "comboBoxSaleType";
comboBoxSaleType.Size = new Size(280, 28);
comboBoxSaleType.TabIndex = 4;
//
// numericUpDownTotal
this.numericUpDownTotal.DecimalPlaces = 2;
this.numericUpDownTotal.Location = new System.Drawing.Point(160, 72);
this.numericUpDownTotal.Size = new System.Drawing.Size(140, 28);
this.numericUpDownTotal.ThousandsSeparator = true;
//
numericUpDownTotal.DecimalPlaces = 2;
numericUpDownTotal.Location = new Point(160, 72);
numericUpDownTotal.Name = "numericUpDownTotal";
numericUpDownTotal.Size = new Size(140, 26);
numericUpDownTotal.TabIndex = 3;
numericUpDownTotal.ThousandsSeparator = true;
//
// dateTimePickerSale
this.dateTimePickerSale.Format = System.Windows.Forms.DateTimePickerFormat.Short;
this.dateTimePickerSale.Location = new System.Drawing.Point(160, 122);
this.dateTimePickerSale.Size = new System.Drawing.Size(280, 28);
//
dateTimePickerSale.Format = DateTimePickerFormat.Short;
dateTimePickerSale.Location = new Point(160, 125);
dateTimePickerSale.Name = "dateTimePickerSale";
dateTimePickerSale.Size = new Size(280, 26);
dateTimePickerSale.TabIndex = 2;
//
// buttonSave
this.buttonSave.Location = new System.Drawing.Point(160, 190);
this.buttonSave.Size = new System.Drawing.Size(120, 40);
this.buttonSave.Text = "Сохранить";
this.buttonSave.UseVisualStyleBackColor = true;
this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
//
buttonSave.Location = new Point(55, 297);
buttonSave.Name = "buttonSave";
buttonSave.Size = new Size(120, 40);
buttonSave.TabIndex = 1;
buttonSave.Text = "Сохранить";
buttonSave.UseVisualStyleBackColor = true;
buttonSave.Click += buttonSave_Click;
//
// buttonCancel
this.buttonCancel.Location = new System.Drawing.Point(300, 190);
this.buttonCancel.Size = new System.Drawing.Size(120, 40);
this.buttonCancel.Text = "Отмена";
this.buttonCancel.UseVisualStyleBackColor = true;
this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
// FormSaleEdit
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(484, 261);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonSave);
this.Controls.Add(this.dateTimePickerSale);
this.Controls.Add(this.numericUpDownTotal);
this.Controls.Add(this.comboBoxSaleType);
this.Controls.Add(this.labelDate);
this.Controls.Add(this.labelAmount);
this.Controls.Add(this.labelType);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Продажа";
((System.ComponentModel.ISupportInitialize)(this.numericUpDownTotal)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
//
buttonCancel.Location = new Point(303, 297);
buttonCancel.Name = "buttonCancel";
buttonCancel.Size = new Size(120, 40);
buttonCancel.TabIndex = 0;
buttonCancel.Text = "Отмена";
buttonCancel.UseVisualStyleBackColor = true;
buttonCancel.Click += buttonCancel_Click;
//
// comboBoxAssemblyId
//
comboBoxAssemblyId.FormattingEnabled = true;
comboBoxAssemblyId.Location = new Point(160, 170);
comboBoxAssemblyId.Name = "comboBoxAssemblyId";
comboBoxAssemblyId.Size = new Size(151, 28);
comboBoxAssemblyId.TabIndex = 8;
//
// comboBoxDeviceId
//
comboBoxDeviceId.FormattingEnabled = true;
comboBoxDeviceId.Location = new Point(160, 222);
comboBoxDeviceId.Name = "comboBoxDeviceId";
comboBoxDeviceId.Size = new Size(151, 28);
comboBoxDeviceId.TabIndex = 9;
//
// labelAssemblyId
//
labelAssemblyId.AutoSize = true;
labelAssemblyId.Location = new Point(30, 173);
labelAssemblyId.Name = "labelAssemblyId";
labelAssemblyId.Size = new Size(127, 20);
labelAssemblyId.TabIndex = 10;
labelAssemblyId.Text = "Номер сборки";
//
// labelDeviceId
//
labelDeviceId.AutoSize = true;
labelDeviceId.Location = new Point(30, 225);
labelDeviceId.Name = "labelDeviceId";
labelDeviceId.Size = new Size(129, 20);
labelDeviceId.TabIndex = 11;
labelDeviceId.Text = "Номер товара";
//
// FormSale
//
AutoScaleDimensions = new SizeF(9F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(484, 380);
Controls.Add(labelDeviceId);
Controls.Add(labelAssemblyId);
Controls.Add(comboBoxDeviceId);
Controls.Add(comboBoxAssemblyId);
Controls.Add(buttonCancel);
Controls.Add(buttonSave);
Controls.Add(dateTimePickerSale);
Controls.Add(numericUpDownTotal);
Controls.Add(comboBoxSaleType);
Controls.Add(labelDate);
Controls.Add(labelAmount);
Controls.Add(labelType);
Font = new Font("Microsoft Sans Serif", 9.75F);
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
Name = "FormSale";
StartPosition = FormStartPosition.CenterParent;
Text = "Продажа";
((System.ComponentModel.ISupportInitialize)numericUpDownTotal).EndInit();
ResumeLayout(false);
PerformLayout();
}
private System.Windows.Forms.Label labelType;
@@ -105,5 +172,9 @@
private System.Windows.Forms.DateTimePicker dateTimePickerSale;
private System.Windows.Forms.Button buttonSave;
private System.Windows.Forms.Button buttonCancel;
private ComboBox comboBoxAssemblyId;
private ComboBox comboBoxDeviceId;
private Label labelAssemblyId;
private Label labelDeviceId;
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Windows.Forms;
using ComputerShop.Entities;
using ComputerShop.Repositories;
@@ -8,6 +9,8 @@ namespace ProjectComputerShop.Forms
public partial class FormSale : Form
{
private readonly ISalesRepository _salesRepository;
private readonly IAssembliesRepository _assembliesRepository;
private readonly IDevicesRepository _devicesRepository;
private int? _saleId;
public int Id
@@ -16,16 +19,27 @@ namespace ProjectComputerShop.Forms
{
try
{
var sales = _salesRepository.ReadAll();
var sale = sales.FirstOrDefault(s => s.Id == value);
var sale = _salesRepository.ReadById(value);
if (sale == null)
throw new InvalidDataException(nameof(sale));
comboBoxSaleType.Text = sale.SaleType;
comboBoxSaleType.SelectedItem = sale.SaleType;
numericUpDownTotal.Value = sale.TotalAmount;
dateTimePickerSale.Value = sale.SaleDate;
LoadAssemblies();
LoadDevices();
if (sale.AssemblyId.HasValue && comboBoxAssemblyId.DataSource != null)
{
comboBoxAssemblyId.SelectedValue = sale.AssemblyId.Value;
}
if (sale.DeviceId.HasValue && comboBoxDeviceId.DataSource != null)
{
comboBoxDeviceId.SelectedValue = sale.DeviceId.Value;
}
_saleId = value;
}
catch (Exception ex)
@@ -39,15 +53,69 @@ namespace ProjectComputerShop.Forms
}
}
public FormSale(ISalesRepository salesRepository)
public FormSale(ISalesRepository salesRepository,
IAssembliesRepository assembliesRepository,
IDevicesRepository devicesRepository)
{
InitializeComponent();
_salesRepository = salesRepository ??
throw new ArgumentNullException(nameof(salesRepository));
_assembliesRepository = assembliesRepository ??
throw new ArgumentNullException(nameof(assembliesRepository));
_devicesRepository = devicesRepository ??
throw new ArgumentNullException(nameof(devicesRepository));
comboBoxSaleType.Items.AddRange(new object[] { "Сборка", "Комплектующее", "Готовая техника" });
comboBoxSaleType.SelectedIndex = 0;
dateTimePickerSale.Value = DateTime.Today;
LoadAssemblies();
LoadDevices();
comboBoxSaleType.SelectedIndexChanged += ComboBoxSaleType_SelectedIndexChanged;
}
private void LoadAssemblies()
{
try
{
var assemblies = _assembliesRepository.ReadAll().ToList();
comboBoxAssemblyId.DataSource = assemblies;
comboBoxAssemblyId.DisplayMember = "AssemblyNumber";
comboBoxAssemblyId.ValueMember = "Id";
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при загрузке сборок: {ex.Message}", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadDevices()
{
try
{
var devices = _devicesRepository.ReadAll().ToList();
comboBoxDeviceId.DataSource = devices;
comboBoxDeviceId.DisplayMember = "Brand";
comboBoxDeviceId.ValueMember = "Id";
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при загрузке устройств: {ex.Message}", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ComboBoxSaleType_SelectedIndexChanged(object sender, EventArgs e)
{
var saleType = comboBoxSaleType.SelectedItem?.ToString();
labelAssemblyId.Visible = (saleType == "Сборка");
comboBoxAssemblyId.Visible = (saleType == "Сборка");
labelDeviceId.Visible = (saleType == "Готовая техника");
comboBoxDeviceId.Visible = (saleType == "Готовая техника");
}
private void buttonSave_Click(object sender, EventArgs e)
@@ -60,7 +128,30 @@ namespace ProjectComputerShop.Forms
if (numericUpDownTotal.Value <= 0)
throw new Exception("Сумма должна быть больше 0");
var sale = CreateSale(_saleId ?? 0);
string saleType = comboBoxSaleType.SelectedItem.ToString();
int? assemblyId = null;
int? deviceId = null;
if (comboBoxAssemblyId.SelectedValue == null)
throw new Exception("Выберите номер сборки");
assemblyId = (int)comboBoxAssemblyId.SelectedValue;
if (comboBoxDeviceId.SelectedValue == null)
throw new Exception("Выберите товар");
deviceId = (int)comboBoxDeviceId.SelectedValue;
var sale = Sales.CreateEntity(
id: _saleId ?? 0,
assemblyId: assemblyId,
deviceId: deviceId,
saleDate: dateTimePickerSale.Value,
totalAmount: numericUpDownTotal.Value,
saleType: saleType
);
if (_saleId.HasValue)
_salesRepository.Update(sale);
@@ -82,11 +173,26 @@ namespace ProjectComputerShop.Forms
private Sales CreateSale(int id)
{
string saleType = comboBoxSaleType.SelectedItem.ToString();
int? assemblyId = null;
int? deviceId = null;
if (saleType == "Сборка" && comboBoxAssemblyId.SelectedValue != null)
{
assemblyId = (int)comboBoxAssemblyId.SelectedValue;
}
else if (saleType == "Готовая техника" && comboBoxDeviceId.SelectedValue != null)
{
deviceId = (int)comboBoxDeviceId.SelectedValue;
}
return Sales.CreateEntity(
id: id,
assemblyId: assemblyId,
deviceId: deviceId,
saleDate: dateTimePickerSale.Value,
totalAmount: numericUpDownTotal.Value,
saleType: comboBoxSaleType.SelectedItem.ToString()
saleType: saleType
);
}
}

View File

@@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.66" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.3.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.0" />

View File

@@ -65,8 +65,8 @@ namespace ComputerShop.Implementations
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
string queryInsert = @"
INSERT INTO Parts (Name, Price, WarrantyMonths, Flags)
VALUES (@Name, @Price, @WarrantyMonths, @Flags);";
INSERT INTO Parts (Name, Price, WarrantyMonths, Flags,""categoryId"")
VALUES (@Name, @Price, @WarrantyMonths, @Flags,@CategoryId);";
connection.Execute(queryInsert, part);
}
catch (Exception ex)
@@ -88,7 +88,8 @@ namespace ComputerShop.Implementations
Name=@Name,
Price=@Price,
WarrantyMonths=@WarrantyMonths,
Flags=@Flags
Flags=@Flags,
""categoryId""=@CategoryId
WHERE Id=@Id";
connection.Execute(queryUpdate, part);
}

View File

@@ -80,8 +80,8 @@ public class SalesRepository : ISalesRepository
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
string queryInsert = @"
INSERT INTO Sales (SaleDate, TotalAmount, SaleType)
VALUES ( @SaleDate, @TotalAmount, @SaleType);";
INSERT INTO Sales (""assemblyId"",""deviceId"", SaleDate, TotalAmount, SaleType)
VALUES ( @AssemblyId,@DeviceId,@SaleDate, @TotalAmount, @SaleType);";
connection.Execute(queryInsert, sale);
}
catch (Exception ex)
@@ -101,9 +101,8 @@ public class SalesRepository : ISalesRepository
var queryUpdate = @"
UPDATE Sales
SET
AssemblyId=@AssemblyId,
PartId=@PartId,
DeviceId=@DeviceId,
""assemblyId""=@AssemblyId,
""deviceId""=@DeviceId,
SaleDate=@SaleDate,
TotalAmount=@TotalAmount,
SaleType=@SaleType