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 void ApplyComponents(MaterialBatchComponent sourceSegment, ProductionRequestContext context) { var recipeComponents = m_recipeRepository.GetRecipe(sourceSegment.Batch.RecipeId.Ensure("Segment nevznikl z existující receptury, nelze změnit")).Components.OrderBy(c => c.SortOrder); var requestComponents = context.Request.Components; foreach (var recipeComponent in recipeComponents) { var compo = new ProductionComponent { MaterialId = recipeComponent.MaterialId, MaterialName = m_materialRepository.GetMaterialById(recipeComponent.MaterialId).Ensure().Name, SortOrder = recipeComponent.SortOrder }; requestComponents.Add(compo); var resolutions = sourceSegment.Components.Where(c => c.Batch.MaterialId == recipeComponent.MaterialId); var resIndex = new Dictionary <string, ProductionComponentResolution>(); var componentUnit = m_unitRepository.GetUnit(recipeComponent.UnitId); foreach (var r in resolutions) { var resolutionAmount = m_amountProcessor.Convert(new Amount(r.ComponentAmount, r.ComponentUnit), componentUnit); var batchAvailability = m_amountProcessor.Convert(m_batchFacade.GetAvailableAmount(r.Batch.Id), componentUnit); if (!resIndex.TryGetValue(r.Batch.BatchNumber, out var resolution)) { resolution = new ProductionComponentResolution { Amount = resolutionAmount.Value, BatchAvailableAmount = batchAvailability.Value, BatchAvailableAmountText = batchAvailability.ToString(), BatchCreationDt = StringUtil.FormatDate(r.Batch.Created), BatchNumber = r.Batch.BatchNumber, Key = Guid.NewGuid().ToString(), Sorter = r.Batch.Created.Ticks, UnitSymbol = componentUnit.Symbol }; compo.Resolutions.Add(resolution); resIndex.Add(r.Batch.BatchNumber, resolution); } else { resolution.Amount += resolutionAmount.Value; } } compo.LastClientAmount = m_amountProcessor .Sum(compo.Resolutions.Select(r => r.GetAmount(m_unitRepository)))?.ToString(); } }
public BatchReportModel QueryBatches(BatchReportQuery query) { if (query.LoadOrdersPage != null) { return(LoadOrders(query.ToKey(), query.LoadOrdersPage.Value)); } else if (query.LoadSaleEventsPage != null) { return(LoadSaleEvents(query.ToKey(), query.LoadSaleEventsPage.Value)); } else if (query.LoadSegmentsPage != null) { return(LoadSegments(query.ToKey(), query.LoadSegmentsPage.Value)); } else if (query.LoadPriceComponentsPage != null) { return(LoadPriceComponents(query.ToKey(), query.LoadPriceComponentsPage.Value)); } var pageSize = query.HasKey ? 1 : c_pageSize; var pageNumber = query.HasKey ? 0 : query.PageNumber; IPurchaseOrder order = null; if (query.RelativeToOrderId != null) { order = m_orderRepository.GetOrder(query.RelativeToOrderId.Value); if (order == null) { throw new InvalidOperationException("Invalid entity reference"); } pageSize = 1000; pageNumber = 0; } var sql = m_database.Sql().Call("LoadBatchesReport") .WithParam("@projectId", m_session.Project.Id) .WithParam("@pageSize", pageSize) .WithParam("@pageNumber", pageNumber) .WithParam("@batchId", query.HasKey ? query.ToKey().UnsafeToString() : null) .WithParam("@materialId", query.MaterialId) .WithParam("@orderNumber", ToProperNull(query.OrderNumber)) .WithParam("@batchNumber", ToProperNull(query.BatchNumberQuery?.Replace("*", "%"))) .WithParam("@dtFrom", query.From) .WithParam("@dtTo", query.To) .WithParam("@closed", query.ClosedBatches) .WithParam("@locked", query.LockedBatches) .WithParam("@inventoryTypeId", query.InventoryTypeId) .WithParam("@onlyProduced", query.ProducedOnly) .WithParam("@onlyBought", query.PurchasedOnly) .WithParam("@compositionId", query.CompositionId) .WithParam("@componentId", query.ComponentId) .WithParam("@orderId", query.RelativeToOrderId) .WithParam("@onlyBlocking", query.BlockedBatchesOnly) .WithParam("@segmentId", query.SegmentId) .WithParam("@invoiceNr", ToProperNull(query.InvoiceNr)); var result = new BatchReportModel { Query = query }; var rawEntries = sql.MapRows(MapEntry); result.Report.AddRange(rawEntries); result.CanLoadMore = (result.Report.Count == c_pageSize); foreach (var b in result.Report.OfType <BatchReportEntry>()) { var material = m_materialRepository.GetMaterialById(b.MaterialId); if (b.IsClosed) { b.AvailableAmount = "0"; } else { /* * var available = m_batchFacade.GetAvailableAmount(b.BatchKey); * b.AvailableAmount = $"{StringUtil.FormatDecimal(available.Value)} {available.Unit.Symbol}"; * b.Available = available; */ var available = new Amount(b.AvailableAmountValue, m_unitRepository.GetUnit(b.AvailableAmountUnitId)); available = m_amountProcessor.Convert(available, material.NominalUnit); b.AvailableAmount = $"{StringUtil.FormatDecimal(available.Value)} {available.Unit.Symbol}"; b.Available = available; } var totalUnit = m_unitRepository.GetUnitBySymbol(b.TotalAmountUnitName); var totalAmount = new Amount(b.TotalAmountValue, totalUnit); totalAmount = m_amountProcessor.Convert(totalAmount, material.NominalUnit); b.BatchVolume = $"{StringUtil.FormatDecimal(totalAmount.Value)} {totalAmount.Unit.Symbol}"; //b.NoDelReason = m_batchFacade.GetDeletionBlockReasons(b.BatchId).FirstOrDefault(); b.CanDelete = !(b.HasStockEvents || b.NumberOfCompositions > 0 || b.NumberOfOrders > 0 || b.NumberOfSaleEvents > 0); if (b.HasStockEvents) { PopulateStockEventCounts(b); } PopulateStockEventSuggestions(b); } if ((query.HasKey) && (result.Report.Count == 0)) { result.Report.Add(new DeletedBatchReportEntry(query.ToKey())); return(result); } if (query.CompositionId != null) { PopulateComponentAmounts(BatchKey.Parse(query.CompositionId), result.Report); result.CustomField1Name = "Použito"; } else if (query.ComponentId != null) { PopulateCompositionAmounts(BatchKey.Parse(query.ComponentId), result.Report); result.CustomField1Name = "Použito"; } else if (query.RelativeToOrderId != null) { result.Report = PopulateRelativeToOrder(order, result.Report); result.CustomField1Name = "Množství"; result.CustomField3Name = "Položka"; result.CustomField2Name = "Balil"; } return(result); }