use of org.compiere.util.AdempiereUserError in project adempiere by adempiere.
the class ImmediateBankTransfer method doIt.
// prepare
/**
* Perform process.
* @return Message (translated text)
* @throws Exception if not successful
*/
protected String doIt() throws Exception {
log.info("From Bank=" + p_From_C_BankAccount_ID + " - To Bank=" + p_To_C_BankAccount_ID + " - C_CashBook_ID=" + p_C_CashBook_ID + " - Amount=" + p_Amount + " - Name=" + p_Name + " - Description=" + p_Description + " - Statement Date=" + p_StatementDate + " - Date Account=" + p_DateAcct);
if (p_To_C_BankAccount_ID == 0 || p_From_C_BankAccount_ID == 0)
throw new IllegalArgumentException("Banks required");
if (p_Name == null || p_Name.length() == 0)
throw new IllegalArgumentException("Name required");
// To_C_BankAccount_ID MUST be different than From_C_BankAccount_ID
if (p_To_C_BankAccount_ID == p_From_C_BankAccount_ID)
throw new AdempiereUserError("Banks From and To must be different");
// Banks and CashBook must have same currency
if (!isSameCurrency())
throw new AdempiereUserError("Banks and CashBook must have same currency");
if (p_Amount.compareTo(new BigDecimal(0)) == 0)
throw new AdempiereUserError("Amount required");
// Login Date
if (p_StatementDate == null)
p_StatementDate = Env.getContextAsDate(getCtx(), "#Date");
if (p_StatementDate == null)
p_StatementDate = new Timestamp(System.currentTimeMillis());
if (p_DateAcct == null)
p_DateAcct = p_StatementDate;
generateBankTransfer();
return "@Created@ = " + m_created;
}
use of org.compiere.util.AdempiereUserError in project adempiere by adempiere.
the class ImportPriceList method doIt.
// prepare
/**
* Perform process.
* @return Message
* @throws Exception
*/
protected String doIt() throws Exception {
StringBuffer sql = null;
int no = 0;
String clientCheck = " AND AD_Client_ID=" + m_AD_Client_ID;
int m_discountschema_id = DB.getSQLValue(get_TrxName(), "SELECT MIN(M_DiscountSchema_ID) FROM M_DiscountSchema WHERE DiscountType='P' AND IsActive='Y' AND AD_Client_ID=?", m_AD_Client_ID);
if (m_discountschema_id <= 0)
throw new AdempiereUserError("Price List Schema not configured");
// Delete Old Imported
if (m_deleteOldImported) {
sql = new StringBuffer("DELETE I_PriceList " + "WHERE I_IsImported='Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.info("Delete Old Impored =" + no);
}
// Set Client, Org, IsActive, Created/Updated, EnforcePriceLimit, IsSOPriceList, IsTaxIncluded, PricePrecision
sql = new StringBuffer("UPDATE I_PriceList " + "SET AD_Client_ID = COALESCE (AD_Client_ID, ").append(m_AD_Client_ID).append(")," + " AD_Org_ID = COALESCE (AD_Org_ID, 0)," + " IsActive = COALESCE (IsActive, 'Y')," + " Created = COALESCE (Created, SysDate)," + " CreatedBy = COALESCE (CreatedBy, 0)," + " Updated = COALESCE (Updated, SysDate)," + " UpdatedBy = COALESCE (UpdatedBy, 0)," + " EnforcePriceLimit = COALESCE (EnforcePriceLimit, 'N')," + " IsSOPriceList = COALESCE (IsSOPriceList, 'N')," + " IsTaxIncluded = COALESCE (IsTaxIncluded, 'N')," + " PricePrecision = COALESCE (PricePrecision, 2)," + " I_ErrorMsg = ' '," + " I_IsImported = 'N' " + "WHERE I_IsImported<>'Y' OR I_IsImported IS NULL");
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.info("Reset=" + no);
// Set Optional BPartner
sql = new StringBuffer("UPDATE I_PriceList " + "SET C_BPartner_ID=(SELECT C_BPartner_ID FROM C_BPartner p" + " WHERE I_PriceList.BPartner_Value=p.Value AND I_PriceList.AD_Client_ID=p.AD_Client_ID) " + "WHERE C_BPartner_ID IS NULL AND BPartner_Value IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.info("BPartner=" + no);
//
sql = new StringBuffer("UPDATE I_PriceList " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid BPartner,' " + "WHERE C_BPartner_ID IS NULL AND BPartner_Value IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
if (no != 0)
log.warning("Invalid BPartner=" + no);
// Product
sql = new StringBuffer("UPDATE I_PriceList " + "SET M_Product_ID=(SELECT MAX(M_Product_ID) FROM M_Product p" + " WHERE I_PriceList.ProductValue=p.Value AND I_PriceList.AD_Client_ID=p.AD_Client_ID) " + "WHERE M_Product_ID IS NULL AND ProductValue IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Product from Value=" + no);
sql = new StringBuffer("UPDATE I_PriceList " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Product, ' " + "WHERE M_Product_ID IS NULL AND (ProductValue IS NOT NULL)" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
if (no != 0)
log.warning("Invalid Product=" + no);
// **** Find Price List
// Name
sql = new StringBuffer("UPDATE I_PriceList " + "SET M_PriceList_ID=(SELECT M_PriceList_ID FROM M_PriceList p" + " WHERE I_PriceList.Name=p.Name AND I_PriceList.AD_Client_ID=p.AD_Client_ID) " + "WHERE M_PriceList_ID IS NULL" + " AND I_IsImported='N'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.info("Price List Existing Value=" + no);
// **** Find Price List Version
// List Name (ID) + ValidFrom
sql = new StringBuffer("UPDATE I_PriceList " + "SET M_PriceList_Version_ID=(SELECT M_PriceList_Version_ID FROM M_PriceList_Version p" + " WHERE I_PriceList.ValidFrom=p.ValidFrom AND I_PriceList.M_PriceList_ID=p.M_PriceList_ID) " + "WHERE M_PriceList_ID IS NOT NULL AND M_PriceList_Version_ID IS NULL" + " AND I_IsImported='N'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.info("Price List Version Existing Value=" + no);
/* UOM For Future USE
// Set UOM (System/own)
sql = new StringBuffer ("UPDATE I_PriceList "
+ "SET X12DE355 = "
+ "(SELECT MAX(X12DE355) FROM C_UOM u WHERE u.IsDefault='Y' AND u.AD_Client_ID IN (0,I_PriceList.AD_Client_ID)) "
+ "WHERE X12DE355 IS NULL AND C_UOM_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set UOM Default=" + no);
//
sql = new StringBuffer ("UPDATE I_PriceList "
+ "SET C_UOM_ID = (SELECT C_UOM_ID FROM C_UOM u WHERE u.X12DE355=I_PriceList.X12DE355 AND u.AD_Client_ID IN (0,I_PriceList.AD_Client_ID)) "
+ "WHERE C_UOM_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.info("Set UOM=" + no);
//
sql = new StringBuffer ("UPDATE I_PriceList "
+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid UOM, ' "
+ "WHERE C_UOM_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
if (no != 0)
log.warning("Invalid UOM=" + no);
*/
// Set Currency
sql = new StringBuffer("UPDATE I_PriceList " + "SET ISO_Code=(SELECT ISO_Code FROM C_Currency c" + " INNER JOIN C_AcctSchema a ON (a.C_Currency_ID=c.C_Currency_ID)" + " INNER JOIN AD_ClientInfo ci ON (a.C_AcctSchema_ID=ci.C_AcctSchema1_ID)" + " WHERE ci.AD_Client_ID=I_PriceList.AD_Client_ID) " + "WHERE C_Currency_ID IS NULL AND ISO_Code IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Currency Default=" + no);
//
sql = new StringBuffer("UPDATE I_PriceList " + "SET C_Currency_ID=(SELECT C_Currency_ID FROM C_Currency c" + " WHERE I_PriceList.ISO_Code=c.ISO_Code AND c.AD_Client_ID IN (0,I_PriceList.AD_Client_ID)) " + "WHERE C_Currency_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.info("doIt- Set Currency=" + no);
//
sql = new StringBuffer("UPDATE I_PriceList " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Currency,' " + "WHERE C_Currency_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
if (no != 0)
log.warning("Invalid Currency=" + no);
// Mandatory Name or PriceListID
sql = new StringBuffer("UPDATE I_PriceList " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Mandatory Name or PriceListID,' " + "WHERE Name IS NULL AND M_PriceList_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
if (no != 0)
log.warning("No Mandatory Name=" + no);
// Mandatory ValidFrom or PriceListVersionID
sql = new StringBuffer("UPDATE I_PriceList " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Mandatory ValidFrom or PriceListVersionID,' " + "WHERE ValidFrom IS NULL AND M_PriceList_Version_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
if (no != 0)
log.warning("No Mandatory ValidFrom=" + no);
// Mandatory BreakValue if BPartner set
sql = new StringBuffer("UPDATE I_PriceList " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No Mandatory BreakValue,' " + "WHERE BreakValue IS NULL AND (C_BPartner_ID IS NOT NULL OR BPartner_Value IS NOT NULL)" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
if (no != 0)
log.warning("No Mandatory BreakValue=" + no);
commitEx();
// -------------------------------------------------------------------
int noInsertpp = 0;
int noUpdatepp = 0;
int noInsertppvb = 0;
int noUpdateppvb = 0;
int noInsertpl = 0;
int noInsertplv = 0;
// Go through Records
log.fine("start inserting/updating ...");
sql = new StringBuffer("SELECT * FROM I_PriceList WHERE I_IsImported='N'").append(clientCheck);
PreparedStatement pstmt_setImported = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// Set Imported = Y
pstmt_setImported = DB.prepareStatement("UPDATE I_PriceList SET I_IsImported='Y', M_PriceList_ID=?, M_PriceList_Version_ID=?, " + "Updated=SysDate, Processed='Y' WHERE I_PriceList_ID=?", get_TrxName());
//
pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
rs = pstmt.executeQuery();
while (rs.next()) {
X_I_PriceList imp = new X_I_PriceList(getCtx(), rs, get_TrxName());
int I_PriceList_ID = imp.getI_PriceList_ID();
int M_PriceList_ID = imp.getM_PriceList_ID();
if (M_PriceList_ID == 0) {
// try to obtain the ID directly from DB
M_PriceList_ID = DB.getSQLValue(get_TrxName(), "SELECT M_PriceList_ID FROM M_PriceList WHERE IsActive='Y' AND AD_Client_ID=? AND Name=?", m_AD_Client_ID, imp.getName());
if (M_PriceList_ID < 0)
M_PriceList_ID = 0;
}
boolean newPriceList = M_PriceList_ID == 0;
log.fine("I_PriceList_ID=" + I_PriceList_ID + ", M_PriceList_ID=" + M_PriceList_ID);
MPriceList pricelist = null;
// PriceList
if (// Insert new Price List
newPriceList) {
pricelist = new MPriceList(imp);
if (pricelist.save()) {
M_PriceList_ID = pricelist.getM_PriceList_ID();
log.finer("Insert Price List");
noInsertpl++;
} else {
StringBuffer sql0 = new StringBuffer("UPDATE I_PriceList i " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert Price List failed")).append("WHERE I_PriceList_ID=").append(I_PriceList_ID);
DB.executeUpdate(sql0.toString(), get_TrxName());
continue;
}
} else {
// NOTE no else clause - if the price list already exists it's not updated
pricelist = new MPriceList(getCtx(), M_PriceList_ID, get_TrxName());
}
int M_PriceList_Version_ID = imp.getM_PriceList_Version_ID();
if (M_PriceList_Version_ID == 0) {
// try to obtain the ID directly from DB
M_PriceList_Version_ID = DB.getSQLValue(get_TrxName(), "SELECT M_PriceList_Version_ID FROM M_PriceList_Version WHERE IsActive='Y' AND ValidFrom=? AND M_PriceList_ID=?", new Object[] { imp.getValidFrom(), M_PriceList_ID });
if (M_PriceList_Version_ID < 0)
M_PriceList_Version_ID = 0;
}
boolean newPriceListVersion = M_PriceList_Version_ID == 0;
log.fine("I_PriceList_ID=" + I_PriceList_ID + ", M_PriceList_Version_ID=" + M_PriceList_Version_ID);
MPriceListVersion pricelistversion = null;
// PriceListVersion
if (// Insert new Price List Version
newPriceListVersion) {
pricelistversion = new MPriceListVersion(pricelist);
pricelistversion.setValidFrom(imp.getValidFrom());
pricelistversion.setName(pricelist.getName() + " " + imp.getValidFrom());
pricelistversion.setM_DiscountSchema_ID(m_discountschema_id);
if (pricelistversion.save()) {
M_PriceList_Version_ID = pricelistversion.getM_PriceList_Version_ID();
log.finer("Insert Price List Version");
noInsertplv++;
} else {
StringBuffer sql0 = new StringBuffer("UPDATE I_PriceList i " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert Price List Version failed")).append("WHERE I_PriceList_ID=").append(I_PriceList_ID);
DB.executeUpdate(sql0.toString(), get_TrxName());
continue;
}
} else {
// NOTE no else clause - if the price list version already exists it's not updated
pricelistversion = new MPriceListVersion(getCtx(), M_PriceList_Version_ID, get_TrxName());
}
// If bpartner then insert/update into M_ProductPriceVendorBreak, otherwise insert/update M_ProductPrice
if (imp.getC_BPartner_ID() > 0) {
// M_ProductPriceVendorBreak
int M_ProductPriceVendorBreak_ID = DB.getSQLValue(get_TrxName(), "SELECT M_ProductPriceVendorBreak_ID " + "FROM M_ProductPriceVendorBreak " + "WHERE M_PriceList_Version_ID=? AND " + "IsActive='Y' AND " + "C_BPartner_ID=? AND " + "M_Product_ID=? AND " + "BreakValue=?", new Object[] { pricelistversion.getM_PriceList_Version_ID(), imp.getC_BPartner_ID(), imp.getM_Product_ID(), imp.getBreakValue() });
if (M_ProductPriceVendorBreak_ID < 0)
M_ProductPriceVendorBreak_ID = 0;
X_M_ProductPriceVendorBreak ppvb = new X_M_ProductPriceVendorBreak(getCtx(), M_ProductPriceVendorBreak_ID, get_TrxName());
boolean isInsert = false;
if (M_ProductPriceVendorBreak_ID == 0) {
ppvb.setM_PriceList_Version_ID(pricelistversion.getM_PriceList_Version_ID());
ppvb.setC_BPartner_ID(imp.getC_BPartner_ID());
ppvb.setM_Product_ID(imp.getM_Product_ID());
ppvb.setBreakValue(imp.getBreakValue());
isInsert = true;
}
if (p_importPriceLimit)
ppvb.setPriceLimit(imp.getPriceLimit());
if (p_importPriceList)
ppvb.setPriceList(imp.getPriceList());
if (p_importPriceStd)
ppvb.setPriceStd(imp.getPriceStd());
if (ppvb.save()) {
if (isInsert)
noInsertppvb++;
else
noUpdateppvb++;
log.finer("Insert/Update Product Price Vendor Break");
} else {
StringBuffer sql0 = new StringBuffer("UPDATE I_PriceList i " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert/Update Product Price Vendor Break Version failed")).append("WHERE I_PriceList_ID=").append(I_PriceList_ID);
DB.executeUpdate(sql0.toString(), get_TrxName());
continue;
}
} else {
// M_ProductPrice
MProductPrice pp = MProductPrice.get(getCtx(), pricelistversion.getM_PriceList_Version_ID(), imp.getM_Product_ID(), get_TrxName());
boolean isInsert = false;
if (pp != null) {
if (p_importPriceLimit)
pp.setPriceLimit(imp.getPriceLimit());
if (p_importPriceList)
pp.setPriceList(imp.getPriceList());
if (p_importPriceStd)
pp.setPriceStd(imp.getPriceStd());
} else {
pp = new MProductPrice(pricelistversion, imp.getM_Product_ID(), p_importPriceList ? imp.getPriceList() : Env.ZERO, p_importPriceStd ? imp.getPriceStd() : Env.ZERO, p_importPriceLimit ? imp.getPriceLimit() : Env.ZERO);
isInsert = true;
}
if (pp.save()) {
log.finer("Insert/Update Product Price");
if (isInsert)
noInsertpp++;
else
noUpdatepp++;
} else {
StringBuffer sql0 = new StringBuffer("UPDATE I_PriceList i " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert/Update Product Price failed")).append("WHERE I_PriceList_ID=").append(I_PriceList_ID);
DB.executeUpdate(sql0.toString(), get_TrxName());
continue;
}
}
// Update I_PriceList
pstmt_setImported.setInt(1, M_PriceList_ID);
pstmt_setImported.setInt(2, M_PriceList_Version_ID);
pstmt_setImported.setInt(3, I_PriceList_ID);
no = pstmt_setImported.executeUpdate();
//
commitEx();
}
// for all I_PriceList
//
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
DB.close(pstmt_setImported);
pstmt_setImported = null;
}
// Set Error to indicator to not imported
sql = new StringBuffer("UPDATE I_PriceList " + "SET I_IsImported='N', Updated=SysDate " + "WHERE I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
addLog(0, null, new BigDecimal(no), "@Errors@");
addLog(0, null, new BigDecimal(noInsertpl), "@M_PriceList_ID@: @Inserted@");
addLog(0, null, new BigDecimal(noInsertplv), "@M_PriceList_Version_ID@: @Inserted@");
addLog(0, null, new BigDecimal(noInsertpp), "Product Price: @Inserted@");
addLog(0, null, new BigDecimal(noUpdatepp), "Product Price: @Updated@");
addLog(0, null, new BigDecimal(noInsertppvb), "@M_ProductPriceVendorBreak_ID@: @Inserted@");
addLog(0, null, new BigDecimal(noUpdateppvb), "@M_ProductPriceVendorBreak_ID@: @Updated@");
return "";
}
use of org.compiere.util.AdempiereUserError in project adempiere by adempiere.
the class CostUpdate method doIt.
// prepare
/**
* Process
* @return info
* @throws Exception
*/
@SuppressWarnings("deprecation")
protected String doIt() throws Exception {
log.info("M_Product_Category_ID=" + p_M_Product_Category_ID + ", Future=" + p_SetFutureCostTo + ", Standard=" + p_SetStandardCostTo + "; M_PriceList_Version_ID=" + p_M_PriceList_Version_ID);
if (p_SetFutureCostTo == null)
p_SetFutureCostTo = "";
if (p_SetStandardCostTo == null)
p_SetStandardCostTo = "";
// Nothing to Do
if (p_SetFutureCostTo.length() == 0 && p_SetStandardCostTo.length() == 0) {
return "-";
}
// PLV required
if (p_M_PriceList_Version_ID == 0 && (p_SetFutureCostTo.equals(TO_PriceListLimit) || p_SetStandardCostTo.equals(TO_PriceListLimit)))
throw new AdempiereUserError("@FillMandatory@ @M_PriceList_Version_ID@");
// Validate Source
if (!isValid(p_SetFutureCostTo))
throw new AdempiereUserError("@NotFound@ @M_CostElement_ID@ (Future) " + p_SetFutureCostTo);
if (!isValid(p_SetStandardCostTo))
throw new AdempiereUserError("@NotFound@ @M_CostElement_ID@ (Standard) " + p_SetStandardCostTo);
// Prepare
m_ce = MCostElement.getMaterialCostElement(client);
if (m_ce.get_ID() == 0)
throw new AdempiereUserError("@NotFound@ @M_CostElement_ID@ (StdCost)");
log.config(m_ce.toString());
m_ass = MAcctSchema.getClientAcctSchema(getCtx(), client.getAD_Client_ID());
for (int i = 0; i < m_ass.length; i++) createNew(m_ass[i]);
commitEx();
// Update Cost
int counter = update();
return "#" + counter;
}
use of org.compiere.util.AdempiereUserError in project adempiere by adempiere.
the class DiscountSchemaReSeq method doIt.
// prepare
/**
* Execute
* @return info
*/
protected String doIt() throws Exception {
log.info("M_DiscountSchema_ID=" + p_M_DiscountSchema_ID);
if (p_M_DiscountSchema_ID == 0)
throw new AdempiereUserError("@M_DiscountSchema_ID@ = 0");
MDiscountSchema ds = new MDiscountSchema(getCtx(), p_M_DiscountSchema_ID, get_TrxName());
if (ds.get_ID() == 0)
throw new AdempiereUserError("@NotFound@ M_DiscountSchema_ID=" + p_M_DiscountSchema_ID);
//
int updated = ds.reSeq();
return "@Updated@ #" + updated;
}
use of org.compiere.util.AdempiereUserError in project adempiere by adempiere.
the class BPartnerOrgLink method doIt.
// prepare
/**
* Perform process.
* @return Message (text with variables)
* @throws Exception if not successful
*/
protected String doIt() throws Exception {
log.info("C_BPartner_ID=" + p_C_BPartner_ID + ", AD_Org_ID=" + p_AD_Org_ID + ", AD_OrgType_ID=" + p_AD_OrgType_ID + ", AD_Role_ID=" + p_AD_Role_ID);
if (p_C_BPartner_ID == 0)
throw new AdempiereUserError("No Business Partner ID");
MBPartner bp = new MBPartner(getCtx(), p_C_BPartner_ID, get_TrxName());
if (bp.get_ID() == 0)
throw new AdempiereUserError("Business Partner not found - C_BPartner_ID=" + p_C_BPartner_ID);
// BP Location
MBPartnerLocation[] locs = bp.getLocations(false);
if (locs == null || locs.length == 0)
throw new IllegalArgumentException("Business Partner has no Location");
// Location
int C_Location_ID = locs[0].getC_Location_ID();
if (C_Location_ID == 0)
throw new IllegalArgumentException("Business Partner Location has no Address");
// Create Org
boolean newOrg = p_AD_Org_ID == 0;
MOrg org = new MOrg(getCtx(), p_AD_Org_ID, get_TrxName());
if (newOrg) {
org.setValue(bp.getValue());
org.setName(bp.getName());
org.setDescription(bp.getDescription());
if (!org.save())
throw new Exception("Organization not saved");
} else // check if linked to already
{
int C_BPartner_ID = org.getLinkedC_BPartner_ID(get_TrxName());
if (C_BPartner_ID > 0)
throw new IllegalArgumentException("Organization '" + org.getName() + "' already linked (to C_BPartner_ID=" + C_BPartner_ID + ")");
}
p_AD_Org_ID = org.getAD_Org_ID();
// Update Org Info
MOrgInfo oInfo = org.getInfo();
oInfo.setAD_OrgType_ID(p_AD_OrgType_ID);
if (newOrg)
oInfo.setC_Location_ID(C_Location_ID);
// Create Warehouse
MWarehouse wh = null;
if (!newOrg) {
MWarehouse[] whs = MWarehouse.getForOrg(getCtx(), p_AD_Org_ID);
if (whs != null && whs.length > 0)
// pick first
wh = whs[0];
}
// New Warehouse
if (wh == null) {
wh = new MWarehouse(org);
if (!wh.save(get_TrxName()))
throw new Exception("Warehouse not saved");
}
// Create Locator
MLocator mLoc = wh.getDefaultLocator();
if (mLoc == null) {
mLoc = new MLocator(wh, "Standard");
mLoc.setIsDefault(true);
mLoc.save(get_TrxName());
}
// Update/Save Org Info
oInfo.setM_Warehouse_ID(wh.getM_Warehouse_ID());
if (!oInfo.save(get_TrxName()))
throw new Exception("Organization Info not saved");
// Update BPartner
bp.setAD_OrgBP_ID(p_AD_Org_ID);
if (bp.getAD_Org_ID() != 0)
// Shared BPartner
bp.setClientOrg(bp.getAD_Client_ID(), 0);
// Save BP
if (!bp.save())
throw new Exception("Business Partner not updated");
// Limit to specific Role
if (p_AD_Role_ID != 0) {
boolean found = false;
MRoleOrgAccess[] orgAccesses = MRoleOrgAccess.getOfOrg(getCtx(), p_AD_Org_ID);
// delete all accesses except the specific
for (int i = 0; i < orgAccesses.length; i++) {
if (orgAccesses[i].getAD_Role_ID() == p_AD_Role_ID)
found = true;
else
orgAccesses[i].delete(true);
}
// create access
if (!found) {
MRoleOrgAccess orgAccess = new MRoleOrgAccess(org, p_AD_Role_ID);
orgAccess.save(get_TrxName());
}
}
// Reset Client Role
MRole.getDefault(getCtx(), true);
return "Business Partner - Organization Link created";
}
Aggregations