ReconcileManager.java :  » ERP-CRM-Financial » jGnash-2 » jgnash » engine » Java Open Source

Java Open Source » ERP CRM Financial » jGnash 2 
jGnash 2 » jgnash » engine » ReconcileManager.java
/*
 * jGnash, a personal finance application
 * Copyright (C) 2001-2008 Craig Cavanaugh
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */
package jgnash.engine;

import java.util.List;
import java.util.prefs.Preferences;

/** Manages the reconciliation options.
 * <p>
 * Both reconcileIncomeExpense and reconcileBothSides can be false, but
 * only one can be true
 * 
 * @author Craig Cavanaugh
 * 
 * $Id: ReconcileManager.java 197 2008-01-01 08:13:35Z ccavanaugh $
 */
public class ReconcileManager {
    private static final String RECONCILE_INCOMEEXPENSE = "reconcileIncomeExpense";

    private static final String RECONCILE_BOTHSIDES = "reconcileBothSides";

    private static boolean reconcileIncomeExpense;

    private static boolean reconcileBothSides;

    static {
        Preferences p = Preferences.userNodeForPackage(ReconcileManager.class);

        reconcileIncomeExpense = p.getBoolean(RECONCILE_INCOMEEXPENSE, false);
        reconcileBothSides = p.getBoolean(RECONCILE_BOTHSIDES, true);
    }

    /** Set so the income or expense side of a transaction is
     * automatically reconciled
     * 
     * @param reconcile true if income and expense accounts should be
     *                  automatically reconciled
     */
    public static void setAutoReconcileIncomeExpense(boolean reconcile) {
        reconcileIncomeExpense = reconcile;
        Preferences p = Preferences.userNodeForPackage(ReconcileManager.class);
        p.putBoolean(RECONCILE_INCOMEEXPENSE, reconcileIncomeExpense);

        if (reconcile == true) {
            setAutoReconcileBothSides(false);
        }
    }

    /** Determines if the income or expense side of a transaction should
     * be automatically reconciled
     * 
     * @return true if income and expense accounts should automatically be
     *         reconciled.
     */
    public static boolean getAutoReconcileIncomeExpense() {
        return reconcileIncomeExpense;
    }

    /** Set so both sides of a double entry transaction are automatically 
     * reconciled
     * 
     * @param reconcile true if income and expense accounts should be
     *                  automatically reconciled
     */
    public static void setAutoReconcileBothSides(boolean reconcile) {
        reconcileBothSides = reconcile;
        Preferences p = Preferences.userNodeForPackage(ReconcileManager.class);
        p.putBoolean(RECONCILE_BOTHSIDES, reconcileBothSides);

        if (reconcile == true) {
            setAutoReconcileIncomeExpense(false);
        }
    }

    /** Determines if both sides of a transaction should be automatically reconciled
     * 
     * @return true if income and expense accounts should automatically be
     *         reconciled.
     */
    public static boolean getAutoReconcileBothSides() {
        return reconcileBothSides;
    }

    public static boolean isAutoReconcileDisabled() {
        return reconcileBothSides == false && reconcileIncomeExpense == false;
    }

    /** Disables auto reconciliation
     */
    public static void setDoNotAutoReconcile() {
        setAutoReconcileBothSides(false);
        setAutoReconcileIncomeExpense(false);
    }

    /** Sets the reconciled state of the transaction using the rules set 
     * by the user
     * 
     * @param account       Base account
     * @param t             Transaction to reconcile
     * @param reconciled    Reconciled state
     */
    public static void reconcileTransaction(Account account, Transaction t, ReconciledState reconciled) {
        t.setReconciled(account, reconciled); // mark transaction reconciled

        if (getAutoReconcileBothSides()) {
            t.setReconciled(reconciled);
        } else if (getAutoReconcileIncomeExpense()) {
            List<TransactionEntry> entries = t.getTransactionEntries();

            for (TransactionEntry entry : entries) {
                Account c = entry.getCreditAccount();
                Account d = entry.getDebitAccount();

                if (c.instanceOf(AccountType.INCOME) || c.instanceOf(AccountType.EXPENSE)) {
                    entry.setCreditReconciled(reconciled);
                }

                if (d.instanceOf(AccountType.INCOME) || d.instanceOf(AccountType.EXPENSE)) {
                    entry.setDebitReconciled(reconciled);
                }
            }
        }
    }

    /** Sets the reconciled state of the transaction using the rules set by the user
     * 
     * @param account       Base account
     * @param t             Transaction to reconcile
     * @param reconciled    ReconciledState.RECONCILED if true; ReconciledState.NOT_RECONCILED if false
     */
    public static void reconcileTransaction(Account account, Transaction t, boolean reconciled) {
        reconcileTransaction(account, t, reconciled ? ReconciledState.RECONCILED : ReconciledState.NOT_RECONCILED);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.