OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
76 ilm 1
package org.openconcerto.modules.subscription;
2
 
3
import java.awt.event.ActionEvent;
4
import java.io.IOException;
152 ilm 5
import java.sql.SQLException;
96 ilm 6
import java.util.Calendar;
76 ilm 7
 
8
import javax.swing.AbstractAction;
9
import javax.swing.JFrame;
96 ilm 10
import javax.swing.JOptionPane;
76 ilm 11
 
12
import org.openconcerto.erp.action.CreateFrameAbstractAction;
13
import org.openconcerto.erp.config.MainFrame;
14
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
96 ilm 15
import org.openconcerto.erp.core.sales.invoice.element.SaisieVenteFactureSQLElement;
16
import org.openconcerto.erp.core.sales.invoice.element.SaisieVenteFactureSQLElement.DoWithRow;
76 ilm 17
import org.openconcerto.erp.modules.AbstractModule;
18
import org.openconcerto.erp.modules.AlterTableRestricted;
19
import org.openconcerto.erp.modules.ComponentsContext;
20
import org.openconcerto.erp.modules.DBContext;
21
import org.openconcerto.erp.modules.MenuContext;
22
import org.openconcerto.erp.modules.ModuleFactory;
96 ilm 23
import org.openconcerto.modules.subscription.element.SubscriptionSQLComponent;
76 ilm 24
import org.openconcerto.modules.subscription.element.SubscriptionSQLElement;
25
import org.openconcerto.modules.subscription.panel.BonCommandeAboPanel;
26
import org.openconcerto.modules.subscription.panel.DevisAboPanel;
27
import org.openconcerto.modules.subscription.panel.FacturesAboPanel;
28
import org.openconcerto.modules.subscription.panel.HistoriqueAbonnement;
96 ilm 29
import org.openconcerto.sql.Configuration;
30
import org.openconcerto.sql.element.SQLElement;
76 ilm 31
import org.openconcerto.sql.element.SQLElementDirectory;
96 ilm 32
import org.openconcerto.sql.model.SQLRow;
33
import org.openconcerto.sql.model.SQLRowAccessor;
34
import org.openconcerto.sql.model.SQLRowValues;
76 ilm 35
import org.openconcerto.sql.utils.SQLCreateTable;
36
import org.openconcerto.sql.view.EditFrame;
96 ilm 37
import org.openconcerto.sql.view.EditPanel.EditMode;
76 ilm 38
import org.openconcerto.sql.view.IListFrame;
39
import org.openconcerto.sql.view.ListeAddPanel;
40
import org.openconcerto.sql.view.list.IListe;
96 ilm 41
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
76 ilm 42
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
43
import org.openconcerto.utils.i18n.TranslationManager;
44
 
