Skip to content

Commit

Permalink
Issue #306: refactored the method
Browse files Browse the repository at this point in the history
EPL-1238
  • Loading branch information
Amar-Etendo committed May 22, 2024
1 parent cc86d88 commit 3e081a8
Showing 1 changed file with 96 additions and 62 deletions.
158 changes: 96 additions & 62 deletions src/org/openbravo/erpCommon/ad_forms/DocInternalConsumption.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,9 @@ public BigDecimal getBalance() {
public Fact createFact(AcctSchema as, ConnectionProvider conn, Connection con,
VariablesSecureApp vars) throws ServletException {
// Select specific definition
String strClassname = AcctServerData.selectTemplateDoc(conn, as.m_C_AcctSchema_ID,
DocumentType);
if (StringUtils.isEmpty(strClassname)) {
strClassname = AcctServerData.selectTemplate(conn, as.m_C_AcctSchema_ID, AD_Table_ID);
}
String strClassname = getTemplateClassname(as, conn);
if (!StringUtils.isEmpty(strClassname)) {
try {
DocInternalConsumptionTemplate newTemplate = (DocInternalConsumptionTemplate) Class
.forName(strClassname)
.getDeclaredConstructor()
.newInstance();
return newTemplate.createFact(this, as, conn, con, vars);
} catch (Exception e) {
log4j.error("Error while creating new instance for DocInternalConsumptionTemplate - " + e);
}
return instantiateTemplateAndCreateFact(strClassname, as, conn, con, vars);
}

C_Currency_ID = as.getC_Currency_ID();
Expand All @@ -179,61 +167,107 @@ public Fact createFact(AcctSchema as, ConnectionProvider conn, Connection con,
FactLine dr = null;
FactLine cr = null;
log4jDocInternalConsumption.debug("CreateFact - before loop");
for (int i = 0; i < p_lines.length; i++) {
DocLine_Material line = (DocLine_Material) p_lines[i];
for (DocLine p_line : p_lines) {
DocLine_Material line = (DocLine_Material) p_line;

Currency costCurrency = FinancialUtils
.getLegalEntityCurrency(OBDal.getInstance().get(Organization.class, line.m_AD_Org_ID));
if (line.transaction != null && line.transaction.getCurrency() != null) {
costCurrency = line.transaction.getCurrency();
}
if (line.transaction != null && !line.transaction.isCostCalculated()) {
Map<String, String> parameters = getNotCalculatedCostParameters(line.transaction);
setMessageResult(conn, STATUS_NotCalculatedCost, "error", parameters);
throw new IllegalStateException();
}
String costs = line.getProductCosts(DateAcct, as, conn, con);
log4jDocInternalConsumption.debug("CreateFact - before DR - Costs: " + costs);
BigDecimal b_Costs = new BigDecimal(costs);
String strCosts = b_Costs.toPlainString();
Account cogsAccount = line.getAccount(ProductInfo.ACCTTYPE_P_Cogs, as, conn);
Product product = OBDal.getInstance().get(Product.class, line.m_M_Product_ID);
if (cogsAccount == null) {
org.openbravo.model.financialmgmt.accounting.coa.AcctSchema schema = OBDal.getInstance()
.get(org.openbravo.model.financialmgmt.accounting.coa.AcctSchema.class,
as.m_C_AcctSchema_ID);
log4j.error("No Account COGS for product: " + product.getName() + " in accounting schema: "
+ schema.getName());
}
Account assetAccount = line.getAccount(ProductInfo.ACCTTYPE_P_Asset, as, conn);
if (assetAccount == null) {
org.openbravo.model.financialmgmt.accounting.coa.AcctSchema schema = OBDal.getInstance()
.get(org.openbravo.model.financialmgmt.accounting.coa.AcctSchema.class,
as.m_C_AcctSchema_ID);
log4j.error("No Account Asset for product: " + product.getName() + " in accounting schema: "
+ schema.getName());
}
dr = fact.createLine(line, cogsAccount, costCurrency.getId(), strCosts, "",
Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
if (dr != null) {
dr.setM_Locator_ID(line.m_M_Locator_ID);
dr.setLocationFromLocator(line.m_M_Locator_ID, true, conn); // from
dr.setLocationFromBPartner(C_BPartner_Location_ID, false, conn); // to
}
log4jDocInternalConsumption.debug("CreateFact - before CR");
cr = fact.createLine(line, assetAccount, costCurrency.getId(), "", strCosts,
Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
if (cr != null) {
cr.setM_Locator_ID(line.m_M_Locator_ID);
cr.setLocationFromLocator(line.m_M_Locator_ID, true, conn); // from
cr.setLocationFromBPartner(C_BPartner_Location_ID, false, conn); // to
}
Currency costCurrency = determineCostCurrency(line);
validateCostCalculation(line, conn);

String strCosts = getCostsString(line, as, conn, con);
createFactLines(as, fact, line, costCurrency, strCosts, Fact_Acct_Group_ID, conn);
}
log4jDocInternalConsumption.debug("CreateFact - after loop");
SeqNo = "0";
return fact;
} // createFact

private String getTemplateClassname(AcctSchema as, ConnectionProvider conn) throws ServletException {
String strClassname = AcctServerData.selectTemplateDoc(conn, as.m_C_AcctSchema_ID, DocumentType);
if (StringUtils.isEmpty(strClassname)) {
strClassname = AcctServerData.selectTemplate(conn, as.m_C_AcctSchema_ID, AD_Table_ID);
}
return strClassname;
}

private Fact instantiateTemplateAndCreateFact(String strClassname, AcctSchema as, ConnectionProvider conn,
Connection con, VariablesSecureApp vars) throws ServletException {
try {
DocInternalConsumptionTemplate newTemplate = (DocInternalConsumptionTemplate) Class.forName(strClassname)
.getDeclaredConstructor().newInstance();
return newTemplate.createFact(this, as, conn, con, vars);
} catch (Exception e) {
log4j.error("Error while creating new instance for DocInternalConsumptionTemplate - " + e);
throw new ServletException(e);
}
}

private DocLine_Material[] getDocLines() {
return (DocLine_Material[]) p_lines;
}

private Currency determineCostCurrency(DocLine_Material line) {
Currency costCurrency = FinancialUtils.getLegalEntityCurrency(
OBDal.getInstance().get(Organization.class, line.m_AD_Org_ID));
if (line.transaction != null && line.transaction.getCurrency() != null) {
costCurrency = line.transaction.getCurrency();
}
return costCurrency;
}

private void validateCostCalculation(DocLine_Material line, ConnectionProvider conn) {
if (line.transaction != null && !line.transaction.isCostCalculated()) {
Map<String, String> parameters = getNotCalculatedCostParameters(line.transaction);
setMessageResult(conn, STATUS_NotCalculatedCost, "error", parameters);
throw new IllegalStateException();
}
}

private String getCostsString(DocLine_Material line, AcctSchema as, ConnectionProvider conn,
Connection con) throws ServletException {
String costs = line.getProductCosts(DateAcct, as, conn, con);
log4jDocInternalConsumption.debug("CreateFact - before DR - Costs: " + costs);
BigDecimal b_Costs = new BigDecimal(costs);
return b_Costs.toPlainString();
}

private void createFactLines(AcctSchema as, Fact fact, DocLine_Material line, Currency costCurrency, String strCosts,
String Fact_Acct_Group_ID, ConnectionProvider conn) throws ServletException {
Account cogsAccount = getAccount(line, ProductInfo.ACCTTYPE_P_Cogs, as, conn, "COGS");
Account assetAccount = getAccount(line, ProductInfo.ACCTTYPE_P_Asset, as, conn, "Asset");

FactLine dr = fact.createLine(line, cogsAccount, costCurrency.getId(), strCosts, "", Fact_Acct_Group_ID,
nextSeqNo(SeqNo), DocumentType, conn);
if (dr != null) {
setFactLineLocations(dr, line, conn);
}

FactLine cr = fact.createLine(line, assetAccount, costCurrency.getId(), "", strCosts, Fact_Acct_Group_ID,
nextSeqNo(SeqNo), DocumentType, conn);
if (cr != null) {
setFactLineLocations(cr, line, conn);
}
}

private Account getAccount(DocLine_Material line, String acctType, AcctSchema as, ConnectionProvider conn,
String accountTypeName) throws ServletException {
Account account = line.getAccount(acctType, as, conn);
if (account == null) {
org.openbravo.model.financialmgmt.accounting.coa.AcctSchema schema = OBDal.getInstance()
.get(org.openbravo.model.financialmgmt.accounting.coa.AcctSchema.class, as.m_C_AcctSchema_ID);
Product product = OBDal.getInstance().get(Product.class, line.m_M_Product_ID);
log4j.error(
"No Account " + accountTypeName + " for product: " + product.getName() + " in accounting schema: " + schema.getName());
}
return account;
}

private void setFactLineLocations(FactLine factLine, DocLine_Material line,
ConnectionProvider conn) throws ServletException {
factLine.setM_Locator_ID(line.m_M_Locator_ID);
factLine.setLocationFromLocator(line.m_M_Locator_ID, true, conn); // from
factLine.setLocationFromBPartner(C_BPartner_Location_ID, false, conn); // to
}

/**
* Get Document Confirmation
*
Expand Down

0 comments on commit 3e081a8

Please sign in to comment.