OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 160 | 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.event.ActionEvent;
4
import java.sql.SQLException;
30 ilm 5
import java.util.ArrayList;
140 ilm 6
import java.util.Calendar;
181 ilm 7
import java.util.Collection;
140 ilm 8
import java.util.Date;
30 ilm 9
import java.util.List;
140 ilm 10
import java.util.Set;
30 ilm 11
 
140 ilm 12
import javax.swing.AbstractAction;
13
import javax.swing.JOptionPane;
14
 
87 ilm 15
import org.openconcerto.erp.modules.AbstractModule;
16
import org.openconcerto.erp.modules.ModuleElement;
140 ilm 17
import org.openconcerto.sql.Configuration;
30 ilm 18
import org.openconcerto.sql.element.GlobalMapper;
19
import org.openconcerto.sql.element.SQLComponent;
140 ilm 20
import org.openconcerto.sql.element.SQLElement;
21
import org.openconcerto.sql.model.FieldPath;
22
import org.openconcerto.sql.model.SQLField;
23
import org.openconcerto.sql.model.SQLInjector;
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.SQLSelect;
28
import org.openconcerto.sql.model.SQLTable;
29
import org.openconcerto.sql.model.Where;
30
import org.openconcerto.sql.model.graph.Path;
31
import org.openconcerto.sql.users.UserManager;
147 ilm 32
import org.openconcerto.sql.users.rights.UserRightsManager;
140 ilm 33
import org.openconcerto.sql.view.EditFrame;
34
import org.openconcerto.sql.view.EditPanel.EditMode;
35
import org.openconcerto.sql.view.EditPanelListener;
36
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
37
import org.openconcerto.sql.view.list.IListe;
38
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
39
import org.openconcerto.sql.view.list.RowAction;
40
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
41
import org.openconcerto.sql.view.list.SQLTableModelSource;
42
import org.openconcerto.ui.EmailComposer;
43
import org.openconcerto.ui.FrameUtil;
30 ilm 44
import org.openconcerto.ui.group.Group;
140 ilm 45
import org.openconcerto.utils.CollectionUtils;
46
import org.openconcerto.utils.ExceptionHandler;
125 ilm 47
import org.openconcerto.utils.ListMap;
140 ilm 48
import org.openconcerto.utils.cc.ITransformer;
30 ilm 49
 
