Search in sources :

Example 1 with PurchaseProfitInfo

use of de.metas.purchasecandidate.grossprofit.PurchaseProfitInfo in project metasfresh-webui-api by metasfresh.

the class PurchaseRowsSaver method createOrUpdatePurchaseCandidate.

private List<PurchaseCandidate> createOrUpdatePurchaseCandidate(@NonNull final PurchaseCandidatesGroup candidatesGroup, @NonNull final Map<PurchaseCandidateId, PurchaseCandidate> existingPurchaseCandidatesById) {
    Quantity qtyToPurchaseRemainingOfGroup = candidatesGroup.getQtyToPurchase();
    if (qtyToPurchaseRemainingOfGroup.signum() <= 0) {
        return ImmutableList.of();
    }
    final PurchaseProfitInfo profitInfo = candidatesGroup.getProfitInfoOrNull();
    final ZonedDateTime purchaseDatePromised = candidatesGroup.getPurchaseDatePromised();
    final Duration reminderTime = candidatesGroup.getReminderTime();
    final List<PurchaseCandidate> allCandidates = getPurchaseCandidates(candidatesGroup, existingPurchaseCandidatesById);
    // 
    // Adjust qtyToPurchaseRemaining: Subtract the qtyToPurchase which was already processed
    {
        final Optional<Quantity> qtyToPurchaseProcessed = computeQtyToPurchaseAlreadyProcessed(allCandidates);
        if (qtyToPurchaseProcessed.isPresent()) {
            qtyToPurchaseRemainingOfGroup = qtyToPurchaseRemainingOfGroup.subtract(qtyToPurchaseProcessed.get());
        }
        if (qtyToPurchaseRemainingOfGroup.signum() < 0) {
            // TODO: throw exception?
            return ImmutableList.of();
        } else if (qtyToPurchaseRemainingOfGroup.signum() == 0) {
            return ImmutableList.of();
        }
    }
    // 
    // Extract all updatable candidates
    final ArrayList<PurchaseCandidate> candidatesToUpdate = allCandidates.stream().filter(candidate -> !candidate.isProcessedOrLocked()).collect(Collectors.toCollection(ArrayList::new));
    final ArrayList<PurchaseCandidate> candidatesChanged = new ArrayList<>();
    // Distribute qtyToPurchase to updatable purchase candidates (FIFO order)
    while (qtyToPurchaseRemainingOfGroup.signum() > 0 && !candidatesToUpdate.isEmpty()) {
        final PurchaseCandidate candidate = candidatesToUpdate.remove(0);
        final Quantity qtyToPurchaseTarget = getQtyToPurchaseTarget(candidate);
        final Quantity qtyToPurchase = qtyToPurchaseTarget.min(qtyToPurchaseRemainingOfGroup);
        candidate.setQtyToPurchase(qtyToPurchase);
        candidate.setPrepared(qtyToPurchase.signum() != 0);
        candidate.setPurchaseDatePromised(purchaseDatePromised);
        candidate.setProfitInfoOrNull(profitInfo);
        candidatesChanged.add(candidate);
        qtyToPurchaseRemainingOfGroup = qtyToPurchaseRemainingOfGroup.subtract(qtyToPurchase);
    }
    // If there is no remaining qty to purchase then ZERO all the remaining purchase candidates lines
    if (qtyToPurchaseRemainingOfGroup.signum() <= 0) {
        while (!candidatesToUpdate.isEmpty()) {
            final PurchaseCandidate candidate = candidatesToUpdate.remove(0);
            candidate.setQtyToPurchase(candidate.getQtyToPurchase().toZero());
            candidatesChanged.add(candidate);
        }
    } else // If there is remaining qty to purchase then add it to last changed purchase candidate line
    if (!candidatesToUpdate.isEmpty()) {
        final PurchaseCandidate lastCandidate = candidatesToUpdate.get(candidatesToUpdate.size() - 1);
        lastCandidate.setQtyToPurchase(lastCandidate.getQtyToPurchase().add(qtyToPurchaseRemainingOfGroup));
        lastCandidate.setPurchaseDatePromised(purchaseDatePromised);
        qtyToPurchaseRemainingOfGroup = qtyToPurchaseRemainingOfGroup.toZero();
    } else // 
    // If there is remaining qty to purchase but no purchase candidate to add to then create a new candidate
    {
        final DemandGroupReference groupReference;
        if (candidatesGroup.getDemandGroupReferences().isEmpty()) {
            groupReference = DemandGroupReference.EMPTY;
        } else {
            groupReference = candidatesGroup.getDemandGroupReferences().get(0);
        }
        final PurchaseCandidate newCandidate = PurchaseCandidate.builder().groupReference(groupReference).salesOrderAndLineIdOrNull(candidatesGroup.getSingleSalesOrderAndLineIdOrNull()).purchaseDatePromised(purchaseDatePromised).reminderTime(reminderTime).orgId(candidatesGroup.getOrgId()).warehouseId(candidatesGroup.getWarehouseId()).vendorId(candidatesGroup.getVendorId()).vendorProductNo(candidatesGroup.getVendorProductNo()).productId(candidatesGroup.getProductId()).attributeSetInstanceId(candidatesGroup.getAttributeSetInstanceId()).qtyToPurchase(qtyToPurchaseRemainingOfGroup).prepared(true).aggregatePOs(candidatesGroup.isAggregatePOs()).profitInfoOrNull(profitInfo).build();
        candidatesChanged.add(newCandidate);
        qtyToPurchaseRemainingOfGroup = qtyToPurchaseRemainingOfGroup.toZero();
    }
    return candidatesChanged;
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Function(java.util.function.Function) OrderAndLineId(de.metas.order.OrderAndLineId) ArrayList(java.util.ArrayList) IOrderLineBL(de.metas.order.IOrderLineBL) ImmutableList(com.google.common.collect.ImmutableList) PurchaseCandidateRepository(de.metas.purchasecandidate.PurchaseCandidateRepository) Quantity(de.metas.quantity.Quantity) Duration(java.time.Duration) Map(java.util.Map) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) NonNull(lombok.NonNull) Collection(java.util.Collection) PurchaseCandidate(de.metas.purchasecandidate.PurchaseCandidate) Set(java.util.Set) PurchaseCandidateId(de.metas.purchasecandidate.PurchaseCandidateId) Collectors(java.util.stream.Collectors) Services(de.metas.util.Services) Objects(java.util.Objects) List(java.util.List) Builder(lombok.Builder) DemandGroupReference(de.metas.purchasecandidate.DemandGroupReference) PurchaseProfitInfo(de.metas.purchasecandidate.grossprofit.PurchaseProfitInfo) Optional(java.util.Optional) Comparator(java.util.Comparator) PurchaseCandidatesGroup(de.metas.purchasecandidate.PurchaseCandidatesGroup) Optional(java.util.Optional) ZonedDateTime(java.time.ZonedDateTime) ArrayList(java.util.ArrayList) Quantity(de.metas.quantity.Quantity) Duration(java.time.Duration) PurchaseCandidate(de.metas.purchasecandidate.PurchaseCandidate) DemandGroupReference(de.metas.purchasecandidate.DemandGroupReference) PurchaseProfitInfo(de.metas.purchasecandidate.grossprofit.PurchaseProfitInfo)

