OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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