OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.sales.invoice.element;
15
 
73 ilm 16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
18 ilm 17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
94 ilm 18
import org.openconcerto.erp.core.common.ui.AbstractVenteArticleItemTable;
28 ilm 19
import org.openconcerto.erp.preferences.DefaultNXProps;
149 ilm 20
import org.openconcerto.erp.rights.NXRights;
18 ilm 21
import org.openconcerto.sql.element.BaseSQLComponent;
22
import org.openconcerto.sql.element.SQLComponent;
156 ilm 23
import org.openconcerto.sql.element.SQLElement;
24
import org.openconcerto.sql.model.SQLInjector;
144 ilm 25
import org.openconcerto.sql.model.SQLRow;
26
import org.openconcerto.sql.model.SQLRowAccessor;
27
import org.openconcerto.sql.model.SQLRowValues;
149 ilm 28
import org.openconcerto.sql.model.SQLSelect;
156 ilm 29
import org.openconcerto.sql.model.SQLTable;
149 ilm 30
import org.openconcerto.sql.model.Where;
18 ilm 31
import org.openconcerto.sql.sqlobject.ElementComboBox;
149 ilm 32
import org.openconcerto.sql.users.rights.UserRights;
94 ilm 33
import org.openconcerto.sql.users.rights.UserRightsManager;
156 ilm 34
import org.openconcerto.sql.view.EditFrame;
35
import org.openconcerto.sql.view.EditPanel.EditMode;
36
import org.openconcerto.sql.view.list.IListe;
37
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
38
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
142 ilm 39
import org.openconcerto.sql.view.list.SQLTableModelSource;
18 ilm 40
import org.openconcerto.ui.DefaultGridBagConstraints;
144 ilm 41
import org.openconcerto.ui.component.InteractionMode;
156 ilm 42
import org.openconcerto.utils.DecimalUtils;
149 ilm 43
import org.openconcerto.utils.cc.ITransformer;
18 ilm 44
 
45
import java.awt.GridBagConstraints;
46
import java.awt.GridBagLayout;
156 ilm 47
import java.awt.event.ActionEvent;
48
import java.math.BigDecimal;
144 ilm 49
import java.sql.SQLException;
18 ilm 50
import java.util.ArrayList;
156 ilm 51
import java.util.HashMap;
144 ilm 52
import java.util.HashSet;
18 ilm 53
import java.util.List;
156 ilm 54
import java.util.Map;
144 ilm 55
import java.util.Set;
18 ilm 56
 
156 ilm 57
import javax.swing.AbstractAction;
18 ilm 58
import javax.swing.JLabel;
59
import javax.swing.JTextField;
60
 