87 ilm 50
public class LeadSQLElement extends ModuleElement {
30 ilm 51
    public static final String ELEMENT_CODE = "customerrelationship.lead";
140 ilm 52
    public static String CODE_NOT_RESTRICT = "VIEW_ALL_LEADS";
30 ilm 53
 
87 ilm 54
    public LeadSQLElement(final AbstractModule module) {
55
        super(module, Module.TABLE_LEAD);
140 ilm 56
 
57
        // Call
181 ilm 58
        final RowAction.PredicateRowAction addDuplicateAction = new RowAction.PredicateRowAction(new AbstractAction("Créer à partir de") {
59
 
60
            @Override
61
            public void actionPerformed(ActionEvent e) {
62
                SQLRow sRow = IListe.get(e).getSelectedRow().asRow();
63
                final SQLTable table = LeadSQLElement.this.getTable().getTable(Module.TABLE_LEAD);
64
                final SQLElement leadElt = LeadSQLElement.this.getDirectory().getElement(table);
65
                EditFrame editFrame = new EditFrame(leadElt);
66
                final SQLRowValues sqlRowValues = new SQLRowValues(table);
67
                sqlRowValues.put("COMPANY", sRow.getObject("COMPANY"));
68
                sqlRowValues.put("PHONE", sRow.getObject("PHONE"));
69
                sqlRowValues.put("FAX", sRow.getObject("FAX"));
70
                sqlRowValues.put("WEBSITE", sRow.getObject("WEBSITE"));
71
                sqlRowValues.put("DATE", new Date());
72
                SQLRowValues adr = new SQLRowValues(sRow.getForeign("ID_ADRESSE").asRowValues());
73
                sqlRowValues.put("ID_ADRESSE", adr);
74
                sqlRowValues.put("INDUSTRY", sRow.getObject("INDUSTRY"));
75
                sqlRowValues.put("REVENUE", sRow.getObject("REVENUE"));
76
                sqlRowValues.put("EMPLOYEES", sRow.getObject("EMPLOYEES"));
77
                sqlRowValues.put("LOCALISATION", sRow.getObject("LOCALISATION"));
78
                sqlRowValues.put("SIRET", sRow.getObject("SIRET"));
79
                sqlRowValues.put("APE", sRow.getObject("APE"));
80
 
81
                editFrame.getSQLComponent().select(sqlRowValues);
82
                FrameUtil.show(editFrame);
83
            }
84
        }, true) {
85
        };
86
        addDuplicateAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
87
        getRowActions().add(addDuplicateAction);
88
 
89
        // Call
140 ilm 90
        final RowAction.PredicateRowAction addCallAction = new RowAction.PredicateRowAction(new AbstractAction("Appeler") {
91
 
92
            @Override
93
            public void actionPerformed(ActionEvent e) {
94
                SQLRow sRow = IListe.get(e).getSelectedRow().asRow();
95
                final SQLTable table = LeadSQLElement.this.getTable().getTable(Module.TABLE_LEAD_CALL);
96
                final SQLElement eCall = LeadSQLElement.this.getDirectory().getElement(table);
97
                EditFrame editFrame = new EditFrame(eCall);
98
                final SQLRowValues sqlRowValues = new SQLRowValues(table);
99
                sqlRowValues.put("ID_LEAD", sRow.getIDNumber());
100
                editFrame.getSQLComponent().select(sqlRowValues);
101
                FrameUtil.show(editFrame);
102
            }
103
        }, true) {
104
        };
105
        addCallAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
106
        getRowActions().add(addCallAction);
107
        // Visit
108
        final RowAction.PredicateRowAction addVisitAction = new RowAction.PredicateRowAction(new AbstractAction("Enregister une visite") {
109
 
110
            @Override
111
            public void actionPerformed(ActionEvent e) {
112
                SQLRow sRow = IListe.get(e).getSelectedRow().asRow();
113
                final SQLTable table = LeadSQLElement.this.getTable().getTable(Module.TABLE_LEAD_VISIT);
114
                final SQLElement eCall = LeadSQLElement.this.getDirectory().getElement(table);
115
                EditFrame editFrame = new EditFrame(eCall);
116
                final SQLRowValues sqlRowValues = new SQLRowValues(table);
117
                sqlRowValues.put("ID_LEAD", sRow.getIDNumber());
118
                editFrame.getSQLComponent().select(sqlRowValues);
119
                FrameUtil.show(editFrame);
120
            }
121
        }, true) {
122
        };
123
        addVisitAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
124
        getRowActions().add(addVisitAction);
125
 
126
        setRowActions();
30 ilm 127
    }
128
 
129
    @Override
140 ilm 130
    protected void _initTableSource(SQLTableModelSource source) {
131
 
132
        super._initTableSource(source);
133
 
147 ilm 134
        if (!UserRightsManager.getCurrentUserRights().haveRight(LeadSQLElement.CODE_NOT_RESTRICT)) {
135
            final SQLRow row = Configuration.getInstance().getRoot().findTable("USER_COMMON").getRow(UserManager.getInstance().getCurrentUser().getId());
136
            final List<SQLRow> rows = row.getReferentRows(Configuration.getInstance().getRoot().findTable("COMMERCIAL").getField("ID_USER_COMMON"));
140 ilm 137
            final List<Integer> listComm = new ArrayList<Integer>();
138
            for (SQLRow sqlRow : rows) {
139
                listComm.add(sqlRow.getID());
140
            }
147 ilm 141
            if (!listComm.isEmpty()) {
140 ilm 142
                source.getReq().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
143
 
144
                    @Override
145
                    public SQLSelect transformChecked(SQLSelect input) {
147 ilm 146
                        final SQLField field = input.getTable(Module.TABLE_LEAD).getField("ID_COMMERCIAL");
140 ilm 147
                        Where w = new Where(field, listComm);
148
                        w = w.or(new Where(field, "IS", (Object) null));
149
                        w = w.or(new Where(field, "=", getTable().getTable("COMMERCIAL").getUndefinedID()));
150
                        input.setWhere(w);
151
                        return input;
152
                    }
153
                });
154
            }
155
        }
156
 
