OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 147 | 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;
7
import java.util.Date;
30 ilm 8
import java.util.List;
140 ilm 9
import java.util.Set;
30 ilm 10
 
140 ilm 11
import javax.swing.AbstractAction;
12
import javax.swing.JOptionPane;
13
 
87 ilm 14
import org.openconcerto.erp.modules.AbstractModule;
15
import org.openconcerto.erp.modules.ModuleElement;
140 ilm 16
import org.openconcerto.sql.Configuration;
30 ilm 17
import org.openconcerto.sql.element.GlobalMapper;
18
import org.openconcerto.sql.element.SQLComponent;
140 ilm 19
import org.openconcerto.sql.element.SQLElement;
20
import org.openconcerto.sql.model.FieldPath;
21
import org.openconcerto.sql.model.SQLField;
22
import org.openconcerto.sql.model.SQLInjector;
23
import org.openconcerto.sql.model.SQLRow;
24
import org.openconcerto.sql.model.SQLRowAccessor;
25
import org.openconcerto.sql.model.SQLRowValues;
26
import org.openconcerto.sql.model.SQLSelect;
27
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.sql.model.Where;
29
import org.openconcerto.sql.model.graph.Path;
30
import org.openconcerto.sql.users.UserManager;
147 ilm 31
import org.openconcerto.sql.users.rights.UserRightsManager;
140 ilm 32
import org.openconcerto.sql.view.EditFrame;
33
import org.openconcerto.sql.view.EditPanel.EditMode;
34
import org.openconcerto.sql.view.EditPanelListener;
35
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
36
import org.openconcerto.sql.view.list.IListe;
37
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
38
import org.openconcerto.sql.view.list.RowAction;
39
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
40
import org.openconcerto.sql.view.list.SQLTableModelSource;
41
import org.openconcerto.ui.EmailComposer;
42
import org.openconcerto.ui.FrameUtil;
30 ilm 43
import org.openconcerto.ui.group.Group;
140 ilm 44
import org.openconcerto.utils.CollectionUtils;
45
import org.openconcerto.utils.ExceptionHandler;
125 ilm 46
import org.openconcerto.utils.ListMap;
140 ilm 47
import org.openconcerto.utils.cc.ITransformer;
30 ilm 48
 
