Search in sources :

Example 1 with PriceSpecification

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

the class PricingConditionsView_CopyRowToEditable method createChangeRequest.

private PricingConditionsRowChangeRequest createChangeRequest(@NonNull final PricingConditionsRow templateRow) {
    final PricingConditionsBreak templatePricingConditionsBreak = templateRow.getPricingConditionsBreak();
    PriceSpecification price = templatePricingConditionsBreak.getPriceSpecification();
    if (price.isNoPrice()) {
        // In case row does not have a price then use BPartner's pricing system
        final BPartnerId bpartnerId = templateRow.getBpartnerId();
        final SOTrx soTrx = SOTrx.ofBoolean(templateRow.isCustomer());
        price = createBasePricingSystemPrice(bpartnerId, soTrx);
    }
    final Percent discount = templatePricingConditionsBreak.getDiscount();
    final PaymentTermId paymentTermIdOrNull = templatePricingConditionsBreak.getPaymentTermIdOrNull();
    final Percent paymentDiscountOverrideOrNull = templatePricingConditionsBreak.getPaymentDiscountOverrideOrNull();
    return PricingConditionsRowChangeRequest.builder().priceChange(CompletePriceChange.of(price)).discount(discount).paymentTermId(Optional.ofNullable(paymentTermIdOrNull)).paymentDiscount(Optional.ofNullable(paymentDiscountOverrideOrNull)).sourcePricingConditionsBreakId(templatePricingConditionsBreak.getId()).build();
}
Also used : PricingConditionsBreak(de.metas.pricing.conditions.PricingConditionsBreak) PaymentTermId(de.metas.payment.paymentterm.PaymentTermId) Percent(de.metas.util.lang.Percent) SOTrx(de.metas.lang.SOTrx) PriceSpecification(de.metas.pricing.conditions.PriceSpecification) BPartnerId(de.metas.bpartner.BPartnerId)

Example 2 with PriceSpecification

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

the class PricingConditionsView method updateOrderLineRecord.

