OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 76 | Rev 129 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
76 ilm 1
/*
2
 * Créé le 1 juin 2012
3
 */
4
package org.openconcerto.modules.subscription.panel;
5
 
6
import java.awt.GridBagConstraints;
7
import java.awt.GridBagLayout;
8
import java.awt.event.ActionEvent;
9
import java.sql.SQLException;
10
import java.util.Calendar;
11
import java.util.Collections;
12
import java.util.Date;
13
import java.util.List;
14
import java.util.Map;
15
 
16
import javax.swing.AbstractAction;
17
import javax.swing.JPanel;
18
 
19
import org.openconcerto.erp.core.sales.quote.report.DevisXmlSheet;
20
import org.openconcerto.erp.panel.ListeFastPrintFrame;
21
import org.openconcerto.modules.subscription.SubscriptionChecker;
22
import org.openconcerto.sql.Configuration;
23
import org.openconcerto.sql.element.SQLElement;
24
import org.openconcerto.sql.model.SQLRow;
25
import org.openconcerto.sql.model.SQLRowAccessor;
26
import org.openconcerto.sql.model.SQLRowValues;
27
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.sql.model.Where;
29
import org.openconcerto.sql.view.EditFrame;
30
import org.openconcerto.sql.view.EditPanel.EditMode;
31
import org.openconcerto.sql.view.ListeAddPanel;
32
import org.openconcerto.sql.view.list.IListe;
33
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
34
import org.openconcerto.sql.view.list.RowAction;
35
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
36
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
37
import org.openconcerto.ui.DefaultGridBagConstraints;
38
import org.openconcerto.utils.ExceptionHandler;
39
import org.openconcerto.utils.SwingWorker2;
40
 
41
/**
42
 * Base pour les panels de gestion des renouvellements d'abonnements
43
 *
44
 * @author Utilisateur
45
 */
