use of com.qcadoo.mes.orders.controllers.responses.OrderCreationResponse in project mes by qcadoo.
the class OrderCreationService method createOrder.
public OrderCreationResponse createOrder(OrderCreationRequest orderCreationRequest) {
Entity parameter = parameterService.getParameter();
boolean createOperationalTasks = !orderCreationRequest.getTechnologyOperations().isEmpty();
if (Objects.isNull(orderCreationRequest.getTechnologyId())) {
if (!isParameterSet(parameter, createOperationalTasks)) {
return new OrderCreationResponse(translationService.translate("basic.dashboard.orderDefinitionWizard.createOrder.parameterNotSet", LocaleContextHolder.getLocale()));
}
}
Entity product = getProduct(orderCreationRequest.getProductId());
Either<String, Entity> isTechnology = getOrCreateTechnology(orderCreationRequest);
if (isTechnology.isLeft()) {
return new OrderCreationResponse(isTechnology.getLeft());
}
Entity technology = isTechnology.getRight();
Entity productionLine = getProductionLine(orderCreationRequest.getProductionLineId(), technology);
OrderCreationResponse response = new OrderCreationResponse(OrderCreationResponse.StatusCode.OK);
Entity order = dataDefinitionService.get(OrdersConstants.PLUGIN_IDENTIFIER, OrdersConstants.MODEL_ORDER).create();
order.setField(OrderFields.NUMBER, numberGeneratorService.generateNumber(OrdersConstants.PLUGIN_IDENTIFIER, OrdersConstants.MODEL_ORDER, 6));
order.setField(OrderFields.NAME, orderService.makeDefaultName(product, technology, LocaleContextHolder.getLocale()));
order.setField(OrderFields.PRODUCT, product);
order.setField(OrderFields.TECHNOLOGY_PROTOTYPE, technology);
order.setField(OrderFields.PRODUCTION_LINE, productionLine);
order.setField(OrderFields.DIVISION, orderService.getDivision(technology));
order.setField(OrderFields.DATE_FROM, orderCreationRequest.getStartDate());
order.setField(OrderFields.DATE_TO, orderCreationRequest.getFinishDate());
order.setField(OrderFields.EXTERNAL_SYNCHRONIZED, true);
order.setField(IS_SUBCONTRACTED, false);
order.setField(OrderFields.STATE, OrderStateStringValues.PENDING);
order.setField(OrderFields.PLANNED_QUANTITY, orderCreationRequest.getQuantity());
order.setField(OrderFields.DESCRIPTION, buildDescription(parameter, orderCreationRequest.getDescription(), technology, product));
order.setField(IGNORE_MISSING_COMPONENTS, parameter.getBooleanField(IGNORE_MISSING_COMPONENTS));
order.setField("typeOfProductionRecording", orderCreationRequest.getTypeOfProductionRecording());
order = order.getDataDefinition().save(order);
if (order.isValid()) {
if (!order.getGlobalMessages().isEmpty()) {
Optional<GlobalMessage> message = order.getGlobalMessages().stream().filter(gm -> gm.getMessage().equals("orders.order.message.plannedQuantityChanged")).findFirst();
message.ifPresent(m -> {
response.setAdditionalInformation(translationService.translate(m.getMessage(), LocaleContextHolder.getLocale(), m.getVars()[0], m.getVars()[1]));
});
}
final StateChangeContext orderStateChangeContext = stateChangeContextBuilder.build(orderStateChangeAspect.getChangeEntityDescriber(), order, OrderState.ACCEPTED.getStringValue());
orderStateChangeAspect.changeState(orderStateChangeContext);
order = order.getDataDefinition().get(order.getId());
if (!order.getStringField(OrderFields.STATE).equals(OrderStateStringValues.ACCEPTED)) {
return new OrderCreationResponse(translationService.translate("basic.dashboard.orderDefinitionWizard.createOrder.acceptError", LocaleContextHolder.getLocale(), order.getStringField(OrderFields.NUMBER)));
}
} else {
return new OrderCreationResponse(translationService.translate("basic.dashboard.orderDefinitionWizard.createOrder.validationError", LocaleContextHolder.getLocale()));
}
if (createOperationalTasks) {
createOperationalTasks(order, orderCreationRequest);
modifyProductionCountingQuantityForEach(order, orderCreationRequest.getTechnologyOperations());
} else {
modifyProductionCountingQuantity(order, orderCreationRequest.getMaterials());
}
response.setMessage(translationService.translate("orders.orderCreationService.created", LocaleContextHolder.getLocale(), order.getStringField(OrderFields.NUMBER)));
if (createOperationalTasks) {
response.setOperationalTasks(dashboardKanbanDataProvider.getOperationalTasksPendingForOrder(order.getId()));
} else {
response.setOrder(dashboardKanbanDataProvider.getOrder(order.getId()));
}
return response;
}
Aggregations