Example 2 with PurchaseProfitInfo

use of de.metas.purchasecandidate.grossprofit.PurchaseProfitInfo in project metasfresh-webui-api by metasfresh.

the class PurchaseRowsLoaderTest method createPurchaseCandidate.

private static PurchaseCandidate createPurchaseCandidate(final I_C_OrderLine orderLine, final VendorProductInfo vendorProductInfo) {
    final CurrencyId currencyId = CurrencyId.ofRepoId(orderLine.getC_Currency_ID());
    final PurchaseProfitInfo profitInfo = PurchaseRowTestTools.createProfitInfo(currencyId);
    final ProductId productId = ProductId.ofRepoId(orderLine.getM_Product_ID());
    final I_C_UOM productStockingUOM = Services.get(IProductBL.class).getStockUOM(productId);
    final PurchaseCandidate purchaseCandidate = PurchaseCandidate.builder().groupReference(DemandGroupReference.EMPTY).orgId(OrgId.ofRepoId(20)).purchaseDatePromised(TimeUtil.asZonedDateTime(orderLine.getDatePromised())).productId(productId).attributeSetInstanceId(AttributeSetInstanceId.ofRepoId(orderLine.getM_AttributeSetInstance_ID())).qtyToPurchase(Quantity.of(orderLine.getQtyOrdered(), productStockingUOM)).salesOrderAndLineIdOrNull(OrderAndLineId.ofRepoIds(orderLine.getC_Order_ID(), orderLine.getC_OrderLine_ID())).vendorId(vendorProductInfo.getVendorId()).vendorProductNo(vendorProductInfo.getVendorProductNo()).aggregatePOs(vendorProductInfo.isAggregatePOs()).warehouseId(WarehouseId.ofRepoId(30)).profitInfoOrNull(profitInfo).build();
    return purchaseCandidate;
}
Also used : IProductBL(de.metas.product.IProductBL) ProductId(de.metas.product.ProductId) PurchaseCandidate(de.metas.purchasecandidate.PurchaseCandidate) CurrencyId(de.metas.money.CurrencyId) I_C_UOM(org.compiere.model.I_C_UOM) PurchaseProfitInfo(de.metas.purchasecandidate.grossprofit.PurchaseProfitInfo)

Example 3 with PurchaseProfitInfo

use of de.metas.purchasecandidate.grossprofit.PurchaseProfitInfo in project metasfresh-webui-api by metasfresh.

the class PurchaseRow method changeRow.

private void changeRow(@NonNull final PurchaseRowChangeRequest request) {
    assertRowType(PurchaseRowType.LINE);
    assertRowEditable();
    // 
    final PurchaseCandidatesGroup candidatesGroup = getPurchaseCandidatesGroup();
    final PurchaseCandidatesGroupBuilder newCandidatesGroup = candidatesGroup.toBuilder();
    boolean hasChanges = false;
    // 
    // QtyToPurchase
    final Quantity qtyToPurchase = request.getQtyToPurchase(this::getCurrentUOM);
    boolean qtyToPurchaseChanged = false;
    if (qtyToPurchase != null && !Objects.equals(candidatesGroup.getQtyToPurchase(), qtyToPurchase)) {
        newCandidatesGroup.qtyToPurchase(qtyToPurchase);
        qtyToPurchaseChanged = true;
        hasChanges = true;
    }
    // 
    // PurchaseDatePromised
    final ZonedDateTime purchaseDatePromised = request.getPurchaseDatePromised();
    if (purchaseDatePromised != null) {
        newCandidatesGroup.purchaseDatePromised(purchaseDatePromised);
        hasChanges = true;
    }
    // Recompute Profit Info
    if (qtyToPurchaseChanged) {
        final PurchaseProfitInfo profitInfo = purchaseProfitInfoService.calculateNoFail(PurchaseProfitInfoRequest.builder().salesOrderAndLineIds(candidatesGroup.getSalesOrderAndLineIds()).qtyToPurchase(qtyToPurchase).vendorProductInfo(candidatesGroup.getVendorProductInfo()).build());
        newCandidatesGroup.profitInfoOrNull(profitInfo);
        hasChanges = true;
    }
    // Stop here if there were no changes
    if (!hasChanges) {
        return;
    }
    // 
    setPurchaseCandidatesGroup(newCandidatesGroup.build());
}
Also used : ZonedDateTime(java.time.ZonedDateTime) PurchaseCandidatesGroupBuilder(de.metas.purchasecandidate.PurchaseCandidatesGroup.PurchaseCandidatesGroupBuilder) PurchaseCandidatesGroup(de.metas.purchasecandidate.PurchaseCandidatesGroup) Quantity(de.metas.quantity.Quantity) PurchaseProfitInfo(de.metas.purchasecandidate.grossprofit.PurchaseProfitInfo)

Aggregations

PurchaseProfitInfo (de.metas.purchasecandidate.grossprofit.PurchaseProfitInfo)3 PurchaseCandidate (de.metas.purchasecandidate.PurchaseCandidate)2 PurchaseCandidatesGroup (de.metas.purchasecandidate.PurchaseCandidatesGroup)2 Quantity (de.metas.quantity.Quantity)2 ZonedDateTime (java.time.ZonedDateTime)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 CurrencyId (de.metas.money.CurrencyId)1 IOrderLineBL (de.metas.order.IOrderLineBL)1 OrderAndLineId (de.metas.order.OrderAndLineId)1 IProductBL (de.metas.product.IProductBL)1 ProductId (de.metas.product.ProductId)1 DemandGroupReference (de.metas.purchasecandidate.DemandGroupReference)1 PurchaseCandidateId (de.metas.purchasecandidate.PurchaseCandidateId)1 PurchaseCandidateRepository (de.metas.purchasecandidate.PurchaseCandidateRepository)1 PurchaseCandidatesGroupBuilder (de.metas.purchasecandidate.PurchaseCandidatesGroup.PurchaseCandidatesGroupBuilder)1 Services (de.metas.util.Services)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1