IDEMPIERE-1695 Errors on Invoice Not Realized Gain/Loss

This commit is contained in:
Carlos Ruiz 2015-01-09 13:33:39 -05:00
parent f4f317c3ad
commit c8f3a94f45
5 changed files with 85 additions and 321 deletions

View File

@ -1,137 +0,0 @@
DROP VIEW t_invoicegl_vt;
CREATE OR REPLACE VIEW t_invoicegl_vt
AS
SELECT i.ad_client_id,
i.ad_org_id,
i.isactive,
i.created,
i.createdby,
i.updated,
i.updatedby,
i.c_invoice_id,
i.issotrx,
i.documentno,
i.docstatus,
i.c_doctype_id,
i.c_order_id,
i.description,
i.salesrep_id,
i.dateinvoiced,
i.dateacct,
i.c_paymentterm_id,
i.c_bpartner_id,
i.c_bpartner_location_id,
i.ad_user_id,
i.isselfservice,
i.c_currency_id,
i.c_conversiontype_id,
i.grandtotal,
i.istaxincluded,
i.c_campaign_id,
i.c_project_id,
i.c_activity_id,
i.ad_orgtrx_id,
i.user1_id,
i.user2_id,
fa.c_locfrom_id,
fa.c_locto_id,
fa.c_salesregion_id,
fa.fact_acct_id,
fa.c_acctschema_id,
fa.account_id,
fa.c_period_id,
fa.gl_category_id,
fa.gl_budget_id,
fa.c_tax_id,
fa.m_locator_id,
fa.postingtype,
fa.amtsourcedr,
fa.amtsourcecr,
fa.amtacctdr,
fa.amtacctcr,
fa.c_uom_id,
fa.qty,
gl.ad_pinstance_id,
gl.apar,
gl.openamt,
gl.percent,
gl.amtrevaldr,
gl.amtrevalcr,
gl.datereval,
gl.c_conversiontypereval_id,
gl.amtsourcebalance,
gl.amtacctbalance,
gl.c_doctypereval_id,
gl.amtrevaldrdiff,
gl.amtrevalcrdiff,
gl.isallcurrencies,
fa.amtacctdr * gl.percent / 100 AS amtacctopendr,
fa.amtacctcr * gl.percent / 100 AS amtacctopencr,
( fa.amtacctdr - fa.amtacctcr ) * gl.percent / 100 AS amtacctopenbalance,
gl.ad_org_id AS t_invoicegl_ad_org_id,
gl.c_invoice_id AS t_invoicegl_c_invoice_id,
gl.created AS t_invoicegl_created,
gl.createdby AS t_invoicegl_createdby,
gl.grandtotal AS t_invoicegl_grandtotal,
gl.isactive AS t_invoicegl_isactive,
gl.updated AS t_invoicegl_updated,
gl.updatedby AS t_invoicegl_updatedby,
i.c_charge_id AS c_invoice_c_charge_id,
i.c_doctypetarget_id,
i.c_dunninglevel_id,
i.chargeamt,
i.c_payment_id,
i.dateordered,
i.dateprinted,
i.docaction,
i.dunninggrace,
i.generateto,
i.invoicecollectiontype,
i.isapproved,
i.isdiscountprinted,
i.isindispute,
i.ispaid,
i.ispayschedulevalid,
i.isprinted,
i.istransferred,
i.m_pricelist_id,
i.m_rma_id,
i.paymentrule,
i.poreference,
i.posted,
i.processedon,
i.processing,
i.ref_invoice_id,
i.reversal_id,
i.sendemail,
i.totallines,
fa.a_asset_id,
fa.ad_org_id AS fact_acct_ad_org_id,
fa.ad_orgtrx_id AS fact_acct_ad_orgtrx_id,
fa.ad_table_id AS fact_acct_ad_table_id,
fa.c_activity_id AS fact_acct_c_activity_id,
fa.c_bpartner_id AS fact_acct_c_bpartner_id,
fa.c_campaign_id AS fact_acct_c_campaign_id,
fa.c_currency_id AS fact_acct_c_currency_id,
fa.c_subacct_id,
fa.dateacct AS fact_acct_dateacct,
fa.datetrx,
fa.description AS fact_acct_description,
fa.isactive AS fact_acct_isactive,
fa.line_id,
fa.m_product_id AS fact_acct_m_product_id,
fa.record_id,
fa.updated AS fact_acct_updated,
fa.updatedby AS fact_acct_updatedby,
fa.user1_id AS fact_acct_user1_id,
fa.user2_id AS fact_acct_user2_id,
fa.userelement1_id,
fa.userelement2_id
FROM t_invoicegl gl
JOIN c_invoice i
ON gl.c_invoice_id = i.c_invoice_id
JOIN fact_acct fa
ON gl.fact_acct_id = fa.fact_acct_id
;

