OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | 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.common.ui;

import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.erp.model.PrixHT;
import org.openconcerto.erp.model.PrixTTC;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.model.SQLBackgroundTableCache;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.sqlobject.ElementComboBox;
import org.openconcerto.utils.GestionDevise;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/***************************************************************************************************
 * SELECTION D'UN MONTANT TTC OU HT + VALEUR TVA
 * 
 * Pour initialiser le ComboSelection --> addRequiredSQLObject(montant.getChoixTaxe(), "ID_TAXE");
 * --> setChoixTaxe(String value);
 **************************************************************************************************/

public class MontantPanel extends JPanel {

    private JRadioButton checkHT;
    private JRadioButton checkTTC;
    private DeviseField textHT;
    private DeviseField textTTC;
    private DeviseField textTaxe;
    private ElementComboBox comboTaxe;
    private boolean ue = false;
    private boolean enabled = true;
    private JLabel labelUE = new JLabel("Calcul d'une TVA intracommunautaire");

    private final DocumentListener listenerTextHT = new DocumentListener() {
        public void changedUpdate(DocumentEvent e) {
            calculMontant();
        }

        public void removeUpdate(DocumentEvent e) {
            calculMontant();
        }

        public void insertUpdate(DocumentEvent e) {
            calculMontant();
        }
    };

    private final DocumentListener listenerTextTTC = new DocumentListener() {
        public void changedUpdate(DocumentEvent e) {
            calculMontant();
        }

        public void removeUpdate(DocumentEvent e) {
            calculMontant();
        }

        public void insertUpdate(DocumentEvent e) {
            calculMontant();
        }
    };
    private final DocumentListener listenerTextTaxe = new DocumentListener() {
        public void changedUpdate(DocumentEvent e) {

            if (MontantPanel.this.enabled) {
                long taxe = GestionDevise.parseLongCurrency(MontantPanel.this.textTaxe.getText());
                if (MontantPanel.this.checkHT.isSelected()) {
                    long ht = GestionDevise.parseLongCurrency(MontantPanel.this.textHT.getText());
                    long ttc = taxe + ht;
                    MontantPanel.this.textTTC.getDocument().removeDocumentListener(MontantPanel.this.listenerTextTTC);
                    MontantPanel.this.textTTC.setText(GestionDevise.currencyToString(ttc));
                    MontantPanel.this.textTTC.getDocument().addDocumentListener(MontantPanel.this.listenerTextTTC);
                } else {
                    long ttc = GestionDevise.parseLongCurrency(MontantPanel.this.textTTC.getText());
                    long ht = ttc - taxe;
                    MontantPanel.this.textHT.getDocument().removeDocumentListener(MontantPanel.this.listenerTextHT);
                    MontantPanel.this.textHT.setText(GestionDevise.currencyToString(ht));
                    MontantPanel.this.textHT.getDocument().addDocumentListener(MontantPanel.this.listenerTextHT);
                }
            }

        }

        public void removeUpdate(DocumentEvent e) {
            changedUpdate(e);
        }

        public void insertUpdate(DocumentEvent e) {
            changedUpdate(e);
        }
    };

    public MontantPanel() {
        uiInit();
    }

