Search in sources :

Example 1 with MDistribution

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

the class Fact method distribute.

//	checkAccounts
/**
	 * 	GL Distribution of Fact Lines
	 *	@return true if success
	 */
public boolean distribute() {
    //  no lines -> nothing to distribute
    if (m_lines.size() == 0)
        return true;
    ArrayList<FactLine> newLines = new ArrayList<FactLine>();
    //	For all fact lines
    for (int i = 0; i < m_lines.size(); i++) {
        FactLine dLine = (FactLine) m_lines.get(i);
        MDistribution[] distributions = MDistribution.get(dLine.getAccount(), m_postingType, m_doc.getC_DocType_ID());
        //New:	
        if (distributions == null || distributions.length == 0) {
            distributions = MDistribution.get(dLine.getCtx(), dLine.getC_AcctSchema_ID(), m_postingType, m_doc.getC_DocType_ID(), dLine.getAD_Org_ID(), dLine.getAccount_ID(), dLine.getM_Product_ID(), dLine.getC_BPartner_ID(), dLine.getC_Project_ID(), dLine.getC_Campaign_ID(), dLine.getC_Activity_ID(), dLine.getAD_OrgTrx_ID(), dLine.getC_SalesRegion_ID(), dLine.getC_LocTo_ID(), dLine.getC_LocFrom_ID(), dLine.getUser1_ID(), dLine.getUser2_ID(), dLine.getUser3_ID(), dLine.getUser4_ID());
            if (distributions == null || distributions.length == 0)
                continue;
        }
        //	Just the first
        if (distributions.length > 1)
            log.warning("More then one Distributiion for " + dLine.getAccount());
        MDistribution distribution = distributions[0];
        // FR 2685367 - GL Distribution delete line instead reverse
        if (distribution.isCreateReversal()) {
            //	Add Reversal
            FactLine reversal = dLine.reverse(distribution.getName());
            log.info("Reversal=" + reversal);
            //	saved in postCommit
            newLines.add(reversal);
        } else {
            // delete the line being distributed
            // or it could be m_lines.remove(dLine);
            m_lines.remove(i);
            i--;
        }
        //	Prepare
        distribution.distribute(dLine.getAccount(), dLine.getSourceBalance(), dLine.getQty(), dLine.getC_Currency_ID());
        MDistributionLine[] lines = distribution.getLines(false);
        for (int j = 0; j < lines.length; j++) {
            MDistributionLine dl = lines[j];
            if (!dl.isActive() || dl.getAmt().signum() == 0)
                continue;
            FactLine factLine = new FactLine(m_doc.getCtx(), m_doc.get_Table_ID(), m_doc.get_ID(), 0, m_trxName);
            //  Set Info & Account
            factLine.setDocumentInfo(m_doc, dLine.getDocLine());
            factLine.setAccount(m_acctSchema, dl.getAccount());
            factLine.setPostingType(m_postingType);
            if (//	set Org explicitly
            dl.isOverwriteOrg())
                factLine.setAD_Org_ID(dl.getOrg_ID());
            // Silvano - freepath - F3P - Bug#2904994 Fact distribtution only overwriting Org
            if (dl.isOverwriteAcct())
                factLine.setAccount_ID(dl.getAccount_ID());
            if (dl.isOverwriteActivity())
                factLine.setC_Activity_ID(dl.getC_Activity_ID());
            if (dl.isOverwriteBPartner())
                factLine.setC_BPartner_ID(dl.getC_BPartner_ID());
            if (dl.isOverwriteCampaign())
                factLine.setC_Campaign_ID(dl.getC_Campaign_ID());
            if (dl.isOverwriteLocFrom())
                factLine.setC_LocFrom_ID(dl.getC_LocFrom_ID());
            if (dl.isOverwriteLocTo())
                factLine.setC_LocTo_ID(dl.getC_LocTo_ID());
            if (dl.isOverwriteOrgTrx())
                factLine.setAD_OrgTrx_ID(dl.getAD_OrgTrx_ID());
            if (dl.isOverwriteProduct())
                factLine.setM_Product_ID(dl.getM_Product_ID());
            if (dl.isOverwriteProject())
                factLine.setC_Project_ID(dl.getC_Project_ID());
            if (dl.isOverwriteSalesRegion())
                factLine.setC_SalesRegion_ID(dl.getC_SalesRegion_ID());
            if (dl.isOverwriteUser1())
                factLine.setUser1_ID(dl.getUser1_ID());
            if (dl.isOverwriteUser2())
                factLine.setUser2_ID(dl.getUser2_ID());
            if (dl.isOverwriteUser3())
                factLine.setUser3_ID(dl.getUser3_ID());
            if (dl.isOverwriteUser4())
                factLine.setUser4_ID(dl.getUser4_ID());
            //
            if (dl.getAmt().signum() < 0)
                factLine.setAmtSource(dLine.getC_Currency_ID(), null, dl.getAmt().abs());
            else
                factLine.setAmtSource(dLine.getC_Currency_ID(), dl.getAmt(), null);
            factLine.setQty(dl.getQty());
            //  Convert
            factLine.convert();
            //
            String description = distribution.getName() + " #" + dl.getLine();
            if (dl.getDescription() != null)
                description += " - " + dl.getDescription();
            factLine.addDescription(description);
            //
            log.info(factLine.toString());
            newLines.add(factLine);
        }
    }
    //	Add Lines
    for (int i = 0; i < newLines.size(); i++) m_lines.add(newLines.get(i));
    return true;
}
Also used : MDistributionLine(org.compiere.model.MDistributionLine) ArrayList(java.util.ArrayList) MDistribution(org.compiere.model.MDistribution)

Example 2 with MDistribution

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

the class DistributionVerify method doIt.

//	prepare
/**
	 * 	Process
	 *	@return message
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    log.info("doIt - GL_Distribution_ID=" + getRecord_ID());
    MDistribution distribution = new MDistribution(getCtx(), getRecord_ID(), get_TrxName());
    if (distribution.get_ID() == 0)
        throw new AdempiereUserError("Not found GL_Distribution_ID=" + getRecord_ID());
    String error = distribution.validate();
    boolean saved = distribution.save();
    if (error != null)
        throw new AdempiereUserError(error);
    if (!saved)
        throw new AdempiereSystemError("@NotSaved@");
    return "@OK@";
}
Also used : AdempiereUserError(org.compiere.util.AdempiereUserError) AdempiereSystemError(org.compiere.util.AdempiereSystemError) MDistribution(org.compiere.model.MDistribution)

Example 3 with MDistribution

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

the class MDistributionTest method testQuery.

public void testQuery() throws Exception {
    MDistribution dist = new MDistribution(getCtx(), 100, getTrxName());
    //red1 test with false no effect as mlines is null
    MDistributionLine[] dl = dist.getLines(true);
    assertTrue("Dist Lines exists", dl.length > 0);
}
Also used : MDistributionLine(org.compiere.model.MDistributionLine) MDistribution(org.compiere.model.MDistribution)

Aggregations

MDistribution (org.compiere.model.MDistribution)3 MDistributionLine (org.compiere.model.MDistributionLine)2 ArrayList (java.util.ArrayList)1 AdempiereSystemError (org.compiere.util.AdempiereSystemError)1 AdempiereUserError (org.compiere.util.AdempiereUserError)1