From 88aa6084354675bf32a6e600655cc3bd414b44da Mon Sep 17 00:00:00 2001 From: leoevgeniy <98206383+leoevgeniy@users.noreply.github.com> Date: Mon, 1 Apr 2024 21:57:05 +0400 Subject: [PATCH] lab2hard done --- .../BusinessLogics/ComponentLogic.cs | 8 +- .../BusinessLogics/OrderLogic.cs | 20 ++- .../BusinessLogics/ShopLogic.cs | 15 ++ .../CarpentryWorkshopBusinessLogic.csproj | 1 + .../BindingModels/ShopBindingModel.cs | 5 +- .../BusinessLogicsContracts/IShopLogic.cs | 1 + .../SearchModels/SupplySearchModel.cs | 14 ++ .../StoragesContracts/IShopStorage.cs | 2 + .../ViewModels/ShopViewModel.cs | 2 + .../Models/IShopModel.cs | 1 + .../DataFileSingleton.cs | 4 + .../Implements/ShopStorage.cs | 154 ++++++++++++++++++ .../Models/Shop.cs | 113 +++++++++++++ .../Implements/ShopStorage.cs | 9 + .../Models/Shop.cs | 4 +- .../CarpentryWorkshopView.csproj | 1 - .../FormMain.Designer.cs | 38 +++-- .../CarpentryWorkshopView/FormMain.cs | 9 + .../FormSell.Designer.cs | 124 ++++++++++++++ .../CarpentryWorkshopView/FormSell.cs | 88 ++++++++++ .../CarpentryWorkshopView/FormSell.resx | 120 ++++++++++++++ .../FormShop.Designer.cs | 48 ++++-- .../CarpentryWorkshopView/FormShop.cs | 8 +- .../CarpentryWorkshopView/Program.cs | 1 + 24 files changed, 758 insertions(+), 32 deletions(-) create mode 100644 CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/SupplySearchModel.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/ShopStorage.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopView/FormSell.Designer.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopView/FormSell.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopView/FormSell.resx diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ComponentLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ComponentLogic.cs index 64de97c..2be2501 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ComponentLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -95,10 +95,10 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost)); } _logger.LogInformation("Component. ComponentName:{ComponentName}. Cost:{ Cost}. Id: { Id}", model.ComponentName, model.Cost, model.Id); - var element = _componentStorage.GetElement(new ComponentSearchModel - { - ComponentName = model.ComponentName - }); + var element = _componentStorage.GetElement(new ComponentSearchModel + { + ComponentName = model.ComponentName + }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Компонент с таким названием уже есть"); diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs index dfbd4c1..384a7ea 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -17,10 +17,13 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IShopStorage _shopStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage) { _logger = logger; _orderStorage = orderStorage; + _shopStorage=shopStorage; } public List? ReadList(OrderSearchModel? model) { @@ -105,8 +108,21 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics { var order = _orderStorage.GetElement(new OrderSearchModel { - Id = model.Id + Id = model.Id, }); + if (order == null) + { + throw new ArgumentNullException(nameof(order)); + } + if (!_shopStorage.RestockingShops(new SupplyBindingModel + { + WoodId = order.WoodId, + Count = order.Count + })) + { + throw new ArgumentException("Недостаточно места"); + } + if (order == null) { throw new Exception("Не найден заказ"); diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs index dbd73e0..128bf55 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -151,5 +151,20 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics throw new InvalidOperationException("Магазин с таким названием уже есть"); } } + public bool Sale(SupplySearchModel model) + { + if (!model.WoodId.HasValue || !model.Count.HasValue) + { + return false; + } + _logger.LogInformation("Check pizza count in all shops"); + if (_shopStorage.Sale(model)) + { + _logger.LogInformation("Selling sucsess"); + return true; + } + _logger.LogInformation("Selling failed"); + return false; + } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj index c14d81b..fceba5f 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj @@ -13,6 +13,7 @@ + diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ShopBindingModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ShopBindingModel.cs index feed4c9..4e8da35 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ShopBindingModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ShopBindingModel.cs @@ -5,9 +5,10 @@ namespace CarpentryWorkshopContracts.BindingModels public class ShopBindingModel : IShopModel { public int Id { get; set; } - public string ShopName { get; set; } - public string Address { get; set; } + public string ShopName { get; set; } = string.Empty; + public string Address { get; set; } = string.Empty; public DateTime DateOpen { get; set; } public Dictionary ShopWoods { get; set; } = new(); + public int WoodMaxCount { get; set; } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs index 5840f0f..97fbb5a 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -13,5 +13,6 @@ namespace CarpentryWorkshopContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool MakeSupply(SupplyBindingModel model); + bool Sale(SupplySearchModel model); } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/SupplySearchModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/SupplySearchModel.cs new file mode 100644 index 0000000..b9d894f --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/SupplySearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopContracts.SearchModels +{ + public class SupplySearchModel + { + public int? WoodId { get; set; } + public int? Count { get; set; } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs index 465cef8..3638058 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs @@ -18,5 +18,7 @@ namespace CarpentryWorkshopContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool Sale(SupplySearchModel model); + bool RestockingShops(SupplyBindingModel model); } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ShopViewModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ShopViewModel.cs index d469d57..ec8eed1 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ShopViewModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ShopViewModel.cs @@ -13,5 +13,7 @@ namespace CarpentryWorkshopContracts.ViewModels [DisplayName("Дата открытия")] public DateTime DateOpen { get; set; } public Dictionary ShopWoods { get; set; } = new(); + [DisplayName("Вместимость")] + public int WoodMaxCount { get; set; } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IShopModel.cs b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IShopModel.cs index 19e6d4c..b6a8824 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IShopModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IShopModel.cs @@ -6,5 +6,6 @@ string Address { get; } DateTime DateOpen { get; } Dictionary ShopWoods { get; } + public int WoodMaxCount { get; } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/DataFileSingleton.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/DataFileSingleton.cs index 7b190ee..92214f9 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/DataFileSingleton.cs +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/DataFileSingleton.cs @@ -9,9 +9,11 @@ namespace CarpentryWorkshopFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string WoodFileName = "Wood.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Woods { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -23,11 +25,13 @@ namespace CarpentryWorkshopFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveWoods() => SaveData(Woods, WoodFileName, "Woods", 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)!)!; Woods = LoadData(WoodFileName, "Wood", x => Wood.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/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/ShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..d78279c --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,154 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Shops.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + return source.Shops.Where(x => x.ShopName.Contains(model.ShopName)).Select(x => x.GetViewModel).ToList(); + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + return source.Shops.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + 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 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; + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop != null) + { + source.Shops.Remove(shop); + source.SaveShops(); + return shop.GetViewModel; + } + return null; + } + + public bool Sale(SupplySearchModel model) + { + if (model == null || !model.WoodId.HasValue || !model.Count.HasValue) + return false; + int remainingSpace = source.Shops.Select(x => x.Woods.ContainsKey(model.WoodId.Value) ? x.Woods[model.WoodId.Value] : 0).Sum(); + if (remainingSpace < model.Count) + { + return false; + } + var shops = source.Shops.Where(x => x.Woods.ContainsKey(model.WoodId.Value)).OrderByDescending(x => x.Woods[model.WoodId.Value]).ToList(); + foreach (var shop in shops) + { + int residue = model.Count.Value - shop.Woods[model.WoodId.Value]; + if (residue > 0) + { + shop.Woods.Remove(model.WoodId.Value); + shop.WoodsUpdate(); + model.Count = residue; + } + else + { + if (residue == 0) + { + shop.Woods.Remove(model.WoodId.Value); + } + else + { + shop.Woods[model.WoodId.Value] = -residue; + } + shop.WoodsUpdate(); + source.SaveShops(); + return true; + } + } + source.SaveShops(); + return false; + } + + public bool RestockingShops(SupplyBindingModel model) + { + if (model == null || source.Shops.Select(x => x.WoodMaxCount - x.ShopWoods.Select(y => y.Value.Item2).Sum()).Sum() < model.Count) + { + return false; + } + foreach (Shop shop in source.Shops) + { + int free_places = shop.WoodMaxCount - shop.ShopWoods.Select(x => x.Value.Item2).Sum(); + if (free_places <= 0) + continue; + free_places = Math.Min(free_places, model.Count); + model.Count -= free_places; + if (shop.Woods.ContainsKey(model.WoodId)) + { + shop.Woods[model.WoodId] += free_places; + } + else + { + shop.Woods.Add(model.WoodId, free_places); + } + shop.WoodsUpdate(); + if (model.Count == 0) + { + source.SaveShops(); + return true; + } + } + return false; + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs new file mode 100644 index 0000000..56372ac --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs @@ -0,0 +1,113 @@ +using CarpentryWorkshopDataModels.Models; +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using CarpentryWorkshopFileImplement; + +namespace CarpentryWorkshopFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } + public string Address { get; private set; } + public DateTime DateOpen { get; private set; } + public Dictionary Woods { get; private set; } = new(); + public int WoodMaxCount { get; private set; } + + private Dictionary? _shopWoods = null; + + public Dictionary ShopWoods + { + get + { + if (_shopWoods == null) + { + var source = DataFileSingleton.GetInstance(); + _shopWoods = Woods.ToDictionary(x => x.Key, y => ((source.Woods.FirstOrDefault(z => z.Id == y.Key) as IWoodModel)!, y.Value)); + } + return _shopWoods; + } + } + + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpen = model.DateOpen, + Woods = model.ShopWoods.ToDictionary(x => x.Key, x => x.Value.Item2), + WoodMaxCount = model.WoodMaxCount + }; + } + + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + Address = element.Element("Address")!.Value, + DateOpen = Convert.ToDateTime(element.Element("DateOpen")!.Value), + Woods = element.Element("ShopWoods")!.Elements("ShopWood")!.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)), + WoodMaxCount = Convert.ToInt32(element.Element("WoodMaxCount")!.Value) + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + DateOpen = model.DateOpen; + WoodMaxCount = model.WoodMaxCount; + Woods = model.ShopWoods.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopWoods = null; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + ShopWoods = ShopWoods, + WoodMaxCount = WoodMaxCount + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("DateOpen", DateOpen.ToString()), + new XElement("ShopWoods", Woods.Select( + x => new XElement("ShopWood", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()), + new XElement("WoodMaxCount", WoodMaxCount.ToString()) + ); + + public void WoodsUpdate() + { + _shopWoods = null; + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs index fc8c47b..a7847f6 100644 --- a/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs @@ -100,5 +100,14 @@ namespace CarpentryWorkshopListImplement.Implements } return null; } + public bool Sale(SupplySearchModel model) + { + throw new NotImplementedException(); + } + + public bool RestockingShops(SupplyBindingModel model) + { + throw new NotImplementedException(); + } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs index 1b9426b..ee13d9f 100644 --- a/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs @@ -16,6 +16,7 @@ namespace CarpentryWorkshopListImplement.Models public string Address { get; private set; } public DateTime DateOpen { get; private set; } public Dictionary ShopWoods { get; private set; } = new(); + public int WoodMaxCount { get; private set; } public static Shop? Create(ShopBindingModel model) { @@ -26,7 +27,8 @@ namespace CarpentryWorkshopListImplement.Models Id = model.Id, ShopName = model.ShopName, Address = model.Address, - DateOpen = model.DateOpen + DateOpen = model.DateOpen, + WoodMaxCount = model.WoodMaxCount, }; } diff --git a/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj b/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj index d0d744a..f62a52a 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj @@ -18,7 +18,6 @@ - diff --git a/CarpentryWorkshop/CarpentryWorkshopView/FormMain.Designer.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormMain.Designer.cs index ebc4608..5ee041e 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/FormMain.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormMain.Designer.cs @@ -33,20 +33,22 @@ КомпонентыToolStripMenuItem = new ToolStripMenuItem(); ИзделияToolStripMenuItem = new ToolStripMenuItem(); магазиныToolStripMenuItem = new ToolStripMenuItem(); + операцииToolStripMenuItem = new ToolStripMenuItem(); + поставкаToolStripMenuItem = new ToolStripMenuItem(); + продажаToolStripMenuItem = new ToolStripMenuItem(); dataGridView = new DataGridView(); ButtonCreateOrder = new Button(); ButtonTakeOrderInWork = new Button(); ButtonOrderReady = new Button(); ButtonIssuedOrder = new Button(); ButtonRef = new Button(); - поставкаToolStripMenuItem = new ToolStripMenuItem(); menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // // menuStrip1 // - menuStrip1.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem }); + menuStrip1.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem, операцииToolStripMenuItem }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; menuStrip1.Size = new Size(896, 24); @@ -55,7 +57,7 @@ // // справочникиToolStripMenuItem // - справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { КомпонентыToolStripMenuItem, ИзделияToolStripMenuItem, магазиныToolStripMenuItem, поставкаToolStripMenuItem }); + справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { КомпонентыToolStripMenuItem, ИзделияToolStripMenuItem, магазиныToolStripMenuItem }); справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; справочникиToolStripMenuItem.Size = new Size(94, 20); справочникиToolStripMenuItem.Text = "Справочники"; @@ -81,6 +83,27 @@ магазиныToolStripMenuItem.Text = "Магазины"; магазиныToolStripMenuItem.Click += магазиныToolStripMenuItem_Click; // + // операцииToolStripMenuItem + // + операцииToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { поставкаToolStripMenuItem, продажаToolStripMenuItem }); + операцииToolStripMenuItem.Name = "операцииToolStripMenuItem"; + операцииToolStripMenuItem.Size = new Size(75, 20); + операцииToolStripMenuItem.Text = "Операции"; + // + // поставка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 += SellToolStripMenuItem_Click; + // // dataGridView // dataGridView.BackgroundColor = SystemColors.ButtonHighlight; @@ -143,13 +166,6 @@ ButtonRef.UseVisualStyleBackColor = true; ButtonRef.Click += ButtonRef_Click; // - // поставкаToolStripMenuItem - // - поставкаToolStripMenuItem.Name = "поставкаToolStripMenuItem"; - поставкаToolStripMenuItem.Size = new Size(180, 22); - поставкаToolStripMenuItem.Text = "Поставка"; - поставкаToolStripMenuItem.Click += поставкиToolStripMenuItem_Click; - // // FormMain // AutoScaleDimensions = new SizeF(7F, 15F); @@ -185,6 +201,8 @@ private System.Windows.Forms.ToolStripMenuItem КомпонентыToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem ИзделияToolStripMenuItem; private ToolStripMenuItem магазиныToolStripMenuItem; + private ToolStripMenuItem операцииToolStripMenuItem; private ToolStripMenuItem поставкаToolStripMenuItem; + private ToolStripMenuItem продажаToolStripMenuItem; } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopView/FormMain.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormMain.cs index 211ce67..b183aad 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/FormMain.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormMain.cs @@ -12,6 +12,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; + namespace CarpentryWorkshopView { public partial class FormMain : Form @@ -162,5 +163,13 @@ namespace CarpentryWorkshopView form.ShowDialog(); } } + private void SellToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSell)); + if (service is FormSell form) + { + form.ShowDialog(); + } + } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopView/FormSell.Designer.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormSell.Designer.cs new file mode 100644 index 0000000..d6d1cf1 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormSell.Designer.cs @@ -0,0 +1,124 @@ +namespace CarpentryWorkshopView +{ + partial class FormSell + { + /// + /// 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() + { + labelWood = new Label(); + comboBoxWood = new ComboBox(); + labelCount = new Label(); + textBoxCount = new TextBox(); + buttonSell = new Button(); + buttonCancel = new Button(); + SuspendLayout(); + // + // labelWood + // + labelWood.AutoSize = true; + labelWood.Location = new Point(10, 10); + labelWood.Name = "labelWood"; + labelWood.Size = new Size(59, 15); + labelWood.TabIndex = 0; + labelWood.Text = "Изделие: "; + // + // comboBoxWood + // + comboBoxWood.FormattingEnabled = true; + comboBoxWood.Location = new Point(101, 8); + comboBoxWood.Margin = new Padding(3, 2, 3, 2); + comboBoxWood.Name = "comboBoxWood"; + comboBoxWood.Size = new Size(210, 23); + comboBoxWood.TabIndex = 1; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(10, 41); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(78, 15); + labelCount.TabIndex = 2; + labelCount.Text = "Количество: "; + // + // textBoxCount + // + textBoxCount.Location = new Point(101, 39); + textBoxCount.Margin = new Padding(3, 2, 3, 2); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(210, 23); + textBoxCount.TabIndex = 3; + // + // buttonSell + // + buttonSell.Location = new Point(112, 74); + buttonSell.Margin = new Padding(3, 2, 3, 2); + buttonSell.Name = "buttonSell"; + buttonSell.Size = new Size(82, 22); + buttonSell.TabIndex = 4; + buttonSell.Text = "Продать"; + buttonSell.UseVisualStyleBackColor = true; + buttonSell.Click += ButtonSell_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(212, 74); + buttonCancel.Margin = new Padding(3, 2, 3, 2); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(82, 22); + buttonCancel.TabIndex = 5; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // FormSell + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(320, 105); + Controls.Add(buttonCancel); + Controls.Add(buttonSell); + Controls.Add(textBoxCount); + Controls.Add(labelCount); + Controls.Add(comboBoxWood); + Controls.Add(labelWood); + Margin = new Padding(3, 2, 3, 2); + Name = "FormSell"; + Text = "Продажа изделий"; + Load += FormSellingPizza_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelWood; + private ComboBox comboBoxWood; + private Label labelCount; + private TextBox textBoxCount; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopView/FormSell.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormSell.cs new file mode 100644 index 0000000..c897230 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormSell.cs @@ -0,0 +1,88 @@ +using Microsoft.Extensions.Logging; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +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 CarpentryWorkshopView +{ + public partial class FormSell : Form + { + private readonly ILogger _logger; + private readonly IWoodLogic _logicP; + private readonly IShopLogic _logicS; + private List _woodList = new List(); + + public FormSell(ILogger logger, IWoodLogic logicP, IShopLogic logicS) + { + InitializeComponent(); + _logger = logger; + _logicP = logicP; + _logicS = logicS; + } + + private void FormSellingPizza_Load(object sender, EventArgs e) + { + _woodList = _logicP.ReadList(null); + if (_woodList != null) + { + comboBoxWood.DisplayMember = "WoodName"; + comboBoxWood.ValueMember = "Id"; + comboBoxWood.DataSource = _woodList; + comboBoxWood.SelectedItem = null; + _logger.LogInformation("Загрузка изделий для продажи"); + } + } + + private void ButtonSell_Click(object sender, EventArgs e) + { + if (comboBoxWood.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание покупки"); + try + { + bool resout = _logicS.Sale(new SupplySearchModel + { + WoodId = Convert.ToInt32(comboBoxWood.SelectedValue), + Count = Convert.ToInt32(textBoxCount.Text) + }); + if (resout) + { + _logger.LogInformation("Проверка пройдена, продажа проведена"); + MessageBox.Show("Продажа проведена", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + else + { + _logger.LogInformation("Проверка не пройдена"); + MessageBox.Show("Продажа не может быть создана.", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания покупки"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopView/FormSell.resx b/CarpentryWorkshop/CarpentryWorkshopView/FormSell.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormSell.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/CarpentryWorkshop/CarpentryWorkshopView/FormShop.Designer.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormShop.Designer.cs index 05f98fc..fc8a8d5 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/FormShop.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormShop.Designer.cs @@ -41,28 +41,31 @@ Название = new DataGridViewTextBoxColumn(); Цена = new DataGridViewTextBoxColumn(); Количество = new DataGridViewTextBoxColumn(); + labelCapacity = new Label(); + numericUpWoodMaxCount = new NumericUpDown(); ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + ((System.ComponentModel.ISupportInitialize)numericUpWoodMaxCount).BeginInit(); SuspendLayout(); // // DateTimePicker // - DateTimePicker.Location = new Point(85, 70); + DateTimePicker.Location = new Point(98, 70); DateTimePicker.Name = "DateTimePicker"; - DateTimePicker.Size = new Size(318, 23); + DateTimePicker.Size = new Size(305, 23); DateTimePicker.TabIndex = 0; // // NameTextBox // - NameTextBox.Location = new Point(85, 12); + NameTextBox.Location = new Point(98, 12); NameTextBox.Name = "NameTextBox"; - NameTextBox.Size = new Size(318, 23); + NameTextBox.Size = new Size(305, 23); NameTextBox.TabIndex = 1; // // AddressTextBox // - AddressTextBox.Location = new Point(85, 41); + AddressTextBox.Location = new Point(98, 41); AddressTextBox.Name = "AddressTextBox"; - AddressTextBox.Size = new Size(318, 23); + AddressTextBox.Size = new Size(305, 23); AddressTextBox.TabIndex = 2; // // NameLabel @@ -94,7 +97,7 @@ // // SaveButton // - SaveButton.Location = new Point(247, 255); + SaveButton.Location = new Point(248, 320); SaveButton.Name = "SaveButton"; SaveButton.Size = new Size(75, 23); SaveButton.TabIndex = 6; @@ -104,7 +107,7 @@ // // CancelButton // - CancelButton.Location = new Point(328, 255); + CancelButton.Location = new Point(329, 320); CancelButton.Name = "CancelButton"; CancelButton.Size = new Size(75, 23); CancelButton.TabIndex = 7; @@ -116,9 +119,9 @@ // DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; DataGridView.Columns.AddRange(new DataGridViewColumn[] { Column1, Название, Цена, Количество }); - DataGridView.Location = new Point(12, 99); + DataGridView.Location = new Point(12, 139); DataGridView.Name = "DataGridView"; - DataGridView.Size = new Size(391, 150); + DataGridView.Size = new Size(391, 175); DataGridView.TabIndex = 8; // // Column1 @@ -146,11 +149,31 @@ Количество.Name = "Количество"; Количество.ReadOnly = true; // + // labelCapacity + // + labelCapacity.AutoSize = true; + labelCapacity.Location = new Point(12, 103); + labelCapacity.Name = "labelCapacity"; + labelCapacity.Size = new Size(80, 15); + labelCapacity.TabIndex = 9; + labelCapacity.Text = "Вместимость"; + // + // numericUpWoodMaxCount + // + numericUpWoodMaxCount.Location = new Point(98, 101); + numericUpWoodMaxCount.Margin = new Padding(3, 2, 3, 2); + numericUpWoodMaxCount.Maximum = new decimal(new int[] { 10000, 0, 0, 0 }); + numericUpWoodMaxCount.Name = "numericUpWoodMaxCount"; + numericUpWoodMaxCount.Size = new Size(305, 23); + numericUpWoodMaxCount.TabIndex = 12; + // // FormShop // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(411, 285); + ClientSize = new Size(411, 355); + Controls.Add(numericUpWoodMaxCount); + Controls.Add(labelCapacity); Controls.Add(DataGridView); Controls.Add(CancelButton); Controls.Add(SaveButton); @@ -164,6 +187,7 @@ Text = "Редактор магазина"; Load += ShopForm_Load; ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ((System.ComponentModel.ISupportInitialize)numericUpWoodMaxCount).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -183,5 +207,7 @@ private DataGridViewTextBoxColumn Название; private DataGridViewTextBoxColumn Цена; private DataGridViewTextBoxColumn Количество; + private Label labelCapacity; + private NumericUpDown numericUpWoodMaxCount; } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopView/FormShop.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormShop.cs index 0076e41..280e210 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/FormShop.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormShop.cs @@ -3,6 +3,7 @@ using CarpentryWorkshopContracts.BusinessLogicsContracts; using CarpentryWorkshopContracts.SearchModels; using CarpentryWorkshopDataModels.Models; using Microsoft.Extensions.Logging; +using System.Windows.Forms; namespace CarpentryWorkshopView { @@ -11,12 +12,15 @@ namespace CarpentryWorkshopView private readonly ILogger _logger; private readonly IShopLogic _logic; public int? _id; + public int Id { set { _id = value; } } private Dictionary _woods; + private DateTime? _dateopen = null; public FormShop(ILogger logger, IShopLogic logic) { InitializeComponent(); _logger = logger; _logic = logic; + _woods = new Dictionary(); } private void ShopForm_Load(object sender, EventArgs e) @@ -32,7 +36,8 @@ namespace CarpentryWorkshopView NameTextBox.Text = shop.ShopName; AddressTextBox.Text = shop.Address; DateTimePicker.Text = shop.DateOpen.ToString(); - _woods = shop.ShopWoods; + numericUpWoodMaxCount.Value = shop.WoodMaxCount; + _woods = shop.ShopWoods ?? new Dictionary(); } LoadData(); } @@ -93,6 +98,7 @@ namespace CarpentryWorkshopView ShopName = NameTextBox.Text, Address = AddressTextBox.Text, DateOpen = DateTimePicker.Value.Date, + WoodMaxCount = (int)numericUpWoodMaxCount.Value, ShopWoods = _woods }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); diff --git a/CarpentryWorkshop/CarpentryWorkshopView/Program.cs b/CarpentryWorkshop/CarpentryWorkshopView/Program.cs index 97554ff..10001e1 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/Program.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/Program.cs @@ -54,6 +54,7 @@ namespace CarpentryWorkshopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file