OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 154 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ilm 1
package org.openconcerto.modules.customerrelationship.lead;
2
 
140 ilm 3
import java.awt.Component;
4
import java.awt.FileDialog;
5
import java.awt.Frame;
6
import java.awt.event.ActionEvent;
30 ilm 7
import java.io.File;
140 ilm 8
import java.io.FilenameFilter;
30 ilm 9
import java.io.IOException;
140 ilm 10
import java.sql.SQLException;
30 ilm 11
 
140 ilm 12
import javax.swing.AbstractAction;
13
import javax.swing.JFrame;
14
 
15
import org.openconcerto.erp.action.CreateFrameAbstractAction;
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
30 ilm 17
import org.openconcerto.erp.config.MainFrame;
18
import org.openconcerto.erp.modules.AbstractModule;
19
import org.openconcerto.erp.modules.ComponentsContext;
20
import org.openconcerto.erp.modules.DBContext;
77 ilm 21
import org.openconcerto.erp.modules.MenuContext;
30 ilm 22
import org.openconcerto.erp.modules.ModuleFactory;
140 ilm 23
import org.openconcerto.modules.customerrelationship.lead.call.CustomerCallSQLElement;
24
import org.openconcerto.modules.customerrelationship.lead.call.CustomerCallServiceSQLElement;
25
import org.openconcerto.modules.customerrelationship.lead.call.LeadCallSQLElement;
26
import org.openconcerto.modules.customerrelationship.lead.call.LeadCallServiceSQLElement;
27
import org.openconcerto.modules.customerrelationship.lead.importer.LeadImporter;
28
import org.openconcerto.modules.customerrelationship.lead.visit.CustomerVisitSQLElement;
29
import org.openconcerto.modules.customerrelationship.lead.visit.CustomerVisitServiceSQLElement;
30
import org.openconcerto.modules.customerrelationship.lead.visit.LeadVisitSQLElement;
31
import org.openconcerto.modules.customerrelationship.lead.visit.LeadVisitServiceSQLElement;
32
import org.openconcerto.openoffice.ContentTypeVersioned;
30 ilm 33
import org.openconcerto.sql.element.GlobalMapper;
140 ilm 34
import org.openconcerto.sql.element.SQLElement;
30 ilm 35
import org.openconcerto.sql.element.SQLElementDirectory;
140 ilm 36
import org.openconcerto.sql.model.ConnectionHandlerNoSetup;
37
import org.openconcerto.sql.model.DBRoot;
38
import org.openconcerto.sql.model.SQLDataSource;
39
import org.openconcerto.sql.model.SQLRow;
40
import org.openconcerto.sql.model.SQLRowValues;
41
import org.openconcerto.sql.model.SQLTable;
30 ilm 42
import org.openconcerto.sql.utils.SQLCreateTable;
140 ilm 43
import org.openconcerto.sql.utils.SQLUtils;
44
import org.openconcerto.sql.view.EditFrame;
45
import org.openconcerto.sql.view.ListeAddPanel;
46
import org.openconcerto.sql.view.list.IListe;
47
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
48
import org.openconcerto.sql.view.list.RowAction;
49
import org.openconcerto.ui.FrameUtil;
50
import org.openconcerto.ui.SwingThreadUtils;
51
import org.openconcerto.utils.ExceptionHandler;
30 ilm 52
 
