OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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