OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Go to most recent revision | Details | 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.ui;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.config.Gestion;
18
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
19
import org.openconcerto.erp.core.customerrelationship.customer.element.RelanceSQLElement;
20
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
21
import org.openconcerto.erp.core.finance.payment.component.EncaisserMontantSQLComponent;
22
import org.openconcerto.erp.rights.ComptaUserRight;
23
import org.openconcerto.erp.rights.NXRights;
24
import org.openconcerto.sql.Configuration;
25
import org.openconcerto.sql.element.SQLElement;
26
import org.openconcerto.sql.model.SQLBase;
27
import org.openconcerto.sql.model.SQLRow;
28
import org.openconcerto.sql.model.SQLRowValues;
29
import org.openconcerto.sql.model.SQLTable;
30
import org.openconcerto.sql.users.UserManager;
31
import org.openconcerto.sql.users.rights.UserRightsManager;
32
import org.openconcerto.sql.view.EditFrame;
33
import org.openconcerto.sql.view.EditPanelListener;
34
import org.openconcerto.sql.view.IListPanel;
35
import org.openconcerto.sql.view.IListener;
36
import org.openconcerto.ui.DefaultGridBagConstraints;
37
import org.openconcerto.utils.GestionDevise;
38
 
39
import java.awt.GridBagConstraints;
40
import java.awt.GridBagLayout;
41
import java.awt.event.ActionEvent;
42
import java.awt.event.ActionListener;
43
import java.awt.event.MouseAdapter;
44
import java.awt.event.MouseEvent;
45
import java.sql.SQLException;
46
import java.util.ArrayList;
47
import java.util.Date;
48
import java.util.List;
49
 
50
import javax.swing.AbstractAction;
51
import javax.swing.JButton;
52
import javax.swing.JCheckBox;
53
import javax.swing.JFrame;
54
import javax.swing.JOptionPane;
55
import javax.swing.JPanel;
56
import javax.swing.JPopupMenu;
57
import javax.swing.SwingConstants;
58
import javax.swing.SwingUtilities;
59
 
