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.finance.payment.component;
15
 
83 ilm 16
import org.openconcerto.erp.core.common.element.BanqueSQLElement;
18 ilm 17
import org.openconcerto.erp.core.common.ui.DeviseField;
18
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
174 ilm 19
import org.openconcerto.erp.core.finance.payment.element.EncaisserMontantSQLElement;
18 ilm 20
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
21
import org.openconcerto.erp.core.finance.payment.ui.EncaisseMontantTable;
22
import org.openconcerto.erp.preferences.ModeReglementDefautPrefPanel;
23
import org.openconcerto.sql.Configuration;
24
import org.openconcerto.sql.element.BaseSQLComponent;
25
import org.openconcerto.sql.element.ElementSQLObject;
73 ilm 26
import org.openconcerto.sql.element.SQLComponent;
18 ilm 27
import org.openconcerto.sql.element.SQLElement;
28
import org.openconcerto.sql.model.SQLInjector;
29
import org.openconcerto.sql.model.SQLRow;
30
import org.openconcerto.sql.model.SQLRowAccessor;
31
import org.openconcerto.sql.model.SQLRowValues;
32
import org.openconcerto.sql.model.SQLTable;
33
import org.openconcerto.sql.sqlobject.ElementComboBox;
34
import org.openconcerto.sql.view.list.RowValuesTableModel;
35
import org.openconcerto.ui.DefaultGridBagConstraints;
36
import org.openconcerto.ui.JDate;
93 ilm 37
import org.openconcerto.ui.JLabelBold;
18 ilm 38
import org.openconcerto.ui.warning.JLabelWarning;
73 ilm 39
import org.openconcerto.utils.ExceptionHandler;
18 ilm 40
import org.openconcerto.utils.GestionDevise;
41
import org.openconcerto.utils.text.SimpleDocumentListener;
42
 
43
import java.awt.GridBagConstraints;
44
import java.awt.GridBagLayout;
45
import java.awt.Insets;
46
import java.sql.SQLException;
47
import java.util.Calendar;
48
import java.util.Collections;
49
import java.util.Comparator;
50
import java.util.List;
51
 
149 ilm 52
import javax.swing.JCheckBox;
18 ilm 53
import javax.swing.JLabel;
54
import javax.swing.JTextField;
55
import javax.swing.SwingConstants;
56
import javax.swing.event.DocumentEvent;
57
import javax.swing.event.TableModelEvent;
58
import javax.swing.event.TableModelListener;
59
 
