diff --git a/LawFirm/LawFirm/FormShopSell.cs b/LawFirm/LawFirm/FormShopSell.cs index 5255aa2..b7e9b89 100644 --- a/LawFirm/LawFirm/FormShopSell.cs +++ b/LawFirm/LawFirm/FormShopSell.cs @@ -66,10 +66,9 @@ namespace LawFirmView try { var operationResult = _logicS.SellDocument( - new DocumentSearchModel + new DocumentBindingModel { - Id = Convert.ToInt32(comboBoxDocument.SelectedValue), - DocumentName = comboBoxDocument.Text + Id = Convert.ToInt32(comboBoxDocument.SelectedValue) }, Convert.ToInt32(textBoxCount.Text) ); diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs index 75cacbd..bc5dd2b 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs @@ -17,11 +17,16 @@ namespace LawFirmBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopLogic _shopLogic; + private readonly IDocumentStorage _documentStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage) { _logger = logger; _orderStorage = orderStorage; + _shopLogic = shopLogic; + _documentStorage = documentStorage; } public bool CreateOrder(OrderBindingModel model) @@ -73,6 +78,22 @@ namespace LawFirmBusinessLogic.BusinessLogics _logger.LogWarning("Status update to " + newStatus.ToString() +" operation failed. Order status incorrect."); return false; } + + if (newStatus == OrderStatus.Готов) + { + var document = _documentStorage.GetElement(new DocumentSearchModel() { Id = model.DocumentId}); + if (document == null) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Document not found."); + return false; + } + if (_shopLogic.CheckThenSupplyMany(document, model.Count) == false) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error."); + return false; + } + } + model.Status = newStatus; if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; if (_orderStorage.Update(model) == null) diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/ShopLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/ShopLogic.cs index b3250a1..2e26e7d 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/ShopLogic.cs @@ -116,8 +116,68 @@ namespace LawFirmBusinessLogic.BusinessLogics } return true; } + public bool CheckThenSupplyMany(IDocumentModel document, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. Document count < 0."); + return false; + } - public bool SellDocument(DocumentSearchModel document, int count) + int freeSpace = 0; + foreach(var shop in _shopStorage.GetFullList()) + { + freeSpace += shop.MaxCountDocuments; + foreach (var doc in shop.ShopDocuments) + { + freeSpace -= doc.Value.Item2; + } + } + + if (freeSpace - count < 0) + { + _logger.LogWarning("Check then supply operation error. There's no place for new docs in shops."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.MaxCountDocuments; + foreach (var doc in shop.ShopDocuments) + { + freeSpace -= doc.Value.Item2; + } + if (freeSpace == 0) + { + continue; + } + if (freeSpace - count >= 0) + { + if (SupplyDocuments(new() { Id = shop.Id}, document, count)) count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (freeSpace - count < 0) + { + if (SupplyDocuments(new() { Id = shop.Id }, document, freeSpace)) count-= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } + } + return false; + } + + public bool SellDocument(IDocumentModel document, int count) {; return _shopStorage.SellDocument(document, count); } diff --git a/LawFirm/LawFirmContracts/BusinessLogicContracts/IShopLogic.cs b/LawFirm/LawFirmContracts/BusinessLogicContracts/IShopLogic.cs index ab76086..48c5760 100644 --- a/LawFirm/LawFirmContracts/BusinessLogicContracts/IShopLogic.cs +++ b/LawFirm/LawFirmContracts/BusinessLogicContracts/IShopLogic.cs @@ -18,7 +18,7 @@ namespace LawFirmContracts.BusinessLogicContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool SupplyDocuments(ShopSearchModel model, IDocumentModel document, int count); - - bool SellDocument(DocumentSearchModel document, int count); + bool SellDocument(IDocumentModel document, int count); + bool CheckThenSupplyMany(IDocumentModel document, int count); } } diff --git a/LawFirm/LawFirmContracts/StorageContracts/IShopStorage.cs b/LawFirm/LawFirmContracts/StorageContracts/IShopStorage.cs index b51b90c..6063bd2 100644 --- a/LawFirm/LawFirmContracts/StorageContracts/IShopStorage.cs +++ b/LawFirm/LawFirmContracts/StorageContracts/IShopStorage.cs @@ -1,6 +1,7 @@ using LawFirmContracts.BindingModels; using LawFirmContracts.SearchModels; using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -17,6 +18,6 @@ namespace LawFirmContracts.StorageContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); - bool SellDocument(DocumentSearchModel model, int count); + bool SellDocument(IDocumentModel model, int count); } } diff --git a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs index 4e76ac6..81c5de4 100644 --- a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using LawFirmContracts.SearchModels; using LawFirmContracts.StorageContracts; using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; using LawFirmFileImplement.Models; using System; using System.Collections.Generic; @@ -80,9 +81,9 @@ namespace LawFirmFileImplement.Implements return shop.GetViewModel; } - public bool SellDocument(DocumentSearchModel model, int count) + public bool SellDocument(IDocumentModel model, int count) { - var document = source.Documents.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id); + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); var countStore = count; diff --git a/LawFirm/LawFirmListImplements/Implements/ShopStorage.cs b/LawFirm/LawFirmListImplements/Implements/ShopStorage.cs index 15f5c4e..d8d0555 100644 --- a/LawFirm/LawFirmListImplements/Implements/ShopStorage.cs +++ b/LawFirm/LawFirmListImplements/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using LawFirmContracts.SearchModels; using LawFirmContracts.StorageContracts; using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; using LawFirmListImplements.Models; using System; using System.Collections.Generic; @@ -116,7 +117,7 @@ namespace LawFirmListImplements.Implements return null; } - public bool SellDocument(DocumentSearchModel model, int count) + public bool SellDocument(IDocumentModel model, int count) { throw new NotImplementedException(); }