60
public class ListeDesEcheancesClientsPanel extends JPanel {
61
 
62
    private ListPanelEcheancesClients panelEcheances;
63
    private EditFrame editEncaisse = null;
64
    private EditFrame editRelance = null;
65
    private JButton relancer, encaisser;
66
 
67
    // TODO GEstion Relance (??? loi NRE pour le calcul des penalites)
68
    public ListeDesEcheancesClientsPanel() {
69
        this.setLayout(new GridBagLayout());
70
        final GridBagConstraints c = new DefaultGridBagConstraints();
71
 
72
        this.panelEcheances = new ListPanelEcheancesClients();
73
 
74
        // PANEL AVEC LA LISTE DES ECHEANCES
75
        c.weightx = 1;
76
        c.weighty = 1;
77
        c.gridwidth = GridBagConstraints.REMAINDER;
78
        c.fill = GridBagConstraints.BOTH;
79
        this.add(this.panelEcheances, c);
80
 
81
        c.gridx = 0;
82
        c.gridy++;
83
        c.gridwidth = 1;
84
 
85
        c.weighty = 0;
86
        c.fill = GridBagConstraints.NONE;
87
        c.weightx = 0;
88
        final JCheckBox checkRegCompta = new JCheckBox("Voir les régularisations de comptabilité");
89
        if (UserRightsManager.getCurrentUserRights().haveRight(ComptaUserRight.MENU)) {
90
            this.add(checkRegCompta, c);
91
        }
92
 
93
        checkRegCompta.addActionListener(new ActionListener() {
94
            @Override
95
            public void actionPerformed(ActionEvent e) {
96
                panelEcheances.setShowRegCompta(checkRegCompta.isSelected());
97
 
98
            }
99
        });
100
 
101
        c.weightx = 1;
102
        c.anchor = GridBagConstraints.EAST;
103
        // Bouton Relancer
104
        this.relancer = new JButton("Relancer");
105
        this.relancer.setHorizontalAlignment(SwingConstants.RIGHT);
106
        if (UserManager.getInstance().getCurrentUser().getRights().haveRight(NXRights.GESTION_ENCAISSEMENT.getCode())) {
107
            c.gridx++;
108
            this.add(this.relancer, c);
109
            this.relancer.addActionListener(new ActionListener() {
110
                public void actionPerformed(ActionEvent e) {
111
                    relanceClient();
112
                }
113
            });
114
        }
115
 
116
        // Bouton Encaisser
117
        this.encaisser = new JButton("Encaisser");
118
        this.encaisser.setHorizontalAlignment(SwingConstants.RIGHT);
119
        c.gridx++;
120
        c.weightx = 0;
121
        if (UserManager.getInstance().getCurrentUser().getRights().haveRight(NXRights.GESTION_ENCAISSEMENT.getCode())) {
122
 
123
            this.add(this.encaisser, c);
124
        }
125
        this.encaisser.addActionListener(new ActionListener() {
126
 
127
            public void actionPerformed(ActionEvent e) {
128
 
129
                List<Integer> selectedIds = ListeDesEcheancesClientsPanel.this.panelEcheances.getListe().getSelection().getSelectedIDs();
130
                List<SQLRow> selectedRows = new ArrayList<SQLRow>(selectedIds.size());
131
                int idCpt = -1;
132
                int idClient = -1;
133
                boolean showMessage = false;
134
 
135
                String numeroFact = "";
136
                for (Integer integer : selectedIds) {
137
                    final SQLRow row = ListeDesEcheancesClientsPanel.this.panelEcheances.getListe().getSource().getPrimaryTable().getRow(integer);
138
                    // System.err.println("ListeDesEcheancesClientsPanel.ListeDesEcheancesClientsPanel().new ActionListener() {...}.actionPerformed()"
139
                    // + row);
140
                    selectedRows.add(row);
141
 
142
                    String nom = row.getForeignRow("ID_MOUVEMENT").getForeignRow("ID_PIECE").getString("NOM");
143
                    numeroFact += " " + nom;
144
 
145
                    SQLRow rowClient = row.getForeignRow("ID_CLIENT");
146
                    int idTmp = rowClient.getInt("ID_COMPTE_PCE");
147
                    int idCliTmp = rowClient.getID();
148
                    if (idCpt > -1 && idCpt != idTmp) {
149
                        JOptionPane.showMessageDialog(null, "Impossible d'effectuer un encaissement sur plusieurs factures ayant des clients avec des comptes différents.");
150
                        return;
151
                    } else {
152
                        idCpt = idTmp;
153
                    }
154
 
155
                    if (idClient > -1 && idClient != idCliTmp) {
156
                        showMessage = true;
157
                    } else {
158
                        idClient = idCliTmp;
159
                    }
160
                }
161
                if (showMessage) {
162
                    int answer = JOptionPane.showConfirmDialog(null, "Attention vous avez sélectionné des factures ayant des clients différents. Voulez vous continuer?");
163
                    if (answer != JOptionPane.YES_OPTION) {
164
                        return;
165
                    }
166
 
167
                }
168
                SQLElement encaisseElt = Configuration.getInstance().getDirectory().getElement("ENCAISSER_MONTANT");
169
                if (ListeDesEcheancesClientsPanel.this.editEncaisse == null) {
170
                    ListeDesEcheancesClientsPanel.this.editEncaisse = new EditFrame(encaisseElt);
171
                    ListeDesEcheancesClientsPanel.this.editEncaisse.setIconImages(Gestion.getFrameIcon());
172
                }
173
 
174
                SQLRowValues rowVals = new SQLRowValues(encaisseElt.getTable());
175
 
176
                rowVals.put("ID_CLIENT", idClient);
177
                rowVals.put("NOM", numeroFact);
178
 
179
                final EncaisserMontantSQLComponent sqlComponent = (EncaisserMontantSQLComponent) ListeDesEcheancesClientsPanel.this.editEncaisse.getSQLComponent();
180
 
181
                sqlComponent.resetValue();
182
                sqlComponent.select(rowVals);
183
                sqlComponent.loadEcheancesFromRows(selectedRows);
184
                ListeDesEcheancesClientsPanel.this.editEncaisse.pack();
185
                ListeDesEcheancesClientsPanel.this.editEncaisse.setVisible(true);
186
            }
187
        });
188
 
189
        // Bouton Fermer
190
        c.gridx++;
191
        c.weightx = 0;
192
        JButton fermer = new JButton("fermer");
193
        this.add(fermer, c);
194
        fermer.addActionListener(new ActionListener() {
195
 
196
            public void actionPerformed(ActionEvent e) {
197
 
198
                JFrame tmpF = (JFrame) SwingUtilities.getRoot(ListeDesEcheancesClientsPanel.this);
199
                tmpF.setVisible(false);
200
                tmpF.dispose();
201
 
202
            }
203
        });
204
 
205
        // Gestion de la souris
206
        this.panelEcheances.getJTable().addMouseListener(new MouseAdapter() {
207
 
208
            public void mousePressed(MouseEvent mE) {
209
 
210
                // Mise à jour de l'echeance sur la frame de reglement
211
                // si cette derniere est cree
212
                final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
213
                final SQLRow row = panelEcheances.getListe().getSelectedRow();
214
                if (mE.getButton() == MouseEvent.BUTTON1) {
215
 
216
                    if (ListeDesEcheancesClientsPanel.this.editEncaisse != null) {
217
                        final SQLRowValues rowVals = new SQLRowValues(base.getTable("ENCAISSER_MONTANT"));
218
                        rowVals.put("ID_ECHEANCE_CLIENT", row.getID());
219
 
220
                        ListeDesEcheancesClientsPanel.this.editEncaisse.getSQLComponent().select(rowVals);
221
                        ListeDesEcheancesClientsPanel.this.editEncaisse.pack();
222
                    }
223
                }
224
 
225
                // Gestion du clic droit
226
                if (mE.getButton() == MouseEvent.BUTTON3) {
227
                    JPopupMenu menuDroit = new JPopupMenu();
228
 
229
                    menuDroit.add(new AbstractAction("Voir la source") {
230
 
231
                        public void actionPerformed(ActionEvent e) {
232
                            MouvementSQLElement.showSource(row.getInt("ID_MOUVEMENT"));
233
                        }
234
                    });
235
 
236
                    if (UserRightsManager.getCurrentUserRights().haveRight(ComptaUserRight.MENU)) {
237
                        if (row.getBoolean("REG_COMPTA")) {
238
 
239
                            menuDroit.add(new AbstractAction("Annuler la régularisation en comptabilité") {
240
 
241
                                public void actionPerformed(ActionEvent e) {
242
 
243
                                    int answer = JOptionPane.showConfirmDialog(ListeDesEcheancesClientsPanel.this, "Etes vous sûr de vouloir annuler la régularisation ?");
244
                                    if (answer == JOptionPane.YES_OPTION) {
245
 
246
                                        SQLRowValues rowVals = row.createEmptyUpdateRow();
247
                                        rowVals.put("REG_COMPTA", Boolean.FALSE);
248
                                        try {
249
                                            rowVals.commit();
250
                                        } catch (SQLException e1) {
251
                                            // TODO Auto-generated catch block
252
                                            e1.printStackTrace();
253
                                        }
254
                                    }
255
                                }
256
                            });
257
                        } else {
258
 
259
                            menuDroit.add(new AbstractAction("Régularisation en comptabilité") {
260
 
261
                                public void actionPerformed(ActionEvent e) {
262
 
263
                                    String price = GestionDevise.currencyToString(row.getLong("MONTANT"));
264
                                    SQLRow rowClient = row.getForeignRow("ID_CLIENT");
265
                                    String nomClient = rowClient.getString("NOM");
266
                                    String piece = "";
267
                                    SQLRow rowMvt = row.getForeignRow("ID_MOUVEMENT");
268
                                    if (rowMvt != null) {
269
                                        SQLRow rowPiece = rowMvt.getForeignRow("ID_PIECE");
270
                                        piece = rowPiece.getString("NOM");
271
                                    }
272
                                    int answer = JOptionPane.showConfirmDialog(ListeDesEcheancesClientsPanel.this, "Etes vous sûr de vouloir régulariser l'échéance de " + nomClient
273
                                            + " d'un montant de " + price + "€ avec une saisie au kilometre?\nNom de la piéce : " + piece + ".");
274
                                    if (answer == JOptionPane.YES_OPTION) {
275
 
276
                                        SQLRowValues rowVals = row.createEmptyUpdateRow();
277
                                        rowVals.put("REG_COMPTA", Boolean.TRUE);
278
                                        try {
279
                                            rowVals.commit();
280
                                        } catch (SQLException e1) {
281
                                            // TODO Auto-generated catch block
282
                                            e1.printStackTrace();
283
                                        }
284
                                    }
285
                                }
286
                            });
287
                        }
288
                    }
289
 
290
                    menuDroit.show(mE.getComponent(), mE.getX(), mE.getY());
291
                }
292
 
293
            }
294
        });
295
 
296
        this.panelEcheances.getListe().addIListener(new IListener() {
297
            public void selectionId(int id, int field) {
298
                if (id > 1) {
299
                    final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
300
                    final SQLTable tableEch = base.getTable("ECHEANCE_CLIENT");
301
                    final SQLRow rowEch = tableEch.getRow(id);
302
 
303
                    int idMvtSource = MouvementSQLElement.getSourceId(rowEch.getInt("ID_MOUVEMENT"));
304
                    SQLRow rowMvtSource = base.getTable("MOUVEMENT").getRow(idMvtSource);
305
 
306
                    if (!rowMvtSource.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
307
                        ListeDesEcheancesClientsPanel.this.relancer.setEnabled(false);
308
                    } else {
309
                        ListeDesEcheancesClientsPanel.this.relancer.setEnabled(true);
310
                    }
311
                    ListeDesEcheancesClientsPanel.this.encaisser.setEnabled(true);
312
                } else {
313
                    ListeDesEcheancesClientsPanel.this.relancer.setEnabled(false);
314
 
315
                    ListeDesEcheancesClientsPanel.this.encaisser.setEnabled(false);
316
                }
317
 
318
            }
319
        });
320
        this.relancer.setEnabled(false);
321
        this.encaisser.setEnabled(false);
322
 
323
    }
324
 
325
    private SQLRow rowSource;
326
 
327
    private void relanceClient() {
328
 
329
        SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
330
        SQLElement relanceElt = Configuration.getInstance().getDirectory().getElement("RELANCE");
331
 
332
        this.rowSource = this.panelEcheances.getListe().getSelectedRow();
333
 
334
        if (this.rowSource != null) {
335
            int idMvtSource = MouvementSQLElement.getSourceId(rowSource.getInt("ID_MOUVEMENT"));
336
            SQLRow rowMvtSource = base.getTable("MOUVEMENT").getRow(idMvtSource);
337
 
338
            if (!rowMvtSource.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
339
                this.relancer.setEnabled(false);
340
                return;
341
            }
342
 
343
            if (this.editRelance == null) {
344
                this.editRelance = new EditFrame(relanceElt);
345
                this.editRelance.setIconImages(Gestion.getFrameIcon());
346
                this.editRelance.addEditPanelListener(new EditPanelListener() {
347
 
348
                    public void cancelled() {
349
                    }
350
 
351
                    public void modified() {
352
                    }
353
 
354
                    public void deleted() {
355
                    }
356
 
357
                    public void inserted(int id) {
358
                        System.err.println("INSERTED " + id + " -- " + rowSource.getID());
359
                        int nbRelance = rowSource.getInt("NOMBRE_RELANCE");
360
                        nbRelance++;
361
 
362
                        SQLRowValues rowValsEch = new SQLRowValues(rowSource.getTable());
363
                        rowValsEch.put("NOMBRE_RELANCE", nbRelance);
364
                        rowValsEch.put("DATE_LAST_RELANCE", new Date());
365
 
366
                        try {
367
                            rowValsEch.update(rowSource.getID());
368
                        } catch (SQLException e1) {
369
                            e1.printStackTrace();
370
                        }
371
                    }
372
                });
373
            }
374
 
375
            SQLRowValues rowVals = new SQLRowValues(relanceElt.getTable());
376
            rowVals.put("ID_SAISIE_VENTE_FACTURE", rowMvtSource.getInt("IDSOURCE"));
377
            rowVals.put("MONTANT", rowSource.getObject("MONTANT"));
378
            rowVals.put("ID_CLIENT", rowSource.getInt("ID_CLIENT"));
379
            rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(RelanceSQLElement.class));
380
            this.editRelance.getSQLComponent().select(rowVals);
381
 
382
            this.editRelance.pack();
383
            this.editRelance.setVisible(true);
384
        } else {
385
            Thread.dumpStack();
386
        }
387
    }
388
 
389
    public IListPanel getListPanelEcheancesClients() {
390
        return this.panelEcheances;
391
    }
392
}