OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 174
Line 16... Line 16...
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
18
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
18
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
19
import org.openconcerto.erp.core.common.ui.DeviseField;
19
import org.openconcerto.erp.core.common.ui.DeviseField;
20
import org.openconcerto.erp.core.customerrelationship.customer.element.RelanceSQLElement;
20
import org.openconcerto.erp.core.customerrelationship.customer.element.RelanceSQLElement;
-
 
21
import org.openconcerto.erp.core.customerrelationship.mail.EmailTemplate;
-
 
22
import org.openconcerto.erp.core.customerrelationship.mail.ValueListener;
21
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
23
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
-
 
24
import org.openconcerto.erp.core.finance.payment.element.EncaisserMontantSQLElement;
-
 
25
import org.openconcerto.erp.core.sales.invoice.action.ImportReglementSage;
22
import org.openconcerto.erp.core.sales.invoice.report.MailRelanceCreator;
26
import org.openconcerto.erp.core.sales.invoice.report.MailRelanceCreator;
23
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
27
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
24
import org.openconcerto.erp.rights.ComptaUserRight;
28
import org.openconcerto.erp.rights.ComptaUserRight;
25
import org.openconcerto.sql.Configuration;
29
import org.openconcerto.sql.Configuration;
26
import org.openconcerto.sql.element.BaseSQLComponent;
30
import org.openconcerto.sql.element.BaseSQLComponent;
27
import org.openconcerto.sql.element.SQLComponent;
31
import org.openconcerto.sql.element.SQLComponent;
28
import org.openconcerto.sql.element.SQLElement;
32
import org.openconcerto.sql.element.SQLElement;
-
 
33
import org.openconcerto.sql.model.ConnectionHandlerNoSetup;
29
import org.openconcerto.sql.model.FieldPath;
34
import org.openconcerto.sql.model.FieldPath;
30
import org.openconcerto.sql.model.SQLBase;
35
import org.openconcerto.sql.model.SQLBase;
-
 
36
import org.openconcerto.sql.model.SQLDataSource;
31
import org.openconcerto.sql.model.SQLField;
37
import org.openconcerto.sql.model.SQLField;
32
import org.openconcerto.sql.model.SQLRow;
38
import org.openconcerto.sql.model.SQLRow;
33
import org.openconcerto.sql.model.SQLRowAccessor;
39
import org.openconcerto.sql.model.SQLRowAccessor;
-
 
40
import org.openconcerto.sql.model.SQLRowListRSH;
34
import org.openconcerto.sql.model.SQLRowValues;
41
import org.openconcerto.sql.model.SQLRowValues;
-
 
42
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
35
import org.openconcerto.sql.model.SQLTable;
43
import org.openconcerto.sql.model.SQLTable;
-
 
44
import org.openconcerto.sql.model.Where;
-
 
45
import org.openconcerto.sql.model.graph.Link;
36
import org.openconcerto.sql.model.graph.Path;
46
import org.openconcerto.sql.model.graph.Path;
37
import org.openconcerto.sql.model.graph.PathBuilder;
47
import org.openconcerto.sql.model.graph.PathBuilder;
38
import org.openconcerto.sql.request.ListSQLRequest;
48
import org.openconcerto.sql.request.ListSQLRequest;
39
import org.openconcerto.sql.sqlobject.ElementComboBox;
49
import org.openconcerto.sql.sqlobject.ElementComboBox;
40
import org.openconcerto.sql.users.rights.UserRightsManager;
50
import org.openconcerto.sql.users.rights.UserRightsManager;
-
 
51
import org.openconcerto.sql.utils.SQLUtils;
41
import org.openconcerto.sql.view.EditFrame;
52
import org.openconcerto.sql.view.EditFrame;
42
import org.openconcerto.sql.view.EditPanel.EditMode;
53
import org.openconcerto.sql.view.EditPanel.EditMode;
43
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
54
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
44
import org.openconcerto.sql.view.list.IListe;
55
import org.openconcerto.sql.view.list.IListe;
45
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
56
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
Line 47... Line 58...
47
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
58
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
48
import org.openconcerto.sql.view.list.SQLTableModelSource;
59
import org.openconcerto.sql.view.list.SQLTableModelSource;
49
import org.openconcerto.ui.DefaultGridBagConstraints;
60
import org.openconcerto.ui.DefaultGridBagConstraints;
50
import org.openconcerto.ui.EmailComposer;
61
import org.openconcerto.ui.EmailComposer;
51
import org.openconcerto.ui.JDate;
62
import org.openconcerto.ui.JDate;
-
 
