OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Rev 182 | 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;
177 ilm 17
import org.openconcerto.erp.config.Gestion;
18 ilm 18
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
132 ilm 19
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
18 ilm 20
import org.openconcerto.erp.core.common.ui.DeviseField;
132 ilm 21
import org.openconcerto.erp.core.customerrelationship.customer.element.RelanceSQLElement;
174 ilm 22
import org.openconcerto.erp.core.customerrelationship.mail.EmailTemplate;
23
import org.openconcerto.erp.core.customerrelationship.mail.ValueListener;
73 ilm 24
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
174 ilm 25
import org.openconcerto.erp.core.finance.payment.element.EncaisserMontantSQLElement;
26
import org.openconcerto.erp.core.sales.invoice.action.ImportReglementSage;
132 ilm 27
import org.openconcerto.erp.core.sales.invoice.report.MailRelanceCreator;
180 ilm 28
import org.openconcerto.erp.core.sales.invoice.report.SituationCompteClientPanel;
132 ilm 29
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
73 ilm 30
import org.openconcerto.erp.rights.ComptaUserRight;
18 ilm 31
import org.openconcerto.sql.Configuration;
32
import org.openconcerto.sql.element.BaseSQLComponent;
33
import org.openconcerto.sql.element.SQLComponent;
151 ilm 34
import org.openconcerto.sql.element.SQLElement;
174 ilm 35
import org.openconcerto.sql.model.ConnectionHandlerNoSetup;
80 ilm 36
import org.openconcerto.sql.model.FieldPath;
73 ilm 37
import org.openconcerto.sql.model.SQLBase;
174 ilm 38
import org.openconcerto.sql.model.SQLDataSource;
73 ilm 39
import org.openconcerto.sql.model.SQLField;
40
import org.openconcerto.sql.model.SQLRow;
41
import org.openconcerto.sql.model.SQLRowAccessor;
174 ilm 42
import org.openconcerto.sql.model.SQLRowListRSH;
18 ilm 43
import org.openconcerto.sql.model.SQLRowValues;
174 ilm 44
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
73 ilm 45
import org.openconcerto.sql.model.SQLTable;
174 ilm 46
import org.openconcerto.sql.model.Where;
47
import org.openconcerto.sql.model.graph.Link;
80 ilm 48
import org.openconcerto.sql.model.graph.Path;
49
import org.openconcerto.sql.model.graph.PathBuilder;
18 ilm 50
import org.openconcerto.sql.request.ListSQLRequest;
51
import org.openconcerto.sql.sqlobject.ElementComboBox;
73 ilm 52
import org.openconcerto.sql.users.rights.UserRightsManager;
174 ilm 53
import org.openconcerto.sql.utils.SQLUtils;
151 ilm 54
import org.openconcerto.sql.view.EditFrame;
55
import org.openconcerto.sql.view.EditPanel.EditMode;
177 ilm 56
import org.openconcerto.sql.view.EditPanelListener;
80 ilm 57
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
73 ilm 58
import org.openconcerto.sql.view.list.IListe;
59
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
60
import org.openconcerto.sql.view.list.RowAction;
61
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
142 ilm 62
import org.openconcerto.sql.view.list.SQLTableModelSource;
18 ilm 63
import org.openconcerto.ui.DefaultGridBagConstraints;
73 ilm 64
import org.openconcerto.ui.EmailComposer;
18 ilm 65
import org.openconcerto.ui.JDate;
180 ilm 66
import org.openconcerto.ui.PanelFrame;
174 ilm 67
import org.openconcerto.ui.SwingThreadUtils;
80 ilm 68
import org.openconcerto.utils.CollectionUtils;
132 ilm 69
import org.openconcerto.utils.ExceptionHandler;
73 ilm 70
import org.openconcerto.utils.GestionDevise;
132 ilm 71
import org.openconcerto.utils.ListMap;
18 ilm 72
 