46
public class AboPanel extends JPanel {
47
 
48
    // Bouton de vérification des abonnements
49
    protected final PredicateRowAction actionCheck;
50
 
51
    // Action de validation des éléments générés dans les IListes
52
    protected final PredicateRowAction actionValid;
53
 
54
    // Action pour voir l'abonnement associé
55
    protected final PredicateRowAction actionGetAbo;
56
 
57
    public AboPanel(final SQLElement elt, final SQLElement itemsElement, final String type) {
58
        super(new GridBagLayout());
59
 
60
        this.actionCheck = new PredicateRowAction(new AbstractAction() {
61
            @Override
62
            public void actionPerformed(ActionEvent e) {
63
                // actionCheck.setEnabled(false);
64
 
65
                Configuration.getInstance().getNonInteractiveSQLExecutor().execute(new Runnable() {
66
 
67
                    public void run() {
68
                        try {
69
                            parseAbonnement(elt, itemsElement, type);
70
                            // createDevis();
71
                        } catch (Exception exn) {
72
                            ExceptionHandler.handle("Une erreur est survenue pendant la vérification des abonnements", exn);
73
                        }
74
                    }
75
                });
76
            }
77
        }, true, false, "subscription.check");
78
        this.actionCheck.setPredicate(IListeEvent.createSelectionCountPredicate(0, Integer.MAX_VALUE));
79
        this.actionValid = new PredicateRowAction(new AbstractAction() {
80
 
81
            @Override
82
            public void actionPerformed(ActionEvent arg0) {
83
 
96 ilm 84
                List<SQLRowValues> list = IListe.get(arg0).getSelectedRows();
76 ilm 85
 
96 ilm 86
                for (SQLRowValues sqlRowAccessor : list) {
76 ilm 87
                    validItem(sqlRowAccessor);
88
                }
89
 
90
                ListeFastPrintFrame frame = new ListeFastPrintFrame(list, DevisXmlSheet.class);
91
                frame.setVisible(true);
92
            }
93
        }, true, "subscription.validate");
94
        this.actionValid.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
95
 
96
        this.actionGetAbo = new PredicateRowAction(new AbstractAction() {
97
            EditFrame frame;
98
 
99
            @Override
100
            public void actionPerformed(ActionEvent arg0) {
101
                final SQLRowAccessor row = IListe.get(arg0).getSelectedRow();
102
                if (this.frame == null) {
103
                    this.frame = new EditFrame(Configuration.getInstance().getDirectory().getElement("ABONNEMENT"), EditMode.MODIFICATION);
104
                }
105
                this.frame.selectionId(row.getInt("ID_ABONNEMENT"));
106
                this.frame.setVisible(true);
107
            }
108
        }, false, "subscription.modify");
109
        this.actionGetAbo.setPredicate(IListeEvent.getSingleSelectionPredicate());
110
 
111
        createUI(elt, itemsElement, type);
112
    }
113
 
114
    protected List<RowAction> getAdditionnalRowActions() {
115
        return Collections.emptyList();
116
    }
117
 
118
    /**
119
     * Interface
120
     *
121
     * @param elt
122
     * @param itemsElement
123
     * @param type
124
     */
125
    private void createUI(final SQLElement elt, final SQLElement itemsElement, final String type) {
126
        final SwingWorker2<SQLTableModelSourceOnline, Object> worker = new SwingWorker2<SQLTableModelSourceOnline, Object>() {
127
 
128
            @Override
129
            protected SQLTableModelSourceOnline doInBackground() throws Exception {
130
                final SQLTableModelSourceOnline tableCmd = elt.getTableSource(true);
131
                Where wD = new Where(elt.getTable().getField("CREATION_AUTO_VALIDER"), "=", Boolean.FALSE);
132
                wD = wD.and(new Where(elt.getTable().getField("ID_ABONNEMENT"), "IS NOT", (Object) null));
133
                tableCmd.getReq().setWhere(wD);
134
                return tableCmd;
135
            }
136
 
137
            @Override
138
            protected void done() {
139
                try {
140
                    final IListe listCmd = new IListe(get());
141
                    listCmd.addIListeAction(actionValid);
142
                    listCmd.addIListeAction(actionGetAbo);
143
                    listCmd.addIListeAction(actionCheck);
144
                    listCmd.addIListeActions(getAdditionnalRowActions());
145
                    final ListeAddPanel listeCmd = new ListeAddPanel(elt, listCmd, "non validés");
146
                    final GridBagConstraints c = new DefaultGridBagConstraints();
147
                    c.gridy = GridBagConstraints.RELATIVE;
148
                    c.weightx = 1;
149
                    c.weighty = 1;
150
                    c.fill = GridBagConstraints.BOTH;
151
                    add(listeCmd, c);
152
                } catch (Exception e) {
153
                    ExceptionHandler.handle("Unable to create subscription list", e);
154
                }
155
            }
156
 
157
        };
158
        worker.execute();
159
 
160
    }
161
 
162
    /**
163
     * Validation d'un élément
164
     *
165
     * @param sqlRowAccessor
166
     */
167
    protected void validItem(SQLRowAccessor sqlRowAccessor) {
168
        SQLRowValues rowVals = sqlRowAccessor.asRowValues();
169
        rowVals.put("CREATION_AUTO_VALIDER", Boolean.TRUE);
170
        try {
171
            rowVals.update();
172
        } catch (SQLException exn) {
173
            exn.printStackTrace();
174
        }
175
    }
176
 
177
    /**
178
     * Duplication des rowValuesItem
179
     *
180
     * @param row
181
     * @param itemsTable
182
     * @param rowValsDest
183
     */
184
    private void copyItems(SQLRow row, SQLTable itemsTable, SQLRowValues rowValsDest) {
185
        // On duplique les elements de devis
186
        final List<SQLRow> myListItem = row.getReferentRows(itemsTable);
187
 
188
        for (final SQLRow rowElt : myListItem) {
189
 
190
            final SQLRowValues rowValsItem = rowElt.createUpdateRow();
191
            rowValsItem.clearPrimaryKeys();
192
            rowValsItem.put("ID_" + row.getTable().getName(), rowValsDest);
193
        }
194
    }
195
 
196
    /**
197
     * Création d'un nouvel élément à partir de celui de base
198
     *
199
     * @param row
200
     * @param rowVals
201
     * @param dateNew
202
     * @param rowAbonnement
203
     */
204
    protected void injectRow(SQLRow row, SQLRowValues rowVals, Date dateNew, SQLRow rowAbonnement) {
205
        rowVals.put("ID_CLIENT", row.getObject("ID_CLIENT"));
206
 
207
        rowVals.put("ID_COMMERCIAL", row.getObject("ID_COMMERCIAL"));
208
 
209
        rowVals.put("DATE", dateNew);
210
 
211
        rowVals.put("T_HT", row.getObject("T_HT"));
212
        rowVals.put("T_TVA", row.getObject("T_TVA"));
213
        rowVals.put("T_SERVICE", row.getObject("T_SERVICE"));
214
        rowVals.put("T_TTC", row.getObject("T_TTC"));
215
 
216
        rowVals.put("INFOS", row.getObject("INFOS"));
217
 
218
        rowVals.put("T_POIDS", row.getObject("T_POIDS"));
219
        rowVals.put("ID_TARIF", row.getObject("ID_TARIF"));
220
        rowVals.put("ID_MODELE", row.getObject("ID_MODELE"));
221
        rowVals.put("ID_ABONNEMENT", rowAbonnement.getID());
222
        rowVals.put("CREATION_AUTO_VALIDER", Boolean.FALSE);
223
 
224
        if (row.getTable().contains("SOURCE")) {
225
            rowVals.put("SOURCE", row.getObject("SOURCE"));
226
            rowVals.put("IDSOURCE", row.getObject("IDSOURCE"));
227
        }
228
 
229
        if (row.getTable().contains("ID_AFFAIRE")) {
230
            rowVals.put("ID_AFFAIRE", row.getObject("ID_AFFAIRE"));
231
        }
232
    }
233
 
234
    /**
235
     * Vérification du renouvellement des abonnements
236
     *
237
     * @param elt
238
     * @param itemsElement
239
     * @param type
240
     */
241
    public void parseAbonnement(SQLElement elt, SQLElement itemsElement, String type) {
242
 
243
        // Date de renouvellement des abonnements
244
        SubscriptionChecker checker = new SubscriptionChecker(elt.getTable());
245
        Map<SQLRow, Calendar> listLastCreateElt = checker.check();
246
 
247
        for (SQLRow rowAbonnement : listLastCreateElt.keySet()) {
248
 
96 ilm 249
            // FIXME On recupere des abonnements archives (à tester sur BD 2Si)
250
            if (!rowAbonnement.isArchived()) {
251
                // On duplique le devis
252
                SQLRow rowsCmd = rowAbonnement.getForeignRow("ID_" + elt.getTable().getName());
253
                Calendar date = listLastCreateElt.get(rowAbonnement);
254
                if (date == null) {
255
                    date = rowsCmd.getDate("DATE");
256
                    date.add(Calendar.MONTH, rowAbonnement.getInt("NB_MOIS_" + type));
257
                }
76 ilm 258
 
96 ilm 259
                // Si l'abonnement n'est pas expiré
260
                if (rowAbonnement.getObject("DATE_FIN_" + type) == null || date.compareTo(rowAbonnement.getDate("DATE_FIN_" + type)) <= 0) {
76 ilm 261
 
96 ilm 262
                    final SQLRowValues rowVals = new SQLRowValues(elt.getTable());
76 ilm 263
 
96 ilm 264
                    injectRow(rowsCmd, rowVals, date.getTime(), rowAbonnement);
265
                    // On duplique items
266
                    copyItems(rowsCmd, itemsElement.getTable(), rowVals);
76 ilm 267
 
96 ilm 268
                    try {
76 ilm 269
 
96 ilm 270
                        rowVals.commit();
271
                        // FIXME Voir avec Guillaume create or not create document
272
                    } catch (SQLException exn) {
76 ilm 273
 
96 ilm 274
                        ExceptionHandler.handle("Erreur lors de la création  " + elt.getSingularName() + " d'abonnement.", exn);
275
                    }
76 ilm 276
                }
277
            }
278
        }
279
    }
280
}