63
import org.openconcerto.ui.SwingThreadUtils;
52
import org.openconcerto.utils.CollectionUtils;
64
import org.openconcerto.utils.CollectionUtils;
53
import org.openconcerto.utils.ExceptionHandler;
65
import org.openconcerto.utils.ExceptionHandler;
54
import org.openconcerto.utils.GestionDevise;
66
import org.openconcerto.utils.GestionDevise;
55
import org.openconcerto.utils.ListMap;
67
import org.openconcerto.utils.ListMap;
56
 
68
 
-
 
69
import java.awt.Component;
-
 
70
import java.awt.FileDialog;
-
 
71
import java.awt.Frame;
57
import java.awt.GridBagConstraints;
72
import java.awt.GridBagConstraints;
58
import java.awt.GridBagLayout;
73
import java.awt.GridBagLayout;
59
import java.awt.event.ActionEvent;
74
import java.awt.event.ActionEvent;
60
import java.io.File;
75
import java.io.File;
61
import java.io.IOException;
76
import java.io.IOException;
62
import java.sql.SQLException;
77
import java.sql.SQLException;
63
import java.text.SimpleDateFormat;
78
import java.text.SimpleDateFormat;
64
import java.util.ArrayList;
79
import java.util.ArrayList;
-
 
80
import java.util.Calendar;
65
import java.util.Date;
81
import java.util.Date;
66
import java.util.List;
82
import java.util.List;
67
import java.util.Locale;
83
import java.util.Locale;
68
import java.util.Set;
84
import java.util.Set;
69
 
85
 
70
import javax.swing.AbstractAction;
86
import javax.swing.AbstractAction;
71
import javax.swing.JLabel;
87
import javax.swing.JLabel;
72
import javax.swing.JOptionPane;
88
import javax.swing.JOptionPane;
73
import javax.swing.JTextField;
89
import javax.swing.JTextField;
74
import javax.swing.SwingUtilities;
-
 
75
 
90
 
76
public class EcheanceClientSQLElement extends ComptaSQLConfElement {
91
public class EcheanceClientSQLElement extends ComptaSQLConfElement {
77
 
92
 
78
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH);
93
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH);
79
 
94
 
Line 109... Line 124...
109
            action.setPredicate(IListeEvent.getSingleSelectionPredicate());
124
            action.setPredicate(IListeEvent.getSingleSelectionPredicate());
110
            getRowActions().add(action);
125
            getRowActions().add(action);
111
        }
126
        }
112
 
127
 
