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;
}
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;
}
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());
}
Aggregations