    private void uiInit() {
        this.setOpaque(false);
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(2, 2, 1, 2);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.WEST;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        this.labelUE.setVisible(this.ue);

        /*******************************************************************************************
         * MONTANT HT
         ******************************************************************************************/

        this.checkHT = new JRadioButton("HT");
        this.checkHT.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setHT(true);
            }
        });
        c.weightx = 0;
        this.add(this.checkHT, c);

        this.textHT = new DeviseField();

        c.gridx++;
        c.weightx = 1;
        this.add(this.textHT, c);

        /*******************************************************************************************
         * CHOIX TAXE
         ******************************************************************************************/
        c.gridx++;
        c.weightx = 0;
        this.add(new JLabel("TVA"), c);
        // choix taxe
        c.insets = new Insets(2, 10, 1, 2);
        this.comboTaxe = new ElementComboBox(false, 8);
        this.comboTaxe.setButtonsVisible(false);

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

        this.comboTaxe.addValueListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                calculMontant();
            }
        });
        c.insets = new Insets(2, 2, 1, 2);

        // Montant taxe
        this.textTaxe = new DeviseField();
        this.textTaxe.setEditable(false);
        this.textTaxe.setEnabled(false);
        c.gridx++;
        c.weightx = 0;
        this.add(this.textTaxe, c);

        /*******************************************************************************************
         * MONTANT TTC
         ******************************************************************************************/

        this.checkTTC = new JRadioButton("TTC");
        this.checkTTC.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setHT(false);
            }
        });

        c.gridy++;
        c.gridx = 0;
        c.weightx = 0;
        this.add(this.checkTTC, c);
        this.textTTC = new DeviseField();
        c.gridx++;
        c.weightx = 1;
        this.add(this.textTTC, c);
        c.gridx++;
        c.gridwidth = GridBagConstraints.REMAINDER;
        this.labelUE.setHorizontalAlignment(SwingConstants.CENTER);
        this.add(this.labelUE, c);

        ButtonGroup grp1 = new ButtonGroup();
        grp1.add(this.checkTTC);
        grp1.add(this.checkHT);
        this.checkHT.setSelected(true);
        setHT(true);
        this.textTTC.getDocument().addDocumentListener(this.listenerTextTTC);
        this.textHT.getDocument().addDocumentListener(this.listenerTextHT);
        this.textTaxe.getDocument().addDocumentListener(this.listenerTextTaxe);
    }

    private void setHT(boolean b) {
        if (b) {
            this.textHT.setEditable(true);
            this.textHT.setEnabled(true);
            this.textTTC.setEditable(false);
            this.textTTC.setEnabled(false);
        } else {
            this.textHT.setEditable(false);
            this.textHT.setEnabled(false);
            this.textTTC.setEditable(true);
            this.textTTC.setEnabled(true);
        }
    }

    public void calculMontant() {

        if (this.enabled) {

            float taux;
            PrixHT pHT;
            PrixTTC pTTC;
            // taux de la TVA selectionnee
            int idTaxe = this.comboTaxe.getSelectedId();
            if (idTaxe > 1) {
                SQLRow ligneTaxe = SQLBackgroundTableCache.getInstance().getCacheForTable(((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("TAXE"))
                        .getRowFromId(idTaxe);
                taux = (ligneTaxe.getFloat("TAUX")) / 100.0F;

                // calcul des montants HT ou TTC
                if (this.checkHT.isSelected()) {

                    if (this.textHT.getText().trim().length() > 0) {
                        pHT = new PrixHT(0);
                        try {
                            if (!this.textHT.getText().trim().equals("-")) {
                                pHT = new PrixHT(GestionDevise.parseLongCurrency(this.textHT.getText()));
                            }
                        } catch (Exception e) {
                            // text is not corret, default to 0
                        }
                        // affichage
                        String tva = GestionDevise.currencyToString(pHT.calculLongTVA(taux));

                        String ttc;
                        if (this.ue) {
                            ttc = this.textHT.getText();
                        } else {
                            ttc = GestionDevise.currencyToString(pHT.calculLongTTC(taux));
                        }
                        updateText(tva, ttc, pHT.toString(), true);
                    } else {
                        updateText("", "", "", true);
                    }
                } else {

                    if (this.textTTC.getText().trim().length() > 0) {

                        pTTC = new PrixTTC(0);
                        try {
                            if (!this.textTTC.getText().trim().equals("-")) {
                                pTTC = new PrixTTC(GestionDevise.parseLongCurrency(this.textTTC.getText()));
                            }
                        } catch (Exception e) {
                            // text is not corret, default to 0
                        }
                        String tva;
                        // affichage
                        if (this.ue) {
                            PrixHT prixHT = new PrixHT(pTTC.getLongValue());
                            tva = GestionDevise.currencyToString(prixHT.calculLongTVA(taux));
                        } else {
                            tva = GestionDevise.currencyToString(pTTC.calculLongTVA(taux));
                        }
                        String ht;
                        if (this.ue) {
                            ht = this.textTTC.getText();
                        } else {
                            ht = GestionDevise.currencyToString(pTTC.calculLongHT(taux));
                        }
                        updateText(tva, pTTC.toString(), ht, false);
                    } else
                        updateText("", "", "", false);
                }
            }
        }
    }

    private void updateText(final String prixTVA, final String prixTTC, final String prixHT, final boolean isHT) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                MontantPanel.this.textHT.getDocument().removeDocumentListener(MontantPanel.this.listenerTextHT);
                MontantPanel.this.textTTC.getDocument().removeDocumentListener(MontantPanel.this.listenerTextTTC);
                MontantPanel.this.textTaxe.getDocument().removeDocumentListener(MontantPanel.this.listenerTextTaxe);
                // Update text with formated values
                if (!isHT) {
                    MontantPanel.this.textHT.setText(prixHT);
                } else {
                    MontantPanel.this.textTTC.setText(prixTTC);
                }
                MontantPanel.this.textTaxe.setText(prixTVA);
                setHT(isHT);
                MontantPanel.this.textHT.getDocument().addDocumentListener(MontantPanel.this.listenerTextHT);
                MontantPanel.this.textTTC.getDocument().addDocumentListener(MontantPanel.this.listenerTextTTC);
                MontantPanel.this.textTaxe.getDocument().addDocumentListener(MontantPanel.this.listenerTextTaxe);
            }
        });
    }

    public void setEnabled(boolean b) {
        this.enabled = b;
        this.checkHT.setEnabled(b);
        this.checkTTC.setEnabled(b);
        this.setHT(this.checkHT.isSelected());
    }

    public DeviseField getMontantTTC() {
        return this.textTTC;
    }

    public DeviseField getMontantHT() {
        return this.textHT;
    }

    public DeviseField getMontantTVA() {
        return this.textTaxe;
    }

    public ElementComboBox getChoixTaxe() {
        return this.comboTaxe;
    }

    public void setChoixTaxe(int value) {
        this.comboTaxe.setValue(value);
    }

    public void setUE(boolean b) {
        this.ue = b;
        this.labelUE.setVisible(b);
        calculMontant();
    }

}