113
        {
128
        {
114
            PredicateRowAction action = new PredicateRowAction(new AbstractAction("Envoyer un mail") {
129
            PredicateRowAction action = new PredicateRowAction(new AbstractAction("Envoyer un mail de relance") {
115
 
130
 
116
                @Override
131
                @Override
117
                public void actionPerformed(ActionEvent arg0) {
132
                public void actionPerformed(ActionEvent e) {
-
 
133
                    final IListe iListe = IListe.get(e);
-
 
134
                    if (iListe.getSelectedRows().isEmpty()) {
-
 
135
                        int result = JOptionPane.showConfirmDialog(iListe,
-
 
136
                                "Souhaitez vous envoyer un email de relance pour toutes les factures\ndont l'échéance est dépassée?\nLes relances ne seront pas envoyées si les factures ont déjà  une relance de moins d'un mois. ",
-
 
137
                                "Relance automatique", JOptionPane.YES_NO_OPTION);
-
 
138
                        if (result == JOptionPane.YES_OPTION) {
-
 
139
                            EmailTemplate.askTemplate(iListe, getTable().getDBRoot(), new ValueListener() {
118
 
140
 
-
 
141
                                @Override
119
                    SQLRow row = IListe.get(arg0).fetchSelectedRow();
142
                                public void valueSelected(Object value) {
-
 
143
                                    final Thread t = new Thread(new Runnable() {
120
                    sendMail(row);
144
                                        @Override
-
 
145
                                        public void run() {
-
 
146
                                            try {
-
 
147
                                                EmailTemplate template = (EmailTemplate) value;
-
 
148
                                                final SQLTable table = getTable();
-
 
149
                                                final SQLRowValues v = new SQLRowValues(table);
-
 
150
                                                v.putNulls(table.getFieldsName());
-
 
151
                                                Where w = new Where(table.getField("REGLE"), "=", false);
-
 
152
                                                w = w.and(new Where(table.getField("REG_COMPTA"), "=", false));
-
 
153
                                                final Calendar c = Calendar.getInstance();
-
 
154
                                                c.add(Calendar.MONTH, -1);
-
 
155
                                                w = w.and(new Where(table.getField("DATE_LAST_RELANCE"), ">", c.getTime()));
-
 
156
                                                w = w.and(new Where(table.getField("DATE"), "<=", Calendar.getInstance().getTime()));
-
 
157
                                                List<SQLRowValues> rowValues = SQLRowValuesListFetcher.create(v).fetch(w);
-
 
158
                                                if (rowValues.isEmpty()) {
-
 
159
                                                    JOptionPane.showMessageDialog(iListe, "Aucune relance à  envoyer.");
-
 
160
                                                } else {
-
 
161
                                                    for (SQLRowValues row : rowValues) {
-
 
162
                                                        sendMail(row.asRow(), template);
121
                }
163
                                                    }
-
 
164
                                                }
-
 
165
                                            } catch (Exception e) {
-
 
166
                                                ExceptionHandler.handle("erreur lors de l'envoi", e);
-
 
167
                                            }
-
 
168
 
-
 
169
                                        }
-
 
170
                                    });
-
 
171
                                    t.setDaemon(true);
-
 
172
                                    t.setName("email relance");
-
 
173
                                    t.start();
-
 
174
 
-
 
175
                                }
-
 
176
                            });
-
 
177
 
-
 
178
                        }
-
 
179
 
-
 
180
                    } else {
-
 
181
                        final SQLTable primaryTable = iListe.getRequest().getPrimaryTable();
-
 
182
                        final List<Integer> selectedIDs = iListe.getSelection().getSelectedIDs();
-
 
183
                        EmailTemplate.askTemplate(iListe, getTable().getDBRoot(), new ValueListener() {
-
 
184
 
-
 
185
                            @Override
-
 
186
                            public void valueSelected(Object value) {
-
 
187
                                final Thread t = new Thread(new Runnable() {
-
 
188
                                    @Override
-
 
189
                                    public void run() {
-
 
190
                                        try {
-
 
191
                                            EmailTemplate template = (EmailTemplate) value;
-
 
192
                                            final List<SQLRow> rows = SQLRowListRSH.fetch(primaryTable, selectedIDs);
-
 
193
                                            for (SQLRow row : rows) {
-
 
194
                                                sendMail(row, template);
-
 
195
                                            }
-
 
196
                                        } catch (Exception e) {
-
 
197
                                            ExceptionHandler.handle("erreur lors de l'envoi", e);
-
 
198
                                        }
-
 
199
 
-
 
200
                                    }
-
 
201
                                });
-
 
202
                                t.setDaemon(true);
-
 
203
                                t.setName("email relance selection");
-
 
204
                                t.start();
-
 
205
 
-
 
206
                            }
-
 
207
                        });
-
 
208
 
-
 
209
                    }
-
 
210
                }
122
            }, false);
211
            }, true);
123
            action.setPredicate(IListeEvent.getSingleSelectionPredicate());
212
            action.setPredicate(IListeEvent.createTotalRowCountPredicate(0, Integer.MAX_VALUE));
124
            getRowActions().add(action);
213
            getRowActions().add(action);
125
        }
214
        }
126
 
215
 
127
        if (UserRightsManager.getCurrentUserRights().haveRight(ComptaUserRight.MENU)) {
216
        if (UserRightsManager.getCurrentUserRights().haveRight(ComptaUserRight.MENU)) {
128
            RowAction actionCancel = new RowAction(new AbstractAction("Annuler la régularisation en comptabilité") {
217
            RowAction actionCancel = new RowAction(new AbstractAction("Annuler la régularisation en comptabilité") {
Line 141... Line 230...
141
                            e1.printStackTrace();
230
                            e1.printStackTrace();
142
                        }
231
                        }
143
                    }
232
                    }
144
                }
233
                }
145
            }, false) {
234
            }, false) {
-
 
235
 
146
                @Override
236
                @Override
147
                public boolean enabledFor(List<SQLRowValues> selection) {
237
                public boolean enabledFor(List<SQLRowValues> selection) {
148
                    if (selection != null && selection.size() == 1) {
238
                    if (selection != null && selection.size() == 1) {
149
                        SQLRowAccessor row = selection.get(0);
239
                        SQLRowAccessor row = selection.get(0);
150
                        return row.getBoolean("REG_COMPTA");
240
                        return row.getBoolean("REG_COMPTA");
Line 182... Line 272...
182
                            e1.printStackTrace();
272
                            e1.printStackTrace();
183
                        }
273
                        }
184
                    }
274
                    }
185
                }
