Skip to content

Commit

Permalink
Fix using system AD_User_ID for contact in some places instead… (#6445)
Browse files Browse the repository at this point in the history
* Fix using system AD_User_ID for contact in some places instead of BPartnerContactId

#6302

* Fix test
#6302
  • Loading branch information
TheBestPessimist committed Apr 6, 2020
1 parent f1ac905 commit 3360664
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 67 deletions.
Expand Up @@ -29,6 +29,7 @@ public interface IDocumentLocation

int getC_BPartner_Location_ID();

// TODO this should be refactored to return BPartnerContactID
int getAD_User_ID();

String getBPartnerAddress();
Expand Down
Expand Up @@ -10,22 +10,22 @@
* 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%
*/


import java.util.ArrayList;
import java.util.List;

import de.metas.bpartner.BPartnerContactId;
import lombok.NonNull;
import org.adempiere.util.agg.key.IAggregationKeyValueHandler;

Expand All @@ -51,7 +51,11 @@ public List<Object> getValues(@NonNull final I_M_ReceiptSchedule rs)
values.add(receiptScheduleBL.getC_BPartner_Effective_ID(rs));
values.add(receiptScheduleBL.getC_BPartner_Location_Effective_ID(rs));
values.add(receiptScheduleBL.getWarehouseEffectiveId(rs).getRepoId());
values.add(receiptScheduleBL.getAD_User_Effective_ID(rs));
final BPartnerContactId bPartnerContactID = receiptScheduleBL.getBPartnerContactID(rs);
if (bPartnerContactID != null)
{
values.add(bPartnerContactID);
}
values.add(rs.getAD_Org_ID());
values.add(rs.getDateOrdered());
values.add(rs.getC_Order_ID());
Expand Down
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Properties;

import de.metas.bpartner.BPartnerContactId;
import org.adempiere.mm.attributes.AttributeSetInstanceId;
import org.adempiere.util.agg.key.IAggregationKeyBuilder;
import org.adempiere.warehouse.LocatorId;
Expand All @@ -45,26 +46,24 @@
import de.metas.quantity.StockQtyAndUOMQty;
import de.metas.util.ISingletonService;

import javax.annotation.Nullable;

public interface IReceiptScheduleBL extends ISingletonService
{
void addReceiptScheduleListener(IReceiptScheduleListener listener);

/**
* Create {@link IInOutProducer} instance for given initial result
*
* @param resultInitial
* @param complete true if generated documents shall be completed
* @return producer
*/
IInOutProducer createInOutProducer(InOutGenerateResult resultInitial, boolean complete);

/**
*
* NOTE: this method assumes that <code>receiptSchedules</code> are already ordered by aggregation keys.
*
* @param ctx
* @param receiptSchedules
* @param result results collector
* @param result results collector
* @param complete if true, complete generated receipts
*/
void generateInOuts(Properties ctx, Iterator<I_M_ReceiptSchedule> receiptSchedules, InOutGenerateResult result, boolean complete);
Expand All @@ -73,17 +72,14 @@ public interface IReceiptScheduleBL extends ISingletonService

/**
* Gets Target Qty (i.e. how much shall we receive in the end).
*
* <p>
* NOTE: {@link I_M_ReceiptSchedule#COLUMNNAME_QtyToMove_Override} is checked first and if is not set then {@link I_M_ReceiptSchedule#COLUMNNAME_QtyOrdered} is considered.
*
* @param rs
* @return target qty
*/
BigDecimal getQtyOrdered(I_M_ReceiptSchedule rs);

/**
*
* @param rs
* @return qty moved
*/
BigDecimal getQtyMoved(I_M_ReceiptSchedule rs);
Expand All @@ -96,35 +92,28 @@ public interface IReceiptScheduleBL extends ISingletonService
StockQtyAndUOMQty getQtyToMove(final I_M_ReceiptSchedule rs);

/**
*
* @param rs
* @return M_Warehouse_Override_ID and falls back to M_Warehouse_ID if no override value is set
*/
WarehouseId getWarehouseEffectiveId(final I_M_ReceiptSchedule rs);

/**
* @param rs
* @return M_Warehouse_Override and falls back to M_Warehouse if no override value is set
*/
I_M_Warehouse getM_Warehouse_Effective(I_M_ReceiptSchedule rs);

/**
* Gets effective locator to be used when receiving materials with this receipt schedule.
*
* @param rs
* @return default locator for {@link #getM_Warehouse_Effective(I_M_ReceiptSchedule)}.
*/
LocatorId getLocatorEffectiveId(I_M_ReceiptSchedule rs);

/**
*
* @param rs
* @return C_BP_Location_Override_ID and falls back to C_BPartner_Location_ID if no override value is set
*/
int getC_BPartner_Location_Effective_ID(I_M_ReceiptSchedule rs);

/**
* @param rs
* @return C_BPartner_Location_Override and falls back to C_BPartner_Location if no override value is set
*/

Expand All @@ -133,33 +122,29 @@ public interface IReceiptScheduleBL extends ISingletonService
BPartnerId getBPartnerEffectiveId(I_M_ReceiptSchedule rs);

/**
* @param rs
* @return C_BPartner_Override_ID and falls back to C_BPartner_ID if no override value is set
* @deprecated Please use {@link #getBPartnerEffectiveId(I_M_ReceiptSchedule)} which returns a {@link BPartnerId}
*/
@Deprecated
int getC_BPartner_Effective_ID(I_M_ReceiptSchedule rs);

/**
* @param rs
* @return C_BPartner_Override and falls back to C_BPartner if no override value is set
*/
I_C_BPartner getC_BPartner_Effective(I_M_ReceiptSchedule rs);

/**
* @param rs
* @return AD_User_Override_ID and falls back to AD_User_ID if no override value is set
*/
int getAD_User_Effective_ID(I_M_ReceiptSchedule rs);
@Nullable
BPartnerContactId getBPartnerContactID(I_M_ReceiptSchedule rs);

/**
*
* @param rs
* @return M_AttributeSetInstance_ID to use
*/
int getM_AttributeSetInstance_Effective_ID(I_M_ReceiptSchedule rs);

/**
*
* @param rs
* @return {@link I_M_AttributeSetInstance} to use
*/
I_M_AttributeSetInstance getM_AttributeSetInstance_Effective(I_M_ReceiptSchedule rs);
Expand All @@ -173,36 +158,26 @@ public interface IReceiptScheduleBL extends ISingletonService

/**
* Updates {@link I_M_ReceiptSchedule#COLUMNNAME_BPartnerAddress}
*
* @param receiptSchedule
*/
void updateBPartnerAddress(I_M_ReceiptSchedule receiptSchedule);

/**
* Updates {@link I_M_ReceiptSchedule#COLUMNNAME_BPartnerAddress_Override}
*
* @param receiptSchedule
*/
void updateBPartnerAddressOverride(I_M_ReceiptSchedule receiptSchedule);

/**
* Gets {@link I_M_ReceiptSchedule_Alloc} from {@link I_M_ReceiptSchedule} to {@link I_M_InOutLine}.
*
* <p>
* If allocation does not exist, it will be created. If allocation exists, it will be returned untouched.
*
* @param receiptSchedule
* @param receipt
* @return
*/
I_M_ReceiptSchedule_Alloc createRsaIfNotExists(I_M_ReceiptSchedule receiptSchedule, I_M_InOutLine receipt);

/**
* Allocate given receipt line to the list of of provided receipt schedules.
*
* <p>
* NOTE: if all receipt schedules were linked to the same Order Line, that order line will be set to given receipt.
*
* @param receiptSchedules
* @param receiptLine
* @return receipt schedule allocations that were created
*/
List<I_M_ReceiptSchedule_Alloc> createReceiptScheduleAllocations(List<? extends I_M_ReceiptSchedule> receiptSchedules, I_M_InOutLine receiptLine);
Expand All @@ -211,47 +186,38 @@ public interface IReceiptScheduleBL extends ISingletonService

/**
* updates preparation time
*
* @param receiptSchedule
*/
void updatePreparationTime(I_M_ReceiptSchedule receiptSchedule);

/**
* Reverse given allocation by creating a new allocation which is canceling the effect.
*
* <p>
* NOTE: reversal allocation will not be saved because user may want to change some fields before saving
*
* @param rsa
* @return reversal allocation (not saved)
*/
I_M_ReceiptSchedule_Alloc reverseAllocation(I_M_ReceiptSchedule_Alloc rsa);

/**
* Close receipt schedule line and mark it as processed.
*
* <p>
* Call the {@link IReceiptScheduleListener}s' beforeClose method, then set the schedule to <code>Processed=Y</code>, then save the given <code>receiptSchedule</code>, then call the listeners' afterClose() methods and finally save the record again.
* <p>
* The saving prior to <code>afterClose()</code> is done because the listeners might also retrieve the same <code>receiptSchedule</code> data record from the DB and might also change and save it.
* See {@link C_OrderLine_ReceiptSchedule} for an example.
* <p>
* The saving after <code>afterClose()</code> is done to accommodate for listeners that only alter the given <code>receiptSchedule</code> without saving it (which is actually the nice thing to do).
*
*
* @param rs
*/
void close(I_M_ReceiptSchedule receiptSchedule);

/**
* Re-open a closed receipt schedule line. Similar to {@link #close(I_M_ReceiptSchedule)}. Also, we save the given <code>receiptSchedule</code> twice for similar reasons.
*
* @param receiptSchedule
*/
void reopen(I_M_ReceiptSchedule receiptSchedule);

/**
* Checks if given receipt schedule is closed (i.e. {@link I_M_ReceiptSchedule#isProcessed()}).
*
* @param receiptSchedule
* @return true if receipt schedule is closed
*/
boolean isClosed(I_M_ReceiptSchedule receiptSchedule);
Expand Down
Expand Up @@ -31,6 +31,7 @@
import java.util.Properties;
import java.util.Set;

import de.metas.bpartner.BPartnerContactId;
import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.ad.trx.processor.api.ITrxItemProcessorContext;
import org.adempiere.mm.attributes.api.AttributeConstants;
Expand Down Expand Up @@ -439,11 +440,11 @@ private final I_M_InOut createReceiptHeader(final I_M_ReceiptSchedule rs)
{
final int bpartnerId = receiptScheduleBL.getC_BPartner_Effective_ID(rs);
final int bpartnerLocationId = receiptScheduleBL.getC_BPartner_Location_Effective_ID(rs);
final int bpartnerContactId = receiptScheduleBL.getAD_User_Effective_ID(rs);
final BPartnerContactId bpartnerContactId = receiptScheduleBL.getBPartnerContactID(rs);

receiptHeader.setC_BPartner_ID(bpartnerId);
receiptHeader.setC_BPartner_Location_ID(bpartnerLocationId);
receiptHeader.setAD_User_ID(bpartnerContactId);
receiptHeader.setAD_User_ID(bpartnerContactId.toRepoId(bpartnerContactId));
}

//
Expand Down
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.Properties;

import de.metas.bpartner.BPartnerContactId;
import org.adempiere.ad.trx.processor.api.ITrxItemProcessorExecutorService;
import org.adempiere.ad.trx.processor.api.LoggableTrxItemExceptionHandler;
import org.adempiere.exceptions.AdempiereException;
Expand Down Expand Up @@ -67,6 +68,8 @@
import de.metas.util.Services;
import lombok.NonNull;

import javax.annotation.Nullable;

public class ReceiptScheduleBL implements IReceiptScheduleBL
{
private final CompositeReceiptScheduleListener listeners = new CompositeReceiptScheduleListener();
Expand Down Expand Up @@ -178,6 +181,7 @@ public I_C_BPartner_Location getC_BPartner_Location_Effective(final I_M_ReceiptS
return location;
}

@Deprecated
@Override
public int getC_BPartner_Effective_ID(final I_M_ReceiptSchedule rs)
{
Expand Down Expand Up @@ -205,16 +209,14 @@ public I_C_BPartner getC_BPartner_Effective(final I_M_ReceiptSchedule rs)
return bpartnersRepo.getById(bpartnerId, I_C_BPartner.class);
}

@Nullable
@Override
public int getAD_User_Effective_ID(final I_M_ReceiptSchedule rs)
public BPartnerContactId getBPartnerContactID(@NonNull final I_M_ReceiptSchedule rs)
{
final int adUserOverrideID = rs.getAD_User_Override_ID();
if (adUserOverrideID > 0)
{
return adUserOverrideID;
}
final int cBPartnerIdRepo = rs.getC_BPartner_Override_ID() > 0 ? rs.getC_BPartner_Override_ID() : rs.getC_BPartner_ID();
final int adUserIdRepo = rs.getAD_User_Override_ID() > 0 ? rs.getAD_User_Override_ID() : rs.getAD_User_ID();

return rs.getAD_User_ID();
return BPartnerContactId.ofRepoIdOrNull(cBPartnerIdRepo, adUserIdRepo);
}

private IDocumentLocation asDocumentLocation(final I_M_ReceiptSchedule receiptSchedule)
Expand Down Expand Up @@ -286,7 +288,7 @@ public String getBPartnerAddress()
@Override
public int getAD_User_ID()
{
return getAD_User_Effective_ID(receiptSchedule);
return BPartnerContactId.toRepoId(getBPartnerContactID(receiptSchedule));
}
};
}
Expand Down Expand Up @@ -318,7 +320,7 @@ public void generateInOuts(final Properties ctx,
@NonNull final IInOutProducer producer,
@NonNull final Iterator<I_M_ReceiptSchedule> receiptSchedules)
{
Services.get(ITrxItemProcessorExecutorService.class).<I_M_ReceiptSchedule, InOutGenerateResult> createExecutor()
Services.get(ITrxItemProcessorExecutorService.class).<I_M_ReceiptSchedule, InOutGenerateResult>createExecutor()
.setContext(ctx)
.setProcessor(producer)
.setExceptionHandler(LoggableTrxItemExceptionHandler.instance)
Expand Down
Expand Up @@ -46,6 +46,7 @@
import de.metas.util.Services;
import de.metas.util.time.SystemTime;

@SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
public class ReceiptScheduleBLTest extends ReceiptScheduleTestBase
{
final boolean complete = true;
Expand Down

0 comments on commit 3360664

Please sign in to comment.