diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index bf255f6..045428b 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -213,5 +213,10 @@ namespace ConfectioneryBusinessLogic } return true; } + + public bool SellPastries(IPastryModel pastry, int needCount) + { + return _shopStorage.SellPastries(pastry, needCount); + } } } diff --git a/ConfectionaryFileImplement/ShopStorage.cs b/ConfectionaryFileImplement/ShopStorage.cs index 3e9d4da..e155218 100644 --- a/ConfectionaryFileImplement/ShopStorage.cs +++ b/ConfectionaryFileImplement/ShopStorage.cs @@ -2,6 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels; using ConfectioneryDataModels.Models; namespace ConfectioneryFileImplement @@ -94,11 +95,12 @@ namespace ConfectioneryFileImplement { return false; } - foreach (var pair in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) + foreach (var shop in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) { - var tuple = pair.Pastries[pastry.Id]; + var tuple = shop.Pastries[pastry.Id]; var diff = Math.Min(tuple.Item2, needCount); - pair.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + shop.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + needCount -= diff; if (needCount <= 0) { diff --git a/Confectionery/FormMain.Designer.cs b/Confectionery/FormMain.Designer.cs index 0710300..35f58fe 100644 --- a/Confectionery/FormMain.Designer.cs +++ b/Confectionery/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.buttonAddPastryInShop = new System.Windows.Forms.Button(); + this.buttonSellPastry = new System.Windows.Forms.Button(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -94,7 +95,7 @@ this.dataGridView.Location = new System.Drawing.Point(12, 27); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowTemplate.Height = 25; - this.dataGridView.Size = new System.Drawing.Size(606, 341); + this.dataGridView.Size = new System.Drawing.Size(606, 399); this.dataGridView.TabIndex = 1; // // buttonCreateOrder @@ -155,7 +156,7 @@ // buttonAddPastryInShop // this.buttonAddPastryInShop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonAddPastryInShop.Location = new System.Drawing.Point(624, 326); + this.buttonAddPastryInShop.Location = new System.Drawing.Point(624, 384); this.buttonAddPastryInShop.Name = "buttonAddPastryInShop"; this.buttonAddPastryInShop.Size = new System.Drawing.Size(147, 31); this.buttonAddPastryInShop.TabIndex = 7; @@ -163,11 +164,23 @@ this.buttonAddPastryInShop.UseVisualStyleBackColor = true; this.buttonAddPastryInShop.Click += new System.EventHandler(this.ButtonAddPastryInShop_Click); // + // buttonSellPastry + // + this.buttonSellPastry.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSellPastry.Location = new System.Drawing.Point(624, 331); + this.buttonSellPastry.Name = "buttonSellPastry"; + this.buttonSellPastry.Size = new System.Drawing.Size(147, 31); + this.buttonSellPastry.TabIndex = 8; + this.buttonSellPastry.Text = "Продать изделие"; + this.buttonSellPastry.UseVisualStyleBackColor = true; + this.buttonSellPastry.Click += new System.EventHandler(this.ButtonSellPastry_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(783, 380); + this.ClientSize = new System.Drawing.Size(783, 438); + this.Controls.Add(this.buttonSellPastry); this.Controls.Add(this.buttonAddPastryInShop); this.Controls.Add(this.button4); this.Controls.Add(this.button3); @@ -202,5 +215,6 @@ private ToolStripMenuItem componentToolStripMenuItem; private ToolStripMenuItem ShopsToolStripMenuItem; private Button buttonAddPastryInShop; + private Button buttonSellPastry; } } \ No newline at end of file diff --git a/Confectionery/FormMain.cs b/Confectionery/FormMain.cs index 3c6c955..6dcaf07 100644 --- a/Confectionery/FormMain.cs +++ b/Confectionery/FormMain.cs @@ -170,5 +170,14 @@ namespace ConfectioneryView form.ShowDialog(); } } + + private void ButtonSellPastry_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellPastry)); + if (service is FormSellPastry form) + { + form.ShowDialog(); + } + } } } \ No newline at end of file diff --git a/Confectionery/FormSellPastry.Designer.cs b/Confectionery/FormSellPastry.Designer.cs new file mode 100644 index 0000000..46f5949 --- /dev/null +++ b/Confectionery/FormSellPastry.Designer.cs @@ -0,0 +1,133 @@ +namespace ConfectioneryView +{ + partial class FormSellPastry + { + /// + /// 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() + { + this.comboBoxPastry = new System.Windows.Forms.ComboBox(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.numericUpDownCount = new System.Windows.Forms.NumericUpDown(); + this.buttonSell = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).BeginInit(); + this.SuspendLayout(); + // + // comboBoxPastry + // + this.comboBoxPastry.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.comboBoxPastry.FormattingEnabled = true; + this.comboBoxPastry.Location = new System.Drawing.Point(141, 12); + this.comboBoxPastry.Name = "comboBoxPastry"; + this.comboBoxPastry.Size = new System.Drawing.Size(121, 23); + this.comboBoxPastry.TabIndex = 0; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 15); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(123, 15); + this.label1.TabIndex = 1; + this.label1.Text = "Изделие на продажу:"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(60, 46); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(75, 15); + this.label2.TabIndex = 2; + this.label2.Text = "Количество:"; + // + // numericUpDownCount + // + this.numericUpDownCount.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.numericUpDownCount.Location = new System.Drawing.Point(142, 46); + this.numericUpDownCount.Maximum = new decimal(new int[] { + 10000000, + 0, + 0, + 0}); + this.numericUpDownCount.Name = "numericUpDownCount"; + this.numericUpDownCount.RightToLeft = System.Windows.Forms.RightToLeft.No; + this.numericUpDownCount.Size = new System.Drawing.Size(120, 23); + this.numericUpDownCount.TabIndex = 3; + // + // buttonSell + // + this.buttonSell.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSell.Location = new System.Drawing.Point(12, 90); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(123, 23); + this.buttonSell.TabIndex = 4; + this.buttonSell.Text = "Продать"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.ButtonSell_Click); + // + // buttonCancel + // + this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonCancel.Location = new System.Drawing.Point(142, 90); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(123, 23); + this.buttonCancel.TabIndex = 5; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormSellPastry + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(277, 122); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.numericUpDownCount); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Controls.Add(this.comboBoxPastry); + this.Name = "FormSellPastry"; + this.Text = "Продажа изделия"; + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private ComboBox comboBoxPastry; + private Label label1; + private Label label2; + private NumericUpDown numericUpDownCount; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/Confectionery/FormSellPastry.cs b/Confectionery/FormSellPastry.cs new file mode 100644 index 0000000..17731cd --- /dev/null +++ b/Confectionery/FormSellPastry.cs @@ -0,0 +1,67 @@ +using ConfectioneryContracts.BusinessLogicsContracts; +using System; +using System.Collections; +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 ConfectioneryView +{ + public partial class FormSellPastry : Form + { + private readonly IShopLogic _shopLogic; + private readonly IPastryLogic _pastryLogic; + + public FormSellPastry(IPastryLogic logic, IShopLogic shopLogic) + { + InitializeComponent(); + _pastryLogic = logic; + _shopLogic = shopLogic; + var list = logic.ReadList(null); + if (list != null) + { + comboBoxPastry.DisplayMember = "PastryName"; + comboBoxPastry.ValueMember = "Id"; + comboBoxPastry.DataSource = list; + comboBoxPastry.SelectedItem = null; + } + } + + private void ButtonSell_Click(object sender, EventArgs e) + { + if (comboBoxPastry.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (numericUpDownCount.Value <= 0) + { + MessageBox.Show("Количество должно быть больше нуля", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + var count = (int)numericUpDownCount.Value; + var pastry = _pastryLogic.ReadElement(new() { Id = (int)comboBoxPastry.SelectedValue }); + if (pastry == null || !_shopLogic.SellPastries(pastry, count)) + { + MessageBox.Show("Не удалось продать изделия. Информацию смотрите в логах", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + DialogResult = DialogResult.OK; + Close(); + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/Confectionery/FormSellPastry.resx b/Confectionery/FormSellPastry.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Confectionery/FormSellPastry.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/Confectionery/Program.cs b/Confectionery/Program.cs index 6f0ea6d..e76fcdb 100644 --- a/Confectionery/Program.cs +++ b/Confectionery/Program.cs @@ -53,6 +53,7 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs index cd2e634..6390a91 100644 --- a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -15,5 +15,6 @@ namespace ConfectioneryContracts.BusinessLogicsContracts bool AddPastry(ShopSearchModel model, IPastryModel pastry, int count); int GetFreePlacesWithPastriesInShops(int countPastries); bool AddPastriesInShops(IPastryModel pastry, int count); + public bool SellPastries(IPastryModel pastry, int needCount); } }