275
                }
186
            }, false) {
276
            }, false) {
-
 
277
 
187
                @Override
278
                @Override
188
                public boolean enabledFor(List<SQLRowValues> selection) {
279
                public boolean enabledFor(List<SQLRowValues> selection) {
189
                    if (selection != null && selection.size() == 1) {
280
                    if (selection != null && selection.size() == 1) {
190
                        SQLRowAccessor row = selection.get(0);
281
                        SQLRowAccessor row = selection.get(0);
191
                        return !row.getBoolean("REG_COMPTA");
282
                        return !row.getBoolean("REG_COMPTA");
192
                    } else {
283
                    } else {
193
                        return true;
284
                        return true;
194
                    }
285
                    }
195
                }
286
                }
-
 
287
 
196
            };
288
            };
197
            getRowActions().add(actionRegul);
289
            getRowActions().add(actionRegul);
198
        }
290
        }
199
 
291
 
-
 
292
        RowAction actionImport = new RowAction(new AbstractAction("Importer") {
-
 
293
 
-
 
294
            public void actionPerformed(ActionEvent e) {
-
 
295
 
-
 
296
                final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
-
 
297
                final FileDialog fd = new FileDialog(frame, "Import XML des réglements Sage", FileDialog.LOAD);
-
 
298
                if (fd.getFile() != null) {
-
 
299
                    try {
-
 
300
                        SQLUtils.executeAtomic(getTable().getDBSystemRoot().getDataSource(), new ConnectionHandlerNoSetup<Object, IOException>() {
-
 
301
                            @Override
-
 
302
                            public Object handle(final SQLDataSource ds) throws SQLException, IOException {
-
 
303
 
-
 
304
                                ImportReglementSage sageImporter = new ImportReglementSage(getDirectory().getElement(EncaisserMontantSQLElement.class));
-
 
305
                                try {
-
 
306
                                    sageImporter.importFromFile(new File(fd.getDirectory(), fd.getFile()));
-
 
307
                                } catch (Exception e) {
-
 
308
                                    e.printStackTrace();
-
 
309
                                    new SQLException(e);
-
 
310
                                }
-
 
311
                                return null;
-
 
312
                            }
-
 
313
 
-
 
314
                        });
-
 
315
                        JOptionPane.showMessageDialog(null, "Import des paiements terminés!");
-
 
316
                    } catch (IOException exn) {
-
 
317
                        ExceptionHandler.handle(frame, "Erreur lors de la lecture du fichier", exn);
-
 
318
                    } catch (SQLException exn) {
-
 
319
                        ExceptionHandler.handle(frame, "Erreur lors de l'insertion des paiements dans la base", exn);
200
    }
320
                    }
201
 
321
 
-
 
322
                }
-
 
323
                fd.setVisible(true);
-
 
324
            }
-
 
325
        }, true) {
-
 
326
            @Override
-
 
327
            public boolean enabledFor(List<SQLRowValues> selection) {
-
 
328
                return true;
-
 
329
            }
-
 
330
        };
-
 
331
        getRowActions().add(actionImport);
202
 
332
 
203
    private void sendMail(final SQLRow row) {
333
    }
204
 
334
 
205
        SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
-
 
206
 
335
 
207
        if (row != null) {
336
    private void sendMail(final SQLRow row, EmailTemplate template) throws Exception {
-
 
337
 
-
 
338
        SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
208
 
339
 
209
            int idMvtSource = MouvementSQLElement.getSourceId(row.getInt("ID_MOUVEMENT"));
340
        int idMvtSource = MouvementSQLElement.getSourceId(row.getInt("ID_MOUVEMENT"));
210
            SQLRow rowMvtSource = base.getTable("MOUVEMENT").getRow(idMvtSource);
341
        SQLRow rowMvtSource = base.getTable("MOUVEMENT").getRow(idMvtSource);
211
 
342
 
212
            if (!rowMvtSource.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
343
        if (!rowMvtSource.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
213
                // this.relancer.setEnabled(false);
-
 
214
                return;
344
            return;
215
            }
345
        }
216
            int idFact = rowMvtSource.getInt("IDSOURCE");
346
        int idFact = rowMvtSource.getInt("IDSOURCE");
217
            SQLRow rowFacture = base.getTable("SAISIE_VENTE_FACTURE").getRow(idFact);
347
        SQLRow rowFacture = base.getTable("SAISIE_VENTE_FACTURE").getRow(idFact);
218
 
348
 
Line 240... Line 370...
240
                    mail = rowCli.getString("MAIL");
370
                mail = rowCli.getString("MAIL");
241
                }
371
            }
