OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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