OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | 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.core.common.ui.IListFilterDatePanel;
18
import org.openconcerto.erp.core.common.ui.IListTotalPanel;
21 ilm 19
import org.openconcerto.erp.core.common.ui.ListeViewPanel;
18 ilm 20
import org.openconcerto.erp.core.finance.accounting.ui.ListeGestCommEltPanel;
151 ilm 21
import org.openconcerto.erp.core.finance.payment.element.SDDMessageSQLElement;
22
import org.openconcerto.erp.core.finance.payment.element.SDDMessageSQLElement.GenerationResult;
23
import org.openconcerto.erp.core.finance.payment.element.SDDMessageSQLElement.IgnoreReason;
24
import org.openconcerto.erp.core.sales.invoice.element.SaisieVenteFactureSQLElement;
156 ilm 25
import org.openconcerto.erp.core.sales.pos.POSConfiguration;
21 ilm 26
import org.openconcerto.erp.core.sales.pos.ui.TextAreaTicketPanel;
151 ilm 27
import org.openconcerto.erp.utils.TM;
18 ilm 28
import org.openconcerto.sql.Configuration;
29
import org.openconcerto.sql.element.SQLElement;
151 ilm 30
import org.openconcerto.sql.model.FieldPath;
18 ilm 31
import org.openconcerto.sql.model.SQLField;
80 ilm 32
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 33
import org.openconcerto.sql.model.SQLRowValues;
34
import org.openconcerto.sql.model.Where;
151 ilm 35
import org.openconcerto.sql.model.graph.Path;
156 ilm 36
import org.openconcerto.sql.request.UpdateBuilder;
18 ilm 37
import org.openconcerto.sql.view.list.IListe;
151 ilm 38
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
21 ilm 39
import org.openconcerto.sql.view.list.ITableModel;
151 ilm 40
import org.openconcerto.sql.view.list.RowAction;
41
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
18 ilm 42
import org.openconcerto.sql.view.list.SQLTableModelColumn;
43
import org.openconcerto.sql.view.list.SQLTableModelColumnPath;
44
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
45
import org.openconcerto.ui.DefaultGridBagConstraints;
19 ilm 46
import org.openconcerto.ui.JLabelBold;
21 ilm 47
import org.openconcerto.ui.PanelFrame;
83 ilm 48
import org.openconcerto.utils.ExceptionHandler;
18 ilm 49
import org.openconcerto.utils.TableSorter;
50
import org.openconcerto.utils.cc.IClosure;
151 ilm 51
import org.openconcerto.utils.i18n.Grammar_fr;
18 ilm 52
 
53
import java.awt.GridBagConstraints;
54
import java.awt.GridBagLayout;
55
import java.awt.Insets;
56
import java.awt.event.ActionEvent;
57
import java.math.BigInteger;
58
import java.sql.SQLException;
59
import java.util.ArrayList;
151 ilm 60
import java.util.Collections;
18 ilm 61
import java.util.Date;
151 ilm 62
import java.util.HashMap;
18 ilm 63
import java.util.List;
151 ilm 64
import java.util.Map;
18 ilm 65
 
151 ilm 66
import javax.swing.AbstractAction;
18 ilm 67
import javax.swing.JButton;
151 ilm 68
import javax.swing.JOptionPane;
18 ilm 69
import javax.swing.JPanel;
70
import javax.swing.JTabbedPane;
71
import javax.swing.JTable;
151 ilm 72
import javax.swing.SwingWorker;
18 ilm 73
import javax.swing.table.TableColumn;
74
 
