public IExtendedMaterialModel CreateBatch(decimal batchAmount, IMaterialUnit preferredBatchUnit, IUnitConversionHelper conversions) { // Nominal = 1kg // Batch = 500g var batchUnit = conversions.GetPrefferedUnit(preferredBatchUnit, NominalUnit); //g var convertedNominalAmount = conversions.ConvertAmount(NominalUnit.Id, batchUnit.Id, NominalAmount); // 1000g var convertedBatchAmount = conversions.ConvertAmount(preferredBatchUnit.Id, batchUnit.Id, batchAmount); //500g var conversionFactor = convertedBatchAmount / convertedNominalAmount; // 0.5 var batch = new ExtendedMaterial(Adaptee) { BatchUnit = batchUnit, BatchAmount = convertedBatchAmount }; var batchComponents = new List <MaterialComponent>(m_components.Count); foreach (var sourceComponent in m_components) { var componentBatchAmount = sourceComponent.Amount * conversionFactor; var batchedComponentMaterial = sourceComponent.Material.CreateBatch( componentBatchAmount, sourceComponent.Unit, conversions); var batchComponent = new MaterialComponent(sourceComponent.Unit, batchedComponentMaterial, componentBatchAmount, null); batchComponents.Add(batchComponent); } return(batch); }
//public Amount ConvertToSuitableUnit(Amount source) //{ // if (source.Unit == null) // { // return source; // } // var availableUnits = m_conversionHelper.GetCompatibleUnits(source.Unit.Id).ToList(); // if (!availableUnits.Any()) // { // return source; // } // var bestUnit = source.Unit; // var readability = StringUtil.GetReadability(source.Value); // foreach (var availableUnit in availableUnits) // { // //var converted = m_conversionHelper.ConvertAmount(source.Unit.Id, availableUnit.Id, ) // } // throw new NotImplementedException(); //} private Amount Calculate(Amount a, Amount b, Func<decimal, decimal, decimal> numericOp) { if ((a == null) && (b == null)) { return null; } if (a == null) { a = new Amount(0, b.Unit); } if (b == null) { b = new Amount(0, a.Unit); } if (a.Unit.Id == b.Unit.Id) { return new Amount(numericOp(a.Value, b.Value), a.Unit); } var targetUnit = m_conversionHelper.GetPrefferedUnit(a.Unit, b.Unit); var convertedA = m_conversionHelper.ConvertAmount(a.Unit.Id, targetUnit.Id, a.Value); var convertedB = m_conversionHelper.ConvertAmount(b.Unit.Id, targetUnit.Id, b.Value); var result = numericOp(convertedA, convertedB); return new Amount(result, targetUnit); }
public MaterialLevelModel GetMaterialLevel(int materialId) { PreloadBatchAmountCache(); return(m_cache.ReadThrough(GetMaterialLevelCacheKey(materialId), TimeSpan.FromDays(1), () => { var material = m_materialRepository.GetMaterialById(materialId); if (material == null) { return null; } var amount = new Amount(0, material.NominalUnit); var batches = m_batchRepository.GetBatchIds(DateTime.Now.AddYears(-10), DateTime.Now.AddYears(1), materialId: material.Id); foreach (var batchId in batches) { amount = m_amountProcessor.Add(amount, GetAvailableAmount(batchId)); } var threshold = m_materialThresholdRepository.GetThreshold(materialId) ?? CreateFakeThreshold(material, amount); var amountUnitThreshold = m_conversionHelper.ConvertAmount( new Amount(threshold.ThresholdQuantity, m_unitRepository.GetUnit(threshold.UnitId)), amount.Unit.Id); return new MaterialLevelModel { MaterialId = material.Id, MaterialName = material.Name, ActualValue = amount.Value, MaxValue = amountUnitThreshold.Value * 10m, MinValue = amountUnitThreshold.Value, PercentLevel = GetPercentLevel(amountUnitThreshold.Value * 10m, amount.Value), UnitId = amount.Unit.Id, Unit = amount.Unit.Symbol, HasThreshold = threshold.Id > 0 }; })); }
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"); } }