OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | 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.sales.invoice.element;

import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
import org.openconcerto.erp.core.common.ui.AbstractVenteArticleItemTable;
import org.openconcerto.erp.preferences.DefaultNXProps;
import org.openconcerto.erp.rights.NXRights;
import org.openconcerto.sql.element.BaseSQLComponent;
import org.openconcerto.sql.element.SQLComponent;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLInjector;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.sqlobject.ElementComboBox;
import org.openconcerto.sql.users.rights.UserRights;
import org.openconcerto.sql.users.rights.UserRightsManager;
import org.openconcerto.sql.view.EditFrame;
import org.openconcerto.sql.view.EditPanel.EditMode;
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.sql.view.list.SQLTableModelSource;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.component.InteractionMode;
import org.openconcerto.utils.DecimalUtils;
import org.openconcerto.utils.cc.ITransformer;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class SaisieVenteFactureItemSQLElement extends ComptaSQLConfElement {

    private final ComptaPropsConfiguration conf;

    public SaisieVenteFactureItemSQLElement(final ComptaPropsConfiguration conf) {
        super(conf.getRootSociete().getTable("SAISIE_VENTE_FACTURE_ELEMENT"));
        this.conf = conf;
        PredicateRowAction action = new PredicateRowAction(new AbstractAction("Transfert vers commande fournisseur") {

            @Override
            public void actionPerformed(ActionEvent e) {
                SQLElement cmd = getDirectory().getElement("COMMANDE");
                EditFrame f = new EditFrame(cmd, EditMode.CREATION);
                f.getSQLComponent().select(transfertCommande(IListe.get(e).getSelectedRows()));
                f.setVisible(true);
            }
        }, false);
        action.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
        getRowActions().add(action);
    }

    public final ComptaPropsConfiguration getConf() {
        return this.conf;
    }
    
    @Override
    protected String getParentFFName() {

        return "ID_SAISIE_VENTE_FACTURE";
    }

    /**
     * Transfert d'une commande en commande fournisseur
     * 
     * @param commandeID
     */
    public SQLRowValues transfertCommande(List<SQLRowValues> factItems) {
        SQLTable tableCmd = getDirectory().getElement("COMMANDE").getTable();
        SQLTable tableCmdElt = getDirectory().getElement("COMMANDE_ELEMENT").getTable();
        SQLRowValues rowCmd = new SQLRowValues(tableCmd);

        final Map<Integer, SQLRowValues> map = new HashMap<Integer, SQLRowValues>();
        for (SQLRowAccessor sqlRow : factItems) {
            // on récupére l'article qui lui correspond
            SQLRow sqlRowFetch = sqlRow.asRow();
            sqlRowFetch.fetchValues();
            if (sqlRowFetch.getObject("ID_ARTICLE") != null && !sqlRowFetch.getForeign("ID_ARTICLE").isUndefined()) {
                int idArt = sqlRowFetch.getForeignID("ID_ARTICLE");
                if (map.containsKey(idArt)) {
                    SQLRowValues rowValsElt = map.get(idArt);
                    rowValsElt.put("QTE", rowValsElt.getInt("QTE") + sqlRowFetch.getInt("QTE"));
                } else {
                    SQLInjector inj = SQLInjector.getInjector(sqlRowFetch.getForeign("ID_ARTICLE").getTable(), tableCmdElt);
                    SQLRowValues rowValsElt = new SQLRowValues(inj.createRowValuesFrom(sqlRowFetch.getForeign("ID_ARTICLE").asRow()));
                    rowValsElt.put("ID_STYLE", sqlRowFetch.getObject("ID_STYLE"));
                    rowValsElt.put("QTE", sqlRowFetch.getObject("QTE"));
                    rowValsElt.put("T_POIDS", sqlRowFetch.getLong("POIDS") * rowValsElt.getInt("QTE"));
                    rowValsElt.put("T_PA_HT", ((BigDecimal) sqlRowFetch.getObject("PA_HT")).multiply(new BigDecimal(rowValsElt.getInt("QTE")), DecimalUtils.HIGH_PRECISION));
                    rowValsElt.put("T_PA_TTC",
                            ((BigDecimal) sqlRowFetch.getObject("T_PA_HT")).multiply(new BigDecimal((sqlRowFetch.getForeign("ID_TAXE").getFloat("TAUX") / 100.0 + 1.0)), DecimalUtils.HIGH_PRECISION));
                    rowValsElt.put("ID_COMMANDE", rowCmd);
                    map.put(idArt, rowValsElt);
                }
            }

        }
        return rowCmd;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.openconcerto.devis.BaseSQLElement#getComboFields()
     */
    protected List<String> getComboFields() {
        List<String> l = new ArrayList<String>();
        l.add("NIVEAU");
        l.add("CODE");
        l.add("NOM");
        l.add("PV_HT");
        l.add("ID_TAXE");
        l.add("POIDS");
        return l;
    }

    @Override
    protected synchronized void _initTableSource(final SQLTableModelSource table) {
        super._initTableSource(table);
    }

    @Override
    public Set<String> getReadOnlyFields() {
        Set<String> s = new HashSet<String>();
        s.add("CODE");
        s.add("NOM");
        s.add("PV_HT");
        s.add("POIDS");
        s.add("ID_TAXE");
        return super.getReadOnlyFields();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.openconcerto.devis.BaseSQLElement#getListFields()
     */
    protected List<String> getListFields() {
        List<String> l = new ArrayList<String>();
        l.add("ID_SAISIE_VENTE_FACTURE");

            l.add("CODE");
            l.add("NOM");
            l.add("DESCRIPTIF");
            String articleAdvanced = DefaultNXProps.getInstance().getStringProperty("ArticleModeVenteAvance");
            Boolean bArticleAdvanced = Boolean.valueOf(articleAdvanced);
            if (bArticleAdvanced) {
                l.add("PRIX_METRIQUE_VT_1");
                l.add("ID_MODE_VENTE_ARTICLE");
            }
            if (UserRightsManager.getCurrentUserRights().haveRight(AbstractVenteArticleItemTable.SHOW_PRIX_ACHAT_CODE)) {
                l.add("PA_HT");
            }
            l.add("PV_HT");
        l.add("QTE");
        if (getTable().contains("ID_ECO_CONTRIBUTION")) {
            l.add("ID_ECO_CONTRIBUTION");
            l.add("ECO_CONTRIBUTION");
            l.add("T_ECO_CONTRIBUTION");
        }
        l.add("T_PA_HT");
        l.add("T_PV_HT");
        l.add("T_PV_TTC");

        return l;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.openconcerto.devis.SQLElement#getComponent()
     */
    public SQLComponent createComponent() {
        return new BaseSQLComponent(this) {
            JTextField textCode = new JTextField();
            JTextField textNom = new JTextField();
            JTextField textMontantHT = new JTextField();
            JTextField textMontantHA = new JTextField();
            ElementComboBox comboTaxe = new ElementComboBox();
            JTextField textPoids = new JTextField();
            JTextField textQte = new JTextField();

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

                // Code
                JLabel labelCode = new JLabel(getLabelFor("CODE"));
                c.weightx = 0;
                this.add(labelCode, c);

                c.weightx = 1;
                c.gridx++;
                textCode.setEditable(false);
                this.add(textCode, c);

                // Libelle
                JLabel labelNom = new JLabel(getLabelFor("NOM"));
                c.weightx = 0;
                c.gridx++;
                this.add(labelNom, c);

                c.weightx = 1;
                c.gridx++;
                textNom.setEditable(false);
                this.add(textNom, c);

                // Montant HT
                JLabel labelMontantHT = new JLabel(getLabelFor("PV_HT"));
                c.weightx = 0;
                c.gridy++;
                c.gridx = 0;
                this.add(labelMontantHT, c);

                c.weightx = 1;
                c.gridx++;
                textMontantHT.setEditable(false);
                this.add(textMontantHT, c);

                // PA HT
                JLabel labelMontantHA = new JLabel(getLabelFor("PA_HT"));
                c.weightx = 0;
                c.gridy++;
                c.gridx = 0;
                this.add(labelMontantHA, c);

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

                // Taxe
                c.gridx++;
                c.weightx = 0;
                c.gridwidth = GridBagConstraints.REMAINDER;
                comboTaxe.setEnabled(false);
                this.add(comboTaxe, c);

                // Poids
                JLabel labelPoids = new JLabel(getLabelFor("POIDS"));
                c.weightx = 0;
                c.gridy++;
                c.gridx = 0;
                c.gridwidth = 1;
                this.add(labelPoids, c);

                c.weightx = 1;
                c.gridx++;
                textPoids.setEditable(false);
                this.add(textPoids, c);

                // Quantité
                JLabel labelQte = new JLabel(getLabelFor("QTE"));
                // JLabel labelQte = new JLabel("TOTO");
                c.weightx = 0;
                c.gridx++;
                this.add(labelQte, c);

                c.weightx = 1;
                c.gridx++;
                textQte.setEditable(false);
                this.add(textQte, c);

                this.addRequiredSQLObject(textCode, "CODE");
                this.addRequiredSQLObject(textNom, "NOM");
                this.addRequiredSQLObject(textMontantHT, "PV_HT");
                this.addRequiredSQLObject(textMontantHA, "PA_HT");
                this.addSQLObject(textPoids, "POIDS");
                this.addSQLObject(textQte, "QTE");
                this.addRequiredSQLObject(comboTaxe, "ID_TAXE");
            }

            @Override
            public void select(SQLRowAccessor r) {
                // TODO Auto-generated method stub
                super.select(r);
                textCode.setEditable(false);
                textNom.setEditable(false);
                textMontantHT.setEditable(false);
                comboTaxe.setInteractionMode(InteractionMode.READ_ONLY);
                textPoids.setEditable(false);
                textQte.setEditable(false);

            }

            @Override
            public void update() {
                int id = getSelectedID();
                super.update();
                SQLRow row = getTable().getRow(id);
                final SQLRowValues createEmptyUpdateRow = row.createEmptyUpdateRow();
                createEmptyUpdateRow.put("PRIX_METRIQUE_HA_1", row.getObject("PA_HT"));
                createEmptyUpdateRow.put("T_PA_HT", row.getObject("PA_HT"));
                try {
                    createEmptyUpdateRow.commit();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
    }

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