View File

@ -1,135 +0,0 @@
DROP VIEW t_invoicegl_vt;
CREATE OR REPLACE VIEW t_invoicegl_vt AS
SELECT i.ad_client_id,
i.ad_org_id,
i.isactive,
i.created,
i.createdby,
i.updated,
i.updatedby,
i.c_invoice_id,
i.issotrx,
i.documentno,
i.docstatus,
i.c_doctype_id,
i.c_order_id,
i.description,
i.salesrep_id,
i.dateinvoiced,
i.dateacct,
i.c_paymentterm_id,
i.c_bpartner_id,
i.c_bpartner_location_id,
i.ad_user_id,
i.isselfservice,
i.c_currency_id,
i.c_conversiontype_id,
i.grandtotal,
i.istaxincluded,
i.c_campaign_id,
i.c_project_id,
i.c_activity_id,
i.ad_orgtrx_id,
i.user1_id,
i.user2_id,
fa.c_locfrom_id,
fa.c_locto_id,
fa.c_salesregion_id,
fa.fact_acct_id,
fa.c_acctschema_id,
fa.account_id,
fa.c_period_id,
fa.gl_category_id,
fa.gl_budget_id,
fa.c_tax_id,
fa.m_locator_id,
fa.postingtype,
fa.amtsourcedr,
fa.amtsourcecr,
fa.amtacctdr,
fa.amtacctcr,
fa.c_uom_id,
fa.qty,
gl.ad_pinstance_id,
gl.apar,
gl.openamt,
gl.percent,
gl.amtrevaldr,
gl.amtrevalcr,
gl.datereval,
gl.c_conversiontypereval_id,
gl.amtsourcebalance,
gl.amtacctbalance,
gl.c_doctypereval_id,
gl.amtrevaldrdiff,
gl.amtrevalcrdiff,
gl.isallcurrencies,
fa.amtacctdr * gl.percent / 100 AS amtacctopendr,
fa.amtacctcr * gl.percent / 100 AS amtacctopencr,
(fa.amtacctdr - fa.amtacctcr) * gl.percent / 100 AS amtacctopenbalance,
gl.ad_org_id AS t_invoicegl_ad_org_id,
gl.c_invoice_id AS t_invoicegl_c_invoice_id,
gl.created AS t_invoicegl_created,
gl.createdby AS t_invoicegl_createdby,
gl.grandtotal AS t_invoicegl_grandtotal,
gl.isactive AS t_invoicegl_isactive,
gl.updated AS t_invoicegl_updated,
gl.updatedby AS t_invoicegl_updatedby,
i.c_charge_id AS c_invoice_c_charge_id,
i.c_doctypetarget_id,
i.c_dunninglevel_id,
i.chargeamt,
i.c_payment_id,
i.dateordered,
i.dateprinted,
i.docaction,
i.dunninggrace,
i.generateto,
i.invoicecollectiontype,
i.isapproved,
i.isdiscountprinted,
i.isindispute,
i.ispaid,
i.ispayschedulevalid,
i.isprinted,
i.istransferred,
i.m_pricelist_id,
i.m_rma_id,
i.paymentrule,
i.poreference,
i.posted,
i.processedon,
i.processing,
i.ref_invoice_id,
i.reversal_id,
i.sendemail,
i.totallines,
fa.a_asset_id,
fa.ad_org_id AS fact_acct_ad_org_id,
fa.ad_orgtrx_id AS fact_acct_ad_orgtrx_id,
fa.ad_table_id AS fact_acct_ad_table_id,
fa.c_activity_id AS fact_acct_c_activity_id,
fa.c_bpartner_id AS fact_acct_c_bpartner_id,
fa.c_campaign_id AS fact_acct_c_campaign_id,
fa.c_currency_id AS fact_acct_c_currency_id,
fa.c_subacct_id,
fa.dateacct AS fact_acct_dateacct,
fa.datetrx,
fa.description AS fact_acct_description,
fa.isactive AS fact_acct_isactive,
fa.line_id,
fa.m_product_id AS fact_acct_m_product_id,
fa.record_id,
fa.updated AS fact_acct_updated,
fa.updatedby AS fact_acct_updatedby,
fa.user1_id AS fact_acct_user1_id,
fa.user2_id AS fact_acct_user2_id,
fa.userelement1_id,
fa.userelement2_id
FROM t_invoicegl gl
JOIN c_invoice i
ON gl.c_invoice_id = i.c_invoice_id
JOIN fact_acct fa
ON gl.fact_acct_id = fa.fact_acct_id
;

