/**
   * 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
Esempio n. 2
0
 /**
  * Create actual Payment
  *
  * @param C_Invoice_ID invoice
  * @param C_BPartner_ID partner ignored when invoice exists
  * @param C_Currency_ID currency
  * @param StmtAmt statement amount
  * @param TrxAmt transaction amt
  * @param C_BP_BankAccount_ID bank account
  * @param DateTrx transaction date
  * @param DateAcct accounting date
  * @param Description description
  * @param AD_Org_ID org
  * @return payment
  */
 private MPayment createPayment(
     int C_Invoice_ID,
     int C_BPartner_ID,
     int C_Currency_ID,
     BigDecimal StmtAmt,
     BigDecimal TrxAmt,
     int C_BP_BankAccount_ID,
     Timestamp DateTrx,
     Timestamp DateAcct,
     String Description,
     int AD_Org_ID) {
   //	Trx Amount = Payment overwrites Statement Amount if defined
   BigDecimal PayAmt = TrxAmt;
   if (PayAmt == null || Env.ZERO.compareTo(PayAmt) == 0) PayAmt = StmtAmt;
   if (C_Invoice_ID == 0 && (PayAmt == null || Env.ZERO.compareTo(PayAmt) == 0))
     throw new IllegalStateException("@PayAmt@ = 0");
   if (PayAmt == null) PayAmt = Env.ZERO;
   //
   MPayment payment = new MPayment(getCtx(), 0, get_TrxName());
   payment.setAD_Org_ID(AD_Org_ID);
   payment.setC_BP_BankAccount_ID(C_BP_BankAccount_ID);
   payment.setTenderType(MPayment.TENDERTYPE_Check);
   if (DateTrx != null) payment.setDateTrx(DateTrx);
   else if (DateAcct != null) payment.setDateTrx(DateAcct);
   if (DateAcct != null) payment.setDateAcct(DateAcct);
   else payment.setDateAcct(payment.getDateTrx());
   payment.setDescription(Description);
   //
   if (C_Invoice_ID != 0) {
     MInvoice invoice = new MInvoice(getCtx(), C_Invoice_ID, null);
     payment.setC_DocType_ID(invoice.isSOTrx()); // 	Receipt
     payment.setC_Invoice_ID(invoice.getC_Invoice_ID());
     payment.setC_BPartner_ID(invoice.getC_BPartner_ID());
     if (PayAmt.signum() != 0) // 	explicit Amount
     {
       payment.setC_Currency_ID(C_Currency_ID);
       if (invoice.isSOTrx()) payment.setPayAmt(PayAmt);
       else //	payment is likely to be negative
       payment.setPayAmt(PayAmt.negate());
       payment.setOverUnderAmt(invoice.getGrandTotal(true).subtract(payment.getPayAmt()));
     } else // set Pay Amout from Invoice
     {
       payment.setC_Currency_ID(invoice.getC_Currency_ID());
       payment.setPayAmt(invoice.getGrandTotal(true));
     }
   } else if (C_BPartner_ID != 0) {
     payment.setC_BPartner_ID(C_BPartner_ID);
     payment.setC_Currency_ID(C_Currency_ID);
     if (PayAmt.signum() < 0) // 	Payment
     {
       payment.setPayAmt(PayAmt.abs());
       payment.setC_DocType_ID(false);
     } else //	Receipt
     {
       payment.setPayAmt(PayAmt);
       payment.setC_DocType_ID(true);
     }
   } else return null;
   payment.save();
   //
   payment.processIt(MPayment.DOCACTION_Complete);
   payment.save();
   return payment;
 } //	createPayment