61
public class SaisieVenteFactureItemSQLElement extends ComptaSQLConfElement {
62
 
156 ilm 63
    private final ComptaPropsConfiguration conf;
64
 
65
    public SaisieVenteFactureItemSQLElement(final ComptaPropsConfiguration conf) {
66
        super(conf.getRootSociete().getTable("SAISIE_VENTE_FACTURE_ELEMENT"));
67
        this.conf = conf;
68
        PredicateRowAction action = new PredicateRowAction(new AbstractAction("Transfert vers commande fournisseur") {
69
 
70
            @Override
71
            public void actionPerformed(ActionEvent e) {
72
                SQLElement cmd = getDirectory().getElement("COMMANDE");
73
                EditFrame f = new EditFrame(cmd, EditMode.CREATION);
74
                f.getSQLComponent().select(transfertCommande(IListe.get(e).getSelectedRows()));
75
                f.setVisible(true);
76
            }
77
        }, false);
78
        action.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
79
        getRowActions().add(action);
18 ilm 80
    }
81
 
156 ilm 82
    public final ComptaPropsConfiguration getConf() {
83
        return this.conf;
18 ilm 84
    }
156 ilm 85
 
132 ilm 86
    @Override
87
    protected String getParentFFName() {
88
 
89
        return "ID_SAISIE_VENTE_FACTURE";
90
    }
91
 
156 ilm 92
    /**
93
     * Transfert d'une commande en commande fournisseur
94
     *
95
     * @param commandeID
96
     */
97
    public SQLRowValues transfertCommande(List<SQLRowValues> factItems) {
98
        SQLTable tableCmd = getDirectory().getElement("COMMANDE").getTable();
99
        SQLTable tableCmdElt = getDirectory().getElement("COMMANDE_ELEMENT").getTable();
100
        SQLRowValues rowCmd = new SQLRowValues(tableCmd);
101
 
102
        final Map<Integer, SQLRowValues> map = new HashMap<Integer, SQLRowValues>();
103
        for (SQLRowAccessor sqlRow : factItems) {
104
            // on récupére l'article qui lui correspond
105
            SQLRow sqlRowFetch = sqlRow.asRow();
106
            sqlRowFetch.fetchValues();
107
            if (sqlRowFetch.getObject("ID_ARTICLE") != null && !sqlRowFetch.getForeign("ID_ARTICLE").isUndefined()) {
108
                int idArt = sqlRowFetch.getForeignID("ID_ARTICLE");
109
                if (map.containsKey(idArt)) {
110
                    SQLRowValues rowValsElt = map.get(idArt);
111
                    rowValsElt.put("QTE", rowValsElt.getInt("QTE") + sqlRowFetch.getInt("QTE"));
112
                } else {
113
                    SQLInjector inj = SQLInjector.getInjector(sqlRowFetch.getForeign("ID_ARTICLE").getTable(), tableCmdElt);
114
                    SQLRowValues rowValsElt = new SQLRowValues(inj.createRowValuesFrom(sqlRowFetch.getForeign("ID_ARTICLE").asRow()));
115
                    rowValsElt.put("ID_STYLE", sqlRowFetch.getObject("ID_STYLE"));
116
                    rowValsElt.put("QTE", sqlRowFetch.getObject("QTE"));
117
                    rowValsElt.put("T_POIDS", sqlRowFetch.getLong("POIDS") * rowValsElt.getInt("QTE"));
118
                    rowValsElt.put("T_PA_HT", ((BigDecimal) sqlRowFetch.getObject("PA_HT")).multiply(new BigDecimal(rowValsElt.getInt("QTE")), DecimalUtils.HIGH_PRECISION));
119
                    rowValsElt.put("T_PA_TTC",
120
                            ((BigDecimal) sqlRowFetch.getObject("T_PA_HT")).multiply(new BigDecimal((sqlRowFetch.getForeign("ID_TAXE").getFloat("TAUX") / 100.0 + 1.0)), DecimalUtils.HIGH_PRECISION));
121
                    rowValsElt.put("ID_COMMANDE", rowCmd);
122
                    map.put(idArt, rowValsElt);
123
                }
124
            }
125
 
126
        }
127
        return rowCmd;
128
    }
129
 
18 ilm 130
    /*
131
     * (non-Javadoc)
132
     *
133
     * @see org.openconcerto.devis.BaseSQLElement#getComboFields()
134
     */
135
    protected List<String> getComboFields() {
136
        List<String> l = new ArrayList<String>();
142 ilm 137
        l.add("NIVEAU");
18 ilm 138
        l.add("CODE");
139
        l.add("NOM");
140
        l.add("PV_HT");
141
        l.add("ID_TAXE");
142
        l.add("POIDS");
143
        return l;
144
    }
145
 
94 ilm 146
    @Override
142 ilm 147
    protected synchronized void _initTableSource(final SQLTableModelSource table) {
148
        super._initTableSource(table);
94 ilm 149
    }
150
 
144 ilm 151
    @Override
152
    public Set<String> getReadOnlyFields() {
153
        Set<String> s = new HashSet<String>();
154
        s.add("CODE");
155
        s.add("NOM");
156
        s.add("PV_HT");
157
        s.add("POIDS");
158
        s.add("ID_TAXE");
159
        return super.getReadOnlyFields();
160
    }
161
 
18 ilm 162
    /*
163
     * (non-Javadoc)
164
     *
165
     * @see org.openconcerto.devis.BaseSQLElement#getListFields()
166
     */
177 ilm 167
    @Override
18 ilm 168
    protected List<String> getListFields() {
169
        List<String> l = new ArrayList<String>();
170
        l.add("ID_SAISIE_VENTE_FACTURE");
177 ilm 171
        l.add("CODE");
172
        l.add("NOM");
173
        l.add("DESCRIPTIF");
174
        String articleAdvanced = DefaultNXProps.getInstance().getStringProperty("ArticleModeVenteAvance");
175
        Boolean bArticleAdvanced = Boolean.valueOf(articleAdvanced);
176
        if (bArticleAdvanced) {
177
            l.add("PRIX_METRIQUE_VT_1");
178
            l.add("ID_MODE_VENTE_ARTICLE");
179
        }
180
        if (UserRightsManager.getCurrentUserRights().haveRight(AbstractVenteArticleItemTable.SHOW_PRIX_ACHAT_CODE)) {
181
            l.add("PA_HT");
182
        }
183
        l.add("PV_HT");
18 ilm 184
        l.add("QTE");
142 ilm 185
        if (getTable().contains("ID_ECO_CONTRIBUTION")) {
186
            l.add("ID_ECO_CONTRIBUTION");
187
            l.add("ECO_CONTRIBUTION");
188
            l.add("T_ECO_CONTRIBUTION");
189
        }
149 ilm 190
        l.add("T_PA_HT");
18 ilm 191
        l.add("T_PV_HT");
192
        l.add("T_PV_TTC");
73 ilm 193
 
18 ilm 194
        return l;
195
    }
196
 
197
    /*
198
     * (non-Javadoc)
199
     *
200
     * @see org.openconcerto.devis.SQLElement#getComponent()
201
     */
202
    public SQLComponent createComponent() {
203
        return new BaseSQLComponent(this) {
144 ilm 204
            JTextField textCode = new JTextField();
205
            JTextField textNom = new JTextField();
206
            JTextField textMontantHT = new JTextField();
207
            JTextField textMontantHA = new JTextField();
208
            ElementComboBox comboTaxe = new ElementComboBox();
209
            JTextField textPoids = new JTextField();
210
            JTextField textQte = new JTextField();
18 ilm 211
 
212
            public void addViews() {
213
                this.setLayout(new GridBagLayout());
214
                final GridBagConstraints c = new DefaultGridBagConstraints();
215
 
216
                // Code
217
                JLabel labelCode = new JLabel(getLabelFor("CODE"));
218
                c.weightx = 0;
219
                this.add(labelCode, c);
220
 
221
                c.weightx = 1;
222
                c.gridx++;
144 ilm 223
                textCode.setEditable(false);
18 ilm 224
                this.add(textCode, c);
225
 
226
                // Libelle
227
                JLabel labelNom = new JLabel(getLabelFor("NOM"));
228
                c.weightx = 0;
229
                c.gridx++;
230
                this.add(labelNom, c);
231
 
232
                c.weightx = 1;
233
                c.gridx++;
144 ilm 234
                textNom.setEditable(false);
18 ilm 235
                this.add(textNom, c);
236
 
237
                // Montant HT
238
                JLabel labelMontantHT = new JLabel(getLabelFor("PV_HT"));
239
                c.weightx = 0;
240
                c.gridy++;
241
                c.gridx = 0;
242
                this.add(labelMontantHT, c);
243
 
244
                c.weightx = 1;
245
                c.gridx++;
144 ilm 246
                textMontantHT.setEditable(false);
18 ilm 247
                this.add(textMontantHT, c);
248
 
144 ilm 249
                // PA HT
250
                JLabel labelMontantHA = new JLabel(getLabelFor("PA_HT"));
251
                c.weightx = 0;
252
                c.gridy++;
253
                c.gridx = 0;
254
                this.add(labelMontantHA, c);
255
 
256
                c.weightx = 1;
257
                c.gridx++;
258
                this.add(textMontantHA, c);
259
 
18 ilm 260
                // Taxe
261
                c.gridx++;
262
                c.weightx = 0;
263
                c.gridwidth = GridBagConstraints.REMAINDER;
144 ilm 264
                comboTaxe.setEnabled(false);
18 ilm 265
                this.add(comboTaxe, c);
266
 
267
                // Poids
268
                JLabel labelPoids = new JLabel(getLabelFor("POIDS"));
269
                c.weightx = 0;
270
                c.gridy++;
271
                c.gridx = 0;
272
                c.gridwidth = 1;
273
                this.add(labelPoids, c);
274
 
275
                c.weightx = 1;
276
                c.gridx++;
144 ilm 277
                textPoids.setEditable(false);
18 ilm 278
                this.add(textPoids, c);
279
 
280
                // Quantité
281
                JLabel labelQte = new JLabel(getLabelFor("QTE"));
144 ilm 282
                // JLabel labelQte = new JLabel("TOTO");
18 ilm 283
                c.weightx = 0;
284
                c.gridx++;
285
                this.add(labelQte, c);
286
 
287
                c.weightx = 1;
288
                c.gridx++;
144 ilm 289
                textQte.setEditable(false);
18 ilm 290
                this.add(textQte, c);
291
 
292
                this.addRequiredSQLObject(textCode, "CODE");
293
                this.addRequiredSQLObject(textNom, "NOM");
294
                this.addRequiredSQLObject(textMontantHT, "PV_HT");
144 ilm 295
                this.addRequiredSQLObject(textMontantHA, "PA_HT");
18 ilm 296
                this.addSQLObject(textPoids, "POIDS");
297
                this.addSQLObject(textQte, "QTE");
298
                this.addRequiredSQLObject(comboTaxe, "ID_TAXE");
299
            }
144 ilm 300
 
301
            @Override
302
            public void select(SQLRowAccessor r) {
303
                // TODO Auto-generated method stub
304
                super.select(r);
305
                textCode.setEditable(false);
306
                textNom.setEditable(false);
307
                textMontantHT.setEditable(false);
308
                comboTaxe.setInteractionMode(InteractionMode.READ_ONLY);
309
                textPoids.setEditable(false);
310
                textQte.setEditable(false);
311
 
312
            }
313
 
314
            @Override
315
            public void update() {
316
                int id = getSelectedID();
317
                super.update();
318
                SQLRow row = getTable().getRow(id);
319
                final SQLRowValues createEmptyUpdateRow = row.createEmptyUpdateRow();
320
                createEmptyUpdateRow.put("PRIX_METRIQUE_HA_1", row.getObject("PA_HT"));
321
                createEmptyUpdateRow.put("T_PA_HT", row.getObject("PA_HT"));
322
                try {
323
                    createEmptyUpdateRow.commit();
324
                } catch (SQLException e) {
325
                    // TODO Auto-generated catch block
326
                    e.printStackTrace();
327
                }
328
            }
18 ilm 329
        };
330
    }
57 ilm 331
 
332
    @Override
333
    protected String createCode() {
156 ilm 334
        return createCodeOfPackage() + ".item";
57 ilm 335
    }
18 ilm 336
}