87 ilm 49
public class LeadSQLElement extends ModuleElement {
30 ilm 50
    public static final String ELEMENT_CODE = "customerrelationship.lead";
140 ilm 51
    public static String CODE_NOT_RESTRICT = "VIEW_ALL_LEADS";
30 ilm 52
 
87 ilm 53
    public LeadSQLElement(final AbstractModule module) {
54
        super(module, Module.TABLE_LEAD);
140 ilm 55
 
56
        // Call
57
        final RowAction.PredicateRowAction addCallAction = new RowAction.PredicateRowAction(new AbstractAction("Appeler") {
58
 
59
            @Override
60
            public void actionPerformed(ActionEvent e) {
61
                SQLRow sRow = IListe.get(e).getSelectedRow().asRow();
62
                final SQLTable table = LeadSQLElement.this.getTable().getTable(Module.TABLE_LEAD_CALL);
63
                final SQLElement eCall = LeadSQLElement.this.getDirectory().getElement(table);
64
                EditFrame editFrame = new EditFrame(eCall);
65
                final SQLRowValues sqlRowValues = new SQLRowValues(table);
66
                sqlRowValues.put("ID_LEAD", sRow.getIDNumber());
67
                editFrame.getSQLComponent().select(sqlRowValues);
68
                FrameUtil.show(editFrame);
69
            }
70
        }, true) {
71
        };
72
        addCallAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
73
        getRowActions().add(addCallAction);
74
        // Visit
75
        final RowAction.PredicateRowAction addVisitAction = new RowAction.PredicateRowAction(new AbstractAction("Enregister une visite") {
76
 
77
            @Override
78
            public void actionPerformed(ActionEvent e) {
79
                SQLRow sRow = IListe.get(e).getSelectedRow().asRow();
80
                final SQLTable table = LeadSQLElement.this.getTable().getTable(Module.TABLE_LEAD_VISIT);
81
                final SQLElement eCall = LeadSQLElement.this.getDirectory().getElement(table);
82
                EditFrame editFrame = new EditFrame(eCall);
83
                final SQLRowValues sqlRowValues = new SQLRowValues(table);
84
                sqlRowValues.put("ID_LEAD", sRow.getIDNumber());
85
                editFrame.getSQLComponent().select(sqlRowValues);
86
                FrameUtil.show(editFrame);
87
            }
88
        }, true) {
89
        };
90
        addVisitAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
91
        getRowActions().add(addVisitAction);
92
 
93
        setRowActions();
30 ilm 94
    }
95
 
96
    @Override
140 ilm 97
    protected void _initTableSource(SQLTableModelSource source) {
98
 
99
        super._initTableSource(source);
100
 
147 ilm 101
        if (!UserRightsManager.getCurrentUserRights().haveRight(LeadSQLElement.CODE_NOT_RESTRICT)) {
102
            final SQLRow row = Configuration.getInstance().getRoot().findTable("USER_COMMON").getRow(UserManager.getInstance().getCurrentUser().getId());
103
            final List<SQLRow> rows = row.getReferentRows(Configuration.getInstance().getRoot().findTable("COMMERCIAL").getField("ID_USER_COMMON"));
140 ilm 104
            final List<Integer> listComm = new ArrayList<Integer>();
105
            for (SQLRow sqlRow : rows) {
106
                listComm.add(sqlRow.getID());
107
            }
147 ilm 108
            if (!listComm.isEmpty()) {
140 ilm 109
                source.getReq().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
110
 
111
                    @Override
112
                    public SQLSelect transformChecked(SQLSelect input) {
147 ilm 113
                        final SQLField field = input.getTable(Module.TABLE_LEAD).getField("ID_COMMERCIAL");
140 ilm 114
                        Where w = new Where(field, listComm);
115
                        w = w.or(new Where(field, "IS", (Object) null));
116
                        w = w.or(new Where(field, "=", getTable().getTable("COMMERCIAL").getUndefinedID()));
117
                        input.setWhere(w);
118
                        return input;
119
                    }
120
                });
121
            }
122
        }
123
 
124
        BaseSQLTableModelColumn adresse = new BaseSQLTableModelColumn("Adresse", String.class) {
125
 
126
            @Override
127
            protected Object show_(SQLRowAccessor r) {
147 ilm 128
                final SQLRowAccessor rAdr = r.getForeign("ID_ADRESSE");
140 ilm 129
                if (rAdr != null && !rAdr.isUndefined()) {
130
                    return rAdr.getString("RUE");
131
                }
132
                return "";
133
            }
134
 
135
            @Override
136
            public Set<FieldPath> getPaths() {
137
                Path p = new Path(getTable());
138
                p = p.add(getTable().getTable("ADRESSE"));
139
                return CollectionUtils.createSet(new FieldPath(p, "RUE"));
140
            }
141
        };
142
        source.getColumns().add(adresse);
143
 
144
        BaseSQLTableModelColumn cp = new BaseSQLTableModelColumn("Code Postal", String.class) {
145
 
146
            @Override
147
            protected Object show_(SQLRowAccessor r) {
148
                SQLRowAccessor rAdr = r.getForeign("ID_ADRESSE");
149
                if (rAdr != null && !rAdr.isUndefined()) {
150
 
151
                    return rAdr.getString("CODE_POSTAL");
152
                }
153
 
154
                return "";
155
 
156
            }
157
 
158
            @Override
159
            public Set<FieldPath> getPaths() {
160
                Path p = new Path(getTable());
161
                p = p.add(getTable().getTable("ADRESSE"));
162
 
163
                return CollectionUtils.createSet(new FieldPath(p, "CODE_POSTAL"));
164
            }
165
        };
