Search in sources :

Example 1 with PricingConditionsBreakBuilder

use of de.metas.pricing.conditions.PricingConditionsBreak.PricingConditionsBreakBuilder in project metasfresh-webui-api by metasfresh.

the class PricingConditionsRowReducers method applyRequestTo.

private PricingConditionsBreak applyRequestTo(@NonNull final PricingConditionsRowChangeRequest request, @NonNull final PricingConditionsBreak rowPricingConditionsBreak) {
    final PricingConditionsBreak pricingConditionsBreakEffective = coalesce(request.getPricingConditionsBreak(), rowPricingConditionsBreak);
    final PricingConditionsBreakBuilder builder = pricingConditionsBreakEffective.toBuilder();
    // Discount%
    if (request.getDiscount() != null) {
        builder.discount(request.getDiscount());
        builder.hasChanges(true);
    }
    // 
    // Payment Term
    final boolean paymentTermChangeRequested = request.getPaymentTermId() != null;
    final boolean paymentDiscountChangeRequested = request.getPaymentDiscount() != null;
    if (paymentTermChangeRequested || paymentDiscountChangeRequested) {
        final PaymentTermId paymentTermIdOrNull;
        if (paymentTermChangeRequested) {
            paymentTermIdOrNull = request.getPaymentTermId().orElse(null);
            builder.paymentTermIdOrNull(paymentTermIdOrNull);
        } else {
            // if no payment term change was requested, we use the old paymentTermId to get derivedPaymentTermIdOrNull.
            paymentTermIdOrNull = pricingConditionsBreakEffective.getPaymentTermIdOrNull();
        }
        final Percent paymentDiscountOrNull = paymentDiscountChangeRequested ? request.getPaymentDiscount().orElse(null) : null;
        builder.paymentDiscountOverrideOrNull(paymentDiscountOrNull);
        final PaymentTermService paymentTermService = SpringContextHolder.instance.getBean(PaymentTermService.class);
        final PaymentTermId derivedPaymentTermIdOrNull = paymentTermService.getOrCreateDerivedPaymentTerm(paymentTermIdOrNull, paymentDiscountOrNull);
        builder.derivedPaymentTermIdOrNull(derivedPaymentTermIdOrNull);
    }
    // Price
    if (request.getPriceChange() != null) {
        final PriceSpecification price = applyPriceChangeTo(request.getPriceChange(), rowPricingConditionsBreak.getPriceSpecification());
        builder.priceSpecification(price);
    }
    PricingConditionsBreak newPricingConditionsBreak = builder.build();
    if (!Objects.equals(newPricingConditionsBreak, pricingConditionsBreakEffective)) {
        newPricingConditionsBreak = newPricingConditionsBreak.toBuilder().hasChanges(true).build();
    }
    return newPricingConditionsBreak;
}
Also used : PricingConditionsBreak(de.metas.pricing.conditions.PricingConditionsBreak) PaymentTermId(de.metas.payment.paymentterm.PaymentTermId) Percent(de.metas.util.lang.Percent) PriceSpecification(de.metas.pricing.conditions.PriceSpecification) PaymentTermService(de.metas.payment.paymentterm.PaymentTermService) PricingConditionsBreakBuilder(de.metas.pricing.conditions.PricingConditionsBreak.PricingConditionsBreakBuilder)

Aggregations

PaymentTermId (de.metas.payment.paymentterm.PaymentTermId)1 PaymentTermService (de.metas.payment.paymentterm.PaymentTermService)1 PriceSpecification (de.metas.pricing.conditions.PriceSpecification)1 PricingConditionsBreak (de.metas.pricing.conditions.PricingConditionsBreak)1 PricingConditionsBreakBuilder (de.metas.pricing.conditions.PricingConditionsBreak.PricingConditionsBreakBuilder)1 Percent (de.metas.util.lang.Percent)1