OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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