Search in sources :

Example 1 with MRoleOrgAccess

use of org.compiere.model.MRoleOrgAccess 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";
}
Also used : AdempiereUserError(org.compiere.util.AdempiereUserError) MBPartner(org.compiere.model.MBPartner) MWarehouse(org.compiere.model.MWarehouse) MBPartnerLocation(org.compiere.model.MBPartnerLocation) MOrg(org.compiere.model.MOrg) MLocator(org.compiere.model.MLocator) MOrgInfo(org.compiere.model.MOrgInfo) MRoleOrgAccess(org.compiere.model.MRoleOrgAccess)

Aggregations

MBPartner (org.compiere.model.MBPartner)1 MBPartnerLocation (org.compiere.model.MBPartnerLocation)1 MLocator (org.compiere.model.MLocator)1 MOrg (org.compiere.model.MOrg)1 MOrgInfo (org.compiere.model.MOrgInfo)1 MRoleOrgAccess (org.compiere.model.MRoleOrgAccess)1 MWarehouse (org.compiere.model.MWarehouse)1 AdempiereUserError (org.compiere.util.AdempiereUserError)1