174 ilm 73
import java.awt.Component;
74
import java.awt.FileDialog;
75
import java.awt.Frame;
18 ilm 76
import java.awt.GridBagConstraints;
77
import java.awt.GridBagLayout;
73 ilm 78
import java.awt.event.ActionEvent;
132 ilm 79
import java.io.File;
73 ilm 80
import java.io.IOException;
81
import java.sql.SQLException;
82
import java.text.SimpleDateFormat;
18 ilm 83
import java.util.ArrayList;
174 ilm 84
import java.util.Calendar;
73 ilm 85
import java.util.Date;
18 ilm 86
import java.util.List;
73 ilm 87
import java.util.Locale;
88
import java.util.Set;
18 ilm 89
 
73 ilm 90
import javax.swing.AbstractAction;
18 ilm 91
import javax.swing.JLabel;
73 ilm 92
import javax.swing.JOptionPane;
18 ilm 93
import javax.swing.JTextField;
94
 
95
public class EcheanceClientSQLElement extends ComptaSQLConfElement {
96
 
73 ilm 97
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH);
98
 
18 ilm 99
    public EcheanceClientSQLElement() {
100
        super("ECHEANCE_CLIENT", "une échéance client", "échéances clients");
73 ilm 101
        {
151 ilm 102
            PredicateRowAction action = new PredicateRowAction(new AbstractAction("Détails client") {
103
 
104
                @Override
105
                public void actionPerformed(ActionEvent arg0) {
106
 
107
                    SQLElement eltClient = Configuration.getInstance().getDirectory().getElement(((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("CLIENT"));
108
 
109
                    EditFrame edit = new EditFrame(eltClient, EditMode.MODIFICATION);
110
 
111
                    edit.selectionId(IListe.get(arg0).getSelectedRow().asRow().getInt("ID_CLIENT"));
112
                    edit.setVisible(true);
113
                }
114
            }, false);
115
            action.setPredicate(IListeEvent.getSingleSelectionPredicate());
116
            getRowActions().add(action);
117
        }
118
        {
73 ilm 119
            PredicateRowAction action = new PredicateRowAction(new AbstractAction("Voir la source") {
120
 
121
                @Override
122
                public void actionPerformed(ActionEvent arg0) {
123
 
80 ilm 124
                    SQLRow row = IListe.get(arg0).fetchSelectedRow();
73 ilm 125
                    MouvementSQLElement.showSource(row.getInt("ID_MOUVEMENT"));
126
                }
127
            }, false);
128
            action.setPredicate(IListeEvent.getSingleSelectionPredicate());
129
            getRowActions().add(action);
130
        }
131
 
132
        {
174 ilm 133
            PredicateRowAction action = new PredicateRowAction(new AbstractAction("Envoyer un mail de relance") {
73 ilm 134
 
135
                @Override
174 ilm 136
                public void actionPerformed(ActionEvent e) {
137
                    final IListe iListe = IListe.get(e);
138
                    if (iListe.getSelectedRows().isEmpty()) {
139
                        int result = JOptionPane.showConfirmDialog(iListe,
140
                                "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. ",
141
                                "Relance automatique", JOptionPane.YES_NO_OPTION);
142
                        if (result == JOptionPane.YES_OPTION) {
143
                            EmailTemplate.askTemplate(iListe, getTable().getDBRoot(), new ValueListener() {
73 ilm 144
 
174 ilm 145
                                @Override
146
                                public void valueSelected(Object value) {
147
                                    final Thread t = new Thread(new Runnable() {
148
                                        @Override
149
                                        public void run() {
150
                                            try {
151
                                                EmailTemplate template = (EmailTemplate) value;
152
                                                final SQLTable table = getTable();
153
                                                final SQLRowValues v = new SQLRowValues(table);
154
                                                v.putNulls(table.getFieldsName());
155
                                                Where w = new Where(table.getField("REGLE"), "=", false);
156
                                                w = w.and(new Where(table.getField("REG_COMPTA"), "=", false));
157
                                                final Calendar c = Calendar.getInstance();
158
                                                c.add(Calendar.MONTH, -1);
159
                                                w = w.and(new Where(table.getField("DATE_LAST_RELANCE"), ">", c.getTime()));
160
                                                w = w.and(new Where(table.getField("DATE"), "<=", Calendar.getInstance().getTime()));
161
                                                List<SQLRowValues> rowValues = SQLRowValuesListFetcher.create(v).fetch(w);
162
                                                if (rowValues.isEmpty()) {
163
                                                    JOptionPane.showMessageDialog(iListe, "Aucune relance à  envoyer.");
164
                                                } else {
165
                                                    for (SQLRowValues row : rowValues) {
166
                                                        sendMail(row.asRow(), template);
167
                                                    }
168
                                                }
169
                                            } catch (Exception e) {
170
                                                ExceptionHandler.handle("erreur lors de l'envoi", e);
171
                                            }
172
 
173
                                        }
174
                                    });
175
                                    t.setDaemon(true);
176
                                    t.setName("email relance");
177
                                    t.start();
178
 
179
                                }
180
                            });
181
 
182
                        }
183
 
184
                    } else {
185
                        final SQLTable primaryTable = iListe.getRequest().getPrimaryTable();
186
                        final List<Integer> selectedIDs = iListe.getSelection().getSelectedIDs();
187
                        EmailTemplate.askTemplate(iListe, getTable().getDBRoot(), new ValueListener() {
188
 
189
                            @Override
190
                            public void valueSelected(Object value) {
191
                                final Thread t = new Thread(new Runnable() {
192
                                    @Override
193
                                    public void run() {
194
                                        try {
195
                                            EmailTemplate template = (EmailTemplate) value;
196
                                            final List<SQLRow> rows = SQLRowListRSH.fetch(primaryTable, selectedIDs);
197
                                            for (SQLRow row : rows) {
198
                                                sendMail(row, template);
199
                                            }
200
                                        } catch (Exception e) {
201
                                            ExceptionHandler.handle("erreur lors de l'envoi", e);
202
                                        }
203
 
204
                                    }
205
                                });
206
                                t.setDaemon(true);
207
                                t.setName("email relance selection");
208
                                t.start();
209
 
210
                            }
211
                        });
212
 
213
                    }
73 ilm 214
                }
174 ilm 215
            }, true);
216
            action.setPredicate(IListeEvent.createTotalRowCountPredicate(0, Integer.MAX_VALUE));
73 ilm 217
            getRowActions().add(action);
180 ilm 218
 
219
            PredicateRowAction actionSituation = new PredicateRowAction(new AbstractAction("Générer une situationde compte") {
220
 
221
                @Override
222
                public void actionPerformed(ActionEvent e) {
223
                    PanelFrame frame = new PanelFrame(new SituationCompteClientPanel(ComptaPropsConfiguration.getInstanceCompta()), "Situation client");
224
                    frame.setVisible(true);
225
                }
226
            }, true);
227
            actionSituation.setPredicate(IListeEvent.createSelectionCountPredicate(0, Integer.MAX_VALUE));
228
            getRowActions().add(actionSituation);
73 ilm 229
        }
230
 
231
        if (UserRightsManager.getCurrentUserRights().haveRight(ComptaUserRight.MENU)) {
232
            RowAction actionCancel = new RowAction(new AbstractAction("Annuler la régularisation en comptabilité") {
233
 
234
                public void actionPerformed(ActionEvent e) {
235
 
236
                    int answer = JOptionPane.showConfirmDialog(null, "Etes vous sûr de vouloir annuler la régularisation ?");
237
                    if (answer == JOptionPane.YES_OPTION) {
80 ilm 238
                        SQLRow row = IListe.get(e).getSelectedRow().asRow();
73 ilm 239
                        SQLRowValues rowVals = row.createEmptyUpdateRow();
240
                        rowVals.put("REG_COMPTA", Boolean.FALSE);
241
                        try {
242
                            rowVals.commit();
243
                        } catch (SQLException e1) {
244
                            // TODO Auto-generated catch block
245
                            e1.printStackTrace();
246
                        }
247
                    }
248
                }
249
            }, false) {
174 ilm 250
 
73 ilm 251
                @Override
93 ilm 252
                public boolean enabledFor(List<SQLRowValues> selection) {
73 ilm 253
                    if (selection != null && selection.size() == 1) {
254
                        SQLRowAccessor row = selection.get(0);
255
                        return row.getBoolean("REG_COMPTA");
256
                    } else {
257
                        return true;
258
                    }
259
                }
260
            };
261
            getRowActions().add(actionCancel);
262
 
263
            RowAction actionRegul = new RowAction(new AbstractAction("Régularisation en comptabilité") {
264
 
265
                public void actionPerformed(ActionEvent e) {
266
 
80 ilm 267
                    SQLRow row = IListe.get(e).fetchSelectedRow();
73 ilm 268
                    String price = GestionDevise.currencyToString(row.getLong("MONTANT"));
269
                    SQLRow rowClient = row.getForeignRow("ID_CLIENT");
270
                    String nomClient = rowClient.getString("NOM");
271
                    String piece = "";
272
                    SQLRow rowMvt = row.getForeignRow("ID_MOUVEMENT");
273
                    if (rowMvt != null) {
274
                        SQLRow rowPiece = rowMvt.getForeignRow("ID_PIECE");
275
                        piece = rowPiece.getString("NOM");
276
                    }
132 ilm 277
                    int answer = JOptionPane.showConfirmDialog(null,
278
                            "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 279
                    if (answer == JOptionPane.YES_OPTION) {
280
 
281
                        SQLRowValues rowVals = row.createEmptyUpdateRow();
282
                        rowVals.put("REG_COMPTA", Boolean.TRUE);
283
                        try {
284
                            rowVals.commit();
285
                        } catch (SQLException e1) {
286
                            // TODO Auto-generated catch block
287
                            e1.printStackTrace();
288
                        }
289
                    }
290
                }
291
            }, false) {
174 ilm 292
 
73 ilm 293
                @Override
93 ilm 294
                public boolean enabledFor(List<SQLRowValues> selection) {
73 ilm 295
                    if (selection != null && selection.size() == 1) {
296
                        SQLRowAccessor row = selection.get(0);
297
                        return !row.getBoolean("REG_COMPTA");
298
                    } else {
299
                        return true;
300
                    }
301
                }
174 ilm 302
 
73 ilm 303
            };
304
            getRowActions().add(actionRegul);
305
        }
306
 
174 ilm 307
        RowAction actionImport = new RowAction(new AbstractAction("Importer") {
308
 
309
            public void actionPerformed(ActionEvent e) {
310
 
311
                final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
312
                final FileDialog fd = new FileDialog(frame, "Import XML des réglements Sage", FileDialog.LOAD);
313
                if (fd.getFile() != null) {
314
                    try {
315
                        SQLUtils.executeAtomic(getTable().getDBSystemRoot().getDataSource(), new ConnectionHandlerNoSetup<Object, IOException>() {
316
                            @Override
317
                            public Object handle(final SQLDataSource ds) throws SQLException, IOException {
318
 
319
                                ImportReglementSage sageImporter = new ImportReglementSage(getDirectory().getElement(EncaisserMontantSQLElement.class));
320
                                try {
321
                                    sageImporter.importFromFile(new File(fd.getDirectory(), fd.getFile()));
322
                                } catch (Exception e) {
323
                                    e.printStackTrace();
324
                                    new SQLException(e);
325
                                }
326
                                return null;
327
                            }
328
 
329
                        });
330
                        JOptionPane.showMessageDialog(null, "Import des paiements terminés!");
331
                    } catch (IOException exn) {
332
                        ExceptionHandler.handle(frame, "Erreur lors de la lecture du fichier", exn);
333
                    } catch (SQLException exn) {
334
                        ExceptionHandler.handle(frame, "Erreur lors de l'insertion des paiements dans la base", exn);
335
                    }
336
 
337
                }
338
                fd.setVisible(true);
339
            }
340
        }, true) {
341
            @Override
342
            public boolean enabledFor(List<SQLRowValues> selection) {
343
                return true;
344
            }
345
        };
346
        getRowActions().add(actionImport);
347
 
18 ilm 348
    }
349
 
80 ilm 350
 
174 ilm 351
    private void sendMail(final SQLRow row, EmailTemplate template) throws Exception {
73 ilm 352
 
353
        SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
354
 
174 ilm 355
        int idMvtSource = MouvementSQLElement.getSourceId(row.getInt("ID_MOUVEMENT"));
356
        SQLRow rowMvtSource = base.getTable("MOUVEMENT").getRow(idMvtSource);
132 ilm 357
 
174 ilm 358
        if (!rowMvtSource.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
359
            return;
360
        }
361
        int idFact = rowMvtSource.getInt("IDSOURCE");
362
        SQLRow rowFacture = base.getTable("SAISIE_VENTE_FACTURE").getRow(idFact);
73 ilm 363
 
174 ilm 364
        final VenteFactureXmlSheet sheet = new VenteFactureXmlSheet(rowFacture);
73 ilm 365
 
174 ilm 366
        Set<SQLField> setContact = null;
367
        SQLTable tableContact = Configuration.getInstance().getRoot().findTable("CONTACT");
368
        setContact = row.getTable().getForeignKeys(tableContact);
132 ilm 369
 
174 ilm 370
        Set<SQLField> setClient = null;
371
        SQLTable tableClient = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("CLIENT");
372
        setClient = row.getTable().getForeignKeys(tableClient);
73 ilm 373
 
174 ilm 374
        // Récupération du mail du client
375
        String mail = "";
376
        for (SQLField field : setContact) {
377
            if (mail == null || mail.trim().length() == 0) {
378
                mail = row.getForeignRow(field.getName()).getString("EMAIL");
73 ilm 379
            }
174 ilm 380
        }
73 ilm 381
 
174 ilm 382
        for (SQLField field : setClient) {
383
            SQLRow rowCli = row.getForeignRow(field.getName());
384
            if (mail == null || mail.trim().length() == 0) {
385
                mail = rowCli.getString("MAIL");
73 ilm 386
            }
174 ilm 387
        }
388
        final String adresseMail = mail;
73 ilm 389
 
174 ilm 390
        MailRelanceCreator creator = new MailRelanceCreator(template, row);
391
        final String references = creator.getObject();
392
        final String text = creator.getValue();
132 ilm 393
 
174 ilm 394
        final File f;
73 ilm 395
 
174 ilm 396
        f = sheet.getOrCreatePDFDocumentFile(true);
132 ilm 397
 
174 ilm 398
        EmailComposer.getInstance().compose(adresseMail, references, text, f.getAbsoluteFile());
142 ilm 399
 
174 ilm 400
        // Création d'une relance
401
        String numero = NumerotationAutoSQLElement.getNextNumero(RelanceSQLElement.class);
402
        SQLRowValues rowValsR = new SQLRowValues(row.getTable().getTable("RELANCE"));
403
        rowValsR.put("DATE", new Date());
404
        rowValsR.put("NUMERO", numero);
405
        rowValsR.put("ID_CLIENT", row.getForeignID("ID_CLIENT"));
406
        rowValsR.put("ID_SAISIE_VENTE_FACTURE", row.getForeignID("ID_SAISIE_VENTE_FACTURE"));
407
        rowValsR.put("MONTANT", row.getObject("MONTANT"));
408
        rowValsR.put("INFOS", "Email");
409
        rowValsR.put("ID_ECHEANCE_CLIENT", row.getID());
132 ilm 410
 
174 ilm 411
        rowValsR.insert();
132 ilm 412
 
174 ilm 413
        SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
414
        SQLRowValues rowVals = new SQLRowValues(tableNum);
415
        int val = tableNum.getRow(2).getInt("RELANCE_START");
416
        val++;
417
        rowVals.put("RELANCE_START", Integer.valueOf(val));
132 ilm 418
 
174 ilm 419
        rowVals.update(2);
132 ilm 420
 
174 ilm 421
        // Incrémentation du nombre de relance
422
        int nbRelance = row.getInt("NOMBRE_RELANCE");
423
        nbRelance++;
132 ilm 424
 
174 ilm 425
        SQLRowValues rowValsEch = new SQLRowValues(row.getTable());
426
        rowValsEch.put("NOMBRE_RELANCE", nbRelance);
427
        rowValsEch.put("DATE_LAST_RELANCE", new Date());
132 ilm 428
 
174 ilm 429
        rowValsEch.update(row.getID());
132 ilm 430
 
73 ilm 431
    }
432
 
83 ilm 433
    @Override
132 ilm 434
    public ListMap<String, String> getShowAs() {
435
        ListMap<String, String> map = new ListMap<String, String>();
83 ilm 436
        return map;
437
    }
438
 
18 ilm 439
    protected List<String> getListFields() {
440
        final List<String> l = new ArrayList<String>();
441
 
83 ilm 442
 
443
            l.add("ID_SAISIE_VENTE_FACTURE");
444
            l.add("DATE");
445
            l.add("ID_CLIENT");
446
            l.add("MONTANT");
447
            l.add("ID_MOUVEMENT");
448
                l.add("NOMBRE_RELANCE");
449
            l.add("INFOS");
450
            l.add("DATE_LAST_RELANCE");
18 ilm 451
        return l;
452
    }
453
 
454
    protected List<String> getComboFields() {
455
        final List<String> l = new ArrayList<String>();
456
        l.add("DATE");
457
        l.add("ID_CLIENT");
458
        l.add("MONTANT");
459
        return l;
460
    }
461
 
462
    @Override
132 ilm 463
    protected void _initListRequest(ListSQLRequest req) {
464
        super._initListRequest(req);
180 ilm 465
        req.addToGraphToFetch("REG_COMPTA", "REGLE", "NOMBRE_RELANCE");
18 ilm 466
    }
467
 
468
    /*
469
     * (non-Javadoc)
470
     *
471
     * @see org.openconcerto.devis.SQLElement#getComponent()
472
     */
473
    public SQLComponent createComponent() {
474
        return new BaseSQLComponent(this) {
475
 
476
            private DeviseField montant;
477
            private JTextField nbRelance;
478
            private JDate date;
479
            private JTextField idMouvement;
480
            private ElementComboBox comboClient;
481
 
482
            public void addViews() {
483
                this.setLayout(new GridBagLayout());
484
                final GridBagConstraints c = new DefaultGridBagConstraints();
485
 
486
                this.montant = new DeviseField();
487
                this.nbRelance = new JTextField();
488
                this.date = new JDate();
489
                this.idMouvement = new JTextField();
490
                this.comboClient = new ElementComboBox();
491
 
492
                // Mouvement
493
                JLabel labelMouvement = new JLabel("Mouvement");
494
                this.add(labelMouvement, c);
495
 
496
                c.weightx = 1;
497
                c.gridx++;
498
                this.add(this.idMouvement, c);
499
 
500
                // Date
501
                JLabel labelDate = new JLabel("Date");
502
                c.gridx++;
503
                this.add(labelDate, c);
504
 
505
                c.gridx++;
506
                c.weightx = 1;
507
                this.add(this.date, c);
508
 
509
                // Client
510
                JLabel labelClient = new JLabel("Client");
511
                c.gridy++;
512
                c.gridx = 0;
513
 
514
                this.add(labelClient, c);
515
 
516
                c.gridx++;
517
                c.weightx = 1;
518
                c.gridwidth = GridBagConstraints.REMAINDER;
519
                this.add(this.comboClient, c);
520
 
521
                // libellé
522
                JLabel labelRelance = new JLabel("Nombre de relance");
523
                c.gridy++;
524
                c.gridx = 0;
525
                c.gridwidth = 1;
526
                this.add(labelRelance, c);
527
 
528
                c.gridx++;
529
                c.weightx = 1;
530
                this.add(this.nbRelance, c);
531
 
532
                // montant
533
                c.gridwidth = 1;
534
                JLabel labelMontant = new JLabel("Montant");
535
                c.gridx++;
536
                this.add(labelMontant, c);
537
 
538
                c.gridx++;
539
                c.weightx = 1;
540
                this.add(this.montant, c);
541
 
542
                this.addSQLObject(this.montant, "MONTANT");
543
                this.addRequiredSQLObject(this.date, "DATE");
544
                this.addSQLObject(this.nbRelance, "NOMBRE_RELANCE");
545
                this.addRequiredSQLObject(this.comboClient, "ID_CLIENT");
546
                this.addSQLObject(this.idMouvement, "ID_MOUVEMENT");
547
            }
548
        };
549
    }
550
 
57 ilm 551
    @Override
552
    protected String createCode() {
156 ilm 553
        return createCodeOfPackage() + ".commitment";
57 ilm 554
    }
555
 
177 ilm 556
    public void relanceClient(final SQLRow rowEch) {
557
        final SQLElement relanceElt = getDirectory().getElement("RELANCE");
558
 
559
        rowEch.fetchValues();
560
 
561
        if (rowEch != null) {
562
            int idMvtSource = MouvementSQLElement.getSourceId(rowEch.getForeignID("ID_MOUVEMENT"));
563
            SQLRow rowMvtSource = getTable().getTable("MOUVEMENT").getRow(idMvtSource);
564
 
565
            if (!rowMvtSource.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
180 ilm 566
                JOptionPane.showMessageDialog(null, "Impossible de relancer un échéance qui n'est pas issue d'une facture.");
177 ilm 567
                return;
568
            }
569
 
570
            EditFrame editRelance = new EditFrame(relanceElt);
571
            editRelance.setIconImages(Gestion.getFrameIcon());
572
            editRelance.addEditPanelListener(new EditPanelListener() {
573
 
574
                public void cancelled() {
575
                    // rien
576
                }
577
 
578
                public void modified() {
579
                    // rien
580
                }
581
 
582
                public void deleted() {
583
                    // rien
584
                }
585
 
586
                public void inserted(int id) {
587
                    int nbRelance = rowEch.getInt("NOMBRE_RELANCE");
588
                    nbRelance++;
589
 
590
                    SQLRowValues rowValsEch = new SQLRowValues(rowEch.getTable());
591
                    rowValsEch.put("NOMBRE_RELANCE", nbRelance);
592
                    rowValsEch.put("DATE_LAST_RELANCE", new Date());
593
 
594
                    try {
595
                        rowValsEch.update(rowEch.getID());
596
                        relanceElt.getTable().getRow(id).createEmptyUpdateRow().put("ID_ECHEANCE_CLIENT", rowEch.getID()).commit();
597
                    } catch (SQLException e1) {
598
                        ExceptionHandler.handle("erreur lors de la mise à jour du nombre de relances", e1);
599
                    }
600
                }
601
            });
602
 
603
            SQLRowValues rowVals = new SQLRowValues(relanceElt.getTable());
604
            rowVals.put("ID_SAISIE_VENTE_FACTURE", rowMvtSource.getInt("IDSOURCE"));
605
            rowVals.put("MONTANT", rowEch.getObject("MONTANT"));
606
            rowVals.put("ID_CLIENT", rowEch.getForeignID("ID_CLIENT"));
607
            rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(RelanceSQLElement.class, new Date()));
608
            editRelance.getSQLComponent().select(rowVals);
609
 
610
            editRelance.pack();
611
            editRelance.setVisible(true);
612
        }
613
 
614
    }
615
 
18 ilm 616
}