OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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