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.sales.product.element;

import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
import org.openconcerto.sql.element.BaseSQLComponent;
import org.openconcerto.sql.element.SQLComponent;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.utils.DecimalUtils;
import org.openconcerto.utils.StringUtils;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;

public class ArticleTarifSQLElement extends ComptaSQLConfElement {

    public ArticleTarifSQLElement() {
        super("ARTICLE_TARIF", "un tarif d'article", "tarifs d'article");
    }

    protected List<String> getListFields() {
        final List<String> l = new ArrayList<String>();
        l.add("ID_TARIF");
        l.add("ID_ARTICLE");
        l.add("QTE");
        l.add("PV_HT");
        l.add("PV_TTC");
        return l;
    }

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

    @Override
    protected String getParentFFName() {
        return "ID_ARTICLE";
    }

    @Override
    public Set<String> getInsertOnlyFields() {
        Set<String> req = new HashSet<>();
        req.add("ID_TARIF");
        req.add("ID_ARTICLE");
        return req;
    }

    @Override
    public Set<String> getReadOnlyFields() {
        Set<String> ro = new HashSet<>();
        ro.add("PV_HT");
        ro.add("PV_TTC");
        return ro;
    }

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

            public void addViews() {
                this.setLayout(new GridBagLayout());
                final GridBagConstraints c = new DefaultGridBagConstraints();

                c.gridx = 0;
                c.weightx = 0;
                // Nom
                this.add(new JLabel(getLabelFor("ID_TARIF")), c);
                c.gridx++;
                c.weightx = 1;
                final SQLRequestComboBox tarifCombo = new SQLRequestComboBox();
                this.add(tarifCombo, c);
                this.addRequiredSQLObject(tarifCombo, "ID_TARIF");

                c.gridy++;
                c.gridx = 0;
                c.weightx = 0;
                this.add(new JLabel(getLabelFor("ID_ARTICLE")), c);
                c.gridx++;
                c.weightx = 1;
                final SQLRequestComboBox artCombo = new SQLRequestComboBox();
                this.add(artCombo, c);
                this.addRequiredSQLObject(artCombo, "ID_ARTICLE");

                c.gridy++;
                c.gridx = 0;
                c.weightx = 0;
                JLabel labelQTE = new JLabel(getLabelFor("QTE"));
                JTextField textQTE = new JTextField();
                this.add(labelQTE, c);
                c.gridx++;
                c.weightx = 1;
                this.add(textQTE, c);
                this.addRequiredSQLObject(textQTE, "QTE");

                c.gridy++;
                c.gridx = 0;
                c.weightx = 0;
                JLabel labelRemise = new JLabel(getLabelFor("POURCENT_REMISE"));
                JTextField textRemise = new JTextField();
                this.add(labelRemise, c);
                c.gridx++;
                c.weightx = 1;
                this.add(textRemise, c);
                this.addSQLObject(textRemise, "POURCENT_REMISE");

                c.gridy++;
                c.gridx = 0;
                c.weightx = 0;
                // Nom
                JLabel labelPVM = new JLabel(getLabelFor("PRIX_METRIQUE_VT_1"));
                JTextField textPVM = new JTextField();
                this.add(labelPVM, c);
                c.gridx++;
                c.weightx = 1;
                this.add(textPVM, c);
                this.addSQLObject(textPVM, "PRIX_METRIQUE_VT_1");

                c.gridy++;
                c.gridx = 0;
                c.weightx = 0;
                JLabel labelPV = new JLabel(getLabelFor("PV_HT"));
                final JTextField textPV = new JTextField();
                this.add(labelPV, c);
                c.gridx++;
                c.weightx = 1;
                this.add(textPV, c);
                this.addSQLObject(textPV, "PV_HT");

                c.gridy++;
                c.gridx = 0;
                c.weightx = 0;
                this.add(new JLabel(getLabelFor("ID_TAXE")), c);
                c.gridx++;
                c.weightx = 1;
                final SQLRequestComboBox taxeCombo = new SQLRequestComboBox();
                this.add(taxeCombo, c);
                this.addSQLObject(taxeCombo, "ID_TAXE");

                c.gridy++;
                c.gridx = 0;
                c.weightx = 0;
                // Nom
                JLabel labelPVTTC = new JLabel(getLabelFor("PV_TTC"));
                final JTextField textTTC = new JTextField();
                this.add(labelPVTTC, c);
                c.gridx++;
                c.weightx = 1;
                this.add(textTTC, c);
                this.addSQLObject(textTTC, "PV_TTC");

                textPVM.getDocument().addDocumentListener(new org.openconcerto.utils.text.SimpleDocumentListener() {
                    @Override
                    public void update(DocumentEvent e) {

                        final BigDecimal ht = StringUtils.getBigDecimalFromUserText(textPVM.getText());

                        if (ht != null) {

                            SQLRowAccessor rTaxe = taxeCombo.getSelectedRowAccessor();
                            if (rTaxe == null || rTaxe.isUndefined()) {
                                SQLRowAccessor a = artCombo.getSelectedRowAccessor();
                                if (a != null && !a.isUndefined()) {
                                    rTaxe = a.asRow().fetchNewRow().getForeignRow("ID_TAXE");
                                }
                                if (rTaxe == null || rTaxe.isUndefined()) {
                                    rTaxe = TaxeCache.getCache().getFirstTaxe();
                                }
                            }

                            Float resultTaux = TaxeCache.getCache().getTauxFromId(rTaxe.getID());

                            if (resultTaux == null) {
                                final SQLRow i = TaxeCache.getCache().getFirstTaxe();
                                resultTaux = i.getFloat("TAUX");
                            }

                            float taux = (resultTaux == null) ? 0.0F : resultTaux.floatValue();

                            BigDecimal r = ht.multiply(BigDecimal.valueOf(taux).divide(BigDecimal.valueOf(100)).add(BigDecimal.ONE), DecimalUtils.HIGH_PRECISION);
                            textPV.setText(textPVM.getText());
                            textTTC.setText(r.setScale(2, RoundingMode.HALF_UP).toString());
                        } else {
                            textPV.setText("");
                            textTTC.setText("");
                        }
                    }
                });

            }
        };
    }

    @Override
    public SQLRowValues createDefaultRowValues(String token) {

        SQLRowValues rowVals = super.createDefaultRowValues(token);
        rowVals.put("QTE", 1);
        return rowVals;
    }

    @Override
    protected String createCode() {
        return createCodeOfPackage() + ".price";
    }
}