181 ilm 157
        if (getTable().contains("MODIFICATION_DATE")) {
158
            BaseSQLTableModelColumn dateM = new BaseSQLTableModelColumn("Date de modification", Date.class) {
159
 
160
                @Override
161
                protected Object show_(SQLRowAccessor r) {
162
                    return r.getObject("MODIFICATION_DATE");
163
                }
164
 
165
                @Override
166
                public Set<FieldPath> getPaths() {
167
                    Path p = new Path(getTable());
168
                    return CollectionUtils.createSet(new FieldPath(p, "MODIFICATION_DATE"));
169
                }
170
            };
171
            source.getColumns().add(1, dateM);
172
        }
173
 
174
        BaseSQLTableModelColumn dateV = new BaseSQLTableModelColumn("Visite", Date.class) {
175
 
176
            @Override
177
            protected Object show_(SQLRowAccessor r) {
178
                Date d = null;
179
                Collection<? extends SQLRowAccessor> l = r.getReferentRows(r.getTable().getTable("LEAD_VISIT"));
180
                for (SQLRowAccessor sqlRowAccessor : l) {
181
                    if (d != null && sqlRowAccessor.getObject("DATE") != null && d.before(sqlRowAccessor.getDate("DATE").getTime())) {
182
                        d = sqlRowAccessor.getDate("DATE").getTime();
183
                    } else {
184
                        if (d == null && sqlRowAccessor.getObject("DATE") != null) {
185
                            d = sqlRowAccessor.getDate("DATE").getTime();
186
                        }
187
                    }
188
 
189
                }
190
                return d;
191
            }
192
 
193
            @Override
194
            public Set<FieldPath> getPaths() {
195
                Path p = new Path(getTable());
196
                p = p.add(p.getLast().getTable("LEAD_VISIT"));
197
                return CollectionUtils.createSet(new FieldPath(p, "DATE"));
198
            }
199
        };
200
        source.getColumns().add(1, dateV);
201
 
202
        BaseSQLTableModelColumn dateA = new BaseSQLTableModelColumn("Appel", Date.class) {
203
 
204
            @Override
205
            protected Object show_(SQLRowAccessor r) {
206
                Date d = null;
207
                Collection<? extends SQLRowAccessor> l = r.getReferentRows(r.getTable().getTable("LEAD_CALL"));
208
                for (SQLRowAccessor sqlRowAccessor : l) {
209
                    if (d != null && sqlRowAccessor.getObject("DATE") != null && d.before(sqlRowAccessor.getDate("DATE").getTime())) {
210
                        d = sqlRowAccessor.getDate("DATE").getTime();
211
                    } else {
212
                        if (d == null && sqlRowAccessor.getObject("DATE") != null) {
213
                            d = sqlRowAccessor.getDate("DATE").getTime();
214
                        }
215
                    }
216
 
217
                }
218
                return d;
219
            }
220
 
221
            @Override
222
            public Set<FieldPath> getPaths() {
223
                Path p = new Path(getTable());
224
                p = p.add(p.getLast().getTable("LEAD_CALL"));
225
                return CollectionUtils.createSet(new FieldPath(p, "DATE"));
226
            }
227
        };
228
        source.getColumns().add(1, dateA);
229
 
140 ilm 230
        BaseSQLTableModelColumn adresse = new BaseSQLTableModelColumn("Adresse", String.class) {
231
 
232
            @Override
233
            protected Object show_(SQLRowAccessor r) {
147 ilm 234
                final SQLRowAccessor rAdr = r.getForeign("ID_ADRESSE");
140 ilm 235
                if (rAdr != null && !rAdr.isUndefined()) {
236
                    return rAdr.getString("RUE");
237
                }
238
                return "";
239
            }
240
 
241
            @Override
242
            public Set<FieldPath> getPaths() {
243
                Path p = new Path(getTable());
244
                p = p.add(getTable().getTable("ADRESSE"));
245
                return CollectionUtils.createSet(new FieldPath(p, "RUE"));
246
            }
247
        };
248
        source.getColumns().add(adresse);
249
 
250
        BaseSQLTableModelColumn cp = new BaseSQLTableModelColumn("Code Postal", String.class) {
251
 
252
            @Override
253
            protected Object show_(SQLRowAccessor r) {
254
                SQLRowAccessor rAdr = r.getForeign("ID_ADRESSE");
255
                if (rAdr != null && !rAdr.isUndefined()) {
256
 
257
                    return rAdr.getString("CODE_POSTAL");
258
                }
259
 
260
                return "";
261
 
262
            }
263
 
264
            @Override
265
            public Set<FieldPath> getPaths() {
266
                Path p = new Path(getTable());
267
                p = p.add(getTable().getTable("ADRESSE"));
268
 
269
                return CollectionUtils.createSet(new FieldPath(p, "CODE_POSTAL"));
270
            }
271
        };
