OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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;
73 ilm 18
import org.openconcerto.erp.core.common.ui.IListFilterDatePanel;
19 ilm 19
import org.openconcerto.erp.core.common.ui.IListTotalPanel;
18 ilm 20
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
21
import org.openconcerto.erp.core.finance.payment.component.EncaisserMontantSQLComponent;
177 ilm 22
import org.openconcerto.erp.core.sales.invoice.element.EcheanceClientSQLElement;
18 ilm 23
import org.openconcerto.erp.rights.ComptaUserRight;
24
import org.openconcerto.erp.rights.NXRights;
25
import org.openconcerto.sql.Configuration;
26
import org.openconcerto.sql.element.SQLElement;
27
import org.openconcerto.sql.model.SQLBase;
28
import org.openconcerto.sql.model.SQLRow;
29
import org.openconcerto.sql.model.SQLRowValues;
30
import org.openconcerto.sql.model.SQLTable;
31
import org.openconcerto.sql.users.rights.UserRightsManager;
32
import org.openconcerto.sql.view.EditFrame;
33
import org.openconcerto.sql.view.IListPanel;
34
import org.openconcerto.sql.view.IListener;
73 ilm 35
import org.openconcerto.sql.view.list.IListe;
18 ilm 36
import org.openconcerto.ui.DefaultGridBagConstraints;
37
 
38
import java.awt.GridBagConstraints;
39
import java.awt.GridBagLayout;
40
import java.awt.event.ActionEvent;
41
import java.awt.event.ActionListener;
42
import java.awt.event.MouseAdapter;
43
import java.awt.event.MouseEvent;
44
import java.util.ArrayList;
19 ilm 45
import java.util.Arrays;
18 ilm 46
import java.util.List;
47
 
48
import javax.swing.JButton;
49
import javax.swing.JCheckBox;
50
import javax.swing.JOptionPane;
51
import javax.swing.JPanel;
52
import javax.swing.SwingConstants;
53
 
