OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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