Search in sources :

Example 11 with MBankStatement

use of org.compiere.model.MBankStatement in project adempiere by adempiere.

the class CloseStatementPOS method generateLostOrProfit.

private void generateLostOrProfit() {
    MBankStatement bankStatement = (MBankStatement) getBankStatements().entrySet().iterator().next();
    MBankStatementLine bankStatementLine = new MBankStatementLine(bankStatement);
    bankStatementLine.setDateAcct(getTransactionDate());
    bankStatementLine.setStatementLineDate(getTransactionDateTo());
    bankStatementLine.setStmtAmt(getDifference());
    bankStatementLine.setC_Charge_ID(getChargeId());
    bankStatementLine.setChargeAmt(getDifference());
    bankStatementLine.saveEx();
}
Also used : MBankStatement(org.compiere.model.MBankStatement) MBankStatementLine(org.compiere.model.MBankStatementLine)

Example 12 with MBankStatement

use of org.compiere.model.MBankStatement in project adempiere by adempiere.

the class CloseStatementPOS method closeBankStatements.

private void closeBankStatements() {
    getBankStatements().entrySet().stream().forEach(entry -> {
        MBankStatement bankStatement = entry.getValue();
        bankStatement.processIt(DocAction.ACTION_Complete);
        bankStatement.saveEx();
    });
}
Also used : MBankStatement(org.compiere.model.MBankStatement)

Example 13 with MBankStatement

use of org.compiere.model.MBankStatement in project adempiere by adempiere.

the class ImportBankStatement method doIt.

//	prepare
/**
	 *  Perform process.
	 *  @return Message
	 *  @throws Exception
	 */
