diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/OrderLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/OrderLogic.cs index 0f93b79..c7b2506 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/OrderLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/OrderLogic.cs @@ -17,11 +17,15 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopLogic _shopLogic; + private readonly IPackageStorage _packageStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, IPackageStorage packageStorage) { _logger = logger; _orderStorage = orderStorage; + _shopLogic = shopLogic; + _packageStorage = packageStorage; } public bool CreateOrder(OrderBindingModel model) @@ -48,7 +52,7 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) { - var viewModel = _orderStorage.GetElement(new() { Id = model.Id }); + var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); if (viewModel == null) { throw new ArgumentNullException(nameof(model)); @@ -65,13 +69,26 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics if (model.Status == OrderStatus.Выдан) { model.DateImplement = DateTime.Now; - } + var package = _packageStorage.GetElement(new() { Id = viewModel.PackageId }); + if (package == null) + { + throw new ArgumentNullException(nameof(package)); + } + + if (!_shopLogic.AddPackage(package, viewModel.Count)) + { + throw new Exception($"AddPackage operation failed. Store is full."); + } + } + else + { + model.DateImplement = viewModel.DateImplement; + } + CheckModel(model, false); model.Sum = viewModel.Sum; model.Count = viewModel.Count; - model.PackageId = viewModel.PackageId; - - CheckModel(model, false); + model.PackageId = viewModel.PackageId; if (_orderStorage.Update(model) == null) { diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/ShopLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/ShopLogic.cs index 3120b4b..55965d0 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/ShopLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/ShopLogic.cs @@ -56,7 +56,7 @@ namespace SoftwareInstallationBusinessLogic public bool Create(ShopBindingModel model) { CheckModel(model); - model.Packages = new(); + model.ShopPackages = new(); if (_shopStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); @@ -90,6 +90,10 @@ namespace SoftwareInstallationBusinessLogic } return true; } + public bool SellPackage(IPackageModel package, int quantity) + { + return _shopStorage.SellPackage(package, quantity); + } private void CheckModel(ShopBindingModel model, bool withParams = true) { if (model == null) @@ -105,6 +109,11 @@ namespace SoftwareInstallationBusinessLogic throw new ArgumentNullException("Нет названия магазина", nameof(model.Name)); } + if (model.PackageMaxCount < 0) + { + throw new ArgumentException("Максимальное количество изделий в магазине не может быть меньше нуля", nameof(model.PackageMaxCount)); + } + _logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}", model.Name, model.Address, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel @@ -128,19 +137,31 @@ namespace SoftwareInstallationBusinessLogic throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(count)); } _logger.LogInformation("AddPackageInShop. ShopName:{ShopName}.Id:{ Id}", model.Name, model.Id); + var element = _shopStorage.GetElement(model); + if (element == null) { _logger.LogWarning("AddPackageInShop element not found"); return false; } + + if (element.PackageMaxCount - element.ShopPackages.Select(x => x.Value.Item2).Sum() < count) + { + throw new ArgumentNullException("Магазин переполнен", nameof(count)); + } _logger.LogInformation("AddPackageInShop find. Id:{Id}", element.Id); - var prevCount = element.Packages.GetValueOrDefault(package.Id, (package, 0)).Item2; - element.Packages[package.Id] = (package, prevCount + count); - _logger.LogInformation( - "AddPackageInShop. Has been added {count} {package} in {ShopName}", - count, package.PackageName, element.Name); + if (element.ShopPackages.TryGetValue(package.Id, out var pair)) + { + element.ShopPackages[package.Id] = (package, count + pair.Item2); + _logger.LogInformation("AddPackageInShop. Has been added {quantity} {package} in {ShopName}", count, package.PackageName, element.Name); + } + else + { + element.ShopPackages[package.Id] = (package, count); + _logger.LogInformation("AddPackageInShop. Has been added {quantity} new Package {package} in {ShopName}", count, package.PackageName, element.Name); + } _shopStorage.Update(new() { @@ -148,9 +169,56 @@ namespace SoftwareInstallationBusinessLogic Address = element.Address, Name = element.Name, DateOpening = element.DateOpening, - Packages = element.Packages + ShopPackages = element.ShopPackages, + PackageMaxCount = element.PackageMaxCount }); return true; } + public bool AddPackage(IPackageModel package, int count) + { + if (package == null) + { + throw new ArgumentNullException(nameof(package)); + } + + if (count <= 0) + { + throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(count)); + } + + var freePlaces = _shopStorage.GetFullList() + .Select(x => x.PackageMaxCount - x.ShopPackages + .Select(p => p.Value.Item2).Sum()).Sum() - count; + + if (freePlaces < 0) + { + _logger.LogInformation("AddPackage. Failed to add package to store. It's full."); + return false; + } + + foreach (var store in _shopStorage.GetFullList()) + { + var temp = Math.Min(count, store.PackageMaxCount - store.ShopPackages.Select(x => x.Value.Item2).Sum()); + + if (temp <= 0) + { + continue; + } + + if (!AddPackage(new() { Id = store.Id }, package, temp)) + { + _logger.LogWarning("An error occurred while adding package to stores"); + return false; + } + + count -= temp; + + if (count == 0) + { + return true; + } + } + return true; + } } } diff --git a/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs b/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs index 152fad0..2d98532 100644 --- a/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs +++ b/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs @@ -16,11 +16,8 @@ namespace SoftwareInstallationContracts.BindingModels public DateTime DateOpening { get; set; } = DateTime.Now; - public Dictionary Packages - { - get; - set; - } = new(); + public Dictionary ShopPackages { get; set; } = new(); public int Id { get; set; } + public int PackageMaxCount { get; set; } } } diff --git a/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IShopLogic.cs b/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IShopLogic.cs index 9f91211..8cd148f 100644 --- a/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IShopLogic.cs @@ -18,5 +18,7 @@ namespace SoftwareInstallationContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool AddPackage(ShopSearchModel model, IPackageModel package, int count); + bool AddPackage(IPackageModel package, int count); + bool SellPackage(IPackageModel package, int count); } } diff --git a/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs b/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs index fb36229..a172eca 100644 --- a/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs @@ -1,6 +1,7 @@ using SoftwareInstallationContracts.BindingModels; using SoftwareInstallationContracts.SearchModels; using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -17,5 +18,6 @@ namespace SoftwareInstallationContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool SellPackage(IPackageModel model, int count); } } diff --git a/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs b/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs index 0649057..4fabdf2 100644 --- a/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs +++ b/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs @@ -20,12 +20,12 @@ namespace SoftwareInstallationContracts.ViewModels [DisplayName("Время открытия")] public DateTime DateOpening { get; set; } = DateTime.Now; - public Dictionary Packages - { - get; - set; - } = new(); + [DisplayName("Вместимость магазина")] + public int PackageMaxCount { get; set; } + + public Dictionary ShopPackages { get; set; } = new(); public int Id { get; set; } + } } diff --git a/SoftwareInstallation/SoftwareInstallationDataModels/IShopModel.cs b/SoftwareInstallation/SoftwareInstallationDataModels/IShopModel.cs index 046852b..e8f014b 100644 --- a/SoftwareInstallation/SoftwareInstallationDataModels/IShopModel.cs +++ b/SoftwareInstallation/SoftwareInstallationDataModels/IShopModel.cs @@ -12,6 +12,7 @@ namespace SoftwareInstallationDataModels string Name { get; } string Address { get; } DateTime DateOpening { get; } - Dictionary Packages { get; } + Dictionary ShopPackages { get; } + public int PackageMaxCount { get; } } } diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs index f38fc23..da61923 100644 --- a/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs @@ -13,9 +13,11 @@ namespace SoftwareInstallationFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string PackageFileName = "Package.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Packages { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -27,11 +29,13 @@ namespace SoftwareInstallationFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SavePackages() => SaveData(Packages, PackageFileName, "Packages", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Packages = LoadData(PackageFileName, "Package", x => Package.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Shop.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Shop.cs new file mode 100644 index 0000000..ede4c54 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Shop.cs @@ -0,0 +1,108 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using SoftwareInstallationDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SoftwareInstallationFileImplement +{ + public class Shop : IShopModel + { + public string Name { get; private set; } = string.Empty; + public string Address { get; private set; } = string.Empty; + + public DateTime DateOpening { get; private set; } + public Dictionary Packages { get; private set; } = new(); + + public Dictionary _Packages = null; + public Dictionary ShopPackages + { + get + { + if (_Packages == null) + { + var source = DataFileSingleton.GetInstance(); + _Packages = Packages.ToDictionary(x => x.Key, y => ((source.Packages.FirstOrDefault(z => z.Id == y.Key) as IPackageModel)!, y.Value)); + } + return _Packages; + } + } + + public int Id { get; private set; } + + public int PackageMaxCount { get; private set; } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Address = model.Address, + PackageMaxCount = model.PackageMaxCount, + DateOpening = model.DateOpening, + Packages = model.ShopPackages.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Name = element.Element("ShopName")!.Value, + Address = element.Element("ShopAddress")!.Value, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + PackageMaxCount = Convert.ToInt32(element.Element("PackageMaxCount")!.Value), + Packages = element.Element("ShopPackages")!.Elements("Package").ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Address = model.Address; + DateOpening = model.DateOpening; + PackageMaxCount = model.PackageMaxCount; + Packages = model.ShopPackages.ToDictionary(x => x.Key, x => x.Value.Item2); + _Packages = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address, + ShopPackages = ShopPackages, + DateOpening = DateOpening, + PackageMaxCount = PackageMaxCount, + }; + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", Name), + new XElement("ShopAddress", Address), + new XElement("DateOpening", DateOpening), + new XElement("PackageMaxCount", PackageMaxCount), + new XElement("ShopPackages", Packages + .Select(x => new XElement("Package", + new XElement("Key", x.Key), + new XElement("Value", x.Value)) + ).ToArray())); + } +} diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs new file mode 100644 index 0000000..96e13bb --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs @@ -0,0 +1,113 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationFileImplement +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + var element = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Shops.Remove(element); + source.SaveShops(); + return element.GetViewModel; + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + return source.Shops.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) && x.Name == + model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + return source.Shops.Where(x => + x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + return source.Shops.Select(x => x.GetViewModel).ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + source.Shops.Add(newShop); + source.SaveShops(); + return newShop.GetViewModel; + } + + public bool SellPackage(IPackageModel model, int quantity) + { + if (source.Shops.Select(x => x.ShopPackages.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum() < quantity) + { + return false; + } + foreach (var Shop in source.Shops.Where(x => x.ShopPackages.ContainsKey(model.Id))) + { + int QuantityInCurrentShop = Shop.ShopPackages[model.Id].Item2; + if (QuantityInCurrentShop <= quantity) + { + Shop.ShopPackages.Remove(model.Id); + quantity -= QuantityInCurrentShop; + } + else + { + Shop.ShopPackages[model.Id] = (Shop.ShopPackages[model.Id].Item1, QuantityInCurrentShop - quantity); + quantity = 0; + } + if (quantity == 0) + { + return true; + } + } + return false; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + var Shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (Shop == null) + { + return null; + } + Shop.Update(model); + source.SaveShops(); + return Shop.GetViewModel; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/Shop.cs b/SoftwareInstallation/SoftwareInstallationListImplement/Shop.cs index 9d231b4..991ef2b 100644 --- a/SoftwareInstallation/SoftwareInstallationListImplement/Shop.cs +++ b/SoftwareInstallation/SoftwareInstallationListImplement/Shop.cs @@ -1,7 +1,7 @@ using SoftwareInstallationContracts.BindingModels; using SoftwareInstallationContracts.ViewModels; -using SoftwareInstallationDataModels.Models; using SoftwareInstallationDataModels; +using SoftwareInstallationDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -18,11 +18,7 @@ namespace SoftwareInstallationListImplement public DateTime DateOpening { get; private set; } - public Dictionary Packages - { - get; - private set; - } = new(); + public Dictionary ShopPackages { get; private set; } = new(); public int Id { get; private set; } @@ -38,7 +34,7 @@ namespace SoftwareInstallationListImplement Name = model.Name, Address = model.Address, DateOpening = model.DateOpening, - Packages = new() + ShopPackages = new() }; } public void Update(ShopBindingModel? model) @@ -50,15 +46,16 @@ namespace SoftwareInstallationListImplement Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; - Packages = model.Packages; + ShopPackages = model.ShopPackages; } public ShopViewModel GetViewModel => new() { Id = Id, Name = Name, Address = Address, - Packages = Packages, + ShopPackages = ShopPackages, DateOpening = DateOpening, }; + public int PackageMaxCount => throw new NotImplementedException(); } } diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/ShopStorage.cs b/SoftwareInstallation/SoftwareInstallationListImplement/ShopStorage.cs index 498ab79..4612199 100644 --- a/SoftwareInstallation/SoftwareInstallationListImplement/ShopStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationListImplement/ShopStorage.cs @@ -2,6 +2,7 @@ using SoftwareInstallationContracts.SearchModels; using SoftwareInstallationContracts.StoragesContracts; using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -95,6 +96,10 @@ namespace SoftwareInstallationListImplement _source.Shops.Add(newShop); return newShop.GetViewModel; } + public bool SellPackage(IPackageModel model, int quantity) + { + throw new NotImplementedException(); + } public ShopViewModel? Update(ShopBindingModel model) { diff --git a/SoftwareInstallation/SoftwareInstallationView/FormComponents.cs b/SoftwareInstallation/SoftwareInstallationView/FormComponents.cs index 60e55c7..70564b2 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormComponents.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormComponents.cs @@ -24,6 +24,7 @@ namespace SoftwareInstallationView InitializeComponent(); _logger = logger; _logic = logic; + LoadData(); } private void FormComponents_Load(object sender, EventArgs e) { diff --git a/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs index 529c9c8..00379be 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs @@ -33,6 +33,8 @@ компонентыToolStripMenuItem = new ToolStripMenuItem(); изделияToolStripMenuItem = new ToolStripMenuItem(); магазиныToolStripMenuItem = new ToolStripMenuItem(); + поставкиToolStripMenuItem = new ToolStripMenuItem(); + продажиToolStripMenuItem = new ToolStripMenuItem(); dataGridView = new DataGridView(); buttonCreateOrder = new Button(); buttonTakeOrderInWork = new Button(); @@ -55,7 +57,7 @@ // // справочникиToolStripMenuItem // - справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, изделияToolStripMenuItem, магазиныToolStripMenuItem }); + справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, изделияToolStripMenuItem, магазиныToolStripMenuItem, поставкиToolStripMenuItem, продажиToolStripMenuItem }); справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; справочникиToolStripMenuItem.Size = new Size(94, 20); справочникиToolStripMenuItem.Text = "Справочники"; @@ -63,24 +65,38 @@ // компонентыToolStripMenuItem // компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - компонентыToolStripMenuItem.Size = new Size(145, 22); + компонентыToolStripMenuItem.Size = new Size(180, 22); компонентыToolStripMenuItem.Text = "Компоненты"; компонентыToolStripMenuItem.Click += компонентыToolStripMenuItem_Click; // // изделияToolStripMenuItem // изделияToolStripMenuItem.Name = "изделияToolStripMenuItem"; - изделияToolStripMenuItem.Size = new Size(145, 22); + изделияToolStripMenuItem.Size = new Size(180, 22); изделияToolStripMenuItem.Text = "Изделия"; изделияToolStripMenuItem.Click += изделияToolStripMenuItem_Click; // // магазиныToolStripMenuItem // магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - магазиныToolStripMenuItem.Size = new Size(145, 22); + магазиныToolStripMenuItem.Size = new Size(180, 22); магазиныToolStripMenuItem.Text = "Магазины"; магазиныToolStripMenuItem.Click += магазиныToolStripMenuItem_Click; // + // поставкиToolStripMenuItem + // + поставкиToolStripMenuItem.Name = "поставкиToolStripMenuItem"; + поставкиToolStripMenuItem.Size = new Size(180, 22); + поставкиToolStripMenuItem.Text = "Поставки"; + поставкиToolStripMenuItem.Click += поставкиToolStripMenuItem_Click; + // + // продажиToolStripMenuItem + // + продажиToolStripMenuItem.Name = "продажиToolStripMenuItem"; + продажиToolStripMenuItem.Size = new Size(180, 22); + продажиToolStripMenuItem.Text = "Продажи"; + продажиToolStripMenuItem.Click += продажиToolStripMenuItem_Click; + // // dataGridView // dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; @@ -164,7 +180,7 @@ Controls.Add(menuStrip); MainMenuStrip = menuStrip; Name = "FormMain"; - Text = "FormMain"; + Text = "Установка ПО"; Load += FormMain_Load; menuStrip.ResumeLayout(false); menuStrip.PerformLayout(); @@ -187,5 +203,7 @@ private Button buttonRefresh; private ToolStripMenuItem магазиныToolStripMenuItem; private Button buttonAddPackageInShop; + private ToolStripMenuItem поставкиToolStripMenuItem; + private ToolStripMenuItem продажиToolStripMenuItem; } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormMain.cs b/SoftwareInstallation/SoftwareInstallationView/FormMain.cs index f3888b9..44e5a95 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormMain.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormMain.cs @@ -23,6 +23,7 @@ namespace SoftwareInstallationView InitializeComponent(); _logger = logger; _orderLogic = orderLogic; + LoadData(); } private void FormMain_Load(object sender, EventArgs e) { @@ -87,6 +88,22 @@ namespace SoftwareInstallationView form.ShowDialog(); } } + private void поставкиToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormAddPackageInShop)); + if (service is FormAddPackageInShop form) + { + form.ShowDialog(); + } + } + private void продажиToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellPackage)); + if (service is FormSellPackage form) + { + form.ShowDialog(); + } + } private void buttonCreateOrder_Click(object sender, EventArgs e) { @@ -179,6 +196,6 @@ namespace SoftwareInstallationView LoadData(); } - + } } diff --git a/SoftwareInstallation/SoftwareInstallationView/FormPackage.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormPackage.Designer.cs index e05be79..259e3b6 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormPackage.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormPackage.Designer.cs @@ -193,7 +193,7 @@ Controls.Add(labelPrice); Controls.Add(labelName); Name = "FormPackage"; - Text = "FormPackage"; + Text = "Изделие"; Load += FormPackage_Load; componentsGroupBox.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)componentsDataGridView).EndInit(); diff --git a/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.Designer.cs new file mode 100644 index 0000000..7a6172c --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.Designer.cs @@ -0,0 +1,119 @@ +namespace SoftwareInstallationView +{ + partial class FormSellPackage + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + CountLabel = new Label(); + PackageСomboBox = new ComboBox(); + PackageLabel = new Label(); + CountTextBox = new TextBox(); + ButtonCancel = new Button(); + SaveButton = new Button(); + SuspendLayout(); + // + // CountLabel + // + CountLabel.AutoSize = true; + CountLabel.Location = new Point(12, 58); + CountLabel.Name = "CountLabel"; + CountLabel.Size = new Size(75, 15); + CountLabel.TabIndex = 2; + CountLabel.Text = "Количество:"; + // + // PackageСomboBox + // + PackageСomboBox.FormattingEnabled = true; + PackageСomboBox.Location = new Point(98, 20); + PackageСomboBox.Name = "PackageСomboBox"; + PackageСomboBox.Size = new Size(184, 23); + PackageСomboBox.TabIndex = 4; + // + // PackageLabel + // + PackageLabel.AutoSize = true; + PackageLabel.Location = new Point(22, 23); + PackageLabel.Name = "PackageLabel"; + PackageLabel.Size = new Size(56, 15); + PackageLabel.TabIndex = 3; + PackageLabel.Text = "Изделие:"; + // + // CountTextBox + // + CountTextBox.Location = new Point(98, 58); + CountTextBox.Name = "CountTextBox"; + CountTextBox.Size = new Size(184, 23); + CountTextBox.TabIndex = 5; + // + // ButtonCancel + // + ButtonCancel.Location = new Point(230, 92); + ButtonCancel.Name = "ButtonCancel"; + ButtonCancel.Size = new Size(97, 29); + ButtonCancel.TabIndex = 7; + ButtonCancel.Text = "Отмена"; + ButtonCancel.UseVisualStyleBackColor = true; + ButtonCancel.Click += ButtonCancel_Click; + // + // SaveButton + // + SaveButton.Location = new Point(127, 92); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(97, 29); + SaveButton.TabIndex = 6; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += SaveButton_Click; + // + // FormSellPackage + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(339, 128); + Controls.Add(ButtonCancel); + Controls.Add(SaveButton); + Controls.Add(CountTextBox); + Controls.Add(PackageСomboBox); + Controls.Add(PackageLabel); + Controls.Add(CountLabel); + Name = "FormSellPackage"; + Text = "Продать изделие"; + Load += FormSellPackage_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label CountLabel; + private ComboBox PackageСomboBox; + private Label PackageLabel; + private TextBox CountTextBox; + private Button ButtonCancel; + private Button SaveButton; + } +} \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.cs b/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.cs new file mode 100644 index 0000000..8466957 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.cs @@ -0,0 +1,101 @@ +using Microsoft.Extensions.Logging; +using SoftwareInstallationContracts.BusinessLogicsContracts; +using SoftwareInstallationContracts.SearchModels; +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; + +namespace SoftwareInstallationView +{ + public partial class FormSellPackage : Form + { + private readonly ILogger _logger; + private readonly IPackageLogic _logicPackage; + private readonly IShopLogic _logicStore; + public FormSellPackage(ILogger logger, IPackageLogic logicPackage, IShopLogic logicStore) + { + InitializeComponent(); + _logger = logger; + _logicPackage = logicPackage; + _logicStore = logicStore; + LoadData(); + } + private void FormSellPackage_Load(object sender, EventArgs e) + { + LoadData(); + } + private void LoadData() + { + _logger.LogInformation("Loading packages for sale."); + + try + { + var list = _logicPackage.ReadList(null); + if (list != null) + { + PackageСomboBox.DisplayMember = "PackageName"; + PackageСomboBox.ValueMember = "Id"; + PackageСomboBox.DataSource = list; + PackageСomboBox.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "List loading error."); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void SaveButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(CountTextBox.Text)) + { + MessageBox.Show("Укажите количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (PackageСomboBox.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _logger.LogInformation("Package sale."); + + try + { + var operationResult = _logicStore.SellPackage(_logicPackage.ReadElement(new PackageSearchModel() + { + Id = Convert.ToInt32(PackageСomboBox.SelectedValue) + })!, Convert.ToInt32(CountTextBox.Text)); + + if (!operationResult) + { + throw new Exception("Ошибка при продаже изделия. Дополнительная информация в логах."); + } + + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Package sale error."); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.resx b/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs index 505e28a..8244c1e 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs @@ -35,12 +35,15 @@ textBoxAddress = new TextBox(); openingDatePicker = new DateTimePicker(); dataGridView = new DataGridView(); - buttonSave = new Button(); - buttonCancel = new Button(); PackageName = new DataGridViewTextBoxColumn(); Price = new DataGridViewTextBoxColumn(); Count = new DataGridViewTextBoxColumn(); + buttonSave = new Button(); + buttonCancel = new Button(); + labelNumeric = new Label(); + numericUpDown = new NumericUpDown(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDown).BeginInit(); SuspendLayout(); // // labelNameShop @@ -86,7 +89,7 @@ // // openingDatePicker // - openingDatePicker.Location = new Point(334, 36); + openingDatePicker.Location = new Point(334, 27); openingDatePicker.Name = "openingDatePicker"; openingDatePicker.Size = new Size(175, 23); openingDatePicker.TabIndex = 5; @@ -100,6 +103,21 @@ dataGridView.Size = new Size(601, 242); dataGridView.TabIndex = 6; // + // PackageName + // + PackageName.HeaderText = "Изделие"; + PackageName.Name = "PackageName"; + // + // Price + // + Price.HeaderText = "Цена"; + Price.Name = "Price"; + // + // Count + // + Count.HeaderText = "Количество"; + Count.Name = "Count"; + // // buttonSave // buttonSave.Location = new Point(384, 376); @@ -120,26 +138,30 @@ buttonCancel.UseVisualStyleBackColor = true; buttonCancel.Click += buttonCancel_Click; // - // PackageName + // labelNumeric // - PackageName.HeaderText = "Изделие"; - PackageName.Name = "PackageName"; + labelNumeric.AutoSize = true; + labelNumeric.Location = new Point(334, 64); + labelNumeric.Name = "labelNumeric"; + labelNumeric.Size = new Size(129, 15); + labelNumeric.TabIndex = 12; + labelNumeric.Text = "Макс. кол-во товара: "; // - // Price + // numericUpDown // - Price.HeaderText = "Цена"; - Price.Name = "Price"; - // - // Count - // - Count.HeaderText = "Количество"; - Count.Name = "Count"; + numericUpDown.Location = new Point(334, 83); + numericUpDown.Margin = new Padding(3, 2, 3, 2); + numericUpDown.Name = "numericUpDown"; + numericUpDown.Size = new Size(131, 23); + numericUpDown.TabIndex = 13; // // FormShop // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(629, 417); + Controls.Add(numericUpDown); + Controls.Add(labelNumeric); Controls.Add(buttonCancel); Controls.Add(buttonSave); Controls.Add(dataGridView); @@ -150,9 +172,10 @@ Controls.Add(labelAddress); Controls.Add(labelNameShop); Name = "FormShop"; - Text = "FormShop"; + Text = "Магазин"; Load += FormShop_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDown).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -171,5 +194,7 @@ private DataGridViewTextBoxColumn PackageName; private DataGridViewTextBoxColumn Price; private DataGridViewTextBoxColumn Count; + private Label labelNumeric; + private NumericUpDown numericUpDown; } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShop.resx b/SoftwareInstallation/SoftwareInstallationView/FormShop.resx index e591603..5389e21 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShop.resx +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.resx @@ -126,4 +126,13 @@ True + + True + + + True + + + True + \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShops.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormShops.Designer.cs index cbf6f9e..1f86d48 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShops.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormShops.Designer.cs @@ -95,7 +95,7 @@ Controls.Add(buttonAdd); Controls.Add(dataGridView); Name = "FormShops"; - Text = "FormShops"; + Text = "Магазины"; Load += FormShops_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ResumeLayout(false);