private void RemoveComponentsNotProposedBySystem(ProductionComponent requestComponent, AllocationRequestResult resolutions, Amount requiredAmount) { for (var i = requestComponent.Resolutions.Count - 1; i >= 0; i--) { var clientResolution = requestComponent.Resolutions[i]; var dbAllocation = resolutions.Allocations.FirstOrDefault(a => a.BatchNumber.Equals(clientResolution.BatchNumber, StringComparison.InvariantCultureIgnoreCase)); if (dbAllocation == null) { // we received allocation which was not proposed by the system requestComponent.Resolutions.RemoveAt(i); continue; } if ((clientResolution.GetAmount(m_unitRepository) == null) || m_amountProcessor.GreaterThan( clientResolution.GetAmount(m_unitRepository), dbAllocation.TotalBatchNumberAvailable)) { // this is an invalid allocation received from client var convertedMaxAvailable = m_amountProcessor.Convert(dbAllocation.TotalBatchNumberAvailable, requiredAmount.Unit); clientResolution.Amount = convertedMaxAvailable.Value; clientResolution.UnitSymbol = convertedMaxAvailable.Unit.Symbol; } } }
public void Process(ProductionRequestContext context) { var material = m_materialFacade.GetMaterialInfo(context.Recipe.ProducedMaterialId); context.Request.MaterialName = material.MaterialName; // Batch number if (string.IsNullOrWhiteSpace(context.Request.ProducingBatchNumber)) { if (material.AutomaticBatches) { context.Request.ProducingBatchNumber = material.AutoBatchNr; } else { context.InvalidateRequest("Nutno vyplnit číslo šarže"); } } // Unit while (true) { if (string.IsNullOrWhiteSpace(context.Request.ProducingUnitSymbol)) { context.Request.ProducingUnitSymbol = material.PreferredUnitSymbol; break; } if (!context.Request.ProducingUnitSymbol.Equals(material.PreferredUnitSymbol, StringComparison.InvariantCultureIgnoreCase)) { var usedUnit = m_unitRepository.GetUnitBySymbol(context.Request.ProducingUnitSymbol.Trim()); if (usedUnit == null) { context.Request.ProducingUnitSymbol = null; continue; } var mainUnit = m_unitRepository.GetUnitBySymbol(material.PreferredUnitSymbol); if (!m_conversionHelper.AreCompatible(usedUnit.Id, mainUnit.Id)) { context.Request.ProducingUnitSymbol = null; continue; } } break; } context.RequestedAmount = new Amount(context.Request.ProducingAmount ?? 0m, m_unitRepository.GetUnitBySymbol(context.Request.ProducingUnitSymbol)); context.NominalRecipeAmount = new Amount(context.Recipe.RecipeProducedAmount, m_unitRepository.GetUnit(context.Recipe.ProducedAmountUnitId)); var commonUnit = m_conversionHelper.GetSmallestCompatibleUnit(context.RequestedAmount.Unit); var convertedRequestedAmount = m_conversionHelper.ConvertAmount(context.RequestedAmount, commonUnit.Id); var convertedNominalAmount = m_conversionHelper.ConvertAmount(context.NominalRecipeAmount, commonUnit.Id); context.ComponentMultiplier = convertedRequestedAmount.Value / convertedNominalAmount.Value; if (context.MinimalAmount != null && m_amountProcessor.GreaterThan(context.MinimalAmount, context.RequestedAmount)) { context.InvalidateRequest($"Nové množství nesmí být méně než {context.MinimalAmount}, protože tolik již bylo spotřebováno"); } }
public IEnumerable <MaterialLevelEntryModel> Load(int inventoryId) { var result = new List <MaterialLevelEntryModel>(); m_database.Sql().Call("GetMaterialLevelsReport").WithParam("@inventoryId", inventoryId).WithParam("@projectId", m_session.Project.Id) .ReadRows <int, string, string, int, decimal, string, string, string>((materialId, materialName, batchNumber, unitId, available, supName, supMail, supPhone) => { var entry = result.FirstOrDefault(r => r.MaterialId == materialId); if (entry == null) { entry = new MaterialLevelEntryModel(); result.Add(entry); entry.MaterialId = materialId; entry.MaterialName = materialName; entry.SupplierName = supName; entry.SupplierEmail = supMail; entry.SupplierPhone = supPhone; } if (available == 0) { return; } var batchModel = entry.Batches.FirstOrDefault(b => b.BatchNumber.Equals(batchNumber, StringComparison.InvariantCultureIgnoreCase) && b.UnitId == unitId); if (!string.IsNullOrWhiteSpace(batchNumber)) { if (batchModel == null) { batchModel = new BatchAmountModel { BatchNumber = batchNumber, UnitId = unitId }; entry.Batches.Add(batchModel); } batchModel.Value += available; } }); foreach (var r in result) { var threshold = m_thresholdRepository.GetThreshold(r.MaterialId); if (threshold != null) { r.Threshold = new Amount(threshold.ThresholdQuantity, m_unitRepository.GetUnit(threshold.UnitId)); } foreach (var batch in r.Batches) { batch.Amount = new Amount(batch.Value, m_unitRepository.GetUnit(batch.UnitId)); } r.Total = m_amountProcessor.Sum(r.Batches.Select(b => b.Amount)); if (string.IsNullOrWhiteSpace(r.UnitSymbol)) { var material = m_materialRepository.GetMaterialById(r.MaterialId); r.DefaultUnitSymbol = material.NominalUnit.Symbol; } r.HasWarning = r.Threshold != null && m_amountProcessor.GreaterThan(r.Threshold, r.Total ?? r.Threshold.ToZeroAmount()); } return(result.OrderBy(r => r.HasWarning ? 0 : 1).ThenBy(r => r.MaterialName)); }
private void SaveAllocation(int eventId, List <ISaleEventAllocation> existing, SaleEventAllocationDto dto) { var existingAllocations = existing .Where(a => a.Batch.MaterialId == dto.MaterialId && a.Batch.BatchNumber == dto.BatchNumber).ToList(); if (existing.Any() && !existingAllocations.Any()) { throw new InvalidOperationException("Pri zmene existujici prodejni akce nesmi byt zmeneny polozky, jen vracene mnozstvi"); } if (dto.ReturnedQuantity != null && m_amountProcessor.GreaterThan(dto.ReturnedQuantity, dto.AllocatedQuantity)) { throw new InvalidOperationException("Nelze vracet vetsi nez blokovane mnozstvi"); } if (!existingAllocations.Any()) { var batches = m_batchFacade.ProposeAllocations(dto.MaterialId, dto.BatchNumber, dto.AllocatedQuantity) .ToList(); if (batches.Any(b => b.Item1 == null)) { throw new InvalidOperationException($"Pozadovane mnozstvi {dto.AllocatedQuantity} {m_materialRepository.GetMaterialById(dto.MaterialId)?.Name} neni v sarzi {dto.BatchNumber} k dispozici"); } foreach (var batch in batches) { m_database.Save(m_database.New <ISaleEventAllocation>(a => { a.AllocationDt = DateTime.Now; a.AllocatedQuantity = batch.Item2.Value; a.UnitId = batch.Item2.Unit.Id; a.BatchId = batch.Item1.Value; a.AllocationUserId = m_session.User.Id; a.SaleEventId = eventId; if (dto.ReturnedQuantity != null) { a.ReturnedQuantity = dto.ReturnedQuantity.Value; a.ReturnDt = DateTime.Now; a.ReturnUserId = m_session.User.Id; } })); } return; } var totalAllocatedInDb = m_amountProcessor.Sum(existingAllocations.Select(a => new Amount(a.AllocatedQuantity, a.Unit))); if (!m_amountProcessor.AreEqual(totalAllocatedInDb, dto.AllocatedQuantity)) { throw new InvalidOperationException("Nelze zmenit alokovane mnozstvi pro existujici akci"); } var totalReturnedInDb = m_amountProcessor.Sum(existingAllocations.Where(a => a.ReturnedQuantity != null) .Select(a => new Amount(a.ReturnedQuantity.Value, a.Unit))); var returnedAmount = dto.ReturnedQuantity == null ? new Amount(0, dto.AllocatedQuantity.Unit) : dto.ReturnedQuantity; if (m_amountProcessor.AreEqual(totalReturnedInDb, returnedAmount)) { return; } if (m_amountProcessor.GreaterThan(totalReturnedInDb, returnedAmount)) { throw new InvalidOperationException("Nelze snizovat vracene mnozstvi"); } var newReturn = m_amountProcessor.Subtract(returnedAmount, totalReturnedInDb); foreach (var existingAloc in existingAllocations) { if (!newReturn.IsPositive) { break; } var alreadyReturnedHere = existingAloc.ReturnedQuantity == null ? new Amount(0, existingAloc.Unit) : new Amount(existingAloc.ReturnedQuantity.Value, existingAloc.Unit); var toReturnHere = m_amountProcessor.Subtract(new Amount(existingAloc.AllocatedQuantity, existingAloc.Unit), alreadyReturnedHere); if (!toReturnHere.IsPositive) { continue; } var actuallyReturningHere = m_amountProcessor.Min(toReturnHere, newReturn); existingAloc.ReturnedQuantity = actuallyReturningHere.Value; existingAloc.UnitId = actuallyReturningHere.Unit.Id; m_database.Save(existingAloc); newReturn = m_amountProcessor.Subtract(newReturn, actuallyReturningHere); } if (newReturn.IsPositive) { throw new InvalidOperationException("Nebylo mozne vratit vsechno pozadovane mnozstvi"); } }