OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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