OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Rev 180 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.erp.core.finance.accounting.model;

import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.erp.core.finance.accounting.ui.PointagePanel;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.model.SQLBase;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.sql.view.list.ITableModel;

import java.util.Date;
import java.util.List;

import javax.swing.SwingWorker;
import javax.swing.table.AbstractTableModel;

import org.apache.commons.dbutils.handlers.ArrayListHandler;

public class PointageModel extends AbstractTableModel {

    private String[] titresCol;
    private String[] titresRow;
    private PointagePanel panel;
    private long debitListe, creditListe, debitTotal, creditTotal, debitPointe, creditPointe, debitNonPointe, creditNonPointe, creditSelection, debitSelection;

    private static final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
    private static final SQLTable tableEcr = base.getTable("ECRITURE");
    int idCpt;

    public PointageModel(int idCpt, PointagePanel panel) {
        this.creditNonPointe = 0;
        this.creditTotal = 0;
        this.creditPointe = 0;
        this.creditSelection = 0;

        this.panel = panel;

        this.debitNonPointe = 0;
        this.debitPointe = 0;
        this.debitTotal = 0;
        this.debitSelection = 0;

        this.idCpt = idCpt;
        this.titresCol = new String[6];
        this.titresCol[0] = "Totaux période";
        this.titresCol[1] = "Pointé";
        this.titresCol[2] = "Relevé courant";
        this.titresCol[3] = "Total";

        this.titresCol[4] = "Sélection";
        this.titresCol[5] = "Liste";

        this.titresRow = new String[3];
        this.titresRow[0] = "Débit";
        this.titresRow[1] = "Crédit";
        this.titresRow[2] = "Solde";

        updateTotauxCompte();
    }

    public void setIdCompte(int id) {
        this.idCpt = id;
        updateTotauxCompte();
        updateSelection(null);
    }

    public void updateSelection(int[] rowIndex) {
        this.creditSelection = 0;
        this.debitSelection = 0;

        if (rowIndex != null) {
            for (int i = 0; i < rowIndex.length; i++) {

                SQLRow row = tableEcr.getRow(rowIndex[i]);

                if (row != null) {

                    // if (row.getString("POINTEE").trim().length() == 0) {
                    this.debitSelection += ((Long) row.getObject("DEBIT")).longValue();
                    this.creditSelection += ((Long) row.getObject("CREDIT")).longValue();
                    // }
                    /*
                     * else {
                     * 
                     * this.debitSelection -= row.getFloat("DEBIT"); this.creditSelection -=
                     * row.getFloat("CREDIT"); }
                     */
                }
            }
        }
        this.fireTableDataChanged();
    }

    private SwingWorker<String, Object> workerUpdater;

