OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Rev 174 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.sales.invoice.element;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
132 ilm 18
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
18 ilm 19
import org.openconcerto.erp.core.common.ui.DeviseField;
132 ilm 20
import org.openconcerto.erp.core.customerrelationship.customer.element.RelanceSQLElement;
73 ilm 21
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
132 ilm 22
import org.openconcerto.erp.core.sales.invoice.report.MailRelanceCreator;
23
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
73 ilm 24
import org.openconcerto.erp.rights.ComptaUserRight;
18 ilm 25
import org.openconcerto.sql.Configuration;
26
import org.openconcerto.sql.element.BaseSQLComponent;
27
import org.openconcerto.sql.element.SQLComponent;
151 ilm 28
import org.openconcerto.sql.element.SQLElement;
80 ilm 29
import org.openconcerto.sql.model.FieldPath;
73 ilm 30
import org.openconcerto.sql.model.SQLBase;
31
import org.openconcerto.sql.model.SQLField;
32
import org.openconcerto.sql.model.SQLRow;
33
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 34
import org.openconcerto.sql.model.SQLRowValues;
73 ilm 35
import org.openconcerto.sql.model.SQLTable;
80 ilm 36
import org.openconcerto.sql.model.graph.Path;
37
import org.openconcerto.sql.model.graph.PathBuilder;
18 ilm 38
import org.openconcerto.sql.request.ListSQLRequest;
39
import org.openconcerto.sql.sqlobject.ElementComboBox;
73 ilm 40
import org.openconcerto.sql.users.rights.UserRightsManager;
151 ilm 41
import org.openconcerto.sql.view.EditFrame;
42
import org.openconcerto.sql.view.EditPanel.EditMode;
80 ilm 43
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
73 ilm 44
import org.openconcerto.sql.view.list.IListe;
45
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
46
import org.openconcerto.sql.view.list.RowAction;
47
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
142 ilm 48
import org.openconcerto.sql.view.list.SQLTableModelSource;
18 ilm 49
import org.openconcerto.ui.DefaultGridBagConstraints;
73 ilm 50
import org.openconcerto.ui.EmailComposer;
18 ilm 51
import org.openconcerto.ui.JDate;
80 ilm 52
import org.openconcerto.utils.CollectionUtils;
132 ilm 53
import org.openconcerto.utils.ExceptionHandler;
73 ilm 54
import org.openconcerto.utils.GestionDevise;
132 ilm 55
import org.openconcerto.utils.ListMap;
18 ilm 56
 
57
import java.awt.GridBagConstraints;
58
import java.awt.GridBagLayout;
73 ilm 59
import java.awt.event.ActionEvent;
132 ilm 60
import java.io.File;
73 ilm 61
import java.io.IOException;
62
import java.sql.SQLException;
63
import java.text.SimpleDateFormat;
18 ilm 64
import java.util.ArrayList;
73 ilm 65
import java.util.Date;
18 ilm 66
import java.util.List;
73 ilm 67
import java.util.Locale;
68
import java.util.Set;
18 ilm 69
 
73 ilm 70
import javax.swing.AbstractAction;
18 ilm 71
import javax.swing.JLabel;
73 ilm 72
import javax.swing.JOptionPane;
18 ilm 73
import javax.swing.JTextField;
132 ilm 74
import javax.swing.SwingUtilities;
18 ilm 75
 
76
public class EcheanceClientSQLElement extends ComptaSQLConfElement {
77
 
73 ilm 78
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH);
79
 
