OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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.sales.order.ui.TypeFactureCommandeCellRenderer;
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeTableCellEditor;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.SQLComponent;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.UndefinedRowValuesCache;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.request.ComboSQLRequest;
import org.openconcerto.sql.sqlobject.ElementComboBox;
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.PercentTableCellRenderer;
import org.openconcerto.ui.table.TimestampTableCellEditor;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Vector;

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

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

    public FacturationCommandeTable(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);

        final SQLTableElement tableElementTF = new SQLTableElement(e.getTable().getField("TYPE_FACTURE"), Integer.class, new TypeFactureCommandeTableCellEditor());
        tableElementTF.setRenderer(new TypeFactureCommandeCellRenderer());
        list.add(tableElementTF);

        final SQLTableElement pourcent = new SQLTableElement(e.getTable().getField("POURCENT"), BigDecimal.class);
        pourcent.setRenderer(new PercentTableCellRenderer());
        list.add(pourcent);

        final SQLTableElement type = new SQLTableElement(e.getTable().getField("ID_TYPE_REGLEMENT"));
        list.add(type);

        final SQLTableElement ajours = new SQLTableElement(e.getTable().getField("AJOURS"));
        list.add(ajours);
        final SQLTableElement finM = new SQLTableElement(e.getTable().getField("FIN_MOIS"));
        list.add(finM);

        final SQLTableElement comptant = new SQLTableElement(e.getTable().getField("COMPTANT"));
        list.add(comptant);

        final SQLTableElement date = new SQLTableElement(e.getTable().getField("DATE_PREVISIONNELLE"), Date.class, new TimestampTableCellEditor(false) {
            @Override
            public Object getCellEditorValue() {

                Object o = super.getCellEditorValue();
                Date d = null;
                if (o != null) {
                    d = new Date(((Timestamp) o).getTime());
                }
                return d;
            }
        });
        list.add(date);

        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());

        // 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
        // qteElement.addModificationListener(this.totalHT);
        // this.pvHT.addModificationListener(this.totalHT);
        // this.totalHT.setModifier(new CellDynamicModifier() {
        // public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
        // System.out.println("Compute totalHT");
        //
        // int qte = Integer.parseInt(row.getObject("QTE").toString());
        // BigDecimal f = row.getBigDecimal("PV_HT");
        // BigDecimal r = f.multiply(new BigDecimal(qte), DecimalUtils.HIGH_PRECISION);
        // return r;
        // }
        //
        // });

    }

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

        // Ajout catégorie
        c.gridwidth = 1;
        c.weightx = 0;
        c.gridx = 0;
        this.add(new JLabel("Ajouter un terme"), c);

        final ElementComboBox boxCat = new ElementComboBox();
        final SQLElement element = Configuration.getInstance().getDirectory().getElement("FACTURATION_COMMANDE_CLIENT");
        ComboSQLRequest req = element.getComboRequest(true);
        req.setWhere(new Where(element.getTable().getField("CHOICE"), "=", Boolean.TRUE));
        boxCat.init(element, req);

        c.gridx++;
        this.add(boxCat, c);

        c.fill = GridBagConstraints.NONE;
        c.gridx++;
        JButton buttonAjouter = new JButton("Ajouter");
        buttonAjouter.setOpaque(false);
        this.add(buttonAjouter, c);

        c.gridwidth = GridBagConstraints.REMAINDER;
        // Listeners
        buttonAjouter.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                SQLRow rowCat = boxCat.getSelectedRow();
                if (rowCat == null || rowCat.isUndefined()) {
                    return;
                }

                SQLRowValues rowVals = new SQLRowValues(Configuration.getInstance().getBase().getTable("FACTURATION_COMMANDE_CLIENT"));
                if (comp.getSelectedID() > 1) {
                    rowVals.put("ID_COMMANDE_CLIENT", comp.getSelectedID());
                }
                rowVals.put("NOM", rowCat.getObject("NOM"));
                rowVals.put("ID_TYPE_REGLEMENT", rowCat.getObject("ID_TYPE_REGLEMENT"));
                rowVals.put("AJOURS", rowCat.getObject("AJOURS"));
                rowVals.put("COMPTANT", rowCat.getObject("COMPTANT"));
                rowVals.put("FIN_MOIS", rowCat.getObject("FIN_MOIS"));
                rowVals.put("MONTANT", rowCat.getObject("MONTANT"));
                rowVals.put("POURCENT", rowCat.getObject("POURCENT"));
                rowVals.put("TYPE_FACTURE", rowCat.getObject("TYPE_FACTURE"));
                getModel().addRow(rowVals);
            }
        });

        c.gridy++;
        c.gridx = 0;
        final JPanel control = new RowValuesTableControlPanel(this.table);
        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());
    }

    public SQLElement getSQLElement() {
        return Configuration.getInstance().getDirectory().getElement("FACTURATION_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();
    }
}