    public void updateTotauxCompte() {
        final Date deb = panel.getDateDeb();
        final Date fin = panel.getDateFin();

        if (workerUpdater != null) {
            workerUpdater.cancel(true);
        }

        workerUpdater = new SwingWorker<String, Object>() {

            @Override
            protected String doInBackground() throws Exception {

                SQLSelect sel = new SQLSelect();
                sel.addSelect(tableEcr.getField("CREDIT"), "SUM");
                sel.addSelect(tableEcr.getField("DEBIT"), "SUM");

                Where w = new Where(tableEcr.getField("ID_COMPTE_PCE"), "=", PointageModel.this.idCpt);

                if (deb != null && fin != null) {
                    w = w.and(new Where(tableEcr.getField("DATE"), deb, fin));
                } else if (deb != null) {
                    w = w.and(new Where(tableEcr.getField("DATE"), ">=", deb));
                } else if (fin != null) {
                    w = w.and(new Where(tableEcr.getField("DATE"), "<=", fin));
                }
                sel.setWhere(w.and(new Where(tableEcr.getField("POINTEE"), "!=", "")));

                String reqPointee = sel.toString();

                Object obPointee = base.getDataSource().execute(reqPointee, new ArrayListHandler());

                List myListPointee = (List) obPointee;

                PointageModel.this.creditPointe = 0;
                PointageModel.this.debitPointe = 0;
                if (!myListPointee.isEmpty()) {

                    for (int i = 0; i < myListPointee.size(); i++) {
                        Object[] objTmp = (Object[]) myListPointee.get(i);

                        if (objTmp[0] != null) {
                            PointageModel.this.creditPointe += ((Number) objTmp[0]).longValue();
                        }
                        if (objTmp[1] != null) {
                            PointageModel.this.debitPointe += ((Number) objTmp[1]).longValue();
                        }
                    }
                }

                sel.setWhere(w.and(new Where(tableEcr.getField("POINTEE"), "=", panel.getCodePointage())));
                String reqReleveCourant = sel.toString();

                Object obReleveCourant = base.getDataSource().execute(reqReleveCourant, new ArrayListHandler());

                List myListReleveCourant = (List) obReleveCourant;

                PointageModel.this.creditNonPointe = 0;
                PointageModel.this.debitNonPointe = 0;
                if (!myListReleveCourant.isEmpty()) {

                    for (int i = 0; i < myListReleveCourant.size(); i++) {
                        Object[] objTmp = (Object[]) myListReleveCourant.get(i);

                        if (objTmp[0] != null) {
                            PointageModel.this.creditNonPointe += ((Number) objTmp[0]).longValue();
                        }

                        if (objTmp[1] != null) {
                            PointageModel.this.debitNonPointe += ((Number) objTmp[1]).longValue();
                        }
                    }
                }

                sel.setWhere(w);
                String reqReleveTotal = sel.toString();

                Object obReleveTotal = base.getDataSource().execute(reqReleveTotal, new ArrayListHandler());

                List myListReleveTotal = (List) obReleveTotal;

                PointageModel.this.creditTotal = 0;
                PointageModel.this.debitTotal = 0;
                if (!myListReleveTotal.isEmpty()) {

                    for (int i = 0; i < myListReleveTotal.size(); i++) {
                        Object[] objTmp = (Object[]) myListReleveTotal.get(i);

                        if (objTmp[0] != null) {
                            PointageModel.this.creditTotal += ((Number) objTmp[0]).longValue();
                        }

                        if (objTmp[1] != null) {
                            PointageModel.this.debitTotal += ((Number) objTmp[1]).longValue();
                        }
                    }
                }

                PointageModel.this.creditListe = 0;
                PointageModel.this.debitListe = 0;
                IListe listeEcr = panel.getEcriturePanel().getListe();
                for (int i = 0; i < listeEcr.getRowCount(); i++) {

                    final SQLRowValues rowAt = ITableModel.getLine(listeEcr.getModel(), i).getRow();
                    if (rowAt.getObject("CREDIT") != null) {
                        PointageModel.this.creditListe += rowAt.getLong("CREDIT");
                    }

                    if (rowAt.getObject("DEBIT") != null) {
                        PointageModel.this.debitListe += rowAt.getLong("DEBIT");
                    }
                }

                return null;
            }

            @Override
            protected void done() {

                if (isCancelled()) {
                    return;
                }
                PointageModel.this.fireTableDataChanged();
            }
        };
        workerUpdater.execute();

    }

    @Override
    public String getColumnName(int column) {

        return this.titresCol[column];
    }

    public int getColumnCount() {

        return this.titresCol.length;
    }

    public int getRowCount() {

        return this.titresRow.length;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 0) {
            return String.class;
        } else {
            return Long.class;
        }
    }

    public Object getValueAt(int rowIndex, int columnIndex) {

        if (columnIndex == 0) {
            return this.titresRow[rowIndex];
        }

        if (columnIndex == 1) {
            if (rowIndex == 0) {
                return Long.valueOf(this.debitPointe);
            }
            if (rowIndex == 1) {
                return Long.valueOf(this.creditPointe);
            }
            if (rowIndex == 2) {
                return Long.valueOf(this.debitPointe - this.creditPointe);
            }
        }

        if (columnIndex == 2) {
            if (rowIndex == 0) {
                return Long.valueOf(this.debitNonPointe);
            }
            if (rowIndex == 1) {
                return Long.valueOf(this.creditNonPointe);
            }
            if (rowIndex == 2) {
                return Long.valueOf(this.debitNonPointe - this.creditNonPointe);
            }
        }

        if (columnIndex == 3) {
            if (rowIndex == 0) {
                return Long.valueOf(this.debitTotal);
            }
            if (rowIndex == 1) {
                return Long.valueOf(this.creditTotal);
            }
            if (rowIndex == 2) {
                return Long.valueOf(this.debitTotal - this.creditTotal);
            }
        }

        if (columnIndex == 4) {
            if (rowIndex == 0) {
                return Long.valueOf(this.debitSelection);
            }
            if (rowIndex == 1) {
                return Long.valueOf(this.creditSelection);
            }
            if (rowIndex == 2) {
                return Long.valueOf(this.debitSelection - this.creditSelection);
            }
        }

        if (columnIndex == 5) {
            if (rowIndex == 0) {
                return Long.valueOf(this.debitListe);
            }
            if (rowIndex == 1) {
                return Long.valueOf(this.creditListe);
            }
            if (rowIndex == 2) {
                return Long.valueOf(this.debitListe - this.creditListe);
            }
        }

        return null;
    }
}