mirror of
https://github.com/s-frick/effigenix.git
synced 2026-03-28 15:59:35 +01:00
feat(production,inventory): Produktionsergebnis automatisch einbuchen (US-7.2)
Event-Infrastruktur (DomainEvent, DomainEventPublisher) im Shared Kernel eingeführt. CompleteBatch publiziert BatchCompleted-Event mit articleId aus Recipe. ProductionDomainEventPublisher konvertiert in IntegrationEvent, BatchCompletedInventoryListener bucht automatisch StockBatch (PRODUCED) + StockMovement (PRODUCTION_OUTPUT) am PRODUCTION_AREA-Lagerort ein. TUI RecordConsumptionScreen: Rezeptbasierte Zutatenauswahl mit skalierten Soll-Mengen, Stock-Batch-Picker und Mengen-Vorbelegung.
This commit is contained in:
parent
e9f2948e61
commit
aa7ac785bb
16 changed files with 797 additions and 117 deletions
|
|
@ -0,0 +1,131 @@
|
|||
package de.effigenix.application.inventory;
|
||||
|
||||
import de.effigenix.application.inventory.command.BookProductionOutputCommand;
|
||||
import de.effigenix.domain.inventory.*;
|
||||
import de.effigenix.shared.common.Result;
|
||||
import de.effigenix.shared.persistence.UnitOfWork;
|
||||
|
||||
public class BookProductionOutput {
|
||||
|
||||
private final StockRepository stockRepository;
|
||||
private final StorageLocationRepository storageLocationRepository;
|
||||
private final StockMovementRepository stockMovementRepository;
|
||||
private final UnitOfWork unitOfWork;
|
||||
|
||||
public BookProductionOutput(StockRepository stockRepository,
|
||||
StorageLocationRepository storageLocationRepository,
|
||||
StockMovementRepository stockMovementRepository,
|
||||
UnitOfWork unitOfWork) {
|
||||
this.stockRepository = stockRepository;
|
||||
this.storageLocationRepository = storageLocationRepository;
|
||||
this.stockMovementRepository = stockMovementRepository;
|
||||
this.unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public Result<ProductionOutputError, Void> execute(BookProductionOutputCommand cmd) {
|
||||
// 1. Find active PRODUCTION_AREA storage location
|
||||
StorageLocation productionArea;
|
||||
switch (storageLocationRepository.findByStorageType(StorageType.PRODUCTION_AREA)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOutputError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var locations) -> {
|
||||
var active = locations.stream().filter(StorageLocation::active).findFirst();
|
||||
if (active.isEmpty()) {
|
||||
return Result.failure(new ProductionOutputError.NoProductionArea(
|
||||
"No active PRODUCTION_AREA storage location found"));
|
||||
}
|
||||
productionArea = active.get();
|
||||
}
|
||||
}
|
||||
|
||||
var articleId = de.effigenix.domain.masterdata.article.ArticleId.of(cmd.articleId());
|
||||
var storageLocationId = productionArea.id();
|
||||
|
||||
return unitOfWork.executeAtomically(() -> {
|
||||
// 2. Find or create Stock for (articleId, storageLocationId)
|
||||
Stock stock;
|
||||
switch (stockRepository.findByArticleIdAndStorageLocationId(articleId, storageLocationId)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOutputError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var opt) -> {
|
||||
if (opt.isPresent()) {
|
||||
stock = opt.get();
|
||||
} else {
|
||||
// Auto-create stock with no minimumLevel/minimumShelfLife
|
||||
var stockDraft = new StockDraft(
|
||||
cmd.articleId(), storageLocationId.value(), null, null, null);
|
||||
switch (Stock.create(stockDraft)) {
|
||||
case Result.Failure(var stockErr) -> {
|
||||
return Result.failure(new ProductionOutputError.StockCreationFailed(
|
||||
stockErr.message()));
|
||||
}
|
||||
case Result.Success(var newStock) -> stock = newStock;
|
||||
}
|
||||
switch (stockRepository.save(stock)) {
|
||||
case Result.Failure(var saveErr) -> {
|
||||
return Result.failure(new ProductionOutputError.RepositoryFailure(
|
||||
saveErr.message()));
|
||||
}
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Add batch to stock
|
||||
StockBatch stockBatch;
|
||||
var batchDraft = new StockBatchDraft(
|
||||
cmd.batchNumber(), "PRODUCED",
|
||||
cmd.quantityAmount(), cmd.quantityUnit(),
|
||||
cmd.bestBeforeDate());
|
||||
switch (stock.addBatch(batchDraft)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOutputError.BatchAdditionFailed(err.message()));
|
||||
}
|
||||
case Result.Success(var batch) -> stockBatch = batch;
|
||||
}
|
||||
|
||||
// 4. Save stock with new batch
|
||||
switch (stockRepository.save(stock)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOutputError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
|
||||
// 5. Record stock movement
|
||||
var movementDraft = new StockMovementDraft(
|
||||
stock.id().value(),
|
||||
cmd.articleId(),
|
||||
stockBatch.id().value(),
|
||||
cmd.batchNumber(),
|
||||
"PRODUCED",
|
||||
"PRODUCTION_OUTPUT",
|
||||
null,
|
||||
cmd.quantityAmount(),
|
||||
cmd.quantityUnit(),
|
||||
null,
|
||||
null,
|
||||
"SYSTEM"
|
||||
);
|
||||
StockMovement movement;
|
||||
switch (StockMovement.record(movementDraft)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOutputError.MovementRecordingFailed(err.message()));
|
||||
}
|
||||
case Result.Success(var mv) -> movement = mv;
|
||||
}
|
||||
|
||||
switch (stockMovementRepository.save(movement)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOutputError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
|
||||
return Result.success(null);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package de.effigenix.application.inventory.command;
|
||||
|
||||
public record BookProductionOutputCommand(
|
||||
String batchNumber,
|
||||
String articleId,
|
||||
String quantityAmount,
|
||||
String quantityUnit,
|
||||
String bestBeforeDate
|
||||
) {}
|
||||
|
|
@ -2,20 +2,30 @@ package de.effigenix.application.production;
|
|||
|
||||
import de.effigenix.application.production.command.CompleteBatchCommand;
|
||||
import de.effigenix.domain.production.*;
|
||||
import de.effigenix.domain.production.event.BatchCompleted;
|
||||
import de.effigenix.shared.common.Result;
|
||||
import de.effigenix.shared.event.DomainEventPublisher;
|
||||
import de.effigenix.shared.persistence.UnitOfWork;
|
||||
import de.effigenix.shared.security.ActorId;
|
||||
import de.effigenix.shared.security.AuthorizationPort;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public class CompleteBatch {
|
||||
|
||||
private final BatchRepository batchRepository;
|
||||
private final RecipeRepository recipeRepository;
|
||||
private final AuthorizationPort authorizationPort;
|
||||
private final DomainEventPublisher domainEventPublisher;
|
||||
private final UnitOfWork unitOfWork;
|
||||
|
||||
public CompleteBatch(BatchRepository batchRepository, AuthorizationPort authorizationPort, UnitOfWork unitOfWork) {
|
||||
public CompleteBatch(BatchRepository batchRepository, RecipeRepository recipeRepository,
|
||||
AuthorizationPort authorizationPort, DomainEventPublisher domainEventPublisher,
|
||||
UnitOfWork unitOfWork) {
|
||||
this.batchRepository = batchRepository;
|
||||
this.recipeRepository = recipeRepository;
|
||||
this.authorizationPort = authorizationPort;
|
||||
this.domainEventPublisher = domainEventPublisher;
|
||||
this.unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
|
|
@ -59,6 +69,34 @@ public class CompleteBatch {
|
|||
}
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
|
||||
// Load recipe to get articleId for the event
|
||||
Recipe recipe;
|
||||
switch (recipeRepository.findById(batch.recipeId())) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new BatchError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var opt) -> {
|
||||
if (opt.isEmpty()) {
|
||||
return Result.failure(new BatchError.RepositoryFailure(
|
||||
"Recipe not found: " + batch.recipeId().value()));
|
||||
}
|
||||
recipe = opt.get();
|
||||
}
|
||||
}
|
||||
|
||||
domainEventPublisher.publish(new BatchCompleted(
|
||||
batch.id(),
|
||||
batch.batchNumber(),
|
||||
batch.recipeId(),
|
||||
recipe.articleId(),
|
||||
batch.actualQuantity(),
|
||||
batch.waste(),
|
||||
batch.bestBeforeDate(),
|
||||
batch.completedAt(),
|
||||
Instant.now()
|
||||
));
|
||||
|
||||
return Result.success(batch);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
package de.effigenix.domain.inventory;
|
||||
|
||||
public sealed interface ProductionOutputError {
|
||||
|
||||
String message();
|
||||
|
||||
record NoProductionArea(String message) implements ProductionOutputError {}
|
||||
record StockCreationFailed(String message) implements ProductionOutputError {}
|
||||
record BatchAdditionFailed(String message) implements ProductionOutputError {}
|
||||
record MovementRecordingFailed(String message) implements ProductionOutputError {}
|
||||
record RepositoryFailure(String message) implements ProductionOutputError {}
|
||||
}
|
||||
|
|
@ -1,17 +1,23 @@
|
|||
package de.effigenix.domain.production.event;
|
||||
|
||||
import de.effigenix.domain.production.BatchId;
|
||||
import de.effigenix.domain.production.BatchNumber;
|
||||
import de.effigenix.domain.production.RecipeId;
|
||||
import de.effigenix.shared.common.Quantity;
|
||||
import de.effigenix.shared.event.DomainEvent;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
/**
|
||||
* Stub – wird derzeit nicht publiziert.
|
||||
* Vorgesehen für spätere Event-Infrastruktur (Inventory stock-in, Audit, Tracing).
|
||||
*/
|
||||
public record BatchCompleted(
|
||||
BatchId batchId,
|
||||
BatchNumber batchNumber,
|
||||
RecipeId recipeId,
|
||||
String articleId,
|
||||
Quantity actualQuantity,
|
||||
Quantity waste,
|
||||
OffsetDateTime completedAt
|
||||
) {}
|
||||
LocalDate bestBeforeDate,
|
||||
OffsetDateTime completedAt,
|
||||
Instant occurredAt
|
||||
) implements DomainEvent {}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import de.effigenix.application.inventory.RecordCountItem;
|
|||
import de.effigenix.application.inventory.StartInventoryCount;
|
||||
import de.effigenix.application.inventory.GetStockMovement;
|
||||
import de.effigenix.application.inventory.ListStockMovements;
|
||||
import de.effigenix.application.inventory.BookProductionOutput;
|
||||
import de.effigenix.application.inventory.ConfirmReservation;
|
||||
import de.effigenix.application.inventory.RecordStockMovement;
|
||||
import de.effigenix.application.inventory.ActivateStorageLocation;
|
||||
|
|
@ -144,6 +145,14 @@ public class InventoryUseCaseConfiguration {
|
|||
return new ListStocksBelowMinimum(stockRepository, authorizationPort);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BookProductionOutput bookProductionOutput(StockRepository stockRepository,
|
||||
StorageLocationRepository storageLocationRepository,
|
||||
StockMovementRepository stockMovementRepository,
|
||||
UnitOfWork unitOfWork) {
|
||||
return new BookProductionOutput(stockRepository, storageLocationRepository, stockMovementRepository, unitOfWork);
|
||||
}
|
||||
|
||||
// ==================== StockMovement Use Cases ====================
|
||||
|
||||
@Bean
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ import de.effigenix.domain.production.BatchRepository;
|
|||
import de.effigenix.domain.production.BatchTraceabilityService;
|
||||
import de.effigenix.domain.production.ProductionOrderRepository;
|
||||
import de.effigenix.domain.production.RecipeRepository;
|
||||
import de.effigenix.shared.event.DomainEventPublisher;
|
||||
import de.effigenix.shared.persistence.UnitOfWork;
|
||||
import de.effigenix.shared.security.AuthorizationPort;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
|
@ -134,9 +135,10 @@ public class ProductionUseCaseConfiguration {
|
|||
}
|
||||
|
||||
@Bean
|
||||
public CompleteBatch completeBatch(BatchRepository batchRepository, AuthorizationPort authorizationPort,
|
||||
public CompleteBatch completeBatch(BatchRepository batchRepository, RecipeRepository recipeRepository,
|
||||
AuthorizationPort authorizationPort, DomainEventPublisher domainEventPublisher,
|
||||
UnitOfWork unitOfWork) {
|
||||
return new CompleteBatch(batchRepository, authorizationPort, unitOfWork);
|
||||
return new CompleteBatch(batchRepository, recipeRepository, authorizationPort, domainEventPublisher, unitOfWork);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
package de.effigenix.infrastructure.integration.event;
|
||||
|
||||
public record BatchCompletedIntegrationEvent(
|
||||
String batchId,
|
||||
String batchNumber,
|
||||
String articleId,
|
||||
String quantityAmount,
|
||||
String quantityUnit,
|
||||
String bestBeforeDate
|
||||
) {}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
package de.effigenix.infrastructure.inventory.event;
|
||||
|
||||
import de.effigenix.application.inventory.BookProductionOutput;
|
||||
import de.effigenix.application.inventory.command.BookProductionOutputCommand;
|
||||
import de.effigenix.infrastructure.integration.event.BatchCompletedIntegrationEvent;
|
||||
import de.effigenix.shared.common.Result;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class BatchCompletedInventoryListener {
|
||||
|
||||
private static final System.Logger logger = System.getLogger(BatchCompletedInventoryListener.class.getName());
|
||||
|
||||
private final BookProductionOutput bookProductionOutput;
|
||||
|
||||
public BatchCompletedInventoryListener(BookProductionOutput bookProductionOutput) {
|
||||
this.bookProductionOutput = bookProductionOutput;
|
||||
}
|
||||
|
||||
@EventListener
|
||||
public void on(BatchCompletedIntegrationEvent event) {
|
||||
var cmd = new BookProductionOutputCommand(
|
||||
event.batchNumber(),
|
||||
event.articleId(),
|
||||
event.quantityAmount(),
|
||||
event.quantityUnit(),
|
||||
event.bestBeforeDate()
|
||||
);
|
||||
switch (bookProductionOutput.execute(cmd)) {
|
||||
case Result.Failure(var err) ->
|
||||
logger.log(System.Logger.Level.WARNING,
|
||||
"Failed to book production output for batch {0}: {1}",
|
||||
event.batchNumber(), err.message());
|
||||
case Result.Success(var ignored) ->
|
||||
logger.log(System.Logger.Level.INFO,
|
||||
"Booked production output for batch {0}", event.batchNumber());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package de.effigenix.infrastructure.production.event;
|
||||
|
||||
import de.effigenix.domain.production.event.BatchCompleted;
|
||||
import de.effigenix.infrastructure.integration.event.BatchCompletedIntegrationEvent;
|
||||
import de.effigenix.shared.event.DomainEvent;
|
||||
import de.effigenix.shared.event.DomainEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ProductionDomainEventPublisher implements DomainEventPublisher {
|
||||
|
||||
private final ApplicationEventPublisher publisher;
|
||||
|
||||
public ProductionDomainEventPublisher(ApplicationEventPublisher publisher) {
|
||||
this.publisher = publisher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publish(DomainEvent event) {
|
||||
switch (event) {
|
||||
case BatchCompleted bc -> publisher.publishEvent(
|
||||
new BatchCompletedIntegrationEvent(
|
||||
bc.batchId().value(),
|
||||
bc.batchNumber().value(),
|
||||
bc.articleId(),
|
||||
bc.actualQuantity().amount().toPlainString(),
|
||||
bc.actualQuantity().uom().name(),
|
||||
bc.bestBeforeDate().toString()
|
||||
)
|
||||
);
|
||||
default -> { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package de.effigenix.shared.event;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public interface DomainEvent {
|
||||
Instant occurredAt();
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package de.effigenix.shared.event;
|
||||
|
||||
public interface DomainEventPublisher {
|
||||
void publish(DomainEvent event);
|
||||
}
|
||||
|
|
@ -8,6 +8,8 @@ package de.effigenix.shared.security;
|
|||
*/
|
||||
public record ActorId(String value) {
|
||||
|
||||
public static final ActorId SYSTEM = new ActorId("SYSTEM");
|
||||
|
||||
public ActorId {
|
||||
if (value == null || value.isBlank()) {
|
||||
throw new IllegalArgumentException("ActorId cannot be null or empty");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue