OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | 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.supplychain.order.table;

import org.openconcerto.erp.core.common.ui.DeviseNumericCellEditor;
import org.openconcerto.erp.core.common.ui.DeviseTableCellRenderer;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.SQLComponent;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLField;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.UndefinedRowValuesCache;
import org.openconcerto.sql.view.list.CellDynamicModifier;
import org.openconcerto.sql.view.list.RowValuesTable;
import org.openconcerto.sql.view.list.RowValuesTableControlPanel;
import org.openconcerto.sql.view.list.RowValuesTableModel;
import org.openconcerto.sql.view.list.RowValuesTableRenderer;
import org.openconcerto.sql.view.list.SQLTableElement;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.table.XTableColumnModel;
import org.openconcerto.utils.DecimalUtils;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.ToolTipManager;

public class ChiffrageCommandeTable extends JPanel {
    private RowValuesTable table;
    private RowValuesTableModel model;
    private SQLComponent comp;

    public ChiffrageCommandeTable(SQLComponent comp) {
        this.comp = comp;
        init();
        uiInit();
    }

    protected void init() {

        final SQLElement e = getSQLElement();

        final List<SQLTableElement> list = new Vector<SQLTableElement>();

        final SQLTableElement tableElementNom = new SQLTableElement(e.getTable().getField("NOM"));
        list.add(tableElementNom);
        if (e.getTable().contains("ID_TYPE_CMD")) {
            final SQLTableElement cat = new SQLTableElement(e.getTable().getField("ID_TYPE_CMD"));
            list.add(cat);
        }
        if (e.getTable().contains("ID_CATEGORIE_HEURE")) {
            final SQLTableElement cat = new SQLTableElement(e.getTable().getField("ID_CATEGORIE_HEURE"));
            list.add(cat);
        }

        final SQLTableElement fam = new SQLTableElement(e.getTable().getField("ID_FAMILLE_ARTICLE"));
        list.add(fam);

        final SQLField fieldHA = e.getTable().getField("PA_HT");
        final DeviseNumericCellEditor editorPAHT = new DeviseNumericCellEditor(fieldHA);
        final SQLTableElement pa = new SQLTableElement(fieldHA, BigDecimal.class, editorPAHT);
        DeviseTableCellRenderer renderer = new DeviseTableCellRenderer();
        renderer.setHideZeroValue(true);
        pa.setRenderer(renderer);
        if (e.getTable().contains("ID_CATEGORIE_HEURE")) {
            pa.setEditable(false);
        }
        list.add(pa);

        final SQLField fieldPV = e.getTable().getField("PV_HT");
        final DeviseNumericCellEditor editorPVHT = new DeviseNumericCellEditor(fieldPV);
        final SQLTableElement pv = new SQLTableElement(fieldPV, BigDecimal.class, editorPVHT);
        pv.setRenderer(renderer);
        list.add(pv);

        SQLTableElement qteU = new SQLTableElement(e.getTable().getField("QTE"), BigDecimal.class) {

            protected Object getDefaultNullValue() {
                return BigDecimal.ZERO;
            }
        };
        qteU.setRenderer(renderer);
        list.add(qteU);

        if (e.getTable().contains("ANT")) {
            SQLTableElement ant = new SQLTableElement(e.getTable().getField("ANT"), BigDecimal.class) {

                protected Object getDefaultNullValue() {
                    return BigDecimal.ZERO;
                }
            };
            ant.setRenderer(renderer);
            list.add(ant);
        }

        if (e.getTable().contains("RESTANT")) {
            SQLTableElement restant = new SQLTableElement(e.getTable().getField("RESTANT"), BigDecimal.class) {

                protected Object getDefaultNullValue() {
                    return BigDecimal.ZERO;
                }
            };
            restant.setRenderer(renderer);
            list.add(restant);
        }

        final SQLTableElement unit = new SQLTableElement(e.getTable().getField("ID_UNITE_VENTE"));
        list.add(unit);

        final SQLField fieldTotalHA = e.getTable().getField("T_PA_HT");
        final DeviseNumericCellEditor editorTotalPAHT = new DeviseNumericCellEditor(fieldTotalHA);
        final SQLTableElement totalpa = new SQLTableElement(fieldTotalHA, BigDecimal.class, editorTotalPAHT);
        totalpa.setRenderer(renderer);
        totalpa.setEditable(false);
        list.add(totalpa);

        final SQLField fieldTotalPV = e.getTable().getField("T_PV_HT");
        final DeviseNumericCellEditor editorTotalPVHT = new DeviseNumericCellEditor(fieldTotalPV);
        final SQLTableElement totalpv = new SQLTableElement(fieldTotalPV, BigDecimal.class, editorTotalPVHT);
        totalpv.setEditable(false);
        totalpv.setRenderer(renderer);
        list.add(totalpv);

        final SQLField fieldMarge = e.getTable().getField("MARGE");
        final DeviseNumericCellEditor editorMarge = new DeviseNumericCellEditor(fieldMarge);
        final SQLTableElement marge = new SQLTableElement(fieldMarge, BigDecimal.class, editorMarge);
        marge.setEditable(false);
        marge.setRenderer(renderer);
        list.add(marge);

        SQLRowValues defautRow = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(e.getTable()));
        this.model = new RowValuesTableModel(e, list, e.getTable().getField("NOM"), false, defautRow);

