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; } } }
private void AddMissingComponents(ProductionComponent requestComponent, AllocationRequestResult resolutions, Amount requiredAmount, bool isSegmentUpdateFirstRound) { var clientComponentsCount = requestComponent.Resolutions.Count; foreach (var resolution in resolutions.Allocations) { var clientAllo = requestComponent.Resolutions.FirstOrDefault(a => a.BatchNumber.Equals(resolution.BatchNumber, StringComparison.InvariantCultureIgnoreCase)); if (clientAllo == null) { var convertedAllocatedAmount = m_amountProcessor.Convert(resolution.Allocated, requiredAmount.Unit); var convertedAvailableAmount = m_amountProcessor.Convert(resolution.TotalBatchNumberAvailable, requiredAmount.Unit); requestComponent.Resolutions.Add(new ProductionComponentResolution { Amount = isSegmentUpdateFirstRound ? 0m : convertedAllocatedAmount.Value, BatchAvailableAmount = convertedAvailableAmount.Value, BatchAvailableAmountText = convertedAvailableAmount.ToString(), UnitSymbol = convertedAllocatedAmount.Unit.Symbol, BatchCreationDt = StringUtil.FormatDate(resolution.BatchCreated), BatchNumber = resolution.BatchNumber, Sorter = resolution.BatchCreated.Ticks, Key = Guid.NewGuid().ToString() }); } } if ((clientComponentsCount > 0) && (requestComponent.Resolutions.Count > clientComponentsCount)) { requestComponent.Resolutions.Sort((a, b) => a.Sorter.CompareTo(b.Sorter)); } }
public AllocationRequestResult ResolveMaterialDemand(int materialId, Amount demand, string batchNumberOrNull, bool batchNumberIsPreferrence, bool includeBatchesWithoutAllocation, DateTime?batchesProducedBefore = null, int?ignoreExistenceOfBatchId = null) { var batchIdNumberAmount = new List <Tuple <int, string, Amount> >(); var batchCreated = new Dictionary <string, DateTime>(); m_database.Sql().Call("GetMaterialResolution") .WithParam("@projectId", m_session.Project.Id) .WithParam("@materialId", materialId) .WithParam("@createdBefore", batchesProducedBefore) .WithParam("@ignoreExistenceOfThisBatch", ignoreExistenceOfBatchId) .ReadRows <int, string, decimal, int, DateTime>( (batchId, batchNumber, available, unitId, created) => { if (!string.IsNullOrWhiteSpace(batchNumberOrNull) && (!batchNumberIsPreferrence) && !batchNumberOrNull.Equals(batchNumber, StringComparison.InvariantCultureIgnoreCase)) { return; } if (!batchCreated.TryGetValue(batchNumber.ToLowerInvariant(), out var dateA)) { batchCreated[batchNumber.ToLowerInvariant()] = created; } else { batchCreated[batchNumber.ToLowerInvariant()] = dateA > created ? dateA : created; } batchIdNumberAmount.Add(new Tuple <int, string, Amount>(batchId, batchNumber, new Amount(available, m_unitRepository.GetUnit(unitId)))); }); if (!string.IsNullOrWhiteSpace(batchNumberOrNull) && batchNumberIsPreferrence) { batchIdNumberAmount = batchIdNumberAmount.OrderByDescending(r => r.Item2.Equals(batchNumberOrNull, StringComparison.InvariantCultureIgnoreCase) ? 1 : 0).ToList(); } var batchIdNumberTotalAllocated = new List <Tuple <int, string, Amount, Amount> >(batchIdNumberAmount.Count); foreach (var src in batchIdNumberAmount) { var canAllocateToThisBatch = m_amountProcessor.Min(demand, src.Item3); batchIdNumberTotalAllocated.Add(new Tuple <int, string, Amount, Amount>(src.Item1, src.Item2, src.Item3, canAllocateToThisBatch)); demand = m_amountProcessor.Subtract(demand, canAllocateToThisBatch); if ((!includeBatchesWithoutAllocation) && (!demand.IsPositive)) { break; } } var result = new AllocationRequestResult(materialId, demand.IsNotPositive, m_amountProcessor.Sum(batchIdNumberTotalAllocated.Select(a => a.Item4)), demand); foreach (var batchNumber in batchIdNumberTotalAllocated.Select(b => b.Item2).Distinct()) { var thisBatchNrAllocations = batchIdNumberTotalAllocated .Where(a => a.Item2.Equals(batchNumber, StringComparison.InvariantCultureIgnoreCase)).ToList(); batchCreated.TryGetValue(batchNumber.ToLowerInvariant(), out var dt); result.Allocations.Add(new BatchAllocation( batchNumber, m_amountProcessor.Sum(thisBatchNrAllocations.Select(a => a.Item3)), m_amountProcessor.Sum(thisBatchNrAllocations.Select(a => a.Item4)), thisBatchNrAllocations.Select(a => new Tuple <int, Amount>(a.Item1, a.Item4)).ToList() , dt)); } return(result); }