OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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