OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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