View File

@ -0,0 +1,6 @@
drop view t_invoicegl_vt
;
SELECT register_migration_script('201501091326_IDEMPIERE-1695.sql') FROM dual
;

View File

@ -0,0 +1,6 @@
drop view t_invoicegl_vt
;
SELECT register_migration_script('201501091326_IDEMPIERE-1695.sql') FROM dual
;

View File

@ -29,7 +29,6 @@ import org.compiere.model.MFactAcct;
import org.compiere.model.MGLCategory;
import org.compiere.model.MInvoice;
import org.compiere.model.MJournal;
import org.compiere.model.MJournalBatch;
import org.compiere.model.MJournalLine;
import org.compiere.model.MOrg;
import org.compiere.model.Query;
@ -147,7 +146,7 @@ public class InvoiceNGL extends SvrProcess
.append(" AND EXISTS (SELECT * FROM C_ElementValue ev ")
.append("WHERE ev.C_ElementValue_ID=fa.Account_ID AND (ev.AccountType='A' OR ev.AccountType='L'))")
.append(" AND fa.C_AcctSchema_ID=").append(p_C_AcctSchema_ID);
if (!p_IsAllCurrencies)
if (p_IsAllCurrencies)
sql.append(" AND i.C_Currency_ID<>a.C_Currency_ID");
if (ONLY_AR.equals(p_APAR))
sql.append(" AND i.IsSOTrx='Y'");
@ -238,19 +237,23 @@ public class InvoiceNGL extends SvrProcess
cat = MGLCategory.get(getCtx(), docType.getGL_Category_ID());
}
//
MJournalBatch batch = new MJournalBatch(getCtx(), 0, get_TrxName());
batch.setDescription (getName());
batch.setC_DocType_ID(p_C_DocTypeReval_ID);
batch.setDateDoc(new Timestamp(System.currentTimeMillis()));
batch.setDateAcct(p_DateReval);
batch.setC_Currency_ID(as.getC_Currency_ID());
if (!batch.save())
return " - Could not create Batch";
MJournal journal = new MJournal (getCtx(), 0, get_TrxName());
journal.setC_DocType_ID(p_C_DocTypeReval_ID);
journal.setPostingType(MJournal.POSTINGTYPE_Actual);
journal.setDateDoc(new Timestamp(System.currentTimeMillis()));
journal.setDateAcct(p_DateReval); // sets the period too
journal.setC_Currency_ID(as.getC_Currency_ID());
journal.setC_AcctSchema_ID (as.getC_AcctSchema_ID());
journal.setC_ConversionType_ID(p_C_ConversionTypeReval_ID);
journal.setGL_Category_ID (cat.getGL_Category_ID());
journal.setDescription(getName()); // updated below
if (!journal.save())
return " - Could not create Journal";
//
MJournal journal = null;
BigDecimal drTotal = Env.ZERO;
BigDecimal crTotal = Env.ZERO;
BigDecimal gainTotal = Env.ZERO;
BigDecimal lossTotal = Env.ZERO;
int AD_Org_ID = 0;
MOrg org = null;
for (int i = 0; i < list.size(); i++)
{
X_T_InvoiceGL gl = list.get(i);
@ -265,25 +268,19 @@ public class InvoiceNGL extends SvrProcess
// Change in Org
if (AD_Org_ID != gl.getAD_Org_ID())
{
createBalancing (asDefaultAccts, journal, drTotal, crTotal, AD_Org_ID, (i+1) * 10);
createBalancing (asDefaultAccts, journal, gainTotal, lossTotal, AD_Org_ID, (i+1) * 10);
//
AD_Org_ID = gl.getAD_Org_ID();
drTotal = Env.ZERO;
crTotal = Env.ZERO;
gainTotal = Env.ZERO;
lossTotal = Env.ZERO;
journal = null;
}
//
if (journal == null)
{
journal = new MJournal (batch);
journal.setC_AcctSchema_ID (as.getC_AcctSchema_ID());
journal.setC_Currency_ID(as.getC_Currency_ID());
journal.setC_ConversionType_ID(p_C_ConversionTypeReval_ID);
MOrg org = MOrg.get(getCtx(), gl.getAD_Org_ID());
if (org == null) {
org = MOrg.get(getCtx(), gl.getAD_Org_ID());
journal.setDescription (getName() + " - " + org.getName());
journal.setGL_Category_ID (cat.getGL_Category_ID());
if (!journal.save())
return " - Could not create Journal";
return " - Could not set Description for Journal";
}
//
MJournalLine line = new MJournalLine(journal);
@ -291,39 +288,66 @@ public class InvoiceNGL extends SvrProcess
line.setDescription(invoice.getSummary());
//
MFactAcct fa = new MFactAcct (getCtx(), gl.getFact_Acct_ID(), null);
line.setC_ValidCombination_ID(MAccount.get(fa));
MAccount acct = MAccount.get(fa);
line.setC_ValidCombination_ID(acct);
BigDecimal dr = gl.getAmtRevalDrDiff();
BigDecimal cr = gl.getAmtRevalCrDiff();
drTotal = drTotal.add(dr);
crTotal = crTotal.add(cr);
// Check if acct.IsActiva to differentiate gain and loss ->
// acct.isActiva negative dr or positive cr -> loss
// acct.isActiva positive dr or negative cr -> gain
// acct.isPassiva negative cr or positive dr -> gain
// acct.isPassiva positive cr or negative dr -> loss
if (acct.isActiva()) {
if (dr.signum() < 0) {
lossTotal = lossTotal.add(dr.negate());
} else if (dr.signum() > 0) {
gainTotal = gainTotal.add(dr);
}
if (cr.signum() > 0) {
lossTotal = lossTotal.add(cr);
} if (cr.signum() < 0) {
gainTotal = gainTotal.add(cr.negate());
}
} else { // isPassiva
if (cr.signum() < 0) {
gainTotal = gainTotal.add(cr.negate());
} else if (cr.signum() > 0) {
lossTotal = lossTotal.add(cr);
}
if (dr.signum() > 0) {
gainTotal = gainTotal.add(dr);
} else if (dr.signum() < 0) {
lossTotal = lossTotal.add(dr.negate());
}
}
line.setAmtSourceDr (dr);
line.setAmtAcctDr (dr);
line.setAmtSourceCr (cr);
line.setAmtAcctCr (cr);
line.saveEx();
}
createBalancing (asDefaultAccts, journal, drTotal, crTotal, AD_Org_ID, (list.size()+1) * 10);
createBalancing (asDefaultAccts, journal, gainTotal, lossTotal, AD_Org_ID, (list.size()+1) * 10);
StringBuilder msgreturn = new StringBuilder(" - ").append(batch.getDocumentNo()).append(" #").append(list.size());
return msgreturn.toString();
StringBuilder msgreturn = new StringBuilder(" - ").append(journal.getDocumentNo()).append(" #").append(list.size());
addLog(journal.getGL_Journal_ID(), null, null, msgreturn.toString(), MJournal.Table_ID, journal.getGL_Journal_ID());
return "OK";
} // createGLJournal
/**
* Create Balancing Entry
* @param asDefaultAccts acct schema default accounts
* @param journal journal
* @param drTotal dr
* @param crTotal cr
* @param gainTotal dr
* @param lossTotal cr
* @param AD_Org_ID org
* @param lineNo base line no
*/
private void createBalancing (MAcctSchemaDefault asDefaultAccts, MJournal journal,
BigDecimal drTotal, BigDecimal crTotal, int AD_Org_ID, int lineNo)
BigDecimal gainTotal, BigDecimal lossTotal, int AD_Org_ID, int lineNo)
{
if (journal == null)
throw new IllegalArgumentException("Jornal is null");
// CR Entry = Gain
if (drTotal.signum() != 0)
if (gainTotal.signum() != 0)
{
MJournalLine line = new MJournalLine(journal);
line.setLine(lineNo+1);
@ -337,12 +361,12 @@ public class InvoiceNGL extends SvrProcess
get_TrxName());
line.setDescription(Msg.getElement(getCtx(), "UnrealizedGain_Acct"));
line.setC_ValidCombination_ID(acct.getC_ValidCombination_ID());
line.setAmtSourceCr (drTotal);
line.setAmtAcctCr (drTotal);
line.setAmtSourceCr (gainTotal);
line.setAmtAcctCr (gainTotal);
line.saveEx();
}
// DR Entry = Loss
if (crTotal.signum() != 0)
if (lossTotal.signum() != 0)
{
MJournalLine line = new MJournalLine(journal);
line.setLine(lineNo+2);
@ -356,8 +380,8 @@ public class InvoiceNGL extends SvrProcess
get_TrxName());
line.setDescription(Msg.getElement(getCtx(), "UnrealizedLoss_Acct"));
line.setC_ValidCombination_ID(acct.getC_ValidCombination_ID());
line.setAmtSourceDr (crTotal);
line.setAmtAcctDr (crTotal);
line.setAmtSourceDr (lossTotal);
line.setAmtAcctDr (lossTotal);
line.saveEx();
}
} // createBalancing