org.jpos.gl.rule.DoubleEntry.java Source code

Java tutorial

Introduction

Here is the source code for org.jpos.gl.rule.DoubleEntry.java

Source

/*
 * MiniGL
 * Copyright (C) 2005 Alejandro Revilla
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.jpos.gl.rule;

import java.util.Date;
import java.util.List;
import java.util.Iterator;
import java.math.BigDecimal;
import org.hibernate.Session;
import org.jpos.gl.JournalRule;
import org.jpos.gl.Account;
import org.jpos.gl.GLSession;
import org.jpos.gl.GLException;
import org.jpos.gl.GLEntry;
import org.jpos.gl.GLTransaction;
import org.jpos.gl.GLPermission;
import org.jpos.gl.Journal;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Verify that debits equals credits.
 *
 * This rule check the following conditions:
 * <ul>
 *  <li>This {@link GLTransaction} has two or more {@link GLEntry}s.</li>
 *  <li>Debits equals Credits (in the reporting currency).</li>
 * </ul>
 * @author <a href="mailto:apr@jpos.org">Alejandro Revilla</a>
 * @see org.jpos.gl.JournalRule
 * @see org.jpos.gl.RuleInfo
 */
public class DoubleEntry implements JournalRule {
    private static final BigDecimal ZERO = new BigDecimal("0.00");
    private static final Log log = LogFactory.getLog(DoubleEntry.class);

    public void check(GLSession session, GLTransaction txn, String param, Account account, int[] entryOffsets,
            short[] layers) throws GLException {
        for (int i = 0; i < layers.length; i++)
            checkEntries(txn, layers[i]);
    }

    private void checkEntries(GLTransaction txn, short layer) throws GLException {
        List list = txn.getEntries();
        // if (list.size() < 2) 
        //     throw new GLException ("too few entries (" + list.size() + ")");
        BigDecimal debits = ZERO;
        BigDecimal credits = ZERO;
        Iterator iter = list.iterator();

        while (iter.hasNext()) {
            GLEntry entry = (GLEntry) iter.next();
            if (entry.getLayer() == layer) {
                if (entry.isDebit())
                    debits = debits.add(entry.getAmount());
                else
                    credits = credits.add(entry.getAmount());
            }
        }
        if (!debits.equals(credits)) {
            throw new GLException("Transaction does not balance. debits=" + debits.toString() + ", credits="
                    + credits.toString());
        }
    }
}