53
public final class Module extends AbstractModule {
54
 
55
    public static final String TABLE_LEAD = "LEAD";
140 ilm 56
    public static final String TABLE_LEAD_CALL = "LEAD_CALL";
57
    public static final String TABLE_CUSTOMER_CALL = "CUSTOMER_CALL";
58
    public static final String TABLE_LEAD_VISIT = "LEAD_VISIT";
59
    public static final String TABLE_CUSTOMER_VISIT = "CUSTOMER_VISIT";
60
    public static final String TABLE_SERVICE = "SERVICE";
61
    //
62
    public static final String TABLE_LEAD_CALL_SERVICE = "LEAD_CALL_SERVICE";
63
    public static final String TABLE_CUSTOMER_CALL_SERVICE = "CUSTOMER_CALL_SERVICE";
64
    public static final String TABLE_LEAD_VISIT_SERVICE = "LEAD_VISIT_SERVICE";
65
    public static final String TABLE_CUSTOMER_VISIT_SERVICE = "CUSTOMER_VISIT_SERVICE";
30 ilm 66
 
67
    public Module(ModuleFactory f) throws IOException {
68
        super(f);
69
    }
70
 
71
    @Override
154 ilm 72
    protected void install(DBContext ctxt) throws SQLException, IOException {
30 ilm 73
        super.install(ctxt);
74
        // TODO use version to upgrade
77 ilm 75
        if (ctxt.getRoot().getTable(TABLE_LEAD) == null) {
30 ilm 76
            final SQLCreateTable createTable = ctxt.getCreateTable(TABLE_LEAD);
77
 
78
            createTable.addVarCharColumn("NUMBER", 20);
79
            createTable.addDateAndTimeColumn("DATE");
80
            createTable.addVarCharColumn("FIRSTNAME", 64);
81
            createTable.addVarCharColumn("NAME", 64);
82
            createTable.addVarCharColumn("COMPANY", 64);
83
            //
84
            createTable.addVarCharColumn("PHONE", 16);
85
            createTable.addVarCharColumn("MOBILE", 16);
86
            createTable.addVarCharColumn("FAX", 16);
87
            createTable.addVarCharColumn("EMAIL", 32);
88
            createTable.addVarCharColumn("WEBSITE", 64);
89
            //
90
            createTable.addVarCharColumn("SOURCE", 200);
91
            createTable.addVarCharColumn("STATUS", 50);
92
            //
93
            createTable.addForeignColumn("ADRESSE");
94
            createTable.addForeignColumn("COMMERCIAL");
95
            //
96
            createTable.addVarCharColumn("INFORMATION", 512);
97
            //
98
            createTable.addVarCharColumn("INDUSTRY", 200);
99
            createTable.addVarCharColumn("RATING", 200);
100
 
101
            createTable.addIntegerColumn("REVENUE", 0);
102
            createTable.addIntegerColumn("EMPLOYEES", 0);
103
 
140 ilm 104
            createTable.addVarCharColumn("ROLE", 128);
105
            createTable.addForeignColumn("CLIENT");
106
            createTable.addVarCharColumn("LOCALISATION", 256);
107
            createTable.addVarCharColumn("INFOS", 128);
108
            createTable.addForeignColumn("TITRE_PERSONNEL");
109
            createTable.addDateAndTimeColumn("REMIND_DATE");
110
            createTable.addVarCharColumn("APE", 128);
111
            createTable.addVarCharColumn("SIRET", 256);
112
            createTable.addVarCharColumn("DISPO", 256);
113
            createTable.addBooleanColumn("MAILING", Boolean.FALSE, false);
114
 
115
            // V2
116
 
117
            // Appel à un prospect
118
            final SQLCreateTable createLCall = ctxt.getCreateTable(TABLE_LEAD_CALL);
119
            createLCall.addDateAndTimeColumn("DATE");
120
            createLCall.addForeignColumn(createTable);
121
            createLCall.addVarCharColumn("INFORMATION", 10240);
122
            createLCall.addDateAndTimeColumn("NEXTCONTACT_DATE");
123
            createLCall.addVarCharColumn("NEXTCONTACT_INFO", 1024);
124
 
125
            // Appel à un client
126
            final SQLCreateTable createCCall = ctxt.getCreateTable(TABLE_CUSTOMER_CALL);
127
            createCCall.addDateAndTimeColumn("DATE");
128
            createCCall.addForeignColumn("CLIENT");
129
            createCCall.addVarCharColumn("INFORMATION", 10240);
130
            createCCall.addDateAndTimeColumn("NEXTCONTACT_DATE");
131
            createCCall.addVarCharColumn("NEXTCONTACT_INFO", 1024);
132
 
133
            // Visites chez un prospect
134
            final SQLCreateTable createLV = ctxt.getCreateTable(TABLE_LEAD_VISIT);
135
            createLV.addDateAndTimeColumn("DATE");
136
            createLV.addForeignColumn(createTable);
137
            createLV.addVarCharColumn("INFORMATION", 10240);
138
            createLV.addDateAndTimeColumn("NEXTCONTACT_DATE");
139
            createLV.addVarCharColumn("NEXTCONTACT_INFO", 1024);
140
            // Visites chez un client
141
            final SQLCreateTable createCV = ctxt.getCreateTable(TABLE_CUSTOMER_VISIT);
142
            createCV.addDateAndTimeColumn("DATE");
143
            createCV.addForeignColumn("CLIENT");
144
            createCV.addVarCharColumn("INFORMATION", 10240);
145
            createCV.addDateAndTimeColumn("NEXTCONTACT_DATE");
146
            createCV.addVarCharColumn("NEXTCONTACT_INFO", 1024);
147
 
148
            // Services
149
            final SQLCreateTable createService = ctxt.getCreateTable(TABLE_SERVICE);
150
            createService.addVarCharColumn("NAME", 256);
151
 
152
            //
153
            if (ctxt.getRoot().getTable(TABLE_LEAD_CALL_SERVICE) == null) {
154
                final SQLCreateTable createTable1 = ctxt.getCreateTable(TABLE_LEAD_CALL_SERVICE);
155
                createTable1.addForeignColumn(createLCall);
156
                createTable1.addForeignColumn(createService);
157
            }
158
            if (ctxt.getRoot().getTable(TABLE_CUSTOMER_CALL_SERVICE) == null) {
159
                final SQLCreateTable createTable1 = ctxt.getCreateTable(TABLE_CUSTOMER_CALL_SERVICE);
160
                createTable1.addForeignColumn(createCCall);
161
                createTable1.addForeignColumn(createService);
162
            }
163
            if (ctxt.getRoot().getTable(TABLE_LEAD_VISIT_SERVICE) == null) {
164
                final SQLCreateTable createTable1 = ctxt.getCreateTable(TABLE_LEAD_VISIT_SERVICE);
165
                createTable1.addForeignColumn(createLV);
166
                createTable1.addForeignColumn(createService);
167
            }
168
            if (ctxt.getRoot().getTable(TABLE_CUSTOMER_VISIT_SERVICE) == null) {
169
                final SQLCreateTable createTable1 = ctxt.getCreateTable(TABLE_CUSTOMER_VISIT_SERVICE);
170
                createTable1.addForeignColumn(createCV);
171
                createTable1.addForeignColumn(createService);
172
            }
30 ilm 173
        }
174
    }
175
 
176
    @Override
140 ilm 177
    protected void setupElements(final SQLElementDirectory dir) {
30 ilm 178
        super.setupElements(dir);
140 ilm 179
 
87 ilm 180
        final LeadSQLElement element = new LeadSQLElement(this);
181
        GlobalMapper.getInstance().map(element.getCode() + ".default", new LeadGroup());
30 ilm 182
        dir.addSQLElement(element);
140 ilm 183
 
184
        dir.addSQLElement(new LeadCallSQLElement(this));
185
        dir.addSQLElement(new CustomerCallSQLElement(this));
186
        dir.addSQLElement(new LeadVisitSQLElement(this));
187
        dir.addSQLElement(new CustomerVisitSQLElement(this));
188
        dir.addSQLElement(new ServiceSQLElement(this));
189
        // Services
190
        dir.addSQLElement(new LeadCallServiceSQLElement(this));
191
        dir.addSQLElement(new CustomerCallServiceSQLElement(this));
192
        dir.addSQLElement(new LeadVisitServiceSQLElement(this));
193
        dir.addSQLElement(new CustomerVisitServiceSQLElement(this));
194
 
195
        // Call
196
        final RowAction.PredicateRowAction addCallAction = new RowAction.PredicateRowAction(new AbstractAction("Appeler") {
197
 
198
            @Override
199
            public void actionPerformed(ActionEvent e) {
200
                SQLRow sRow = IListe.get(e).getSelectedRow().asRow();
201
                final SQLElement eCall = dir.getElement(Module.TABLE_CUSTOMER_CALL);
202
                final SQLTable table = eCall.getTable();
203
                EditFrame editFrame = new EditFrame(eCall);
204
                final SQLRowValues sqlRowValues = new SQLRowValues(table);
205
                sqlRowValues.put("ID_CLIENT", sRow.getIDNumber());
206
                editFrame.getSQLComponent().select(sqlRowValues);
207
                FrameUtil.show(editFrame);
208
            }
209
        }, true) {
210
        };
211
        addCallAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
212
        dir.getElement("CLIENT").getRowActions().add(addCallAction);
213
        // Visit
214
        final RowAction.PredicateRowAction addVisitAction = new RowAction.PredicateRowAction(new AbstractAction("Enregister une visite") {
215
 
216
            @Override
217
            public void actionPerformed(ActionEvent e) {
218
                SQLRow sRow = IListe.get(e).getSelectedRow().asRow();
219
                final SQLElement eCall = dir.getElement(Module.TABLE_CUSTOMER_VISIT);
220
                final SQLTable table = eCall.getTable();
221
                EditFrame editFrame = new EditFrame(eCall);
222
                final SQLRowValues sqlRowValues = new SQLRowValues(table);
223
                sqlRowValues.put("ID_CLIENT", sRow.getIDNumber());
224
                editFrame.getSQLComponent().select(sqlRowValues);
225
                FrameUtil.show(editFrame);
226
            }
227
        }, true) {
228
        };
229
        addVisitAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
230
        dir.getElement("CLIENT").getRowActions().add(addVisitAction);
231
 
30 ilm 232
    }
233
 
234
    @Override
235
    protected void setupComponents(ComponentsContext ctxt) {
77 ilm 236
 
237
    }
238
 
239
    @Override
140 ilm 240
    protected void setupMenu(final MenuContext ctxt) {
241
        ctxt.addMenuItem(new LeadListAction(), MainFrame.LIST_MENU);
242
 
243
        ctxt.addMenuItem(new CreateFrameAbstractAction("Liste des services") {
244
            @Override
245
            public JFrame createFrame() {
246
                final JFrame frame = new JFrame("Services proposés");
247
                frame.setContentPane(new ListeAddPanel(ctxt.getElement(TABLE_SERVICE)));
248
                return frame;
249
            }
250
        }, MainFrame.STRUCTURE_MENU);
251
        ctxt.addMenuItem(new SuiviClientProspectListAction(), MainFrame.LIST_MENU);
252
        // ctxt.addMenuItem(ctxt.createListAction(TABLE_CUSTOMER_CALL), MainFrame.LIST_MENU);
253
        // ctxt.addMenuItem(ctxt.createListAction(TABLE_CUSTOMER_VISIT), MainFrame.LIST_MENU);
254
        // ctxt.addMenuItem(ctxt.createListAction(TABLE_LEAD_CALL), MainFrame.LIST_MENU);
255
        // ctxt.addMenuItem(ctxt.createListAction(TABLE_LEAD_VISIT), MainFrame.LIST_MENU);
256
 
257
        ctxt.addMenuItem(new AbstractAction("Import de prospects") {
258
            @Override
259
            public void actionPerformed(ActionEvent e) {
260
                final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
261
                final FileDialog fd = new FileDialog(frame, "Import de prospects", FileDialog.LOAD);
262
                fd.setFilenameFilter(new FilenameFilter() {
263
                    @Override
264
                    public boolean accept(File dir, String name) {
265
                        return name.endsWith("." + ContentTypeVersioned.SPREADSHEET.getExtension());
266
                    }
267
                });
268
                fd.setVisible(true);
269
                if (fd.getFile() != null) {
270
                    final DBRoot rootSociete = ((ComptaPropsConfiguration) ComptaPropsConfiguration.getInstance()).getRootSociete();
271
                    try {
272
                        SQLUtils.executeAtomic(rootSociete.getDBSystemRoot().getDataSource(), new ConnectionHandlerNoSetup<Object, IOException>() {
273
                            @Override
274
                            public Object handle(final SQLDataSource ds) throws SQLException, IOException {
275
                                File f = new File(fd.getDirectory());
276
 
277
                                LeadImporter imp = new LeadImporter();
278
                                try {
279
                                    imp.exportLead(rootSociete, f, new File(fd.getDirectory(), fd.getFile()));
280
                                    imp.importFromFile(f, rootSociete);
281
                                } catch (Exception exn) {
282
                                    // TODO Bloc catch auto-généré
283
                                    exn.printStackTrace();
284
                                }
285
                                return null;
286
                            }
287
                        });
288
                    } catch (IOException exn) {
289
                        ExceptionHandler.handle(frame, "Erreur lors de la lecture du fichier", exn);
290
                    } catch (SQLException exn) {
291
                        ExceptionHandler.handle(frame, "Erreur lors de l'insertion dans la base", exn);
292
                    }
293
                }
294
            }
295
        }, MainFrame.FILE_MENU);
30 ilm 296
    }
297
 
298
    @Override
299
    protected void start() {
140 ilm 300
        new LeadCustomerSQLInjector();
301
        new LeadContactSQLInjector();
30 ilm 302
    }
303
 
304
    @Override
305
    protected void stop() {
306
    }
307
 
308
}