How to create a new document with specific accounting

From ADempiere
Revision as of 14:09, 29 March 2007 by Rvergara (Talk)

Jump to: navigation, search
This Wiki is read-only for reference purposes to avoid broken links.

In order to generate a new document within ADempiere that has a specific accounting to be generated upon completion of the document, it is necessary to complete the following 3 steps. For this example it is assumed that the new document behaves similarly to an invoice but generates different accounting transactions:


1. Create a new Base Document using the system/system login. In C_DocType DocBaseType we need a 3 capital letter code that identifies our new document base. In this example we will use PTK, then:

search key:PTK name:DOCTYPE_PTCheck


2. In the java class Doc.java, ADempiere defines the documents supported by the application. We need to add the following statement:

public static final String DOCTYPE_NInvoice = "PTK";


3. Every document table has a class that defines how the accounting transactions are generated. For instance the tables C_Order and C_Invoice have the corresponding and related classes Doc_Order.java and DocInvoice.java. Within these classes you will find a method named "createFacts". This method contains the logic for the document accounting generation.

In reviewing the method you will find the following structure for each document:

if (getDocumentType().equals("A_Type_of_Document") {

} else if (getDocumentType().equals("Another_Type_Of_Document") ) {

}

In order to add accounting for our newly added document (PTCheck) we need to add the following:


{else if (getDocumentType().equals(DOCTYPE_PTCheck)) //PTK {

  BigDecimal grossAmt = getAmount(Doc.AMTTYPE_Gross);
  BigDecimal serviceAmt = Env.ZERO;
  
  //  Header Charge           CR
  BigDecimal amt = getAmount(Doc.AMTTYPE_Charge);
  if (amt != null && amt.signum() != 0)
   fact.createLine(null, getAccount(Doc.ACCTTYPE_Charge, as),
    getC_Currency_ID(), null, amt);
  //  TaxDue                  CR
  for (int i = 0; i < m_taxes.length; i++)
  {
   amt = m_taxes[i].getAmount();
   if (amt != null && amt.signum() != 0)
   {
    FactLine tl = fact.createLine(null, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxDue, as),
     getC_Currency_ID(), null, amt);
    if (tl != null)
     tl.setC_Tax_ID(m_taxes[i].getC_Tax_ID());
   }
  }
  //  Revenue                 CR
  for (int i = 0; i < p_lines.length; i++)
  {
   amt = p_lines[i].getAmtSource();
   BigDecimal dAmt = null;
   if (as.isTradeDiscountPosted())
   {
    BigDecimal discount = p_lines[i].getDiscount();
    if (discount != null && discount.signum() != 0)
    {
     amt = amt.add(discount);
     dAmt = discount;
    }
   }
   fact.createLine (p_lines[i],
    p_lines[i].getAccount(ProductCost.ACCTTYPE_P_Revenue, as),
    getC_Currency_ID(), dAmt, amt);
   if (!p_lines[i].isItem())
   {
    grossAmt = grossAmt.subtract(amt);
    serviceAmt = serviceAmt.add(amt);
   }
  }
  //  Set Locations
  FactLine[] fLines = fact.getLines();
  for (int i = 0; i < fLines.length ; i++)
  {
   if (fLines[i] != null)
   {
    fLines[i].setLocationFromOrg(fLines[i].getAD_Org_ID(), true);      //  from Loc
    fLines[i].setLocationFromBPartner(getC_BPartner_Location_ID(), false);  //  to Loc
   }
  }
  
  //  Receivables     DR
  int receivables_ID = getValidCombination_ID(Doc.ACCTTYPE_BankAsset, as);
  int receivablesServices_ID = getValidCombination_ID (Doc.ACCTTYPE_BankAsset, as);
  if (m_allLinesItem || !as.isPostServices()
   || receivables_ID == receivablesServices_ID)
  {
   grossAmt = getAmount(Doc.AMTTYPE_Gross);
   serviceAmt = Env.ZERO;
  }
  else if (m_allLinesService)
  {
   serviceAmt = getAmount(Doc.AMTTYPE_Gross);
   grossAmt = Env.ZERO;
  }
  if (grossAmt.signum() != 0)
   fact.createLine(null, MAccount.get(getCtx(), receivables_ID),
    getC_Currency_ID(), grossAmt, null);
  if (serviceAmt.signum() != 0)
   fact.createLine(null, MAccount.get(getCtx(), receivablesServices_ID),
    getC_Currency_ID(), serviceAmt, null);

} } The Revenue CR comment marks the code that searches the Credit account and the Receivables DR comment marks the code that searches the Debit account.

The fact.createLine statement uses a format like this:

fact.createLine (p_lines[i],p_lines[i].getAccount(ProductCost.ACCTTYPE_P_Revenue, as),getC_Currency_ID(), dAmt, amt);


where p_lines[i].getAccount(ProductCost.ACCTTYPE_P_Revenue, as) is the method that searches the account needed for posting. This method, in turn, calls the getValidCombination_ID method in the class doc.java.