45
public final class Module extends AbstractModule {
46
 
47
    public Module(ModuleFactory f) throws IOException {
48
        super(f);
49
 
50
    }
51
 
52
    @Override
152 ilm 53
    protected void install(DBContext ctxt) throws SQLException, IOException {
76 ilm 54
        super.install(ctxt);
55
 
56
        if (ctxt.getLastInstalledVersion() == null) {
57
 
58
            if (ctxt.getRoot().getTable("ABONNEMENT") == null) {
59
 
60
                // ABONNEMENT
61
                final SQLCreateTable createTable = ctxt.getCreateTable("ABONNEMENT");
62
                createTable.addVarCharColumn("NUMERO", 256);
63
                createTable.addVarCharColumn("DESCRIPTION", 1024);
64
                createTable.addVarCharColumn("NOM", 256);
65
                createTable.addVarCharColumn("INTITULE_FACTURE", 1024);
66
                createTable.addVarCharColumn("INFOS", 2048);
67
                createTable.addColumn("DATE", "date");
68
                createTable.addForeignColumn("ID_CLIENT", ctxt.getRoot().getTable("CLIENT"));
69
                createTable.addForeignColumn("ID_SAISIE_VENTE_FACTURE", ctxt.getRoot().getTable("SAISIE_VENTE_FACTURE"));
70
                createTable.addForeignColumn("ID_DEVIS", ctxt.getRoot().getTable("DEVIS"));
71
                createTable.addColumn("DATE_DEBUT_FACTURE", "date");
72
                createTable.addColumn("DATE_FIN_FACTURE", "date");
73
                createTable.addColumn("DATE_DEBUT_DEVIS", "date");
74
                createTable.addColumn("DATE_FIN_DEVIS", "date");
75
                createTable.addColumn("NB_MOIS_DEVIS", "int DEFAULT 3");
76
                createTable.addColumn("NB_MOIS_FACTURE", "int DEFAULT 3");
77
                createTable.addColumn("CREATE_FACTURE", "boolean DEFAULT false");
78
                createTable.addColumn("CREATE_DEVIS", "boolean DEFAULT false");
79
 
80
                createTable.addForeignColumn("ID_COMMANDE_CLIENT", ctxt.getRoot().getTable("COMMANDE_CLIENT"));
81
                createTable.addColumn("DATE_DEBUT_COMMANDE", "date");
82
                createTable.addColumn("DATE_FIN_COMMANDE", "date");
83
                createTable.addColumn("NB_MOIS_COMMANDE", "int DEFAULT 3");
84
                createTable.addColumn("CREATE_COMMANDE", "boolean DEFAULT false");
85
 
86
                AlterTableRestricted alterNumero = ctxt.getAlterTable("NUMEROTATION_AUTO");
87
                alterNumero.addVarCharColumn("ABONNEMENT_FORMAT", 128);
88
                alterNumero.addIntegerColumn("ABONNEMENT_START", 1);
89
 
90
                AlterTableRestricted alterFact = ctxt.getAlterTable("SAISIE_VENTE_FACTURE");
91
                alterFact.addForeignColumn("ID_ABONNEMENT", createTable);
92
                alterFact.addColumn("CREATION_AUTO_VALIDER", "boolean DEFAULT false");
93
 
94
                AlterTableRestricted alterDevis = ctxt.getAlterTable("COMMANDE_CLIENT");
95
                alterDevis.addForeignColumn("ID_ABONNEMENT", createTable);
96
                alterDevis.addColumn("CREATION_AUTO_VALIDER", "boolean DEFAULT false");
97
 
98
                AlterTableRestricted alterCmd = ctxt.getAlterTable("DEVIS");
99
                alterCmd.addForeignColumn("ID_ABONNEMENT", createTable);
100
                alterCmd.addColumn("CREATION_AUTO_VALIDER", "boolean DEFAULT false");
101
 
152 ilm 102
                ctxt.executeSQL();
103
                SQLRowValues rowVals = new SQLRowValues(ctxt.getRoot().getTable("ABONNEMENT"));
104
                rowVals.insert();
76 ilm 105
            }
106
 
107
        }
108
    }
109
 
110
    @Override
96 ilm 111
    protected void setupElements(final SQLElementDirectory dir) {
76 ilm 112
        super.setupElements(dir);
113
        TranslationManager.getInstance().addTranslationStreamFromClass(this.getClass());
114
        dir.addSQLElement(SubscriptionSQLElement.class);
115
        NumerotationAutoSQLElement.addClass(SubscriptionSQLElement.class, "ABONNEMENT");
96 ilm 116
        ((SaisieVenteFactureSQLElement) dir.getElement("SAISIE_VENTE_FACTURE")).putSpecialAction("subscription.autocreate", new DoWithRow() {
117
            @Override
118
            public void process(SQLRow row) {
119
                createAbonnement(row);
120
            }
121
        });
122
        PredicateRowAction action = new PredicateRowAction(new AbstractAction("Créer un abonnement") {
123
 
124
            @Override
125
            public void actionPerformed(ActionEvent e) {
126
                final SQLRowAccessor r = IListe.get(e).getSelectedRow();
127
                createAbonnement(r);
128
 
129
            }
130
        }, true);
131
        action.setPredicate(IListeEvent.getSingleSelectionPredicate());
132
        dir.getElement("SAISIE_VENTE_FACTURE").getRowActions().add(action);
133
        NumerotationAutoSQLElement.addClass(SubscriptionSQLElement.class, "ABONNEMENT");
134
 
76 ilm 135
    }
136
 
96 ilm 137
    private void createAbonnement(SQLRowAccessor r) {
138
 
139
        SQLElement aboElt = Configuration.getInstance().getDirectory().getElement("ABONNEMENT");
140
        if (r.getReferentRows(aboElt.getTable()).size() == 0) {
141
            final SubscriptionSQLComponent createComponent = (SubscriptionSQLComponent) aboElt.createComponent(SQLElement.DEFAULT_COMP_ID);
142
            EditFrame frame = new EditFrame(createComponent, EditMode.CREATION);
143
            SQLRowValues rowVals = new SQLRowValues(aboElt.getTable());
144
            SQLRowAccessor rowClient = r.getForeign("ID_CLIENT");
145
            rowVals.put("ID_CLIENT", rowClient.getID());
146
            rowVals.put("ID_SAISIE_VENTE_FACTURE", r.getID());
147
            rowVals.put("CREATE_FACTURE", Boolean.TRUE);
148
            rowVals.put("NOM", "Abonnement");
149
            rowVals.put("INTITULE_FACTURE", r.getString("NOM"));
150
            Calendar c = Calendar.getInstance();
151
            rowVals.put("DATE_DEBUT_FACTURE", c.getTime());
152
            c.add(Calendar.YEAR, 2);
153
            rowVals.put("NB_MOIS_FACTURE", 12);
154
 
155
            createComponent.setLightUI(false);
156
 
157
            createComponent.select(rowVals);
158
            frame.setVisible(true);
159
        } else {
160
            JOptionPane.showMessageDialog(null, "Cette facture est déjà associée à un abonnement!");
161
        }
162
    }
163
 
76 ilm 164
    @Override
165
    protected void setupComponents(final ComponentsContext ctxt) {
166
 
167
    }
168
 
169
    @Override
170
    protected void setupMenu(final MenuContext ctxt) {
171
        ctxt.addMenuItem(new CreateFrameAbstractAction("Liste des abonnements") {
172
            @Override
173
            public JFrame createFrame() {
174
                IListFrame frame = new IListFrame(new ListeAddPanel(ctxt.getElement("ABONNEMENT")));
175
                return frame;
176
            }
177
        }, MainFrame.LIST_MENU);
178
 
179
        ctxt.addMenuItem(new CreateFrameAbstractAction("Saisie abonnements") {
180
            @Override
181
            public JFrame createFrame() {
182
                return new EditFrame(ctxt.getElement("ABONNEMENT"));
183
            }
184
        }, MainFrame.CREATE_MENU);
185
 
186
        ctxt.addMenuItem(new CreateFrameAbstractAction("Historique des abonnements") {
187
            @Override
188
            public JFrame createFrame() {
189
                HistoriqueAbonnement histo = new HistoriqueAbonnement();
190
 
191
                return histo.getFrame();
192
            }
193
        }, MainFrame.LIST_MENU);
194
    }
195
 
196
    @Override
197
    protected void start() {
198
        MainFrame.getInstance().getTabbedPane().addTab("Devis d'abonnements", new DevisAboPanel());
199
        MainFrame.getInstance().getTabbedPane().addTab("Bon de commande d'abonnements", new BonCommandeAboPanel());
200
        MainFrame.getInstance().getTabbedPane().addTab("Facture d'abonnements", new FacturesAboPanel());
201
        SourceCellValueProvider.register();
202
    }
203
 
204
    @Override
205
    protected void stop() {
206
    }
207
}