protected String doIt() throws java.lang.Exception {
    log.info("AD_Org_ID=" + p_AD_Org_ID + ", C_BankAccount_ID" + p_C_BankAccount_ID);
    StringBuffer sql = null;
    int no = 0;
    String clientCheck = " AND AD_Client_ID=" + p_AD_Client_ID;
    //	Delete Old Imported
    if (p_deleteOldImported) {
        sql = new StringBuffer("DELETE I_BankStatement " + "WHERE I_IsImported='Y'").append(clientCheck);
        no = DB.executeUpdate(sql.toString(), get_TrxName());
        log.fine("Delete Old Impored =" + no);
    }
    //	Set Client, Org, IsActive, Created/Updated
    sql = new StringBuffer("UPDATE I_BankStatement " + "SET AD_Client_ID = COALESCE (AD_Client_ID,").append(p_AD_Client_ID).append(")," + " AD_Org_ID = COALESCE (AD_Org_ID,").append(p_AD_Org_ID).append("),");
    sql.append(" IsActive = COALESCE (IsActive, 'Y')," + " Created = COALESCE (Created, SysDate)," + " CreatedBy = COALESCE (CreatedBy, 0)," + " Updated = COALESCE (Updated, SysDate)," + " UpdatedBy = COALESCE (UpdatedBy, 0)," + " I_ErrorMsg = ' '," + " I_IsImported = 'N' " + "WHERE I_IsImported<>'Y' OR I_IsImported IS NULL OR AD_Client_ID IS NULL OR AD_Org_ID IS NULL OR AD_Client_ID=0 OR AD_Org_ID=0");
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.info("Reset=" + no);
    sql = new StringBuffer("UPDATE I_BankStatement o " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Org, '" + "WHERE (AD_Org_ID IS NULL OR AD_Org_ID=0" + " OR EXISTS (SELECT * FROM AD_Org oo WHERE o.AD_Org_ID=oo.AD_Org_ID AND (oo.IsSummary='Y' OR oo.IsActive='N')))" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid Org=" + no);
    //	Set Bank Account
    sql = new StringBuffer("UPDATE I_BankStatement i " + "SET C_BankAccount_ID=" + "( " + " SELECT C_BankAccount_ID " + " FROM C_BankAccount a, C_Bank b " + " WHERE b.IsOwnBank='Y' " + " AND a.AD_Client_ID=i.AD_Client_ID " + " AND a.C_Bank_ID=b.C_Bank_ID " + " AND a.AccountNo=i.BankAccountNo " + " AND b.RoutingNo=i.RoutingNo " + " OR b.SwiftCode=i.RoutingNo " + ") " + "WHERE i.C_BankAccount_ID IS NULL " + "AND i.I_IsImported<>'Y' " + "OR i.I_IsImported IS NULL").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Bank Account (With Routing No)=" + no);
    //
    sql = new StringBuffer("UPDATE I_BankStatement i " + "SET C_BankAccount_ID=" + "( " + " SELECT C_BankAccount_ID " + " FROM C_BankAccount a, C_Bank b " + " WHERE b.IsOwnBank='Y' " + " AND a.C_Bank_ID=b.C_Bank_ID " + " AND a.AccountNo=i.BankAccountNo " + " AND a.AD_Client_ID=i.AD_Client_ID " + ") " + "WHERE i.C_BankAccount_ID IS NULL " + "AND i.I_isImported<>'Y' " + "OR i.I_isImported IS NULL").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Bank Account (Without Routing No)=" + no);
    //
    sql = new StringBuffer("UPDATE I_BankStatement i " + "SET C_BankAccount_ID=(SELECT C_BankAccount_ID FROM C_BankAccount a WHERE a.C_BankAccount_ID=").append(p_C_BankAccount_ID);
    sql.append(" and a.AD_Client_ID=i.AD_Client_ID) " + "WHERE i.C_BankAccount_ID IS NULL " + "AND i.BankAccountNo IS NULL " + "AND i.I_isImported<>'Y' " + "OR i.I_isImported IS NULL").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Bank Account=" + no);
    //	
    sql = new StringBuffer("UPDATE I_BankStatement " + "SET I_isImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Bank Account, ' " + "WHERE C_BankAccount_ID IS NULL " + "AND I_isImported<>'Y' " + "OR I_isImported IS NULL").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid Bank Account=" + no);
    //	Set Currency
    sql = new StringBuffer("UPDATE I_BankStatement i " + "SET C_Currency_ID=(SELECT C_Currency_ID FROM C_Currency c" + " WHERE i.ISO_Code=c.ISO_Code AND c.AD_Client_ID IN (0,i.AD_Client_ID)) " + "WHERE C_Currency_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Set Currency=" + no);
    //
    sql = new StringBuffer("UPDATE I_BankStatement i " + "SET C_Currency_ID=(SELECT C_Currency_ID FROM C_BankAccount WHERE C_BankAccount_ID=i.C_BankAccount_ID) " + "WHERE i.C_Currency_ID IS NULL " + "AND i.ISO_Code IS NULL").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Set Currency=" + no);
    //
    sql = new StringBuffer("UPDATE I_BankStatement " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Currency,' " + "WHERE C_Currency_ID IS NULL " + "AND I_IsImported<>'E' " + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid Currency=" + no);
    //	Set Amount
    sql = new StringBuffer("UPDATE I_BankStatement " + "SET ChargeAmt=0 " + "WHERE ChargeAmt IS NULL " + "AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Charge Amount=" + no);
    //
    sql = new StringBuffer("UPDATE I_BankStatement " + "SET InterestAmt=0 " + "WHERE InterestAmt IS NULL " + "AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Interest Amount=" + no);
    //
    sql = new StringBuffer("UPDATE I_BankStatement " + "SET TrxAmt=StmtAmt - InterestAmt - ChargeAmt " + "WHERE TrxAmt IS NULL " + "AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Transaction Amount=" + no);
    //
    sql = new StringBuffer("UPDATE I_BankStatement " + "SET I_isImported='E', I_ErrorMsg=I_ErrorMsg||'Err=Invalid Amount, ' " + "WHERE TrxAmt + ChargeAmt + InterestAmt <> StmtAmt " + "AND I_isImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Invaid Amount=" + no);
    //	Set Valuta Date
    sql = new StringBuffer("UPDATE I_BankStatement " + "SET ValutaDate=StatementLineDate " + "WHERE ValutaDate IS NULL " + "AND I_isImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Valuta Date=" + no);
    //	Check Payment<->Invoice combination
    sql = new StringBuffer("UPDATE I_BankStatement " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'Err=Invalid Payment<->Invoice, ' " + "WHERE I_BankStatement_ID IN " + "(SELECT I_BankStatement_ID " + "FROM I_BankStatement i" + " INNER JOIN C_Payment p ON (i.C_Payment_ID=p.C_Payment_ID) " + "WHERE i.C_Invoice_ID IS NOT NULL " + " AND p.C_Invoice_ID IS NOT NULL " + " AND p.C_Invoice_ID<>i.C_Invoice_ID) ").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Payment<->Invoice Mismatch=" + no);
    //	Check Payment<->BPartner combination
    sql = new StringBuffer("UPDATE I_BankStatement " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'Err=Invalid Payment<->BPartner, ' " + "WHERE I_BankStatement_ID IN " + "(SELECT I_BankStatement_ID " + "FROM I_BankStatement i" + " INNER JOIN C_Payment p ON (i.C_Payment_ID=p.C_Payment_ID) " + "WHERE i.C_BPartner_ID IS NOT NULL " + " AND p.C_BPartner_ID IS NOT NULL " + " AND p.C_BPartner_ID<>i.C_BPartner_ID) ").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Payment<->BPartner Mismatch=" + no);
    //	Check Invoice<->BPartner combination
    sql = new StringBuffer("UPDATE I_BankStatement " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'Err=Invalid Invoice<->BPartner, ' " + "WHERE I_BankStatement_ID IN " + "(SELECT I_BankStatement_ID " + "FROM I_BankStatement i" + " INNER JOIN C_Invoice v ON (i.C_Invoice_ID=v.C_Invoice_ID) " + "WHERE i.C_BPartner_ID IS NOT NULL " + " AND v.C_BPartner_ID IS NOT NULL " + " AND v.C_BPartner_ID<>i.C_BPartner_ID) ").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Invoice<->BPartner Mismatch=" + no);
    //	Check Invoice.BPartner<->Payment.BPartner combination
    sql = new StringBuffer("UPDATE I_BankStatement " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'Err=Invalid Invoice.BPartner<->Payment.BPartner, ' " + "WHERE I_BankStatement_ID IN " + "(SELECT I_BankStatement_ID " + "FROM I_BankStatement i" + " INNER JOIN C_Invoice v ON (i.C_Invoice_ID=v.C_Invoice_ID)" + " INNER JOIN C_Payment p ON (i.C_Payment_ID=p.C_Payment_ID) " + "WHERE p.C_Invoice_ID<>v.C_Invoice_ID" + " AND v.C_BPartner_ID<>p.C_BPartner_ID) ").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.info("Invoice.BPartner<->Payment.BPartner Mismatch=" + no);
    //	Detect Duplicates
    sql = new StringBuffer("SELECT i.I_BankStatement_ID, l.C_BankStatementLine_ID, i.EftTrxID " + "FROM I_BankStatement i, C_BankStatement s, C_BankStatementLine l " + "WHERE i.I_isImported='N' " + "AND s.C_BankStatement_ID=l.C_BankStatement_ID " + "AND i.EftTrxID IS NOT NULL AND " + //	Concatenate EFT Info
    "(l.EftTrxID||l.EftAmt||l.EftStatementLineDate||l.EftValutaDate||l.EftTrxType||l.EftCurrency||l.EftReference||s.EftStatementReference " + "||l.EftCheckNo||l.EftMemo||l.EftPayee||l.EftPayeeAccount) " + "= " + "(i.EftTrxID||i.EftAmt||i.EftStatementLineDate||i.EftValutaDate||i.EftTrxType||i.EftCurrency||i.EftReference||i.EftStatementReference " + "||i.EftCheckNo||i.EftMemo||i.EftPayee||i.EftPayeeAccount) ");
    StringBuffer updateSql = new StringBuffer("UPDATE I_Bankstatement " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'Err=Duplicate['||?||']' " + "WHERE I_BankStatement_ID=?").append(clientCheck);
    PreparedStatement pupdt = DB.prepareStatement(updateSql.toString(), get_TrxName());
    PreparedStatement pstmtDuplicates = null;
    no = 0;
    try {
        pstmtDuplicates = DB.prepareStatement(sql.toString(), get_TrxName());
        ResultSet rs = pstmtDuplicates.executeQuery();
        while (rs.next()) {
            String info = //	l.C_BankStatementLine_ID
            "Line_ID=" + rs.getInt(2) + ",EDTTrxID=" + //	i.EftTrxID
            rs.getString(3);
            pupdt.setString(1, info);
            //	i.I_BankStatement_ID
            pupdt.setInt(2, rs.getInt(1));
            pupdt.executeUpdate();
            no++;
        }
        rs.close();
        pstmtDuplicates.close();
        pupdt.close();
        rs = null;
        pstmtDuplicates = null;
        pupdt = null;
    } catch (Exception e) {
        log.log(Level.SEVERE, "DetectDuplicates " + e.getMessage());
    }
    if (no != 0)
        log.info("Duplicates=" + no);
    commitEx();
    //Import Bank Statement
    sql = new StringBuffer("SELECT * FROM I_BankStatement" + " WHERE I_IsImported='N'" + clientCheck + " ORDER BY C_BankAccount_ID, Name, EftStatementDate, EftStatementReference");
    MBankStatement statement = null;
    MBankAccount account = null;
    PreparedStatement pstmt = null;
    int lineNo = 10;
    int noInsert = 0;
    int noInsertLine = 0;
    try {
        pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            X_I_BankStatement imp = new X_I_BankStatement(m_ctx, rs, get_TrxName());
            //	Get the bank account for the first statement
            if (account == null) {
                account = MBankAccount.get(m_ctx, imp.getC_BankAccount_ID());
                statement = null;
                log.info("New Statement, Account=" + account.getAccountNo());
            } else //	Create a new Bank Statement for every account
            if (account.getC_BankAccount_ID() != imp.getC_BankAccount_ID()) {
                account = MBankAccount.get(m_ctx, imp.getC_BankAccount_ID());
                statement = null;
                log.info("New Statement, Account=" + account.getAccountNo());
            } else //	Create a new Bank Statement for every statement name
            if ((statement.getName() != null) && (imp.getName() != null)) {
                if (!statement.getName().equals(imp.getName())) {
                    statement = null;
                    log.info("New Statement, Statement Name=" + imp.getName());
                }
            } else //	Create a new Bank Statement for every statement reference
            if ((statement.getEftStatementReference() != null) && (imp.getEftStatementReference() != null)) {
                if (!statement.getEftStatementReference().equals(imp.getEftStatementReference())) {
                    statement = null;
                    log.info("New Statement, Statement Reference=" + imp.getEftStatementReference());
                }
            } else //	Create a new Bank Statement for every statement date
            if ((statement.getStatementDate() != null) && (imp.getStatementDate() != null)) {
                if (!statement.getStatementDate().equals(imp.getStatementDate())) {
                    statement = null;
                    log.info("New Statement, Statement Date=" + imp.getStatementDate());
                }
            }
            //	New Statement
            if (statement == null) {
                statement = new MBankStatement(account);
                statement.setEndingBalance(Env.ZERO);
                //	Copy statement data
                if (imp.getName() != null) {
                    statement.setName(imp.getName());
                }
                if (imp.getStatementDate() != null) {
                    statement.setStatementDate(imp.getStatementDate());
                }
                statement.setDescription(imp.getDescription());
                statement.setEftStatementReference(imp.getEftStatementReference());
                statement.setEftStatementDate(imp.getEftStatementDate());
                if (statement.save()) {
                    noInsert++;
                }
                lineNo = 10;
            }
            //	New StatementLine
            MBankStatementLine line = new MBankStatementLine(statement, lineNo);
            //	Copy statement line data
            //line.setC_BPartner_ID(imp.getC_BPartner_ID());
            //line.setC_Invoice_ID(imp.getC_Invoice_ID());
            line.setReferenceNo(imp.getReferenceNo());
            line.setDescription(imp.getLineDescription());
            line.setStatementLineDate(imp.getStatementLineDate());
            line.setDateAcct(imp.getStatementLineDate());
            line.setValutaDate(imp.getValutaDate());
            line.setIsReversal(imp.isReversal());
            line.setC_Currency_ID(imp.getC_Currency_ID());
            line.setTrxAmt(imp.getTrxAmt());
            line.setStmtAmt(imp.getStmtAmt());
            if (imp.getC_Charge_ID() != 0) {
                line.setC_Charge_ID(imp.getC_Charge_ID());
            }
            line.setInterestAmt(imp.getInterestAmt());
            line.setChargeAmt(imp.getChargeAmt());
            line.setMemo(imp.getMemo());
            if (imp.getC_Payment_ID() != 0) {
                line.setC_Payment_ID(imp.getC_Payment_ID());
            }
            //	Copy statement line reference data
            line.setEftTrxID(imp.getEftTrxID());
            line.setEftTrxType(imp.getEftTrxType());
            line.setEftCheckNo(imp.getEftCheckNo());
            line.setEftReference(imp.getEftReference());
            line.setEftMemo(imp.getEftMemo());
            line.setEftPayee(imp.getEftPayee());
            line.setEftPayeeAccount(imp.getEftPayeeAccount());
            line.setEftStatementLineDate(imp.getEftStatementLineDate());
            line.setEftValutaDate(imp.getEftValutaDate());
            line.setEftCurrency(imp.getEftCurrency());
            line.setEftAmt(imp.getEftAmt());
            //	Save statement line
            if (line.save()) {
                imp.setC_BankStatement_ID(statement.getC_BankStatement_ID());
                imp.setC_BankStatementLine_ID(line.getC_BankStatementLine_ID());
                imp.setI_IsImported(true);
                imp.setProcessed(true);
                imp.saveEx();
                noInsertLine++;
                lineNo += 10;
            }
            line = null;
        }
        //	Close database connection
        rs.close();
        pstmt.close();
        rs = null;
        pstmt = null;
    } catch (Exception e) {
        log.log(Level.SEVERE, sql.toString(), e);
    }
    //	Set Error to indicator to not imported
    sql = new StringBuffer("UPDATE I_BankStatement " + "SET I_IsImported='N', Updated=SysDate " + "WHERE I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    addLog(0, null, new BigDecimal(no), "@Errors@");
    //
    addLog(0, null, new BigDecimal(noInsert), "@C_BankStatement_ID@: @Inserted@");
    addLog(0, null, new BigDecimal(noInsertLine), "@C_BankStatementLine_ID@: @Inserted@");
    return "";
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) X_I_BankStatement(org.compiere.model.X_I_BankStatement) MBankStatement(org.compiere.model.MBankStatement) MBankStatementLine(org.compiere.model.MBankStatementLine) MBankAccount(org.compiere.model.MBankAccount) BigDecimal(java.math.BigDecimal)

Aggregations

MBankStatement (org.compiere.model.MBankStatement)13 MBankStatementLine (org.compiere.model.MBankStatementLine)9 MPayment (org.compiere.model.MPayment)7 BigDecimal (java.math.BigDecimal)6 MBankAccount (org.compiere.model.MBankAccount)5 Timestamp (java.sql.Timestamp)3 KeyNamePair (org.compiere.util.KeyNamePair)3 AdempiereException (org.adempiere.exceptions.AdempiereException)2 X_I_BankStatement (org.compiere.model.X_I_BankStatement)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AdempiereSystemError (org.compiere.util.AdempiereSystemError)1 AdempiereUserError (org.compiere.util.AdempiereUserError)1