OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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