diff --git a/IceCreamShop/IceCreamShop/MainForm.Designer.cs b/IceCreamShop/IceCreamShop/MainForm.Designer.cs index c419e35..72d96f3 100644 --- a/IceCreamShop/IceCreamShop/MainForm.Designer.cs +++ b/IceCreamShop/IceCreamShop/MainForm.Designer.cs @@ -40,6 +40,7 @@ ReadyButton = new Button(); IssuedButton = new Button(); RefreshButton = new Button(); + продажиToolStripMenuItem = new ToolStripMenuItem(); menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); SuspendLayout(); @@ -50,60 +51,62 @@ menuStrip1.Items.AddRange(new ToolStripItem[] { ToolStripMenu }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; - menuStrip1.Padding = new Padding(6, 3, 0, 3); - menuStrip1.Size = new Size(1296, 30); + menuStrip1.Padding = new Padding(5, 2, 0, 2); + menuStrip1.Size = new Size(1134, 24); menuStrip1.TabIndex = 0; menuStrip1.Text = "menuStrip1"; // // ToolStripMenu // - ToolStripMenu.DropDownItems.AddRange(new ToolStripItem[] { КомпонентыStripMenuItem, МороженноеStripMenuItem, магазиныToolStripMenuItem, поставкиToolStripMenuItem }); + ToolStripMenu.DropDownItems.AddRange(new ToolStripItem[] { КомпонентыStripMenuItem, МороженноеStripMenuItem, магазиныToolStripMenuItem, поставкиToolStripMenuItem, продажиToolStripMenuItem }); ToolStripMenu.Name = "ToolStripMenu"; - ToolStripMenu.Size = new Size(117, 24); + ToolStripMenu.Size = new Size(94, 20); ToolStripMenu.Text = "Справочники"; // // КомпонентыStripMenuItem // КомпонентыStripMenuItem.Name = "КомпонентыStripMenuItem"; - КомпонентыStripMenuItem.Size = new Size(186, 26); + КомпонентыStripMenuItem.Size = new Size(180, 22); КомпонентыStripMenuItem.Text = "Компоненты"; КомпонентыStripMenuItem.Click += КомпонентыStripMenuItem_Click; // // МороженноеStripMenuItem // МороженноеStripMenuItem.Name = "МороженноеStripMenuItem"; - МороженноеStripMenuItem.Size = new Size(186, 26); + МороженноеStripMenuItem.Size = new Size(180, 22); МороженноеStripMenuItem.Text = "Мороженное"; МороженноеStripMenuItem.Click += МороженноеStripMenuItem_Click; // // магазиныToolStripMenuItem // магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - магазиныToolStripMenuItem.Size = new Size(186, 26); + магазиныToolStripMenuItem.Size = new Size(180, 22); магазиныToolStripMenuItem.Text = "Магазины"; магазиныToolStripMenuItem.Click += магазиныToolStripMenuItem_Click; // // поставкиToolStripMenuItem // поставкиToolStripMenuItem.Name = "поставкиToolStripMenuItem"; - поставкиToolStripMenuItem.Size = new Size(186, 26); + поставкиToolStripMenuItem.Size = new Size(180, 22); поставкиToolStripMenuItem.Text = "Поставки"; поставкиToolStripMenuItem.Click += поставкиToolStripMenuItem_Click; // // DataGridView // DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - DataGridView.Location = new Point(11, 31); + DataGridView.Location = new Point(10, 23); + DataGridView.Margin = new Padding(3, 2, 3, 2); DataGridView.Name = "DataGridView"; DataGridView.RowHeadersWidth = 51; - DataGridView.Size = new Size(1007, 407); + DataGridView.Size = new Size(881, 305); DataGridView.TabIndex = 1; // // CreateOrderButton // - CreateOrderButton.Location = new Point(1025, 31); + CreateOrderButton.Location = new Point(897, 23); + CreateOrderButton.Margin = new Padding(3, 2, 3, 2); CreateOrderButton.Name = "CreateOrderButton"; - CreateOrderButton.Size = new Size(259, 29); + CreateOrderButton.Size = new Size(227, 22); CreateOrderButton.TabIndex = 2; CreateOrderButton.Text = "Создать заказ"; CreateOrderButton.UseVisualStyleBackColor = true; @@ -111,9 +114,10 @@ // // TakeInWorkButton // - TakeInWorkButton.Location = new Point(1025, 67); + TakeInWorkButton.Location = new Point(897, 50); + TakeInWorkButton.Margin = new Padding(3, 2, 3, 2); TakeInWorkButton.Name = "TakeInWorkButton"; - TakeInWorkButton.Size = new Size(259, 29); + TakeInWorkButton.Size = new Size(227, 22); TakeInWorkButton.TabIndex = 3; TakeInWorkButton.Text = "Отдать заказ в работу"; TakeInWorkButton.UseVisualStyleBackColor = true; @@ -121,9 +125,10 @@ // // ReadyButton // - ReadyButton.Location = new Point(1025, 101); + ReadyButton.Location = new Point(897, 76); + ReadyButton.Margin = new Padding(3, 2, 3, 2); ReadyButton.Name = "ReadyButton"; - ReadyButton.Size = new Size(259, 29); + ReadyButton.Size = new Size(227, 22); ReadyButton.TabIndex = 4; ReadyButton.Text = "Заказ готов"; ReadyButton.UseVisualStyleBackColor = true; @@ -131,9 +136,10 @@ // // IssuedButton // - IssuedButton.Location = new Point(1025, 136); + IssuedButton.Location = new Point(897, 102); + IssuedButton.Margin = new Padding(3, 2, 3, 2); IssuedButton.Name = "IssuedButton"; - IssuedButton.Size = new Size(259, 29); + IssuedButton.Size = new Size(227, 22); IssuedButton.TabIndex = 5; IssuedButton.Text = "Заказ выдан"; IssuedButton.UseVisualStyleBackColor = true; @@ -141,19 +147,27 @@ // // RefreshButton // - RefreshButton.Location = new Point(1025, 171); + RefreshButton.Location = new Point(897, 128); + RefreshButton.Margin = new Padding(3, 2, 3, 2); RefreshButton.Name = "RefreshButton"; - RefreshButton.Size = new Size(259, 29); + RefreshButton.Size = new Size(227, 22); RefreshButton.TabIndex = 6; RefreshButton.Text = "Обновить"; RefreshButton.UseVisualStyleBackColor = true; RefreshButton.Click += RefreshButton_Click; // + // продажиToolStripMenuItem + // + продажиToolStripMenuItem.Name = "продажиToolStripMenuItem"; + продажиToolStripMenuItem.Size = new Size(180, 22); + продажиToolStripMenuItem.Text = "Продажи"; + продажиToolStripMenuItem.Click += продажиToolStripMenuItem_Click; + // // MainForm // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1296, 451); + ClientSize = new Size(1134, 338); Controls.Add(RefreshButton); Controls.Add(IssuedButton); Controls.Add(ReadyButton); @@ -162,6 +176,7 @@ Controls.Add(DataGridView); Controls.Add(menuStrip1); MainMenuStrip = menuStrip1; + Margin = new Padding(3, 2, 3, 2); Name = "MainForm"; Text = "Форма главная"; Load += MainForm_Load; @@ -186,5 +201,6 @@ private Button RefreshButton; private ToolStripMenuItem магазиныToolStripMenuItem; private ToolStripMenuItem поставкиToolStripMenuItem; + private ToolStripMenuItem продажиToolStripMenuItem; } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/MainForm.cs b/IceCreamShop/IceCreamShop/MainForm.cs index e523217..cec910d 100644 --- a/IceCreamShop/IceCreamShop/MainForm.cs +++ b/IceCreamShop/IceCreamShop/MainForm.cs @@ -198,5 +198,14 @@ Program.ServiceProvider?.GetService(typeof(OrderForm)); form.ShowDialog(); } } + + private void продажиToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(SellForm)); + if (service is SellForm form) + { + form.ShowDialog(); + } + } } } diff --git a/IceCreamShop/IceCreamShop/Program.cs b/IceCreamShop/IceCreamShop/Program.cs index 0f67844..51f1264 100644 --- a/IceCreamShop/IceCreamShop/Program.cs +++ b/IceCreamShop/IceCreamShop/Program.cs @@ -34,8 +34,8 @@ namespace IceCreamShop option.AddNLog("nlog.config"); }); services.AddTransient(); - services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -51,6 +51,7 @@ namespace IceCreamShop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } diff --git a/IceCreamShop/IceCreamShop/SellForm.Designer.cs b/IceCreamShop/IceCreamShop/SellForm.Designer.cs new file mode 100644 index 0000000..95509c4 --- /dev/null +++ b/IceCreamShop/IceCreamShop/SellForm.Designer.cs @@ -0,0 +1,117 @@ +namespace IceCreamShop +{ + partial class SellForm + { + /// + /// 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() + { + IceCreamComboBox = new ComboBox(); + IceCreamLabel = new Label(); + CountLabel = new Label(); + CountTextBox = new TextBox(); + SaveButton = new Button(); + CancelButton = new Button(); + SuspendLayout(); + // + // IceCreamComboBox + // + IceCreamComboBox.FormattingEnabled = true; + IceCreamComboBox.Location = new Point(99, 12); + IceCreamComboBox.Name = "IceCreamComboBox"; + IceCreamComboBox.Size = new Size(121, 23); + IceCreamComboBox.TabIndex = 0; + // + // IceCreamLabel + // + IceCreamLabel.AutoSize = true; + IceCreamLabel.Location = new Point(12, 20); + IceCreamLabel.Name = "IceCreamLabel"; + IceCreamLabel.Size = new Size(81, 15); + IceCreamLabel.TabIndex = 1; + IceCreamLabel.Text = "Мороженное"; + // + // CountLabel + // + CountLabel.AutoSize = true; + CountLabel.Location = new Point(21, 49); + CountLabel.Name = "CountLabel"; + CountLabel.Size = new Size(72, 15); + CountLabel.TabIndex = 2; + CountLabel.Text = "Количество"; + // + // CountTextBox + // + CountTextBox.Location = new Point(99, 41); + CountTextBox.Name = "CountTextBox"; + CountTextBox.Size = new Size(121, 23); + CountTextBox.TabIndex = 3; + // + // SaveButton + // + SaveButton.Location = new Point(64, 73); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(75, 23); + SaveButton.TabIndex = 4; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += SaveButton_Click; + // + // CancelButton + // + CancelButton.Location = new Point(145, 73); + CancelButton.Name = "CancelButton"; + CancelButton.Size = new Size(75, 23); + CancelButton.TabIndex = 5; + CancelButton.Text = "Отмена"; + CancelButton.UseVisualStyleBackColor = true; + // + // SellForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(224, 108); + Controls.Add(CancelButton); + Controls.Add(SaveButton); + Controls.Add(CountTextBox); + Controls.Add(CountLabel); + Controls.Add(IceCreamLabel); + Controls.Add(IceCreamComboBox); + Name = "SellForm"; + Text = "Форма продажи"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private ComboBox IceCreamComboBox; + private Label IceCreamLabel; + private Label CountLabel; + private TextBox CountTextBox; + private Button SaveButton; + private Button CancelButton; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/SellForm.cs b/IceCreamShop/IceCreamShop/SellForm.cs new file mode 100644 index 0000000..96143c6 --- /dev/null +++ b/IceCreamShop/IceCreamShop/SellForm.cs @@ -0,0 +1,117 @@ +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; +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 IceCreamShop +{ + public partial class SellForm : Form + { + private readonly List? _iceCreamList; + IShopLogic _shopLogic; + IIceCreamLogic _iceCreamLogic; + public SellForm(IIceCreamLogic iceCreamLogic, IShopLogic shopLogic) + { + InitializeComponent(); + _shopLogic = shopLogic; + _iceCreamLogic = iceCreamLogic; + _iceCreamList = iceCreamLogic.ReadList(null); + if (_iceCreamList != null) + { + IceCreamComboBox.DisplayMember = "IceCreamName"; + IceCreamComboBox.ValueMember = "Id"; + IceCreamComboBox.DataSource = _iceCreamList; + IceCreamComboBox.SelectedItem = null; + } + } + public int IceCreamId + { + get + { + return + Convert.ToInt32(IceCreamComboBox.SelectedValue); + } + set + { + IceCreamComboBox.SelectedValue = value; + } + } + + public IIceCreamModel? IceCreamModel + { + get + { + if (_iceCreamList == null) + { + return null; + } + foreach (var elem in _iceCreamList) + { + if (elem.Id == IceCreamId) + { + return elem; + } + } + return null; + } + } + + + public int Count + { + get { return Convert.ToInt32(CountTextBox.Text); } + set + { CountTextBox.Text = value.ToString(); } + } + + private void SaveButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(CountTextBox.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (IceCreamComboBox.SelectedValue == null) + { + MessageBox.Show("Выберите мороженное", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + try + { + int count = Convert.ToInt32(CountTextBox.Text); + + bool res = _shopLogic.MakeSell( + _iceCreamLogic.ReadElement(new() { Id = Convert.ToInt32(IceCreamComboBox.SelectedValue) }), + count + ); + + if (!res) + { + throw new Exception("Ошибка при продаже."); + } + + MessageBox.Show("Продажа прошла успешно"); + DialogResult = DialogResult.OK; + Close(); + + } + catch (Exception err) + { + MessageBox.Show("Ошибка продажи"); + return; + } + } + } +} diff --git a/IceCreamShop/IceCreamShop/SellForm.resx b/IceCreamShop/IceCreamShop/SellForm.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/IceCreamShop/IceCreamShop/SellForm.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/IceCreamShop/IceCreamShop/ShopForm.Designer.cs b/IceCreamShop/IceCreamShop/ShopForm.Designer.cs index e2dec45..a2c5b56 100644 --- a/IceCreamShop/IceCreamShop/ShopForm.Designer.cs +++ b/IceCreamShop/IceCreamShop/ShopForm.Designer.cs @@ -41,66 +41,65 @@ Название = new DataGridViewTextBoxColumn(); Цена = new DataGridViewTextBoxColumn(); Количество = new DataGridViewTextBoxColumn(); + CapacityUpDown = new NumericUpDown(); + CapacityLabel = new Label(); ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + ((System.ComponentModel.ISupportInitialize)CapacityUpDown).BeginInit(); SuspendLayout(); // // DateTimePicker // - DateTimePicker.Location = new Point(97, 93); - DateTimePicker.Margin = new Padding(3, 4, 3, 4); + DateTimePicker.Location = new Point(85, 70); DateTimePicker.Name = "DateTimePicker"; - DateTimePicker.Size = new Size(363, 27); + DateTimePicker.Size = new Size(318, 23); DateTimePicker.TabIndex = 0; // // NameTextBox // - NameTextBox.Location = new Point(97, 16); - NameTextBox.Margin = new Padding(3, 4, 3, 4); + NameTextBox.Location = new Point(85, 12); NameTextBox.Name = "NameTextBox"; - NameTextBox.Size = new Size(363, 27); + NameTextBox.Size = new Size(318, 23); NameTextBox.TabIndex = 1; // // AddressTextBox // - AddressTextBox.Location = new Point(97, 55); - AddressTextBox.Margin = new Padding(3, 4, 3, 4); + AddressTextBox.Location = new Point(85, 41); AddressTextBox.Name = "AddressTextBox"; - AddressTextBox.Size = new Size(363, 27); + AddressTextBox.Size = new Size(318, 23); AddressTextBox.TabIndex = 2; // // NameLabel // NameLabel.AutoSize = true; - NameLabel.Location = new Point(14, 20); + NameLabel.Location = new Point(12, 15); NameLabel.Name = "NameLabel"; - NameLabel.Size = new Size(77, 20); + NameLabel.Size = new Size(59, 15); NameLabel.TabIndex = 3; NameLabel.Text = "Название"; // // AdressLabel // AdressLabel.AutoSize = true; - AdressLabel.Location = new Point(14, 59); + AdressLabel.Location = new Point(12, 44); AdressLabel.Name = "AdressLabel"; - AdressLabel.Size = new Size(51, 20); + AdressLabel.Size = new Size(40, 15); AdressLabel.TabIndex = 4; AdressLabel.Text = "Адрес"; // // DateLabel // DateLabel.AutoSize = true; - DateLabel.Location = new Point(14, 101); + DateLabel.Location = new Point(12, 76); DateLabel.Name = "DateLabel"; - DateLabel.Size = new Size(41, 20); + DateLabel.Size = new Size(32, 15); DateLabel.TabIndex = 5; DateLabel.Text = "Дата"; // // SaveButton // - SaveButton.Location = new Point(282, 340); - SaveButton.Margin = new Padding(3, 4, 3, 4); + SaveButton.Location = new Point(247, 284); SaveButton.Name = "SaveButton"; - SaveButton.Size = new Size(86, 31); + SaveButton.Size = new Size(75, 23); SaveButton.TabIndex = 6; SaveButton.Text = "Сохранить"; SaveButton.UseVisualStyleBackColor = true; @@ -108,10 +107,9 @@ // // CancelButton // - CancelButton.Location = new Point(375, 340); - CancelButton.Margin = new Padding(3, 4, 3, 4); + CancelButton.Location = new Point(328, 284); CancelButton.Name = "CancelButton"; - CancelButton.Size = new Size(86, 31); + CancelButton.Size = new Size(75, 23); CancelButton.TabIndex = 7; CancelButton.Text = "Отмена"; CancelButton.UseVisualStyleBackColor = true; @@ -121,11 +119,10 @@ // DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; DataGridView.Columns.AddRange(new DataGridViewColumn[] { Column1, Название, Цена, Количество }); - DataGridView.Location = new Point(14, 132); - DataGridView.Margin = new Padding(3, 4, 3, 4); + DataGridView.Location = new Point(12, 128); DataGridView.Name = "DataGridView"; DataGridView.RowHeadersWidth = 51; - DataGridView.Size = new Size(447, 200); + DataGridView.Size = new Size(391, 150); DataGridView.TabIndex = 8; // // Column1 @@ -160,11 +157,30 @@ Количество.ReadOnly = true; Количество.Width = 125; // + // CapacityUpDown + // + CapacityUpDown.Location = new Point(98, 99); + CapacityUpDown.Maximum = new decimal(new int[] { 10000, 0, 0, 0 }); + CapacityUpDown.Name = "CapacityUpDown"; + CapacityUpDown.Size = new Size(120, 23); + CapacityUpDown.TabIndex = 9; + // + // CapacityLabel + // + CapacityLabel.AutoSize = true; + CapacityLabel.Location = new Point(12, 107); + CapacityLabel.Name = "CapacityLabel"; + CapacityLabel.Size = new Size(80, 15); + CapacityLabel.TabIndex = 10; + CapacityLabel.Text = "Вместимость"; + // // ShopForm // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(470, 380); + ClientSize = new Size(411, 315); + Controls.Add(CapacityLabel); + Controls.Add(CapacityUpDown); Controls.Add(DataGridView); Controls.Add(CancelButton); Controls.Add(SaveButton); @@ -174,11 +190,11 @@ Controls.Add(AddressTextBox); Controls.Add(NameTextBox); Controls.Add(DateTimePicker); - Margin = new Padding(3, 4, 3, 4); Name = "ShopForm"; Text = "Форма магазина"; Load += ShopForm_Load; ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ((System.ComponentModel.ISupportInitialize)CapacityUpDown).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -198,5 +214,7 @@ private DataGridViewTextBoxColumn Название; private DataGridViewTextBoxColumn Цена; private DataGridViewTextBoxColumn Количество; + private NumericUpDown CapacityUpDown; + private Label CapacityLabel; } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/ShopForm.cs b/IceCreamShop/IceCreamShop/ShopForm.cs index 7af90da..9477799 100644 --- a/IceCreamShop/IceCreamShop/ShopForm.cs +++ b/IceCreamShop/IceCreamShop/ShopForm.cs @@ -42,7 +42,8 @@ namespace IceCreamShop NameTextBox.Text = shop.ShopName; AddressTextBox.Text = shop.Address; DateTimePicker.Text = shop.DateOpen.ToString(); - _iceCreams = shop.ShopIceCreams; + CapacityUpDown.Value = shop.MaxCapacity; + _iceCreams = shop.ShopIceCreams ?? new Dictionary(); } LoadData(); } @@ -103,7 +104,7 @@ namespace IceCreamShop ShopName = NameTextBox.Text, Address = AddressTextBox.Text, DateOpen = DateTimePicker.Value.Date, - ShopIceCreams = _iceCreams + MaxCapacity = Convert.ToInt32(CapacityUpDown.Value), }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/OrderLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/OrderLogic.cs index ef27b2a..78c7efc 100644 --- a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/OrderLogic.cs @@ -5,6 +5,7 @@ using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; using IceCreamShopDataModels; using IceCreamShopDataModels.Enums; +using IceCreamShopDataModels.Models; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -18,11 +19,17 @@ namespace IceCreamShopBusinessLogic.BusinessLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopStorage _shopStorage; + private readonly IShopLogic _shopLogic; + private readonly IIceCreamStorage _iceCreamStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(IOrderStorage orderStorage, IShopStorage shopStorage, IShopLogic shopLogic, IIceCreamStorage iceCreamStorage, ILogger logger) { - _logger = logger; _orderStorage = orderStorage; + _shopStorage = shopStorage; + _logger = logger; + _shopLogic = shopLogic; + _iceCreamStorage = iceCreamStorage; } public List? ReadList(OrderSearchModel? model) @@ -52,6 +59,69 @@ namespace IceCreamShopBusinessLogic.BusinessLogic return true; } + public bool CheckSupply(IIceCreamModel iceCream, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. IceCream count < 0."); + return false; + } + + int freeSpace = 0; + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace += shop.MaxCapacity; + foreach (var doc in shop.ShopIceCreams) + { + freeSpace -= doc.Value.Item2; + } + } + + if (freeSpace - count < 0) + { + _logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.MaxCapacity; + foreach (var doc in shop.ShopIceCreams) + { + freeSpace -= doc.Value.Item2; + } + if (freeSpace == 0) + { + continue; + } + if (freeSpace - count >= 0) + { + if (_shopLogic.MakeSupply(new() { Id = shop.Id }, iceCream, count)) + count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (freeSpace - count < 0) + { + if (_shopLogic.MakeSupply(new() { Id = shop.Id }, iceCream, freeSpace)) + count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } + } + return false; + } + public bool ChangeStatus(OrderBindingModel model, OrderStatus status) { CheckModel(model); @@ -66,6 +136,20 @@ namespace IceCreamShopBusinessLogic.BusinessLogic _logger.LogWarning("Status change operation failed"); throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный"); } + if(status == OrderStatus.Готов) + { + var icecream = _iceCreamStorage.GetElement(new IceCreamSearchModel() { Id = model.IceCreamId }); + if (icecream == null) + { + _logger.LogWarning("Status update to " + status.ToString() + " operation failed. Document not found."); + return false; + } + if (CheckSupply(icecream, model.Count) == false) + { + _logger.LogWarning("Status update to " + status.ToString() + " operation failed. Shop supply error."); + return false; + } + } model.Status = status; if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; _orderStorage.Update(model); diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ShopLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ShopLogic.cs index e5e9410..ae4c719 100644 --- a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ShopLogic.cs +++ b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ShopLogic.cs @@ -52,25 +52,42 @@ _shopStorage.GetFilteredList(model); ShopViewModel curModel = _shopStorage.GetElement(model); if (curModel == null) throw new ArgumentNullException(nameof(curModel)); - if (curModel.ShopIceCreams.TryGetValue(iceCream.Id, out var pair)) + var countItems = curModel.ShopIceCreams.Select(x => x.Value.Item2).Sum(); + if (curModel.MaxCapacity - countItems >= count) { - curModel.ShopIceCreams[iceCream.Id] = (pair.Item1, pair.Item2 + count); + if (curModel.ShopIceCreams.TryGetValue(iceCream.Id, out var sameDocument)) + { + curModel.ShopIceCreams[iceCream.Id] = (iceCream, sameDocument.Item2 + count); + _logger.LogInformation("Same iceCream found by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, curModel.ShopName); + } + else + { + curModel.ShopIceCreams[iceCream.Id] = (iceCream, count); + _logger.LogInformation("New iceCream added by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, curModel.ShopName); + } + _shopStorage.Update(new() + { + Id = curModel.Id, + ShopName = curModel.ShopName, + Address = curModel.Address, + DateOpen = curModel.DateOpen, + ShopIceCreams = curModel.ShopIceCreams, + MaxCapacity = curModel.MaxCapacity + }); } else { - curModel.ShopIceCreams.Add(iceCream.Id, (iceCream, count)); + _logger.LogWarning("Required shop is overflowed"); + return false; } - Update(new() - { - Id = curModel.Id, - ShopName = curModel.ShopName, - DateOpen = curModel.DateOpen, - Address = curModel.Address, - ShopIceCreams = curModel.ShopIceCreams, - }); return true; } + public bool MakeSell(IIceCreamModel iceCream, int count) + { + return _shopStorage.SellIceCreams(iceCream, count); + } + public ShopViewModel ReadElement(ShopSearchModel model) { if (model == null) diff --git a/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs b/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs index 9b202bf..10e58da 100644 --- a/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs +++ b/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs @@ -13,6 +13,7 @@ namespace IceCreamShopContracts.BindingModels public string ShopName { get; set; } public string Address { get; set; } public DateTime DateOpen { get; set; } + public int MaxCapacity { get; set; } public Dictionary ShopIceCreams { get; set; } = new(); } } diff --git a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs index c9ee0e2..e5e7e32 100644 --- a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -18,5 +18,6 @@ namespace IceCreamShopContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool MakeSupply(ShopSearchModel model, IIceCreamModel iceCream, int count); + bool MakeSell(IIceCreamModel iceCream, int count); } } diff --git a/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs index 1a7eb3c..d0ae61e 100644 --- a/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs +++ b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs @@ -18,5 +18,6 @@ namespace IceCreamShopContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + public bool SellIceCreams(IIceCreamModel model, int count); } } diff --git a/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs b/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs index 0b93fb1..740053a 100644 --- a/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs +++ b/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs @@ -17,6 +17,8 @@ namespace IceCreamShopContracts.ViewModels public string Address { get; set; } [DisplayName("Дата открытия")] public DateTime DateOpen { get; set; } + [DisplayName("Вместимость")] + public int MaxCapacity { get; set; } public Dictionary ShopIceCreams { get; set; } = new(); } } diff --git a/IceCreamShop/IceCreamShopDataModels/IShopModel.cs b/IceCreamShop/IceCreamShopDataModels/IShopModel.cs index b852783..352bd82 100644 --- a/IceCreamShop/IceCreamShopDataModels/IShopModel.cs +++ b/IceCreamShop/IceCreamShopDataModels/IShopModel.cs @@ -12,5 +12,6 @@ namespace IceCreamShopDataModels.Models string Address { get; } DateTime DateOpen { get; } Dictionary ShopIceCreams { get; } + int MaxCapacity { get; } } } diff --git a/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs index 0bd54c8..fdcc3d8 100644 --- a/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs +++ b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs @@ -14,9 +14,11 @@ namespace IceCreamShopFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string IceCreamFileName = "IceCream.xml"; + private readonly string ShopFileName = "Shops.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List IceCreams { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { @@ -33,6 +35,8 @@ namespace IceCreamShopFileImplement "IceCreams", 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 => @@ -41,6 +45,8 @@ namespace IceCreamShopFileImplement IceCream.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/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs index 234bd35..432809c 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs @@ -2,6 +2,7 @@ using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; using IceCreamShopFileImplement.Models; using System; using System.Collections.Generic; @@ -91,5 +92,6 @@ model) return null; } + } } diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs index 983f9d1..8df2a2b 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs @@ -90,14 +90,10 @@ model) { if (model == null) return null; - foreach (var iceCream in source.IceCreams) - { - if (iceCream.Id == model.IceCreamId) - { - model.IceCreamName = iceCream.IceCreamName; - break; - } - } + var iceCream = source.IceCreams.FirstOrDefault(x => (x.Id == model.IceCreamId)); + if (iceCream == null) + return model; + model.IceCreamName = iceCream.IceCreamName; return model; } diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..70c2277 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,140 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; +using IceCreamShopFileImplement; +using IceCreamShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopFileImplement.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.Name)) + { + return new(); + } + return _source.Shops + .Where(x => x.ShopName.Contains(model.Name)) +.Select(x => x.GetViewModel) +.ToList(); ; + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + return _source.Shops + .FirstOrDefault(x => +(!string.IsNullOrEmpty(model.Name) && x.ShopName == +model.Name) || + (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 component = _source.Shops.FirstOrDefault(x => x.Id == +model.Id); + if (component == null) + { + return null; + } + component.Update(model); + _source.SaveShops(); + return component.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 CheckAvailability(int iceCreamId, int count) + { + int minus = _source.Shops.Select(x => x.ShopIceCreams.Select(y => (y.Value.Item1.Id == iceCreamId ? y.Value.Item2 : 0)).Sum()).Sum(); + count -= minus; + return count <= 0; + } + + public bool SellIceCreams(IIceCreamModel model, int count) + { + var iceCream = _source.IceCreams.FirstOrDefault(x => x.Id == model.Id); + + if (iceCream == null || !CheckAvailability(iceCream.Id, count)) + { + return false; + } + + for (int i = 0; i < _source.Shops.Count; i++) + { + var shop = _source.Shops[i]; + var icecreams = shop.ShopIceCreams; + + foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == iceCream.Id)) + { + var min = Math.Min(icecream.Value.Item2, count); + icecreams[icecream.Value.Item1.Id] = (icecream.Value.Item1, icecream.Value.Item2 - min); + count -= min; + + if (count <= 0) + { + break; + } + } + + shop.Update(new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpen = shop.DateOpen, + MaxCapacity = shop.MaxCapacity, + ShopIceCreams = icecreams + }); + } + _source.SaveShops(); + return true; + } + + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs new file mode 100644 index 0000000..10d6f24 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs @@ -0,0 +1,118 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; +using IceCreamShopFileImplement; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Threading.Tasks.Dataflow; +using System.Xml.Linq; + +namespace IceCreamShopFileImplement.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 IceCreams { get; private set; } = new(); + private Dictionary? _shopIceCreams = +null; + public Dictionary ShopIceCreams + { + get + { + if (_shopIceCreams == null) + { + var source = DataFileSingleton.GetInstance(); + _shopIceCreams = IceCreams.ToDictionary(x => x.Key, y => + ((source.IceCreams.FirstOrDefault(z => z.Id == y.Key) as IIceCreamModel)!, + y.Value)); + } + return _shopIceCreams; + } + } + + public int MaxCapacity { 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, + DateOpen = model.DateOpen, + MaxCapacity = model.MaxCapacity, + IceCreams = model.ShopIceCreams.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, + Address = element.Element("Address")!.Value, + MaxCapacity = Convert.ToInt32(element.Element("MaxCapacity")!.Value), + DateOpen = Convert.ToDateTime(element.Element("DateOpen")!.Value), + IceCreams = + element.Element("ShopIceCreams")!.Elements("ShopIceCream") + .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; + DateOpen = model.DateOpen; + MaxCapacity = model.MaxCapacity; + if (model.ShopIceCreams.Count > 0) + { + IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopIceCreams = null; + } + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + MaxCapacity = MaxCapacity, + ShopIceCreams = ShopIceCreams + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("DateOpen", DateOpen), + new XElement("MaxCapacity", MaxCapacity), + new XElement("ShopIceCreams", IceCreams + .Select(x => new XElement("ShopIceCream", + new XElement("Key", x.Key), + new XElement("Value", x.Value)) + ).ToArray())); + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs index 0d6e6ea..06b9cc4 100644 --- a/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs +++ b/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs @@ -106,5 +106,64 @@ namespace IceCreamShopListImplement.Implements return null; } + public bool CheckAvailability(int iceCreamId, int count) + { + foreach (var shop in _source.Shops) + { + foreach (var icecream in shop.ShopIceCreams) + { + if (icecream.Value.Item1.Id == iceCreamId) + { + count -= icecream.Value.Item2; + } + if (count <= 0) + { + break; + } + } + } + + return count <= 0; + } + + public bool SellIceCreams(IIceCreamModel model, int count) + { + var iceCream = _source.IceCreams.FirstOrDefault(x => x.Id == model.Id); + + if (iceCream == null || !CheckAvailability(iceCream.Id, count)) + { + return false; + } + + for (int i = 0; i < _source.Shops.Count; i++) + { + var shop = _source.Shops[i]; + var icecreams = shop.ShopIceCreams; + + foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == iceCream.Id)) + { + var min = Math.Min(icecream.Value.Item2, count); + icecreams[icecream.Value.Item1.Id] = (icecream.Value.Item1, icecream.Value.Item2 - min); + count -= min; + + if (count <= 0) + { + break; + } + } + + shop.Update(new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpen = shop.DateOpen, + MaxCapacity = shop.MaxCapacity, + ShopIceCreams = icecreams + }); + } + return true; + } + } } diff --git a/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs index 239004a..fd5f34c 100644 --- a/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs +++ b/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs @@ -16,6 +16,7 @@ namespace IceCreamShopListImplement.Models public string ShopName { get; private set; } public string Address { get; private set; } public DateTime DateOpen { get; private set; } + public int MaxCapacity { get; private set; } public Dictionary ShopIceCreams { get; private set; } = new(); public static Shop? Create (ShopBindingModel model)