diff --git a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs index 16875ac..5ede0f4 100644 --- a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -17,11 +17,14 @@ namespace CarRepairShopBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IRepairStorage _repairStorage; + private readonly IShopLogic _slogic; + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IRepairStorage repairStorage, IShopLogic shopLogic) { _logger = logger; _orderStorage = orderStorage; + _repairStorage = repairStorage; + _slogic = shopLogic; } public List? ReadList(OrderSearchModel? model) @@ -67,8 +70,9 @@ namespace CarRepairShopBusinessLogic.BusinessLogics private bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) { - - if(model.Status + 1 != newStatus) + var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (viewModel == null) throw new ArgumentNullException(nameof(model)); + if (model.Status + 1 != newStatus) { _logger.LogInformation("Status update operation failed. Incorrect order status"); return false; @@ -78,6 +82,23 @@ namespace CarRepairShopBusinessLogic.BusinessLogics { model.DateImplement = DateTime.Now; } + + if(model.Status == OrderStatus.Готов) + { + model.DateImplement = DateTime.Now; + var repair = _repairStorage.GetElement(new() { Id = viewModel.RepairId }); + if(repair == null) throw new ArgumentNullException(nameof(repair)); + + if(!_slogic.AddRepairs(repair, viewModel.Count)) + { + throw new Exception($"Невозможно выдать изделия, магазины переполнены."); + } + } + else + { + model.DateImplement = viewModel.DateImplement; + } + if(_orderStorage.Update(model) == null) { model.Status--; diff --git a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/ShopLogic.cs b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/ShopLogic.cs index 3425ea6..3e81b94 100644 --- a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -92,6 +92,8 @@ namespace CarRepairShopBusinessLogic.BusinessLogics if (shop == null) return false; + if(count > shop.MaxCountRepairs) throw new ArgumentNullException("Количество добавляемых изделий превышает максимум допустимых"); + if (!shop.ShopRepairs.ContainsKey(repair.Id)) { shop.ShopRepairs[repair.Id] = (repair, count); @@ -106,6 +108,7 @@ namespace CarRepairShopBusinessLogic.BusinessLogics ShopName = shop.ShopName, ShopAddress = shop.ShopAddress, DateOpen = shop.DateOpen, + MaxCountRepairs = shop.MaxCountRepairs, ShopRepairs = shop.ShopRepairs }); return true; @@ -139,5 +142,53 @@ namespace CarRepairShopBusinessLogic.BusinessLogics throw new InvalidOperationException("Магазин с таким названием уже есть"); } } + + public bool AddRepairs(IRepairModel model, int count) + { + if(count <= 0) + { + throw new ArgumentNullException("Количество должно быть больше 0", nameof(count)); + } + _logger.LogInformation("AddRepairs. Repair: {Repair}. Count: {Count}", model?.RepairName, count); + + var capacity = _shopStorage.GetFullList().Select(x => x.MaxCountRepairs - x.ShopRepairs.Select(x => x.Value.Item2).Sum()).Sum() - count; + + if(capacity < 0) + { + _logger.LogWarning("AddRepairs operation failed. Sell {count} Repairs ", capacity); + return false; + } + + foreach(var shop in _shopStorage.GetFullList()) + { + if(shop.MaxCountRepairs - shop.ShopRepairs.Select(x => x.Value.Item2).Sum() < count) + { + if(!AddRepair(new() { Id = shop.Id }, model, shop.MaxCountRepairs - shop.ShopRepairs.Select(x => x.Value.Item2).Sum())){ + _logger.LogWarning("AddRepairs operation failed."); + return false; + } + count -= shop.MaxCountRepairs - shop.ShopRepairs.Select(x => x.Value.Item2).Sum(); + } + else + { + if (!AddRepair(new() { Id = shop.Id}, model, count)) + { + _logger.LogWarning("AddRepairs operation failed."); + return false; + } + count -= count; + } + if (count == 0) + { + return true; + } + } + return true; + } + + public bool SellRepairs(IRepairModel model, int count) + { + return _shopStorage.SellRepairs(model, count); + } } } diff --git a/CarRepairShop/CarRepairShopContracts/BindingModels/OrderBindingModel.cs b/CarRepairShop/CarRepairShopContracts/BindingModels/OrderBindingModel.cs index e8d90dc..857b043 100644 --- a/CarRepairShop/CarRepairShopContracts/BindingModels/OrderBindingModel.cs +++ b/CarRepairShop/CarRepairShopContracts/BindingModels/OrderBindingModel.cs @@ -12,7 +12,7 @@ namespace CarRepairShopContracts.BindingModels { public int Id { get; set; } public int RepairId { get; set; } - + public string RepairName { get; set; } = string.Empty; public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; diff --git a/CarRepairShop/CarRepairShopContracts/BindingModels/ShopBindingModel.cs b/CarRepairShop/CarRepairShopContracts/BindingModels/ShopBindingModel.cs index 703cdb7..12640b7 100644 --- a/CarRepairShop/CarRepairShopContracts/BindingModels/ShopBindingModel.cs +++ b/CarRepairShop/CarRepairShopContracts/BindingModels/ShopBindingModel.cs @@ -13,6 +13,7 @@ namespace CarRepairShopContracts.BindingModels public string ShopName { get; set; } = string.Empty; public string ShopAddress { get; set; } = string.Empty; public DateTime DateOpen { get; set; } = DateTime.Now; + public int MaxCountRepairs { get; set; } public Dictionary ShopRepairs { get; set; } = new(); } } diff --git a/CarRepairShop/CarRepairShopContracts/BusinessLogicsContracts/IShopLogic.cs b/CarRepairShop/CarRepairShopContracts/BusinessLogicsContracts/IShopLogic.cs index b44d23a..da55f9d 100644 --- a/CarRepairShop/CarRepairShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/CarRepairShop/CarRepairShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -19,5 +19,7 @@ namespace CarRepairShopContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool AddRepair(ShopSearchModel model, IRepairModel repair, int count); + bool SellRepairs(IRepairModel model, int count); + bool AddRepairs(IRepairModel model, int count); } } diff --git a/CarRepairShop/CarRepairShopContracts/StoragesContracts/IShopStorage.cs b/CarRepairShop/CarRepairShopContracts/StoragesContracts/IShopStorage.cs index 4205c2c..839a31b 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; using System; using System.Collections.Generic; using System.Linq; @@ -18,5 +19,7 @@ namespace CarRepairShopContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + + bool SellRepairs(IRepairModel model, int count); } } diff --git a/CarRepairShop/CarRepairShopContracts/ViewModels/ShopViewModel.cs b/CarRepairShop/CarRepairShopContracts/ViewModels/ShopViewModel.cs index c6458a6..7f3ad73 100644 --- a/CarRepairShop/CarRepairShopContracts/ViewModels/ShopViewModel.cs +++ b/CarRepairShop/CarRepairShopContracts/ViewModels/ShopViewModel.cs @@ -21,6 +21,9 @@ namespace CarRepairShopContracts.ViewModels [DisplayName("Дата открытия")] public DateTime DateOpen { get; set; } = DateTime.Now; + [DisplayName("Вместимость магазина")] + public int MaxCountRepairs { get; set; } + public Dictionary ShopRepairs { get; set; } = new(); } } diff --git a/CarRepairShop/CarRepairShopDataModels/Models/IShopModel.cs b/CarRepairShop/CarRepairShopDataModels/Models/IShopModel.cs index e62110a..838982a 100644 --- a/CarRepairShop/CarRepairShopDataModels/Models/IShopModel.cs +++ b/CarRepairShop/CarRepairShopDataModels/Models/IShopModel.cs @@ -11,6 +11,7 @@ namespace CarRepairShopDataModels.Models string ShopName { get; } string ShopAddress { get; } DateTime DateOpen { get; } + public int MaxCountRepairs { get; set; } Dictionary ShopRepairs { get; } } } diff --git a/CarRepairShop/CarRepairShopFileImplement/DataFileSingleton.cs b/CarRepairShop/CarRepairShopFileImplement/DataFileSingleton.cs index eae3701..c2837c9 100644 --- a/CarRepairShop/CarRepairShopFileImplement/DataFileSingleton.cs +++ b/CarRepairShop/CarRepairShopFileImplement/DataFileSingleton.cs @@ -14,9 +14,11 @@ namespace CarRepairShopFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; 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) @@ -28,11 +30,13 @@ namespace CarRepairShopFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveRepairs() => SaveData(Repairs, RepairFileName, "Repairs", 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)!)!; 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..5c4af0c --- /dev/null +++ b/CarRepairShop/CarRepairShopFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,114 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels.Models; +using CarRepairShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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 && model.Id == x.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.ShopName.Contains(model.ShopName) || 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 SellRepairs(IRepairModel model, int count) + { + if(_source.Shops.Select(x => x.ShopRepairs.FirstOrDefault(x => x.Key == model.Id).Value.Item2).Sum() < count) + { + return false; + } + + var list = _source.Shops.Where(x => x.ShopRepairs.ContainsKey(model.Id)); + + foreach(var shop in list) + { + if (shop.ShopRepairs[model.Id].Item2 < count) + { + count -= shop.ShopRepairs[model.Id].Item2; + shop.ShopRepairs[model.Id] = (shop.ShopRepairs[model.Id].Item1, 0); + } + else + { + shop.ShopRepairs[model.Id] = (shop.ShopRepairs[model.Id].Item1, shop.ShopRepairs[model.Id].Item2 - count); + count -= count; + } + + Update(new() + { + ShopName = shop.ShopName, + ShopAddress = shop.ShopAddress, + DateOpen = shop.DateOpen, + MaxCountRepairs = shop.MaxCountRepairs, + ShopRepairs = shop.ShopRepairs, + }); + + if(count == 0) + { + return true; + } + } + return true; + } + } +} diff --git a/CarRepairShop/CarRepairShopFileImplement/Models/Shop.cs b/CarRepairShop/CarRepairShopFileImplement/Models/Shop.cs new file mode 100644 index 0000000..554e1b3 --- /dev/null +++ b/CarRepairShop/CarRepairShopFileImplement/Models/Shop.cs @@ -0,0 +1,101 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace CarRepairShopFileImplement.Models +{ + internal class Shop : IShopModel + { + public int Id { get; set; } + + public string ShopName { get; set; } = string.Empty; + + public string ShopAddress { get; set; } = string.Empty; + + public DateTime DateOpen { get; set; } + + public int MaxCountRepairs { get; set; } + + public Dictionary countRepair { get; private set; } = new(); + + public Dictionary _repairs = null; + + public Dictionary ShopRepairs + { + get + { + if(_repairs == null) + { + var source = DataFileSingleton.GetInstance(); + _repairs = countRepair.ToDictionary(x => x.Key, y => ((source.Repairs.FirstOrDefault(z => z.Id == y.Key) as IRepairModel)!, y.Value)); + } + return _repairs; + } + } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) return null; + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + ShopAddress = model.ShopAddress, + DateOpen = model.DateOpen, + MaxCountRepairs = model.MaxCountRepairs, + countRepair = model.ShopRepairs.ToDictionary(x => x.Key, x=> x.Value.Item2) + }; + } + 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, + ShopAddress = element.Element("ShopAddress")!.Value, + DateOpen = Convert.ToDateTime(element.Element("DateOpen")!.Value), + MaxCountRepairs = Convert.ToInt32(element.Element("MaxCountRepairs")!.Value), + countRepair = element.Element("ShopRepairs")!.Elements("ShopRepairs").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; + ShopAddress = model.ShopAddress; + DateOpen = model.DateOpen; + MaxCountRepairs = model.MaxCountRepairs; + countRepair = model.ShopRepairs.ToDictionary(x => x.Key, x => x.Value.Item2); + _repairs = null; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + ShopAddress = ShopAddress, + DateOpen = DateOpen, + MaxCountRepairs = MaxCountRepairs, + ShopRepairs = ShopRepairs + }; + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("ShopAddress", ShopAddress), + new XElement("DateOpen", DateOpen.ToString()), + new XElement("MaxCountRepairs", MaxCountRepairs.ToString()), + new XElement("ShopRepairs", countRepair.Select(x => new XElement("ShopRepairs", + 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 45c7f54..9cc4f05 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.Models; using System; using System.Collections.Generic; @@ -102,5 +103,10 @@ namespace CarRepairShopListImplement.Implements } return null; } + + public bool SellRepairs(IRepairModel model, int count) + { + throw new NotImplementedException(); + } } } diff --git a/CarRepairShop/CarRepairShopListImplement/Models/Shop.cs b/CarRepairShop/CarRepairShopListImplement/Models/Shop.cs index 18db0e8..0888bba 100644 --- a/CarRepairShop/CarRepairShopListImplement/Models/Shop.cs +++ b/CarRepairShop/CarRepairShopListImplement/Models/Shop.cs @@ -15,6 +15,7 @@ namespace CarRepairShopListImplement.Models public string ShopName { get; private set; } = string.Empty; public string ShopAddress { get; private set; } = string.Empty; public DateTime DateOpen { get; private set; } = DateTime.Now; + public int MaxCountRepairs { get; set; } public Dictionary ShopRepairs { get; private set; } = new Dictionary(); public static Shop? Create(ShopBindingModel? model) @@ -46,5 +47,6 @@ namespace CarRepairShopListImplement.Models DateOpen = DateOpen, ShopRepairs = ShopRepairs }; + } } diff --git a/CarRepairShop/CarRepairShopView/FormMain.Designer.cs b/CarRepairShop/CarRepairShopView/FormMain.Designer.cs index ce990da..8595078 100644 --- a/CarRepairShop/CarRepairShopView/FormMain.Designer.cs +++ b/CarRepairShop/CarRepairShopView/FormMain.Designer.cs @@ -40,6 +40,7 @@ buttonOrderReady = new Button(); buttonIssuedOrder = new Button(); buttonRef = new Button(); + buttonSellRepairs = new Button(); menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -158,11 +159,23 @@ buttonRef.UseVisualStyleBackColor = true; buttonRef.Click += buttonRef_Click; // + // buttonSellRepairs + // + buttonSellRepairs.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonSellRepairs.Location = new Point(844, 323); + buttonSellRepairs.Name = "buttonSellRepairs"; + buttonSellRepairs.Size = new Size(198, 28); + buttonSellRepairs.TabIndex = 7; + buttonSellRepairs.Text = "Продать услуги"; + buttonSellRepairs.UseVisualStyleBackColor = true; + buttonSellRepairs.Click += buttonSellRepairs_Click; + // // FormMain // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1059, 377); + Controls.Add(buttonSellRepairs); Controls.Add(buttonRef); Controls.Add(buttonIssuedOrder); Controls.Add(buttonOrderReady); @@ -195,5 +208,6 @@ private Button buttonRef; private ToolStripMenuItem ShopsToolStripMenuItem; private ToolStripMenuItem FillShopToolStripMenuItem; + private Button buttonSellRepairs; } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopView/FormMain.cs b/CarRepairShop/CarRepairShopView/FormMain.cs index 94954f9..8638e78 100644 --- a/CarRepairShop/CarRepairShopView/FormMain.cs +++ b/CarRepairShop/CarRepairShopView/FormMain.cs @@ -187,5 +187,15 @@ namespace CarRepairShopView form.ShowDialog(); } } + + private void buttonSellRepairs_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellRepairs)); + if (service is FormSellRepairs form) + { + form.ShowDialog(); + LoadData(); + } + } } } diff --git a/CarRepairShop/CarRepairShopView/FormSellRepairs.Designer.cs b/CarRepairShop/CarRepairShopView/FormSellRepairs.Designer.cs new file mode 100644 index 0000000..a107545 --- /dev/null +++ b/CarRepairShop/CarRepairShopView/FormSellRepairs.Designer.cs @@ -0,0 +1,119 @@ +namespace CarRepairShopView +{ + partial class FormSellRepairs + { + /// + /// 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() + { + label1 = new Label(); + label2 = new Label(); + comboBoxRepairs = new ComboBox(); + textBoxCount = new TextBox(); + buttonSell = new Button(); + buttonCancel = new Button(); + SuspendLayout(); + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(17, 17); + label1.Name = "label1"; + label1.Size = new Size(51, 15); + label1.TabIndex = 0; + label1.Text = "Ремонт:"; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(19, 62); + label2.Name = "label2"; + label2.Size = new Size(49, 15); + label2.TabIndex = 1; + label2.Text = "Кол-во:"; + // + // comboBoxRepairs + // + comboBoxRepairs.FormattingEnabled = true; + comboBoxRepairs.Location = new Point(74, 12); + comboBoxRepairs.Name = "comboBoxRepairs"; + comboBoxRepairs.Size = new Size(149, 23); + comboBoxRepairs.TabIndex = 2; + // + // textBoxCount + // + textBoxCount.Location = new Point(74, 59); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(149, 23); + textBoxCount.TabIndex = 3; + // + // buttonSell + // + buttonSell.Location = new Point(148, 92); + buttonSell.Name = "buttonSell"; + buttonSell.Size = new Size(75, 28); + buttonSell.TabIndex = 4; + buttonSell.Text = "Продать"; + buttonSell.UseVisualStyleBackColor = true; + buttonSell.Click += buttonSell_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(19, 92); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(75, 28); + buttonCancel.TabIndex = 5; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // FormSellRepairs + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(249, 125); + Controls.Add(buttonCancel); + Controls.Add(buttonSell); + Controls.Add(textBoxCount); + Controls.Add(comboBoxRepairs); + Controls.Add(label2); + Controls.Add(label1); + Name = "FormSellRepairs"; + Text = "Продать услуги"; + Load += FormSellRepairs_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label label1; + private Label label2; + private ComboBox comboBoxRepairs; + private TextBox textBoxCount; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopView/FormSellRepairs.cs b/CarRepairShop/CarRepairShopView/FormSellRepairs.cs new file mode 100644 index 0000000..3a48887 --- /dev/null +++ b/CarRepairShop/CarRepairShopView/FormSellRepairs.cs @@ -0,0 +1,95 @@ +using CarRepairShopContracts.BusinessLogicsContracts; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.StoragesContracts; +using Microsoft.Extensions.Logging; +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 CarRepairShopView +{ + public partial class FormSellRepairs : Form + { + private readonly IRepairLogic _rlogic; + private readonly IShopLogic _slogic; + private readonly ILogger _logger; + public FormSellRepairs(ILogger logger, IRepairLogic repairLogic, IShopLogic shopLogic) + { + InitializeComponent(); + _rlogic = repairLogic; + _slogic = shopLogic; + _logger = logger; + } + + private void FormSellRepairs_Load(object sender, EventArgs e) + { + try + { + var list = _rlogic.ReadList(null); + if (list != null) + { + comboBoxRepairs.DisplayMember = "RepairName"; + comboBoxRepairs.ValueMember = "Id"; + comboBoxRepairs.DataSource = list; + comboBoxRepairs.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки списка"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonSell_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (comboBoxRepairs.SelectedValue == null) + { + MessageBox.Show("Выберите Ремонт", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _logger.LogInformation("Продажа"); + + try + { + var operationResult = _slogic.SellRepairs(_rlogic.ReadElement(new RepairSearchModel() + { + Id = Convert.ToInt32(comboBoxRepairs.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, "Ошибка продажи"); + 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/FormSellRepairs.resx b/CarRepairShop/CarRepairShopView/FormSellRepairs.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/CarRepairShop/CarRepairShopView/FormSellRepairs.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 c3a3f7c..daec11c 100644 --- a/CarRepairShop/CarRepairShopView/FormShop.Designer.cs +++ b/CarRepairShop/CarRepairShopView/FormShop.Designer.cs @@ -35,14 +35,17 @@ label3 = new Label(); dateTimePickerDateOpen = new DateTimePicker(); groupBox = new GroupBox(); - buttonSave = new Button(); - buttonCancel = new Button(); dataGridView = new DataGridView(); ColumnId = new DataGridViewTextBoxColumn(); ColumnName = new DataGridViewTextBoxColumn(); ColumnCount = new DataGridViewTextBoxColumn(); + buttonSave = new Button(); + buttonCancel = new Button(); + label4 = new Label(); + numericUpDownCount = new NumericUpDown(); groupBox.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDownCount).BeginInit(); SuspendLayout(); // // textBoxName @@ -96,33 +99,13 @@ // groupBox // groupBox.Controls.Add(dataGridView); - groupBox.Location = new Point(12, 47); + groupBox.Location = new Point(12, 94); groupBox.Name = "groupBox"; groupBox.Size = new Size(611, 274); groupBox.TabIndex = 8; groupBox.TabStop = false; groupBox.Text = "Ремонты"; // - // buttonSave - // - buttonSave.Location = new Point(530, 327); - buttonSave.Name = "buttonSave"; - buttonSave.Size = new Size(93, 32); - buttonSave.TabIndex = 9; - buttonSave.Text = "Сохранить"; - buttonSave.UseVisualStyleBackColor = true; - buttonSave.Click += buttonSave_Click; - // - // buttonCancel - // - buttonCancel.Location = new Point(431, 327); - buttonCancel.Name = "buttonCancel"; - buttonCancel.Size = new Size(93, 32); - buttonCancel.TabIndex = 10; - buttonCancel.Text = "Отмена"; - buttonCancel.UseVisualStyleBackColor = true; - buttonCancel.Click += buttonCancel_Click; - // // dataGridView // dataGridView.AllowUserToAddRows = false; @@ -159,11 +142,49 @@ ColumnCount.Name = "ColumnCount"; ColumnCount.ReadOnly = true; // + // buttonSave + // + buttonSave.Location = new Point(530, 374); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(93, 32); + buttonSave.TabIndex = 9; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(431, 374); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(93, 32); + buttonCancel.TabIndex = 10; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // label4 + // + label4.AutoSize = true; + label4.Location = new Point(17, 55); + label4.Name = "label4"; + label4.Size = new Size(83, 15); + label4.TabIndex = 11; + label4.Text = "Вместимость:"; + // + // numericUpDownCount + // + numericUpDownCount.Location = new Point(106, 51); + numericUpDownCount.Name = "numericUpDownCount"; + numericUpDownCount.Size = new Size(120, 23); + numericUpDownCount.TabIndex = 13; + // // FormShop // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(635, 368); + ClientSize = new Size(635, 418); + Controls.Add(numericUpDownCount); + Controls.Add(label4); Controls.Add(buttonCancel); Controls.Add(buttonSave); Controls.Add(groupBox); @@ -178,6 +199,7 @@ Load += FormShop_Load; groupBox.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDownCount).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -197,5 +219,7 @@ private DataGridViewTextBoxColumn ColumnId; private DataGridViewTextBoxColumn ColumnName; private DataGridViewTextBoxColumn ColumnCount; + private Label label4; + private NumericUpDown numericUpDownCount; } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopView/FormShop.cs b/CarRepairShop/CarRepairShopView/FormShop.cs index d139762..e83716e 100644 --- a/CarRepairShop/CarRepairShopView/FormShop.cs +++ b/CarRepairShop/CarRepairShopView/FormShop.cs @@ -46,6 +46,7 @@ namespace CarRepairShopView textBoxName.Text = view.ShopName; textBoxAddress.Text = view.ShopAddress; dateTimePickerDateOpen.Value = view.DateOpen; + numericUpDownCount.Value = view.MaxCountRepairs; _shopRepairs = view.ShopRepairs ?? new Dictionary(); LoadData(); } @@ -106,6 +107,7 @@ namespace CarRepairShopView ShopName = textBoxName.Text, ShopAddress = textBoxAddress.Text, DateOpen = dateTimePickerDateOpen.Value.Date, + MaxCountRepairs = (int)numericUpDownCount.Value, 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 32b786b..df705ad 100644 --- a/CarRepairShop/CarRepairShopView/Program.cs +++ b/CarRepairShop/CarRepairShopView/Program.cs @@ -55,6 +55,7 @@ namespace CarRepairShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file