54
public class ListeDesEcheancesClientsPanel extends JPanel {
55
 
56
    private ListPanelEcheancesClients panelEcheances;
57
    private EditFrame editEncaisse = null;
58
    private EditFrame editRelance = null;
156 ilm 59
    private JButton relancer;
60
    private JButton encaisser;
18 ilm 61
 
73 ilm 62
    public ListeDesEcheancesClientsPanel() {
63
        this(false);
64
    }
65
 
18 ilm 66
    // TODO GEstion Relance (??? loi NRE pour le calcul des penalites)
73 ilm 67
    public ListeDesEcheancesClientsPanel(boolean onlyOld) {
18 ilm 68
        this.setLayout(new GridBagLayout());
69
        final GridBagConstraints c = new DefaultGridBagConstraints();
70
 
73 ilm 71
        this.panelEcheances = new ListPanelEcheancesClients(onlyOld);
18 ilm 72
 
73
        // PANEL AVEC LA LISTE DES ECHEANCES
74
        c.weightx = 1;
75
        c.weighty = 1;
76
        c.gridwidth = GridBagConstraints.REMAINDER;
77
        c.fill = GridBagConstraints.BOTH;
78
        this.add(this.panelEcheances, c);
79
 
73 ilm 80
        final IListe liste = this.panelEcheances.getListe();
81
        IListFilterDatePanel datePanel = new IListFilterDatePanel(liste, liste.getRequest().getPrimaryTable().getField("DATE"), IListFilterDatePanel.getDefaultMap());
19 ilm 82
 
83
        c.weighty = 0;
84
        c.gridy++;
73 ilm 85
        c.weightx = 1;
86
        this.add(datePanel, c);
87
 
88
        IListTotalPanel totalPanel = new IListTotalPanel(liste, Arrays.asList(this.panelEcheances.getElement().getTable().getField("MONTANT")));
89
 
90
        c.weighty = 0;
91
        c.gridy++;
19 ilm 92
        c.anchor = GridBagConstraints.EAST;
93
        c.weightx = 0;
94
        c.fill = GridBagConstraints.NONE;
95
        this.add(totalPanel, c);
96
 
97
        c.anchor = GridBagConstraints.WEST;
18 ilm 98
        c.gridx = 0;
99
        c.gridy++;
100
        c.gridwidth = 1;
101
 
102
        c.weighty = 0;
103
        c.fill = GridBagConstraints.NONE;
104
        c.weightx = 0;
105
        final JCheckBox checkRegCompta = new JCheckBox("Voir les régularisations de comptabilité");
106
        if (UserRightsManager.getCurrentUserRights().haveRight(ComptaUserRight.MENU)) {
107
            this.add(checkRegCompta, c);
108
        }
109
 
110
        checkRegCompta.addActionListener(new ActionListener() {
111
            @Override
112
            public void actionPerformed(ActionEvent e) {
113
                panelEcheances.setShowRegCompta(checkRegCompta.isSelected());
114
 
115
            }
116
        });
117
 
118
        c.weightx = 1;
119
        c.anchor = GridBagConstraints.EAST;
120
        // Bouton Relancer
121
        this.relancer = new JButton("Relancer");
122
        this.relancer.setHorizontalAlignment(SwingConstants.RIGHT);
142 ilm 123
        if (UserRightsManager.getCurrentUserRights().haveRight(NXRights.GESTION_ENCAISSEMENT.getCode())) {
18 ilm 124
            c.gridx++;
125
            this.add(this.relancer, c);
126
            this.relancer.addActionListener(new ActionListener() {
127
                public void actionPerformed(ActionEvent e) {
177 ilm 128
                    ((EcheanceClientSQLElement) panelEcheances.getElement()).relanceClient(getListPanelEcheancesClients().getListe().getSelectedRow().asRow());
18 ilm 129
                }
130
            });
131
        }
132
 
133
        // Bouton Encaisser
134
        this.encaisser = new JButton("Encaisser");
135
        this.encaisser.setHorizontalAlignment(SwingConstants.RIGHT);
136
        c.gridx++;
137
        c.weightx = 0;
142 ilm 138
        if (UserRightsManager.getCurrentUserRights().haveRight(NXRights.GESTION_ENCAISSEMENT.getCode())) {
18 ilm 139
 
140
            this.add(this.encaisser, c);
141
        }
142
        this.encaisser.addActionListener(new ActionListener() {
143
 
144
            public void actionPerformed(ActionEvent e) {
145
 
146
                List<Integer> selectedIds = ListeDesEcheancesClientsPanel.this.panelEcheances.getListe().getSelection().getSelectedIDs();
156 ilm 147
                List<SQLRow> selectedRows = new ArrayList<>(selectedIds.size());
18 ilm 148
                int idCpt = -1;
149
                int idClient = -1;
156 ilm 150
                int idCptTiers = -1;
151
                String tiers = "";
18 ilm 152
                boolean showMessage = false;
153
 
156 ilm 154
                StringBuilder numerosFacturesBuilders = new StringBuilder();
18 ilm 155
                for (Integer integer : selectedIds) {
156
                    final SQLRow row = ListeDesEcheancesClientsPanel.this.panelEcheances.getListe().getSource().getPrimaryTable().getRow(integer);
156 ilm 157
 
18 ilm 158
                    selectedRows.add(row);
156 ilm 159
                    tiers = row.getString("TIERS");
18 ilm 160
                    String nom = row.getForeignRow("ID_MOUVEMENT").getForeignRow("ID_PIECE").getString("NOM");
161
 
156 ilm 162
                    numerosFacturesBuilders.append(nom);
163
                    numerosFacturesBuilders.append(' ');
164
 
18 ilm 165
                    SQLRow rowClient = row.getForeignRow("ID_CLIENT");
156 ilm 166
                    if (rowClient != null && !rowClient.isUndefined()) {
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
                        }
18 ilm 175
 
156 ilm 176
                        if (idClient > -1 && idClient != idCliTmp) {
177
                            showMessage = true;
178
                        } else {
179
                            idClient = idCliTmp;
180
                        }
18 ilm 181
                    }
156 ilm 182
                    SQLRow rowCptTiers = row.getForeignRow("ID_COMPTE_PCE_TIERS");
183
                    if (rowCptTiers != null && !rowCptTiers.isUndefined()) {
184
                        idCptTiers = rowCptTiers.getID();
185
                    }
18 ilm 186
                }
187
                if (showMessage) {
188
                    int answer = JOptionPane.showConfirmDialog(null, "Attention vous avez sélectionné des factures ayant des clients différents. Voulez vous continuer?");
189
                    if (answer != JOptionPane.YES_OPTION) {
190
                        return;
191
                    }
192
 
193
                }
194
                SQLElement encaisseElt = Configuration.getInstance().getDirectory().getElement("ENCAISSER_MONTANT");
195
                if (ListeDesEcheancesClientsPanel.this.editEncaisse == null) {
196
                    ListeDesEcheancesClientsPanel.this.editEncaisse = new EditFrame(encaisseElt);
197
                    ListeDesEcheancesClientsPanel.this.editEncaisse.setIconImages(Gestion.getFrameIcon());
198
                }
199
 
200
                SQLRowValues rowVals = new SQLRowValues(encaisseElt.getTable());
156 ilm 201
                if (idClient > -1) {
202
                    rowVals.put("ID_CLIENT", idClient);
203
                }
204
                rowVals.put("NOM", numerosFacturesBuilders.toString().trim());
205
                rowVals.put("TIERS", tiers);
206
                if (idCptTiers > -1) {
207
                    rowVals.put("ID_COMPTE_PCE_TIERS", idCptTiers);
208
                }
18 ilm 209
 
210
                final EncaisserMontantSQLComponent sqlComponent = (EncaisserMontantSQLComponent) ListeDesEcheancesClientsPanel.this.editEncaisse.getSQLComponent();
211
 
212
                sqlComponent.resetValue();
213
                sqlComponent.select(rowVals);
214
                sqlComponent.loadEcheancesFromRows(selectedRows);
93 ilm 215
                ListeDesEcheancesClientsPanel.this.editEncaisse.setTitle("Encaissement de factures clients");
18 ilm 216
                ListeDesEcheancesClientsPanel.this.editEncaisse.pack();
217
                ListeDesEcheancesClientsPanel.this.editEncaisse.setVisible(true);
218
            }
219
        });
220
 
221
        // Gestion de la souris
222
        this.panelEcheances.getJTable().addMouseListener(new MouseAdapter() {
156 ilm 223
            @Override
18 ilm 224
            public void mousePressed(MouseEvent mE) {
225
                if (mE.getButton() == MouseEvent.BUTTON1) {
156 ilm 226
                    // Mise à jour de l'echeance sur la frame de reglement
227
                    // si cette derniere est cree
228
                    final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
229
                    final SQLRowValues selectedRow = panelEcheances.getListe().getSelectedRow();
230
                    final SQLRow row = selectedRow.asRow();
231
                    if (row == null) {
232
                        JOptionPane.showMessageDialog(ListeDesEcheancesClientsPanel.this, "Selection", "Merci de sélectionner une ligne", JOptionPane.PLAIN_MESSAGE);
233
                        return;
234
                    }
18 ilm 235
                    if (ListeDesEcheancesClientsPanel.this.editEncaisse != null) {
236
                        final SQLRowValues rowVals = new SQLRowValues(base.getTable("ENCAISSER_MONTANT"));
237
                        rowVals.put("ID_ECHEANCE_CLIENT", row.getID());
238
 
239
                        ListeDesEcheancesClientsPanel.this.editEncaisse.getSQLComponent().select(rowVals);
240
                        ListeDesEcheancesClientsPanel.this.editEncaisse.pack();
241
                    }
242
                }
243
 
244
            }
245
        });
246
 
73 ilm 247
        liste.addIListener(new IListener() {
18 ilm 248
            public void selectionId(int id, int field) {
249
                if (id > 1) {
250
                    final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
251
                    final SQLTable tableEch = base.getTable("ECHEANCE_CLIENT");
252
                    final SQLRow rowEch = tableEch.getRow(id);
156 ilm 253
                    final int idMvtSource = MouvementSQLElement.getSourceId(rowEch.getInt("ID_MOUVEMENT"));
254
                    final SQLRow rowMvtSource = base.getTable("MOUVEMENT").getRow(idMvtSource);
18 ilm 255
 
156 ilm 256
                    ListeDesEcheancesClientsPanel.this.relancer.setEnabled(rowMvtSource.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE"));
18 ilm 257
                    ListeDesEcheancesClientsPanel.this.encaisser.setEnabled(true);
258
                } else {
259
                    ListeDesEcheancesClientsPanel.this.relancer.setEnabled(false);
260
                    ListeDesEcheancesClientsPanel.this.encaisser.setEnabled(false);
261
                }
262
 
263
            }
264
        });
265
        this.relancer.setEnabled(false);
266
        this.encaisser.setEnabled(false);
267
 
268
    }
269
 
270
    public IListPanel getListPanelEcheancesClients() {
271
        return this.panelEcheances;
272
    }
273
}