/**
  * Create Balancing Entry
  *
  * @param asDefaultAccts acct schema default accounts
  * @param journal journal
  * @param drTotal dr
  * @param crTotal 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) {
   if (journal == null) throw new IllegalArgumentException("Jornal is null");
   //		CR Entry = Gain
   if (drTotal.signum() != 0) {
     MJournalLine line = new MJournalLine(journal);
     line.setLine(lineNo + 1);
     MAccount base = MAccount.get(getCtx(), asDefaultAccts.getUnrealizedGain_Acct());
     MAccount acct =
         MAccount.get(
             getCtx(),
             asDefaultAccts.getAD_Client_ID(),
             AD_Org_ID,
             asDefaultAccts.getC_AcctSchema_ID(),
             base.getAccount_ID(),
             base.getC_SubAcct_ID(),
             base.getM_Product_ID(),
             base.getC_BPartner_ID(),
             base.getAD_OrgTrx_ID(),
             base.getC_LocFrom_ID(),
             base.getC_LocTo_ID(),
             base.getC_SalesRegion_ID(),
             base.getC_Project_ID(),
             base.getC_Campaign_ID(),
             base.getC_Activity_ID(),
             base.getUser1_ID(),
             base.getUser2_ID(),
             base.getUserElement1_ID(),
             base.getUserElement2_ID());
     line.setDescription(Msg.getElement(getCtx(), "UnrealizedGain_Acct"));
     // TODO: C_ValidCombination_ID is no longer a column because we have DR/CR accounts
     // line.setC_ValidCombination_ID(acct.getC_ValidCombination_ID());
     line.setAmtSourceCr(drTotal);
     line.setAmtAcctCr(drTotal);
     line.save();
   }
   //	DR Entry = Loss
   if (crTotal.signum() != 0) {
     MJournalLine line = new MJournalLine(journal);
     line.setLine(lineNo + 2);
     MAccount base = MAccount.get(getCtx(), asDefaultAccts.getUnrealizedLoss_Acct());
     MAccount acct =
         MAccount.get(
             getCtx(),
             asDefaultAccts.getAD_Client_ID(),
             AD_Org_ID,
             asDefaultAccts.getC_AcctSchema_ID(),
             base.getAccount_ID(),
             base.getC_SubAcct_ID(),
             base.getM_Product_ID(),
             base.getC_BPartner_ID(),
             base.getAD_OrgTrx_ID(),
             base.getC_LocFrom_ID(),
             base.getC_LocTo_ID(),
             base.getC_SalesRegion_ID(),
             base.getC_Project_ID(),
             base.getC_Campaign_ID(),
             base.getC_Activity_ID(),
             base.getUser1_ID(),
             base.getUser2_ID(),
             base.getUserElement1_ID(),
             base.getUserElement2_ID());
     line.setDescription(Msg.getElement(getCtx(), "UnrealizedLoss_Acct"));
     // TODO: C_ValidCombination_ID is no longer a column because we have DR/CR accounts
     // line.setC_ValidCombination_ID(acct.getC_ValidCombination_ID());
     line.setAmtSourceDr(crTotal);
     line.setAmtAcctDr(crTotal);
     line.save();
   }
 } //	createBalancing
  /**
   * Create GL Journal
   *
   * @return document info
   */
  private String createGLJournal() {
    // FR: [ 2214883 ] Remove SQL code and Replace for Query
    String whereClause = "AD_PInstance_ID=?";
    List<X_T_InvoiceGL> list =
        new Query(getCtx(), X_T_InvoiceGL.Table_Name, whereClause, get_TrxName())
            .setParameters(new Object[] {getAD_PInstance_ID()})
            .setOrderBy("AD_Org_ID")
            .list();
    // FR: [ 2214883 ] Remove SQL code and Replace for Query

    if (list.size() == 0) return " - No Records found";

    //
    MAcctSchema as = MAcctSchema.get(getCtx(), p_C_AcctSchema_ID);
    MAcctSchemaDefault asDefaultAccts = MAcctSchemaDefault.get(getCtx(), p_C_AcctSchema_ID);
    MGLCategory cat = MGLCategory.getDefaultSystem(getCtx());
    if (cat == null) {
      MDocType docType = MDocType.get(getCtx(), p_C_DocTypeReval_ID);
      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 = null;
    BigDecimal drTotal = Env.ZERO;
    BigDecimal crTotal = Env.ZERO;
    int AD_Org_ID = 0;
    for (int i = 0; i < list.size(); i++) {
      X_T_InvoiceGL gl = list.get(i);
      if (gl.getAmtRevalDrDiff().signum() == 0 && gl.getAmtRevalCrDiff().signum() == 0) continue;
      MInvoice invoice = new MInvoice(getCtx(), gl.getC_Invoice_ID(), null);
      if (invoice.getC_Currency_ID() == as.getC_Currency_ID()) continue;
      //
      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());
        journal.setDescription(getName() + " - " + org.getName());
        journal.setGL_Category_ID(cat.getGL_Category_ID());
        if (!journal.save()) return " - Could not create Journal";
      }
      //
      MJournalLine line = new MJournalLine(journal);
      line.setLine((i + 1) * 10);
      line.setDescription(invoice.getSummary());
      //
      MFactAcct fa = new MFactAcct(getCtx(), gl.getFact_Acct_ID(), null);
      // TODO: C_ValidCombination_ID is no longer a column because we have DR/CR accounts
      // line.setC_ValidCombination(MAccount.get(fa));
      BigDecimal dr = gl.getAmtRevalDrDiff();
      BigDecimal cr = gl.getAmtRevalCrDiff();
      drTotal = drTotal.add(dr);
      crTotal = crTotal.add(cr);
      line.setAmtSourceDr(dr);
      line.setAmtAcctDr(dr);
      line.setAmtSourceCr(cr);
      line.setAmtAcctCr(cr);
      line.save();
      //
      if (AD_Org_ID == 0) // 	invoice org id
      AD_Org_ID = gl.getAD_Org_ID();
      //	Change in Org
      if (AD_Org_ID != gl.getAD_Org_ID()) {
        createBalancing(asDefaultAccts, journal, drTotal, crTotal, AD_Org_ID, (i + 1) * 10);
        //
        AD_Org_ID = gl.getAD_Org_ID();
        drTotal = Env.ZERO;
        crTotal = Env.ZERO;
        journal = null;
      }
    }
    createBalancing(asDefaultAccts, journal, drTotal, crTotal, AD_Org_ID, (list.size() + 1) * 10);

    return " - " + batch.getDocumentNo() + " #" + list.size();
  } //	createGLJournal