Search in sources :

Example 6 with MBPartnerLocation

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

the class HRCreateInvoice method createInvoice.

/**
     * Create invoice for employee
     * @param process
     * @param partner
     * @param employee
     * @param docTypeId
     * @return
     */
private MInvoice createInvoice(MHRProcess process, MBPartner partner, MHREmployee employee, int docTypeId, Timestamp dateInvoice) {
    MBPartnerLocation partnerLocation = getLocationBill(partner);
    if (partnerLocation == null) {
        log.log(Level.SEVERE, " @C_BPartner_Location_ID@ @NotFound@ : " + partner.getName());
        addLog(0, process.getDateAcct(), null, "@Bill_Location_ID@ @NotFound@ " + process.getDocumentNo() + " " + partner.getName());
        return null;
    }
    String paymentRule = employee.getPaymentRule();
    if (paymentRule == null || paymentRule.isEmpty())
        paymentRule = partner.getPaymentRule();
    if (paymentRule == null) {
        log.log(Level.SEVERE, " @PaymentRule@ @NotFound@ : " + partner.getName());
        addLog(0, process.getDateAcct(), null, "@PaymentRule@ @NotFound@ " + process.getDocumentNo() + " " + process.getName() + " " + partner.getValue() + " " + partner.getName());
        return null;
    }
    log.info("New Invoice for ");
    MInvoice invoice = new MInvoice(process.getCtx(), 0, partner.get_TrxName());
    invoice.setAD_Org_ID(process.getAD_Org_ID());
    invoice.setIsSOTrx(false);
    invoice.setPaymentRule(paymentRule);
    invoice.setC_DocTypeTarget_ID(docTypeId);
    invoice.setC_DocType_ID(docTypeId);
    invoice.setDescription(Msg.parseTranslation(process.getCtx(), "@HR_Process_ID@ " + process.getName() + " @DocumentNo@ " + process.getDocumentNo()));
    invoice.setDateOrdered(dateInvoice);
    invoice.setDateInvoiced(dateInvoice);
    invoice.setDateAcct(dateInvoice);
    invoice.setBPartner(partner);
    MPaymentTerm paymentTerm = MPaymentTerm.getPaymentTermByDefault(getCtx(), partner.get_TrxName());
    if (paymentTerm != null)
        invoice.setC_PaymentTerm_ID(paymentTerm.getC_PaymentTerm_ID());
    if (employee.getC_Activity_ID() != 0)
        invoice.setC_Activity_ID(employee.getC_Activity_ID());
    invoice.setDocStatus(DocAction.STATUS_Drafted);
    invoice.setDocAction(DocAction.ACTION_None);
    invoice.setSalesRep_ID(Env.getAD_User_ID(process.getCtx()));
    invoice.saveEx();
    addLog(0, invoice.getDateInvoiced(), invoice.getGrandTotal(), "@C_Invoice_ID@ " + invoice.getDocumentNo() + " @C_BPartner_ID@  @TaxId@ " + invoice.getC_BPartner().getValue() + " @Name@ " + invoice.getC_BPartner().getName());
    return invoice;
}
Also used : MPaymentTerm(org.compiere.model.MPaymentTerm) MInvoice(org.compiere.model.MInvoice) MBPartnerLocation(org.compiere.model.MBPartnerLocation)

Example 7 with MBPartnerLocation

use of org.compiere.model.MBPartnerLocation 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)

Example 8 with MBPartnerLocation

use of org.compiere.model.MBPartnerLocation 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)

Example 9 with MBPartnerLocation

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

the class InventoryUtil method getCreatePartner.

public static MBPartner getCreatePartner(String value) {
    Properties ctx = Env.getCtx();
    String whereClause = MBPartner.COLUMNNAME_Value + "=?";
    MBPartner bp = new Query(ctx, MBPartner.Table_Name, whereClause, null).setParameters(new Object[] { value }).setClient_ID().firstOnly();
    if (bp == null) {
        bp = new MBPartner(ctx, 0, null);
    }
    bp.setValue(value);
    bp.setName(value);
    setGeneratedTag(bp);
    bp.setIsCustomer(true);
    bp.setIsVendor(true);
    bp.setC_BP_Group_ID(MBPGroup.getDefault(ctx).get_ID());
    bp.saveEx();
    //
    if (bp.getLocations(false).length == 0) {
        MLocation loc = new MLocation(ctx, 0, null);
        loc.saveEx();
        //
        MBPartnerLocation bpl = new MBPartnerLocation(bp);
        bpl.setC_Location_ID(loc.get_ID());
        bpl.saveEx();
    }
    return bp;
}
Also used : Query(org.compiere.model.Query) MBPartner(org.compiere.model.MBPartner) Properties(java.util.Properties) MLocation(org.compiere.model.MLocation) MBPartnerLocation(org.compiere.model.MBPartnerLocation)

Example 10 with MBPartnerLocation

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

the class CalloutDistributionOrder method bPartner.

public String bPartner(Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value) {
    I_DD_Order order = GridTabWrapper.create(mTab, I_DD_Order.class);
    MOrg org = MOrg.get(ctx, order.getAD_Org_ID());
    int C_BPartner_ID = org.getLinkedC_BPartner_ID(null);
    if (C_BPartner_ID > 0) {
        MBPartnerLocation[] locations = MBPartnerLocation.getForBPartner(ctx, C_BPartner_ID, null);
        order.setC_BPartner_ID(C_BPartner_ID);
        if (locations.length > 0) {
            order.setC_BPartner_Location_ID(locations[0].getC_BPartner_Location_ID());
        }
    }
    return "";
}
Also used : MOrg(org.compiere.model.MOrg) MBPartnerLocation(org.compiere.model.MBPartnerLocation)

Aggregations

MBPartnerLocation (org.compiere.model.MBPartnerLocation)18 MBPartner (org.compiere.model.MBPartner)13 MLocation (org.compiere.model.MLocation)10 MUser (org.compiere.model.MUser)9 ResultSet (java.sql.ResultSet)6 BigDecimal (java.math.BigDecimal)5 PreparedStatement (java.sql.PreparedStatement)5 SQLException (java.sql.SQLException)4 Query (org.compiere.model.Query)4 Properties (java.util.Properties)3 Timestamp (java.sql.Timestamp)2 MInvoice (org.compiere.model.MInvoice)2 MLocator (org.compiere.model.MLocator)2 MOrderLine (org.compiere.model.MOrderLine)2 MOrg (org.compiere.model.MOrg)2 AdempiereUserError (org.compiere.util.AdempiereUserError)2 KeyNamePair (org.compiere.util.KeyNamePair)2 File (java.io.File)1 LocalDate (java.time.LocalDate)1 ZoneId (java.time.ZoneId)1