166
        source.getColumns().add(cp);
167
 
168
        BaseSQLTableModelColumn ville = new BaseSQLTableModelColumn("Ville", String.class) {
169
 
170
            @Override
171
            protected Object show_(SQLRowAccessor r) {
172
 
173
                SQLRowAccessor rAdr = r.getForeign("ID_ADRESSE");
174
                if (rAdr != null && !rAdr.isUndefined()) {
175
 
176
                    return rAdr.getString("VILLE");
177
                }
178
                return "";
179
 
180
            }
181
 
182
            @Override
183
            public Set<FieldPath> getPaths() {
184
                Path p = new Path(getTable());
185
                p = p.add(getTable().getTable("ADRESSE"));
186
 
187
                return CollectionUtils.createSet(new FieldPath(p, "VILLE"));
188
            }
189
        };
190
        source.getColumns().add(ville);
191
 
192
        if (getTable().contains("REMIND_DATE")) {
193
            BaseSQLTableModelColumn dateRemind = new BaseSQLTableModelColumn("Date de rappel", Date.class) {
194
 
195
                @Override
196
                protected Object show_(SQLRowAccessor r) {
197
 
198
                    Calendar c = r.getDate("REMIND_DATE");
199
                    if (c == null) {
200
                        return null;
201
                    } else {
202
                        return c.getTime();
203
                    }
204
 
205
                }
206
 
207
                @Override
208
                public Set<FieldPath> getPaths() {
209
                    Path p = new Path(getTable());
210
                    return CollectionUtils.createSet(new FieldPath(p, "REMIND_DATE"));
211
                }
212
            };
213
 
214
            dateRemind.setRenderer(new RemindDateRenderer());
215
            source.getColumns().add(dateRemind);
216
 
217
        }
218
    }
219
 
220
    private void setRowActions() {
221
 
222
        AbstractAction action = new AbstractAction("Transférer en client") {
223
 
224
            @Override
225
            public void actionPerformed(ActionEvent e) {
226
 
227
                final SQLRow row = IListe.get(e).getSelectedRow().asRow();
228
 
229
                SQLRowAccessor foreign = row.getForeign("ID_CLIENT");
230
                if (foreign == null || foreign.isUndefined()) {
231
 
232
                    // Client
233
                    SQLRowValues rowVals = SQLInjector.getInjector(row.getTable(), row.getTable().getTable("CLIENT")).createRowValuesFrom(row);
234
 
235
                    SQLRowAccessor adresse = row.getForeign("ID_ADRESSE");
236
                    if (adresse != null && !adresse.isUndefined()) {
237
                        SQLRowValues rowValsAdr = new SQLRowValues(adresse.asRowValues());
238
                        // rowValsAdr.clearPrimaryKeys();
239
                        rowVals.put("ID_ADRESSE", rowValsAdr);
240
                    }
241
 
242
                    // Contact
243
                    SQLRowValues rowValsContact = SQLInjector.getInjector(row.getTable(), row.getTable().getTable("CONTACT")).createRowValuesFrom(row);
244
                    rowValsContact.put("ID_CLIENT", rowVals);
245
 
246
                    EditFrame frame = new EditFrame(Configuration.getInstance().getDirectory().getElement("CLIENT"), EditMode.CREATION);
247
                    frame.getSQLComponent().select(rowVals);
248
                    frame.setVisible(true);
249
 
250
                    frame.addEditPanelListener(new EditPanelListener() {
251
 
252
                        @Override
253
                        public void modified() {
254
                        }
255
 
256
                        @Override
257
                        public void inserted(int id) {
258
                            SQLRowValues rowVals = row.asRowValues();
259
                            rowVals.put("ID_CLIENT", id);
260
                            rowVals.put("STATUS", "Acquis");
261
                            try {
262
                                rowVals.commit();
263
                            } catch (SQLException exn) {
264
                                exn.printStackTrace();
265
                            }
266
                        }
267
 
268
                        @Override
269
                        public void deleted() {
270
                        }
271
 
272
                        @Override
273
                        public void cancelled() {
274
                        }
275
                    });
276
                } else {
277
                    JOptionPane.showMessageDialog(null, "Ce prospect a déjà été transféré en client!");
278
                }
279
            }
280
        };
281
        PredicateRowAction transfertClient = new PredicateRowAction(action, true);
282
        transfertClient.setPredicate(IListeEvent.getSingleSelectionPredicate());
283
        this.getRowActions().add(transfertClient);
284
 
285
        AbstractAction actionMail = new AbstractAction("Envoyer un e-mail") {
286
 
287
            @Override
288
            public void actionPerformed(ActionEvent e) {
289
 
290
                sendMail(IListe.get(e).getSelectedRows());
291
 
292
            }
293
        };
294
        PredicateRowAction mail = new PredicateRowAction(actionMail, true);
295
        mail.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
296
        this.getRowActions().add(mail);
297
    }