272
        source.getColumns().add(cp);
273
 
274
        BaseSQLTableModelColumn ville = new BaseSQLTableModelColumn("Ville", String.class) {
275
 
276
            @Override
277
            protected Object show_(SQLRowAccessor r) {
278
 
279
                SQLRowAccessor rAdr = r.getForeign("ID_ADRESSE");
280
                if (rAdr != null && !rAdr.isUndefined()) {
281
 
282
                    return rAdr.getString("VILLE");
283
                }
284
                return "";
285
 
286
            }
287
 
288
            @Override
289
            public Set<FieldPath> getPaths() {
290
                Path p = new Path(getTable());
291
                p = p.add(getTable().getTable("ADRESSE"));
292
 
293
                return CollectionUtils.createSet(new FieldPath(p, "VILLE"));
294
            }
295
        };
296
        source.getColumns().add(ville);
297
 
181 ilm 298
        BaseSQLTableModelColumn dpt = new BaseSQLTableModelColumn("Département", String.class) {
299
 
300
            @Override
301
            protected Object show_(SQLRowAccessor r) {
302
 
303
                String s = r.getForeign("ID_ADRESSE").getString("CODE_POSTAL");
304
                if (s != null && s.length() >= 2) {
305
                    return s.substring(0, 2);
306
                } else {
307
                    return s;
308
                }
309
 
310
            }
311
 
312
            @Override
313
            public Set<FieldPath> getPaths() {
314
                Path p = new Path(getTable());
315
                final SQLTable clientT = getTable().getForeignTable("ID_CLIENT");
316
                p = p.add(clientT);
317
                p = p.add(clientT.getField("ID_ADRESSE"));
318
                return CollectionUtils.createSet(new FieldPath(p, "VILLE"), new FieldPath(p, "CODE_POSTAL"));
319
            }
320
        };
321
 
322
        source.getColumns().add(dpt);
323
 
140 ilm 324
        if (getTable().contains("REMIND_DATE")) {
325
            BaseSQLTableModelColumn dateRemind = new BaseSQLTableModelColumn("Date de rappel", Date.class) {
326
 
327
                @Override
328
                protected Object show_(SQLRowAccessor r) {
329
 
330
                    Calendar c = r.getDate("REMIND_DATE");
331
                    if (c == null) {
332
                        return null;
333
                    } else {
334
                        return c.getTime();
335
                    }
336
 
337
                }
338
 
339
                @Override
340
                public Set<FieldPath> getPaths() {
341
                    Path p = new Path(getTable());
342
                    return CollectionUtils.createSet(new FieldPath(p, "REMIND_DATE"));
343
                }
344
            };
345
 
346
            dateRemind.setRenderer(new RemindDateRenderer());
347
            source.getColumns().add(dateRemind);
348
 
349
        }
350
    }
351
 