private void updateOrderLineRecord(@NonNull final PricingConditionsBreak pricingConditionsBreak, @Nullable final Money basePrice) {
    final IOrderDAO ordersRepo = Services.get(IOrderDAO.class);
    final IOrderLineBL orderLineBL = Services.get(IOrderLineBL.class);
    final I_C_OrderLine orderLineRecord = ordersRepo.getOrderLineById(getOrderLineId());
    orderLineRecord.setIsTempPricingConditions(pricingConditionsBreak.isTemporaryPricingConditionsBreak());
    if (pricingConditionsBreak.isTemporaryPricingConditionsBreak()) {
        orderLineRecord.setM_DiscountSchema_ID(-1);
        orderLineRecord.setM_DiscountSchemaBreak_ID(-1);
        final PriceSpecification price = pricingConditionsBreak.getPriceSpecification();
        final PriceSpecificationType type = price.getType();
        if (type == PriceSpecificationType.NONE) {
        // 
        } else if (type == PriceSpecificationType.BASE_PRICING_SYSTEM) {
            assumeNotNull(basePrice, "If type={}, then the given basePrice may not be null; pricingConditionsBreak={}", type, pricingConditionsBreak);
            final BigDecimal priceEntered = limitPrice(basePrice.toBigDecimal(), orderLineRecord);
            orderLineRecord.setIsManualPrice(true);
            orderLineRecord.setPriceEntered(priceEntered);
            orderLineRecord.setC_Currency_ID(basePrice.getCurrencyId().getRepoId());
            orderLineRecord.setBase_PricingSystem_ID(price.getBasePricingSystemId().getRepoId());
        } else if (type == PriceSpecificationType.FIXED_PRICE) {
            final Money fixedPrice = price.getFixedPrice();
            Check.assumeNotNull(fixedPrice, "fixedPrice shall not be null for {}", price);
            final BigDecimal priceEntered = limitPrice(fixedPrice.toBigDecimal(), orderLineRecord);
            orderLineRecord.setIsManualPrice(true);
            orderLineRecord.setPriceEntered(priceEntered);
            orderLineRecord.setC_Currency_ID(fixedPrice.getCurrencyId().getRepoId());
            orderLineRecord.setBase_PricingSystem_ID(-1);
        }
        orderLineRecord.setIsManualDiscount(true);
        orderLineRecord.setDiscount(pricingConditionsBreak.getDiscount().toBigDecimal());
        // make sure it's not overwritten by whatever the system comes up with when we save the orderLine.
        orderLineRecord.setIsManualPaymentTerm(true);
        orderLineRecord.setC_PaymentTerm_Override_ID(PaymentTermId.toRepoId(pricingConditionsBreak.getDerivedPaymentTermIdOrNull()));
        orderLineRecord.setPaymentDiscount(Percent.toBigDecimalOrNull(pricingConditionsBreak.getPaymentDiscountOverrideOrNull()));
        // 
        // PriceActual & Discount
        Percent discountEffective = pricingConditionsBreak.getDiscount();
        final BigDecimal priceActual = discountEffective.subtractFromBase(orderLineRecord.getPriceEntered(), CurrencyPrecision.TWO.toInt());
        final BigDecimal priceActualEffective = limitPrice(priceActual, orderLineRecord);
        if (priceActualEffective.compareTo(priceActual) != 0) {
            discountEffective = PriceAndDiscount.calculateDiscountFromPrices(orderLineRecord.getPriceEntered(), priceActualEffective, CurrencyPrecision.TWO);
        }
        // 
        orderLineRecord.setIsManualDiscount(true);
        orderLineRecord.setDiscount(discountEffective.toBigDecimal());
        orderLineRecord.setPriceActual(priceActualEffective);
    } else {
        final PricingConditionsBreakId pricingConditionsBreakId = pricingConditionsBreak.getId();
        orderLineRecord.setM_DiscountSchema_ID(pricingConditionsBreakId.getDiscountSchemaId());
        orderLineRecord.setM_DiscountSchemaBreak_ID(pricingConditionsBreakId.getDiscountSchemaBreakId());
        orderLineRecord.setIsManualDiscount(false);
        orderLineRecord.setIsManualPrice(false);
        orderLineRecord.setIsManualPaymentTerm(false);
        final OrderLinePriceUpdateRequest orderLinePriceUpdateRequest = OrderLinePriceUpdateRequest.prepare(orderLineRecord).pricingConditionsBreakOverride(pricingConditionsBreak).build();
        orderLineBL.updatePrices(orderLinePriceUpdateRequest);
    }
    orderLineBL.updateLineNetAmtFromQtyEntered(orderLineRecord);
    orderLineBL.setTaxAmtInfo(orderLineRecord);
    InterfaceWrapperHelper.save(orderLineRecord);
}
Also used : IOrderLineBL(de.metas.order.IOrderLineBL) PricingConditionsBreakId(de.metas.pricing.conditions.PricingConditionsBreakId) Money(de.metas.money.Money) IOrderDAO(de.metas.order.IOrderDAO) OrderLinePriceUpdateRequest(de.metas.order.OrderLinePriceUpdateRequest) Percent(de.metas.util.lang.Percent) PriceSpecificationType(de.metas.pricing.conditions.PriceSpecificationType) PriceSpecification(de.metas.pricing.conditions.PriceSpecification) I_C_OrderLine(de.metas.interfaces.I_C_OrderLine) BigDecimal(java.math.BigDecimal)

Example 3 with PriceSpecification

use of de.metas.pricing.conditions.PriceSpecification 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

PriceSpecification (de.metas.pricing.conditions.PriceSpecification)3 Percent (de.metas.util.lang.Percent)3 PaymentTermId (de.metas.payment.paymentterm.PaymentTermId)2 PricingConditionsBreak (de.metas.pricing.conditions.PricingConditionsBreak)2 BPartnerId (de.metas.bpartner.BPartnerId)1 I_C_OrderLine (de.metas.interfaces.I_C_OrderLine)1 SOTrx (de.metas.lang.SOTrx)1 Money (de.metas.money.Money)1 IOrderDAO (de.metas.order.IOrderDAO)1 IOrderLineBL (de.metas.order.IOrderLineBL)1 OrderLinePriceUpdateRequest (de.metas.order.OrderLinePriceUpdateRequest)1 PaymentTermService (de.metas.payment.paymentterm.PaymentTermService)1 PriceSpecificationType (de.metas.pricing.conditions.PriceSpecificationType)1 PricingConditionsBreakBuilder (de.metas.pricing.conditions.PricingConditionsBreak.PricingConditionsBreakBuilder)1 PricingConditionsBreakId (de.metas.pricing.conditions.PricingConditionsBreakId)1 BigDecimal (java.math.BigDecimal)1