18 ilm 80
    public EcheanceClientSQLElement() {
81
        super("ECHEANCE_CLIENT", "une échéance client", "échéances clients");
73 ilm 82
        {
151 ilm 83
            PredicateRowAction action = new PredicateRowAction(new AbstractAction("Détails client") {
84
 
85
                @Override
86
                public void actionPerformed(ActionEvent arg0) {
87
 
88
                    SQLElement eltClient = Configuration.getInstance().getDirectory().getElement(((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("CLIENT"));
89
 
90
                    EditFrame edit = new EditFrame(eltClient, EditMode.MODIFICATION);
91
 
92
                    edit.selectionId(IListe.get(arg0).getSelectedRow().asRow().getInt("ID_CLIENT"));
93
                    edit.setVisible(true);
94
                }
95
            }, false);
96
            action.setPredicate(IListeEvent.getSingleSelectionPredicate());
97
            getRowActions().add(action);
98
        }
99
        {
73 ilm 100
            PredicateRowAction action = new PredicateRowAction(new AbstractAction("Voir la source") {
101
 
102
                @Override
103
                public void actionPerformed(ActionEvent arg0) {
104
 
80 ilm 105
                    SQLRow row = IListe.get(arg0).fetchSelectedRow();
73 ilm 106
                    MouvementSQLElement.showSource(row.getInt("ID_MOUVEMENT"));
107
                }
108
            }, false);
109
            action.setPredicate(IListeEvent.getSingleSelectionPredicate());
110
            getRowActions().add(action);
111
        }
112
 
113
        {
114
            PredicateRowAction action = new PredicateRowAction(new AbstractAction("Envoyer un mail") {
115
 
116
                @Override
117
                public void actionPerformed(ActionEvent arg0) {
118
 
80 ilm 119
                    SQLRow row = IListe.get(arg0).fetchSelectedRow();
73 ilm 120
                    sendMail(row);
121
                }
122
            }, false);
123
            action.setPredicate(IListeEvent.getSingleSelectionPredicate());
124
            getRowActions().add(action);
125
        }
126
 
127
        if (UserRightsManager.getCurrentUserRights().haveRight(ComptaUserRight.MENU)) {
128
            RowAction actionCancel = new RowAction(new AbstractAction("Annuler la régularisation en comptabilité") {
129
 
130
                public void actionPerformed(ActionEvent e) {
131
 
132
                    int answer = JOptionPane.showConfirmDialog(null, "Etes vous sûr de vouloir annuler la régularisation ?");
133
                    if (answer == JOptionPane.YES_OPTION) {
80 ilm 134
                        SQLRow row = IListe.get(e).getSelectedRow().asRow();
73 ilm 135
                        SQLRowValues rowVals = row.createEmptyUpdateRow();
136
                        rowVals.put("REG_COMPTA", Boolean.FALSE);
137
                        try {
138
                            rowVals.commit();
139
                        } catch (SQLException e1) {
140
                            // TODO Auto-generated catch block
141
                            e1.printStackTrace();
142
                        }
143
                    }
144
                }
145
            }, false) {
146
                @Override
93 ilm 147
                public boolean enabledFor(List<SQLRowValues> selection) {
73 ilm 148
                    if (selection != null && selection.size() == 1) {
149
                        SQLRowAccessor row = selection.get(0);
150
                        return row.getBoolean("REG_COMPTA");
151
                    } else {
152
                        return true;
153
                    }
154
                }
155
            };
156
            getRowActions().add(actionCancel);
157
 
158
            RowAction actionRegul = new RowAction(new AbstractAction("Régularisation en comptabilité") {
159
 
160
                public void actionPerformed(ActionEvent e) {
161
 
80 ilm 162
                    SQLRow row = IListe.get(e).fetchSelectedRow();
73 ilm 163
                    String price = GestionDevise.currencyToString(row.getLong("MONTANT"));
164
                    SQLRow rowClient = row.getForeignRow("ID_CLIENT");
165
                    String nomClient = rowClient.getString("NOM");
166
                    String piece = "";
167
                    SQLRow rowMvt = row.getForeignRow("ID_MOUVEMENT");
168
                    if (rowMvt != null) {
169
                        SQLRow rowPiece = rowMvt.getForeignRow("ID_PIECE");
170
                        piece = rowPiece.getString("NOM");
171
                    }
132 ilm 172
                    int answer = JOptionPane.showConfirmDialog(null,
173
                            "Etes vous sûr de vouloir régulariser l'échéance de " + nomClient + " d'un montant de " + price + "€ avec une saisie au kilometre?\nNom de la piéce : " + piece + ".");
73 ilm 174
                    if (answer == JOptionPane.YES_OPTION) {
175
 
176
                        SQLRowValues rowVals = row.createEmptyUpdateRow();
177
                        rowVals.put("REG_COMPTA", Boolean.TRUE);
178
                        try {
179
                            rowVals.commit();
180
                        } catch (SQLException e1) {
181
                            // TODO Auto-generated catch block
182
                            e1.printStackTrace();
183
                        }
184
                    }
185
                }
186
            }, false) {
187
                @Override
93 ilm 188
                public boolean enabledFor(List<SQLRowValues> selection) {
73 ilm 189
                    if (selection != null && selection.size() == 1) {
190
                        SQLRowAccessor row = selection.get(0);
191
                        return !row.getBoolean("REG_COMPTA");
192
                    } else {
193
                        return true;
194
                    }
195
                }
196
            };
197
            getRowActions().add(actionRegul);
198
        }
199
 
18 ilm 200
    }
201
 
80 ilm 202
 
132 ilm 203
    private void sendMail(final SQLRow row) {
73 ilm 204
 
205
        SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
206
 
207
        if (row != null) {
132 ilm 208
 
73 ilm 209
            int idMvtSource = MouvementSQLElement.getSourceId(row.getInt("ID_MOUVEMENT"));
210
            SQLRow rowMvtSource = base.getTable("MOUVEMENT").getRow(idMvtSource);
211
 
212
            if (!rowMvtSource.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
213
                // this.relancer.setEnabled(false);
214
                return;
215
            }
216
            int idFact = rowMvtSource.getInt("IDSOURCE");
217
            SQLRow rowFacture = base.getTable("SAISIE_VENTE_FACTURE").getRow(idFact);
218
 
142 ilm 219
            final VenteFactureXmlSheet sheet = new VenteFactureXmlSheet(rowFacture);
132 ilm 220
 
73 ilm 221
            Set<SQLField> setContact = null;
222
            SQLTable tableContact = Configuration.getInstance().getRoot().findTable("CONTACT");
223
            setContact = row.getTable().getForeignKeys(tableContact);
224
 
225
            Set<SQLField> setClient = null;
226
            SQLTable tableClient = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("CLIENT");
227
            setClient = row.getTable().getForeignKeys(tableClient);
228
 
132 ilm 229
            // Récupération du mail du client
73 ilm 230
            String mail = "";
231
            for (SQLField field : setContact) {
232
                if (mail == null || mail.trim().length() == 0) {
233
                    mail = row.getForeignRow(field.getName()).getString("EMAIL");
234
                }
235
            }
236
 
237
            for (SQLField field : setClient) {
238
                SQLRow rowCli = row.getForeignRow(field.getName());
239
                if (mail == null || mail.trim().length() == 0) {
240
                    mail = rowCli.getString("MAIL");
241
                }
242
            }
243
            final String adresseMail = mail;
244
 
132 ilm 245
            MailRelanceCreator creator = new MailRelanceCreator();
246
            final String references = creator.getObject(row);
247
            final String text = creator.getValue(row);
248
 
73 ilm 249
            final Thread t = new Thread() {
250
                @Override
251
                public void run() {
252
 
142 ilm 253
                    final File f;
73 ilm 254
                    try {
142 ilm 255
                        f = sheet.getOrCreatePDFDocumentFile(true);
132 ilm 256
 
142 ilm 257
                        EmailComposer.getInstance().compose(adresseMail, references, text, f.getAbsoluteFile());
258
 
132 ilm 259
                        // Création d'une relance
260
                        String numero = NumerotationAutoSQLElement.getNextNumero(RelanceSQLElement.class);
261
                        SQLRowValues rowValsR = new SQLRowValues(row.getTable().getTable("RELANCE"));
262
                        rowValsR.put("DATE", new Date());
263
                        rowValsR.put("NUMERO", numero);
264
                        rowValsR.put("ID_CLIENT", row.getForeignID("ID_CLIENT"));
265
                        rowValsR.put("ID_SAISIE_VENTE_FACTURE", row.getForeignID("ID_SAISIE_VENTE_FACTURE"));
266
                        rowValsR.put("MONTANT", row.getObject("MONTANT"));
267
                        rowValsR.put("INFOS", "Email");
268
                        rowValsR.put("ID_ECHEANCE_CLIENT", row.getID());
269
                        try {
270
                            rowValsR.insert();
271
 
272
                            SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
273
                            SQLRowValues rowVals = new SQLRowValues(tableNum);
274
                            int val = tableNum.getRow(2).getInt("RELANCE_START");
275
                            val++;
276
                            rowVals.put("RELANCE_START", Integer.valueOf(val));
277
 
278
                            rowVals.update(2);
279
 
280
                            // Incrémentation du nombre de relance
281
                            int nbRelance = row.getInt("NOMBRE_RELANCE");
282
                            nbRelance++;
283
 
284
                            SQLRowValues rowValsEch = new SQLRowValues(row.getTable());
285
                            rowValsEch.put("NOMBRE_RELANCE", nbRelance);
286
                            rowValsEch.put("DATE_LAST_RELANCE", new Date());
287
 
288
                            rowValsEch.update(row.getID());
289
 
290
                        } catch (SQLException e) {
291
 
292
                            e.printStackTrace();
293
                        }
73 ilm 294
                    } catch (IOException exn) {
295
                        exn.printStackTrace();
296
                    } catch (InterruptedException exn) {
297
                        exn.printStackTrace();
142 ilm 298
                    } catch (Exception e1) {
299
                        // TODO Auto-generated catch block
300
                        e1.printStackTrace();
73 ilm 301
                    }
302
 
303
                }
304
            };
305
 
306
            t.start();
307
        }
308
    }
309
 
83 ilm 310
    @Override
132 ilm 311
    public ListMap<String, String> getShowAs() {
312
        ListMap<String, String> map = new ListMap<String, String>();
83 ilm 313
        return map;
314
    }
315
 
18 ilm 316
    protected List<String> getListFields() {
317
        final List<String> l = new ArrayList<String>();
318
 
83 ilm 319
 
320
            l.add("ID_SAISIE_VENTE_FACTURE");
321
            l.add("DATE");
322
            l.add("ID_CLIENT");
323
            l.add("MONTANT");
324
            l.add("ID_MOUVEMENT");
325
                l.add("NOMBRE_RELANCE");
326
            l.add("INFOS");
327
            l.add("DATE_LAST_RELANCE");
18 ilm 328
        return l;
329
    }
330
 
331
    protected List<String> getComboFields() {
332
        final List<String> l = new ArrayList<String>();
333
        l.add("DATE");
334
        l.add("ID_CLIENT");
335
        l.add("MONTANT");
336
        return l;
337
    }
338
 
339
    @Override
132 ilm 340
    protected void _initListRequest(ListSQLRequest req) {
341
        super._initListRequest(req);
342
        req.addToGraphToFetch("REG_COMPTA", "REGLE");
18 ilm 343
    }
344
 
345
    /*
346
     * (non-Javadoc)
347
     *
348
     * @see org.openconcerto.devis.SQLElement#getComponent()
349
     */
350
    public SQLComponent createComponent() {
351
        return new BaseSQLComponent(this) {
352
 
353
            private DeviseField montant;
354
            private JTextField nbRelance;
355
            private JDate date;
356
            private JTextField idMouvement;
357
            private ElementComboBox comboClient;
358
 
359
            public void addViews() {
360
                this.setLayout(new GridBagLayout());
361
                final GridBagConstraints c = new DefaultGridBagConstraints();
362
 
363
                this.montant = new DeviseField();
364
                this.nbRelance = new JTextField();
365
                this.date = new JDate();
366
                this.idMouvement = new JTextField();
367
                this.comboClient = new ElementComboBox();
368
 
369
                // Mouvement
370
                JLabel labelMouvement = new JLabel("Mouvement");
371
                this.add(labelMouvement, c);
372
 
373
                c.weightx = 1;
374
                c.gridx++;
375
                this.add(this.idMouvement, c);
376
 
377
                // Date
378
                JLabel labelDate = new JLabel("Date");
379
                c.gridx++;
380
                this.add(labelDate, c);
381
 
382
                c.gridx++;
383
                c.weightx = 1;
384
                this.add(this.date, c);
385
 
386
                // Client
387
                JLabel labelClient = new JLabel("Client");
388
                c.gridy++;
389
                c.gridx = 0;
390
 
391
                this.add(labelClient, c);
392
 
393
                c.gridx++;
394
                c.weightx = 1;
395
                c.gridwidth = GridBagConstraints.REMAINDER;
396
                this.add(this.comboClient, c);
397
 
398
                // libellé
399
                JLabel labelRelance = new JLabel("Nombre de relance");
400
                c.gridy++;
401
                c.gridx = 0;
402
                c.gridwidth = 1;
403
                this.add(labelRelance, c);
404
 
405
                c.gridx++;
406
                c.weightx = 1;
407
                this.add(this.nbRelance, c);
408
 
409
                // montant
410
                c.gridwidth = 1;
411
                JLabel labelMontant = new JLabel("Montant");
412
                c.gridx++;
413
                this.add(labelMontant, c);
414
 
415
                c.gridx++;
416
                c.weightx = 1;
417
                this.add(this.montant, c);
418
 
419
                this.addSQLObject(this.montant, "MONTANT");
420
                this.addRequiredSQLObject(this.date, "DATE");
421
                this.addSQLObject(this.nbRelance, "NOMBRE_RELANCE");
422
                this.addRequiredSQLObject(this.comboClient, "ID_CLIENT");
423
                this.addSQLObject(this.idMouvement, "ID_MOUVEMENT");
424
            }
425
        };
426
    }
427
 
57 ilm 428
    @Override
429
    protected String createCode() {
156 ilm 430
        return createCodeOfPackage() + ".commitment";
57 ilm 431
    }
432
 
18 ilm 433
}