352
    private void setRowActions() {
353
 
354
        AbstractAction action = new AbstractAction("Transférer en client") {
355
 
356
            @Override
357
            public void actionPerformed(ActionEvent e) {
358
 
359
                final SQLRow row = IListe.get(e).getSelectedRow().asRow();
360
 
361
                SQLRowAccessor foreign = row.getForeign("ID_CLIENT");
362
                if (foreign == null || foreign.isUndefined()) {
363
 
364
                    // Client
365
                    SQLRowValues rowVals = SQLInjector.getInjector(row.getTable(), row.getTable().getTable("CLIENT")).createRowValuesFrom(row);
366
 
367
                    SQLRowAccessor adresse = row.getForeign("ID_ADRESSE");
368
                    if (adresse != null && !adresse.isUndefined()) {
369
                        SQLRowValues rowValsAdr = new SQLRowValues(adresse.asRowValues());
370
                        // rowValsAdr.clearPrimaryKeys();
371
                        rowVals.put("ID_ADRESSE", rowValsAdr);
372
                    }
373
 
374
                    // Contact
375
                    SQLRowValues rowValsContact = SQLInjector.getInjector(row.getTable(), row.getTable().getTable("CONTACT")).createRowValuesFrom(row);
376
                    rowValsContact.put("ID_CLIENT", rowVals);
377
 
378
                    EditFrame frame = new EditFrame(Configuration.getInstance().getDirectory().getElement("CLIENT"), EditMode.CREATION);
379
                    frame.getSQLComponent().select(rowVals);
380
                    frame.setVisible(true);
381
 
382
                    frame.addEditPanelListener(new EditPanelListener() {
383
 
384
                        @Override
385
                        public void modified() {
386
                        }
387
 
388
                        @Override
389
                        public void inserted(int id) {
390
                            SQLRowValues rowVals = row.asRowValues();
391
                            rowVals.put("ID_CLIENT", id);
392
                            rowVals.put("STATUS", "Acquis");
393
                            try {
394
                                rowVals.commit();
395
                            } catch (SQLException exn) {
396
                                exn.printStackTrace();
397
                            }
398
                        }
399
 
400
                        @Override
401
                        public void deleted() {
402
                        }
403
 
404
                        @Override
405
                        public void cancelled() {
406
                        }
407
                    });
408
                } else {
409
                    JOptionPane.showMessageDialog(null, "Ce prospect a déjà été transféré en client!");
410
                }
411
            }
412
        };
413
        PredicateRowAction transfertClient = new PredicateRowAction(action, true);
414
        transfertClient.setPredicate(IListeEvent.getSingleSelectionPredicate());
415
        this.getRowActions().add(transfertClient);
416
 
417
        AbstractAction actionMail = new AbstractAction("Envoyer un e-mail") {
418
 
419
            @Override
420
            public void actionPerformed(ActionEvent e) {
421
 
422
                sendMail(IListe.get(e).getSelectedRows());
423
 
424
            }
425
        };
426
        PredicateRowAction mail = new PredicateRowAction(actionMail, true);
427
        mail.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
428
        this.getRowActions().add(mail);
429
    }
430
 
431
    protected void sendMail(List<SQLRowValues> l) {
432
 
433
        String mail = "";
434
 
435
        // #endif
436
        for (SQLRowAccessor rowCli : l) {
437
            String string = rowCli.getString("EMAIL");
438
            if (string != null && string.trim().length() > 0) {
439
                mail += string + ";";
440
            }
441
        }
442
 
443
        try {
444
            EmailComposer.getInstance().compose(mail, "", "");
445
        } catch (Exception exn) {
446
            ExceptionHandler.handle(null, "Impossible de créer le courriel", exn);
447
        }
448
 
449
    }
450
 
451
    @Override
30 ilm 452
    protected String createCode() {
453
        return ELEMENT_CODE;
454
    }
455
 
456
    @Override
457
    protected List<String> getListFields() {
458
        final List<String> l = new ArrayList<String>();
140 ilm 459
        l.add("ID_COMMERCIAL");
460
        l.add("DATE");
30 ilm 461
        l.add("COMPANY");
140 ilm 462
        l.add("LOCALISATION");
463
        l.add("ID_TITRE_PERSONNEL");
464
        l.add("NAME");
30 ilm 465
        l.add("FIRSTNAME");
140 ilm 466
        l.add("PHONE");
467
        l.add("MOBILE");
468
        l.add("EMAIL");
469
        l.add("SOURCE");
470
        l.add("INFORMATION");
471
        l.add("INFOS");
472
        // l.add("REMIND_DATE");
30 ilm 473
        return l;
474
    }
475
 
476
    @Override
477
    protected List<String> getComboFields() {
478
        final List<String> l = new ArrayList<String>();
479
        l.add("COMPANY");
480
        l.add("FIRSTNAME");
481
        l.add("NAME");
482
        return l;
483
    }
484
 
485
    @Override
486
    protected List<String> getPrivateFields() {
487
        final List<String> l = new ArrayList<String>();
488
        l.add("ID_ADRESSE");
489
        return l;
490
    }
491
 
492
    @Override
125 ilm 493
    public ListMap<String, String> getShowAs() {
494
        return ListMap.singleton(null, getComboFields());
30 ilm 495
    }
496
 
497
    @Override
498
    public SQLComponent createComponent() {
499
        final String groupId = this.getCode() + ".default";
500
        final Group group = GlobalMapper.getInstance().getGroup(groupId);
501
        if (group == null) {
502
            throw new IllegalStateException("No group found for id " + groupId);
503
        }
87 ilm 504
        return createComponent(group);
505
    }
506
 
507
    protected SQLComponent createComponent(final Group group) {
30 ilm 508
        return new LeadSQLComponent(this, group);
509
    }
510
}