Search in sources :

Example 1 with MOpportunity

use of org.compiere.model.MOpportunity in project adempiere by adempiere.

the class ConvertLead method doIt.

@Override
protected String doIt() throws Exception {
    if (p_AD_User_ID <= 0)
        throw new FillMandatoryException("AD_User_ID");
    MUser lead = new MUser(getCtx(), p_AD_User_ID, get_TrxName());
    if (!lead.isSalesLead() || lead.getC_BPartner_ID() != 0)
        throw new AdempiereUserError("Lead already converted");
    MBPartner bp = MBPartner.getTemplate(getCtx(), getAD_Client_ID());
    bp.set_TrxName(get_TrxName());
    if (!Util.isEmpty(lead.getBPName()))
        bp.setName(lead.getBPName());
    else
        bp.setName(lead.getName());
    bp.saveEx();
    addLog("Business Partner created.");
    lead.setC_BPartner_ID(bp.getC_BPartner_ID());
    if (lead.getC_Location_ID() != 0) {
        MLocation leadAddress = (MLocation) lead.getC_Location();
        MBPartnerLocation loc = new MBPartnerLocation(bp);
        MLocation address = new MLocation(getCtx(), 0, get_TrxName());
        PO.copyValues(leadAddress, address);
        address.saveEx();
        loc.setC_Location_ID(address.getC_Location_ID());
        loc.setPhone(lead.getPhone());
        loc.setPhone2(lead.getPhone2());
        loc.setFax(lead.getFax());
        loc.saveEx();
        lead.setC_BPartner_Location_ID(loc.getC_BPartner_Location_ID());
        addLog("Contact Location added.");
    }
    // company address
    if (lead.getBP_Location_ID() != 0) {
        MLocation leadAddress = (MLocation) lead.getBP_Location();
        MBPartnerLocation loc = new MBPartnerLocation(bp);
        MLocation address = new MLocation(getCtx(), 0, get_TrxName());
        PO.copyValues(leadAddress, address);
        address.saveEx();
        loc.setC_Location_ID(address.getC_Location_ID());
        loc.saveEx();
        addLog("BP Address added.");
    }
    if (p_createOpportunity) {
        MOpportunity op = new MOpportunity(getCtx(), 0, get_TrxName());
        op.setAD_User_ID(lead.getAD_User_ID());
        op.setC_BPartner_ID(bp.getC_BPartner_ID());
        op.setExpectedCloseDate(p_expectedCloseDate != null ? p_expectedCloseDate : new Timestamp(System.currentTimeMillis()));
        op.setOpportunityAmt(p_opportunityAmt != null ? p_opportunityAmt : Env.ZERO);
        if (p_C_SalesStage_ID > 0)
            op.setC_SalesStage_ID(p_C_SalesStage_ID);
        String sql = "SELECT Probability FROM C_SalesStage WHERE C_SalesStage_ID = ?";
        BigDecimal probability = DB.getSQLValueBD(get_TrxName(), sql, p_C_SalesStage_ID);
        op.setProbability(probability != null ? probability : Env.ZERO);
        op.setDescription(p_Description);
        if (p_C_Currency_ID > 0)
            op.setC_Currency_ID(p_C_Currency_ID);
        else
            op.setC_Currency_ID(Env.getContextAsInt(getCtx(), "$C_Currency_ID"));
        if (p_SalesRep_ID > 0)
            op.setSalesRep_ID(p_SalesRep_ID);
        else if (lead.getSalesRep_ID() > 0)
            op.setSalesRep_ID(lead.getSalesRep_ID());
        else
            op.setSalesRep_ID(Env.getContextAsInt(getCtx(), "#SalesRep_ID"));
        op.setC_Campaign_ID(lead.getC_Campaign_ID());
        op.saveEx();
        addLog("Opportunity created.");
    }
    lead.setIsSalesLead(false);
    lead.setLeadStatus(MUser.LEADSTATUS_Converted);
    lead.saveEx();
    addLog("Lead converted.");
    return "@OK@";
}
Also used : AdempiereUserError(org.compiere.util.AdempiereUserError) MOpportunity(org.compiere.model.MOpportunity) MBPartner(org.compiere.model.MBPartner) FillMandatoryException(org.adempiere.exceptions.FillMandatoryException) MUser(org.compiere.model.MUser) MLocation(org.compiere.model.MLocation) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) MBPartnerLocation(org.compiere.model.MBPartnerLocation)

Aggregations

BigDecimal (java.math.BigDecimal)1 Timestamp (java.sql.Timestamp)1 FillMandatoryException (org.adempiere.exceptions.FillMandatoryException)1 MBPartner (org.compiere.model.MBPartner)1 MBPartnerLocation (org.compiere.model.MBPartnerLocation)1 MLocation (org.compiere.model.MLocation)1 MOpportunity (org.compiere.model.MOpportunity)1 MUser (org.compiere.model.MUser)1 AdempiereUserError (org.compiere.util.AdempiereUserError)1