OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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