151 ilm 75
public class ListeDesVentesPanel extends JPanel {
18 ilm 76
 
77
    private ListeGestCommEltPanel listeFact;
151 ilm 78
    // private JButton buttonEnvoye, buttonRegle, buttonDupliquer;
93 ilm 79
    private JLabelBold textField = new JLabelBold("0");
80
    private JLabelBold textField2 = new JLabelBold("0");
18 ilm 81
 
82
    public ListeDesVentesPanel() {
83
        this.setLayout(new GridBagLayout());
84
        GridBagConstraints c = new GridBagConstraints();
85
        c.insets = new Insets(2, 2, 1, 2);
86
        c.fill = GridBagConstraints.BOTH;
87
        c.anchor = GridBagConstraints.NORTHWEST;
88
        c.gridx = 0;
89
        c.gridy = 0;
90
        c.gridwidth = 1;
91
        c.gridheight = 1;
92
        c.weightx = 1;
93
        c.weighty = 1;
94
 
95
        JTabbedPane tabbedPane = new JTabbedPane();
96
 
80 ilm 97
        final SQLElement elementVF = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE");
18 ilm 98
        // tab Vente facture
80 ilm 99
        final SQLElement eltFacture = elementVF;
18 ilm 100
        final SQLTableModelSourceOnline src = eltFacture.getTableSource(true);
101
        // Filter
80 ilm 102
        Where wPrev = new Where(eltFacture.getTable().getField("PREVISIONNELLE"), "=", Boolean.FALSE);
103
        if (src.getReq().getWhere() != null) {
104
            wPrev = wPrev.and(src.getReq().getWhere());
105
        }
18 ilm 106
        src.getReq().setWhere(wPrev);
107
 
108
        final ListeFactureRenderer rend = new ListeFactureRenderer();
109
        for (SQLTableModelColumn column : src.getColumns()) {
110
            if (column.getClass().isAssignableFrom(SQLTableModelColumnPath.class)) {
111
                ((SQLTableModelColumnPath) column).setEditable(false);
112
            }
83 ilm 113
            // FIXME Voir pour appliquer le renderer correctement
114
            if (!column.getName().startsWith("Avancement")) {
115
                column.setRenderer(rend);
116
            }
18 ilm 117
        }
118
 
119
        final SQLTableModelColumn dateEnvoiCol = src.getColumn(eltFacture.getTable().getField("DATE_ENVOI"));
120
            ((SQLTableModelColumnPath) dateEnvoiCol).setEditable(true);
121
        final SQLTableModelColumn dateReglCol = src.getColumn(eltFacture.getTable().getField("DATE_REGLEMENT"));
61 ilm 122
        if (dateReglCol != null)
123
            ((SQLTableModelColumnPath) dateReglCol).setEditable(true);
18 ilm 124
 
19 ilm 125
            // Edition des dates d'envois
126
            dateEnvoiCol.setColumnInstaller(new IClosure<TableColumn>() {
127
                @Override
128
                public void executeChecked(TableColumn columnDateEnvoi) {
151 ilm 129
                    final org.openconcerto.ui.table.TimestampTableCellEditor cellEditor = new org.openconcerto.ui.table.TimestampTableCellEditor();
130
                    cellEditor.setAllowNull(true);
131
                    columnDateEnvoi.setCellEditor(cellEditor);
19 ilm 132
                    columnDateEnvoi.setCellRenderer(new DateEnvoiRenderer());
133
                }
134
            });
18 ilm 135
 
136
        // Edition des dates de reglement
61 ilm 137
        if (dateReglCol != null) {
138
            dateReglCol.setColumnInstaller(new IClosure<TableColumn>() {
151 ilm 139
 
61 ilm 140
                @Override
141
                public void executeChecked(TableColumn columnDateReglement) {
151 ilm 142
                    final org.openconcerto.ui.table.TimestampTableCellEditor cellEditor = new org.openconcerto.ui.table.TimestampTableCellEditor();
143
                    cellEditor.setAllowNull(true);
144
                    columnDateReglement.setCellEditor(cellEditor);
61 ilm 145
                    columnDateReglement.setCellRenderer(new DateEnvoiRenderer());
146
                }
147
            });
148
        }
18 ilm 149
 
151 ilm 150
        src.getColumns().add(new SQLTableModelColumnPath(new FieldPath(new Path(eltFacture.getTable()).addForeignField(SaisieVenteFactureSQLElement.MESSAGE_FIELD_NAME), "MessageIdentification")));
151
 
18 ilm 152
        this.listeFact = new ListeGestCommEltPanel(eltFacture, new IListe(src), true);
151 ilm 153
        final SDDMessageSQLElement sepaMsgElem = eltFacture.getDirectory().getElement(SDDMessageSQLElement.class);
154
        final String aMessageLabel = sepaMsgElem.getName().getVariant(Grammar_fr.INDEFINITE_ARTICLE_SINGULAR);
155
        this.listeFact.getListe().addIListeAction(new RowAction.PredicateRowAction(new AbstractAction("Générer " + aMessageLabel, null) {
156
            @Override
157
            public void actionPerformed(ActionEvent e) {
158
                final IListe l = IListe.get(e);
159
                final List<Integer> selectedIDs = l.getSelection().getSelectedIDs();
160
                // TODO dialog with label informing of the successful creation of message for n
161
                // invoices/n2 messages too far in the future/n3 messages with collection date
162
                // changed and having a button to open the file chooser
156 ilm 163
                new SwingWorker<GenerationResult, Void>() {
151 ilm 164
                    @Override
165
                    protected GenerationResult doInBackground() throws Exception {
166
                        return sepaMsgElem.generateXML(selectedIDs);
167
                    }
168
 
169
                    @Override
170
                    protected void done() {
171
                        final GenerationResult genRes;
172
                        try {
173
                            genRes = this.get();
174
                        } catch (Exception e) {
175
                            ExceptionHandler.handle(l, "Impossible de générer " + aMessageLabel, e);
176
                            return;
177
                        }
178
                        final int includedInvoicesCount = genRes.getIncludedInvoicesCount();
179
                        final Map<String, Object> tmMap = new HashMap<>();
180
                        tmMap.put("msgElem", sepaMsgElem.getName());
181
                        tmMap.put("invoiceElem", elementVF.getName());
182
                        tmMap.put("invoiceElemCount", includedInvoicesCount);
183
                        if (genRes.getDDInvoicesWithoutMessage().isEmpty()) {
184
                            JOptionPane.showMessageDialog(l, TM.getTM().trM("sddMessage.generation.noneNeeded", tmMap));
185
                        } else if (genRes.getIgnoredInvoices().isEmpty()) {
186
                            JOptionPane.showMessageDialog(l, TM.getTM().trM("sddMessage.generation.noneIgnored", tmMap));
187
                        } else {
188
                            final int futureCount = genRes.getIgnoredInvoices().getNonNull(IgnoreReason.TOO_FAR_IN_FUTURE).size();
189
                            final int duplicateCount = genRes.getIgnoredInvoices().getNonNull(IgnoreReason.DUPLICATE_MANDATE).size();
190
                            final int missingInfoCount = genRes.getIgnoredInvoices().getNonNull(IgnoreReason.MISSING_INFO).size();
191
                            tmMap.put("futureCount", futureCount);
192
                            tmMap.put("duplicateCount", duplicateCount);
193
                            tmMap.put("missingInfoCount", missingInfoCount);
194
                            final StringBuilder msg = new StringBuilder(256);
195
                            msg.append(TM.getTM().trM("sddMessage.generation.someIgnored", tmMap));
196
                            if (futureCount > 0) {
156 ilm 197
                                msg.append("\n- ");
151 ilm 198
                                msg.append(TM.getTM().trM("sddMessage.generation.someIgnored.future", tmMap));
199
                            }
200
                            if (duplicateCount > 0) {
156 ilm 201
                                msg.append("\n- ");
151 ilm 202
                                msg.append(TM.getTM().trM("sddMessage.generation.someIgnored.duplicateMandate", tmMap));
203
                            }
204
                            if (missingInfoCount > 0) {
156 ilm 205
                                msg.append("\n- ");
151 ilm 206
                                msg.append(TM.getTM().trM("sddMessage.generation.someIgnored.missingInfo", tmMap));
207
                            }
208
                            final int messageType = duplicateCount == 0 ? JOptionPane.WARNING_MESSAGE : JOptionPane.ERROR_MESSAGE;
209
                            JOptionPane.showMessageDialog(l, msg.toString(), null, messageType);
210
                        }
211
                        if (genRes.getInsertedMessage() != null) {
212
                            sepaMsgElem.exportXML(l, Collections.singletonList(genRes.getInsertedMessage()));
213
                        }
214
                    }
215
                }.execute();
216
            }
217
        }, false, true).setPredicate(IListeEvent.getNonEmptySelectionPredicate()));
156 ilm 218
        // TODO remove once we have a join with {SENT, OK, DEFINITIVE_ERROR, TRANSIENT_ERROR}
219
        this.listeFact.getListe().addIListeAction(new RowAction.PredicateRowAction(new AbstractAction("Prélever à nouveau", null) {
220
            @Override
221
            public void actionPerformed(ActionEvent e) {
222
                final IListe l = IListe.get(e);
223
                if (JOptionPane.showConfirmDialog(l, "Voulez-vous vraiment prélever à nouveau les factures sélectionnées ? Cette action est définitive.", "Prélever à nouveau",
224
                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION)
225
                    return;
226
                final List<Integer> selectedIDs = l.getSelection().getSelectedIDs();
227
                final UpdateBuilder upd = new UpdateBuilder(l.getSource().getPrimaryTable());
228
                upd.setObject(SaisieVenteFactureSQLElement.MESSAGE_FIELD_NAME, upd.getTable().getForeignTable(SaisieVenteFactureSQLElement.MESSAGE_FIELD_NAME).getUndefinedIDNumber());
229
                upd.setObject(SaisieVenteFactureSQLElement.END2END_FIELD_NAME, "");
230
                // don't allow to debit already payed invoices
231
                upd.setWhere(new Where(upd.getTable().getKey(), selectedIDs).and(Where.isNull(upd.getTable().getField("DATE_REGLEMENT"))));
232
 
233
                upd.getTable().getDBSystemRoot().getDataSource().execute(upd.asString());
234
                for (final Integer id : selectedIDs)
235
                    upd.getTable().fireTableModified(id, upd.getFieldsNames());
236
            }
237
        }, false, true).setPredicate(IListeEvent.getNonEmptySelectionPredicate()));
18 ilm 238
        this.listeFact.setOpaque(false);
151 ilm 239
        this.listeFact.getListe().setModificationAllowed(true);
18 ilm 240
        final JTable tableFact = this.listeFact.getListe().getJTable();
241
        final SQLTableModelColumn numeroCol = src.getColumn(eltFacture.getTable().getField("NUMERO"));
242
        ((TableSorter) tableFact.getModel()).setSortingStatus(src.getColumns().indexOf(numeroCol), TableSorter.ASCENDING);
243
 
244
        JPanel panelFacture = new JPanel(new GridBagLayout());
245
        GridBagConstraints cFacture = new DefaultGridBagConstraints();
246
 
247
        cFacture.gridy++;
248
        cFacture.gridx = 0;
249
        cFacture.weighty = 1;
250
        cFacture.weightx = 1;
251
        cFacture.gridwidth = GridBagConstraints.REMAINDER;
252
        cFacture.fill = GridBagConstraints.BOTH;
253
        panelFacture.add(this.listeFact, cFacture);
254
 
255
        List<SQLField> l = new ArrayList<SQLField>();
142 ilm 256
        if (this.listeFact.getListe().getSource().getColumn(eltFacture.getTable().getField("T_HA")) != null) {
257
            l.add(eltFacture.getTable().getField("T_HA"));
258
        }
18 ilm 259
        l.add(eltFacture.getTable().getField("T_HT"));
260
        l.add(eltFacture.getTable().getField("T_TTC"));
61 ilm 261
        final IListTotalPanel total = new IListTotalPanel(this.listeFact.getListe(), l);
18 ilm 262
        cFacture.weighty = 0;
263
        cFacture.fill = GridBagConstraints.NONE;
264
        cFacture.gridy++;
265
        cFacture.anchor = GridBagConstraints.EAST;
266
        total.setOpaque(false);
267
        panelFacture.add(total, cFacture);
268
 
19 ilm 269
 
18 ilm 270
        IListFilterDatePanel filterDate = new IListFilterDatePanel(this.listeFact.getListe(), eltFacture.getTable().getField("DATE"), IListFilterDatePanel.getDefaultMap());
271
        cFacture.weighty = 0;
272
        cFacture.fill = GridBagConstraints.HORIZONTAL;
273
        cFacture.gridy++;
274
        filterDate.setOpaque(false);
275
        panelFacture.add(filterDate, cFacture);
276
        tabbedPane.add("Ventes avec facture", panelFacture);
277
 
151 ilm 278
        PredicateRowAction actionRegle = new PredicateRowAction(new AbstractAction("Facture réglée") {
18 ilm 279
 
151 ilm 280
            @Override
281
            public void actionPerformed(ActionEvent e) {
282
                final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
283
                for (SQLRowAccessor sqlRowAccessor : selectedRows) {
284
                    final SQLRowValues rowVals = sqlRowAccessor.asRow().createEmptyUpdateRow();
285
                    rowVals.put("DATE_REGLEMENT", new Date());
286
                    try {
287
                        rowVals.update();
288
                    } catch (SQLException e1) {
289
                        ExceptionHandler.handle("Modification impossible", e1);
290
                    }
291
                }
292
            }
293
        }, true);
294
        actionRegle.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
295
        this.listeFact.getListe().addIListeAction(actionRegle);
80 ilm 296
 
151 ilm 297
        PredicateRowAction actionEnvoye = new PredicateRowAction(new AbstractAction("Facture envoyée") {
298
 
299
            @Override
300
            public void actionPerformed(ActionEvent e) {
301
                final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
302
                for (SQLRowAccessor sqlRowAccessor : selectedRows) {
303
                    final SQLRowValues rowVals = sqlRowAccessor.asRow().createEmptyUpdateRow();
304
                    rowVals.put("DATE_ENVOI", new Date());
305
                    try {
306
                        rowVals.update();
307
                    } catch (SQLException e1) {
308
                        ExceptionHandler.handle("Modification impossible", e1);
309
                    }
80 ilm 310
                }
18 ilm 311
            }
151 ilm 312
        }, true);
313
        actionEnvoye.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
314
        this.listeFact.getListe().addIListeAction(actionEnvoye);
18 ilm 315
 
21 ilm 316
 
317
            {
318
                // Tab Vente caisse
319
                ListeViewPanel panelTicket = new ListeViewPanel(Configuration.getInstance().getDirectory().getElement("TICKET_CAISSE")) {
320
                    @Override
321
                    protected void handleAction(JButton source, ActionEvent evt) {
322
                        if (source == this.buttonModifier) {
156 ilm 323
                            POSConfiguration posConf = POSConfiguration.getInstance();
324
                            try {
325
                                if (posConf == null)
326
                                    posConf = POSConfiguration.setInstance();
327
                            } catch (Exception e) {
328
                                ExceptionHandler.handle(source, "Impossible d'initialiser la configuration de la caisse", e);
329
                                return;
330
                            }
331
                            new PanelFrame(new TextAreaTicketPanel(posConf, this.getListe().fetchSelectedRow()), "Ticket").setVisible(true);
21 ilm 332
                        } else {
333
                            super.handleAction(source, evt);
334
                        }
335
                    }
336
                };
337
 
338
                JPanel panel = new JPanel(new GridBagLayout());
339
                GridBagConstraints cc = new DefaultGridBagConstraints();
340
                cc.weightx = 1;
341
                cc.weighty = 1;
342
                cc.fill = GridBagConstraints.BOTH;
343
                panel.add(panelTicket, cc);
344
 
345
                final List<SQLField> l2 = new ArrayList<SQLField>();
346
                l2.add(panelTicket.getElement().getTable().getField("TOTAL_HT"));
347
                l2.add(panelTicket.getElement().getTable().getField("TOTAL_TTC"));
61 ilm 348
                final IListTotalPanel total2 = new IListTotalPanel(panelTicket.getListe(), l2);
21 ilm 349
                cc.weighty = 0;
350
                cc.fill = GridBagConstraints.NONE;
351
                cc.gridy++;
352
                cc.anchor = GridBagConstraints.EAST;
353
                total2.setOpaque(false);
354
                panel.add(total2, cc);
355
 
356
                IListFilterDatePanel filterDate2 = new IListFilterDatePanel(panelTicket.getListe(), panelTicket.getElement().getTable().getField("DATE"), IListFilterDatePanel.getDefaultMap());
357
                cc.weighty = 0;
358
                cc.fill = GridBagConstraints.HORIZONTAL;
359
                cc.gridy++;
360
                filterDate2.setOpaque(false);
361
                panel.add(filterDate2, cc);
362
 
363
                tabbedPane.add("Ventes caisse", panel);
364
 
365
            }
19 ilm 366
            // Tab Vente comptoir
21 ilm 367
            {
368
                final ListeGestCommEltPanel listeVC = new ListeGestCommEltPanel(Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_COMPTOIR"), true);
151 ilm 369
                listeVC.getListe().setModificationAllowed(false);
21 ilm 370
                listeVC.setOpaque(false);
18 ilm 371
 
21 ilm 372
                final JTable table = listeVC.getListe().getJTable();
373
                for (int i = 0; i < table.getColumnCount(); i++) {
374
                    if (table.getColumnClass(i) == Long.class || table.getColumnClass(i) == BigInteger.class) {
375
                        table.getColumnModel().getColumn(i).setCellRenderer(rend);
376
                    }
19 ilm 377
                }
18 ilm 378
 
21 ilm 379
                JPanel panelComptoir = new JPanel(new GridBagLayout());
380
                GridBagConstraints cc = new DefaultGridBagConstraints();
381
                cc.weightx = 1;
382
                cc.weighty = 1;
383
                cc.fill = GridBagConstraints.BOTH;
384
                panelComptoir.add(listeVC, cc);
18 ilm 385
 
21 ilm 386
                final List<SQLField> l2 = new ArrayList<SQLField>();
387
                l2.add(listeVC.getElement().getTable().getField("MONTANT_HT"));
388
                l2.add(listeVC.getElement().getTable().getField("MONTANT_TTC"));
61 ilm 389
                final IListTotalPanel total2 = new IListTotalPanel(listeVC.getListe(), l2);
21 ilm 390
                cc.weighty = 0;
391
                cc.fill = GridBagConstraints.NONE;
392
                cc.gridy++;
393
                cc.anchor = GridBagConstraints.EAST;
394
                total2.setOpaque(false);
395
                panelComptoir.add(total2, cc);
18 ilm 396
 
21 ilm 397
                IListFilterDatePanel filterDate2 = new IListFilterDatePanel(listeVC.getListe(), listeVC.getElement().getTable().getField("DATE"), IListFilterDatePanel.getDefaultMap());
398
                cc.weighty = 0;
399
                cc.fill = GridBagConstraints.HORIZONTAL;
400
                cc.gridy++;
401
                filterDate2.setOpaque(false);
402
                panelComptoir.add(filterDate2, cc);
18 ilm 403
 
21 ilm 404
                tabbedPane.add("Ventes comptoir", panelComptoir);
405
            }
18 ilm 406
        this.add(tabbedPane, c);
407
    }
408
 
409
}