@Override public ICalculate appendAdditionalDirectCosts() { realProductionCost.value = realProductionCost.value.add(BigDecimalUtils.convertNullToZero(realProductionCost.order .getDecimalField("directAdditionalCost"))); return this; } }
@Override public void parse(String cellValue, BindingErrorsAccessor errorsAccessor, Consumer<Object> valueConsumer) { Locale locale = LocaleContextHolder.getLocale(); if (validateDecimalFormat(cellValue, locale, errorsAccessor)) { Either<Exception, Optional<BigDecimal>> either = BigDecimalUtils.tryParse(cellValue, locale); if (either.isLeft()) { // This is very unlikely to happen as decimal string was already checked errorsAccessor.addError(ERROR_CODE_INVALID_NUMERIC_FORMAT); } else if (either.getRight().isPresent()) { valueConsumer.accept(either.getRight().get()); } } }
@Override public BigDecimal calculateProductCostForGivenQuantity(final Entity product, final BigDecimal quantity, final String calculateMaterialCostsMode) { BigDecimal cost = BigDecimalUtils.convertNullToZero(product.getField(ProductsCostFields.forMode( calculateMaterialCostsMode).getStrValue())); BigDecimal costForNumber = BigDecimalUtils.convertNullToOne(product.getDecimalField("costForNumber")); if (BigDecimalUtils.valueEquals(costForNumber, BigDecimal.ZERO)) { costForNumber = BigDecimal.ONE; } BigDecimal costPerUnit = cost.divide(costForNumber, numberService.getMathContext()); return costPerUnit.multiply(quantity, numberService.getMathContext()); }
private Map<String, BigDecimal> estimatePieceworkCostCalculationForSingleOperation( final EntityTreeNode calculationOperationComponent, final BigDecimal productionCostMargin, final BigDecimal operationRuns) { Map<String, BigDecimal> costs = Maps.newHashMap(); BigDecimal pieceworkCost = BigDecimalUtils.convertNullToZero(calculationOperationComponent .getDecimalField(CalculationOperationComponentFields.PIECEWORK_COST)); BigDecimal numberOfOperations = BigDecimalUtils.convertNullToOne(calculationOperationComponent .getField(CalculationOperationComponentFields.NUMBER_OF_OPERATIONS)); BigDecimal pieceworkCostPerOperation = pieceworkCost.divide(numberOfOperations, numberService.getMathContext()); BigDecimal operationCost = operationRuns.multiply(pieceworkCostPerOperation, numberService.getMathContext()); BigDecimal operationMarginCost = operationCost.multiply(productionCostMargin.divide(BigDecimal.valueOf(100), numberService.getMathContext())); BigDecimal totalOperationCost = numberService.setScaleWithDefaultMathContext(operationCost.add(operationMarginCost, numberService.getMathContext())); costs.put(L_OPERATION_COST, numberService.setScaleWithDefaultMathContext(operationCost)); costs.put(L_OPERATION_MARGIN_COST, numberService.setScaleWithDefaultMathContext(operationMarginCost)); costs.put(L_PIECES, numberService.setScaleWithDefaultMathContext(operationRuns)); costs.put(L_TOTAL_OPERATION_COST, totalOperationCost); return costs; }
private BigDecimal zeroToOne(final BigDecimal bigDecimal) { if (BigDecimalUtils.valueEquals(bigDecimal, BigDecimal.ZERO)) { return BigDecimal.ONE; } return bigDecimal; }
public void quantityChange(final ViewDefinitionState view, final ComponentState state, final String[] args) { FormComponent form = (FormComponent) view.getComponentByReference("form"); Entity deliveredProductReservation = form.getEntity(); Entity deliveredProduct = deliveredProductReservation.getBelongsToField(DeliveredProductReservationFields.DELIVERED_PRODUCT); BigDecimal conversion = deliveredProduct.getDecimalField(DeliveredProductFields.CONVERSION); Object deliveredQuantityRawValue = deliveredProductReservation.getField(DeliveredProductFields.DELIVERED_QUANTITY); Either<Exception, Optional<BigDecimal>> tryParseDeliveredQuantity = BigDecimalUtils.tryParseAndIgnoreSeparator(deliveredQuantityRawValue == null ? "" : deliveredQuantityRawValue.toString(), LocaleContextHolder.getLocale()); if (conversion != null && tryParseDeliveredQuantity.isRight() && tryParseDeliveredQuantity.getRight().isPresent()) { BigDecimal deliveredQuantity = tryParseDeliveredQuantity.getRight().get(); FieldComponent additionalQuantity = (FieldComponent) view.getComponentByReference(DeliveredProductReservationFields.ADDITIONAL_QUANTITY); BigDecimal newAdditionalQuantity = deliveredQuantity.multiply(conversion, numberService.getMathContext()); newAdditionalQuantity = newAdditionalQuantity.setScale(NumberService.DEFAULT_MAX_FRACTION_DIGITS_IN_DECIMAL, RoundingMode.HALF_UP); additionalQuantity.setFieldValue(numberService.formatWithMinimumFractionDigits(newAdditionalQuantity, 0)); additionalQuantity.requestComponentUpdateState(); } }
public BigDecimal getDifference() { if (planned == null || registered == null) { return null; } if (BigDecimalUtils.valueEquals(planned, BigDecimal.ZERO)) { return null; } return registered.subtract(planned); }
public void additionalQuantityChange(final ViewDefinitionState view, final ComponentState state, final String[] args) { FormComponent form = (FormComponent) view.getComponentByReference("form"); Entity deliveredProductReservation = form.getEntity(); Entity deliveredProduct = deliveredProductReservation.getBelongsToField(DeliveredProductReservationFields.DELIVERED_PRODUCT); BigDecimal conversion = deliveredProduct.getDecimalField(DeliveredProductFields.CONVERSION); Object additionalQuantityRawValue = deliveredProductReservation.getField(DeliveredProductReservationFields.ADDITIONAL_QUANTITY); Either<Exception, Optional<BigDecimal>> tryParseAdditionalQuantity = BigDecimalUtils.tryParseAndIgnoreSeparator(additionalQuantityRawValue == null ? "" : additionalQuantityRawValue.toString(), LocaleContextHolder.getLocale()); if (conversion != null && tryParseAdditionalQuantity.isRight() && tryParseAdditionalQuantity.getRight().isPresent()) { BigDecimal additionalQuantity = tryParseAdditionalQuantity.getRight().get(); FieldComponent deliveredQuantity = (FieldComponent) view.getComponentByReference(DeliveredProductReservationFields.DELIVERED_QUANTITY); BigDecimal newDeliveredQuantity = additionalQuantity.divide(conversion, numberService.getMathContext()); newDeliveredQuantity = newDeliveredQuantity.setScale(NumberService.DEFAULT_MAX_FRACTION_DIGITS_IN_DECIMAL, RoundingMode.HALF_UP); deliveredQuantity.setFieldValue(numberService.formatWithMinimumFractionDigits(newDeliveredQuantity, 0)); deliveredQuantity.requestComponentUpdateState(); } }
public BigDecimal getLaborCostMargin(final BigDecimal laborCost, final BigDecimal margin) { return laborCost.multiply(BigDecimalUtils.convertNullToZero(margin).divide(new BigDecimal(100l)), numberService.getMathContext()); }
public void setCommissionedPlannedQuantity(final DataDefinition orderDD, final Entity order) { if (order == null) { return; } Object quantity = order.getField(OrderFields.PLANNED_QUANTITY); if (quantity != null) { if (BigDecimalUtils.tryParse(quantity.toString(), LocaleContextHolder.getLocale()).isRight()) { order.setField(OrderFields.COMMISSIONED_PLANNED_QUANTITY, numberService.setScaleWithDefaultMathContext(order.getDecimalField(OrderFields.PLANNED_QUANTITY))); } } }
private boolean hasPlannedQuantityChanged(final Entity order, final BigDecimal plannedQuantity) { Entity existingOrder = getExistingOrder(order); if (existingOrder == null) { return false; } BigDecimal existingOrderPlannedQuantity = existingOrder.getDecimalField(OrderFields.PLANNED_QUANTITY); if (existingOrderPlannedQuantity == null) { return true; } return !BigDecimalUtils.valueEquals(existingOrderPlannedQuantity, plannedQuantity); }
public void additionalQuantityChange(final ViewDefinitionState view, final ComponentState state, final String[] args) { FormComponent form = (FormComponent) view.getComponentByReference("form"); Entity orderedProductReservation = form.getEntity(); Entity orderedProduct = orderedProductReservation.getBelongsToField(OrderedProductReservationFields.ORDERED_PRODUCT); BigDecimal conversion = orderedProduct.getDecimalField(OrderedProductFields.CONVERSION); Object additionalQuantityRawValue = orderedProductReservation .getField(OrderedProductReservationFields.ADDITIONAL_QUANTITY); Either<Exception, Optional<BigDecimal>> tryParseAdditionalQuantity = BigDecimalUtils.tryParseAndIgnoreSeparator( additionalQuantityRawValue == null ? "" : additionalQuantityRawValue.toString(), LocaleContextHolder.getLocale()); if (conversion != null && tryParseAdditionalQuantity.isRight() && tryParseAdditionalQuantity.getRight().isPresent()) { BigDecimal additionalQuantity = tryParseAdditionalQuantity.getRight().get(); FieldComponent orderedQuantity = (FieldComponent) view .getComponentByReference(OrderedProductReservationFields.ORDERED_QUANTITY); BigDecimal newOrderedQuantity = calculationQuantityService.calculateQuantity(additionalQuantity, conversion, orderedProduct.getBelongsToField(OrderedProductFields.PRODUCT).getStringField(ProductFields.UNIT)); orderedQuantity.setFieldValue(numberService.formatWithMinimumFractionDigits(newOrderedQuantity, 0)); orderedQuantity.requestComponentUpdateState(); } }
public BigDecimal getMaterialCostMargin(final BigDecimal materialCost, final BigDecimal margin) { return materialCost.multiply(BigDecimalUtils.convertNullToZero(margin).divide(new BigDecimal(100l)), numberService.getMathContext()); }
private boolean transferQuantityIsNonEmptyDecimalValue(final Entity transfer) { Either<Exception, Optional<BigDecimal>> quantity = BigDecimalUtils.tryParse( ObjectUtils.toString(transfer.getField(TransferFields.QUANTITY)), LocaleContextHolder.getLocale()); if (quantity.isLeft()) { appendErrorToModelField(transfer, QUANTITY, "qcadooView.validate.field.error.invalidNumericFormat"); return false; } if (!quantity.getRight().isPresent()) { appendErrorToModelField(transfer, QUANTITY, "materialFlow.validate.global.error.fillRequired"); return false; } return true; }
public boolean validatePlannedQuantity(final DataDefinition productionCountingQuantityDD, final FieldDefinition plannedQuantityFieldDefinition, final Entity productionCountingQuantity, final Object oldValue, final Object newValue) { // I don't check if entity is updated or created (check null on id) because we should disallow also creating // of new ones if editing production progresses for accepted orders is locked. if (BigDecimalUtils.valueEquals((BigDecimal) oldValue, (BigDecimal) newValue)) { return true; } if (progressModifyLockHelper.isLocked(productionCountingQuantity .getBelongsToField(ProductionCountingQuantityFields.ORDER))) { productionCountingQuantity .addError(plannedQuantityFieldDefinition, "basicProductionCounting.productionCountingQuantity.plannedQuantity.error.valueChangeIsNotAllowedForAcceptetOrder"); return false; } return true; }
public void quantityChange(final ViewDefinitionState view, final ComponentState state, final String[] args) { FormComponent form = (FormComponent) view.getComponentByReference("form"); Entity orderedProductReservation = form.getEntity(); Entity orderedProduct = orderedProductReservation.getBelongsToField(OrderedProductReservationFields.ORDERED_PRODUCT); BigDecimal conversion = orderedProduct.getDecimalField(OrderedProductFields.CONVERSION); Object orderedQuantityRawValue = orderedProductReservation.getField(OrderedProductReservationFields.ORDERED_QUANTITY); Either<Exception, Optional<BigDecimal>> tryParseOrderedQuantity = BigDecimalUtils.tryParseAndIgnoreSeparator( orderedQuantityRawValue == null ? "" : orderedQuantityRawValue.toString(), LocaleContextHolder.getLocale()); if (conversion != null && tryParseOrderedQuantity.isRight() && tryParseOrderedQuantity.getRight().isPresent()) { BigDecimal orderedQuantity = tryParseOrderedQuantity.getRight().get(); FieldComponent additionalQuantity = (FieldComponent) view .getComponentByReference(OrderedProductReservationFields.ADDITIONAL_QUANTITY); BigDecimal newAdditionalQuantity = calculationQuantityService.calculateAdditionalQuantity(orderedQuantity, conversion, orderedProduct.getBelongsToField(OrderedProductFields.PRODUCT).getStringField(ProductFields.ADDITIONAL_UNIT)); additionalQuantity.setFieldValue(numberService.formatWithMinimumFractionDigits(newAdditionalQuantity, 0)); additionalQuantity.requestComponentUpdateState(); } }
public static BigDecimal conversion(Entity position) { return convertNullToZero(position.getDecimalField(PositionFields.CONVERSION)); }
public void fillQuantitiesInAdditionalUnit(final ViewDefinitionState view) { AwesomeDynamicListComponent adl = (AwesomeDynamicListComponent) view.getComponentByReference("corrections"); for (FormComponent form : adl.getFormComponents()) { Entity correction = form.getPersistedEntityWithIncludedFormValues(); FieldComponent quantityField = form.findFieldComponentByName(ProductToIssueCorrectionFields.CORRECTION_QUANTITY); Either<Exception, com.google.common.base.Optional<BigDecimal>> maybeQuantity = BigDecimalUtils.tryParse(quantityField.getFieldValue() .toString(), LocaleContextHolder.getLocale()); BigDecimal quantityAdditionalUnit = BigDecimal.ZERO; BigDecimal conversion = correction.getDecimalField(IssueFields.CONVERSION); if (conversion != null && maybeQuantity.isRight() && maybeQuantity.getRight().isPresent()) { BigDecimal correctionQuantity = maybeQuantity.getRight().get(); quantityAdditionalUnit = numberService.setScaleWithDefaultMathContext(correctionQuantity.multiply(conversion, numberService.getMathContext())); correction.setField(ProductToIssueCorrectionFields.CORRECTION_QUANTITY_IN_ADDITIONAL_UNIT, quantityAdditionalUnit); form.setEntity(correction); } } }
public void onConversionChange(final ViewDefinitionState viewDefinitionState, final ComponentState state, final String[] args) { FieldComponent conversionField = (FieldComponent) viewDefinitionState.getComponentByReference(ResourceFields.CONVERSION); FieldComponent quantityInAdditionalUnitField = (FieldComponent) viewDefinitionState .getComponentByReference(ResourceFields.QUANTITY_IN_ADDITIONAL_UNIT); FieldComponent additionalUnitField = (FieldComponent) viewDefinitionState .getComponentByReference(ResourceFields.GIVEN_UNIT); Either<Exception, Optional<BigDecimal>> maybeConversion = BigDecimalUtils.tryParseAndIgnoreSeparator( (String) conversionField.getFieldValue(), viewDefinitionState.getLocale()); if (maybeConversion.isRight() && maybeConversion.getRight().isPresent()) { FormComponent form = (FormComponent) viewDefinitionState.getComponentByReference(L_FORM); Entity resource = form.getPersistedEntityWithIncludedFormValues(); BigDecimal newAdditionalQuantity = calculationQuantityService.calculateAdditionalQuantity( resource.getDecimalField(ResourceFields.QUANTITY), maybeConversion.getRight().get(), (String) additionalUnitField.getFieldValue()); String quantityInAdditionalUnitFormatted = numberService.format(newAdditionalQuantity); quantityInAdditionalUnitField.setFieldValue(quantityInAdditionalUnitFormatted); } else { quantityInAdditionalUnitField.setFieldValue(null); } }
public static String amountAndRest(Entity position) { BigDecimal amount = position.getDecimalField(PositionFields.QUANTITY); amount = amount.setScale(0, RoundingMode.DOWN); BigDecimal wholeAmount = amount.multiply(convertNullToZero(position.getDecimalField(PositionFields.CONVERSION))); BigDecimal rest = convertNullToZero(position.getDecimalField(PositionFields.GIVEN_QUANTITY)).subtract(wholeAmount); rest = rest.setScale(5, RoundingMode.HALF_UP); return amount.stripTrailingZeros().toPlainString() + "\n" + rest.stripTrailingZeros().toPlainString(); }