Skip to content

Commit

Permalink
#208 WEBUI_M_ReceiptSchedule_ReceiveCUs_WithParam
Browse files Browse the repository at this point in the history
also refactored WEBUI_M_ReceiptSchedule_ReceiveCUs
  • Loading branch information
teosarca committed Mar 4, 2017
1 parent 58dec77 commit fef50fc
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Stream;

import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.model.InterfaceWrapperHelper;
Expand Down Expand Up @@ -66,12 +67,25 @@
*
*/
@Profile(WebRestApiApplication.PROFILE_Webui)
public class WEBUI_M_ReceiptSchedule_GeneratePlanningHUs_MultiRow extends JavaProcess implements IProcessPrecondition
public class WEBUI_M_ReceiptSchedule_ReceiveCUs extends JavaProcess implements IProcessPrecondition
{
private final transient IHUReceiptScheduleBL huReceiptScheduleBL = Services.get(IHUReceiptScheduleBL.class);
private final transient IReceiptScheduleBL receiptScheduleBL = Services.get(IReceiptScheduleBL.class);
private final transient IHandlingUnitsDAO handlingUnitsDAO = Services.get(IHandlingUnitsDAO.class);

private boolean allowMultipleReceiptsSchedules = true; // by default we shall allow multiple lines
private boolean allowNoQuantityAvailable = false; // by default we shall not allow lines which have no quantity available

protected final void setAllowMultipleReceiptsSchedules(final boolean allowMultipleReceiptsSchedules)
{
this.allowMultipleReceiptsSchedules = allowMultipleReceiptsSchedules;
}

protected final void setAllowNoQuantityAvailable(final boolean allowNoQuantityAvailable)
{
this.allowNoQuantityAvailable = allowNoQuantityAvailable;
}

@Override
public ProcessPreconditionsResolution checkPreconditionsApplicable(final IProcessPreconditionsContext context)
{
Expand All @@ -86,21 +100,28 @@ public ProcessPreconditionsResolution checkPreconditionsApplicable(final IProces
// return ProcessPreconditionsResolution.rejectWithInternalReason("select more than one row");
// }

//
// Check if we are allowed to select multiple lines
if (!allowMultipleReceiptsSchedules && context.getSelectionSize() > 1)
{
return ProcessPreconditionsResolution.rejectWithInternalReason("select only one line");
}

//
// Fetch the receipt schedules which have some qty available for receiving
final List<I_M_ReceiptSchedule> receiptSchedules = context.getSelectedModels(I_M_ReceiptSchedule.class)
.stream()
.filter(receiptSchedule -> getAvailableQtyToReceive(receiptSchedule).signum() > 0)
.filter(receiptSchedule -> allowNoQuantityAvailable || getDefaultAvailableQtyToReceive(receiptSchedule).signum() > 0)
.collect(ImmutableList.toImmutableList());
if(receiptSchedules.isEmpty())
if (receiptSchedules.isEmpty())
{
return ProcessPreconditionsResolution.reject("nothing to receive");
}

//
// Make sure each of them are eligible for receiving
{
ProcessPreconditionsResolution rejectResolution = receiptSchedules.stream()
final ProcessPreconditionsResolution rejectResolution = receiptSchedules.stream()
.map(receiptSchedule -> WEBUI_M_ReceiptSchedule_GeneratePlanningHUs_Base.checkEligibleForReceivingHUs(receiptSchedule))
.filter(resolution -> !resolution.isAccepted())
.findFirst()
Expand Down Expand Up @@ -130,14 +151,12 @@ public ProcessPreconditionsResolution checkPreconditionsApplicable(final IProces
//
return ProcessPreconditionsResolution.accept();
}

@Override
@RunOutOfTrx
protected String doIt() throws Exception
protected final String doIt() throws Exception
{
final List<I_M_HU> hus = retrieveSelectedRecordsQueryBuilder(I_M_ReceiptSchedule.class)
.create()
.stream(I_M_ReceiptSchedule.class)
final List<I_M_HU> hus = streamReceiptSchedulesToReceive()
.map(this::createPlanningVHU)
.filter(hu -> hu != null)
.collect(GuavaCollectors.toImmutableList());
Expand All @@ -147,6 +166,13 @@ protected String doIt() throws Exception
return MSG_OK;
}

protected Stream<I_M_ReceiptSchedule> streamReceiptSchedulesToReceive()
{
return retrieveSelectedRecordsQueryBuilder(I_M_ReceiptSchedule.class)
.create()
.stream(I_M_ReceiptSchedule.class);
}

private I_M_HU createPlanningVHU(final I_M_ReceiptSchedule receiptSchedule)
{
//
Expand Down Expand Up @@ -188,17 +214,24 @@ private I_M_HU createPlanningVHU(final I_M_ReceiptSchedule receiptSchedule)
InterfaceWrapperHelper.setTrxName(vhu, ITrx.TRXNAME_None);
return vhu;
}

protected final BigDecimal getDefaultAvailableQtyToReceive(final I_M_ReceiptSchedule rs)
{
final BigDecimal qty = receiptScheduleBL.getQtyToMove(rs);
return qty == null || qty.signum() <= 0 ? BigDecimal.ZERO : qty;
}

private final BigDecimal getAvailableQtyToReceive(final I_M_ReceiptSchedule rs)
protected BigDecimal getEffectiveQtyToReceive(final I_M_ReceiptSchedule rs)
{
return receiptScheduleBL.getQtyToMove(rs);
BigDecimal defaultAvailableQtyToReceive = getDefaultAvailableQtyToReceive(rs);
return defaultAvailableQtyToReceive;
}

private final IAllocationRequest createAllocationRequest(final I_M_ReceiptSchedule rs)
{
// Get Qty
final BigDecimal qty = getAvailableQtyToReceive(rs);
if (qty == null || qty.signum() <= 0)
final BigDecimal qty = getEffectiveQtyToReceive(rs);
if (qty.signum() <= 0)
{
// nothing to do
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package de.metas.ui.web.handlingunits.process;

import java.math.BigDecimal;
import java.util.stream.Stream;

import org.adempiere.exceptions.FillMandatoryException;

import de.metas.handlingunits.model.I_M_ReceiptSchedule;
import de.metas.process.IProcessDefaultParameter;
import de.metas.process.IProcessDefaultParametersProvider;
import de.metas.process.Param;

/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

public class WEBUI_M_ReceiptSchedule_ReceiveCUs_WithParam extends WEBUI_M_ReceiptSchedule_ReceiveCUs implements IProcessDefaultParametersProvider
{
private static final String PARAM_QtyCU = "QtyCU";
@Param(parameterName = PARAM_QtyCU, mandatory = true)
private BigDecimal p_QtyCU;

public WEBUI_M_ReceiptSchedule_ReceiveCUs_WithParam()
{
super();

// configure defaults
setAllowMultipleReceiptsSchedules(false);
setAllowNoQuantityAvailable(true);
}

@Override
public Object getParameterDefaultValue(final IProcessDefaultParameter parameter)
{
if (PARAM_QtyCU.equals(parameter.getColumnName()))
{
final I_M_ReceiptSchedule receiptSchedule = getM_ReceiptSchedule();
return getDefaultAvailableQtyToReceive(receiptSchedule);
}
else
{
return DEFAULT_VALUE_NOTAVAILABLE;
}
}

@Override
protected Stream<I_M_ReceiptSchedule> streamReceiptSchedulesToReceive()
{
return Stream.of(getM_ReceiptSchedule());
}

private I_M_ReceiptSchedule getM_ReceiptSchedule()
{
return getRecord(I_M_ReceiptSchedule.class);
}

@Override
protected BigDecimal getEffectiveQtyToReceive(I_M_ReceiptSchedule rs)
{
if(p_QtyCU == null || p_QtyCU.signum() <= 0)
{
throw new FillMandatoryException(PARAM_QtyCU);
}
return p_QtyCU;
}
}

0 comments on commit fef50fc

Please sign in to comment.