OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011-2019 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.tax.element;

import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
import org.openconcerto.erp.core.sales.product.element.TaxeCatComptableSQLElement;
import org.openconcerto.erp.core.sales.product.ui.TaxeCategorieComptableTable;
import org.openconcerto.erp.model.ISQLCompteSelector;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.BaseSQLComponent;
import org.openconcerto.sql.element.SQLComponent;
import org.openconcerto.sql.model.SQLDataSource;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.request.UpdateBuilder;
import org.openconcerto.sql.sqlobject.ElementComboBox;
import org.openconcerto.sql.utils.SQLUtils;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.utils.ProductInfo;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class TaxeSQLElement extends ComptaSQLConfElement {

    public TaxeSQLElement() {
        super("TAXE", "une taxe", "taxes");

        PredicateRowAction action = new PredicateRowAction(new AbstractAction("Définir par défaut") {

            @Override
            public void actionPerformed(ActionEvent e) {

                final SQLRowAccessor row = IListe.get(e).getSelectedRow();
                final SQLDataSource ds = row.getTable().getDBSystemRoot().getDataSource();

                try {
                    SQLUtils.executeAtomic(ds, new SQLUtils.SQLFactory<Object>() {
                        @Override
                        public Object create() throws SQLException {
                            // Transaction
                            UpdateBuilder upRemoveDefault = new UpdateBuilder(row.getTable());
                            upRemoveDefault.set("DEFAULT", "FALSE");

                            ds.execute(upRemoveDefault.asString());

                            UpdateBuilder upSetDefault = new UpdateBuilder(row.getTable());
                            upSetDefault.set("DEFAULT", "TRUE");
                            upSetDefault.setWhere(new Where(row.getTable().getKey(), "=", row.getID()));

                            ds.execute(upSetDefault.asString());

                            JOptionPane.showMessageDialog(null, "Rédémarrez " + ProductInfo.getInstance().getName() + " pour valider le changement de TVA par défaut.");

                            return null;
                        }
                    });
                } catch (Exception ex) {
                    throw new IllegalStateException("Erreur lors d'affectation de la nouvelle TVA par défaut", ex);
                }

            }
        }, true);
        action.setPredicate(IListeEvent.getSingleSelectionPredicate());

        this.getRowActions().add(action);

    }

    protected List<String> getListFields() {
        final List<String> l = new ArrayList<String>();
        l.add("NOM");
        l.add("TAUX");
        l.add("DEFAULT");
        return l;
    }

    protected List<String> getComboFields() {
        final List<String> l = new ArrayList<String>();
        l.add("NOM");
        return l;
    }

    public SQLComponent createComponent() {
        return new BaseSQLComponent(this) {

            TaxeCategorieComptableTable tableCatComptable = new TaxeCategorieComptableTable(getElement().getDirectory().getElement(TaxeCatComptableSQLElement.class));

            @Override
            protected void addViews() {

                this.setLayout(new GridBagLayout());
                GridBagConstraints c = new DefaultGridBagConstraints();
                JLabel labelNom = new JLabel(getLabelFor("NOM"), SwingConstants.RIGHT);

                this.add(labelNom, c);
                c.gridx++;
                c.weightx = 1;
                c.fill = GridBagConstraints.NONE;
                JTextField fieldNom = new JTextField(40);
                DefaultGridBagConstraints.lockMinimumSize(fieldNom);
                this.add(fieldNom, c);
                c.gridx = 0;
                c.gridy++;
                c.weightx = 0;
                c.fill = GridBagConstraints.HORIZONTAL;
                JLabel labelTaux = new JLabel(getLabelFor("TAUX"), SwingConstants.RIGHT);
                this.add(labelTaux, c);
                c.gridx++;
                c.fill = GridBagConstraints.NONE;
                JTextField fieldTaux = new JTextField(6);
                DefaultGridBagConstraints.lockMinimumSize(fieldTaux);
                this.add(fieldTaux, c);

                JLabel labelCompteCol = new JLabel(getLabelFor("ID_COMPTE_PCE_COLLECTE"), SwingConstants.RIGHT);
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx = 0;
                c.gridy++;
                this.add(labelCompteCol, c);
                c.gridx++;
                c.weightx = 1;
                c.gridwidth = GridBagConstraints.REMAINDER;
                ISQLCompteSelector compteCol = new ISQLCompteSelector();
                this.add(compteCol, c);

                JLabel labelCompteDed = new JLabel(getLabelFor("ID_COMPTE_PCE_DED"), SwingConstants.RIGHT);
                c.gridx = 0;
                c.gridy++;
                c.weightx = 0;
                c.gridwidth = 1;
                c.fill = GridBagConstraints.HORIZONTAL;
                this.add(labelCompteDed, c);
                c.gridx++;
                c.weightx = 1;

                c.gridwidth = GridBagConstraints.REMAINDER;
                ISQLCompteSelector compteDed = new ISQLCompteSelector();
                this.add(compteDed, c);

                JLabel labelCompteVente = new JLabel(getLabelFor("ID_COMPTE_PCE_VENTE"), SwingConstants.RIGHT);
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridwidth = 1;
                c.gridx = 0;
                c.gridy++;
                this.add(labelCompteVente, c);
                c.gridx++;
                c.weightx = 1;
                c.gridwidth = GridBagConstraints.REMAINDER;
                ISQLCompteSelector compteVente = new ISQLCompteSelector();
                this.add(compteVente, c);

                JLabel labelCompteVenteS = new JLabel(getLabelFor("ID_COMPTE_PCE_VENTE_SERVICE"), SwingConstants.RIGHT);
                c.gridx = 0;
                c.gridy++;
                c.weightx = 0;
                c.gridwidth = 1;
                c.fill = GridBagConstraints.HORIZONTAL;
                this.add(labelCompteVenteS, c);
                c.gridx++;
                c.weightx = 1;

                c.gridwidth = GridBagConstraints.REMAINDER;
                ISQLCompteSelector compteVenteS = new ISQLCompteSelector();
                this.add(compteVenteS, c);

                JLabel labelCompteColIntra = new JLabel(getLabelFor("ID_COMPTE_PCE_COLLECTE_INTRA"), SwingConstants.RIGHT);
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx = 0;
                c.gridy++;
                c.gridwidth = 1;
                c.weightx = 0;
                this.add(labelCompteColIntra, c);
                c.gridx++;
                c.weightx = 1;
                c.gridwidth = GridBagConstraints.REMAINDER;
                ISQLCompteSelector compteColIntra = new ISQLCompteSelector();
                this.add(compteColIntra, c);

                JLabel labelCompteDedIntra = new JLabel(getLabelFor("ID_COMPTE_PCE_DED_INTRA"), SwingConstants.RIGHT);
                c.gridx = 0;
                c.gridy++;
                c.weightx = 0;
                c.gridwidth = 1;
                c.fill = GridBagConstraints.HORIZONTAL;
                this.add(labelCompteDedIntra, c);
                c.gridx++;
                c.weightx = 1;

                c.gridwidth = GridBagConstraints.REMAINDER;
                ISQLCompteSelector compteDedIntra = new ISQLCompteSelector();
                this.add(compteDedIntra, c);

                c.gridy++;
                c.gridx = 0;
                c.weighty = 1;
                c.weightx = 1;
                c.fill = GridBagConstraints.BOTH;
                c.gridwidth = GridBagConstraints.REMAINDER;

                this.add(createCategorieComptablePanel(), c);

                this.addSQLObject(compteCol, "ID_COMPTE_PCE_COLLECTE");
                this.addSQLObject(compteDed, "ID_COMPTE_PCE_DED");
                this.addSQLObject(compteVente, "ID_COMPTE_PCE_VENTE");
                this.addSQLObject(compteVenteS, "ID_COMPTE_PCE_VENTE_SERVICE");
                this.addSQLObject(compteColIntra, "ID_COMPTE_PCE_COLLECTE_INTRA");
                this.addSQLObject(compteDedIntra, "ID_COMPTE_PCE_DED_INTRA");

                this.addRequiredSQLObject(fieldNom, "NOM");
                this.addRequiredSQLObject(fieldTaux, "TAUX");
            }

            @Override
            public void select(SQLRowAccessor r) {
                super.select(r);
                if (r != null) {
                    this.tableCatComptable.getRowValuesTable().insertFrom(r);
                }
            }

            @Override
            public int insert(SQLRow order) {
                int id = super.insert(order);
                this.tableCatComptable.updateField("ID_TAXE", id);
                return id;
            }

            @Override
            public void update() {
                super.update();
                this.tableCatComptable.updateField("ID_TAXE", getSelectedID());
            }

            protected JPanel createCategorieComptablePanel() {
                JPanel panel = new JPanel(new GridBagLayout());
                panel.setOpaque(false);
                GridBagConstraints c = new DefaultGridBagConstraints();

                // Ajout catégorie
                c.gridwidth = 1;
                c.weightx = 0;
                c.gridy++;
                c.gridx = 0;
                panel.add(new JLabel("Ajouter la catégorie "), c);

                final ElementComboBox boxCat = new ElementComboBox();
                boxCat.init(getDirectory().getElement("CATEGORIE_COMPTABLE"));

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

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

                c.gridwidth = GridBagConstraints.REMAINDER;
                c.fill = GridBagConstraints.BOTH;
                c.weightx = 1;
                c.weighty = 1;
                c.gridy++;
                c.gridx = 0;
                c.fill = GridBagConstraints.BOTH;
                this.tableCatComptable.setOpaque(false);
                panel.add(this.tableCatComptable, c);

                // Listeners
                buttonAjouter.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {

                        SQLRow rowCat = boxCat.getSelectedRow();
                        if (rowCat == null || rowCat.isUndefined()) {
                            return;
                        }
                        int nbRows = tableCatComptable.getModel().getRowCount();

                        for (int i = 0; i < nbRows; i++) {
                            SQLRowValues rowVals = tableCatComptable.getModel().getRowValuesAt(i);
                            int idTarif = Integer.parseInt(rowVals.getObject("ID_CATEGORIE_COMPTABLE").toString());
                            if (idTarif == rowCat.getID()) {
                                JOptionPane.showMessageDialog(null, "Impossible d'ajouter.\nLa catégorie est déjà présente dans la liste!");
                                return;
                            }
                        }

                        SQLRowValues rowVals = new SQLRowValues(Configuration.getInstance().getBase().getTable("TAXE_CATEGORIE_COMPTABLE"));
                        if (getSelectedID() > 1) {
                            rowVals.put("ID_TAXE", getSelectedID());
                        }
                        rowVals.put("ID_CATEGORIE_COMPTABLE", rowCat.getID());
                        tableCatComptable.getModel().addRow(rowVals);
                    }
                });
                buttonSupprimer.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        tableCatComptable.removeSelectedRow();
                    }
                });
                return panel;
            }
        };
    }

    @Override
    protected String createCode() {
        return "finance.tax";
    }
}