diff --git a/CarRepairShop/CarRepairShop.sln b/CarRepairShop/CarRepairShop.sln index d41853f..3d67e9a 100644 --- a/CarRepairShop/CarRepairShop.sln +++ b/CarRepairShop/CarRepairShop.sln @@ -13,7 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopDataModels", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopListImplement", "CarRepairShopListImplement\CarRepairShopListImplement.csproj", "{5EF355A9-2708-47C4-B7B1-6D1CB89CDC02}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRepairShopFileImplement", "CarRepairShopFileImplement\CarRepairShopFileImplement.csproj", "{FD920623-E8C5-45DE-9D7F-A6C643102F9D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopFileImplement", "CarRepairShopFileImplement\CarRepairShopFileImplement.csproj", "{4966A8B7-5985-48DF-834A-3E41D2E0D01D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -41,10 +41,10 @@ Global {5EF355A9-2708-47C4-B7B1-6D1CB89CDC02}.Debug|Any CPU.Build.0 = Debug|Any CPU {5EF355A9-2708-47C4-B7B1-6D1CB89CDC02}.Release|Any CPU.ActiveCfg = Release|Any CPU {5EF355A9-2708-47C4-B7B1-6D1CB89CDC02}.Release|Any CPU.Build.0 = Release|Any CPU - {FD920623-E8C5-45DE-9D7F-A6C643102F9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FD920623-E8C5-45DE-9D7F-A6C643102F9D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FD920623-E8C5-45DE-9D7F-A6C643102F9D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FD920623-E8C5-45DE-9D7F-A6C643102F9D}.Release|Any CPU.Build.0 = Release|Any CPU + {4966A8B7-5985-48DF-834A-3E41D2E0D01D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4966A8B7-5985-48DF-834A-3E41D2E0D01D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4966A8B7-5985-48DF-834A-3E41D2E0D01D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4966A8B7-5985-48DF-834A-3E41D2E0D01D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs index 5253fc5..f5d6b56 100644 --- a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -4,6 +4,7 @@ using CarRepairShopContracts.SearchModels; using CarRepairShopContracts.StoragesContracts; using CarRepairShopContracts.ViewModels; using CarRepairShopDataModels.Enums; +using CarRepairShopDataModels.Models; using Microsoft.Extensions.Logging; namespace CarRepairShopBusinessLogic.BusinessLogics @@ -14,10 +15,20 @@ namespace CarRepairShopBusinessLogic.BusinessLogics private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IRepairStorage _repairStorage; + + private readonly IShopStorage _shopStorage; + + private readonly IShopLogic _shopLogic; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IRepairStorage repairStorage, + IShopStorage shopStorage, IShopLogic shopLogic) { _logger = logger; _orderStorage = orderStorage; + _repairStorage = repairStorage; + _shopStorage = shopStorage; + _shopLogic = shopLogic; } public bool CreateOrder(OrderBindingModel model) @@ -106,6 +117,20 @@ namespace CarRepairShopBusinessLogic.BusinessLogics newStatus, order.Status); return false; } + if (newStatus == OrderStatus.Выдан) + { + var repair = _repairStorage.GetElement(new RepairSearchModel() { Id = order.RepairId }); + if (repair == null) + { + _logger.LogWarning("Change status operation failed. Repairs not found"); + return false; + } + if (!DeliverRepairs(repair, order.Count)) + { + _logger.LogWarning("Change status operation failed. Repairs delivery operation failed"); + return false; + } + } model.RepairId = order.RepairId; model.Count = order.Count; model.Sum = order.Sum; @@ -125,5 +150,62 @@ namespace CarRepairShopBusinessLogic.BusinessLogics } return true; } + private bool DeliverRepairs(IRepairModel repair, int count) + { + if (count <= 0) + { + _logger.LogWarning("Repairs delivery operation failed. Repair count <= 0"); + return false; + } + + var shopList = _shopStorage.GetFullList(); + int shopsCapacity = shopList.Sum(x => x.RepairMaxAmount); + int currentRepairs = shopList.Select(x => x.ShopRepairs.Sum(y => y.Value.Item2)).Sum(); + int freePlaces = shopsCapacity - currentRepairs; + + if (freePlaces < count) + { + _logger.LogWarning("Repairs delivery operation failed. No space for new repairs"); + return false; + } + + foreach (var shop in shopList) + { + freePlaces = shop.RepairMaxAmount - shop.ShopRepairs.Sum(x => x.Value.Item2); + if (freePlaces == 0) + { + continue; + } + if (freePlaces >= count) + { + if (_shopLogic.MakeShipment(new() { Id = shop.Id }, repair, count)) + { + count = 0; + } + else + { + _logger.LogWarning("Repairs delivery operation failed"); + return false; + } + } + else + { + if (_shopLogic.MakeShipment(new() { Id = shop.Id }, repair, freePlaces)) + { + count -= freePlaces; + } + else + { + _logger.LogWarning("Repairs delivery operation failed"); + return false; + } + } + if (count == 0) + { + return true; + } + } + return false; + } } } diff --git a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/ShopLogic.cs b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/ShopLogic.cs index 3cc14bc..3a76eec 100644 --- a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -105,6 +105,11 @@ namespace CarRepairShopBusinessLogic.BusinessLogics _logger.LogWarning("MakeShipment(GetElement). Element not found"); return false; } + if (shop.RepairMaxAmount - shop.ShopRepairs.Sum(x => x.Value.Item2) < count) + { + _logger.LogWarning("MakeShipment error. No space for new repairs"); + return false; + } if (shop.ShopRepairs.ContainsKey(repair.Id)) { var shopIC = shop.ShopRepairs[repair.Id]; @@ -125,6 +130,7 @@ namespace CarRepairShopBusinessLogic.BusinessLogics ShopName = shop.ShopName, Address = shop.Address, DateOpening = shop.DateOpening, + RepairMaxAmount = shop.RepairMaxAmount, ShopRepairs = shop.ShopRepairs, }) == null) { @@ -133,6 +139,10 @@ namespace CarRepairShopBusinessLogic.BusinessLogics } return true; } + public bool MakeSale(IRepairModel model, int count) + { + return _shopStorage.MakeSale(model, count); + } private void CheckModel(ShopBindingModel model, bool withParams = true) { diff --git a/CarRepairShop/CarRepairShopContracts/BindingModels/ShopBindingModel.cs b/CarRepairShop/CarRepairShopContracts/BindingModels/ShopBindingModel.cs index 3d09765..994546d 100644 --- a/CarRepairShop/CarRepairShopContracts/BindingModels/ShopBindingModel.cs +++ b/CarRepairShop/CarRepairShopContracts/BindingModels/ShopBindingModel.cs @@ -17,5 +17,7 @@ namespace CarRepairShopContracts.BindingModels get; set; } = new(); + + public int RepairMaxAmount { get; set; } } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopContracts/BusinessLogicsContracts/IShopLogic.cs b/CarRepairShop/CarRepairShopContracts/BusinessLogicsContracts/IShopLogic.cs index 02e0872..c84608c 100644 --- a/CarRepairShop/CarRepairShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/CarRepairShop/CarRepairShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -18,5 +18,7 @@ namespace CarRepairShopContracts.BusinessLogicsContracts bool Delete(ShopBindingModel model); bool MakeShipment(ShopSearchModel shopModel, IRepairModel Repair, int count); + + bool MakeSale(IRepairModel model, int count); } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopContracts/StoragesContracts/IShopStorage.cs b/CarRepairShop/CarRepairShopContracts/StoragesContracts/IShopStorage.cs index 439d071..6fd3b3f 100644 --- a/CarRepairShop/CarRepairShopContracts/StoragesContracts/IShopStorage.cs +++ b/CarRepairShop/CarRepairShopContracts/StoragesContracts/IShopStorage.cs @@ -1,6 +1,7 @@ using CarRepairShopContracts.BindingModels; using CarRepairShopContracts.SearchModels; using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels.Models; namespace CarRepairShopContracts.StoragesContracts { @@ -17,5 +18,7 @@ namespace CarRepairShopContracts.StoragesContracts ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + + bool MakeSale(IRepairModel model, int count); } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopContracts/ViewModels/ShopViewModel.cs b/CarRepairShop/CarRepairShopContracts/ViewModels/ShopViewModel.cs index 824e926..640ace7 100644 --- a/CarRepairShop/CarRepairShopContracts/ViewModels/ShopViewModel.cs +++ b/CarRepairShop/CarRepairShopContracts/ViewModels/ShopViewModel.cs @@ -21,5 +21,8 @@ namespace CarRepairShopContracts.ViewModels get; set; } = new(); + + [DisplayName("Максимальное количество ремонтов")] + public int RepairMaxAmount { get; set; } } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopDataModels/Models/IShopModel.cs b/CarRepairShop/CarRepairShopDataModels/Models/IShopModel.cs index 5e53025..d2d0ccc 100644 --- a/CarRepairShop/CarRepairShopDataModels/Models/IShopModel.cs +++ b/CarRepairShop/CarRepairShopDataModels/Models/IShopModel.cs @@ -11,5 +11,7 @@ namespace CarRepairShopDataModels.Models DateTime DateOpening { get; } Dictionary ShopRepairs { get; } + + int RepairMaxAmount { get; } } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopFileImplement/DataFileSingleton.cs b/CarRepairShop/CarRepairShopFileImplement/DataFileSingleton.cs index f9aebff..be8e3ab 100644 --- a/CarRepairShop/CarRepairShopFileImplement/DataFileSingleton.cs +++ b/CarRepairShop/CarRepairShopFileImplement/DataFileSingleton.cs @@ -13,12 +13,16 @@ namespace CarRepairShopFileImplement private readonly string RepairFileName = "Repair.xml"; + private readonly string ShopFileName = "Shop.xml"; + public List Components { get; private set; } public List Orders { get; private set; } public List Repairs { get; private set; } + public List Shops { get; private set; } + public static DataFileSingleton GetInstance() { if (instance == null) @@ -34,11 +38,14 @@ namespace CarRepairShopFileImplement 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)!)!; Repairs = LoadData(RepairFileName, "Repair", x => Repair.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/CarRepairShop/CarRepairShopFileImplement/Implements/ShopStorage.cs b/CarRepairShop/CarRepairShopFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..2d050bb --- /dev/null +++ b/CarRepairShop/CarRepairShopFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,134 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopContracts.ViewModels; +using CarRepairShopFileImplement; +using CarRepairShopDataModels.Models; +using CarRepairShopFileImplement.Models; +using System.Collections.Generic; + +namespace CarRepairShopFileImplement.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 element = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Shops.Remove(element); + source.SaveShops(); + return element.GetViewModel; + } + return null; + } + + public bool MakeSale(IRepairModel model, int count) + { + var repair = source.Repairs.FirstOrDefault(x => x.Id == model.Id); + int countInShops = source.Shops.SelectMany(x => x.ShopRepairs).Sum(y => y.Key == model.Id ? y.Value.Item2 : 0); + + if (repair == null || countInShops < count) + { + return false; + } + + foreach (var shop in source.Shops) + { + var shopRepairs = shop.ShopRepairs.Where(x => x.Key == model.Id); + if (shopRepairs.Any()) + { + var shopRepair = shopRepairs.First(); + int min = Math.Min(shopRepair.Value.Item2, count); + if (min == shopRepair.Value.Item2) + { + shop.ShopRepairs.Remove(shopRepair.Key); + } + else + { + shop.ShopRepairs[shopRepair.Key] = (shopRepair.Value.Item1, shopRepair.Value.Item2 - min); + } + shop.Update(new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpening = shop.DateOpening, + ShopRepairs = shop.ShopRepairs, + RepairMaxAmount = shop.RepairMaxAmount + }); + count -= min; + if (count <= 0) + { + break; + } + } + } + source.SaveShops(); + return true; + } + } +} diff --git a/CarRepairShop/CarRepairShopFileImplement/Models/Shop.cs b/CarRepairShop/CarRepairShopFileImplement/Models/Shop.cs new file mode 100644 index 0000000..ec3d829 --- /dev/null +++ b/CarRepairShop/CarRepairShopFileImplement/Models/Shop.cs @@ -0,0 +1,108 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels.Models; +using System.Xml.Linq; + +namespace CarRepairShopFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + + public string ShopName { get; private set; } = string.Empty; + + public string Address { get; private set; } = string.Empty; + + public DateTime DateOpening { get; private set; } + + public Dictionary Repairs { get; private set; } = new(); + + private Dictionary? _shopRepairs = null; + + public Dictionary ShopRepairs + { + get + { + if (_shopRepairs == null) + { + var source = DataFileSingleton.GetInstance(); + _shopRepairs = Repairs.ToDictionary(x => x.Key, + y => ((source.Repairs.FirstOrDefault(z => z.Id == y.Key) as IRepairModel)!, y.Value)); + } + return _shopRepairs; + } + } + + public int RepairMaxAmount { get; private set; } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + Repairs = model.ShopRepairs.ToDictionary(x => x.Key, x => x.Value.Item2), + RepairMaxAmount = model.RepairMaxAmount + }; + } + + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Shop() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + Address = element.Element("Address")!.Value, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + RepairMaxAmount = Convert.ToInt32(element.Element("RepairMaxAmount")!.Value), + Repairs = element.Element("ShopRepairs")!.Elements("ShopRepair") + .ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + RepairMaxAmount = model.RepairMaxAmount; + Repairs = model.ShopRepairs.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopRepairs = null; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + ShopRepairs = ShopRepairs, + RepairMaxAmount = RepairMaxAmount + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("DateOpening", DateOpening.ToString()), + new XElement("RepairMaxAmount", RepairMaxAmount.ToString()), + new XElement("ShopRepairs", + Repairs.Select(x => new XElement("ShopRepair", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/CarRepairShop/CarRepairShopListImplement/Implements/ShopStorage.cs b/CarRepairShop/CarRepairShopListImplement/Implements/ShopStorage.cs index 80b8c72..9609239 100644 --- a/CarRepairShop/CarRepairShopListImplement/Implements/ShopStorage.cs +++ b/CarRepairShop/CarRepairShopListImplement/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using CarRepairShopContracts.SearchModels; using CarRepairShopContracts.StoragesContracts; using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels.Models; using CarRepairShopListImplement; using CarRepairShopListImplement.Models; @@ -106,5 +107,10 @@ namespace CarRepairShopListImplement.Implements } return null; } + + public bool MakeSale(IRepairModel model, int count) + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopListImplement/Models/Shop.cs b/CarRepairShop/CarRepairShopListImplement/Models/Shop.cs index a0947dc..27e012b 100644 --- a/CarRepairShop/CarRepairShopListImplement/Models/Shop.cs +++ b/CarRepairShop/CarRepairShopListImplement/Models/Shop.cs @@ -20,6 +20,8 @@ namespace CarRepairShopListImplement.Models private set; } = new Dictionary(); + public int RepairMaxAmount { get; private set; } + public static Shop? Create(ShopBindingModel? model) { if (model == null) @@ -32,7 +34,8 @@ namespace CarRepairShopListImplement.Models ShopName = model.ShopName, Address = model.Address, DateOpening = model.DateOpening, - ShopRepairs = model.ShopRepairs + ShopRepairs = model.ShopRepairs, + RepairMaxAmount = model.RepairMaxAmount }; } @@ -46,6 +49,7 @@ namespace CarRepairShopListImplement.Models Address = model.Address; DateOpening = model.DateOpening; ShopRepairs = model.ShopRepairs; + RepairMaxAmount = model.RepairMaxAmount; } public ShopViewModel GetViewModel => new() @@ -54,7 +58,8 @@ namespace CarRepairShopListImplement.Models ShopName = ShopName, Address = Address, DateOpening = DateOpening, - ShopRepairs = ShopRepairs + ShopRepairs = ShopRepairs, + RepairMaxAmount = RepairMaxAmount }; } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopView/FormMain.cs b/CarRepairShop/CarRepairShopView/FormMain.cs index 6d75706..4eeb432 100644 --- a/CarRepairShop/CarRepairShopView/FormMain.cs +++ b/CarRepairShop/CarRepairShopView/FormMain.cs @@ -162,5 +162,14 @@ namespace CarRepairShopView form.ShowDialog(); } } + + private void продажаРемонтовToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSale)); + if (service is FormSale form) + { + form.ShowDialog(); + } + } } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopView/FormMain.designer.cs b/CarRepairShop/CarRepairShopView/FormMain.designer.cs index 68e602d..4639e22 100644 --- a/CarRepairShop/CarRepairShopView/FormMain.designer.cs +++ b/CarRepairShop/CarRepairShopView/FormMain.designer.cs @@ -40,6 +40,7 @@ this.buttonCreateOrder = new System.Windows.Forms.Button(); this.dataGridView = new System.Windows.Forms.DataGridView(); this.buttonRef = new System.Windows.Forms.Button(); + продажаРемонтовToolStripMenuItem = new ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -48,7 +49,8 @@ // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.справочникиToolStripMenuItem, - пополнениеМагазинаToolStripMenuItem }); + пополнениеМагазинаToolStripMenuItem, + продажаРемонтовToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Padding = new System.Windows.Forms.Padding(7, 2, 0, 2); @@ -69,21 +71,21 @@ // компонентыToolStripMenuItem // this.компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(145, 22); this.компонентыToolStripMenuItem.Text = "Компоненты"; this.компонентыToolStripMenuItem.Click += new System.EventHandler(this.КомпонентыToolStripMenuItem_Click); // // ремонтыToolStripMenuItem // this.ремонтыToolStripMenuItem.Name = "ремонтыToolStripMenuItem"; - this.ремонтыToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.ремонтыToolStripMenuItem.Size = new System.Drawing.Size(145, 22); this.ремонтыToolStripMenuItem.Text = "Ремонты"; this.ремонтыToolStripMenuItem.Click += new System.EventHandler(this.РемонтыToolStripMenuItem_Click); // // магазиныToolStripMenuItem // магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - магазиныToolStripMenuItem.Size = new Size(180, 22); + магазиныToolStripMenuItem.Size = new Size(145, 22); магазиныToolStripMenuItem.Text = "Магазины"; магазиныToolStripMenuItem.Click += МагазиныToolStripMenuItem_Click; // @@ -173,6 +175,13 @@ this.buttonRef.UseVisualStyleBackColor = true; this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); // + // продажаРемонтовToolStripMenuItem + // + продажаРемонтовToolStripMenuItem.Name = "продажаРемонтовToolStripMenuItem"; + продажаРемонтовToolStripMenuItem.Size = new Size(143, 20); + продажаРемонтовToolStripMenuItem.Text = "Продажа Ремонтов"; + продажаРемонтовToolStripMenuItem.Click += продажаРемонтовToolStripMenuItem_Click; + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -213,6 +222,7 @@ private System.Windows.Forms.Button buttonRef; private ToolStripMenuItem магазиныToolStripMenuItem; private ToolStripMenuItem пополнениеМагазинаToolStripMenuItem; + private ToolStripMenuItem продажаРемонтовToolStripMenuItem; } } diff --git a/CarRepairShop/CarRepairShopView/FormSale.Designer.cs b/CarRepairShop/CarRepairShopView/FormSale.Designer.cs new file mode 100644 index 0000000..f951c58 --- /dev/null +++ b/CarRepairShop/CarRepairShopView/FormSale.Designer.cs @@ -0,0 +1,127 @@ +namespace CarRepairShopView +{ + partial class FormSale + { + /// + /// 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() + { + buttonCancel = new Button(); + buttonSale = new Button(); + textBoxCount = new TextBox(); + labelCount = new Label(); + comboBoxRepair = new ComboBox(); + labelRepair = new Label(); + SuspendLayout(); + // + // buttonCancel + // + buttonCancel.Location = new Point(253, 83); + buttonCancel.Margin = new Padding(4, 3, 4, 3); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(88, 27); + buttonCancel.TabIndex = 17; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // buttonSale + // + buttonSale.Location = new Point(159, 83); + buttonSale.Margin = new Padding(4, 3, 4, 3); + buttonSale.Name = "buttonSale"; + buttonSale.Size = new Size(88, 27); + buttonSale.TabIndex = 16; + buttonSale.Text = "Продать"; + buttonSale.UseVisualStyleBackColor = true; + buttonSale.Click += ButtonSale_Click; + // + // textBoxCount + // + textBoxCount.Location = new Point(101, 51); + textBoxCount.Margin = new Padding(4, 3, 4, 3); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(252, 23); + textBoxCount.TabIndex = 15; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(13, 54); + labelCount.Margin = new Padding(4, 0, 4, 0); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(78, 15); + labelCount.TabIndex = 14; + labelCount.Text = "Количество :"; + // + // comboBoxRepair + // + comboBoxRepair.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxRepair.FormattingEnabled = true; + comboBoxRepair.Location = new Point(101, 15); + comboBoxRepair.Margin = new Padding(4, 3, 4, 3); + comboBoxRepair.Name = "comboBoxRepair"; + comboBoxRepair.Size = new Size(252, 23); + comboBoxRepair.TabIndex = 13; + // + // labelRepair + // + labelRepair.AutoSize = true; + labelRepair.Location = new Point(13, 19); + labelRepair.Margin = new Padding(4, 0, 4, 0); + labelRepair.Name = "labelRepair"; + labelRepair.Size = new Size(80, 15); + labelRepair.TabIndex = 12; + labelRepair.Text = "Ремонт :"; + // + // FormRepairSale + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(373, 123); + Controls.Add(buttonCancel); + Controls.Add(buttonSale); + Controls.Add(textBoxCount); + Controls.Add(labelCount); + Controls.Add(comboBoxRepair); + Controls.Add(labelRepair); + Name = "FormRepairSale"; + StartPosition = FormStartPosition.CenterScreen; + Text = "Продажа ремонта"; + Load += FormRepairSale_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button buttonCancel; + private Button buttonSale; + private TextBox textBoxCount; + private Label labelCount; + private ComboBox comboBoxRepair; + private Label labelRepair; + } +} \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopView/FormSale.cs b/CarRepairShop/CarRepairShopView/FormSale.cs new file mode 100644 index 0000000..38b1c12 --- /dev/null +++ b/CarRepairShop/CarRepairShopView/FormSale.cs @@ -0,0 +1,87 @@ +using CarRepairShopContracts.BusinessLogicsContracts; +using CarRepairShopContracts.BindingModels; +using Microsoft.Extensions.Logging; + +namespace CarRepairShopView +{ + public partial class FormSale : Form + { + private readonly ILogger _logger; + + private readonly IRepairLogic _logicRepair; + + private readonly IShopLogic _logicShop; + + public FormSale(ILogger logger, IRepairLogic logicRepair, IShopLogic logicShop) + { + InitializeComponent(); + _logger = logger; + _logicRepair = logicRepair; + _logicShop = logicShop; + } + + private void FormRepairSale_Load(object sender, EventArgs e) + { + _logger.LogInformation("Repairs loading"); + try + { + var list = _logicRepair.ReadList(null); + if (list != null) + { + comboBoxRepair.DisplayMember = "RepairName"; + comboBoxRepair.ValueMember = "Id"; + comboBoxRepair.DataSource = list; + comboBoxRepair.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Repairs loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonSale_Click(object sender, EventArgs e) + { + if (comboBoxRepair.SelectedValue == null) + { + MessageBox.Show("Выберите ремонт", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Repair sale"); + try + { + var operationResult = _logicShop.MakeSale( + new RepairBindingModel + { + Id = Convert.ToInt32(comboBoxRepair.SelectedValue) + }, + Convert.ToInt32(textBoxCount.Text) + ); + if (!operationResult) + { + throw new Exception("Ошибка при продаже."); + } + MessageBox.Show("Продажа прошла успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Repair 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/CarRepairShop/CarRepairShopView/FormSale.resx b/CarRepairShop/CarRepairShopView/FormSale.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/CarRepairShop/CarRepairShopView/FormSale.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/CarRepairShop/CarRepairShopView/FormShop.Designer.cs b/CarRepairShop/CarRepairShopView/FormShop.Designer.cs index 3e3562a..753e9dd 100644 --- a/CarRepairShop/CarRepairShopView/FormShop.Designer.cs +++ b/CarRepairShop/CarRepairShopView/FormShop.Designer.cs @@ -41,6 +41,8 @@ ColumnCount = new DataGridViewTextBoxColumn(); buttonSave = new Button(); buttonCancel = new Button(); + textBoxMaximum = new TextBox(); + labelMaximum = new Label(); groupBoxRepairs.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -101,7 +103,7 @@ // groupBoxRepairs // groupBoxRepairs.Controls.Add(dataGridView); - groupBoxRepairs.Location = new Point(4, 100); + groupBoxRepairs.Location = new Point(5, 138); groupBoxRepairs.Margin = new Padding(4, 3, 4, 3); groupBoxRepairs.Name = "groupBoxRepairs"; groupBoxRepairs.Padding = new Padding(4, 3, 4, 3); @@ -150,7 +152,7 @@ // // buttonSave // - buttonSave.Location = new Point(255, 394); + buttonSave.Location = new Point(256, 434); buttonSave.Margin = new Padding(4, 3, 4, 3); buttonSave.Name = "buttonSave"; buttonSave.Size = new Size(88, 27); @@ -161,7 +163,7 @@ // // buttonCancel // - buttonCancel.Location = new Point(359, 394); + buttonCancel.Location = new Point(360, 434); buttonCancel.Margin = new Padding(4, 3, 4, 3); buttonCancel.Name = "buttonCancel"; buttonCancel.Size = new Size(88, 27); @@ -170,11 +172,31 @@ buttonCancel.UseVisualStyleBackColor = true; buttonCancel.Click += ButtonCancel_Click; // + // textBoxMaximum + // + textBoxMaximum.Location = new Point(169, 97); + textBoxMaximum.Margin = new Padding(4, 3, 4, 3); + textBoxMaximum.Name = "textBoxMaximum"; + textBoxMaximum.Size = new Size(175, 23); + textBoxMaximum.TabIndex = 11; + // + // labelMaximum + // + labelMaximum.AutoSize = true; + labelMaximum.Location = new Point(14, 100); + labelMaximum.Margin = new Padding(4, 0, 4, 0); + labelMaximum.Name = "labelMaximum"; + labelMaximum.Size = new Size(147, 15); + labelMaximum.TabIndex = 10; + labelMaximum.Text = "Максимум ремонта :"; + // // FormShop // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(478, 432); + ClientSize = new Size(478, 468); + Controls.Add(textBoxMaximum); + Controls.Add(labelMaximum); Controls.Add(buttonCancel); Controls.Add(buttonSave); Controls.Add(groupBoxRepairs); @@ -209,5 +231,7 @@ private DataGridViewTextBoxColumn ColumnCount; private Button buttonSave; private Button buttonCancel; + private TextBox textBoxMaximum; + private Label labelMaximum; } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopView/FormShop.cs b/CarRepairShop/CarRepairShopView/FormShop.cs index 11c937a..392a71c 100644 --- a/CarRepairShop/CarRepairShopView/FormShop.cs +++ b/CarRepairShop/CarRepairShopView/FormShop.cs @@ -38,6 +38,7 @@ namespace CarRepairShopView { textBoxName.Text = view.ShopName; textBoxAddress.Text = view.Address; + textBoxMaximum.Text = view.RepairMaxAmount.ToString(); dateTimePicker.Value = view.DateOpening; _shopRepairs = view.ShopRepairs ?? new Dictionary(); LoadData(); @@ -89,6 +90,11 @@ namespace CarRepairShopView MessageBox.Show("Заполните дату", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } + if (string.IsNullOrEmpty(textBoxMaximum.Text)) + { + MessageBox.Show("Заполните максимальное количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } _logger.LogInformation("Shop saving"); try { @@ -98,6 +104,7 @@ namespace CarRepairShopView ShopName = textBoxName.Text, Address = textBoxAddress.Text, DateOpening = dateTimePicker.Value, + RepairMaxAmount = Convert.ToInt32(textBoxMaximum.Text), ShopRepairs = _shopRepairs }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); diff --git a/CarRepairShop/CarRepairShopView/Program.cs b/CarRepairShop/CarRepairShopView/Program.cs index 64bbd80..cf574c9 100644 --- a/CarRepairShop/CarRepairShopView/Program.cs +++ b/CarRepairShop/CarRepairShopView/Program.cs @@ -54,6 +54,7 @@ namespace CarRepairShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file