298
 
299
    protected void sendMail(List<SQLRowValues> l) {
300
 
301
        String mail = "";
302
 
303
        // #endif
304
        for (SQLRowAccessor rowCli : l) {
305
            String string = rowCli.getString("EMAIL");
306
            if (string != null && string.trim().length() > 0) {
307
                mail += string + ";";
308
            }
309
        }
310
 
311
        try {
312
            EmailComposer.getInstance().compose(mail, "", "");
313
        } catch (Exception exn) {
314
            ExceptionHandler.handle(null, "Impossible de créer le courriel", exn);
315
        }
316
 
317
    }
318
 
319
    @Override
30 ilm 320
    protected String createCode() {
321
        return ELEMENT_CODE;
322
    }
323
 
324
    @Override
325
    protected List<String> getListFields() {
326
        final List<String> l = new ArrayList<String>();
140 ilm 327
        l.add("ID_COMMERCIAL");
328
        l.add("DATE");
30 ilm 329
        l.add("COMPANY");
140 ilm 330
        l.add("LOCALISATION");
331
        l.add("ID_TITRE_PERSONNEL");
332
        l.add("NAME");
30 ilm 333
        l.add("FIRSTNAME");
140 ilm 334
        l.add("PHONE");
335
        l.add("MOBILE");
336
        l.add("EMAIL");
337
        l.add("SOURCE");
338
        l.add("INFORMATION");
339
        l.add("INFOS");
340
        // l.add("REMIND_DATE");
30 ilm 341
        return l;
342
    }
343
 
344
    @Override
345
    protected List<String> getComboFields() {
346
        final List<String> l = new ArrayList<String>();
347
        l.add("COMPANY");
348
        l.add("FIRSTNAME");
349
        l.add("NAME");
350
        return l;
351
    }
352
 
353
    @Override
354
    protected List<String> getPrivateFields() {
355
        final List<String> l = new ArrayList<String>();
356
        l.add("ID_ADRESSE");
357
        return l;
358
    }
359
 
360
    @Override
125 ilm 361
    public ListMap<String, String> getShowAs() {
362
        return ListMap.singleton(null, getComboFields());
30 ilm 363
    }
364
 
365
    @Override
366
    public SQLComponent createComponent() {
367
        final String groupId = this.getCode() + ".default";
368
        final Group group = GlobalMapper.getInstance().getGroup(groupId);
369
        if (group == null) {
370
            throw new IllegalStateException("No group found for id " + groupId);
371
        }
87 ilm 372
        return createComponent(group);
373
    }
374
 
375
    protected SQLComponent createComponent(final Group group) {
30 ilm 376
        return new LeadSQLComponent(this, group);
377
    }
378
}