OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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