60
public class EncaisserMontantSQLComponent extends BaseSQLComponent {
61
 
62
    private EncaisseMontantTable table = new EncaisseMontantTable();
63
 
64
    private JTextField nom = new JTextField();
93 ilm 65
    private DeviseField montant = new DeviseField(10);
21 ilm 66
    private JLabel labelWarning = new JLabelWarning("Le montant est trop élevé!");
18 ilm 67
 
68
    private JDate date;
69
 
70
    public EncaisserMontantSQLComponent(SQLElement elt) {
71
        super(elt);
72
    }
73
 
74
    @Override
75
    public void addViews() {
76
        this.setLayout(new GridBagLayout());
77
 
78
        final GridBagConstraints c = new DefaultGridBagConstraints();
79
 
80
        c.gridwidth = GridBagConstraints.REMAINDER;
81
        c.weightx = 1;
93 ilm 82
        this.add(new JLabelBold("Echéances"), c);
18 ilm 83
        c.gridy++;
84
        c.weighty = 1;
85
        c.fill = GridBagConstraints.BOTH;
86
        this.add(this.table, c);
87
        this.table.getRowValuesTable().setEnabled(false);
88
        c.fill = GridBagConstraints.HORIZONTAL;
89
        c.gridwidth = 1;
90
        c.gridy++;
91
        c.weighty = 0;
92
 
93
        // Client
94
        final ElementComboBox comboClient = new ElementComboBox(true, 25);
95
        c.gridx = 0;
96
        c.gridy++;
97
        c.weightx = 0;
93 ilm 98
        this.add(new JLabel(getLabelFor("ID_CLIENT"), SwingConstants.RIGHT), c);
18 ilm 99
 
100
        c.gridx++;
101
        c.weightx = 1;
93 ilm 102
        c.gridwidth = 1;
18 ilm 103
        this.add(comboClient, c);
104
        c.gridwidth = 1;
105
 
106
        // Date
107
        this.date = new JDate(true);
108
        c.gridx = GridBagConstraints.RELATIVE;
109
        c.weightx = 0;
110
        c.gridwidth = 1;
111
        this.add(new JLabel("Date"), c);
112
        // c.gridx++;
93 ilm 113
        c.weightx = 0;
18 ilm 114
        this.add(this.date, c);
115
 
116
        this.addSQLObject(comboClient, "ID_CLIENT");
149 ilm 117
 
118
        final ElementComboBox comboD = new ElementComboBox();
119
        this.addSQLObject(comboD, "ID_DEVIS");
120
 
18 ilm 121
        // Nom
122
        c.gridy++;
123
        c.gridx = 0;
93 ilm 124
        final JLabel label = new JLabel(getLabelFor("NOM"), SwingConstants.RIGHT);
18 ilm 125
        c.weightx = 0;
126
        this.add(label, c);
127
        c.gridx++;
128
        c.weightx = 1;
149 ilm 129
        c.gridwidth = 2;
130
        this.add(this.nom, c);
131
        c.gridx += 2;
18 ilm 132
        c.gridwidth = GridBagConstraints.REMAINDER;
149 ilm 133
        JCheckBox box = new JCheckBox(getLabelFor("ACOMPTE"));
134
        box.setEnabled(false);
135
        this.add(box, c);
136
        addView(box, "ACOMPTE");
18 ilm 137
 
138
        // Montant
139
        c.gridwidth = 1;
140
        c.gridy++;
141
        c.gridx = 0;
142
        c.weightx = 0;
93 ilm 143
        this.add(new JLabel("Montant encaissé", SwingConstants.RIGHT), c);
18 ilm 144
 
145
        c.gridx++;
93 ilm 146
        c.gridwidth = 3;
18 ilm 147
        c.fill = GridBagConstraints.NONE;
148
        this.add(this.montant, c);
149
 
150
        // Warning
151
        c.gridx++;
21 ilm 152
        c.gridwidth = GridBagConstraints.REMAINDER;
18 ilm 153
        this.labelWarning.setHorizontalAlignment(SwingConstants.RIGHT);
154
        this.add(this.labelWarning, c);
155
 
156
        /***********************************************************************************
157
         * * MODE DE REGLEMENT
158
         **********************************************************************************/
159
        c.fill = GridBagConstraints.HORIZONTAL;
160
        c.gridwidth = GridBagConstraints.REMAINDER;
161
        c.gridx = 0;
162
        c.gridy++;
163
        c.weightx = 1;
93 ilm 164
        final JLabelBold sep = new JLabelBold("Mode de règlement");
18 ilm 165
        c.insets = new Insets(10, 2, 1, 2);
166
        this.add(sep, c);
167
        c.insets = new Insets(2, 2, 1, 2);
168
 
169
        c.gridx = 0;
170
        c.gridy++;
171
        c.gridwidth = GridBagConstraints.REMAINDER;
172
        this.addView("ID_MODE_REGLEMENT", BaseSQLComponent.REQ + ";" + BaseSQLComponent.DEC + ";" + BaseSQLComponent.SEP);
173
        final ElementSQLObject eltModeRegl = (ElementSQLObject) this.getView("ID_MODE_REGLEMENT");
174
        this.add(eltModeRegl, c);
175
 
176
        this.addRequiredSQLObject(this.date, "DATE");
177
        this.addRequiredSQLObject(this.montant, "MONTANT");
178
 
179
        this.addSQLObject(this.nom, "NOM");
156 ilm 180
 
181
        this.addSQLObject(new ElementComboBox(), "ID_COMPTE_PCE_TIERS");
182
        this.addSQLObject(new JTextField(), "TIERS");
18 ilm 183
        DefaultGridBagConstraints.lockMinimumSize(this.montant);
184
 
185
        final TableModelListener tableListener = new TableModelListener() {
186
 
187
            @Override
188
            public void tableChanged(TableModelEvent e) {
189
                final RowValuesTableModel model = table.getRowValuesTable().getRowValuesTableModel();
190
                if (e.getColumn() == TableModelEvent.ALL_COLUMNS || e.getColumn() == model.getColumnIndexForElement(table.getMontantElement())) {
191
 
192
                    final int rowCount = model.getRowCount();
193
                    long total = 0;
194
                    for (int i = 0; i < rowCount; i++) {
195
                        Number nHT = (Number) model.getValueAt(i, model.getColumnIndexForElement(table.getMontantElement()));
196
                        if (nHT != null) {
197
                            total += nHT.longValue();
198
                        }
199
                    }
200
 
201
                    montant.setText(GestionDevise.currencyToString(total));
202
 
203
                    // Selection du mode de reglement
73 ilm 204
                    if (getMode() == SQLComponent.Mode.INSERTION) {
149 ilm 205
                        if (rowCount >= 1 && model.getRowValuesAt(0).getObject("ID_MOUVEMENT_ECHEANCE") != null && !model.getRowValuesAt(0).isForeignEmpty("ID_MOUVEMENT_ECHEANCE")) {
73 ilm 206
                            final int idScr = MouvementSQLElement.getSourceId(model.getRowValuesAt(0).getInt("ID_MOUVEMENT_ECHEANCE"));
207
                            SQLTable tableMvt = Configuration.getInstance().getDirectory().getElement("MOUVEMENT").getTable();
208
                            if (idScr > 1) {
209
                                SQLRow rowMvt = tableMvt.getRow(idScr);
210
                                String source = rowMvt.getString("SOURCE");
211
                                int idSource = rowMvt.getInt("IDSOURCE");
212
                                SQLElement eltSource = Configuration.getInstance().getDirectory().getElement(source);
213
                                if (eltSource != null) {
214
                                    SQLRow rowSource = eltSource.getTable().getRow(idSource);
18 ilm 215
 
73 ilm 216
                                    if (rowSource != null) {
217
                                        SQLRow rowModeRegl = rowSource.getForeignRow("ID_MODE_REGLEMENT");
218
                                        if (rowModeRegl != null) {
219
                                            System.err.println("Set mode de règlement");
220
                                            int idTypeRegl = rowModeRegl.getInt("ID_TYPE_REGLEMENT");
221
                                            SQLTable tableModeRegl = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT").getTable();
222
                                            SQLRowValues rowVals = new SQLRowValues(tableModeRegl);
149 ilm 223
                                            if (idTypeRegl == TypeReglementSQLElement.INDEFINI) {
73 ilm 224
                                                idTypeRegl = TypeReglementSQLElement.CHEQUE;
225
                                            }
226
                                            rowVals.put("ID_TYPE_REGLEMENT", idTypeRegl);
227
                                            rowVals.put("COMPTANT", Boolean.TRUE);
228
                                            rowVals.put("AJOURS", 0);
229
                                            rowVals.put("LENJOUR", 0);
83 ilm 230
                                            rowVals.put("ID_" + BanqueSQLElement.TABLENAME, rowModeRegl.getInt("ID_" + BanqueSQLElement.TABLENAME));
73 ilm 231
                                            eltModeRegl.setValue(rowVals);
18 ilm 232
                                        }
233
                                    }
234
                                }
235
                            }
236
                        }
237
                    }
238
 
239
                }
21 ilm 240
                if (e.getColumn() == TableModelEvent.ALL_COLUMNS || e.getColumn() == model.getColumnIndexForElement(table.getMontantAReglerElement())) {
241
                    updateWarning();
242
                }
18 ilm 243
            }
244
        };
245
 
246
        this.montant.getDocument().addDocumentListener(new SimpleDocumentListener() {
247
 
248
            @Override
249
            public void update(DocumentEvent e) {
250
                table.getRowValuesTable().getRowValuesTableModel().removeTableModelListener(tableListener);
251
                updateMontant(montant.getText());
252
                table.getRowValuesTable().getRowValuesTableModel().addTableModelListener(tableListener);
21 ilm 253
                updateWarning();
18 ilm 254
 
255
            }
256
        });
257
        this.table.getRowValuesTable().getRowValuesTableModel().addTableModelListener(tableListener);
258
 
259
    }
260
 
261
    private void updateMontant(String s) {
262
 
263
        long total = 0;
264
        if (s.trim().length() > 0) {
265
            total = GestionDevise.parseLongCurrency(s);
266
        }
267
        final RowValuesTableModel model = table.getRowValuesTable().getRowValuesTableModel();
268
 
269
        final int rowCount = model.getRowCount();
270
        for (int i = 0; i < rowCount; i++) {
271
            Number nHT = (Number) model.getValueAt(i, model.getColumnIndexForElement(table.getMontantAReglerElement()));
272
            Long value = Long.valueOf(0);
273
            if (i < rowCount - 1) {
274
                if (nHT.longValue() <= total) {
275
                    value = nHT.longValue();
276
                } else {
277
                    value = total;
278
                }
279
            } else {
280
                value = total;
281
            }
282
            model.putValue(value, i, "MONTANT_REGLE");
283
            total = total - value;
284
        }
285
    }
286
 
287
    @Override
288
    public void select(SQLRowAccessor r) {
289
        super.select(r);
290
        if (r != null && r.getID() > 1) {
291
            this.table.insertFrom("ID_ENCAISSER_MONTANT", r.getID());
149 ilm 292
        } else if (r != null && r instanceof SQLRowValues) {
293
            this.table.insertFrom((SQLRowValues) r);
294
 
18 ilm 295
        }
296
    }
297
 
298
    @Override
299
    public int insert(SQLRow order) {
300
 
301
        int id = super.insert(order);
174 ilm 302
        this.table.updateField("ID_ENCAISSER_MONTANT", id);
18 ilm 303
 
174 ilm 304
        System.out.println("Génération des ecritures du reglement");
305
        SQLRow row = getTable().getRow(id);
18 ilm 306
 
174 ilm 307
        try {
308
            ((EncaisserMontantSQLElement) getElement()).regleFacture(row);
73 ilm 309
        } catch (Exception e) {
174 ilm 310
            ExceptionHandler.handle("Erreur de génération des écritures", e);
18 ilm 311
        }
174 ilm 312
 
18 ilm 313
        return id;
314
    }
315
 
316
    @Override
317
    protected SQLRowValues createDefaults() {
318
        SQLRowValues vals = new SQLRowValues(this.getTable());
319
        SQLRowAccessor r;
320
        this.table.getModel().clearRows();
321
        try {
322
            r = ModeReglementDefautPrefPanel.getDefaultRow(false);
323
            SQLElement eltModeReglement = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
324
            if (r.getID() > 1) {
325
                SQLRowValues rowVals = eltModeReglement.createCopy(r.getID());
326
                vals.put("ID_MODE_REGLEMENT", rowVals);
327
            }
328
        } catch (SQLException e) {
329
            System.err.println("Impossible de sélectionner le mode de règlement par défaut du client.");
330
            e.printStackTrace();
331
        }
332
        return vals;
333
    }
334
 
335
    // test si le montant est correct par rapport à l'echeance selectionnée
21 ilm 336
    private final void updateWarning() {
18 ilm 337
 
338
        long montantValue = 0;
339
 
340
        if (this.table.getRowValuesTable().getRowCount() == 0) {
341
            this.labelWarning.setVisible(false);
21 ilm 342
            return;
18 ilm 343
        }
344
 
345
        try {
346
            if (this.montant.getText().trim().length() != 0) {
347
                montantValue = GestionDevise.parseLongCurrency(this.montant.getText().trim());
348
            }
349
        } catch (NumberFormatException e) {
350
            System.err.println("format float incorrect " + e);
351
            e.printStackTrace();
352
        }
353
 
354
        final RowValuesTableModel model = table.getRowValuesTable().getRowValuesTableModel();
355
 
356
        final int rowCount = model.getRowCount();
357
        long total = 0;
358
        for (int i = 0; i < rowCount; i++) {
359
            Number nHT = (Number) model.getValueAt(i, model.getColumnIndexForElement(table.getMontantAReglerElement()));
360
            total += nHT.longValue();
361
        }
362
 
21 ilm 363
        this.labelWarning.setVisible(montantValue <= 0 || montantValue > total);
18 ilm 364
    }
365
 
366
    public void loadEcheancesFromRows(List<SQLRow> rows) {
367
 
368
        Collections.sort(rows, new Comparator<SQLRow>() {
369
            @Override
370
            public int compare(SQLRow o1, SQLRow o2) {
371
                Calendar c1 = o1.getDate("DATE");
372
                Calendar c2 = o2.getDate("DATE");
373
                if (c1 == null) {
374
                    return -1;
375
                }
376
                if (c2 == null) {
377
                    return 1;
378
                }
379
                if (c1.getTime().before(c2.getTime())) {
380
                    return -1;
381
                } else {
382
                    return 1;
383
                }
384
            }
385
        });
386
 
387
        SQLTable tableEch = Configuration.getInstance().getDirectory().getElement("ECHEANCE_CLIENT").getTable();
388
        SQLTable tableEnc = Configuration.getInstance().getDirectory().getElement("ENCAISSER_MONTANT_ELEMENT").getTable();
389
        SQLInjector inj = SQLInjector.getInjector(tableEch, tableEnc);
390
        for (SQLRow row : rows) {
391
 
392
            SQLRowValues rowVals = inj.createRowValuesFrom(row.getID());
393
            rowVals.put("MONTANT_REGLE", rowVals.getObject("MONTANT_A_REGLER"));
394
            table.getModel().addRow(rowVals);
395
            int rowIndex = table.getModel().getRowCount() - 1;
396
            table.getModel().fireTableModelModified(rowIndex);
397
        }
398
        this.table.getModel().fireTableDataChanged();
399
        this.table.repaint();
400
 
401
    }
402
 
403
}