242
            }
372
        }
243
            final String adresseMail = mail;
373
        final String adresseMail = mail;
244
 
374
 
245
            MailRelanceCreator creator = new MailRelanceCreator();
375
        MailRelanceCreator creator = new MailRelanceCreator(template, row);
246
            final String references = creator.getObject(row);
376
        final String references = creator.getObject();
247
            final String text = creator.getValue(row);
377
        final String text = creator.getValue();
248
 
-
 
249
            final Thread t = new Thread() {
-
 
250
                @Override
-
 
251
                public void run() {
-
 
252
 
378
 
253
                    final File f;
379
        final File f;
254
                    try {
-
 
-
 
380
 
255
                        f = sheet.getOrCreatePDFDocumentFile(true);
381
        f = sheet.getOrCreatePDFDocumentFile(true);
256
 
382
 
257
                        EmailComposer.getInstance().compose(adresseMail, references, text, f.getAbsoluteFile());
383
        EmailComposer.getInstance().compose(adresseMail, references, text, f.getAbsoluteFile());
258
 
384
 
259
                        // Création d'une relance
385
        // Création d'une relance
Line 264... Line 390...
264
                        rowValsR.put("ID_CLIENT", row.getForeignID("ID_CLIENT"));
390
        rowValsR.put("ID_CLIENT", row.getForeignID("ID_CLIENT"));
265
                        rowValsR.put("ID_SAISIE_VENTE_FACTURE", row.getForeignID("ID_SAISIE_VENTE_FACTURE"));
391
        rowValsR.put("ID_SAISIE_VENTE_FACTURE", row.getForeignID("ID_SAISIE_VENTE_FACTURE"));
266
                        rowValsR.put("MONTANT", row.getObject("MONTANT"));
392
        rowValsR.put("MONTANT", row.getObject("MONTANT"));
267
                        rowValsR.put("INFOS", "Email");
393
        rowValsR.put("INFOS", "Email");
268
                        rowValsR.put("ID_ECHEANCE_CLIENT", row.getID());
394
        rowValsR.put("ID_ECHEANCE_CLIENT", row.getID());
269
                        try {
-
 
-
 
395
 
270
                            rowValsR.insert();
396
        rowValsR.insert();
271
 
397
 
272
                            SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
398
        SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
273
                            SQLRowValues rowVals = new SQLRowValues(tableNum);
399
        SQLRowValues rowVals = new SQLRowValues(tableNum);
274
                            int val = tableNum.getRow(2).getInt("RELANCE_START");
400
        int val = tableNum.getRow(2).getInt("RELANCE_START");
Line 285... Line 411...
285
                            rowValsEch.put("NOMBRE_RELANCE", nbRelance);
411
        rowValsEch.put("NOMBRE_RELANCE", nbRelance);
286
                            rowValsEch.put("DATE_LAST_RELANCE", new Date());
412
        rowValsEch.put("DATE_LAST_RELANCE", new Date());
287
 
413
 
288
                            rowValsEch.update(row.getID());
414
        rowValsEch.update(row.getID());
289
 
415
 
290
                        } catch (SQLException e) {
-
 
291
 
-
 
292
                            e.printStackTrace();
-
 
293
                        }
-
 
294
                    } catch (IOException exn) {
-
 
295
                        exn.printStackTrace();
-
 
296
                    } catch (InterruptedException exn) {
-
 
297
                        exn.printStackTrace();
-
 
298
                    } catch (Exception e1) {
-
 
299
                        // TODO Auto-generated catch block
-
 
300
                        e1.printStackTrace();
-
 
301
                    }
-
 
302
 
-
 
303
                }
-
 
304
            };
-
 
305
 
-
 
306
            t.start();
-
 
307
        }
-
 
308
    }
416
    }
309
 
417
 
310
    @Override
418
    @Override
311
    public ListMap<String, String> getShowAs() {
419
    public ListMap<String, String> getShowAs() {
312
        ListMap<String, String> map = new ListMap<String, String>();
420
        ListMap<String, String> map = new ListMap<String, String>();