use of com.qcadoo.view.api.ViewDefinitionState in project mes by qcadoo.
the class PlannedEventDetailsListeners method onRemoveRelatedEvents.
public void onRemoveRelatedEvents(final ViewDefinitionState view, final ComponentState state, final String[] args) {
GridComponent relatedEventsGrid = (GridComponent) view.getComponentByReference(PlannedEventFields.RELATED_EVENTS);
FormComponent form = (FormComponent) view.getComponentByReference(QcadooViewConstants.L_FORM);
Entity currentEvent = dataDefinitionService.get(CmmsMachinePartsConstants.PLUGIN_IDENTIFIER, CmmsMachinePartsConstants.MODEL_PLANNED_EVENT).get(form.getEntityId());
List<Entity> relatedEventsForCurrentEvent = Lists.newArrayList(currentEvent.getManyToManyField(PlannedEventFields.RELATED_EVENTS));
List<Entity> relatedEventsToDelete = relatedEventsGrid.getSelectedEntities();
for (Entity relatedEventToDelete : relatedEventsToDelete) {
List<Entity> relatedEvents = Lists.newArrayList(relatedEventToDelete.getManyToManyField(PlannedEventFields.RELATED_EVENTS));
Optional<Entity> eventToDelete = relatedEvents.stream().filter(event -> event.getId().compareTo(currentEvent.getId()) == 0).findFirst();
if (eventToDelete.isPresent()) {
relatedEvents.remove(eventToDelete.get());
relatedEventToDelete.setField(PlannedEventFields.RELATED_EVENTS, relatedEvents);
relatedEventToDelete.getDataDefinition().save(relatedEventToDelete);
}
Optional<Entity> eventToDeleteFromCurrent = relatedEventsForCurrentEvent.stream().filter(event -> event.getId().compareTo(relatedEventToDelete.getId()) == 0).findFirst();
if (eventToDeleteFromCurrent.isPresent()) {
relatedEventsForCurrentEvent.remove(eventToDeleteFromCurrent.get());
}
}
currentEvent.setField(PlannedEventFields.RELATED_EVENTS, relatedEventsForCurrentEvent);
currentEvent.getDataDefinition().save(currentEvent);
Entity formEntity = form.getEntity();
formEntity = formEntity.getDataDefinition().get(formEntity.getId());
form.setEntity(formEntity);
view.performEvent(view, "refresh");
}
use of com.qcadoo.view.api.ViewDefinitionState in project mes by qcadoo.
the class DeliveryDetailsListeners method getSelectedProducts.
private List<Entity> getSelectedProducts(final ViewDefinitionState view) {
GridComponent orderedProductGrid = (GridComponent) view.getComponentByReference(DeliveryFields.ORDERED_PRODUCTS);
List<Entity> result = Lists.newArrayList();
Set<Long> ids = orderedProductGrid.getSelectedEntitiesIds();
if (Objects.nonNull(ids) && !ids.isEmpty()) {
final SearchCriteriaBuilder searchCriteria = deliveriesService.getOrderedProductDD().find();
searchCriteria.add(SearchRestrictions.in("id", ids));
result = searchCriteria.list().getEntities();
if (!result.isEmpty()) {
String numbersFilter = orderedProductGrid.getFilters().get("productNumber");
String numberAndAdditionalCodeFilter = orderedProductGrid.getFilters().get("mergedProductNumberAndAdditionalCode");
if (StringUtils.isNotBlank(numbersFilter) && numbersFilter.startsWith("[") && numbersFilter.endsWith("]")) {
List<String> numbersOrder = Lists.newArrayList(numbersFilter.replace("[", "").replace("]", "").split(","));
result.sort((o1, o2) -> {
String number1 = o1.getBelongsToField(OrderedProductFields.PRODUCT).getStringField(ProductFields.NUMBER);
String number2 = o2.getBelongsToField(OrderedProductFields.PRODUCT).getStringField(ProductFields.NUMBER);
return Integer.valueOf(numbersOrder.indexOf(number1)).compareTo(numbersOrder.indexOf(number2));
});
} else if (StringUtils.isNotBlank(numberAndAdditionalCodeFilter) && numberAndAdditionalCodeFilter.startsWith("[") && numberAndAdditionalCodeFilter.endsWith("]")) {
List<String> numbersOrder = Lists.newArrayList(numberAndAdditionalCodeFilter.replace("[", "").replace("]", "").split(","));
result.sort((o1, o2) -> {
String number1 = Optional.ofNullable(o1.getBelongsToField(OrderedProductFields.ADDITIONAL_CODE)).map(ac -> ac.getStringField(AdditionalCodeFields.CODE)).orElse(o1.getBelongsToField(OrderedProductFields.PRODUCT).getStringField(ProductFields.NUMBER));
String number2 = Optional.ofNullable(o2.getBelongsToField(OrderedProductFields.ADDITIONAL_CODE)).map(ac -> ac.getStringField(AdditionalCodeFields.CODE)).orElse(o2.getBelongsToField(OrderedProductFields.PRODUCT).getStringField(ProductFields.NUMBER));
return Integer.valueOf(numbersOrder.indexOf(number1)).compareTo(numbersOrder.indexOf(number2));
});
}
}
}
return result;
}
use of com.qcadoo.view.api.ViewDefinitionState in project mes by qcadoo.
the class ProductsToIssueDetailsHooks method onBeforeRender.
public void onBeforeRender(final ViewDefinitionState view) {
FormComponent productToIssueForm = (FormComponent) view.getComponentByReference(QcadooViewConstants.L_FORM);
FieldComponent locationFromLabel = (FieldComponent) view.getComponentByReference("locationFromLabel");
FieldComponent helperField = (FieldComponent) view.getComponentByReference("generated");
Entity helper = productToIssueForm.getEntity();
Entity locationFrom = helper.getBelongsToField("locationFrom");
if (Objects.nonNull(locationFrom)) {
locationFromLabel.setFieldValue(translationService.translate("productFlowThruDivision.productsToIssueHelperDetails.window.mainTab.form.locationFromLabel.label", LocaleContextHolder.getLocale(), locationFrom.getStringField(LocationFields.NUMBER)));
locationFromLabel.setRequired(true);
}
if (view.isViewAfterRedirect() && helper.getHasManyField("issues").isEmpty()) {
String idsStr = helper.getStringField("productsToIssueIds");
String[] split = idsStr.split(",");
List<Long> ids = Lists.newArrayList(split).stream().map(Long::valueOf).collect(Collectors.toList());
Multimap<Entity, Entity> locationFromProductMultimap = HashMultimap.create();
Multimap<Entity, Entity> locationToProductMultimap = HashMultimap.create();
List<Entity> createdIssues = Lists.newArrayList();
for (Long id : ids) {
Entity productToIssue = getProductToIssueDD().get(id);
Entity product = productToIssue.getBelongsToField(ProductsToIssueFields.PRODUCT);
locationFromProductMultimap.put(productToIssue.getBelongsToField(ProductsToIssueFields.WAREHOUSE_ISSUE).getBelongsToField(WarehouseIssueFields.PLACE_OF_ISSUE), product);
locationToProductMultimap.put(productToIssue.getBelongsToField(ProductsToIssueFields.LOCATION), product);
createdIssues.add(createIssue(getIssueDD(), productToIssue));
}
Map<Long, Map<Long, BigDecimal>> stockForWarehousesFrom = getStockForWarehouses(locationFromProductMultimap);
Map<Long, Map<Long, BigDecimal>> stockForWarehousesTo = getStockForWarehouses(locationToProductMultimap);
fillLocationQuantity(createdIssues, stockForWarehousesFrom, stockForWarehousesTo);
helper.setField("issues", sortIssuesBasedOnFilter(view, createdIssues));
productToIssueForm.setEntity(helper);
} else {
List<Entity> issues = helper.getHasManyField("issues");
if (!issues.isEmpty() && issues.stream().allMatch(issue -> Objects.nonNull(issue.getId()))) {
WindowComponent window = (WindowComponent) view.getComponentByReference(QcadooViewConstants.L_WINDOW);
Ribbon ribbon = window.getRibbon();
RibbonGroup group = ribbon.getGroupByName("actions");
RibbonActionItem createDocumentsItem = group.getItemByName("createDocuments");
RibbonActionItem createDocumentsAndGoBackItem = group.getItemByName("createDocumentsAndGoBack");
createDocumentsItem.setEnabled(!Boolean.valueOf((String) helperField.getFieldValue()));
createDocumentsItem.requestUpdate(true);
createDocumentsAndGoBackItem.setEnabled(!Boolean.valueOf((String) helperField.getFieldValue()));
createDocumentsAndGoBackItem.requestUpdate(true);
}
fillQuantitiesInAdditionalUnit(view);
}
fillUnits(view);
}
use of com.qcadoo.view.api.ViewDefinitionState in project mes by qcadoo.
the class ProductsToIssueHelperDetailsListeners method copyPositionMessages.
private void copyPositionMessages(Entity invalidPosition, final ViewDefinitionState componentMessagesHolder) {
if (componentMessagesHolder == null) {
return;
}
Locale locale = LocaleContextHolder.getLocale();
String productNumber = Optional.of(invalidPosition).map(ip -> ip.getBelongsToField(PositionFields.PRODUCT)).map(p -> p.getStringField(ProductFields.NUMBER)).orElse("???");
for (ErrorMessage errorMessage : invalidPosition.getGlobalErrors()) {
String translatedMessage = translationService.translate(errorMessage.getMessage(), locale, errorMessage.getVars());
translatedMessage = translationService.translate("productFlowThruDivision.issue.documentBuild.position.error", locale, translatedMessage, productNumber);
componentMessagesHolder.addTranslatedMessage(translatedMessage, ComponentState.MessageType.FAILURE, errorMessage.getAutoClose(), errorMessage.isExtraLarge());
}
for (ErrorMessage errorMessage : invalidPosition.getErrors().values()) {
String translatedMessage = translationService.translate(errorMessage.getMessage(), locale, errorMessage.getVars());
translatedMessage = translationService.translate("productFlowThruDivision.issue.documentBuild.position.error", locale, translatedMessage, productNumber);
componentMessagesHolder.addTranslatedMessage(translatedMessage, ComponentState.MessageType.FAILURE, errorMessage.getAutoClose(), errorMessage.isExtraLarge());
}
}
use of com.qcadoo.view.api.ViewDefinitionState in project mes by qcadoo.
the class ProductsToIssueHelperDetailsListeners method updateQuantities.
public void updateQuantities(final ViewDefinitionState view, final ComponentState state, final String[] args) {
AwesomeDynamicListComponent adl = (AwesomeDynamicListComponent) view.getComponentByReference(L_ISSUES);
Long changedId = (Long) state.getFieldValue();
for (FormComponent form : adl.getFormComponents()) {
Entity issue = form.getPersistedEntityWithIncludedFormValues();
Entity product = issue.getBelongsToField(IssueFields.PRODUCT);
if (product != null && product.getId().equals(changedId)) {
Entity locationTo = issue.getBelongsToField(IssueFields.LOCATION);
Entity warehouseIssue = issue.getBelongsToField(IssueFields.WAREHOUSE_ISSUE);
Entity locationFrom = warehouseIssue.getBelongsToField(WarehouseIssueFields.PLACE_OF_ISSUE);
Optional<Entity> maybeProductToIssue = warehouseIssue.getHasManyField(WarehouseIssueFields.PRODUCTS_TO_ISSUES).stream().filter(p -> p.getBelongsToField(ProductsToIssueFields.PRODUCT).getId().equals(product.getId()) && p.getBelongsToField(ProductsToIssueFields.LOCATION).getId().equals(locationTo.getId())).findFirst();
BigDecimal quantityFrom = materialFlowResourcesService.getResourcesQuantityForLocationAndProduct(locationFrom, product);
BigDecimal quantityTo = materialFlowResourcesService.getResourcesQuantityForLocationAndProduct(locationTo, product);
if (maybeProductToIssue.isPresent()) {
Entity productToIssue = maybeProductToIssue.get();
BigDecimal demandQuantity = productToIssue.getDecimalField(ProductsToIssueFields.DEMAND_QUANTITY);
BigDecimal issuedQuantity = productToIssue.getDecimalField(ProductsToIssueFields.ISSUE_QUANTITY);
issue.setField(IssueFields.ADDITIONAL_CODE, productToIssue.getBelongsToField(ProductsToIssueFields.ADDITIONAL_CODE));
issue.setField(IssueFields.STORAGE_LOCATION, productToIssue.getBelongsToField(ProductsToIssueFields.STORAGE_LOCATION));
BigDecimal quantityPerUnit = null;
BigDecimal issueQuantity = BigDecimal.ZERO;
if (warehouseIssueParameterService.issueForOrder()) {
Entity order = warehouseIssue.getBelongsToField(WarehouseIssueFields.ORDER);
BigDecimal plannedQuantity = order.getDecimalField(OrderFields.PLANNED_QUANTITY);
if (plannedQuantity != null && demandQuantity != null) {
quantityPerUnit = demandQuantity.divide(plannedQuantity, numberService.getMathContext());
}
}
if (demandQuantity != null && issuedQuantity != null) {
issueQuantity = demandQuantity.subtract(issuedQuantity);
}
if (issueQuantity.compareTo(BigDecimal.ZERO) == -1) {
issueQuantity = BigDecimal.ZERO;
}
issue.setField(IssueFields.DEMAND_QUANTITY, demandQuantity);
issue.setField(IssueFields.QUANTITY_PER_UNIT, quantityPerUnit);
issue.setField(IssueFields.ISSUE_QUANTITY, issueQuantity);
BigDecimal conversion = productToIssue.getDecimalField(ProductsToIssueFields.CONVERSION);
if (conversion != null) {
issue.setField(IssueFields.CONVERSION, conversion);
String unit = product.getStringField(ProductFields.UNIT);
String additionalUnit = product.getStringField(ProductFields.ADDITIONAL_UNIT);
BigDecimal newAdditionalQuantity = calculationQuantityService.calculateAdditionalQuantity(demandQuantity, conversion, Optional.ofNullable(additionalUnit).orElse(unit));
issue.setField(IssueFields.ADDITIONAL_DEMAND_QUANTITY, newAdditionalQuantity);
}
} else {
issue.setField(IssueFields.DEMAND_QUANTITY, BigDecimal.ZERO);
issue.setField(IssueFields.QUANTITY_PER_UNIT, BigDecimal.ZERO);
}
issue.setField(IssueFields.LOCATIONS_QUANTITY, quantityFrom);
issue.setField(IssueFields.LOCATION_TO_QUANTITY, quantityTo);
form.setEntity(issue);
}
}
}
Aggregations