use of com.qcadoo.view.api.ViewDefinitionState in project mes by qcadoo.
the class DeliveryDetailsListeners method downloadProductAttachment.
public void downloadProductAttachment(final ViewDefinitionState view, final ComponentState state, final String[] args) {
FormComponent deliveryForm = (FormComponent) view.getComponentByReference(QcadooViewConstants.L_FORM);
GridComponent orderedProductsGrid = (GridComponent) view.getComponentByReference(DeliveryFields.ORDERED_PRODUCTS);
Set<Long> ids = orderedProductsGrid.getSelectedEntitiesIds();
SearchCriteriaBuilder searchCriteria = deliveriesService.getOrderedProductDD().find().createAlias(BasicConstants.MODEL_PRODUCT, BasicConstants.MODEL_PRODUCT, JoinType.INNER).createAlias(BasicConstants.MODEL_PRODUCT + L_DOT + ProductFields.PRODUCT_ATTACHMENTS, ProductFields.PRODUCT_ATTACHMENTS, JoinType.INNER).setProjection(SearchProjections.list().add(alias(field(ProductFields.PRODUCT_ATTACHMENTS + L_DOT + ProductAttachmentFields.ATTACHMENT), ProductAttachmentFields.ATTACHMENT)));
if (ids.isEmpty()) {
searchCriteria.createAlias(DeliveriesConstants.MODEL_DELIVERY, DeliveriesConstants.MODEL_DELIVERY, JoinType.INNER).add(SearchRestrictions.in(DeliveriesConstants.MODEL_DELIVERY + L_DOT + "id", deliveryForm.getEntityId()));
} else {
searchCriteria.add(SearchRestrictions.in("id", ids));
}
List<Entity> result = searchCriteria.list().getEntities();
if (result.isEmpty()) {
return;
}
List<File> attachments = result.stream().map(productAttachment -> new File(productAttachment.getStringField(ProductAttachmentFields.ATTACHMENT))).collect(Collectors.toList());
File zipFile;
try {
zipFile = fileService.compressToZipFile(attachments, false);
} catch (IOException e) {
LOG.error("Unable to compress documents to zip file.", e);
return;
}
view.redirectTo(fileService.getUrl(zipFile.getAbsolutePath()) + "?clean", true, false);
}
use of com.qcadoo.view.api.ViewDefinitionState in project mes by qcadoo.
the class DeliveryDetailsListeners method fillBasedOnPricesFromLastDeliveryOffer.
private void fillBasedOnPricesFromLastDeliveryOffer(final ViewDefinitionState view, final boolean deliveryUseNominalCostWhenPriceNotSpecified) {
GridComponent orderedProductsGrid = (GridComponent) view.getComponentByReference(DeliveryFields.ORDERED_PRODUCTS);
FieldComponent currencyField = (FieldComponent) view.getComponentByReference(DeliveryFields.CURRENCY);
Long deliveryCurrencyId = (Long) currencyField.getFieldValue();
Entity plnCurrency = currencyService.getCurrencyByAlphabeticCode(CurrencyService.PLN);
List<Entity> orderedProducts = deliveriesService.getSelectedOrderedProducts(orderedProductsGrid);
List<Entity> productsToMessage = Lists.newArrayList();
orderedProducts.forEach(orderedProduct -> {
Entity product = orderedProduct.getBelongsToField(OrderedProductFields.PRODUCT);
Entity supplier = orderedProduct.getBelongsToField(OrderedProductFields.DELIVERY).getBelongsToField(DeliveryFields.SUPPLIER);
if (Objects.isNull(supplier)) {
if (deliveryUseNominalCostWhenPriceNotSpecified) {
Entity currency = product.getBelongsToField(ProductFieldsCNFP.NOMINAL_COST_CURRENCY);
BigDecimal pricePerUnit = getPricePerUnit(deliveryCurrencyId, plnCurrency, product, productsToMessage, currency, product.getDecimalField(ProductFieldsCNFP.NOMINAL_COST));
orderedProduct.setField(OrderedProductFields.PRICE_PER_UNIT, pricePerUnit);
orderedProduct.getDataDefinition().save(orderedProduct);
}
return;
}
Entity offerProduct = getLastOfferProduct(supplier, product);
if (Objects.nonNull(offerProduct)) {
Entity currency = currencyService.getCurrentCurrency();
BigDecimal pricePerUnit = getPricePerUnit(deliveryCurrencyId, plnCurrency, product, productsToMessage, currency, offerProduct.getDecimalField(OrderedProductFields.PRICE_PER_UNIT));
orderedProduct.setField(OrderedProductFields.PRICE_PER_UNIT, pricePerUnit);
orderedProduct.setField(L_OFFER, offerProduct.getBelongsToField(L_OFFER));
orderedProduct.getDataDefinition().save(orderedProduct);
} else if (deliveryUseNominalCostWhenPriceNotSpecified) {
Entity currency = product.getBelongsToField(ProductFieldsCNFP.NOMINAL_COST_CURRENCY);
BigDecimal pricePerUnit = getPricePerUnit(deliveryCurrencyId, plnCurrency, product, productsToMessage, currency, product.getDecimalField(ProductFieldsCNFP.NOMINAL_COST));
orderedProduct.setField(OrderedProductFields.PRICE_PER_UNIT, pricePerUnit);
orderedProduct.getDataDefinition().save(orderedProduct);
}
});
if (!productsToMessage.isEmpty()) {
view.addMessage("deliveries.orderedProducts.differentCurrencies", MessageType.INFO, false, productsToMessage.stream().map(e -> e.getStringField(ProductFields.NUMBER)).collect(Collectors.joining(", ")));
}
orderedProductsGrid.reloadEntities();
BigDecimal totalPrice = orderedProducts.stream().filter(orderedProduct -> Objects.nonNull(orderedProduct.getDecimalField(OrderedProductFields.TOTAL_PRICE))).map(orderedProduct -> orderedProduct.getDecimalField(OrderedProductFields.TOTAL_PRICE)).reduce(BigDecimal.ZERO, BigDecimal::add);
FieldComponent totalPriceComponent = (FieldComponent) view.getComponentByReference(L_ORDERED_PRODUCTS_CUMULATED_TOTAL_PRICE);
totalPriceComponent.setFieldValue(numberService.formatWithMinimumFractionDigits(totalPrice, 0));
totalPriceComponent.requestComponentUpdateState();
}
use of com.qcadoo.view.api.ViewDefinitionState in project mes by qcadoo.
the class DeliveryDetailsListeners method fillBasedOnLastPurchasePrice.
private void fillBasedOnLastPurchasePrice(final ViewDefinitionState view, final boolean deliveryUseNominalCostWhenPriceNotSpecified) {
GridComponent orderedProductsGrid = (GridComponent) view.getComponentByReference(DeliveryFields.ORDERED_PRODUCTS);
FieldComponent currencyField = (FieldComponent) view.getComponentByReference(DeliveryFields.CURRENCY);
Long deliveryCurrencyId = (Long) currencyField.getFieldValue();
Entity plnCurrency = currencyService.getCurrencyByAlphabeticCode(CurrencyService.PLN);
List<Entity> orderedProducts = deliveriesService.getSelectedOrderedProducts(orderedProductsGrid);
List<Entity> productsToMessage = Lists.newArrayList();
orderedProducts.forEach(orderedProduct -> {
Entity product = orderedProduct.getBelongsToField(OrderedProductFields.PRODUCT);
BigDecimal lastPurchaseCost = product.getDecimalField(ProductFieldsCNFP.LAST_PURCHASE_COST);
BigDecimal pricePerUnit;
if (BigDecimalUtils.convertNullToZero(lastPurchaseCost).compareTo(BigDecimal.ZERO) == 0 && deliveryUseNominalCostWhenPriceNotSpecified) {
Entity currency = product.getBelongsToField(ProductFieldsCNFP.NOMINAL_COST_CURRENCY);
pricePerUnit = getPricePerUnit(deliveryCurrencyId, plnCurrency, product, productsToMessage, currency, product.getDecimalField(ProductFieldsCNFP.NOMINAL_COST));
} else {
if (Objects.isNull(lastPurchaseCost)) {
orderedProduct.setField(OrderedProductFields.TOTAL_PRICE, null);
}
Entity currency = product.getBelongsToField(ProductFieldsCNFP.LAST_PURCHASE_COST_CURRENCY);
pricePerUnit = getPricePerUnit(deliveryCurrencyId, plnCurrency, product, productsToMessage, currency, lastPurchaseCost);
}
orderedProduct.setField(OrderedProductFields.PRICE_PER_UNIT, pricePerUnit);
orderedProduct.getDataDefinition().save(orderedProduct);
});
if (!productsToMessage.isEmpty()) {
view.addMessage("deliveries.orderedProducts.differentCurrencies", MessageType.INFO, false, productsToMessage.stream().map(e -> e.getStringField(ProductFields.NUMBER)).collect(Collectors.joining(", ")));
}
orderedProductsGrid.reloadEntities();
BigDecimal totalPrice = orderedProducts.stream().filter(orderedProduct -> Objects.nonNull(orderedProduct.getDecimalField(OrderedProductFields.TOTAL_PRICE))).map(orderedProduct -> orderedProduct.getDecimalField(OrderedProductFields.TOTAL_PRICE)).reduce(BigDecimal.ZERO, BigDecimal::add);
FieldComponent totalPriceComponent = (FieldComponent) view.getComponentByReference(L_ORDERED_PRODUCTS_CUMULATED_TOTAL_PRICE);
totalPriceComponent.setFieldValue(numberService.formatWithMinimumFractionDigits(totalPrice, 0));
totalPriceComponent.requestComponentUpdateState();
}
use of com.qcadoo.view.api.ViewDefinitionState in project mes by qcadoo.
the class SalesPlanDetailsHooks method setRibbonEnabled.
private void setRibbonEnabled(final ViewDefinitionState view) {
FormComponent salesPlanForm = (FormComponent) view.getComponentByReference(QcadooViewConstants.L_FORM);
Entity salesPlan = salesPlanForm.getEntity();
String state = salesPlan.getStringField(SalesPlanFields.STATE);
GridComponent productsGrid = (GridComponent) view.getComponentByReference(SalesPlanFields.PRODUCTS);
WindowComponent window = (WindowComponent) view.getComponentByReference(QcadooViewConstants.L_WINDOW);
Ribbon ribbon = window.getRibbon();
RibbonGroup salesPlanMaterialRequirementRibbonGroup = ribbon.getGroupByName(L_SALES_PLAN_MATERIAL_REQUIREMENT);
RibbonActionItem createSalesPlanMaterialRequirementRibbonActionItem = salesPlanMaterialRequirementRibbonGroup.getItemByName(L_CREATE_SALES_PLAN_MATERIAL_REQUIREMENT);
RibbonActionItem openPositionsImportPageRibbonActionItem = window.getRibbon().getGroupByName(L_IMPORT).getItemByName(L_OPEN_POSITIONS_IMPORT_PAGE);
RibbonGroup technologiesRibbonGroup = ribbon.getGroupByName("technologies");
RibbonActionItem useOtherTechnologyActionItem = technologiesRibbonGroup.getItemByName("useOtherTechnology");
RibbonActionItem fillTechnologyActionItem = technologiesRibbonGroup.getItemByName("fillTechnology");
boolean isEnabled = Objects.nonNull(salesPlanForm.getEntityId()) && state.equals(SalesPlanStateStringValues.DRAFT);
createSalesPlanMaterialRequirementRibbonActionItem.setEnabled(isEnabled);
createSalesPlanMaterialRequirementRibbonActionItem.requestUpdate(true);
openPositionsImportPageRibbonActionItem.setEnabled(isEnabled);
openPositionsImportPageRibbonActionItem.requestUpdate(true);
useOtherTechnologyActionItem.setEnabled(isEnabled && !productsGrid.getSelectedEntitiesIds().isEmpty() && productsGrid.getSelectedEntities().stream().noneMatch(e -> e.getStringField(TECHNOLOGY_NUMBER) == null) && productsGrid.getSelectedEntities().stream().map(e -> e.getStringField(TECHNOLOGY_NUMBER)).distinct().count() == 1L);
useOtherTechnologyActionItem.requestUpdate(true);
fillTechnologyActionItem.setEnabled(isEnabled && !productsGrid.getSelectedEntitiesIds().isEmpty() && productsGrid.getSelectedEntities().stream().allMatch(e -> e.getStringField(TECHNOLOGY_NUMBER) == null && e.getStringField("productFamily") != null) && productsGrid.getSelectedEntities().stream().map(e -> e.getStringField("productFamily")).distinct().count() == 1L);
fillTechnologyActionItem.requestUpdate(true);
}
use of com.qcadoo.view.api.ViewDefinitionState in project mes by qcadoo.
the class CostNormsGeneratorListeners method updateCostNorms.
public void updateCostNorms(final ViewDefinitionState view, final ComponentState state, final String[] args) {
FormComponent costNormsGeneratorForm = (FormComponent) view.getComponentByReference(QcadooViewConstants.L_FORM);
Entity costNormsGenerator = costNormsGeneratorForm.getPersistedEntityWithIncludedFormValues();
String costsSource = costNormsGenerator.getStringField(CostNormsGeneratorFields.COSTS_SOURCE);
if ("01mes".equals(costsSource)) {
boolean allProducts = ProductsToUpdate.of(costNormsGenerator).compareTo(ProductsToUpdate.ALL) == 0;
List<Entity> products = Lists.newArrayList();
if (!allProducts) {
products = costNormsGenerator.getHasManyField(CostNormsGeneratorFields.PRODUCTS);
if (products.isEmpty()) {
view.addMessage("materialFlowResources.info.costNormsNotUpdated", ComponentState.MessageType.INFO);
return;
}
}
List<Entity> warehouses = costNormsGenerator.getHasManyField(CostNormsGeneratorFields.WAREHOUSES).stream().map(warehouse -> warehouse.getBelongsToField(CostNormsLocationFields.LOCATION)).collect(Collectors.toList());
costNormsService.updateCostNormsForProductsFromWarehouses(products, warehouses);
}
view.addMessage("materialFlowResources.success.costNormsUpdated", ComponentState.MessageType.SUCCESS);
}
Aggregations