        this.table = new RowValuesTable(this.model, null, true);
        ToolTipManager.sharedInstance().unregisterComponent(this.table);
        ToolTipManager.sharedInstance().unregisterComponent(this.table.getTableHeader());

        this.table.readState();

        Map<String, Boolean> mapCustom = getCustomVisibilityMap();
        if (mapCustom != null) {
            for (String string : mapCustom.keySet()) {
                setColumnVisible(model.getColumnForField(string), mapCustom.get(string));
            }
        }

        this.table.writeState();

        // Autocompletion
        // AutoCompletionManager m = new AutoCompletionManager(tableElementNom,
        // ((ComptaPropsConfiguration)
        // Configuration.getInstance()).getSQLBaseSociete().getField("ECHANTILLON.NOM"), this.table,
        // this.table.getRowValuesTableModel());
        // m.fill("NOM", "NOM");
        // m.fill("PV_HT", "PV_HT");

        // // Calcul automatique du total HT
        qteU.addModificationListener(totalpv);
        pv.addModificationListener(totalpv);
        totalpv.setModifier(new CellDynamicModifier() {
            public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
                System.out.println("Compute totalHT");

                BigDecimal qte = row.getBigDecimal("QTE");
                BigDecimal f = row.getBigDecimal("PV_HT");
                if (f == null) {
                    return null;
                }
                BigDecimal r = f.multiply(qte, DecimalUtils.HIGH_PRECISION);
                return r;
            }
        });

        qteU.addModificationListener(totalpa);
        pa.addModificationListener(totalpa);
        totalpa.setModifier(new CellDynamicModifier() {
            public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
                System.out.println("Compute totalHT");

                BigDecimal qte = row.getBigDecimal("QTE");
                BigDecimal f = row.getBigDecimal("PA_HT");
                if (f == null) {
                    return null;
                }
                BigDecimal r = f.multiply(qte, DecimalUtils.HIGH_PRECISION);
                return r;
            }

        });
        totalpv.addModificationListener(marge);
        totalpa.addModificationListener(marge);
        marge.setModifier(new CellDynamicModifier() {
            public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {

                BigDecimal f = row.getBigDecimal("T_PV_HT");
                if (f == null) {
                    return null;
                }
                BigDecimal f2 = row.getBigDecimal("T_PA_HT");
                if (f2 == null) {
                    return null;
                }
                BigDecimal r = f.subtract(f2);
                return r;
            }
        });
    }

    protected void setColumnVisible(int col, boolean visible) {
        if (col >= 0) {
            XTableColumnModel columnModel = this.table.getColumnModel();
            columnModel.setColumnVisible(columnModel.getColumnByModelIndex(col), visible);
        }
    }

    public static Map<String, Boolean> map = new HashMap<String, Boolean>();

    protected Map<String, Boolean> getCustomVisibilityMap() {
        return map;
    }

    /**
     * 
     */
    protected void uiInit() {
        // Ui init
        this.setLayout(new GridBagLayout());
        this.setOpaque(false);
        final GridBagConstraints c = new DefaultGridBagConstraints();
        c.weightx = 1;

        c.gridy++;
        c.gridx = 0;
        JButton loadDefaultButton = new JButton(new AbstractAction("Charger les valeurs par défaut") {

            @Override
            public void actionPerformed(ActionEvent e) {
                for (SQLRowValues sqlRowValues : lists) {
                    getModel().addRow(sqlRowValues);
                }
            }
        });
        final JPanel control = new RowValuesTableControlPanel(this.table, Arrays.asList(loadDefaultButton));
        control.setOpaque(false);
        this.add(control, c);

        c.gridy++;
        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1;
        c.weightx = 1;
        JScrollPane comp = new JScrollPane(this.table);
        comp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        this.add(comp, c);
        this.table.setDefaultRenderer(Long.class, new RowValuesTableRenderer());

        for (SQLRowValues sqlRowValues : lists) {
            getModel().addRow(sqlRowValues);
        }
    }

    private static List<SQLRowValues> lists = new ArrayList<SQLRowValues>();

    public static void addDefaultSQLRowValues(SQLRowValues rowVals) {
        lists.add(rowVals);
    }

    public SQLElement getSQLElement() {
        return Configuration.getInstance().getDirectory().getElement("CHIFFRAGE_COMMANDE_CLIENT");
    }

    public void updateField(String field, int id) {
        this.table.updateField(field, id);
    }

    public RowValuesTable getRowValuesTable() {
        return this.table;
    }

    public void insertFrom(String field, int id) {
        this.table.insertFrom(field, id);
    }

    public RowValuesTableModel getModel() {
        return this.table.getRowValuesTableModel();
    }

    public void refreshTable() {
        this.table.repaint();
    }
}