OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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